#include "LBBCLeafDistributionSerializer.h" namespace LBBC { LeafDistributionSerializer::LeafDistributionSerializer() : BBC::EntityDistributionSerializer() { } LeafDistributionSerializer::~LeafDistributionSerializer() { } void LeafDistributionSerializer::setLeafDistribution(LeafDistribution *value) { mLeafDistribution = value; } LeafDistribution* LeafDistributionSerializer::getLeafDistribution() { return mLeafDistribution; } void LeafDistributionSerializer::readLeafDistribution(TiXmlDocument *document) { mEntity->loadMesh(true); TiXmlNode *parentNode = document->FirstChild("tree"); TiXmlNode *leavesNode = parentNode->FirstChild("leaves"); unsigned numEntities = Ogre::StringConverter::parseInt(leavesNode->ToElement()->Attribute("count")); float dMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("dMin")); float dMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("dMax")); float nXMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nXMin")); float nXMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nXMax")); float nYMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nYMin")); float nYMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nYMax")); float nZMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nZMin")); float nZMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nZMax")); mLeafDistribution->setMinD(dMin); mLeafDistribution->setMaxD(dMax); mLeafDistribution->setMinNormal(Ogre::Vector3(nXMin,nYMin,nZMin)); mLeafDistribution->setMaxNormal(Ogre::Vector3(nXMax,nYMax,nZMax)); for (TiXmlNode *leafNode = leavesNode->FirstChild("leaf"); leafNode; leafNode = leafNode->NextSibling() ) { Leaf *leaf = new Leaf(); leaf->readLeaf(leafNode); addFacesInfo(leaf); mLeafDistribution->addEntity(leaf); } } void LeafDistributionSerializer::addFacesInfo(Leaf *leaf) { for (unsigned int iFace = 0; iFace < leaf->getNumFaces(); iFace++) { leaf->addFaceInfo( mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(leaf->getFace(iFace)).x), mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(leaf->getFace(iFace)).y), mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(leaf->getFace(iFace)).z)); } } void LeafDistributionSerializer::writeLeafDistribution(TiXmlDocument *document) { TiXmlNode *parentNode = document->InsertEndChild(TiXmlElement("tree"))->ToElement(); TiXmlNode *leavesNode = parentNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("leaves").c_str()))->ToElement(); Ogre::String sLeaves = Ogre::StringConverter::toString(mLeafDistribution->getNumEntities()); Ogre::String sDmin = Ogre::StringConverter::toString(mLeafDistribution->getMinD()); Ogre::String sDmax = Ogre::StringConverter::toString(mLeafDistribution->getMaxD()); Ogre::String sNxMin = Ogre::StringConverter::toString(mLeafDistribution->getMinNormal().x); Ogre::String sNxMax = Ogre::StringConverter::toString(mLeafDistribution->getMaxNormal().x); Ogre::String sNyMin = Ogre::StringConverter::toString(mLeafDistribution->getMinNormal().y); Ogre::String sNyMax = Ogre::StringConverter::toString(mLeafDistribution->getMaxNormal().y); Ogre::String sNzMin = Ogre::StringConverter::toString(mLeafDistribution->getMinNormal().z); Ogre::String sNzMax = Ogre::StringConverter::toString(mLeafDistribution->getMaxNormal().z); leavesNode->ToElement()->SetAttribute("count",sLeaves.c_str()); leavesNode->ToElement()->SetAttribute("dMin",sDmin.c_str()); leavesNode->ToElement()->SetAttribute("dMax",sDmax.c_str()); leavesNode->ToElement()->SetAttribute("nXMin",sNxMin.c_str()); leavesNode->ToElement()->SetAttribute("nXMax",sNxMax.c_str()); leavesNode->ToElement()->SetAttribute("nYMin",sNyMin.c_str()); leavesNode->ToElement()->SetAttribute("nYMax",sNyMax.c_str()); leavesNode->ToElement()->SetAttribute("nZMin",sNzMin.c_str()); leavesNode->ToElement()->SetAttribute("nZMax",sNzMax.c_str()); for (unsigned int k=0; k < mLeafDistribution->getNumEntities(); k++) { Leaf *leaf = (Leaf *)mLeafDistribution->getEntity(k).get(); TiXmlNode *leafNode = leavesNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("leaf").c_str()))->ToElement(); leaf->writeLeaf(leafNode); } } }