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
45 * @param name name of rule
46 * @param content content of rule
47 * @param probability probability of rule
49 Rule::Rule(string name
, string content
, double probability
)
53 setProbability(probability
);
60 * @param ruleString the rule string
62 Rule::Rule(string ruleString
)
64 fromString(ruleString
);
91 * Construct rule from string
92 * @param ruleString the rule string
94 void Rule::fromString(string ruleString
)
97 setName(ruleString
.substr(0, 1));
100 // probability factor
104 if (ruleString
[1] == '=')
106 // use default probability
109 else if (ruleString
[1] == '(')
112 while (ruleString
[i
] != ')')
117 setProbability(strtod(string(ruleString
, 2, i
- 2).c_str(), NULL
));
124 setContent(string(ruleString
, i
));
130 * Construct rule string
131 * @return the rule string
133 string
Rule::toString()
136 string rulesString
= getName();
139 // probability factor
141 if (_probability
> 0.0 && _probability
< 1.0)
143 ostringstream probabilityString
;
144 probabilityString
<< getProbability();
148 rulesString
+= string(probabilityString
.str(), 0, 4);
157 rulesString
+= getContent();
167 * @param name the name
169 void Rule::setName(string name
)
178 * @param content the content
180 void Rule::setContent(string content
)
182 string preprocessedRule
;
184 // preprocess the rule content
185 for (int i
= 0; i
< content
.size(); i
++)
187 // TODO: allow string as name
188 if (content
[i
] >= 'A' && content
[i
] <= 'Z' && content
[i
] != 'F')
191 preprocessedRule
+= '@';
193 preprocessedRule
+= content
[i
];
196 _content
= preprocessedRule
;
202 * Set rule probability
203 * @param probability the probability factor
205 void Rule::setProbability(double probability
)
207 if (probability
>= 0 && probability
<= 1)
209 _probability
= probability
;
221 * @return the rule name
223 string
Rule::getName()
232 * @return the rule content
234 string
Rule::getContent()
236 string unpreprocessedRule
;
238 // unpreprocess the rule content
239 for (int i
= 0; i
< _content
.size(); i
++)
241 if (_content
[i
] != '@')
243 unpreprocessedRule
+= _content
[i
];
247 return unpreprocessedRule
;
253 * Get rule probability
254 * @return the probability
256 double Rule::getProbability()
264 * Get preprocessed content
265 * @return the preprocessed content
267 string
Rule::getPreprocessedContent()