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 #ifndef SOFA_COMPONENT_CONTAINER_ARTICULATEDHIERARCHYCONTAINER_INL
00027 #define SOFA_COMPONENT_CONTAINER_ARTICULATEDHIERARCHYCONTAINER_INL
00028
00029 #include <sofa/component/container/ArticulatedHierarchyContainer.h>
00030 #include <sofa/helper/system/FileRepository.h>
00031
00032 namespace sofa
00033 {
00034
00035 namespace component
00036 {
00037
00038 namespace container
00039 {
00040
00041 ArticulatedHierarchyContainer::ArticulationCenter::Articulation::Articulation():
00042 axis(initData(&axis, (Vector3) Vector3(1,0,0), "rotationAxis", "Set the rotation axis for the articulation")),
00043 rotation(initData(&rotation, (bool) false, "rotation", "Rotation")),
00044 translation(initData(&translation, (bool) false, "translation", "Translation")),
00045 articulationIndex(initData(&articulationIndex, (int) 0, "articulationIndex", "Articulation index"))
00046 {
00047 }
00048
00049 ArticulatedHierarchyContainer::ArticulationCenter::ArticulationCenter():
00050 parentIndex(initData(&parentIndex, "parentIndex", "Parent of the center articulation")),
00051 childIndex(initData(&childIndex, "childIndex", "Child of the center articulation")),
00052 globalPosition(initData(&globalPosition, "globalPosition", "Global position of the articulation center")),
00053 posOnParent(initData(&posOnParent, "posOnParent", "Parent position of the articulation center")),
00054 posOnChild(initData(&posOnChild, "posOnChild", "Child position of the articulation center")),
00055 articulationProcess(initData(&articulationProcess, (int) 0, "articulationProcess", " 0 - (default) hierarchy between articulations (euler angles)\n 1- ( on Parent) no hierarchy - axis are attached to the parent\n 2- (attached on Child) no hierarchy - axis are attached to the child"))
00056 {
00057 }
00058
00059 ArticulatedHierarchyContainer::ArticulationCenter* ArticulatedHierarchyContainer::getArticulationCenterAsChild(int index)
00060 {
00061 vector<ArticulationCenter*>::const_iterator ac = articulationCenters.begin();
00062 vector<ArticulationCenter*>::const_iterator acEnd = articulationCenters.end();
00063 for (; ac != acEnd; ac++)
00064 {
00065 if ((*ac)->childIndex.getValue() == index)
00066 return (*ac);
00067 }
00068 return (*ac);
00069 }
00070
00071 vector<ArticulatedHierarchyContainer::ArticulationCenter*> ArticulatedHierarchyContainer::getAcendantList(int index)
00072 {
00073 unsigned int i=0;
00074 acendantList.clear();
00075 while (index !=0)
00076 {
00077 acendantList.push_back(getArticulationCenterAsChild(index));
00078 index = acendantList[i]->parentIndex.getValue();
00079 i++;
00080 }
00081 return acendantList;
00082 }
00083
00084 vector<ArticulatedHierarchyContainer::ArticulationCenter*> ArticulatedHierarchyContainer::getArticulationCenters()
00085 {
00086 return articulationCenters;
00087 }
00088
00089 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*> ArticulatedHierarchyContainer::ArticulationCenter::getArticulations()
00090 {
00091 return articulations;
00092 }
00093
00094 ArticulatedHierarchyContainer::ArticulatedHierarchyContainer():
00095 filename(initData(&filename, "filename", "BVH File to load the articulation", false))
00096 {
00097 joint = NULL;
00098 id = 0;
00099 chargedFromFile = false;
00100 numOfFrames = 0;
00101 dtbvh = 0.0;
00102 }
00103
00104
00105 void ArticulatedHierarchyContainer::buildCenterArticulationsTree(sofa::helper::io::bvh::BVHJoint* bvhjoint, int id_buf, const char* name, simulation::tree::GNode* node)
00106 {
00107 std::vector<sofa::helper::io::bvh::BVHJoint*> jointChildren = bvhjoint->getChildren();
00108 if (jointChildren.size()==0)
00109 return;
00110
00111 std::string str(name);
00112 str.append("/");
00113 str.append(bvhjoint->getName());
00114
00115 simulation::tree::GNode* nodeOfArticulationCenters = new simulation::tree::GNode(str);
00116 node->addChild(nodeOfArticulationCenters);
00117
00118 ArticulationCenter* ac = new ArticulationCenter();
00119 nodeOfArticulationCenters->addObject(ac);
00120 articulationCenters.push_back(ac);
00121
00122 ac->posOnParent.setValue(Vector3(bvhjoint->getOffset()->x,bvhjoint->getOffset()->y,bvhjoint->getOffset()->z));
00123 ac->posOnChild.setValue(Vector3(0,0,0));
00124 ac->parentIndex.setValue(id_buf);
00125 ac->childIndex.setValue(bvhjoint->getId()+1);
00126
00127 simulation::tree::GNode* nodeOfArticulations = new simulation::tree::GNode("articulations");
00128 nodeOfArticulationCenters->addChild(nodeOfArticulations);
00129
00130 sofa::helper::io::bvh::BVHChannels* channels = bvhjoint->getChannels();
00131 sofa::helper::io::bvh::BVHMotion* motion = bvhjoint->getMotion();
00132
00133 ArticulationCenter::Articulation* a;
00134
00135 for (unsigned int j=0; j<channels->channels.size(); j++)
00136 {
00137 switch(channels->channels[j])
00138 {
00139 case sofa::helper::io::bvh::BVHChannels::NOP:
00140 break;
00141 case sofa::helper::io::bvh::BVHChannels::Xposition:
00142 a = new ArticulationCenter::Articulation();
00143 nodeOfArticulations->addObject(a);
00144 ac->articulations.push_back(a);
00145 a->axis.setValue(Vector3(1,0,0));
00146 a->translation.setValue(true);
00147 a->articulationIndex.setValue(id);
00148 for (int k=0; k<motion->frameCount; k++)
00149 a->motion.push_back(motion->frames[k][j]);
00150 id++;
00151 break;
00152 case sofa::helper::io::bvh::BVHChannels::Yposition:
00153 a = new ArticulationCenter::Articulation();
00154 nodeOfArticulations->addObject(a);
00155 ac->articulations.push_back(a);
00156 a->axis.setValue(Vector3(0,1,0));
00157 a->translation.setValue(true);
00158 a->articulationIndex.setValue(id);
00159 for (int k=0; k<motion->frameCount; k++)
00160 a->motion.push_back(motion->frames[k][j]);
00161 id++;
00162 break;
00163 case sofa::helper::io::bvh::BVHChannels::Zposition:
00164 a = new ArticulationCenter::Articulation();
00165 nodeOfArticulations->addObject(a);
00166 ac->articulations.push_back(a);
00167 a->axis.setValue(Vector3(0,0,1));
00168 a->translation.setValue(true);
00169 a->articulationIndex.setValue(id);
00170 for (int k=0; k<motion->frameCount; k++)
00171 a->motion.push_back(motion->frames[k][j]);
00172 id++;
00173 break;
00174 case sofa::helper::io::bvh::BVHChannels::Xrotation:
00175 a = new ArticulationCenter::Articulation();
00176 nodeOfArticulations->addObject(a);
00177 ac->articulations.push_back(a);
00178 a->axis.setValue(Vector3(1,0,0));
00179 a->rotation.setValue(true);
00180 a->articulationIndex.setValue(id);
00181 for (int k=0; k<motion->frameCount; k++)
00182 a->motion.push_back(motion->frames[k][j]);
00183 id++;
00184 break;
00185 case sofa::helper::io::bvh::BVHChannels::Yrotation:
00186 a = new ArticulationCenter::Articulation();
00187 nodeOfArticulations->addObject(a);
00188 ac->articulations.push_back(a);
00189 a->axis.setValue(Vector3(0,1,0));
00190 a->rotation.setValue(true);
00191 a->articulationIndex.setValue(id);
00192 for (int k=0; k<motion->frameCount; k++)
00193 a->motion.push_back(motion->frames[k][j]);
00194 id++;
00195 break;
00196 case sofa::helper::io::bvh::BVHChannels::Zrotation:
00197 a = new ArticulationCenter::Articulation();
00198 nodeOfArticulations->addObject(a);
00199 ac->articulations.push_back(a);
00200 a->axis.setValue(Vector3(0,0,1));
00201 a->rotation.setValue(true);
00202 a->articulationIndex.setValue(id);
00203 for (int k=0; k<motion->frameCount; k++)
00204 a->motion.push_back(motion->frames[k][j]);
00205 id++;
00206 break;
00207 }
00208 }
00209
00210 for(unsigned int i=0; i<jointChildren.size(); i++)
00211 {
00212 buildCenterArticulationsTree(jointChildren[i], bvhjoint->getId()+1, bvhjoint->getName(), nodeOfArticulationCenters);
00213 }
00214 }
00215
00216 void ArticulatedHierarchyContainer::init ()
00217 {
00218 simulation::tree::GNode* context = dynamic_cast<simulation::tree::GNode *>(this->getContext());
00219
00220 std::string file = filename.getValue();
00221 if ( sofa::helper::system::DataRepository.findFile (file) )
00222 {
00223
00224 sofa::helper::io::bvh::BVHLoader loader = sofa::helper::io::bvh::BVHLoader();
00225 joint = loader.load(sofa::helper::system::DataRepository.getFile ( file ).c_str());
00226 chargedFromFile = true;
00227 numOfFrames = joint->getMotion()->frameCount;
00228 dtbvh = joint->getMotion()->frameTime;
00229 }
00230
00231 if (joint != NULL)
00232 {
00233 simulation::tree::GNode* articulationCenters = new simulation::tree::GNode("ArticulationCenters");
00234 context->addChild(articulationCenters);
00235
00236 buildCenterArticulationsTree(joint, 0, "Root", articulationCenters);
00237
00238 component::MechanicalObject<Vec1dTypes>* mm1 = dynamic_cast<component::MechanicalObject<Vec1dTypes>*>(context->getMechanicalState());
00239 mm1->resize(id);
00240
00241 context = *context->child.begin();
00242 component::MechanicalObject<RigidTypes>* mm2 = dynamic_cast<component::MechanicalObject<RigidTypes>*>(context->getMechanicalState());
00243 mm2->resize(joint->getNumJoints()+1);
00244 }
00245 else
00246 {
00247 context->getTreeObjects<ArticulationCenter>(&articulationCenters);
00248 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = articulationCenters.begin();
00249 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acEnd = articulationCenters.end();
00250 for (; ac != acEnd; ac++)
00251 {
00252 context = dynamic_cast<simulation::tree::GNode *>((*ac)->getContext());
00253 for (simulation::tree::GNode::ChildIterator it = context->child.begin(); it != context->child.end(); ++it)
00254 {
00255 GNode* n = *it;
00256 n->getTreeObjects<ArticulationCenter::Articulation>(&(*ac)->articulations);
00257 }
00258 }
00259 }
00260 }
00261
00262 }
00263
00264 }
00265
00266 }
00267
00268 #endif