From 315d5aec043d2c75fec52bcf3a29a41da063de49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Mon, 1 Dec 2014 14:09:55 +0100 Subject: [PATCH] implemented all methods of the converter class, needs testing --- converter/converter.cc | 142 ++++++++++++++++++++++++++--------------- converter/converter.hh | 9 ++- converter/main.cc | 7 +- 3 files changed, 96 insertions(+), 62 deletions(-) diff --git a/converter/converter.cc b/converter/converter.cc index 61c8032..b9850e2 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -1,5 +1,6 @@ #include "converter.hh" #include +#include using namespace tinyxml2; @@ -22,12 +23,22 @@ Converter::Converter(std::string level){ //Load the Level xml file const char* charXmlFile = xmlFile.c_str(); doc->LoadFile(charXmlFile); + nextID.push_back(1); + nextID.push_back(1); if (doc->ErrorID()!=0){ printf("Could not open xml, creating new xml.\n"); + }else{ + XMLElement* thisComposition = doc->FirstChildElement("composition"); + int idGreen, idBlue; + for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){ + thisComposition->QueryIntAttribute("idGreen", &idGreen); + thisComposition->QueryIntAttribute("idBlue", &idBlue); + if(idGreen > nextID[0] || (idGreen == nextID[0] && idBlue > nextID[1])){ + nextID[0] = idGreen; + nextID[1] = idBlue; + } + } } - - nextId.push_back(1); - nextId.push_back(250); //TODO } Converter::Converter(){ @@ -36,56 +47,75 @@ Converter::Converter(){ Converter::~Converter(){ } -void Converter::updateComposition(unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ - //TODO -} - - -std::vector Converter::newComposition(unsigned int type, unsigned int posX, unsigned int posZ){ - XMLNode* thisComposition = compositions->FirstChild(); - for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){ - int thisType; - XMLElement* thisCompositionElement = thisComposition->ToElement(); - thisCompositionElement->QueryIntAttribute("typeID", &thisType); - if(thisType == type){ - //XMLElement* newComposition = doc.NewElement(); - //newComposition = thisComposition; - //XMLElement* newComposition = thisComposition->Clone(); //TODO write own clone function - doc->InsertFirstChild(newComposition); - - XMLElement* idBlue = doc->NewElement("" + itoa(nextId[1]) + ""); - XMLElement* idGreen = doc->NewElement("" + itoa(nextId[0]) + ""); - XMLElement* zPos = doc->NewElement("" + itoa(posZ) + ""); - XMLElement* yOffset = doc->NewElement("0.0"); - XMLElement* xPos = doc->NewElement("" + itoa(posX) + ""); - XMLElement* zRot = doc->NewElement("0.0"); - XMLElement* yRot = doc->NewElement("0.0"); - XMLElement* xRot = doc->NewElement("0.0"); - XMLElement* scale = doc->NewElement("1.0"); - - newComposition->InsertFirstChild(idBlue); - newComposition->InsertFirstChild(idGreen); - newComposition->InsertFirstChild(zPos); - newComposition->InsertFirstChild(yOffset); - newComposition->InsertFirstChild(xPos); - newComposition->InsertFirstChild(zRot); - newComposition->InsertFirstChild(yRot); - newComposition->InsertFirstChild(xRot); - newComposition->InsertFirstChild(scale); - } - } +std::vector Converter::newComposition(unsigned int type, float posX, float posZ){ + XMLElement* newComposition = doc->NewElement("composition"); + doc->InsertFirstChild(newComposition); - std::vector ret = nextId; - nextId[1] += 1; - if (nextId[1] == 256){ - nextId[1] = 1; - nextId[0] +=1; + XMLElement* typeID = doc->NewElement("typeID"); + XMLElement* idBlue = doc->NewElement("idBlue"); + XMLElement* idGreen = doc->NewElement("idGreen"); + XMLElement* zPos = doc->NewElement("zPos"); + XMLElement* yOffset = doc->NewElement("yOffset"); + XMLElement* xPos = doc->NewElement("xPos"); + XMLElement* zRot = doc->NewElement("zRot"); + XMLElement* yRot = doc->NewElement("yRot"); + XMLElement* xRot = doc->NewElement("xRot"); + XMLElement* scale = doc->NewElement("scale"); + + typeID->SetText(std::to_string(type).c_str()); + idBlue->SetText(std::to_string(nextID[1]).c_str()); + idGreen->SetText(std::to_string(nextID[0]).c_str()); + zPos->SetText(std::to_string(posZ).c_str()); + yOffset->SetText("0.0"); + xPos->SetText(std::to_string(posX).c_str()); + zRot->SetText("0.0"); + yRot->SetText("0.0"); + xRot->SetText("0.0"); + scale->SetText("1.0"); + + newComposition->InsertFirstChild(typeID); + newComposition->InsertFirstChild(idBlue); + newComposition->InsertFirstChild(idGreen); + newComposition->InsertFirstChild(zPos); + newComposition->InsertFirstChild(yOffset); + newComposition->InsertFirstChild(xPos); + newComposition->InsertFirstChild(zRot); + newComposition->InsertFirstChild(yRot); + newComposition->InsertFirstChild(xRot); + newComposition->InsertFirstChild(scale); + + std::vector ret = nextID; + nextID[1] += 1; + if (nextID[1] == 255){ + nextID[1] = 0; + nextID[0] +=1; } return ret; } +void Converter::updateComposition(unsigned int idG, unsigned int idB, float posX, float posZ){ + XMLElement* thisComposition = doc->FirstChildElement("composition"); + int idGreen, idBlue; + for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){ + thisComposition->QueryIntAttribute("idGreen", &idGreen); + thisComposition->QueryIntAttribute("idBlue", &idBlue); + if(idGreen == idG && idBlue == idB){ + thisComposition->FirstChildElement("xPos")->SetText(std::to_string(posX).c_str()); + thisComposition->FirstChildElement("zPos")->SetText(std::to_string(posZ).c_str()); + } + } +} + void Converter::deleteComposition(unsigned int idG, unsigned int idB){ - //TODO + XMLElement* thisComposition = doc->FirstChildElement("composition"); + int idGreen, idBlue; + for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){ + thisComposition->QueryIntAttribute("idGreen", &idGreen); + thisComposition->QueryIntAttribute("idBlue", &idBlue); + if(idGreen == idG && idBlue == idB){ + doc->DeleteChild(thisComposition); + } + } } void Converter::save(){ @@ -93,11 +123,17 @@ void Converter::save(){ doc->SaveFile(charXmlFile); } -std::vector Converter::getNextId(){ - return nextId; +std::vector Converter::getNextID(){ + return nextID; } - -void Converter::newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ - //TODO -} +/* finding a typeID in compositions: + XMLElement* thisComposition = compositions->FirstChildElement("composition"); + for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){ + int thisType; + thisComposition->QueryIntAttribute("typeID", &thisType); + if(thisType == type){ + ... + } + } +*/ diff --git a/converter/converter.hh b/converter/converter.hh index e625ee2..32c5c42 100644 --- a/converter/converter.hh +++ b/converter/converter.hh @@ -11,18 +11,17 @@ class Converter { Converter(std::string level); Converter(); ~Converter(); - void updateComposition(unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ); - std::vector newComposition(unsigned int type, unsigned int posX, unsigned int posZ); + void updateComposition(unsigned int idG, unsigned int idB, float posX, float posZ); + std::vector newComposition(unsigned int type, float posX, float posZ); void deleteComposition(unsigned int idG, unsigned int idB); void save(); - std::vector getNextId(); + std::vector getNextID(); private: - std::vector nextId; + std::vector nextID; std::string xmlFile; XMLDocument* doc = new XMLDocument(); XMLDocument* compositions = new XMLDocument(); - void newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ); }; #endif diff --git a/converter/main.cc b/converter/main.cc index 53060e3..70eeb55 100644 --- a/converter/main.cc +++ b/converter/main.cc @@ -34,12 +34,12 @@ int main( int argc, char *argv[] ){ if(image[pixel]!=0){ if(image[pixel+1]==0 && image[pixel+2]==0){ std::vector temp; - temp = conv.newComposition(image[pixel], rowNum, columnNum); + temp = conv.newComposition(image[pixel], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); idFound[temp[0]][temp[1]] = true; image[pixel+1] = temp[0]; image[pixel+2] = temp[1]; }else{ - conv.updateComposition(image[pixel+1], image[pixel+2], rowNum, columnNum); + conv.updateComposition(image[pixel+1], image[pixel+2], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); idFound[image[pixel+1]][image[pixel+2]] = true; } } @@ -53,8 +53,7 @@ int main( int argc, char *argv[] ){ } //delete compositions that were not in the png anymore - - for (int i=1; i<=conv.getNextId()[1]; i++){ + for (int i=1; i<=conv.getNextID()[1]; i++){ for (int j=1; j<256; j++){ if (! idFound[i][j]){ conv.deleteComposition(i,j);