X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flsystemparameters.cpp;fp=src%2Flsystemparameters.cpp;h=c12e9e3ecaa7583a2427a612b03c5550166ee81b;hb=dd73b028a74ef8b57569c3e76751481d2124e491;hp=0000000000000000000000000000000000000000;hpb=4f63b3f3fc8251568c9dc51042ca3c561e8c4020;p=lsystem3d.git diff --git a/src/lsystemparameters.cpp b/src/lsystemparameters.cpp new file mode 100644 index 0000000..c12e9e3 --- /dev/null +++ b/src/lsystemparameters.cpp @@ -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 +#include + +#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 rules = lsystem->getRules(); + map::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; + } +}