// Copyright (C) 2006 Erik Dahlberg
//
-// This file is part of LSystem3d.
+// This file is part of LSystem3D.
//
// LSystem3D is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
#include <cmath>
-#include <map>
#include <string>
#include "lindenmayersystem.h"
#include "model.h"
+#include "rule.h"
+#include "ruleset.h"
#include "turtle.h"
+using namespace std;
+
/**
_axiom.clear();
_rules.clear();
_turtle->setAngle(0.0);
- _numIterations = 0;
+ _depth = 0;
_turtle->reset();
_model->clear();
// model session
_model->begin();
_model->setColorIndex(0);
- recursiveWalk(_axiom, _numIterations);
+ recursiveWalk(_axiom.getPreprocessedContent(), _depth);
_model->end();
}
* Set the initial rule (the axiom)
* @param axiom the axiom
*/
-void LindenmayerSystem::setAxiom(string axiom)
+void LindenmayerSystem::setAxiom(Rule axiom)
{
- _axiom = verifyAndPreprocessRule(axiom);
+ _axiom = axiom;
}
/**
* Set one rule
- * @param name rule name
* @param rule the rule
*/
-void LindenmayerSystem::setRule(string name, string rule)
+void LindenmayerSystem::setRule(Rule rule)
{
- _rules[name] = verifyAndPreprocessRule(rule);
+ _rules.addRule(rule);
}
/**
- * Set the number of iterations
- * @param numIterations number of iterations
+ * Set depth of recursion
+ * @param depth depth of recursion
*/
-void LindenmayerSystem::setNumIterations(int numIterations)
+void LindenmayerSystem::setDepth(int depth)
{
- _numIterations = numIterations;
+ _depth = depth;
}
* Get the initial rule (the axiom)
* @return the axiom
*/
-string LindenmayerSystem::getAxiom()
+Rule LindenmayerSystem::getAxiom()
{
- return unpreprocessRule(_axiom);
+ return _axiom;
}
* Get all rules
* @return the rules
*/
-map<string,string> LindenmayerSystem::getRules()
+RuleSet LindenmayerSystem::getRules()
{
- map<string,string> theUnpreprocessedRules;
-
- // unpreprocess all rules
- map<string,string>::iterator currentRule;
- for (currentRule = _rules.begin(); currentRule != _rules.end(); currentRule++)
- {
- theUnpreprocessedRules[currentRule->first] = unpreprocessRule(currentRule->second);
- }
-
- return theUnpreprocessedRules;
+ return _rules;
}
/**
- * Get the number of iterations
- * @return number of iterations
+ * Get depth of recursion
+ * @return depth of recursion
*/
-int LindenmayerSystem::getNumIterations()
+int LindenmayerSystem::getDepth()
{
- return _numIterations;
+ return _depth;
}
// recursion
if (level > 0)
{
- char ruleName[] = {rule[i], '\0'};
- recursiveWalk(_rules[ruleName], level - 1);
+ string name = rule.substr(i, 1);
+ recursiveWalk(_rules.findRule(name).getPreprocessedContent(), level - 1);
}
break;
// walk / rule
case 'F':
- // replacement rule for 'F'?
- if (_rules.count("F") != 0 && level > 0)
{
- recursiveWalk(_rules["F"], level - 1);
- }
- else
- {
- // no rule for "F"
- _turtle->walk();
- }
-
+ // replacement rule for 'F'?
+ Rule rule = _rules.findRule("F");
+ if (rule.getProbability() != 0.0 && level > 0)
+ {
+ recursiveWalk(rule.getPreprocessedContent(), level - 1);
+ }
+ else
+ {
+ // no rule for "F"
+ _turtle->walk();
+ }
+ }
+
break;
break;
- // restore state from stack
+ // load state from stack
case ']':
_turtle->pop();
}
}
}
-
-
-
-/**
- * Verify and preprocess one rule string
- * @param rule the rule
- * @return the preprocessed rule
- */
-string LindenmayerSystem::verifyAndPreprocessRule(string rule)
-{
- // TODO: verifying
-
- string preprocessedRule;
-
- // check every element in rule
- for (int i = 0; i < rule.size(); i++)
- {
- // TODO: allow strings as names
- if (rule[i] >= 'A' && rule[i] <= 'Z' && rule[i] != 'F')
- {
- // add rule operator
- preprocessedRule += '@';
- }
- preprocessedRule += rule[i];
- }
-
- return preprocessedRule;
-}
-
-
-
-/**
- * Unpreprocess one rule string
- * @param rule the rule
- * @return the unpreprocessed rule
- */
-string LindenmayerSystem::unpreprocessRule(string rule)
-{
- string unpreprocessedRule;
-
- // check every element in rule
- for (int i = 0; i < rule.size(); i++)
- {
- // ignore rule operator
- if (rule[i] != '@')
- {
- unpreprocessedRule += rule[i];
- }
- }
-
- return unpreprocessedRule;
-}