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
00027 #include <sofa/helper/io/bvh/BVHJoint.h>
00028
00029 #include <sofa/helper/system/gl.h>
00030 #include <sofa/helper/system/glu.h>
00031 #include <sofa/helper/system/glut.h>
00032
00033 #include <iostream>
00034
00035 namespace sofa
00036 {
00037
00038 namespace helper
00039 {
00040
00041 namespace io
00042 {
00043
00044 namespace bvh
00045 {
00046
00047 int BVHJoint::lastId = 0;
00048
00049 BVHJoint::BVHJoint(const char *_name, bool _endSite, BVHJoint *_parent)
00050 :parent(_parent),endSite(_endSite)
00051 {
00052 offset = NULL;
00053 channels = NULL;
00054 motion = NULL;
00055
00056 id = lastId++;
00057
00058 if (!endSite)
00059 strcpy(name,_name);
00060 else
00061 strcpy(name,"End Site");
00062 }
00063
00064 BVHJoint::~BVHJoint()
00065 {
00066 if (offset) delete offset;
00067 if (channels) delete channels;
00068 if (motion) delete motion;
00069
00070 lastId--;
00071 }
00072
00073 void BVHJoint::initMotion(double fTime, unsigned int fCount)
00074 {
00075 if (channels != NULL)
00076 this->getMotion()->init(fTime, fCount, channels->size);
00077
00078 for (unsigned int i=0; i < children.size(); i++)
00079 children[i]->initMotion(fTime, fCount);
00080 }
00081
00082 void BVHJoint::display(int frameNum)
00083 {
00084 glPushMatrix();
00085 glDisable(GL_LIGHTING);
00086 glColor3f(0.0, 0.0, 0.0);
00087 glBegin(GL_LINES);
00088 glVertex3d(0.0, 0.0, 0.0);
00089 glVertex3d(offset->x, offset->y, offset->z);
00090 glEnd();
00091 glTranslatef(offset->x, offset->y, offset->z);
00092
00093 glGetDoublev(GL_MODELVIEW_MATRIX,matrix);
00094
00095 if (channels != NULL)
00096 {
00097 for (unsigned int i=0; i<channels->size; i++)
00098 {
00099 switch (channels->channels[i])
00100 {
00101 case BVHChannels::Xposition:
00102 glTranslatef(motion->frames[frameNum][i],0,0);
00103 break;
00104 case BVHChannels::Yposition:
00105 glTranslatef(0,motion->frames[frameNum][i],0);
00106 break;
00107 case BVHChannels::Zposition:
00108 glTranslatef(0,0,motion->frames[frameNum][i]);
00109 break;
00110 case BVHChannels::Xrotation:
00111 glRotatef(motion->frames[frameNum][i],1,0,0);
00112 break;
00113 case BVHChannels::Yrotation:
00114 glRotatef(motion->frames[frameNum][i],0,1,0);
00115 break;
00116 case BVHChannels::Zrotation:
00117 glRotatef(motion->frames[frameNum][i],0,0,1);
00118 break;
00119 default:
00120 break;
00121 }
00122 }
00123 }
00124
00125 glColor3f(1.0,0.0,0.0);
00126 glutSolidSphere(0.01,16,16);
00127
00128 for (unsigned int i=0; i<children.size(); i++)
00129 {
00130 children[i]->display(frameNum);
00131 }
00132
00133 glPopMatrix();
00134 }
00135
00136 void BVHJoint::displayInGlobalFrame(void)
00137 {
00138 glPushMatrix();
00139 glLoadIdentity();
00140 glTranslatef(0.0,0.0,-4.0);
00141 glMultMatrixd(matrix);
00142 glDisable(GL_LIGHTING);
00143 glColor3f(1.0, 0.0, 0.0);
00144 glutSolidSphere(0.005,16,16);
00145 glPopMatrix();
00146
00147 for (unsigned int i=0; i<children.size(); i++)
00148 {
00149 children[i]->displayInGlobalFrame();
00150 }
00151 }
00152
00153
00154 int BVHJoint::getNumJoints(char *s)
00155 {
00156 int tmp(0);
00157
00158 if (s!=NULL)
00159 {
00160 if (strcmp(name,s) == 0)
00161 return accumulateNumJoints();
00162
00163 for (unsigned int i=0; i<children.size(); i++)
00164 tmp += children[i]->getNumJoints(s);
00165 }
00166 else
00167 return accumulateNumJoints();
00168
00169 return tmp;
00170 }
00171
00172 int BVHJoint::accumulateNumJoints(void)
00173 {
00174 int tmp = 1;
00175
00176 if (children.size() != 0)
00177 {
00178 for (unsigned int i=0; i<children.size(); i++)
00179 tmp += children[i]->accumulateNumJoints();
00180 }
00181
00182 return tmp;
00183 }
00184
00185
00186 int BVHJoint::getNumSegments(char *s)
00187 {
00188 return (getNumJoints(s) - 1);
00189 }
00190
00191
00192 void BVHJoint::dump(char *fName, char *rootJointName)
00193 {
00194 FILE *f = fopen(fName,"w+");
00195
00196 fprintf(f,"Catheter_Name Walk\n\n");
00197 fprintf(f,"Number_of_Nodes %d\n", getNumJoints(rootJointName));
00198 fprintf(f,"Number_of_Segments %d\n", getNumSegments(rootJointName));
00199
00200 fprintf(f, "\nList_of_Nodes\n");
00201 dumpPosition(f, rootJointName);
00202
00203 fprintf(f, "\nSegments\n");
00204 dumpSegment(f, rootJointName);
00205
00206 fprintf(f, "\nRotationAxis\n");
00207 dumpRotation(f, rootJointName);
00208
00209 fprintf(f, "\nRotationLimits\n");
00210 dumpRotationLimit(f, rootJointName);
00211
00212 fprintf(f, "\nRotationStiffness\n");
00213 dumpRotationStiffness(f, rootJointName);
00214
00215 fprintf(f, "\nTranslationAxis\n");
00216 fprintf(f, "\t0 0 1.000000 0.000000 0.000000\n");
00217 fprintf(f, "\t1 0 0.000000 1.000000 0.000000\n");
00218 fprintf(f, "\t2 0 0.000000 0.000000 1.000000\n");
00219
00220 fprintf(f, "\nTranslationStiffness\n");
00221 fprintf(f, "\t0 10000.0\n");
00222 fprintf(f, "\t1 10000.0\n");
00223 fprintf(f, "\t2 10000.0\n");
00224
00225 fclose(f);
00226 }
00227
00228
00229 void BVHJoint::dumpPosition(FILE *f, char *s)
00230 {
00231 if (s!=NULL)
00232 {
00233 if (strcmp(name,s) == 0)
00234 dumpPosition(f, id);
00235 else
00236 for (unsigned int i=0; i<children.size(); i++)
00237 children[i]->dumpPosition(f, s);
00238 }
00239 else
00240 dumpPosition(f, id);
00241 }
00242
00243
00244 void BVHJoint::dumpPosition(FILE *f, int beginIndex)
00245 {
00246 fprintf(f, "\t%d %f %f %f\n", id - beginIndex, matrix[12], matrix[13], matrix[14]);
00247 for (unsigned int i=0; i<children.size(); i++)
00248 children[i]->dumpPosition(f, beginIndex);
00249 }
00250
00251
00252 void BVHJoint::dumpSegment(FILE *f, char *s)
00253 {
00254 int cpt = 0;
00255
00256 if (s!=NULL)
00257 {
00258 if (strcmp(name,s) == 0)
00259 dumpSegment(f, cpt, id);
00260 else
00261 for (unsigned int i=0; i<children.size(); i++)
00262 children[i]->dumpSegment(f, s);
00263 }
00264 else
00265 dumpSegment(f, cpt, id);
00266 }
00267
00268
00269 void BVHJoint::dumpSegment(FILE *f, int &cpt, int beginIndex)
00270 {
00271 for (unsigned int i=0; i<children.size(); i++)
00272 {
00273 fprintf(f, "\t%d %d %d\n", cpt++, id - beginIndex, children[i]->id - beginIndex);
00274 children[i]->dumpSegment(f, cpt, beginIndex);
00275 }
00276 }
00277
00278
00279 void BVHJoint::dumpRotation(FILE *f, char *s)
00280 {
00281 int cpt = 0;
00282
00283 if (s!=NULL)
00284 {
00285 if (strcmp(name,s) == 0)
00286 dumpRotation(f, cpt, id);
00287 else
00288 for (unsigned int i=0; i<children.size(); i++)
00289 children[i]->dumpRotation(f, s);
00290 }
00291 else
00292 dumpRotation(f, cpt, id);
00293 }
00294
00295
00296 void BVHJoint::dumpRotation(FILE *f, int &cpt, int beginIndex)
00297 {
00298 fprintf(f, "\t%d %d %f %f %f\n", cpt++, id - beginIndex, matrix[0], matrix[1], matrix[2]);
00299 fprintf(f, "\t%d %d %f %f %f\n", cpt++, id - beginIndex, matrix[4], matrix[5], matrix[6]);
00300 fprintf(f, "\t%d %d %f %f %f\n", cpt++, id - beginIndex, matrix[8], matrix[9], matrix[10]);
00301
00302 for (unsigned int i=0; i<children.size(); i++)
00303 children[i]->dumpRotation(f, cpt, beginIndex);
00304 }
00305
00306
00307 void BVHJoint::dumpRotationLimit(FILE *f, char *s)
00308 {
00309 int cpt = 0;
00310
00311 if (s!=NULL)
00312 {
00313 if (strcmp(name,s) == 0)
00314 dumpRotationLimit(f, cpt);
00315 else
00316 for (unsigned int i=0; i<children.size(); i++)
00317 children[i]->dumpRotationLimit(f, s);
00318 }
00319 else
00320 dumpRotationLimit(f, cpt);
00321 }
00322
00323
00324 void BVHJoint::dumpRotationLimit(FILE *f, int &cpt)
00325 {
00326 fprintf(f, "\t%d -1000000.0 1000000.0\n", cpt++);
00327 fprintf(f, "\t%d -1000000.0 1000000.0\n", cpt++);
00328 fprintf(f, "\t%d -1000000.0 1000000.0\n", cpt++);
00329
00330 for (unsigned int i=0; i<children.size(); i++)
00331 children[i]->dumpRotationLimit(f, cpt);
00332 }
00333
00334
00335 void BVHJoint::dumpRotationStiffness(FILE *f, char *s)
00336 {
00337 int cpt = 0;
00338
00339 if (s!=NULL)
00340 {
00341 if (strcmp(name,s) == 0)
00342 dumpRotationStiffness(f, cpt);
00343 else
00344 for (unsigned int i=0; i<children.size(); i++)
00345 children[i]->dumpRotationStiffness(f, s);
00346 }
00347 else
00348 dumpRotationStiffness(f, cpt);
00349 }
00350
00351
00352 void BVHJoint::dumpRotationStiffness(FILE *f, int &cpt)
00353 {
00354 fprintf(f, "\t%d 1000000000.0\n", cpt++);
00355 fprintf(f, "\t%d 1000000000.0\n", cpt++);
00356 fprintf(f, "\t%d 1000000000.0\n", cpt++);
00357
00358 for (unsigned int i=0; i<children.size(); i++)
00359 children[i]->dumpRotationStiffness(f, cpt);
00360 }
00361
00362 void BVHJoint::debug(int tab)
00363 {
00364 for (int i=0; i<tab; i++)
00365 std::cout << "\t";
00366
00367 std::cout << name << std::endl;
00368
00369 if (offset != NULL)
00370 {
00371 for (int i=0; i<tab; i++)
00372 std::cout << "\t";
00373 std::cout << "offset " << offset->x << " " << offset->y << " " << offset->z << std::endl;
00374 }
00375
00376 if (channels != NULL)
00377 {
00378 for (int i=0; i<tab; i++)
00379 std::cout << "\t";
00380 std::cout << "channels ";
00381 for (unsigned int i=0; i<channels->channels.size(); i++)
00382 std::cout << channels->channels[i] << " ";
00383 std::cout << std::endl;
00384 }
00385
00386 for (unsigned int i=0; i<children.size(); i++)
00387 {
00388 children[i]->debug(tab+1);
00389 }
00390 }
00391
00392 int BVHJoint::getId()
00393 {
00394 return id;
00395 }
00396
00397 char* BVHJoint::getName()
00398 {
00399 return name;
00400 }
00401
00402 BVHOffset* BVHJoint::getOffset()
00403 {
00404 return offset;
00405 }
00406
00407 }
00408
00409 }
00410
00411 }
00412
00413 }