Initial revision
[lsystem3d.git] / lsystem3d / src / lsystemparameters.cpp
CommitLineData
e5247a3b 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 "lsystemparameters.h"
26
27using namespace std;
28
29
30
31/**
32 * Constructor
33 */
34LSystemParameters::LSystemParameters()
35{
36}
37
38
39
40/**
41 * Destructor
42 */
43LSystemParameters::~LSystemParameters()
44{
45}
46
47
48
49/**
50 * Load parameters from xml file
51 * @param lsystem put parameters into this l-system
52 * @param path path to xml file
53 */
54void LSystemParameters::load(LindenmayerSystem *lsystem, string path)
55{
56 if (lsystem)
57 {
58 try
59 {
60 // load
61 loadFromDisk(path);
62
63 // axiom
64 findChild("axiom");
65 lsystem->setAxiom(getString());
66
67 // rules
68 if (findChild("rule"))
69 {
70 do
71 {
72 lsystem->setRule(getAttribute("name"), getString());
73 } while (findNextChild());
74 }
75
76 // angle
77 findChild("angle");
78 lsystem->setAngle(getNumber());
79
80 // iterations
81 findChild("iterations");
82 lsystem->setNumIterations((int)getNumber());
83 }
84 catch (xmlpp::exception e)
85 {
86 cerr << "LSystemParameters::load(): " << e.what() << endl;
87 }
88 }
89 else
90 {
91 cerr << "invalid lsystem" << endl;
92 }
93}
94
95
96
97/**
98 * Save parameters to xml file
99 * @param lsystem get parameters from this l-system
100 * @param path path to xml file
101 */
102void LSystemParameters::save(LindenmayerSystem *lsystem, string path)
103{
104 if (lsystem && !lsystem->getAxiom().empty() && !lsystem->getRules().empty())
105 {
106 // new document with root node
107 createDocumentWithRoot("lsystem");
108
109 // axiom
110 addChildToRoot("axiom");
111 addString(lsystem->getAxiom());
112
113 // rules
114 map<string,string> rules = lsystem->getRules();
115 map<string,string>::iterator currentRule;
116 for (currentRule = rules.begin(); currentRule != rules.end(); currentRule++)
117 {
118 addChildToRoot("rule");
119 addString(currentRule->second);
120 addAttribute("name", currentRule->first);
121 }
122
123 // angle
124 addChildToRoot("angle");
125 addNumber(lsystem->getAngle());
126
127 // iterations
128 addChildToRoot("iterations");
129 addNumber(lsystem->getNumIterations());
130
131 // save
132 saveToDisk(path);
133 }
134 else
135 {
136 cerr << "invalid lsystem" << endl;
137 }
138}