Minor cleanup.
[lsystem3d.git] / src / rule.cpp
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 #include <string>
23
24 #include <glibmm/stringutils.h>
25
26 #include "rule.h"
27
28 using namespace std;
29
30
31
32 /**
33 * Constructor
34 */
35 Rule::Rule()
36 {
37 clear();
38 }
39
40
41
42 /**
43 * Constructor
44 * @param name name of rule
45 * @param content content of rule
46 * @param probability probability of rule
47 */
48 Rule::Rule(string name, string content, double probability)
49 {
50 setName(name);
51 setContent(content);
52 setProbability(probability);
53 }
54
55
56
57 /**
58 * Constructor
59 * @param ruleString the rule string
60 */
61 Rule::Rule(string ruleString)
62 {
63 fromString(ruleString);
64 }
65
66
67
68 /**
69 * Destructor
70 */
71 Rule::~Rule()
72 {
73 }
74
75
76
77 /**
78 * Clear rule
79 */
80 void Rule::clear()
81 {
82 _name.clear();
83 _content.clear();
84 _probability = 0.0;
85 }
86
87
88
89 /**
90 * Construct rule from string
91 * @param ruleString the rule string
92 */
93 void Rule::fromString(string ruleString)
94 {
95 // name
96 setName(ruleString.substr(0, 1));
97
98
99 // probability factor
100
101 int i = 2;
102
103 if (ruleString[1] == '=')
104 {
105 // use default probability
106 setProbability(1.0);
107 }
108 else if (ruleString[1] == '(')
109 {
110 // set probability
111 while (ruleString[i] != ')')
112 {
113 i++;
114 }
115 setProbability(Glib::Ascii::strtod(string(ruleString, 2, i - 2)));
116
117 // ignore the '='
118 i += 2;
119 }
120
121 // content
122 setContent(string(ruleString, i));
123 }
124
125
126
127 /**
128 * Construct rule string
129 * @return the rule string
130 */
131 string Rule::toString()
132 {
133 // name
134 string rulesString = getName();
135
136
137 // probability factor
138
139 if (_probability != 0.0)
140 {
141 // TODO: precision
142 string probabilityString(Glib::Ascii::dtostr(getProbability()), 0, 4);
143
144 rulesString += '(';
145 rulesString += probabilityString;
146 rulesString += ')';
147 }
148
149
150 rulesString += '=';
151
152
153 // content
154 rulesString += getContent();
155
156
157 return rulesString;
158 }
159
160
161
162 /**
163 * Set rule name
164 * @param name the name
165 */
166 void Rule::setName(string name)
167 {
168 _name = name;
169 }
170
171
172
173 /**
174 * Set rule content
175 * @param content the content
176 */
177 void Rule::setContent(string content)
178 {
179 string preprocessedRule;
180
181 // preprocess the rule content
182 for (int i = 0; i < content.size(); i++)
183 {
184 // TODO: allow string as name
185 if (content[i] >= 'A' && content[i] <= 'Z' && content[i] != 'F')
186 {
187 // add rule operator
188 preprocessedRule += '@';
189 }
190 preprocessedRule += content[i];
191 }
192
193 _content = preprocessedRule;
194 }
195
196
197
198 /**
199 * Set rule probability
200 * @param probability the probability factor
201 */
202 void Rule::setProbability(double probability)
203 {
204 if (probability >= 0 && probability <= 1)
205 {
206 _probability = probability;
207 }
208 else
209 {
210 probability = 0.0;
211 }
212 }
213
214
215
216 /**
217 * Get rule name
218 * @return the rule name
219 */
220 string Rule::getName()
221 {
222 return _name;
223 }
224
225
226
227 /**
228 * Get rule content
229 * @return the rule content
230 */
231 string Rule::getContent()
232 {
233 string unpreprocessedRule;
234
235 // unpreprocess the rule content
236 for (int i = 0; i < _content.size(); i++)
237 {
238 if (_content[i] != '@')
239 {
240 unpreprocessedRule += _content[i];
241 }
242 }
243
244 return unpreprocessedRule;
245 }
246
247
248
249 /**
250 * Get rule probability
251 * @return the probability
252 */
253 double Rule::getProbability()
254 {
255 return _probability;
256 }
257
258
259
260 /**
261 * Get preprocessed content
262 * @return the preprocessed content
263 */
264 string Rule::getPreprocessedContent()
265 {
266 return _content;
267 }