MBSim

Project Page
element.h
00001 /* Copyright (C) 2004-2009 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  *          rzander@users.berlios.de
00019  */
00020 
00021 #ifndef _ELEMENT_H_
00022 #define _ELEMENT_H_
00023 
00024 #include "fmatvec/fmatvec.h"
00025 #include "mbsim/objectfactory.h"
00026 #include <string>
00027 #include <hdf5serie/vectorserie.h>
00028 #include "mbxmlutilstinyxml/tinyxml.h"
00029 
00030 #define MBSIMNS_ "http://mbsim.berlios.de/MBSim"
00031 #define MBSIMNS "{"MBSIMNS_"}"
00032 
00033 #include "mbsim_event.h"
00034 
00035 #ifdef HAVE_OPENMBVCPPINTERFACE
00036 namespace OpenMBV {
00037   class Group;
00038 }
00039 #endif
00040 
00041 namespace H5 {
00042   class Group;
00043 }
00044 
00048 namespace MBSim {
00049 
00050   class ObjectInterface;
00051 
00053   enum PlotFeatureStatus {
00054     enabled, 
00055     disabled, 
00056     unset 
00057   };
00058 
00059   // NOTE!!! When adding a new PlotFeature here, the default setting for this feature must
00060   // be specified in dynamic_system_solver.cc:DynamicSystemSolver::constructor() and the
00061   // new feature must also be added in
00062   // element.cc:Element::initializeUsingXML(MBXMLUtils::TiXmlElement *element)
00063   // and in
00064   // mbsimxml/schema/mbsim.xsd.in
00066   enum PlotFeature {
00067     plotRecursive=0, 
00068     separateFilePerGroup, 
00069     state, 
00070     stateDerivative, 
00071     notMinimalState, // TODO
00072     rightHandSide, 
00073     globalPosition, 
00074     globalVelocity, 
00075     globalAcceleration, 
00076     energy, 
00077     openMBV, 
00078     generalizedLinkForce, 
00079     linkKinematics, 
00080     stopVector, 
00081     debug, 
00082     LASTPLOTFEATURE
00083   };
00084 
00089   enum InitStage {
00090     modelBuildup=0, /*< build up complex internal models */
00091     resolveXMLPath, 
00092     preInit, 
00093     resize, 
00094     plot, 
00095     relativeFrameContourLocation, 
00096     worldFrameContourLocation, 
00097     reorganizeHierarchy, 
00098     unknownStage, 
00099     calculateLocalInitialValues, 
00100     LASTINITSTAGE
00101   };
00102 
00103   class DynamicSystemSolver;
00104 
00111   class Element {
00112     public:
00116       Element(const std::string &name);
00117 
00121       virtual ~Element();
00122 
00123       /* INTERFACE */
00127       virtual void initDataInterfaceBase(DynamicSystemSolver *parentds) {};
00128 
00132       virtual std::string getType() const { return "Element"; }
00133 
00138       virtual void setDynamicSystemSolver(DynamicSystemSolver *sys) { ds = sys; }
00139 
00145       virtual void plot(double t, double dt = 1);
00146 
00152       virtual void plotAtSpecialEvent(double t, double dt = 1) {}
00153 
00157       virtual void closePlot();
00158       /***************************************************/
00159 
00163       const std::string& getName() const { return name; }
00164 
00168       const std::string getShortName() const {
00169         size_t i = name.length() - 1;
00170 
00171         while ((name[i-1] != '/') and (i > 1)) {
00172           i--;
00173         }
00174         if(i == 1)
00175           i--;
00176 
00177         return name.substr(i, name.length() - i);
00178       }
00179 
00183       void setName(const std::string &str) { name = str; }
00184 
00188       DynamicSystemSolver* getDynamicSystemSolver() { return ds; }
00189 
00194       virtual void init(InitStage stage);
00195 
00199       virtual void createPlotGroup();
00200 
00204       H5::Group *getPlotGroup() { return plotGroup; }
00205 
00212       virtual void setPlotFeature(PlotFeature pf, PlotFeatureStatus value) { plotFeature[pf]=value; }
00213 
00219       void setPlotFeatureForChildren(PlotFeature pf, PlotFeatureStatus value) { plotFeatureForChildren[pf]=value; }
00220 
00226       void setPlotFeatureRecursive(PlotFeature pf, PlotFeatureStatus value) { plotFeature[pf]=value; plotFeatureForChildren[pf]=value; }
00227 
00231       PlotFeatureStatus getPlotFeature(PlotFeature pf) { return plotFeature[pf]; }
00232 
00236       PlotFeatureStatus getPlotFeatureForChildren(PlotFeature pf) { return plotFeatureForChildren[pf]; }
00237 
00238       virtual void initializeUsingXML(MBXMLUtils::TiXmlElement *element);
00239       virtual MBXMLUtils::TiXmlElement* writeXMLFile(MBXMLUtils::TiXmlNode *element);
00240 
00244       template<class T>
00245         T* getByPath(std::string path) {
00246           Element * e = getByPathSearch(path);
00247           if (dynamic_cast<T*>(e))
00248             return (T*)(e);
00249           else
00250             throw MBSimError("ERROR in "+getName()+" (Element::getByPath): Element \""+path+"\" not found or not of wanted type!");
00251         }
00252 
00256       virtual Element* getByPathSearch(std::string path) {return 0; }
00257 
00258       // some convenience function for XML
00259       static double getDouble(MBXMLUtils::TiXmlElement *e);
00260       static int getInt(MBXMLUtils::TiXmlElement *e);
00261       static bool getBool(MBXMLUtils::TiXmlElement *e);
00262       static fmatvec::Vec3 getVec3(MBXMLUtils::TiXmlElement *e);
00263       static fmatvec::Vec getVec(MBXMLUtils::TiXmlElement *e, int rows=0);
00264       static fmatvec::Mat3xV getMat3xV(MBXMLUtils::TiXmlElement *e, int cols=0);
00265       static fmatvec::Mat getMat(MBXMLUtils::TiXmlElement *e, int rows=0, int cols=0);
00266       static fmatvec::SqrMat3 getSqrMat3(MBXMLUtils::TiXmlElement *e);
00267       static fmatvec::SqrMat getSqrMat(MBXMLUtils::TiXmlElement *e, int size=0);
00268       static fmatvec::SymMat3 getSymMat3(MBXMLUtils::TiXmlElement *e);
00269       static fmatvec::SymMat getSymMat(MBXMLUtils::TiXmlElement *e, int size=0);
00270 
00271 #ifdef HAVE_OPENMBVCPPINTERFACE
00272       virtual OpenMBV::Group* getOpenMBVGrp() {return 0;}
00273 #endif
00274 
00275       virtual Element* getParent() {return parent;}
00276       virtual const Element* getParent() const {return parent;}
00277       virtual void setParent(Element* parent_) {parent = parent_;}
00282       std::string getPath(char pathDelim='.');
00283 
00284       std::string getXMLPath(MBSim::Element *ref=0, bool rel=false);
00285 
00286     protected:
00287       Element *parent;
00288 
00292       std::string name;
00293 
00297       DynamicSystemSolver *ds;
00298 
00302       H5::VectorSerie<double> *plotVectorSerie;
00303 
00307       std::vector<double> plotVector;
00308 
00312       std::vector<std::string> plotColumns;
00313 
00317       H5::Group *plotGroup;
00318 
00319       void updatePlotFeatures();
00320 
00321     private:
00325       PlotFeatureStatus plotFeature[LASTPLOTFEATURE], plotFeatureForChildren[LASTPLOTFEATURE];
00326   };
00327 
00328 }
00329 
00330 #endif