* Adaptation to new classes Rule/RuleSet.
[lsystem3d.git] / src / lsystemparameters.cpp
CommitLineData
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
22#include <iostream>
23#include <string>
24
7cd25edc 25#include <glibmm/stringutils.h>
26
15c82487 27#include "lsystemparameters.h"
7cd25edc 28#include "rule.h"
29#include "ruleset.h"
15c82487 30
31using namespace std;
32
33
34
35/**
36 * Constructor
37 */
38LSystemParameters::LSystemParameters()
39{
40}
41
42
43
44/**
45 * Destructor
46 */
47LSystemParameters::~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 */
58void 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");
7cd25edc 69 Rule axiom("axiom", getString(), 1.0);
70 lsystem->setAxiom(axiom);
15c82487 71
72 // rules
73 if (findChild("rule"))
74 {
75 do
76 {
7cd25edc 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);
15c82487 84 } while (findNextChild());
85 }
86
87 // angle
88 findChild("angle");
89 lsystem->setAngle(getNumber());
90
7cd25edc 91 // depth
92 findChild("depth");
93 lsystem->setDepth((int)getNumber());
15c82487 94 }
95 catch (xmlpp::exception e)
96 {
97 cerr << "LSystemParameters::load(): " << e.what() << endl;
98 }
99 }
100 else
101 {
526db675 102 cerr << "invalid L-system" << endl;
15c82487 103 }
104}
105
106
107
108/**
109 * Save parameters to xml file
526db675 110 * @param lsystem get parameters from this L-system
15c82487 111 * @param path path to xml file
112 */
113void LSystemParameters::save(LindenmayerSystem *lsystem, string path)
114{
7cd25edc 115 if (lsystem &&
116 lsystem->getAxiom().getProbability() != 0.0 &&
117 !lsystem->getRules().getRules().empty())
15c82487 118 {
119 // new document with root node
120 createDocumentWithRoot("lsystem");
121
122 // axiom
123 addChildToRoot("axiom");
7cd25edc 124 addString(lsystem->getAxiom().getContent());
15c82487 125
126 // rules
7cd25edc 127 rulemap rules = lsystem->getRules().getRules();
128 for (rulemap::iterator it = rules.begin(); it != rules.end(); ++it)
15c82487 129 {
130 addChildToRoot("rule");
7cd25edc 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);
15c82487 137 }
138
139 // angle
140 addChildToRoot("angle");
141 addNumber(lsystem->getAngle());
142
7cd25edc 143 // depth
144 addChildToRoot("depth");
145 addNumber(lsystem->getDepth());
15c82487 146
147 // save
148 saveToDisk(path);
149 }
150 else
151 {
526db675 152 cerr << "invalid L-system" << endl;
15c82487 153 }
154}