15c82487 |
1 | // Copyright (C) 2006 Erik Dahlberg |
2 | // |
7cd25edc |
3 | // This file is part of LSystem3D. |
15c82487 |
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 | |
b983918b |
22 | #include <cstdlib> |
23 | |
15c82487 |
24 | #include <iostream> |
25 | #include <string> |
26 | |
27 | #include "lsystemparameters.h" |
7cd25edc |
28 | #include "rule.h" |
29 | #include "ruleset.h" |
15c82487 |
30 | |
31 | using namespace std; |
32 | |
33 | |
34 | |
35 | /** |
36 | * Constructor |
37 | */ |
38 | LSystemParameters::LSystemParameters() |
39 | { |
40 | } |
41 | |
42 | |
43 | |
44 | /** |
45 | * Destructor |
46 | */ |
47 | LSystemParameters::~LSystemParameters() |
48 | { |
49 | } |
50 | |
51 | |
52 | |
53 | /** |
54 | * Load parameters from xml file |
526db675 |
55 | * @param lsystem put parameters into this L-system |
15c82487 |
56 | * @param path path to xml file |
57 | */ |
58 | void LSystemParameters::load(LindenmayerSystem *lsystem, string path) |
59 | { |
60 | if (lsystem) |
61 | { |
b983918b |
62 | lsystem->clear(); |
63 | |
15c82487 |
64 | try |
65 | { |
66 | // load |
67 | loadFromDisk(path); |
68 | |
69 | // axiom |
70 | findChild("axiom"); |
7cd25edc |
71 | Rule axiom("axiom", getString(), 1.0); |
72 | lsystem->setAxiom(axiom); |
15c82487 |
73 | |
74 | // rules |
75 | if (findChild("rule")) |
76 | { |
77 | do |
78 | { |
7cd25edc |
79 | // construct the rule |
80 | string name = getAttribute("name"); |
81 | string content = getString(); |
b983918b |
82 | double probability = strtod(getAttribute("probability").c_str(), NULL); |
7cd25edc |
83 | Rule completeRule(name, content, probability); |
84 | |
85 | lsystem->setRule(completeRule); |
15c82487 |
86 | } while (findNextChild()); |
87 | } |
88 | |
89 | // angle |
90 | findChild("angle"); |
91 | lsystem->setAngle(getNumber()); |
92 | |
7cd25edc |
93 | // depth |
94 | findChild("depth"); |
95 | lsystem->setDepth((int)getNumber()); |
b983918b |
96 | |
97 | // initial segment diameter |
98 | findChild("diameter"); |
99 | lsystem->setDiameter(getNumber()); |
100 | |
101 | // segment diameter factor |
102 | double diameterFactor = strtod(getAttribute("factor").c_str(), NULL); |
103 | lsystem->setDiameterFactor(diameterFactor); |
104 | |
15c82487 |
105 | } |
106 | catch (xmlpp::exception e) |
107 | { |
108 | cerr << "LSystemParameters::load(): " << e.what() << endl; |
109 | } |
110 | } |
111 | else |
112 | { |
526db675 |
113 | cerr << "invalid L-system" << endl; |
15c82487 |
114 | } |
115 | } |
116 | |
117 | |
118 | |
119 | /** |
120 | * Save parameters to xml file |
526db675 |
121 | * @param lsystem get parameters from this L-system |
15c82487 |
122 | * @param path path to xml file |
123 | */ |
124 | void LSystemParameters::save(LindenmayerSystem *lsystem, string path) |
125 | { |
7cd25edc |
126 | if (lsystem && |
127 | lsystem->getAxiom().getProbability() != 0.0 && |
128 | !lsystem->getRules().getRules().empty()) |
15c82487 |
129 | { |
130 | // new document with root node |
131 | createDocumentWithRoot("lsystem"); |
132 | |
133 | // axiom |
134 | addChildToRoot("axiom"); |
7cd25edc |
135 | addString(lsystem->getAxiom().getContent()); |
15c82487 |
136 | |
137 | // rules |
7cd25edc |
138 | rulemap rules = lsystem->getRules().getRules(); |
139 | for (rulemap::iterator it = rules.begin(); it != rules.end(); ++it) |
15c82487 |
140 | { |
141 | addChildToRoot("rule"); |
7cd25edc |
142 | addString(it->second.getContent()); |
143 | |
144 | addAttribute("name", it->second.getName()); |
b983918b |
145 | |
146 | string probabilityString = doubleToString(it->second.getProbability()); |
7cd25edc |
147 | addAttribute("probability", probabilityString); |
15c82487 |
148 | } |
149 | |
150 | // angle |
151 | addChildToRoot("angle"); |
152 | addNumber(lsystem->getAngle()); |
153 | |
7cd25edc |
154 | // depth |
155 | addChildToRoot("depth"); |
156 | addNumber(lsystem->getDepth()); |
15c82487 |
157 | |
b983918b |
158 | // initial segment diameter |
159 | addChildToRoot("diameter"); |
160 | addNumber(lsystem->getDiameter()); |
161 | |
162 | // segment diameter factor |
163 | string diameterFactor = doubleToString(lsystem->getDiameterFactor()); |
164 | addAttribute("factor", diameterFactor); |
165 | |
15c82487 |
166 | // save |
167 | saveToDisk(path); |
168 | } |
169 | else |
170 | { |
526db675 |
171 | cerr << "invalid L-system" << endl; |
15c82487 |
172 | } |
173 | } |