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_WRITESTATE_H 00026 #define SOFA_COMPONENT_MISC_WRITESTATE_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/defaulttype/DataTypeInfo.h> 00035 #include <sofa/simulation/common/Visitor.h> 00036 00037 #include <fstream> 00038 00039 namespace sofa 00040 { 00041 00042 namespace component 00043 { 00044 00045 namespace misc 00046 { 00047 00054 class WriteState: public core::objectmodel::BaseObject 00055 { 00056 public: 00057 Data < std::string > f_filename; 00058 Data < bool > f_writeX; 00059 Data < bool > f_writeV; 00060 Data < double > f_interval; 00061 Data < helper::vector<double> > f_time; 00062 Data < double > f_period; 00063 Data < helper::vector<unsigned int> > f_DOFsX; 00064 Data < helper::vector<unsigned int> > f_DOFsV; 00065 Data < double > f_stopAt; 00066 Data < double > f_keperiod; 00067 00068 protected: 00069 core::componentmodel::behavior::BaseMechanicalState* mmodel; 00070 std::ofstream* outfile; 00071 unsigned int nextTime; 00072 double lastTime; 00073 bool kineticEnergyThresholdReached; 00074 double timeToTestEnergyIncrease; 00075 double savedKineticEnergy; 00076 00077 public: 00078 WriteState(); 00079 00080 virtual ~WriteState(); 00081 00082 virtual void init(); 00083 00084 virtual void reset(); 00085 00086 virtual void handleEvent(sofa::core::objectmodel::Event* event); 00087 00088 00091 template<class T> 00092 static bool canCreate(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg) 00093 { 00094 if (dynamic_cast<core::componentmodel::behavior::BaseMechanicalState*>(context->getMechanicalState()) == NULL) 00095 return false; 00096 return BaseObject::canCreate(obj, context, arg); 00097 } 00098 00099 }; 00100 00102 class WriteStateCreator: public Visitor 00103 { 00104 public: 00105 WriteStateCreator() : sceneName(""), counterWriteState(0), createInMapping(false) {} 00106 WriteStateCreator(std::string &n, int c=0) { sceneName=n; counterWriteState=c; } 00107 virtual Result processNodeTopDown( simulation::Node* ); 00108 00109 void setSceneName(std::string &n) { sceneName = n; } 00110 void setCounter(int c) { counterWriteState = c; } 00111 void setCreateInMapping(bool b) { createInMapping=b; } 00112 protected: 00113 void addWriteState(sofa::core::componentmodel::behavior::BaseMechanicalState*ms, simulation::Node* gnode); 00114 00115 std::string sceneName; 00116 int counterWriteState; //avoid to have two same files if two mechanical objects has the same name 00117 bool createInMapping; 00118 }; 00119 00120 class WriteStateActivator: public simulation::Visitor 00121 { 00122 public: 00123 WriteStateActivator( bool active) : state(active) {} 00124 virtual Result processNodeTopDown( simulation::Node* ); 00125 00126 bool getState() const { return state; } 00127 void setState(bool active) { state=active; } 00128 protected: 00129 void changeStateWriter(sofa::component::misc::WriteState *ws); 00130 00131 bool state; 00132 }; 00133 00134 } // namespace misc 00135 00136 } // namespace component 00137 00138 } // namespace sofa 00139 00140 #endif
1.5.7.1