| 1 | // Copyright (C) 2006 Erik Dahlberg |
| 2 | // |
| 3 | // This file is part of LSystem3d. |
| 4 | // |
| 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. |
| 9 | // |
| 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. |
| 14 | // |
| 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 |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | #ifndef LINDENMAYERSYSTEM_H |
| 23 | #define LINDENMAYERSYSTEM_H |
| 24 | |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | |
| 28 | #include "model.h" |
| 29 | #include "turtle.h" |
| 30 | |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Lindenmayer System |
| 35 | */ |
| 36 | class LindenmayerSystem |
| 37 | { |
| 38 | public: |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * @param model empty model |
| 43 | */ |
| 44 | LindenmayerSystem(Model *model); |
| 45 | |
| 46 | /** |
| 47 | * Destructor |
| 48 | */ |
| 49 | ~LindenmayerSystem(); |
| 50 | |
| 51 | /** |
| 52 | * Clear all parameters |
| 53 | */ |
| 54 | void clear(); |
| 55 | |
| 56 | /** |
| 57 | * Generate L-system data |
| 58 | */ |
| 59 | void generate(); |
| 60 | |
| 61 | /** |
| 62 | * Set the initial rule (the axiom) |
| 63 | * @param axiom the axiom |
| 64 | */ |
| 65 | void setAxiom(string axiom); |
| 66 | |
| 67 | /** |
| 68 | * Set one rule |
| 69 | * @param name rule name |
| 70 | * @param rule the rule |
| 71 | */ |
| 72 | void setRule(string name, string rule); |
| 73 | |
| 74 | /** |
| 75 | * Set the turn/pitch/roll angle |
| 76 | * @param degrees the angle, in degrees |
| 77 | */ |
| 78 | void setAngle(double degrees); |
| 79 | |
| 80 | /** |
| 81 | * Set the number of iterations |
| 82 | * @param numIterations number of iterations |
| 83 | */ |
| 84 | void setNumIterations(int numIterations); |
| 85 | |
| 86 | /** |
| 87 | * Set diameter of segment |
| 88 | * @param diameter the diameter |
| 89 | */ |
| 90 | void setDiameter(double diameter); |
| 91 | |
| 92 | /** |
| 93 | * Set diameter factor |
| 94 | * @param diameterFactor the diameter factor |
| 95 | */ |
| 96 | void setDiameterFactor(double diameterFactor); |
| 97 | |
| 98 | /** |
| 99 | * Get the initial rule (the axiom) |
| 100 | * @return the axiom |
| 101 | */ |
| 102 | string getAxiom(); |
| 103 | |
| 104 | /** |
| 105 | * Get all rules |
| 106 | * @return the rules |
| 107 | */ |
| 108 | map<string,string> getRules(); |
| 109 | |
| 110 | /** |
| 111 | * Get the turn/pitch/roll angle |
| 112 | * @return the angle, in degrees |
| 113 | */ |
| 114 | double getAngle(); |
| 115 | |
| 116 | /** |
| 117 | * Get the number of iterations |
| 118 | * @return number of iterations |
| 119 | */ |
| 120 | int getNumIterations(); |
| 121 | |
| 122 | /** |
| 123 | * Get the generated model |
| 124 | * @return generated model |
| 125 | */ |
| 126 | Model *getModel(); |
| 127 | |
| 128 | protected: |
| 129 | |
| 130 | /** |
| 131 | * Recursively apply the replacement rules |
| 132 | * @param rule rule |
| 133 | * @param level recursion level |
| 134 | */ |
| 135 | void recursiveWalk(string rule, int level); |
| 136 | |
| 137 | /** |
| 138 | * Verify and preprocess one rule string |
| 139 | * @param rule the rule |
| 140 | * @return the preprocessed rule |
| 141 | */ |
| 142 | string verifyAndPreprocessRule(string rule); |
| 143 | |
| 144 | /** |
| 145 | * Unpreprocess one rule string |
| 146 | * @param rule the rule |
| 147 | * @return the unpreprocessed rule |
| 148 | */ |
| 149 | string unpreprocessRule(string rule); |
| 150 | |
| 151 | // Parameters |
| 152 | string _axiom; |
| 153 | map<string,string> _rules; // TODO: use unsorted container? |
| 154 | int _numIterations; |
| 155 | |
| 156 | Model *_model; // The active model |
| 157 | Turtle *_turtle; // The rendering turtle |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | |
| 162 | #endif |