1 // Copyright (C) 2006 Erik Dahlberg
3 // This file is part of LSystem3D.
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // LSystem3D is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lindenmayersystem.h"
38 * @param model empty model
40 LindenmayerSystem::LindenmayerSystem(Model
*model
)
43 _turtle
= new Turtle(model
);
53 LindenmayerSystem::~LindenmayerSystem()
61 * Clear all parameters
63 void LindenmayerSystem::clear()
67 _turtle
->setAngle(0.0);
77 * Generate L-system data
79 void LindenmayerSystem::generate()
83 _model
->setColorIndex(0);
84 recursiveWalk(_axiom
.getPreprocessedContent(), _depth
);
91 * Render L-system data
93 void LindenmayerSystem::render()
101 * Set the initial rule (the axiom)
102 * @param axiom the axiom
104 void LindenmayerSystem::setAxiom(Rule axiom
)
113 * @param rule the rule
115 void LindenmayerSystem::setRule(Rule rule
)
117 _rules
.addRule(rule
);
123 * Set the turn/pitch/roll angle
124 * @param degrees the angle, in degrees
126 void LindenmayerSystem::setAngle(double degrees
)
128 // convert from degrees to radians
129 double radians
= (degrees
/ 360.0) * (2.0 * M_PIl
);
131 _turtle
->setAngle(radians
);
137 * Set recursion depth
138 * @param depth depth of recursion
140 void LindenmayerSystem::setDepth(int depth
)
148 * Set initial segment diameter
149 * @param diameter the diameter
151 void LindenmayerSystem::setDiameter(double diameter
)
153 _segmentDiameter
= diameter
;
154 _model
->setDiameter(diameter
);
160 * Set segment diameter factor
161 * @param diameterFactor the diameter factor
163 void LindenmayerSystem::setDiameterFactor(double diameterFactor
)
165 _model
->setDiameterFactor(diameterFactor
);
171 * Get the initial rule (the axiom)
174 Rule
LindenmayerSystem::getAxiom()
185 RuleSet
LindenmayerSystem::getRules()
193 * Get the turn/pitch/roll angle
194 * @return the angle, in degrees
196 double LindenmayerSystem::getAngle()
198 double radians
= _turtle
->getAngle();
200 // convert from radians to degrees
201 double degrees
= 360.0 * (radians
/ (2.0 * M_PIl
));
209 * Get recursion depth
210 * @return depth of recursion
212 int LindenmayerSystem::getDepth()
220 * Get initial segment diameter
221 * @return the diameter
223 double LindenmayerSystem::getDiameter()
225 return _segmentDiameter
;
231 * Get segment diameter factor
232 * @return the diameter factor
234 double LindenmayerSystem::getDiameterFactor()
236 return _model
->getDiameterFactor();
242 * Recursively apply the replacement rules
243 * @param rule the rule
244 * @param level recursion level
246 void LindenmayerSystem::recursiveWalk(string rule
, int level
)
248 // process every element in the rule string
249 for (int i
= 0; i
< rule
.size(); i
++)
256 // ignore marker, i.e. the "@"
262 string name
= rule
.substr(i
, 1);
263 recursiveWalk(_rules
.findRule(name
).getPreprocessedContent(), level
- 1);
273 // replacement rule for 'F'?
274 Rule rule
= _rules
.findRule("F");
275 if (rule
.getProbability() != 0.0 && level
> 0)
277 recursiveWalk(rule
.getPreprocessedContent(), level
- 1);
299 _turtle
->turnRight();
306 _turtle
->pitchDown();
327 _turtle
->rollRight();
331 // turn around 180 degrees
334 _turtle
->turnAround();
338 // save state to stack
345 // load state from stack
352 // create a filled surface
359 // close a filled surface
366 // one vertex in a filled surface
373 // decrement segment diameter
376 _model
->decrementDiameter();
380 // increment current index to color table
387 // decrement current index to color table