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
64 * @param rule the rule
66 void RuleSet::addRule(Rule rule
)
68 _rules
.insert(make_pair(rule
.getName(), rule
));
74 * Get one rule with repect of probability factor
75 * @param name the rule name
78 Rule
RuleSet::findRule(string ruleName
)
83 int numRules
= _rules
.count(ruleName
);
88 // no rule with this name
89 selectedRule
.setProbability(0.0);
91 else if (numRules
== 1)
93 // only one rule with this name
94 selectedRule
= _rules
.find(ruleName
)->second
;
98 // many rules with this name
104 int ranges
[numRules
+ 1];
106 double lastRange
= ranges
[0] = 0;
109 // TODO: move to addRule()
110 pair
<rulemap::iterator
, rulemap::iterator
> interval
= _rules
.equal_range(ruleName
);
111 for (rulemap::iterator it
= interval
.first
; it
!= interval
.second
; it
++)
113 // calculate and store ranges
114 lastRange
+= it
->second
.getProbability() * RAND_MAX
;
115 ranges
[index
++] = (int)lastRange
;
118 ranges
[numRules
] = RAND_MAX
;
121 // choose random range
126 for (int i
= 0; i
< numRules
; i
++)
128 if (random
>= ranges
[i
] && random
<= ranges
[i
+ 1])
140 rulemap::iterator it
= interval
.first
;
141 for (int i
= 0; i
< selection
; i
++)
145 selectedRule
= it
->second
;
158 rulemap
RuleSet::getRules()