00001
00002 #include <Joint.h>
00003
00004 #define PI 3.14259
00005
00006
00007 Joint::Joint()
00008 {
00009 N_son=0;
00010 father = this;
00011 strcpy(name,"NO_NAME");
00012 for(int k=0;k<3;orientation[k]=0,k++);
00013
00014 access_number = -1;
00015 level=0;
00016 }
00017
00018 Joint::~Joint()
00019 {
00020 destroy();
00021 }
00022
00023
00024
00025 int Joint::destroy()
00026 {
00027 access_number = -1;
00028 return 0;
00029 }
00030
00031 int Joint::set_name(const char* _name)
00032 {
00033 strcpy(name,_name);
00034 return 0;
00035 }
00036
00037 int Joint::set_name(const std::string& _name)
00038 {
00039 strcpy(name,_name.data());
00040 return 0;
00041 }
00042
00043 char *Joint::get_name()
00044 {
00045 return name;
00046 }
00047
00048 int Joint::set_position(double x,double y,double z)
00049 {
00050 M.set_translation(x,y,z);
00051 return 0;
00052 }
00053
00054 int Joint::add_position(double x,double y,double z)
00055 {
00056 M.add_translation(x,y,z);
00057 return 0;
00058 }
00059 int Joint::add_position(double *translation)
00060 {
00061 M.add_translation(translation);
00062 return 0;
00063 }
00064
00065 int Joint::get_position(double *_position)
00066 {
00067 for(int k_dim=0;k_dim<3;k_dim++)
00068 _position[k_dim]=M.value(3,k_dim);
00069 return 0;
00070 }
00071
00072 double* Joint::get_position()
00073 {
00074
00075 double *position=NULL;
00076 position = new double[3];
00077 if(position==NULL)
00078 {
00079 cout<<"ERROR allocation position in joint get_position\n"<<endl;
00080 exit(-1);
00081 }
00082
00083 get_position(position);
00084
00085
00086 return position;
00087 }
00088
00089 int Joint::set_orientation(double t1,double t2,double t3)
00090 {
00091 double t[3]={t1,t2,t3};
00092
00093 M.set_euler_angle(t);
00094 orientation[0]=t1;
00095 orientation[1]=t2;
00096 orientation[2]=t3;
00097
00098 return 0;
00099 }
00100
00101 int Joint::set_orientation(double *t)
00102 {
00103 M.set_euler_angle(t);
00104 for(int k_dim=0;k_dim<3;k_dim++)
00105 orientation[k_dim]=t[k_dim];
00106 return 0;
00107 }
00108
00109 int Joint::add_rotation(double *v,double theta)
00110 {
00111 if(theta>2*PI)
00112 printf("\n\n<<WARNING in add rotation, theta >2*PI, are you sure that you are sending radians angles ?>>\n\n");
00113
00114 return M.multiply_vector_rotation(v,theta);
00115 }
00116
00117 int Joint::add_rotation(double v0,double v1,double v2,double theta)
00118 {
00119 double v[3]={v0,v1,v2};
00120 return M.multiply_vector_rotation(v,theta);
00121 }
00122
00123 int Joint::get_orientation(double *_orientation)
00124 {
00125 for(int k_dim=0;k_dim<3;_orientation[k_dim]=orientation[k_dim],k_dim++);
00126 return 0;
00127 }
00128 double* Joint::get_orientation()
00129 {
00130 return orientation;
00131 }
00132
00133 int Joint::set_father(Joint *_father)
00134 {
00135 father = _father;
00136 return 0;
00137 }
00138
00139 Joint* Joint::get_father()
00140 {
00141 return father;
00142 }
00143
00144 int Joint::set_son(int k,Joint *new_joint)
00145 {
00146 int count=0;
00147 if(k>=0 && k<N_son)
00148 {
00149 std::list <Joint*>::iterator it_joint;
00150 for(it_joint=son.begin(),count=0;count<k;count++,it_joint++);
00151 *it_joint = new_joint;
00152 return 0;
00153 }
00154
00155 cout<<"WARNING TRY TO SET INCORRECT SON n."<<k<<" with only "<<N_son<<" in memory for "<<name<<"("<<access_number<<")"<<endl;
00156
00157 return -1;
00158 }
00159
00160 Joint* Joint::get_son(int k)
00161 {
00162 int count=0;
00163 if(k>=0 && k<N_son)
00164 {
00165 std::list <Joint*>::iterator it_joint;
00166 for(it_joint=son.begin(),count=0;count<k;count++,it_joint++);
00167 return *it_joint;
00168 }
00169
00170 cout<<"WARNING TRY TO GET INCORRECT SON n."<<k<<" with only "<<N_son<<" in memory for "<<name<<"("<<access_number<<")"<<endl;
00171 return NULL;
00172 }
00173
00174
00175 int Joint::add_son()
00176 {
00177 int ok=0;
00178
00179 N_son++;
00180 Joint *new_son = new Joint();
00181 new_son->set_father(this);
00182 son.push_front(new_son);
00183
00184 return ok;
00185 }
00186
00187 int Joint::get_N_son()
00188 {
00189 return N_son;
00190 }
00191
00192 Matrix Joint::get_matrix()
00193 {
00194 return M;
00195 }
00196
00197 int Joint::scale(double *s)
00198 {
00199 return M.scale(s);
00200 }
00201 int Joint::scale(double s0,double s1,double s2)
00202 {
00203 double s[3]={s0,s1,s2};
00204 return scale(s);
00205 }
00206
00207 int Joint::set_bind_pose(Matrix D)
00208 {
00209 bind_pose = D;
00210 return 0;
00211 }
00212
00213 Matrix Joint::get_bind_pose()
00214 {
00215 return bind_pose;
00216 }
00217
00218
00219 int Joint::multiply_matrix(Matrix R)
00220 {
00221 M = M*R;
00222 return 0;
00223 }
00224
00225 int Joint::set_matrix(Matrix _M)
00226 {
00227 M = _M;
00228 return 0;
00229 }
00230
00231
00232 int Joint::set_position_bind(double x,double y,double z)
00233 {
00234 double t[3]={x,y,z};
00235 return set_position_bind(t);
00236 }
00237
00238 int Joint::set_position_bind(double *t)
00239 {
00240 for(int k_dim=0;k_dim<3;k_dim++)
00241 bind_pose.set_value(t[k_dim],3,k_dim);
00242 return 0;
00243 }
00244
00245 int Joint::set_access_number(int _access_number)
00246 {
00247 access_number = _access_number;
00248 return 0;
00249 }
00250
00251 int Joint::get_access_number()
00252 {
00253 return access_number;
00254 }
00255
00256 int Joint::get_matrix(Matrix *_M)
00257 {
00258 int k_1=0;
00259 int k_2=0;
00260 for(k_1=0;k_1<4;k_1++)
00261 for(k_2=0;k_2<4;k_2++)
00262 _M->set_value(M.value(k_1,k_2),k_1,k_2);
00263
00264 return 0;
00265 }
00266
00267
00268 int Joint::set_world_matrix(Matrix _world_matrix)
00269 {
00270 world_matrix = _world_matrix;
00271 return 0;
00272 }
00273 Matrix Joint::get_world_matrix()
00274 {
00275 return world_matrix;
00276 }
00277 int Joint::set_T(Matrix _T)
00278 {
00279 T = _T;
00280 return 0;
00281 }
00282 Matrix Joint::get_T()
00283 {
00284 return T;
00285 }
00286
00287 int Joint::get_bind_pose(Matrix *_M)
00288 {
00289 *_M = bind_pose;
00290 return 0;
00291 }
00292
00293 Joint& Joint::operator=(const Joint& J)
00294 {
00295 N_son = 0;
00296 strcpy(name,J.name);
00297 M = J.M;
00298 for(int k=0;k<3;k++)
00299 orientation[k] = J.orientation[k];
00300 bind_pose = J.bind_pose;
00301 world_matrix = J.world_matrix;
00302 T = J.T;
00303 access_number = J.access_number;
00304 level = J.level;
00305
00306 return *this;
00307 }
00308
00309
00310
00311 double Joint::get_min_distance_to_bone(V_3D vertex)
00312 {
00313 double distance_min=999.9f;
00314 double current_distance = 0.0f;
00315
00316 V_3D P1 = world_matrix.get_position();
00317 V_3D P2;
00318 V_3D closest;
00319
00320 V_3D u1,u2;
00321 double projection=0.0f;
00322
00323 Joint *_son=0;
00324 if(get_N_son()==0)
00325 {
00326 distance_min = 9999.99f;
00327 }
00328 else
00329 {
00330 int k_son=0;
00331 for(k_son=0;k_son<get_N_son();k_son++)
00332 {
00333 _son = get_son(k_son);
00334 P2 = _son->get_world_matrix().get_position();
00335
00336 u1 = vertex-P1;
00337 u2 = P2-P1;
00338
00339 projection = u1.dot(u2)/u2.dot(u2);
00340 if(projection<0)
00341 closest = P1;
00342 else if(projection>1)
00343 closest = P2;
00344 else
00345 closest = P1 + projection*u2;
00346
00347 current_distance = (vertex-closest).norm();
00348
00349 if(current_distance<distance_min)
00350 distance_min=current_distance;
00351 }
00352 }
00353
00354 return distance_min;
00355 }
00356
00357
00358 int Joint::get_level()
00359 {return level;}
00360
00361 int Joint::set_level(int _level)
00362 {level = _level;return 0;}
00363
00364 Joint::Joint(const Joint& _j)
00365 {
00366 N_son=0;
00367 father = this;
00368 strcpy(name,"NO_NAME");
00369 for(int k=0;k<3;orientation[k]=0,k++);
00370
00371 access_number = -1;
00372 level=_j.level;
00373 }
00374
00375 int Joint::set_weight_name(const char* _weight_name)
00376 {weight_name=_weight_name;return 0;}
00377 const char* Joint::get_weight_name() const
00378 {return weight_name.data();}
00379