* Adaptation to new classes Rule/RuleSet.
[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 <iostream>
23 #include <string>
24
25 #include <glibmm/stringutils.h>
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 try
63 {
64 // load
65 loadFromDisk(path);
66
67 // axiom
68 findChild("axiom");
69 Rule axiom("axiom", getString(), 1.0);
70 lsystem->setAxiom(axiom);
71
72 // rules
73 if (findChild("rule"))
74 {
75 do
76 {
77 // construct the rule
78 string name = getAttribute("name");
79 string content = getString();
80 double probability = Glib::Ascii::strtod(getAttribute("probability"));
81 Rule completeRule(name, content, probability);
82
83 lsystem->setRule(completeRule);
84 } while (findNextChild());
85 }
86
87 // angle
88 findChild("angle");
89 lsystem->setAngle(getNumber());
90
91 // depth
92 findChild("depth");
93 lsystem->setDepth((int)getNumber());
94 }
95 catch (xmlpp::exception e)
96 {
97 cerr << "LSystemParameters::load(): " << e.what() << endl;
98 }
99 }
100 else
101 {
102 cerr << "invalid L-system" << endl;
103 }
104 }
105
106
107
108 /**
109 * Save parameters to xml file
110 * @param lsystem get parameters from this L-system
111 * @param path path to xml file
112 */
113 void LSystemParameters::save(LindenmayerSystem *lsystem, string path)
114 {
115 if (lsystem &&
116 lsystem->getAxiom().getProbability() != 0.0 &&
117 !lsystem->getRules().getRules().empty())
118 {
119 // new document with root node
120 createDocumentWithRoot("lsystem");
121
122 // axiom
123 addChildToRoot("axiom");
124 addString(lsystem->getAxiom().getContent());
125
126 // rules
127 rulemap rules = lsystem->getRules().getRules();
128 for (rulemap::iterator it = rules.begin(); it != rules.end(); ++it)
129 {
130 addChildToRoot("rule");
131 addString(it->second.getContent());
132
133 addAttribute("name", it->second.getName());
134
135 string probabilityString = Glib::Ascii::dtostr(it->second.getProbability());
136 addAttribute("probability", probabilityString);
137 }
138
139 // angle
140 addChildToRoot("angle");
141 addNumber(lsystem->getAngle());
142
143 // depth
144 addChildToRoot("depth");
145 addNumber(lsystem->getDepth());
146
147 // save
148 saveToDisk(path);
149 }
150 else
151 {
152 cerr << "invalid L-system" << endl;
153 }
154 }