Mapping.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SOFA_CORE_MAPPING_H
00028 #define SOFA_CORE_MAPPING_H
00029
00030 #include <sofa/core/BaseMapping.h>
00031 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00032
00033 namespace sofa
00034 {
00035
00036 namespace core
00037 {
00038
00046 template <class TIn, class TOut>
00047 class Mapping : public BaseMapping
00048 {
00049 public:
00051 typedef TIn In;
00053 typedef TOut Out;
00054
00055 protected:
00057 In* fromModel;
00059 Out* toModel;
00061 Data< std::string > object1;
00063 Data< std::string > object2;
00064 public:
00070 Mapping(In* from=NULL, Out* to=NULL);
00072 virtual ~Mapping();
00073
00075 virtual void setModels(In* from, Out* to);
00076
00078 In* getFromModel();
00080 Out* getToModel();
00081
00083 objectmodel::BaseObject* getFrom();
00085 objectmodel::BaseObject* getTo();
00086
00093 virtual void apply( typename Out::VecCoord& out, const typename In::VecCoord& in ) = 0;
00094
00101 virtual void applyJ( typename Out::VecDeriv& out, const typename In::VecDeriv& in ) = 0;
00102
00103 virtual void init();
00104
00109 virtual void updateMapping();
00110
00114 virtual void disable();
00115
00121 template<class T>
00122 static bool canCreate(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
00123 {
00124 if (arg->findObject(arg->getAttribute("object1","../..")) == NULL)
00125 std::cerr << "Cannot create "<<className(obj)<<" as object1 is missing.\n";
00126 if (arg->findObject(arg->getAttribute("object2","..")) == NULL)
00127 std::cerr << "Cannot create "<<className(obj)<<" as object2 is missing.\n";
00128 if (dynamic_cast<In*>(arg->findObject(arg->getAttribute("object1","../.."))) == NULL)
00129 return false;
00130 if (dynamic_cast<Out*>(arg->findObject(arg->getAttribute("object2",".."))) == NULL)
00131 return false;
00132 return BaseMapping::canCreate(obj, context, arg);
00133 }
00134
00139 template<class T>
00140 static void create(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
00141 {
00142 obj = new T(
00143 (arg?dynamic_cast<In*>(arg->findObject(arg->getAttribute("object1","../.."))):NULL),
00144 (arg?dynamic_cast<Out*>(arg->findObject(arg->getAttribute("object2",".."))):NULL));
00145 if (context) context->addObject(obj);
00146 if ((arg) && (arg->getAttribute("object1")))
00147 {
00148 obj->object1.setValue( arg->getAttribute("object1") );
00149 arg->removeAttribute("object1");
00150 }
00151 if ((arg) && (arg->getAttribute("object2")))
00152 {
00153 obj->object2.setValue( arg->getAttribute("object2") );
00154 arg->removeAttribute("object2");
00155 }
00156 if (arg) obj->parse(arg);
00157 }
00158
00159 virtual std::string getTemplateName() const
00160 {
00161 return templateName(this);
00162 }
00163
00164
00165 static std::string templateName(const Mapping<TIn, TOut>* = NULL)
00166 {
00167 if (In::Name() == std::string("MechanicalState"))
00168 return std::string("MechanicalMapping[")+TIn::DataTypes::Name() + std::string(",") + TOut::DataTypes::Name() + std::string("]");
00169 else
00170 return std::string("Mapping[")+TIn::DataTypes::Name() + std::string(",") + TOut::DataTypes::Name() + std::string("]");
00171 }
00172
00173 protected:
00175 bool getShow() const { return this->getContext()->getShowMappings(); }
00176 };
00177
00178 }
00179
00180 }
00181
00182 #endif