Initial revision
[lsystem3d.git] / src / lsystemparameters.cpp
diff --git a/src/lsystemparameters.cpp b/src/lsystemparameters.cpp
new file mode 100644 (file)
index 0000000..c12e9e3
--- /dev/null
@@ -0,0 +1,138 @@
+// Copyright (C) 2006 Erik Dahlberg
+// 
+// This file is part of LSystem3d.
+// 
+// LSystem3D is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+// 
+// LSystem3D is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with LSystem3D; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+
+
+#include <iostream>
+#include <string>
+
+#include "lsystemparameters.h"
+
+using namespace std;
+
+
+
+/**
+ * Constructor
+ */
+LSystemParameters::LSystemParameters()
+{
+}
+
+
+
+/**
+ * Destructor
+ */
+LSystemParameters::~LSystemParameters()
+{
+}
+
+
+
+/**
+ * Load parameters from xml file
+ * @param lsystem put parameters into this l-system
+ * @param path path to xml file
+ */
+void LSystemParameters::load(LindenmayerSystem *lsystem, string path)
+{
+    if (lsystem)
+    {
+        try
+        {
+            // load
+            loadFromDisk(path);
+    
+            // axiom
+            findChild("axiom");
+            lsystem->setAxiom(getString());
+            
+            // rules
+            if (findChild("rule"))
+            {
+                do
+                {
+                    lsystem->setRule(getAttribute("name"), getString());
+                } while (findNextChild());
+            }
+    
+            // angle
+            findChild("angle");
+            lsystem->setAngle(getNumber());
+            
+            // iterations        
+            findChild("iterations");
+            lsystem->setNumIterations((int)getNumber());
+        }
+        catch (xmlpp::exception e)
+        {
+            cerr << "LSystemParameters::load(): " << e.what() << endl;
+        }
+    }
+    else
+    {
+        cerr << "invalid lsystem" << endl;
+    }
+}
+
+
+
+/**
+ * Save parameters to xml file
+ * @param lsystem get parameters from this l-system
+ * @param path path to xml file
+ */
+void LSystemParameters::save(LindenmayerSystem *lsystem, string path)
+{
+    if (lsystem && !lsystem->getAxiom().empty() && !lsystem->getRules().empty())
+    {
+        // new document with root node
+        createDocumentWithRoot("lsystem");
+        
+        // axiom
+        addChildToRoot("axiom");
+        addString(lsystem->getAxiom());
+        
+        // rules
+        map<string,string> rules = lsystem->getRules();
+        map<string,string>::iterator currentRule;
+        for (currentRule = rules.begin(); currentRule != rules.end(); currentRule++)
+        {
+            addChildToRoot("rule");
+            addString(currentRule->second);
+            addAttribute("name", currentRule->first);
+        }
+        
+        // angle
+        addChildToRoot("angle");
+        addNumber(lsystem->getAngle());
+        
+        // iterations
+        addChildToRoot("iterations");
+        addNumber(lsystem->getNumIterations());
+        
+        // save
+        saveToDisk(path);
+    }
+    else
+    {
+        cerr << "invalid lsystem" << endl;
+    }
+}