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
25 #include <glibmm/stringutils.h>
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
)
69 Rule
axiom("axiom", getString(), 1.0);
70 lsystem
->setAxiom(axiom
);
73 if (findChild("rule"))
78 string name
= getAttribute("name");
79 string content
= getString();
80 double probability
= Glib::Ascii::strtod(getAttribute("probability"));
81 Rule
completeRule(name
, content
, probability
);
83 lsystem
->setRule(completeRule
);
84 } while (findNextChild());
89 lsystem
->setAngle(getNumber());
93 lsystem
->setDepth((int)getNumber());
95 catch (xmlpp::exception e
)
97 cerr
<< "LSystemParameters::load(): " << e
.what() << endl
;
102 cerr
<< "invalid L-system" << endl
;
109 * Save parameters to xml file
110 * @param lsystem get parameters from this L-system
111 * @param path path to xml file
113 void LSystemParameters::save(LindenmayerSystem
*lsystem
, string path
)
116 lsystem
->getAxiom().getProbability() != 0.0 &&
117 !lsystem
->getRules().getRules().empty())
119 // new document with root node
120 createDocumentWithRoot("lsystem");
123 addChildToRoot("axiom");
124 addString(lsystem
->getAxiom().getContent());
127 rulemap rules
= lsystem
->getRules().getRules();
128 for (rulemap::iterator it
= rules
.begin(); it
!= rules
.end(); ++it
)
130 addChildToRoot("rule");
131 addString(it
->second
.getContent());
133 addAttribute("name", it
->second
.getName());
135 string probabilityString
= Glib::Ascii::dtostr(it
->second
.getProbability());
136 addAttribute("probability", probabilityString
);
140 addChildToRoot("angle");
141 addNumber(lsystem
->getAngle());
144 addChildToRoot("depth");
145 addNumber(lsystem
->getDepth());
152 cerr
<< "invalid L-system" << endl
;