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
27 #include "lsystemparameters.h"
38 LSystemParameters::LSystemParameters()
47 LSystemParameters::~LSystemParameters()
54 * Load parameters from xml file
55 * @param lsystem put parameters into this L-system
56 * @param path path to xml file
58 void LSystemParameters::load(LindenmayerSystem
*lsystem
, string path
)
71 Rule
axiom("axiom", getString(), 1.0);
72 lsystem
->setAxiom(axiom
);
75 if (findChild("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
);
85 lsystem
->setRule(completeRule
);
86 } while (findNextChild());
91 lsystem
->setAngle(getNumber());
95 lsystem
->setDepth((int)getNumber());
97 // initial segment diameter
98 findChild("diameter");
99 lsystem
->setDiameter(getNumber());
101 // segment diameter factor
102 double diameterFactor
= strtod(getAttribute("factor").c_str(), NULL
);
103 lsystem
->setDiameterFactor(diameterFactor
);
106 catch (xmlpp::exception e
)
108 cerr
<< "LSystemParameters::load(): " << e
.what() << endl
;
113 cerr
<< "invalid L-system" << endl
;
120 * Save parameters to xml file
121 * @param lsystem get parameters from this L-system
122 * @param path path to xml file
124 void LSystemParameters::save(LindenmayerSystem
*lsystem
, string path
)
127 lsystem
->getAxiom().getProbability() != 0.0 &&
128 !lsystem
->getRules().getRules().empty())
130 // new document with root node
131 createDocumentWithRoot("lsystem");
134 addChildToRoot("axiom");
135 addString(lsystem
->getAxiom().getContent());
138 rulemap rules
= lsystem
->getRules().getRules();
139 for (rulemap::iterator it
= rules
.begin(); it
!= rules
.end(); ++it
)
141 addChildToRoot("rule");
142 addString(it
->second
.getContent());
144 addAttribute("name", it
->second
.getName());
146 string probabilityString
= doubleToString(it
->second
.getProbability());
147 addAttribute("probability", probabilityString
);
151 addChildToRoot("angle");
152 addNumber(lsystem
->getAngle());
155 addChildToRoot("depth");
156 addNumber(lsystem
->getDepth());
158 // initial segment diameter
159 addChildToRoot("diameter");
160 addNumber(lsystem
->getDiameter());
162 // segment diameter factor
163 string diameterFactor
= doubleToString(lsystem
->getDiameterFactor());
164 addAttribute("factor", diameterFactor
);
171 cerr
<< "invalid L-system" << endl
;