Mapping.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 :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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 } // namespace core
00179 
00180 } // namespace sofa
00181 
00182 #endif

Generated on Sun Nov 23 02:29:25 2008 for Sofa by  doxygen 1.5.7.1