Hosted by: BerliOS Developer Logo

OpenMBV C++ Interface

BerliOS Project Page
/home/user/MBSimWindows/openmbv/openmbvcppinterface/openmbvcppinterface/object.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 _OPENMBV_OBJECT_H_
00021 #define _OPENMBV_OBJECT_H_
00022 
00023 #include <string>
00024 #include <H5Cpp.h>
00025 #include <sstream>
00026 #include <openmbvcppinterface/objectfactory.h>
00027 #include <mbxmlutilstinyxml/tinyxml.h>
00028 #include <mbxmlutilstinyxml/tinynamespace.h>
00029 #include <vector>
00030 
00031 #define OPENMBVNS_ "http://openmbv.berlios.de/OpenMBV"
00032 #define OPENMBVNS "{"OPENMBVNS_"}"
00033 
00034 #define MBXMLUTILSPARAMNS_ "http://openmbv.berlios.de/MBXMLUtils/parameter"
00035 #define MBXMLUTILSPARAMNS "{"MBXMLUTILSPARAMNS_"}"
00036 
00037 namespace OpenMBV {
00038 
00039   class Group;
00040 
00041   template<class t>
00042   class SimpleParameter;
00043   typedef SimpleParameter<double> ScalarParameter;
00044   typedef SimpleParameter<std::vector<double> > VectorParameter;
00045   typedef SimpleParameter<std::vector<std::vector<double> > > MatrixParameter;
00046 
00048   class Object {
00049     friend class Group;
00050     protected:
00051       std::string name;
00052       std::string enableStr, boundingBoxStr;
00053       std::string ID; // Note: the ID is metadata and stored as a processing instruction in XML
00054       bool selected; // Note: the selected flag is metadata and not stored in XML but used by OpenMBVGUI
00055       Group* parent;
00056 
00057       virtual void createHDF5File()=0;
00058       virtual void openHDF5File()=0;
00059       H5::Group *hdf5Group;
00060       virtual void terminate()=0;
00061 
00062       std::map<std::string, double> scalarParameter;
00063       std::map<std::string, std::vector<double> > vectorParameter;
00064       std::map<std::string, std::vector<std::vector<double> > > matrixParameter;
00065 
00066       double get(const ScalarParameter& src);
00067       std::vector<double> get(const VectorParameter& src);
00068       std::vector<std::vector<double> > get(const MatrixParameter& src);
00069 
00070       void set(ScalarParameter& dst, const ScalarParameter& src);
00071       void set(VectorParameter& dst, const VectorParameter& src);
00072       void set(MatrixParameter& dst, const MatrixParameter& src);
00073       
00074       virtual void collectParameter(std::map<std::string, double>& sp, std::map<std::string, std::vector<double> >& vp, std::map<std::string, std::vector<std::vector<double> > >& mp, bool collectAlsoSeparateGroup=false);
00075 
00077       virtual ~Object();
00078     public:
00080       Object();
00081 
00084       virtual void destroy() const;
00085 
00087       virtual std::string getClassName()=0;
00088 
00090       void setEnable(bool enable) { enableStr=(enable==true)?"true":"false"; }
00091 
00092       bool getEnable() { return enableStr=="true"?true:false; }
00093 
00095       void setBoundingBox(bool bbox) { boundingBoxStr=(bbox==true)?"true":"false"; }
00096 
00097       bool getBoundingBox() { return boundingBoxStr=="true"?true:false; }
00098 
00100       void setName(const std::string& name_) { name=name_; }
00101 
00102       std::string getName() { return name; }
00103 
00105       virtual std::string getFullName(bool includingFileName=false, bool stopAtSeparateFile=false);
00106 
00108       virtual void initializeUsingXML(MBXMLUtils::TiXmlElement *element);
00109 
00110       virtual MBXMLUtils::TiXmlElement *writeXMLFile(MBXMLUtils::TiXmlNode *parent);
00111 
00113       Group* getSeparateGroup();
00114 
00116       Group* getTopLevelGroup();
00117 
00118       Group* getParent() { return parent; }
00119 
00121       std::string getID() const { return ID; }
00123       void setID(std::string ID_) { ID=ID_; }
00124 
00126       bool getSelected() const { return selected; }
00128       void setSelected(bool selected_) { selected=selected_; }
00129 
00130       // FROM NOW ONLY CONVENIENCE FUNCTIONS FOLLOW !!!
00131       static std::string fixPath(std::string oldFile, std::string newFile) { return MBXMLUtils::fixPath(oldFile, newFile); }
00132 
00133       static ScalarParameter getDouble(MBXMLUtils::TiXmlElement *e);
00134       static VectorParameter getVec(MBXMLUtils::TiXmlElement *e, unsigned int rows=0);
00135       static MatrixParameter getMat(MBXMLUtils::TiXmlElement *e, unsigned int rows=0, unsigned int cols=0);
00136 
00137       static std::string numtostr(int i) { std::ostringstream oss; oss << i; return oss.str(); }
00138       static std::string numtostr(double d) { std::ostringstream oss; oss << d; return oss.str(); } 
00139 
00140 
00141       template <class T>
00142       static void addElementText(MBXMLUtils::TiXmlElement *parent, std::string name, T value) {
00143         std::ostringstream oss;
00144         oss<<value;
00145         parent->LinkEndChild(new MBXMLUtils::TiXmlElement(name))->LinkEndChild(new MBXMLUtils::TiXmlText(oss.str()));
00146       }
00147 
00148       void addElementText(MBXMLUtils::TiXmlElement *parent, std::string name, double value, double def);
00149 
00150       void addElementText(MBXMLUtils::TiXmlElement *parent, std::string name, SimpleParameter<double> value, double def);
00151 
00152       template <class T>
00153       static void addAttribute(MBXMLUtils::TiXmlNode *node, std::string name, T value) {
00154         if(node->ToElement()) {
00155           std::ostringstream oss;
00156           oss<<value;
00157           node->ToElement()->SetAttribute(name, oss.str());
00158         }
00159       }
00160 
00161       template <class T>
00162       static void addAttribute(MBXMLUtils::TiXmlNode *node, std::string name, T value, std::string def) {
00163         if(value!=def) addAttribute(node, name, value);
00164       }
00165 
00166     protected:
00167       static std::vector<double> toVector(std::string str);
00168       static std::vector<std::vector<double> > toMatrix(std::string str);
00169   };
00170 
00171 }
00172 
00173 #endif