00001 #include "mechanism.h"
00002
00003 #include <string.h>
00004
00005
00016 void InitMechanism(Tmechanism *m)
00017 {
00018 InitVector(sizeof(void *),CopyVoidPtr,DeleteVoidPtr,INIT_NUM_JOINTS,&(m->joints));
00019 InitVector(sizeof(void *),CopyVoidPtr,DeleteVoidPtr,INIT_NUM_LINKS,&(m->links));
00020 m->nbodies=0;
00021 m->maxCoord=0.0;
00022 }
00023
00024 unsigned int GetMechanismNLinks(Tmechanism *m)
00025 {
00026 return(VectorSize(&(m->links)));
00027 }
00028
00029 unsigned int GetMechanismNJoints(Tmechanism *m)
00030 {
00031 return(VectorSize(&(m->joints)));
00032 }
00033
00034 unsigned int GetMechanismNBodies(Tmechanism *m)
00035 {
00036 return(m->nbodies);
00037 }
00038
00039 unsigned int GetLinkID(char *name,Tmechanism *m)
00040 {
00041 unsigned int id,nl;
00042 boolean found;
00043 Tlink *l;
00044
00045 nl=GetMechanismNLinks(m);
00046 found=FALSE;
00047 id=0;
00048 while((!found)&&(id<nl))
00049 {
00050 l=GetMechanismLink(id,m);
00051 if ((l!=NULL)&&(strcmp(name,GetLinkName(l))==0))
00052 found=TRUE;
00053 else
00054 id++;
00055 }
00056 if (found)
00057 return(id);
00058 else
00059 return(NO_UINT);
00060 }
00061
00062 signed int GetMechanismDOF(Tmechanism *m)
00063 {
00064 signed int ndof,constraints;
00065 unsigned int i,nj;
00066
00067 ndof=6*(GetMechanismNLinks(m)-1);
00068
00069 nj=GetMechanismNJoints(m);
00070 constraints=0;
00071 for(i=0;i<nj;i++)
00072 constraints+=(6-GetJointDOF(GetMechanismJoint(i,m)));
00073
00074 return(ndof-constraints);
00075 }
00076
00077 void GetMechanismDefiningPoint(unsigned int lID,unsigned int bID,unsigned int pID,
00078 double *p,Tmechanism *m)
00079 {
00080 GetCPolyhedronDefiningPoint(pID,p,GetLinkBody(bID,GetMechanismLink(lID,m)));
00081 }
00082
00083 unsigned int AddLink2Mechanism(Tlink *l,Tmechanism *m)
00084 {
00085 Tlink *li;
00086 unsigned int k;
00087
00088 NEW(li,1,Tlink);
00089 CopyLink(li,l);
00090 k=GetMechanismNLinks(m);
00091 SetVectorElement(k,&li,&(m->links));
00092
00093 m->maxCoord+=GetLinkMaxCoordinate(l);
00094
00095 return(k);
00096 }
00097
00098 unsigned int AddJoint2Mechanism(Tjoint *j,Tmechanism *m)
00099 {
00100 Tjoint *ji;
00101 unsigned int k;
00102
00103 NEW(ji,1,Tjoint);
00104 CopyJoint(ji,j);
00105 k=GetMechanismNJoints(m);
00106 SetVectorElement(k,&ji,&(m->joints));
00107
00108 m->maxCoord+=GetJointMaxCoordinate(j);
00109
00110 return(k);
00111 }
00112
00113 unsigned int AddBody2Mechanism(unsigned int lID,Tcpolyhedron *b,Tmechanism *m)
00114 {
00115 Tlink *lk;
00116
00117 lk=GetMechanismLink(lID,m);
00118 if (lk==NULL)
00119 Error("Unkown link in AddBody2Mechanism");
00120
00121
00122 m->maxCoord-=GetLinkMaxCoordinate(lk);
00123 AddBody2Link(b,lk);
00124 m->maxCoord+=GetLinkMaxCoordinate(lk);
00125
00126 m->nbodies++;
00127
00128 return(m->nbodies);
00129 }
00130
00131 Tlink *GetMechanismLink(unsigned int i,Tmechanism *m)
00132 {
00133 Tlink **l;
00134
00135 l=(Tlink **)GetVectorElement(i,&(m->links));
00136 if (l==NULL)
00137 return(NULL);
00138 else
00139 return(*l);
00140 }
00141
00142 Tjoint *GetMechanismJoint(unsigned int i,Tmechanism *m)
00143 {
00144 Tjoint **j;
00145
00146 j=(Tjoint **)GetVectorElement(i,&(m->joints));
00147 if (j==NULL)
00148 return(NULL);
00149 else
00150 return(*j);
00151 }
00152
00153 double GetMechanismMaxCoordinate(Tmechanism *m)
00154 {
00155 return(m->maxCoord);
00156 }
00157
00158 void PlotMechanism(Tplot3d *pt,double axesLength,Tmechanism *m)
00159 {
00160 unsigned int i;
00161 unsigned int n;
00162 Tlink *l;
00163
00164 n=GetMechanismNLinks(m);
00165 if (n>0) Start3dBlock(pt);
00166 for(i=0;i<n;i++)
00167 {
00168 l=GetMechanismLink(i,m);
00169 if (l!=NULL)
00170 PlotLink(pt,axesLength,l);
00171 }
00172 if (n>0)
00173 {
00174 Tjoint *j;
00175 unsigned int nj;
00176
00177
00178 nj=GetMechanismNJoints(m);
00179 for(i=0;i<nj;i++)
00180 {
00181 j=GetMechanismJoint(i,m);
00182 if (j!=NULL)
00183 PlotJoint(pt,j);
00184 }
00185 }
00186
00187 if (n>0) Close3dBlock(pt);
00188 }
00189
00190 void RegenerateMechanismSolution(TCuikSystem *cs,double *sol,Tmechanism *m)
00191 {
00192 unsigned int i;
00193 unsigned int n;
00194 Tlink *l;
00195 Tjoint *j;
00196
00197 n=GetMechanismNLinks(m);
00198 for(i=0;i<n;i++)
00199 {
00200 l=GetMechanismLink(i,m);
00201 if (l!=NULL)
00202 RegenerateLinkSolution(cs,sol,IsGroundLink(i),l);
00203 }
00204
00205 n=GetMechanismNJoints(m);
00206 for(i=0;i<n;i++)
00207 {
00208 j=GetMechanismJoint(i,m);
00209 if (l!=NULL)
00210 RegenerateJointSolution(cs,sol,j);
00211 }
00212 }
00213
00214 void MoveMechanism(Tplot3d *pt,TCuikSystem *cs,double *sol,
00215 double **r,Tmechanism *m)
00216 {
00217 unsigned int i;
00218 unsigned int n;
00219 Tlink *l;
00220
00221 n=GetMechanismNLinks(m);
00222 if (n>0) Start3dBlock(pt);
00223 for(i=1;i<n;i++)
00224 {
00225 l=GetMechanismLink(i,m);
00226 if (l!=NULL)
00227 MoveLink(pt,cs,sol,r[i],l);
00228 }
00229
00230 if(n>0)
00231 {
00232 Tjoint *j;
00233 unsigned int nj;
00234
00235
00236
00237 nj=GetMechanismNJoints(m);
00238 for(i=0;i<nj;i++)
00239 {
00240 j=GetMechanismJoint(i,m);
00241 if (j!=NULL)
00242 MoveJoint(pt,cs,sol,r,j);
00243 }
00244 }
00245
00246 if (n>0) Close3dBlock(pt);
00247 }
00248
00249 void DeleteMechanism(Tmechanism *m)
00250 {
00251
00252 unsigned int i;
00253 unsigned int n;
00254 Tlink *l;
00255 Tjoint *j;
00256
00257 n=GetMechanismNLinks(m);
00258 for(i=0;i<n;i++)
00259 {
00260 l=GetMechanismLink(i,m);
00261 if (l!=NULL)
00262 {
00263 DeleteLink(l);
00264 free(l);
00265 }
00266 }
00267
00268 n=GetMechanismNJoints(m);
00269 for(i=0;i<n;i++)
00270 {
00271 j=GetMechanismJoint(i,m);
00272 if (j!=NULL)
00273 {
00274 DeleteJoint(j);
00275 free(j);
00276 }
00277 }
00278
00279 DeleteVector(&(m->links));
00280 DeleteVector(&(m->joints));
00281 }