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 #ifndef SOFA_COMPONENT_MISC_READSTATE_H 00026 #define SOFA_COMPONENT_MISC_READSTATE_H 00027 00028 #include <sofa/core/componentmodel/behavior/ForceField.h> 00029 #include <sofa/core/componentmodel/behavior/BaseMechanicalState.h> 00030 #include <sofa/core/objectmodel/BaseObject.h> 00031 #include <sofa/core/objectmodel/Event.h> 00032 #include <sofa/simulation/common/AnimateBeginEvent.h> 00033 #include <sofa/simulation/common/AnimateEndEvent.h> 00034 #include <sofa/simulation/common/Visitor.h> 00035 00036 #include <fstream> 00037 00038 namespace sofa 00039 { 00040 00041 namespace component 00042 { 00043 00044 namespace misc 00045 { 00046 00049 class ReadState: public core::objectmodel::BaseObject 00050 { 00051 public: 00052 00053 Data < std::string > f_filename; 00054 Data < double > f_interval; 00055 Data < double > f_shift; 00056 00057 protected: 00058 core::componentmodel::behavior::BaseMechanicalState* mmodel; 00059 std::ifstream* infile; 00060 double nextTime; 00061 double lastTime; 00062 public: 00063 ReadState(); 00064 00065 virtual ~ReadState(); 00066 00067 virtual void init(); 00068 00069 virtual void reset(); 00070 00071 void setTime(double time); 00072 00073 virtual void handleEvent(sofa::core::objectmodel::Event* event); 00074 00075 void processReadState(); 00076 void processReadState(double time); 00077 00080 template<class T> 00081 static bool canCreate(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg) 00082 { 00083 if (dynamic_cast<core::componentmodel::behavior::BaseMechanicalState*>(context->getMechanicalState()) == NULL) 00084 return false; 00085 return BaseObject::canCreate(obj, context, arg); 00086 } 00087 00088 00089 }; 00090 00091 00093 class ReadStateCreator: public Visitor 00094 { 00095 public: 00096 ReadStateCreator():sceneName(""), counterReadState(0), createInMapping(false) {} 00097 ReadStateCreator(std::string &n, bool i=true, int c=0 ) { sceneName=n;init=i; counterReadState=c; } 00098 virtual Result processNodeTopDown( simulation::Node* ); 00099 00100 void setSceneName(std::string &n){ sceneName = n;} 00101 void setCounter(int c){counterReadState = c;}; 00102 void setCreateInMapping(bool b){createInMapping=b;} 00103 protected: 00104 void addReadState(sofa::core::componentmodel::behavior::BaseMechanicalState *ms, simulation::Node* gnode); 00105 bool init; 00106 std::string sceneName; 00107 int counterReadState; //avoid to have two same files if two mechanical objects has the same name 00108 bool createInMapping; 00109 }; 00110 00111 class ReadStateActivator: public Visitor 00112 { 00113 public: 00114 ReadStateActivator( bool active):state(active) {} 00115 virtual Result processNodeTopDown( simulation::Node* ); 00116 00117 bool getState() const{return state;}; 00118 void setState(bool active){state=active;}; 00119 protected: 00120 void changeStateReader(sofa::component::misc::ReadState *ws); 00121 00122 bool state; 00123 }; 00124 00125 class ReadStateModifier: public simulation::Visitor 00126 { 00127 public: 00128 ReadStateModifier( double _time):time(_time) {} 00129 virtual Result processNodeTopDown( simulation::Node* ); 00130 00131 double getTime() const { return time; } 00132 void setTime(double _time) { time=_time; } 00133 protected: 00134 void changeTimeReader(sofa::component::misc::ReadState *rs) { rs->processReadState(time); } 00135 00136 double time; 00137 }; 00138 00139 } // namespace misc 00140 00141 } // namespace component 00142 00143 } // namespace sofa 00144 00145 #endif
1.5.7.1