ROOT logo
// $Id: Glass_SKEL.cxx 2089 2008-11-23 20:31:03Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#include "GrowingPlant.h"
#include "GrowingPlant.c7"

// GrowingPlant

//______________________________________________________________________________
//
//


ClassImp(GrowingPlant);


void GrowingPlant::_init()
{}

GrowingPlant::GrowingPlant(const Text_t* n, const Text_t* t) :
ZNode(n, t),
mLevel(1),
mLineColor(1, 1, 0)
{
  _init();
  // bUseDispList  = true;
}

GrowingPlant::~GrowingPlant()
{}
//=================================================================================
void
GrowingPlant::SegmentListStepTime(SegmentList& oldExp, SegmentList& newExp, int level)
{
   bool debug = false;
  
  ++level; 
  //printf("duming new \n"); DumpList(newExp);
  int idx=0;
  for (Segments_i it = oldExp.begin(); it != oldExp.end(); ++it)
  {
    if (debug) printf("[%d] segment %c (%d, %d) step time \n", idx++, (*it).mType, (*it).mParam1, (*it).mParam2);
    SegmentStepTime(it, oldExp, newExp);
  }
  oldExp.swap(newExp);
  newExp.clear();
  
  if (level < mLevel)
  {
    if (debug)
    {
      printf("\n\n\n\ngrowing plant lsit step time for level %d sizes %d , %d \n", level, (int)oldExp.size(), (int)newExp.size());
      DumpList(oldExp);
    }
    SegmentListStepTime(oldExp, newExp, level);  
  }
}

void
GrowingPlant::Produce()
{
  // calback from triangulate method
   
  mSegments.clear();
  mSegments = mStart;
  
  // create starting expresion from start rule and init paramters
  if (mLevel > 1)
  {
    SegmentList refExp;
    SegmentListStepTime(mSegments, refExp, 1);
  }
   
  //  printf("finish Produce \n"); DumpInfo();

}


//==============================================================================

void GrowingPlant::DumpInfo()  
{
  // printf("Expression[%d]: \n", (int)mSegments.size());
  for (Segments_i i = mSegments.begin(); i != mSegments.end(); ++i)
  {
    printf("%c", (*i).mType);
  }      
  printf(" \n");
}

void GrowingPlant::DumpList(Segments_t& slist)  
{
  int nstack = 0;
  
  // printf("Expression[%d]: \n", (int)slist.size());
  for (Segments_i i = slist.begin(); i != slist.end(); ++i)
  {
    if ((*i).mType == '[') nstack++;
    if ((*i).mType == ']') nstack--;
    
    printf("%c", (*i).mType);
  }      
  printf(" \n");
  
  if (nstack) printf("!!!!!!!!!!!!!!!!!!stack %d \n", nstack);
}

//==============================================================================
//==============================================================================

GrowingPlant::Segments_i GrowingPlant::NeighbourFront( SegmentList& slist, Segments_i startIt, int depth, bool debug)
{
  static const Exc_t _eh("Weed_GL_Rnr::NeighbourFront ");  
  Segments_i startItOrig = startIt;
  ++startIt;
  
  if (debug) {
    printf("front neighbour-front search ");
    for (Segments_i i = startIt; i != slist.end(); ++i)
    {
      printf("%c", (*i).mType);
    }      
    printf(" \n");
  }
  
  int nStack = 0;  
  for (GrowingPlant::Segments_i i = startIt; i != slist.end(); ++i)
  {
    if ((*i).mType == '[')
    {
      --nStack;
    }
    else if ((*i).mType == ']')
    {
      if (depth == 0 && nStack == 0)  return slist.end();
      ++nStack;  
    }
    else 
    {
      if (nStack == depth && ((*i).mType == 'I' ||  (*i).mType == 'S' ||(*i).mType == 'F' ||(*i).mType == 'T' ||  (*i).mType == 'U' ))
      {
        if (debug) printf("found %c \n",  (*i).mType);
        return i;
      }
      else
      {
       if (debug) printf("skip %c \n",  (*i).mType);
      }
    }
  }

  ISerr(_eh + Form(" [%c] 'it == end.", (*startItOrig).mType));
  
  return slist.end();
}
//_____________________________________________________________________________________________

