Hosted by: BerliOS Developer Logo

OpenMBV C++ Interface

BerliOS Project Page
/home/user/MBSimWindows/openmbv/openmbvcppinterface/openmbvcppinterface/polygonpoint.h
00001 /*
00002    OpenMBV - Open Multi Body Viewer.
00003    Copyright (C) 2009 Markus Friedrich
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018    */
00019 
00020 #ifndef POLYGONPOINT_H
00021 #define POLYGONPOINT_H
00022 
00023 #include <vector>
00024 #include <fstream>
00025 #include <iostream>
00026 #include <mbxmlutilstinyxml/tinyxml.h>
00027 #include "openmbvcppinterface/body.h"
00028 #include <stdexcept>
00029 
00030 namespace OpenMBV {
00031 
00038   class PolygonPoint {
00039     public:
00041       PolygonPoint(double x_, double y_, int b_) : x(x_), y(y_), b(b_) {}
00042 
00043       /* GETTER / SETTER */
00044       double getXComponent() { return x; } 
00045       double getYComponent() { return y; } 
00046       int getBorderValue() { return b; }
00047       /***************************************************/
00048 
00049       /* CONVENIENCE */
00051       static void serializePolygonPointContour(MBXMLUtils::TiXmlElement *parent, const std::vector<PolygonPoint*> *cont) {
00052         std::string str;
00053         str="[ ";
00054         for(std::vector<PolygonPoint*>::const_iterator j=cont->begin(); j!=cont->end(); j++) {
00055           str+=Object::numtostr((*j)->getXComponent())+", "+Object::numtostr((*j)->getYComponent())+", "+Object::numtostr((*j)->getBorderValue());
00056           if(j+1!=cont->end()) str+=";    "; else str+=" ]";
00057         }
00058         Object::addElementText(parent, OPENMBVNS"contour", str);
00059       }
00060 
00061       static std::vector<PolygonPoint*>* initializeUsingXML(MBXMLUtils::TiXmlElement *element) {
00062         MatrixParameter matParam=Body::getMat(element);
00063         if(matParam.getParamStr()!="") throw std::runtime_error("only numeric values are allowd for contours (vector<PolygonPoint*>)");
00064         std::vector<std::vector<double> > mat=matParam.getValue();
00065         std::vector<PolygonPoint*> *contour=new std::vector<PolygonPoint*>;
00066         for(size_t r=0; r<mat.size(); r++) {
00067           PolygonPoint *pp=new PolygonPoint(mat[r][0], mat[r][1], (int)(mat[r][2]));
00068           contour->push_back(pp);
00069         }
00070         return contour;
00071       }
00072 
00073     private:
00074       double x, y;
00075       int b;
00076   };
00077 
00078 }
00079 
00080 #endif /* POLYGONPOINT_H */
00081