Context.cpp

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 #include <sofa/core/objectmodel/Context.h>
00028 // #include <sofa/simulation/common/Visitor.h>
00029 
00030 
00031 namespace sofa
00032 {
00033 
00034 namespace core
00035 {
00036 
00037 namespace objectmodel
00038 {
00039 
00040 Context::Context()
00041   : is_activated(initData(&is_activated, true, "activated", "To Activate a node"))
00042   , worldGravity_(initData(&worldGravity_, Vec3(0,0,0),"gravity","Gravity in the world coordinate system"))
00043   , dt_(initData(&dt_,0.01,"dt","Time step"))
00044   , time_(initData(&time_,0.,"time","Current time"))
00045   , animate_(initData(&animate_,false,"animate","Animate the Simulation(applied at initialization only)"))
00046   , showVisualModels_           (initData(&showVisualModels_,           -1, "showVisualModels","display Visual Models"))
00047   , showBehaviorModels_         (initData(&showBehaviorModels_,         -1,"showBehaviorModels","display Behavior Models"))
00048   , showCollisionModels_        (initData(&showCollisionModels_,        -1,"showCollisionModels","display Collision Models"))
00049   , showBoundingCollisionModels_(initData(&showBoundingCollisionModels_,-1,"showBoundingCollisionModels","display Bounding Collision Models"))        
00050   , showMappings_               (initData(&showMappings_,               -1,"showMappings","display Mappings"))
00051   , showMechanicalMappings_     (initData(&showMechanicalMappings_,     -1,"showMechanicalMappings","display Mechanical Mappings"))
00052   , showForceFields_            (initData(&showForceFields_,            -1,"showForceFields","display Force Fields"))
00053   , showInteractionForceFields_ (initData(&showInteractionForceFields_, -1,"showInteractionForceFields","display Interaction Force Fields"))
00054   , showWireFrame_              (initData(&showWireFrame_,              -1,"showWireFrame","display in WireFrame"))
00055   , showNormals_                (initData(&showNormals_,                -1,"showNormals","display Normals"))
00056   , multiThreadSimulation_(initData(&multiThreadSimulation_,false,"multiThreadSimulation","Apply multithreaded simulation"))
00057   , currentLevel_(initData(&currentLevel_,0,"currentLevel","Current level of details"))
00058   , coarsestLevel_(initData(&coarsestLevel_,3,"coarsestLevel","Coarsest level of details"))
00059   , finestLevel_(initData(&finestLevel_,0,"finestLevel","Finest level of details"))
00060 {
00061     setPositionInWorld(objectmodel::BaseContext::getPositionInWorld());
00062     setGravityInWorld(objectmodel::BaseContext::getLocalGravity());
00063     setVelocityInWorld(objectmodel::BaseContext::getVelocityInWorld());
00064     setVelocityBasedLinearAccelerationInWorld(objectmodel::BaseContext::getVelocityBasedLinearAccelerationInWorld());
00065     //setDt(objectmodel::BaseContext::getDt());
00066     //setTime(objectmodel::BaseContext::getTime());
00067     //setAnimate(objectmodel::BaseContext::getAnimate());
00068     //setShowCollisionModels(objectmodel::BaseContext::getShowCollisionModels());
00069     //setShowBoundingCollisionModels(objectmodel::BaseContext::getShowBoundingCollisionModels());
00070     //setShowBehaviorModels(objectmodel::BaseContext::getShowBehaviorModels());
00071     //setShowVisualModels(objectmodel::BaseContext::getShowVisualModels());
00072     //setShowMappings(objectmodel::BaseContext::getShowMappings());
00073     //setShowMechanicalMappings(objectmodel::BaseContext::getShowMechanicalMappings());
00074     //setShowForceFields(objectmodel::BaseContext::getShowForceFields());
00075     //setShowInteractionForceFields(objectmodel::BaseContext::getShowInteractionForceFields());
00076     //setShowWireFrame(objectmodel::BaseContext::getShowWireFrame());
00077     //setShowNormals(objectmodel::BaseContext::getShowNormals());
00078     //setMultiThreadSimulation(objectmodel::BaseContext::getMultiThreadSimulation());   
00079 }
00080 
00082 bool Context::isActive() const {return is_activated.getValue();}
00083 
00085 void Context::setActive(bool val){ is_activated.setValue(val);}
00086 
00088 const Context::Frame& Context::getPositionInWorld() const
00089 {
00090     return localFrame_;
00091 }
00093 void Context::setPositionInWorld(const Frame& f)
00094 {
00095     localFrame_ = f;
00096 }
00097 
00099 const Context::SpatialVector& Context::getVelocityInWorld() const
00100 {
00101     return spatialVelocityInWorld_;
00102 }
00104 void Context::setVelocityInWorld(const SpatialVector& v)
00105 {
00106     spatialVelocityInWorld_ = v;
00107 }
00108 
00110 const Context::Vec3& Context::getVelocityBasedLinearAccelerationInWorld() const
00111 {
00112     return velocityBasedLinearAccelerationInWorld_;
00113 }
00115 void Context::setVelocityBasedLinearAccelerationInWorld(const Vec3& a )
00116 {
00117     velocityBasedLinearAccelerationInWorld_ = a;
00118 }
00119 
00120 
00121 
00123 double Context::getDt() const
00124 {
00125     return dt_.getValue();
00126 }
00127 
00129 double Context::getTime() const
00130 {
00131     return time_.getValue();
00132 }
00133 
00135 // const Context::Vec3& Context::getGravity() const
00136 // {
00137 //  return gravity_;
00138 // }
00139 
00141 Context::Vec3 Context::getLocalGravity() const
00142 {
00143     return getPositionInWorld().backProjectVector(worldGravity_.getValue());
00144 }
00145 
00147 const Context::Vec3& Context::getGravityInWorld() const
00148 {
00149     return worldGravity_.getValue();
00150 }
00151 
00152 
00153 
00155 bool Context::getAnimate() const
00156 {
00157     return animate_.getValue();
00158 }
00159 
00161 bool Context::getMultiThreadSimulation() const
00162 {
00163     return multiThreadSimulation_.getValue();
00164 }
00165 
00167 bool Context::getShowCollisionModels() const
00168 {
00169   if (showCollisionModels_.getValue() < 0) return false;
00170   else return showCollisionModels_.getValue() != 0;
00171 }
00172 
00174 bool Context::getShowBoundingCollisionModels() const
00175 {
00176   if (showBoundingCollisionModels_.getValue() < 0) return false;
00177   else return showBoundingCollisionModels_.getValue()!= 0;
00178 }
00179 
00181 bool Context::getShowBehaviorModels() const
00182 {
00183   if (showBehaviorModels_.getValue() < 0) return false;
00184   else  return showBehaviorModels_.getValue()!= 0;
00185 }
00186 
00188 bool Context::getShowVisualModels() const
00189 {
00190   if (showVisualModels_.getValue() < 0) return true;
00191   else return showVisualModels_.getValue()!= 0;
00192 }
00193 
00195 bool Context::getShowMappings() const
00196 {
00197   if (showMappings_.getValue() < 0) return false;
00198   else return showMappings_.getValue()!= 0;
00199 }
00200 
00202 bool Context::getShowMechanicalMappings() const
00203 {
00204   if (showMechanicalMappings_.getValue() < 0) return false;
00205   else return showMechanicalMappings_.getValue()!= 0;
00206 }
00207 
00209 bool Context::getShowForceFields() const
00210 {
00211   if (showForceFields_.getValue() < 0) return false;
00212   else return showForceFields_.getValue()!= 0;
00213 }
00214 
00216 bool Context::getShowInteractionForceFields() const
00217 {
00218   if (showInteractionForceFields_.getValue() < 0) return false;
00219   else return showInteractionForceFields_.getValue()!= 0;
00220 }
00221 
00223 bool Context::getShowWireFrame() const
00224 {
00225   if (showWireFrame_.getValue() < 0) return false;
00226   else return showWireFrame_.getValue()!= 0;
00227 }
00228 
00230 bool Context::getShowNormals() const
00231 {
00232   if (showNormals_.getValue() < 0) return false;
00233   else return showNormals_.getValue()!= 0;
00234 }
00235 
00236 
00237 // Multiresolution
00238 
00239 int Context::getCurrentLevel() const
00240 {
00241     return currentLevel_.getValue();
00242 }
00243 int Context::getCoarsestLevel() const
00244 {
00245     return coarsestLevel_.getValue();
00246 }
00247 int Context::getFinestLevel() const
00248 {
00249     return finestLevel_.getValue();
00250 }
00251 
00252 
00253 //===============================================================================
00254 
00256 void Context::setDt(double val)
00257 {
00258     dt_.setValue(val);
00259 }
00260 
00262 void Context::setTime(double val)
00263 {
00264     time_.setValue(val);
00265 }
00266 
00268 // void Context::setGravity(const Vec3& g)
00269 // {
00270 //  gravity_ = g;
00271 // }
00272 
00274 void Context::setGravityInWorld(const Vec3& g)
00275 {
00276     worldGravity_ .setValue(g);
00277 }
00278 
00280 void Context::setAnimate(bool val)
00281 {
00282     animate_.setValue(val);
00283 }
00284 
00286 void Context::setMultiThreadSimulation(bool val)
00287 {
00288     multiThreadSimulation_.setValue(val);
00289 }
00290 
00292 void Context::setShowCollisionModels(bool val)
00293 {
00294     showCollisionModels_.setValue(val);
00295 }
00296 
00298 void Context::setShowBoundingCollisionModels(bool val)
00299 {
00300     showBoundingCollisionModels_.setValue(val);
00301 }
00302 
00304 void Context::setShowBehaviorModels(bool val)
00305 {
00306     showBehaviorModels_.setValue(val);
00307 }
00308 
00310 void Context::setShowVisualModels(bool val)
00311 {
00312     showVisualModels_.setValue(val);
00313 }
00314 
00316 void Context::setShowMappings(bool val)
00317 {
00318     showMappings_.setValue(val);
00319 }
00320 
00322 void Context::setShowMechanicalMappings(bool val)
00323 {
00324     showMechanicalMappings_.setValue(val);
00325 }
00326 
00328 void Context::setShowForceFields(bool val)
00329 {
00330     showForceFields_.setValue(val);
00331 }
00332 
00334 void Context::setShowInteractionForceFields(bool val)
00335 {
00336     showInteractionForceFields_.setValue(val);
00337 }
00338 
00340 void Context::setShowWireFrame(bool val)
00341 {
00342     showWireFrame_.setValue(val);
00343 }
00344 
00346 void Context::setShowNormals(bool val)
00347 {
00348     showNormals_.setValue(val);
00349 }
00350 
00351 
00352 // Multiresolution
00353 
00354 bool Context::setCurrentLevel(int l)
00355 {
00356     if( l > coarsestLevel_.getValue() )
00357     {
00358         currentLevel_.setValue(coarsestLevel_.getValue());
00359         return false;
00360     }
00361     else if( l < 0 /*finestLevel_.getValue()*/ )
00362     {
00363 //      currentLevel_.setValue(finestLevel_.getValue());
00364         currentLevel_.setValue( 0 );
00365         return false;
00366     }
00367     currentLevel_.setValue(l);
00368     if( l == coarsestLevel_.getValue() ) return false;
00369     return true;
00370 }
00371 void Context::setCoarsestLevel(int l)
00372 {
00373     coarsestLevel_.setValue( l );
00374 }
00375 void Context::setFinestLevel(int l)
00376 {
00377     finestLevel_.setValue( l );
00378 }
00379 
00380 //======================
00381 
00382 
00383 void Context::copyContext(const Context& c)
00384 {
00385     // BUGFIX 12/01/06 (Jeremie A.): Can't use operator= on the class as it will copy other data in the BaseContext class (such as name)...
00386     // *this = c;
00387 
00388     copySimulationContext(c);
00389     copyVisualContext(c);    
00390 }
00391 
00392 
00393 void Context::copySimulationContext(const Context& c)
00394 {
00395   worldGravity_.setValue(c.worldGravity_.getValue());  
00396   dt_.setValue(c.dt_.getValue());
00397   time_.setValue(c.time_.getValue());
00398   animate_.setValue(c.animate_.getValue());
00399   multiThreadSimulation_.setValue(c.multiThreadSimulation_.getValue());
00400   
00401   localFrame_ = c.localFrame_;
00402   spatialVelocityInWorld_ = c.spatialVelocityInWorld_;
00403   velocityBasedLinearAccelerationInWorld_ = c.velocityBasedLinearAccelerationInWorld_;
00404     
00405     // for multiresolution
00406 //  finestLevel_ = c.finestLevel_;
00407 //  coarsestLevel_ = c.coarsestLevel_;
00408 //  currentLevel_ = c.currentLevel_;
00409 
00410 }
00411 
00412 void Context::copyVisualContext(const Context& c)
00413 {
00414   showCollisionModels_.setValue(c.showCollisionModels_.getValue());
00415   showBoundingCollisionModels_.setValue(c.showBoundingCollisionModels_.getValue());
00416   showBehaviorModels_.setValue(c.showBehaviorModels_.getValue());
00417   showVisualModels_.setValue(c.showVisualModels_.getValue());
00418   showMappings_.setValue(c.showMappings_.getValue());
00419   showMechanicalMappings_.setValue(c.showMechanicalMappings_.getValue());
00420   showForceFields_.setValue(c.showForceFields_.getValue());
00421   showInteractionForceFields_.setValue(c.showInteractionForceFields_.getValue());
00422   showWireFrame_.setValue(c.showWireFrame_.getValue());
00423   showNormals_.setValue(c.showNormals_.getValue());
00424 }
00425 
00426 std::ostream& operator << (std::ostream& out, const Context& c )
00427 {
00428     out<<std::endl<<"local gravity = "<<c.getLocalGravity();
00429     out<<std::endl<<"transform from local to world = "<<c.getPositionInWorld();
00430     //out<<std::endl<<"transform from world to local = "<<c.getWorldToLocal();
00431     out<<std::endl<<"spatial velocity = "<<c.getVelocityInWorld();
00432     out<<std::endl<<"acceleration of the origin = "<<c.getVelocityBasedLinearAccelerationInWorld();
00433     return out;
00434 }
00435 
00436 
00437 
00438 
00439 
00440 } // namespace objectmodel
00441 
00442 } // namespace core
00443 
00444 } // namespace sofa
00445 

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