Save and load segment dimeter and factor.
[lsystem3d.git] / src / lsystemparameters.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 <cstdlib>
23
24 #include <iostream>
25 #include <string>
26
27 #include "lsystemparameters.h"
28 #include "rule.h"
29 #include "ruleset.h"
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
55 * @param lsystem put parameters into this L-system
56 * @param path path to xml file
57 */
58 void LSystemParameters::load(LindenmayerSystem *lsystem, string path)
59 {
60 if (lsystem)
61 {
62 lsystem->clear();
63
64 try
65 {
66 // load
67 loadFromDisk(path);
68
69 // axiom
70 findChild("axiom");
71 Rule axiom("axiom", getString(), 1.0);
72 lsystem->setAxiom(axiom);
73
74 // rules
75 if (findChild("rule"))
76 {
77 do
78 {
79 // construct the rule
80 string name = getAttribute("name");
81 string content = getString();
82 double probability = strtod(getAttribute("probability").c_str(), NULL);
83 Rule completeRule(name, content, probability);
84
85 lsystem->setRule(completeRule);
86 } while (findNextChild());
87 }
88
89 // angle
90 findChild("angle");
91 lsystem->setAngle(getNumber());
92
93 // depth
94 findChild("depth");
95 lsystem->setDepth((int)getNumber());
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
105 }
106 catch (xmlpp::exception e)
107 {
108 cerr << "LSystemParameters::load(): " << e.what() << endl;
109 }
110 }
111 else
112 {
113 cerr << "invalid L-system" << endl;
114 }
115 }
116
117
118
119 /**
120 * Save parameters to xml file
121 * @param lsystem get parameters from this L-system
122 * @param path path to xml file
123 */
124 void LSystemParameters::save(LindenmayerSystem *lsystem, string path)
125 {
126 if (lsystem &&
127 lsystem->getAxiom().getProbability() != 0.0 &&
128 !lsystem->getRules().getRules().empty())
129 {
130 // new document with root node
131 createDocumentWithRoot("lsystem");
132
133 // axiom
134 addChildToRoot("axiom");
135 addString(lsystem->getAxiom().getContent());
136
137 // rules
138 rulemap rules = lsystem->getRules().getRules();
139 for (rulemap::iterator it = rules.begin(); it != rules.end(); ++it)
140 {
141 addChildToRoot("rule");
142 addString(it->second.getContent());
143
144 addAttribute("name", it->second.getName());
145
146 string probabilityString = doubleToString(it->second.getProbability());
147 addAttribute("probability", probabilityString);
148 }
149
150 // angle
151 addChildToRoot("angle");
152 addNumber(lsystem->getAngle());
153
154 // depth
155 addChildToRoot("depth");
156 addNumber(lsystem->getDepth());
157
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
166 // save
167 saveToDisk(path);
168 }
169 else
170 {
171 cerr << "invalid L-system" << endl;
172 }
173 }