Node.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 //
00026 // C++ Interface: Node
00027 //
00028 // Description: 
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2008
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 #ifndef SOFA_COMPONENT_NODE_H
00037 #define SOFA_COMPONENT_NODE_H
00038 
00039 #include <sofa/core/objectmodel/Context.h>
00040 // moved from GNode (27/04/08)
00041 #include <sofa/core/objectmodel/BaseNode.h>
00042 #include <sofa/core/BehaviorModel.h>
00043 #include <sofa/core/objectmodel/ContextObject.h>
00044 #include <sofa/core/CollisionModel.h>
00045 #include <sofa/core/VisualModel.h>
00046 #include <sofa/core/Shader.h>
00047 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00048 #include <sofa/core/Mapping.h>
00049 #include <sofa/core/componentmodel/behavior/MechanicalMapping.h>
00050 #include <sofa/core/componentmodel/behavior/ForceField.h>
00051 #include <sofa/core/componentmodel/behavior/InteractionForceField.h>
00052 #include <sofa/core/componentmodel/behavior/Mass.h>
00053 #include <sofa/core/componentmodel/behavior/Constraint.h>
00054 #include <sofa/core/componentmodel/topology/Topology.h>
00055 #include <sofa/core/componentmodel/topology/BaseTopology.h>
00056 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00057 #include <sofa/core/componentmodel/behavior/LinearSolver.h>
00058 #include <sofa/core/componentmodel/behavior/OdeSolver.h>
00059 #include <sofa/core/componentmodel/behavior/MasterSolver.h>
00060 #include <sofa/core/componentmodel/collision/Pipeline.h>
00061 #include <sofa/core/objectmodel/Event.h>
00062 
00063 #include <sofa/simulation/common/VisitorScheduler.h>
00064 using sofa::simulation::VisitorScheduler;
00065 namespace sofa
00066 {
00067 namespace simulation
00068 {
00069 class Visitor;
00070 }
00071 }
00072 using sofa::simulation::Visitor;
00073 
00074 #include <sofa/helper/system/thread/CTime.h>
00075 #include <string>
00076 #include <stack>
00077 
00078 namespace sofa {
00079 
00080 namespace simulation {
00081 
00089 class Node : public sofa::core::objectmodel::Context
00090 {
00091     
00092 public:
00093     Node(const std::string& name="");
00094 
00095     virtual ~Node();
00096 
00100     void init();
00102     void reinit();
00104     void animate( double dt );
00106     void glDraw();
00108     
00111     
00114     virtual void doExecuteVisitor(Visitor* action)=0;
00115         
00117     void executeVisitor( simulation::Visitor* action); 
00118     
00120     void execute(simulation::Visitor& action)
00121     {
00122       simulation::Visitor* p = &action;
00123         executeVisitor(p);
00124     }
00125 
00127     void execute(simulation::Visitor* p)
00128     {
00129         executeVisitor(p);
00130     }
00131 
00133     template<class Act>
00134     void execute()
00135     {
00136         Act action;
00137         simulation::Visitor* p = &action;
00138         executeVisitor(p);
00139     }
00141 
00144         // methods moved from GNode (27/04/08)
00145 
00147     template < class T >
00148     class Sequence
00149     {
00150     protected:
00151         std::vector< T* > elems;
00152     public:
00153         typedef T* value_type;
00154         typedef typename std::vector< T* >::const_iterator iterator;
00155         typedef typename std::vector< T* >::const_reverse_iterator reverse_iterator;
00156 
00157         iterator begin() const
00158         {
00159             return elems.begin();
00160         }
00161         iterator end() const
00162         {
00163             return elems.end();
00164         }
00165         reverse_iterator rbegin() const
00166         {
00167             return elems.rbegin();
00168         }
00169         reverse_iterator rend() const
00170         {
00171             return elems.rend();
00172         }
00173         unsigned int size() const
00174         {
00175             return elems.size();
00176         }
00177         bool empty() const
00178         {
00179             return elems.empty();
00180         }
00181         T* operator[](unsigned int i) const
00182         {
00183             return elems[i];
00184         }
00186         void swap( iterator a, iterator b )
00187         {
00188             T*& wa = const_cast<T*&>(*a);
00189             T*& wb = const_cast<T*&>(*b);
00190             T* tmp = *a;
00191             wa = *b;
00192             wb = tmp;
00193         }
00194         //friend class Node;
00195         bool add
00196             (T* elem)
00197         {
00198             if (elem == NULL)
00199                 return false;
00200             elems.push_back(elem);
00201             return true;
00202         }
00203         bool remove
00204             (T* elem)
00205         {
00206             if (elem == NULL)
00207                 return false;
00208             typename std::vector< T* >::iterator it = elems.begin();
00209             while (it != elems.end() && (*it)!=elem)
00210                 ++it;
00211             if (it != elems.end())
00212             {
00213                 elems.erase(it);
00214                 return true;
00215             }
00216             else
00217                 return false;
00218         }
00219         void clear() { elems.clear(); }
00220     };
00221 
00223     template < class T >
00224     class Single
00225     {
00226     protected:
00227         T* elems[2];
00228     public:
00229         typedef T* value_type;
00230         typedef T* const * iterator;
00231 
00232         Single()
00233         {
00234             elems[0] = NULL;
00235             elems[1] = NULL;
00236         }
00237         iterator begin() const
00238         {
00239             return elems;
00240         }
00241         iterator end() const
00242         {
00243             return (elems[0]==NULL)?elems:elems+1;
00244         }
00245         unsigned int size() const
00246         {
00247             return (elems[0]==NULL)?0:1;
00248         }
00249         bool empty() const
00250         {
00251             return (elems[0]==NULL);
00252         }
00253         T* operator[](unsigned int i) const
00254         {
00255             return elems[i];
00256         }
00257         operator T*() const
00258         {
00259             return elems[0];
00260         }
00261         T* operator->() const
00262         {
00263             return elems[0];
00264         }
00265         //friend class Node;
00266         bool add
00267             (T* elem)
00268         {
00269             if (elem == NULL)
00270                 return false;
00271             elems[0] = elem;
00272             return true;
00273         }
00274         bool remove
00275             (T* elem)
00276         {
00277             if (elem == NULL)
00278                 return false;
00279             if (elems[0] == elem)
00280             {
00281                 elems[0] = NULL;
00282                 return true;
00283             }
00284             else
00285                 return false;
00286         }
00287     };
00288 
00289     Sequence<core::objectmodel::BaseObject> object;
00290     typedef Sequence<core::objectmodel::BaseObject>::iterator ObjectIterator;
00291 
00292     Single<core::componentmodel::behavior::MasterSolver> masterSolver;
00293     Sequence<core::componentmodel::behavior::OdeSolver> solver;
00294     Sequence<core::componentmodel::behavior::LinearSolver> linearSolver;
00295     Single<core::componentmodel::behavior::BaseMechanicalState> mechanicalState;
00296     Single<core::componentmodel::behavior::BaseMechanicalMapping> mechanicalMapping;
00297     Single<core::componentmodel::behavior::BaseMass> mass;
00298     Single<core::componentmodel::topology::Topology> topology;
00299     Single<core::componentmodel::topology::BaseMeshTopology> meshTopology;
00300     Single<sofa::core::Shader> shader;
00301 
00302     //warning : basic topology are not yet used in the release version
00303     Sequence<core::componentmodel::topology::BaseTopology> basicTopology;
00304 
00305     Sequence<core::componentmodel::behavior::BaseForceField> forceField;
00306     Sequence<core::componentmodel::behavior::InteractionForceField> interactionForceField;
00307     Sequence<core::componentmodel::behavior::BaseConstraint> constraint;
00308     Sequence<core::objectmodel::ContextObject> contextObject;
00309 
00310     Sequence<core::BaseMapping> mapping;
00311     Sequence<core::BehaviorModel> behaviorModel;
00312     Sequence<core::VisualModel> visualModel;
00313     Sequence<core::CollisionModel> collisionModel;
00314 
00315     Single<core::componentmodel::collision::Pipeline> collisionPipeline;
00316     Sequence<core::objectmodel::BaseObject> unsorted;
00318    
00319     
00322     
00323     
00325     virtual bool addObject(core::objectmodel::BaseObject* obj);
00326 
00328     virtual bool removeObject(core::objectmodel::BaseObject* obj);
00329 
00331     virtual void moveObject(core::objectmodel::BaseObject* obj);
00332 
00334     core::objectmodel::BaseObject* getObject(const std::string& name) const;
00335 
00337     template<class Object, class Container>
00338     void getNodeObjects(Container* list)
00339     {
00340         this->get<Object, Container>(list, Local);
00341     }
00342 
00345     template<class Object>
00346     void getNodeObject(Object*& result)
00347     {
00348         result = this->get<Object>(Local);
00349     }
00350 
00351     template<class Object>
00352     Object* getNodeObject()
00353     {
00354         return this->get<Object>(Local);
00355     }
00356 
00358     template<class Object, class Container>
00359     void getTreeObjects(Container* list)
00360     {
00361         this->get<Object, Container>(list, SearchDown);
00362     }
00363 
00366     template<class Object>
00367     void getTreeObject(Object*& result)
00368     {
00369         result = this->get<Object>(SearchDown);
00370     }
00371 
00372     template<class Object>
00373     Object* getTreeObject()
00374     {
00375         return this->get<Object>(SearchDown);
00376     }
00377     
00379     virtual core::componentmodel::topology::Topology* getTopology() const;
00380     
00382     virtual core::componentmodel::topology::BaseMeshTopology* getMeshTopology() const;
00383 
00385     virtual core::objectmodel::BaseObject* getMechanicalState() const;
00386     
00388     virtual core::objectmodel::BaseObject* getShader() const;
00389 
00391     virtual void removeControllers();
00392 
00394     
00397     
00398         void setLogTime(bool);
00399     bool getLogTime() const { return logTime_; }
00400 
00401     typedef helper::system::thread::ctime_t ctime_t;
00402 
00403     struct NodeTimer
00404     {
00405         ctime_t tNode; 
00406         ctime_t tTree; 
00407         int nVisit;    
00408     };
00409 
00410     struct ObjectTimer
00411     {
00412         ctime_t tObject; 
00413         int nVisit;    
00414     };
00415 
00417     void resetTime();
00418 
00420     const NodeTimer& getTotalTime() const { return totalTime; }
00421 
00423     const std::map<std::string, NodeTimer>& getVisitorTime() const { return actionTime; }
00424 
00426     const NodeTimer& getVisitorTime(const std::string& s) { return actionTime[s]; }
00427 
00429     const NodeTimer& getVisitorTime(const char* s) { return actionTime[s]; }
00430 
00432     const std::map<std::string, std::map<core::objectmodel::BaseObject*, ObjectTimer> >& getObjectTime() const { return objectTime; }
00433 
00435     const std::map<core::objectmodel::BaseObject*, ObjectTimer>& getObjectTime(const std::string& s) { return objectTime[s]; }
00436 
00438     const std::map<core::objectmodel::BaseObject*, ObjectTimer>& getObjectTime(const char* s) { return objectTime[s]; }
00439 
00441     ctime_t getTimeFreq() const;
00442 
00444     ctime_t startTime() const;
00445 
00447     virtual void addTime(ctime_t t, const std::string& s, core::objectmodel::BaseObject* obj);
00448 
00450     virtual ctime_t endTime(ctime_t t0, const std::string& s, core::objectmodel::BaseObject* obj);
00451 
00453     virtual void addTime(ctime_t t, const std::string& s, core::objectmodel::BaseObject* obj, core::objectmodel::BaseObject* parent);
00454 
00456     virtual ctime_t endTime(ctime_t t0, const std::string& s, core::objectmodel::BaseObject* obj, core::objectmodel::BaseObject* parent);
00458 
00459     Node* setDebug(bool);
00460     bool getDebug() const;
00461     // debug
00462     void printComponents();
00463     
00464     /*
00466     virtual core::objectmodel::BaseNode* getParent();
00467 
00469     virtual const core::objectmodel::BaseNode* getParent() const;
00470 
00472     virtual sofa::helper::vector< core::objectmodel::BaseNode* >  getChildren();
00473     
00475     virtual const sofa::helper::vector< core::objectmodel::BaseNode* >  getChildren() const;
00476     */
00477 
00478     const BaseContext* getContext() const;
00479     BaseContext* getContext();
00480 
00482     virtual void updateContext();
00483 
00485     virtual void updateSimulationContext();
00486     
00488     virtual void initVisualContext(){}
00489     
00491     virtual void propagateEvent( core::objectmodel::Event* event );
00492 
00494     virtual void updateVisualContext(int FILTER=10);
00495     
00496     Single<VisitorScheduler> actionScheduler;
00497     
00498     // VisitorScheduler can use doExecuteVisitor() method
00499     friend class VisitorScheduler;
00500     
00502     virtual void initialize();
00503     
00505     virtual void setDefaultVisualContextValue();
00506 
00507 protected:
00508     bool debug_;
00509     bool logTime_;
00510 
00513 
00514     NodeTimer totalTime;
00515     std::map<std::string, NodeTimer> actionTime;
00516     std::map<std::string, std::map<core::objectmodel::BaseObject*, ObjectTimer> > objectTime;
00517 
00519     
00520     virtual void doAddObject(core::objectmodel::BaseObject* obj);
00521     virtual void doRemoveObject(core::objectmodel::BaseObject* obj);
00522 
00523 
00524     std::stack<Visitor*> actionStack;
00525     virtual void notifyAddObject(core::objectmodel::BaseObject* ){}
00526     virtual void notifyRemoveObject(core::objectmodel::BaseObject* ){}
00527     virtual void notifyMoveObject(core::objectmodel::BaseObject* , Node* /*prev*/){}
00528     
00529     BaseContext* _context;
00530 
00531         // Added by FF to model component dependencies
00532   public:
00534     Data < sofa::helper::vector < std::string > > depend;
00536     void sortComponents();
00537 };
00538 
00539 }
00540 
00541 }
00542 
00543 #endif

Generated on Sun Nov 23 04:05:47 2008 for Sofa by  doxygen 1.5.7.1