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
24 #include <glibmm/stringutils.h>
44 * @param name name of rule
45 * @param content content of rule
46 * @param probability probability of rule
48 Rule::Rule(string name
, string content
, double probability
)
52 setProbability(probability
);
59 * @param ruleString the rule string
61 Rule::Rule(string ruleString
)
63 fromString(ruleString
);
90 * Construct rule from string
91 * @param ruleString the rule string
93 void Rule::fromString(string ruleString
)
96 setName(ruleString
.substr(0, 1));
103 if (ruleString
[1] == '=')
105 // use default probability
108 else if (ruleString
[1] == '(')
111 while (ruleString
[i
] != ')')
115 setProbability(Glib::Ascii::strtod(string(ruleString
, 2, i
- 2)));
122 setContent(string(ruleString
, i
));
128 * Construct rule string
129 * @return the rule string
131 string
Rule::toString()
134 string rulesString
= getName();
137 // probability factor
139 if (_probability
!= 0.0)
142 string
probabilityString(Glib::Ascii::dtostr(getProbability()), 0, 4);
145 rulesString
+= probabilityString
;
154 rulesString
+= getContent();
164 * @param name the name
166 void Rule::setName(string name
)
175 * @param content the content
177 void Rule::setContent(string content
)
179 string preprocessedRule
;
181 // preprocess the rule content
182 for (int i
= 0; i
< content
.size(); i
++)
184 // TODO: allow string as name
185 if (content
[i
] >= 'A' && content
[i
] <= 'Z' && content
[i
] != 'F')
188 preprocessedRule
+= '@';
190 preprocessedRule
+= content
[i
];
193 _content
= preprocessedRule
;
199 * Set rule probability
200 * @param probability the probability factor
202 void Rule::setProbability(double probability
)
204 if (probability
>= 0 && probability
<= 1)
206 _probability
= probability
;
218 * @return the rule name
220 string
Rule::getName()
229 * @return the rule content
231 string
Rule::getContent()
233 string unpreprocessedRule
;
235 // unpreprocess the rule content
236 for (int i
= 0; i
< _content
.size(); i
++)
238 if (_content
[i
] != '@')
240 unpreprocessedRule
+= _content
[i
];
244 return unpreprocessedRule
;
250 * Get rule probability
251 * @return the probability
253 double Rule::getProbability()
261 * Get preprocessed content
262 * @return the preprocessed content
264 string
Rule::getPreprocessedContent()