GrowingPlant::Segments_i GrowingPlant::NeighbourBack(SegmentList& slist, Segments_i startIt, int depth, bool debug)
{
  if (startIt == slist.begin()) return slist.end(); 
  
  static const Exc_t _eh("Weed_GL_Rnr::NeighbourBack ");
  char t = (*startIt).mType;
  if (debug)
  {
    printf("back neighbour-back search ");
    for (Segments_i j = startIt; j != slist.begin(); --j)
    {
      Segments_i i = j; 
      --i;
      printf("%c", (*i).mType);
    }      
    printf(" \n");
  }
  
  
  int nStack = 0;  
  for (GrowingPlant::Segments_i j = startIt; j != slist.begin(); --j)
  {
    
    Segments_i i = j;
    --i;
    if ((*i).mType == '[')
    {
      if (depth == 0 && nStack == 0) return slist.end();
      
      --nStack;
      if (debug) printf("increase stack \n");
    }
    else if ((*i).mType == ']')
    {
      ++nStack;  
      if (debug) printf("decrease stack \n");
    }
    else if (nStack == depth)
    {
      if ( (*i).mType == 'I' ||  (*i).mType == 'S'||(*i).mType == 'F'||(*i).mType == 'T' ||  (*i).mType == 'U' )
      {
        if (debug) printf("found back %c for %c\n", (*i).mType, t);
        return i;
      }
      else 
      {
        if (debug) printf("skip %c \n",  (*i).mType);
      }
    }
  }
  // printf( "found back %c not it == begin.\n",t );
  return slist.end();
}


//=============================================================================
void GrowingPlant::StepPlus()  
{
  SetLevel(++mLevel);
}

void GrowingPlant::StepMinus()  
{
  if (mLevel > 1)
    SetLevel(--mLevel);
}

//=============================================================================
GrowingPlant::SegmentList::SegmentList():
mChunkSize(1000)
{
  reserve(1000);
}

GrowingPlant::SegmentList&
GrowingPlant::SegmentList::s(char t, int v1, int v2)
{
  // Set defualt parameters. 
  // printf("sed defults %c (%d, %d) \n", t, v1, v2);
  for (GrowingPlant::Segments_i i = mDefaults.begin(); i != mDefaults.end(); ++i)
  {
    if ((*i).mType == t)
    {
      (*i).mParam1 = v1;
      (*i).mParam2 = v2;
      return *this;
    }
  }
  
  mDefaults.push_back(Segment(t, v1, v2));
  return *this;
}

GrowingPlant::SegmentList&
GrowingPlant::SegmentList::x(char t, int p1, int p2)
{
  push_back(Segment(t, p1, p2));
  //printf("add char/value %c %d \n", t, v);
  return *this;
}

