ArticulatedHierarchyBVHController.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 //
00026 // C++ Implementation: ArticulatedHierarchyBVHController
00027 //
00028 // Description: 
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2008
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 #include <sofa/component/controller/ArticulatedHierarchyBVHController.h>
00037 #include <sofa/helper/system/FileRepository.h>
00038 #include <sofa/core/ObjectFactory.h>
00039 
00040 namespace sofa
00041 {
00042 
00043 namespace component
00044 {
00045 
00046 namespace controller
00047 {
00048 
00049 void ArticulatedHierarchyBVHController::init()
00050 {
00051     sofa::simulation::tree::GNode* curNode = dynamic_cast<sofa::simulation::tree::GNode*>(this->getContext());
00052     if (curNode)
00053     {
00054         curNode->getTreeObjects<ArticulatedHierarchyContainer::ArticulationCenter, ArtCenterVec >(&m_artCenterVec);
00055         curNode->getTreeObject(ahc);
00056         frame = 0;
00057         n=0;
00058     }   
00059 }
00060 
00061 void ArticulatedHierarchyBVHController::reset()
00062 {
00063     frame = 0;
00064     n=0;
00065 }
00066 
00067 void ArticulatedHierarchyBVHController::applyController(void)
00068 {
00069     double ndiv = 1.0/ahc->dtbvh;
00070     int frameInc = 1;
00071     if (ndiv < 1.0)
00072         frameInc = int(ahc->numOfFrames/(ahc->numOfFrames/ahc->dtbvh));
00073 
00074     for (unsigned int i=0; i<m_artCenterVec.size(); i++)
00075     {
00076         ArtCenterVecIt artCenterIt = m_artCenterVec.begin();
00077         ArtCenterVecIt artCenterItEnd = m_artCenterVec.end();
00078         
00079         while ((artCenterIt != artCenterItEnd))
00080         {
00081             ArtVecIt it = (*artCenterIt)->articulations.begin();
00082             ArtVecIt itEnd = (*artCenterIt)->articulations.end();
00083             while (it != itEnd)
00084             {
00085                 std::vector< MechanicalState<sofa::defaulttype::Vec1dTypes>* > articulatedObjects;
00086 
00087                 sofa::simulation::tree::GNode* curNode = dynamic_cast<sofa::simulation::tree::GNode*>(this->getContext());
00088                 if (curNode)
00089                     curNode->getTreeObjects<MechanicalState<sofa::defaulttype::Vec1dTypes>, std::vector< MechanicalState<sofa::defaulttype::Vec1dTypes>* > >(&articulatedObjects);
00090 
00091                     if (!articulatedObjects.empty())
00092                     {
00093                         // Reference potential initial articulations value for interaction springs 
00094                         // and Current articulation value at the coresponding artculation
00095 
00096                         std::vector< MechanicalState<sofa::defaulttype::Vec1dTypes>* >::iterator articulatedObjIt = articulatedObjects.begin();
00097                         std::vector< MechanicalState<sofa::defaulttype::Vec1dTypes>* >::iterator articulatedObjItEnd = articulatedObjects.end();
00098 
00099                         //  while (articulatedObjIt != articulatedObjItEnd)
00100                         if ((*it)->translation.getValue())
00101                         {
00102                             double diffMotions = (*it)->motion[frame+1] - (*it)->motion[frame];
00103                             (*(*articulatedObjIt)->getX())[(*it)->articulationIndex.getValue()] = (*it)->motion[frame] + (n/ndiv)*diffMotions;
00104                             (*(*articulatedObjIt)->getXfree())[(*it)->articulationIndex.getValue()] = (*it)->motion[frame] + (n/ndiv)*diffMotions;
00105                         }
00106                         else
00107                         {
00108                             double diffMotions = (((*it)->motion[frame+1]/180.0)*3.14) - (((*it)->motion[frame]/180.0)*3.14);
00109                             (*(*articulatedObjIt)->getX())[(*it)->articulationIndex.getValue()] = (((*it)->motion[frame]/180.0)*3.14) + (n/ndiv)*diffMotions;
00110                             (*(*articulatedObjIt)->getXfree())[(*it)->articulationIndex.getValue()] = (((*it)->motion[frame]/180.0)*3.14) + (n/ndiv)*diffMotions;
00111                         }
00112                     }
00113             ++it;
00114             }
00115         ++artCenterIt;
00116         }
00117     }
00118     if (frame<(ahc->numOfFrames-2))
00119     {
00120         if (n<ndiv-1)
00121         {
00122             n++;
00123         }
00124         else
00125         {
00126             if((frame+frameInc) <= (ahc->numOfFrames-2))
00127             frame+=frameInc;
00128             n=0;
00129         }
00130     }
00131 }
00132 
00133 SOFA_DECL_CLASS(ArticulatedHierarchyBVHController)
00134 
00135 // Register in the Factory  
00136 int ArticulatedHierarchyBVHControllerClass = core::RegisterObject("")
00137 .add< ArticulatedHierarchyBVHController >()
00138 ;
00139 } // namespace controller
00140 
00141 } // namespace component
00142 
00143 } // namespace sofa

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