Initial revision
[lsystem3d.git] / lsystem3d / src / lindenmayersystem.h
diff --git a/lsystem3d/src/lindenmayersystem.h b/lsystem3d/src/lindenmayersystem.h
new file mode 100644 (file)
index 0000000..efadab9
--- /dev/null
@@ -0,0 +1,140 @@
+// Copyright (C) 2006 Erik Dahlberg
+// 
+// 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
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+// 
+// LSystem3D is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with LSystem3D; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+
+
+#ifndef LINDENMAYERSYSTEM_H
+#define LINDENMAYERSYSTEM_H
+
+#include <map>
+#include <string>
+
+#include "model.h"
+#include "turtle.h"
+
+using namespace std;
+
+
+
+/**
+ * Lindenmayer System
+ */
+class LindenmayerSystem
+{
+public:
+    /**
+     * Constructor
+     * @param model the model for generation
+     */
+    LindenmayerSystem(Model *model);
+
+    /**
+     * Destructor
+     */
+    ~LindenmayerSystem();
+    
+    /**
+     * Set the initial rule (the axiom)
+     * @param axiom the axiom
+     */
+    void setAxiom(string axiom);
+    
+    /**
+     * Set one rule
+     * @param name rule name
+     * @param rule the rule
+     */
+    void setRule(string name, string rule);
+    
+    /**
+     * Set the turn/pitch/roll angle
+     * @param degrees the angle, in degrees
+     */
+    void setAngle(double degrees);
+    
+    /**
+     * Set the number of iterations
+     * @param numIterations number of iterations
+     */
+    void setNumIterations(int numIterations);
+    
+    /**
+     * Get the initial rule (the axiom)
+     * @return the axiom
+     */
+    string getAxiom();
+    
+    /**
+     * Get all rules
+     * @return the rules
+     */
+    map<string,string> getRules();
+    
+    /**
+     * Get the turn/pitch/roll angle
+     * @return the angle, in degrees
+     */
+    double getAngle();
+    
+    /**
+     * Get the number of iterations
+     * @return number of iterations
+     */
+    int getNumIterations();
+    
+    /**
+     * Generate l-system data
+     */
+    void generate();
+    
+protected:
+    
+    /**
+     * Walk through the rule string for specified number of levels
+     * @param rule the rule
+     * @param level current iteration level
+     */
+    void recursiveWalk(string rule, int level);
+    
+    /**
+     * Verify and preprocess one rule string
+     * @param ruleOrAxiom the rule
+     * @return the preprocessed rule
+     */
+    string verifyAndPreprocessRule(string ruleOrAxiom);
+    
+    /**
+     * Unpreprocess one rule string
+     * @param ruleOrAxiom the rule
+     * @return the unpreprocessed rule
+     */
+    string unpreprocessRule(string ruleOrAxiom);
+    
+    // Parameters
+    string _axiom;
+    map<string,string> _rules;      // TODO: use unsorted container?
+    int _numIterations;
+    
+    Model *_model;      // The model for generation
+    Turtle *_turtle;    // The rendering turtle
+};
+
+
+
+#endif