CoordinateSystem.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 :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 // Author: Fran�is Faure, INRIA-UJF, (C) 2006
00026 //
00027 // Copyright: See COPYING file that comes with this distribution
00028 
00029 #include <sofa/component/contextobject/CoordinateSystem.h>
00030 #include <sofa/core/ObjectFactory.h>
00031 #include <sofa/simulation/tree/GNode.h>
00032 #include <iostream>
00033 #include <sofa/helper/system/gl.h>
00034 
00035 using std::cerr;
00036 using std::endl;
00037 
00038 namespace sofa
00039 {
00040 
00041 namespace component
00042 {
00043 
00044 namespace contextobject
00045 {
00046 
00047 CoordinateSystem::CoordinateSystem()
00048   : origin(initData(&origin,defaulttype::Vec3f(),"origin", "Position of the local frame"))
00049     , orientation(initData(&orientation,defaulttype::Vec3f(),"orientation", "Orientation of the local frame"))
00050     , positionInParent_( Frame::identity() )
00051 //, velocity_( Vec(0,0,0), Vec(0,0,0) )
00052 {}
00053 
00054 
00055 // CoordinateSystem::Frame  CoordinateSystem::getPositionInWorld() const
00056 // {
00057 //     return core::objectmodel::BaseObject::getContext()->getPositionInWorld() * positionInParent_;
00058 // }
00059 
00060 const CoordinateSystem::Frame&  CoordinateSystem::getTransform() const
00061 {
00062     return positionInParent_;
00063 }
00064 
00065 void CoordinateSystem::setTransform( const Frame& f )
00066 {
00067     positionInParent_ = f;
00068 }
00069 
00070 // CoordinateSystem* CoordinateSystem::setOrigin( const Vec& v )
00071 // {
00072 //     positionInParent_ = Frame( v, this->getOrientation() );
00073 //     return this;
00074 // }
00075 // 
00076 CoordinateSystem::Vec CoordinateSystem::getOrigin() const
00077 {
00078     return positionInParent_.getOrigin();
00079 }
00080 // 
00081 // CoordinateSystem* CoordinateSystem::setOrientation( const Rot& r )
00082 // {
00083 //     positionInParent_ = Frame( this->getOrigin(), r );
00084 //     return this;
00085 // }
00086 // 
00087 CoordinateSystem::Rot CoordinateSystem::getOrientation( ) const
00088 {
00089     return positionInParent_.getOrientation();
00090 }
00091 // 
00092 // CoordinateSystem* CoordinateSystem::set( const Vec& v, const Rot& r )
00093 // {
00094 //     positionInParent_ = Frame( v, r );
00095 //     return this;
00096 // }
00097 
00098 /*const CoordinateSystem::SpatialVector&  CoordinateSystem::getVelocity() const
00099 {
00100     return velocity_;
00101 }*/
00102 /*CoordinateSystem::SpatialVector  CoordinateSystem::getVelocityInWorld() const
00103 {
00104     return core::objectmodel::BaseObject::getContext()->getVelocityInWorld() + getPositionInWorld() * velocity__;
00105 }*/
00106 // CoordinateSystem* CoordinateSystem::setVelocity( const SpatialVector& f )
00107 // {
00108 //     velocity_ = f;
00109 //     return this;
00110 // }
00111 
00112 
00113 void CoordinateSystem::apply()
00114 {
00115     //cerr<<"CoordinateSystem::apply(), frame = "<<   getName() <<", t="<<getContext()->getTime() << endl;
00116     core::objectmodel::BaseContext* context = getContext();
00117     //cerr<<"CoordinateSystem::apply, current position = "<<context->getPositionInWorld()<<endl;
00118     //cerr<<"CoordinateSystem::apply, transform = "<<this->getTransform()<<endl;
00119 
00120     // store parent position and velocity
00121     Frame parentToWorld = context->getPositionInWorld();
00122     SpatialVector parentSpatialVelocity = context->getVelocityInWorld();
00123     Vec parentLinearVelocity = parentSpatialVelocity.getLinearVelocity() ;
00124     Vec parentAngularVelocity = parentSpatialVelocity.getAngularVelocity() ;
00125     Vec parentLinearAcceleration = context->getVelocityBasedLinearAccelerationInWorld() ;
00126 
00127 
00128     // Velocity induced by the rotation of the parent frame. Local origin is defined in parent frame.
00129     Vec originInParentProjected = parentToWorld.projectVector(getOrigin());
00130     Vec vinduced = parentAngularVelocity.cross( originInParentProjected );
00131     // Acceleration induced by the rotation of the parent frame. Local origin is defined in parent frame.
00132     Vec ainduced = parentAngularVelocity.cross( vinduced );
00133 
00134 
00135 
00136 
00137     // update context
00138     defaulttype::Vec3d newLinearAcceleration = parentLinearAcceleration + ainduced;
00139     Frame newLocalToWorld = parentToWorld * getTransform();
00140     SpatialVector newSpatialVelocity ( parentSpatialVelocity /*+ newLocalToWorld * getVelocity()*/ );
00141 
00142     context->setVelocityBasedLinearAccelerationInWorld( newLinearAcceleration );
00143     context->setPositionInWorld( newLocalToWorld );
00144     context->setVelocityInWorld( newSpatialVelocity );
00145     //cerr<<"CoordinateSystem::apply, new position = "<<context->getPositionInWorld()<<endl;
00146 
00147 }
00148 
00149 
00150 
00151 void CoordinateSystem::draw()
00152 {
00153 /*
00154     glPushAttrib(GL_COLOR_BUFFER_BIT | GL_LIGHTING_BIT);
00155     glDisable(GL_LIGHTING);
00156     glBegin( GL_LINES );
00157     glColor3f( 1.f,0.f,0.f );
00158     glVertex3f( 0.f,0.f,0.f );
00159     glVertex3f( 1.f,0.f,0.f );
00160     glColor3f( 0.f,1.f,0.f );
00161     glVertex3f( 0.f,0.f,0.f );
00162     glVertex3f( 0.f,1.f,0.f );
00163     glColor3f( 0.f,0.f,1.f );
00164     glVertex3f( 0.f,0.f,0.f );
00165     glVertex3f( 0.f,0.f,1.f );
00166     glEnd();
00167     glPopAttrib();
00168     */
00169 }
00170 
00171 using namespace sofa::defaulttype; 
00172 
00173 
00174 
00175 void CoordinateSystem::reinit()
00176 {
00177   typedef CoordinateSystem::Frame Frame;
00178   typedef CoordinateSystem::Vec Vec;
00179   typedef CoordinateSystem::Rot Rot;
00180   setTransform( Frame( origin.getValue(), Rot::createFromRotationVector( orientation.getValue() ) ));
00181 }
00182 
00183 
00184 void CoordinateSystem::init()
00185 {
00186   reinit();
00187 }
00188 SOFA_DECL_CLASS(CoordinateSystem)
00189 
00190 int CoordinateSystemClass = core::RegisterObject("Translation and orientation of the local reference frame with respect to its parent")
00191 .add< CoordinateSystem >()
00192 ;
00193 
00194 } // namespace contextobject
00195 
00196 } // namespace component
00197 
00198 } // namespace sofa
00199 

Generated on Sun Nov 23 02:49:36 2008 for Sofa by  doxygen 1.5.7.1