GrowingPlant::SegmentList&
GrowingPlant::SegmentList::x(const char* nodes)
{
  for (size_t i = 0, e = strlen(nodes); i < e; ++i)
  {
    int param1 = -1;
    int param2 = -1;
    for  (GrowingPlant::Segments_i d = mDefaults.begin(); d != mDefaults.end(); ++d)
    {
      if ((*d).mType == nodes[i])
      {
        param1 = (*d).mParam1;
        param2 = (*d).mParam2;
        break;
      }
    }
    push_back(Segment(nodes[i], param1, param2));
  }
  
  return *this;
}
 GrowingPlant.cxx:1
 GrowingPlant.cxx:2
 GrowingPlant.cxx:3
 GrowingPlant.cxx:4
 GrowingPlant.cxx:5
 GrowingPlant.cxx:6
 GrowingPlant.cxx:7
 GrowingPlant.cxx:8
 GrowingPlant.cxx:9
 GrowingPlant.cxx:10
 GrowingPlant.cxx:11
 GrowingPlant.cxx:12
 GrowingPlant.cxx:13
 GrowingPlant.cxx:14
 GrowingPlant.cxx:15
 GrowingPlant.cxx:16
 GrowingPlant.cxx:17
 GrowingPlant.cxx:18
 GrowingPlant.cxx:19
 GrowingPlant.cxx:20
 GrowingPlant.cxx:21
 GrowingPlant.cxx:22
 GrowingPlant.cxx:23
 GrowingPlant.cxx:24
 GrowingPlant.cxx:25
 GrowingPlant.cxx:26
 GrowingPlant.cxx:27
 GrowingPlant.cxx:28
 GrowingPlant.cxx:29
 GrowingPlant.cxx:30
 GrowingPlant.cxx:31
 GrowingPlant.cxx:32
 GrowingPlant.cxx:33
 GrowingPlant.cxx:34
 GrowingPlant.cxx:35
 GrowingPlant.cxx:36
 GrowingPlant.cxx:37
 GrowingPlant.cxx:38
 GrowingPlant.cxx:39
 GrowingPlant.cxx:40
 GrowingPlant.cxx:41
 GrowingPlant.cxx:42
 GrowingPlant.cxx:43
 GrowingPlant.cxx:44
 GrowingPlant.cxx:45
 GrowingPlant.cxx:46
 GrowingPlant.cxx:47
 GrowingPlant.cxx:48
 GrowingPlant.cxx:49
 GrowingPlant.cxx:50
 GrowingPlant.cxx:51
 GrowingPlant.cxx:52
 GrowingPlant.cxx:53
 GrowingPlant.cxx:54
 GrowingPlant.cxx:55
 GrowingPlant.cxx:56
 GrowingPlant.cxx:57
 GrowingPlant.cxx:58
 GrowingPlant.cxx:59
 GrowingPlant.cxx:60
 GrowingPlant.cxx:61
 GrowingPlant.cxx:62
 GrowingPlant.cxx:63
 GrowingPlant.cxx:64
 GrowingPlant.cxx:65
 GrowingPlant.cxx:66
 GrowingPlant.cxx:67
 GrowingPlant.cxx:68
 GrowingPlant.cxx:69
 GrowingPlant.cxx:70
 GrowingPlant.cxx:71
 GrowingPlant.cxx:72
 GrowingPlant.cxx:73
 GrowingPlant.cxx:74
 GrowingPlant.cxx:75
 GrowingPlant.cxx:76
 GrowingPlant.cxx:77
 GrowingPlant.cxx:78
 GrowingPlant.cxx:79
 GrowingPlant.cxx:80
 GrowingPlant.cxx:81
 GrowingPlant.cxx:82
 GrowingPlant.cxx:83
 GrowingPlant.cxx:84
 GrowingPlant.cxx:85
 GrowingPlant.cxx:86
 GrowingPlant.cxx:87
 GrowingPlant.cxx:88
 GrowingPlant.cxx:89
 GrowingPlant.cxx:90
 GrowingPlant.cxx:91
 GrowingPlant.cxx:92
 GrowingPlant.cxx:93
 GrowingPlant.cxx:94
 GrowingPlant.cxx:95
 GrowingPlant.cxx:96
 GrowingPlant.cxx:97
 GrowingPlant.cxx:98
 GrowingPlant.cxx:99
 GrowingPlant.cxx:100
 GrowingPlant.cxx:101
 GrowingPlant.cxx:102
 GrowingPlant.cxx:103
 GrowingPlant.cxx:104
 GrowingPlant.cxx:105
 GrowingPlant.cxx:106
 GrowingPlant.cxx:107
 GrowingPlant.cxx:108
 GrowingPlant.cxx:109
 GrowingPlant.cxx:110
 GrowingPlant.cxx:111
 GrowingPlant.cxx:112
 GrowingPlant.cxx:113
 GrowingPlant.cxx:114
 GrowingPlant.cxx:115
 GrowingPlant.cxx:116
 GrowingPlant.cxx:117
 GrowingPlant.cxx:118
 GrowingPlant.cxx:119
 GrowingPlant.cxx:120
 GrowingPlant.cxx:121
 GrowingPlant.cxx:122
 GrowingPlant.cxx:123
 GrowingPlant.cxx:124
 GrowingPlant.cxx:125
 GrowingPlant.cxx:126
 GrowingPlant.cxx:127
 GrowingPlant.cxx:128
 GrowingPlant.cxx:129
 GrowingPlant.cxx:130
 GrowingPlant.cxx:131
 GrowingPlant.cxx:132
 GrowingPlant.cxx:133
 GrowingPlant.cxx:134
 GrowingPlant.cxx:135
 GrowingPlant.cxx:136
 GrowingPlant.cxx:137
 GrowingPlant.cxx:138
 GrowingPlant.cxx:139
 GrowingPlant.cxx:140
 GrowingPlant.cxx:141
 GrowingPlant.cxx:142
 GrowingPlant.cxx:143
 GrowingPlant.cxx:144
 GrowingPlant.cxx:145
 GrowingPlant.cxx:146
 GrowingPlant.cxx:147
 GrowingPlant.cxx:148
 GrowingPlant.cxx:149
 GrowingPlant.cxx:150
 GrowingPlant.cxx:151
 GrowingPlant.cxx:152
 GrowingPlant.cxx:153
 GrowingPlant.cxx:154
 GrowingPlant.cxx:155
 GrowingPlant.cxx:156
 GrowingPlant.cxx:157
 GrowingPlant.cxx:158
 GrowingPlant.cxx:159
 GrowingPlant.cxx:160
 GrowingPlant.cxx:161
 GrowingPlant.cxx:162
 GrowingPlant.cxx:163
 GrowingPlant.cxx:164
 GrowingPlant.cxx:165
 GrowingPlant.cxx:166
 GrowingPlant.cxx:167
 GrowingPlant.cxx:168
 GrowingPlant.cxx:169
 GrowingPlant.cxx:170
 GrowingPlant.cxx:171
 GrowingPlant.cxx:172
 GrowingPlant.cxx:173
 GrowingPlant.cxx:174
 GrowingPlant.cxx:175
 GrowingPlant.cxx:176
 GrowingPlant.cxx:177
 GrowingPlant.cxx:178
 GrowingPlant.cxx:179
 GrowingPlant.cxx:180
 GrowingPlant.cxx:181
 GrowingPlant.cxx:182
 GrowingPlant.cxx:183
 GrowingPlant.cxx:184
 GrowingPlant.cxx:185
 GrowingPlant.cxx:186
 GrowingPlant.cxx:187
 GrowingPlant.cxx:188
 GrowingPlant.cxx:189
 GrowingPlant.cxx:190
 GrowingPlant.cxx:191
 GrowingPlant.cxx:192
 GrowingPlant.cxx:193
 GrowingPlant.cxx:194
 GrowingPlant.cxx:195
 GrowingPlant.cxx:196
 GrowingPlant.cxx:197
 GrowingPlant.cxx:198
 GrowingPlant.cxx:199
 GrowingPlant.cxx:200
 GrowingPlant.cxx:201
 GrowingPlant.cxx:202
 GrowingPlant.cxx:203
 GrowingPlant.cxx:204
 GrowingPlant.cxx:205
 GrowingPlant.cxx:206
 GrowingPlant.cxx:207
 GrowingPlant.cxx:208
 GrowingPlant.cxx:209
 GrowingPlant.cxx:210
 GrowingPlant.cxx:211
 GrowingPlant.cxx:212
 GrowingPlant.cxx:213
 GrowingPlant.cxx:214
 GrowingPlant.cxx:215
 GrowingPlant.cxx:216
 GrowingPlant.cxx:217
 GrowingPlant.cxx:218
 GrowingPlant.cxx:219
 GrowingPlant.cxx:220
 GrowingPlant.cxx:221
 GrowingPlant.cxx:222
 GrowingPlant.cxx:223
 GrowingPlant.cxx:224
 GrowingPlant.cxx:225
 GrowingPlant.cxx:226
 GrowingPlant.cxx:227
 GrowingPlant.cxx:228
 GrowingPlant.cxx:229
 GrowingPlant.cxx:230
 GrowingPlant.cxx:231
 GrowingPlant.cxx:232
 GrowingPlant.cxx:233
 GrowingPlant.cxx:234
 GrowingPlant.cxx:235
 GrowingPlant.cxx:236
 GrowingPlant.cxx:237
 GrowingPlant.cxx:238
 GrowingPlant.cxx:239
 GrowingPlant.cxx:240
 GrowingPlant.cxx:241
 GrowingPlant.cxx:242
 GrowingPlant.cxx:243
 GrowingPlant.cxx:244
 GrowingPlant.cxx:245
 GrowingPlant.cxx:246
 GrowingPlant.cxx:247
 GrowingPlant.cxx:248
 GrowingPlant.cxx:249
 GrowingPlant.cxx:250
 GrowingPlant.cxx:251
 GrowingPlant.cxx:252
 GrowingPlant.cxx:253
 GrowingPlant.cxx:254
 GrowingPlant.cxx:255
 GrowingPlant.cxx:256
 GrowingPlant.cxx:257
 GrowingPlant.cxx:258
 GrowingPlant.cxx:259
 GrowingPlant.cxx:260
 GrowingPlant.cxx:261
 GrowingPlant.cxx:262
 GrowingPlant.cxx:263
 GrowingPlant.cxx:264
 GrowingPlant.cxx:265
 GrowingPlant.cxx:266
 GrowingPlant.cxx:267
 GrowingPlant.cxx:268
 GrowingPlant.cxx:269
 GrowingPlant.cxx:270
 GrowingPlant.cxx:271
 GrowingPlant.cxx:272
 GrowingPlant.cxx:273
 GrowingPlant.cxx:274
 GrowingPlant.cxx:275
 GrowingPlant.cxx:276
 GrowingPlant.cxx:277
 GrowingPlant.cxx:278
 GrowingPlant.cxx:279
 GrowingPlant.cxx:280
 GrowingPlant.cxx:281
 GrowingPlant.cxx:282