mechanism.c
Go to the documentation of this file.
1 #include "mechanism.h"
2 
3 #include <string.h>
4 
5 
17 {
20  m->nbodies=0;
21  m->allSpheres=TRUE;
22  m->maxCoord=0.0;
23 }
24 
26 {
27  return(VectorSize(&(m->links)));
28 }
29 
31 {
32  return(VectorSize(&(m->joints)));
33 }
34 
36 {
37  return(m->nbodies);
38 }
39 
41 {
42  return(m->allSpheres);
43 }
44 
46 {
47  return(m->maxCoord);
48 }
49 
50 unsigned int GetLinkID(char *name,Tmechanism *m)
51 {
52  unsigned int id,nl;
53  boolean found;
54  Tlink *l;
55 
56  nl=GetMechanismNLinks(m);
57  found=FALSE;
58  id=0;
59  while((!found)&&(id<nl))
60  {
61  l=GetMechanismLink(id,m);
62  if ((l!=NULL)&&(strcmp(name,GetLinkName(l))==0))
63  found=TRUE;
64  else
65  id++;
66  }
67  if (found)
68  return(id);
69  else
70  return(NO_UINT);
71 }
72 
74 {
75  signed int ndof,constraints;
76  unsigned int i,nj;
77 
78  ndof=6*(GetMechanismNLinks(m)-1); /*Ground link is fixed !!*/
79 
80  nj=GetMechanismNJoints(m);
81  constraints=0;
82  for(i=0;i<nj;i++)
83  constraints+=(6-GetJointDOF(GetMechanismJoint(i,m)));
84 
85  return(ndof-constraints);
86 }
87 
88 void GetMechanismDefiningPoint(unsigned int lID,unsigned int bID,unsigned int pID,
89  double *p,Tmechanism *m)
90 {
92 }
93 
94 unsigned int AddLink2Mechanism(Tlink *l,Tmechanism *m)
95 {
96  Tlink *li;
97  unsigned int k;
98 
99  NEW(li,1,Tlink);
100  CopyLink(li,l);
101  k=GetMechanismNLinks(m);
102  SetVectorElement(k,&li,&(m->links));
103 
105  m->nbodies+=LinkNBodies(l);
106  m->allSpheres=((m->allSpheres)&&(IsLinkAllSpheres(l)));
107 
108  return(k);
109 }
110 
112 {
113  Tjoint *ji;
114  unsigned int k;
115 
116  NEW(ji,1,Tjoint);
117  CopyJoint(ji,j);
118  k=GetMechanismNJoints(m);
119  SetVectorElement(k,&ji,&(m->joints));
120 
122  m->allSpheres=((m->allSpheres)&&(IsJointAllSpheres(j)));
123 
124  return(k);
125 }
126 
127 unsigned int AddBody2Mechanism(unsigned int lID,Tpolyhedron *b,Tmechanism *m)
128 {
129  Tlink *lk;
130 
131  lk=GetMechanismLink(lID,m);
132  if (lk==NULL)
133  Error("Unkown link in AddBody2Mechanism");
134 
135 
137  AddBody2Link(b,lk);
138 
140  m->nbodies++;
141  m->allSpheres=((m->allSpheres)&&(IsLinkAllSpheres(lk)));
142 
143  return(m->nbodies);
144 }
145 
146 Tlink *GetMechanismLink(unsigned int i,Tmechanism *m)
147 {
148  Tlink **l;
149 
150  l=(Tlink **)GetVectorElement(i,&(m->links));
151  if (l==NULL)
152  return(NULL);
153  else
154  return(*l);
155 }
156 
157 unsigned int GetMechanismLinkID(char *ln,Tmechanism *m)
158 {
159  unsigned int i,nl;
160  Tlink **l;
161  boolean found;
162 
163  nl=VectorSize(&(m->links));
164  found=FALSE;
165  i=0;
166  while((!found)&&(i<nl))
167  {
168  l=(Tlink **)GetVectorElement(i,&(m->links));
169 
170  if ((l!=NULL)&&(strcmp(ln,GetLinkName(*l))==0))
171  found=TRUE;
172  else
173  i++;
174  }
175 
176  if (found)
177  return(i);
178  else
179  return(NO_UINT);
180 }
181 
183 {
184  Tjoint **j;
185 
186  j=(Tjoint **)GetVectorElement(i,&(m->joints));
187  if (j==NULL)
188  return(NULL);
189  else
190  return(*j);
191 }
192 
194 {
195  unsigned int nj,i;
196  boolean all;
197 
198  nj=GetMechanismNJoints(m);
199  all=TRUE;
200  i=0;
201  while((all)&&(i<nj))
202  {
204  i++;
205  }
206  return(all);
207 }
208 
209 void PlotMechanism(Tplot3d *pt,double axesLength,Tmechanism *m)
210 {
211  unsigned int i;
212  unsigned int n;
213  Tlink *l;
214 
215  n=GetMechanismNLinks(m);
216  if (n>0) Start3dBlock(pt);
217  for(i=0;i<n;i++)
218  {
219  l=GetMechanismLink(i,m);
220  if (l!=NULL)
221  PlotLink(pt,axesLength,l);
222  }
223 
224  /*We only plot joints if there is at least one link to plot*/
225  if (n>0)
226  {
227  Tjoint *j;
228  unsigned int nj;
229 
230  nj=GetMechanismNJoints(m);
231  for(i=0;i<nj;i++)
232  {
233  j=GetMechanismJoint(i,m);
234  if (j!=NULL)
235  PlotJoint(pt,j);
236  }
237  }
238 
239  if (n>0) Close3dBlock(pt);
240 }
241 
243  double *sol,Tmechanism *m)
244 {
245  unsigned int i;
246  unsigned int n;
247  Tlink *l;
248  Tjoint *j;
249 
250  n=GetMechanismNLinks(m);
251  for(i=0;i<n;i++)
252  {
253  l=GetMechanismLink(i,m);
254  if (l!=NULL)
255  RegenerateLinkSolution(p,cs,sol,IsGroundLink(i),l);
256  }
257 
258  n=GetMechanismNJoints(m);
259  for(i=0;i<n;i++)
260  {
261  j=GetMechanismJoint(i,m);
262  if (j!=NULL)
263  RegenerateJointSolution(p,cs,sol,j);
264  }
265 }
266 
268 {
269  unsigned int i;
270  unsigned int n;
271  Tlink *l;
272  Tjoint *j;
273  unsigned int r;
274 
275  r=(unsigned int)(GetParameter(CT_REPRESENTATION,p));
276 
277  /* for DOF representations -> nothing to regenerate */
278  if (r!=REP_JOINTS)
279  {
280  n=GetMechanismNLinks(m);
281  for(i=0;i<n;i++)
282  {
283  l=GetMechanismLink(i,m);
284  if (l!=NULL)
285  RegenerateLinkBox(p,cs,b,IsGroundLink(i),l);
286  }
287 
288  n=GetMechanismNJoints(m);
289  for(i=0;i<n;i++)
290  {
291  j=GetMechanismJoint(i,m);
292  if (j!=NULL)
293  RegenerateJointBox(p,cs,b,j);
294  }
295  }
296 }
297 
299  Tmechanism *m)
300 {
301  Tjoint *j;
302  unsigned int i,nj,k;
303  unsigned int l1,l2;
304 
305  nj=GetMechanismNJoints(m);
306  k=0;
307  for(i=0;i<nj;i++)
308  {
309  j=GetMechanismJoint(i,m);
310  if (j!=NULL)
311  {
312  l1=JointFromID(j);
313  l2=JointToID(j);
314  GetJointDOFValues(p,&(tl[l1]),&(tl[l2]),&(dof[k]),j);
315  k+=GetJointDOF(j);
316  }
317  }
318 }
319 
321 {
322  unsigned int i,n;
323  Tlink *l;
324 
325  n=GetMechanismNLinks(m);
326  for(i=0;i<n;i++)
327  {
328  l=GetMechanismLink(i,m);
329  if (l!=NULL)
330  LinkPrintAtoms(f,&(tl[i]),l);
331  }
332  fprintf(f,"\n");
333 }
334 
336 {
337  unsigned int i,n,nr,na;
338  Tlink *l;
339 
340  n=GetMechanismNLinks(m);
341 
342  /* number of rigid = number of links with balls */
343  nr=0;
344  for(i=0;i<n;i++)
345  {
346  l=GetMechanismLink(i,m);
347  if (l!=NULL)
348  {
349  if (LinkNAtoms(l)>0)
350  nr++;
351  }
352  }
353  fprintf(f,"%u\n",nr);
354 
355  /* now store */
356  for(i=0;i<n;i++)
357  {
358  l=GetMechanismLink(i,m);
359  if (l!=NULL)
360  {
361  na=LinkNAtoms(l);
362  if (na>0)
363  {
364  fprintf(f,"%u %u\n",na,IsGroundLink(i));
365  LinkStoreAtoms(f,&(tl[i]),l);
366  }
367  }
368  }
369  fprintf(f,"\n");
370 }
371 
373  Tplot3d *pt,THTransform *tl,Tmechanism *m)
374 {
375  unsigned int i;
376  unsigned int n;
377  unsigned int l1,l2;
378  Tlink *l;
379 
380  n=GetMechanismNLinks(m);
381  if (n>0) Start3dBlock(pt);
382  for(i=1;i<n;i++) /*Ground link (ID==0) does not move*/
383  {
384  l=GetMechanismLink(i,m);
385  if (l!=NULL)
386  MoveLinkFromTransform(pt,&(tl[i]),l);
387  }
388 
389  if(n>0)
390  {
391  Tjoint *j;
392  unsigned int nj,k;
393 
394  /*We only attempt to move joints if there is at least one link to plot*/
395 
396  nj=GetMechanismNJoints(m);
397  k=0;
398  for(i=0;i<nj;i++)
399  {
400  j=GetMechanismJoint(i,m);
401  if (j!=NULL)
402  {
403  l1=JointFromID(j);
404  l2=JointToID(j);
405  MoveJointFromTransforms(pr,pt,&(tl[l1]),&(tl[l2]),j);
406  k+=GetJointDOF(j);
407  }
408  }
409  }
410 
411  if (n>0) Close3dBlock(pt);
412 }
413 
414 void PrintMechanism(FILE *f,char *path,char *prefix,Tmechanism *m)
415 {
416  unsigned int i,n;
417  Tlink *l;
418  Tjoint *j;
419 
420  n=GetMechanismNLinks(m);
421  if (n>0)
422  fprintf(f,"[LINKS]\n\n");
423  for(i=0;i<n;i++)
424  {
425  l=GetMechanismLink(i,m);
426  if (l!=NULL)
427  PrintLink(f,path,prefix,l);
428  }
429 
430  n=GetMechanismNJoints(m);
431  if (n>0)
432  fprintf(f,"[JOINTS]\n\n");
433  for(i=0;i<n;i++)
434  {
435  j=GetMechanismJoint(i,m);
436  if (j!=NULL)
437  PrintJoint(f,j);
438  }
439 }
440 
442 {
443 
444  unsigned int i;
445  unsigned int n;
446  Tlink *l;
447  Tjoint *j;
448 
449  n=GetMechanismNLinks(m);
450  for(i=0;i<n;i++)
451  {
452  l=GetMechanismLink(i,m);
453  if (l!=NULL)
454  {
455  DeleteLink(l);
456  free(l);
457  }
458  }
459 
460  n=GetMechanismNJoints(m);
461  for(i=0;i<n;i++)
462  {
463  j=GetMechanismJoint(i,m);
464  if (j!=NULL)
465  {
466  DeleteJoint(j);
467  free(j);
468  }
469  }
470 
471  DeleteVector(&(m->links));
472  DeleteVector(&(m->joints));
473 }
void RegenerateJointSolution(Tparameters *p, TCuikSystem *cs, double *sol, Tjoint *j)
Computes the values for the dummy variables used in the joint equations.
Definition: joint.c:2256
void DeleteVector(void *vector)
Destructor.
Definition: vector.c:388
signed int GetJointDOF(Tjoint *j)
Computes the degrees of freedom allowed by a given joint.
Definition: joint.c:1224
#define REP_JOINTS
One of the possible values of the REPRESENTATION parameter.
Definition: parameters.h:60
void PlotMechanism(Tplot3d *pt, double axesLength, Tmechanism *m)
Adds a mechanism to a 3d scene.
Definition: mechanism.c:209
unsigned int VectorSize(Tvector *vector)
Gets the number of elements in a vector.
Definition: vector.c:169
double GetJointMaxCoordinate(Tjoint *j)
Returns the maximum coordinate value for all the objects in the joint.
Definition: joint.c:3130
#define FALSE
FALSE.
Definition: boolean.h:30
void GetJointDOFValues(Tparameters *p, THTransform *t1, THTransform *t2, double *dof, Tjoint *j)
Recovers the joint DOFs from the absolute poses of the links.
Definition: joint.c:2644
unsigned int GetLinkID(char *name, Tmechanism *m)
Gets the identifier of a link given its name.
Definition: mechanism.c:50
#define INIT_NUM_LINKS
Initial room for links in a mechanism.
Definition: mechanism.h:28
Relation between two links.
Definition: joint.h:177
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
void * GetVectorElement(unsigned int i, Tvector *vector)
Returns a pointer to a vector element.
Definition: vector.c:269
A homgeneous transform in R^3.
double GetMechanismMaxCoordinate(Tmechanism *m)
Returns the sum of the maximum coordinate value for all the links and joints in the mechanism...
Definition: mechanism.c:45
boolean IsMechanismAllSpheres(Tmechanism *m)
TRUE if the mechanism is composed by spheres only.
Definition: mechanism.c:40
double maxCoord
Definition: mechanism.h:58
#define TRUE
TRUE.
Definition: boolean.h:21
void Close3dBlock(Tplot3d *p)
Ends a block of commands.
Definition: plot3d.c:146
void Error(const char *s)
General error function.
Definition: error.c:80
Tvector joints
Definition: mechanism.h:52
A mechanism description.
Definition: mechanism.h:51
boolean AllRevolute(Tmechanism *m)
TRUE if all joints are revolute joints.
Definition: mechanism.c:193
void InitMechanism(Tmechanism *m)
Constructor.
Definition: mechanism.c:16
unsigned int nbodies
Definition: mechanism.h:56
void CopyVoidPtr(void *a, void *b)
Copy constructor for void pointers.
Definition: vector.c:87
void GetMechanismDOFsFromTransforms(Tparameters *p, THTransform *tl, double *dof, Tmechanism *m)
Extract the joint DOF values form the poses of all links.
Definition: mechanism.c:298
unsigned int AddJoint2Mechanism(Tjoint *j, Tmechanism *m)
Adds a joint to a mechanism.
Definition: mechanism.c:111
A polyhedron.
Definition: polyhedron.h:124
Tjoint * GetMechanismJoint(unsigned int i, Tmechanism *m)
Gets a joint given its identifier.
Definition: mechanism.c:182
unsigned int GetMechanismNBodies(Tmechanism *m)
Gets the number of convex sub-parts (or bodies) of a mechanism.
Definition: mechanism.c:35
A 3D plot.
Definition: plot3d.h:54
#define INIT_NUM_JOINTS
Initial room for joints in a mechanism.
Definition: mechanism.h:36
unsigned int GetMechanismNLinks(Tmechanism *m)
Gets the number of links of a mechanism.
Definition: mechanism.c:25
void GetMechanismDefiningPoint(unsigned int lID, unsigned int bID, unsigned int pID, double *p, Tmechanism *m)
Gets a point from the mechanism.
Definition: mechanism.c:88
unsigned int GetMechanismNJoints(Tmechanism *m)
Gets the number of joints of a mechanism.
Definition: mechanism.c:30
void DeleteMechanism(Tmechanism *m)
Destructor.
Definition: mechanism.c:441
void InitVector(unsigned int ele_size, void(*Copy)(void *, void *), void(*Delete)(void *), unsigned int max_ele, Tvector *vector)
Constructor.
Definition: vector.c:100
void MoveMechanismFromTransforms(Tparameters *pr, Tplot3d *pt, THTransform *tl, Tmechanism *m)
Displaces a mechanism in a 3d scene.
Definition: mechanism.c:372
void SetVectorElement(unsigned int i, void *e, Tvector *vector)
Adds an element to the vector in a given position.
Definition: vector.c:234
Definition of the Tmechanism type and the associated functions.
unsigned int GetJointType(Tjoint *j)
Gets the joint type.
Definition: joint.c:931
A table of parameters.
unsigned int AddLink2Mechanism(Tlink *l, Tmechanism *m)
Adds a link to a mechanism.
Definition: mechanism.c:94
void MoveJointFromTransforms(Tparameters *p, Tplot3d *pt, THTransform *t1, THTransform *t2, Tjoint *j)
Displaces a joint in a 3d scene.
Definition: joint.c:3199
void PrintMechanism(FILE *f, char *path, char *prefix, Tmechanism *m)
Stores the mechanisms information into a file.
Definition: mechanism.c:414
#define CT_REPRESENTATION
Representation.
Definition: parameters.h:215
#define REV_JOINT
One of the possible type of joints.
Definition: joint.h:59
A box.
Definition: box.h:83
void PlotJoint(Tplot3d *pt, Tjoint *j)
Adds a joint to a 3d scene.
Definition: joint.c:3135
void PrintJoint(FILE *f, Tjoint *j)
Stores the joint information into a file.
Definition: joint.c:3310
Tlink * GetMechanismLink(unsigned int i, Tmechanism *m)
Gets a link given its identifier.
Definition: mechanism.c:146
A cuiksystem, i.e., a set of variables and equations defining a position analysis problem...
Definition: cuiksystem.h:181
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
unsigned int JointFromID(Tjoint *j)
Gets the identifier of the first link involved in the joint.
Definition: joint.c:941
unsigned int AddBody2Mechanism(unsigned int lID, Tpolyhedron *b, Tmechanism *m)
Adds a convex sub-part to a mechanism.
Definition: mechanism.c:127
void RegenerateMechanismBox(Tparameters *p, TCuikSystem *cs, Tbox *b, Tmechanism *m)
Computes the values for the non-system variables.
Definition: mechanism.c:267
Tvector links
Definition: mechanism.h:53
void DeleteVoidPtr(void *a)
Destructor for void pointers.
Definition: vector.c:92
void GetPolyhedronDefiningPoint(unsigned int i, double *point, Tpolyhedron *p)
Gets a point defining a a object.
Definition: polyhedron.c:1190
unsigned int GetMechanismLinkID(char *ln, Tmechanism *m)
Gets a link identifier given its name.
Definition: mechanism.c:157
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
signed int GetMechanismMobility(Tmechanism *m)
Computes the mobility of a given mechanism.
Definition: mechanism.c:73
boolean allSpheres
Definition: mechanism.h:57
boolean IsJointAllSpheres(Tjoint *j)
Identifies joint formed only by spheres.
Definition: joint.c:1004
void MechanismPrintAtoms(FILE *f, THTransform *tl, Tmechanism *m)
Prints the center of the atoms in a mechanism.
Definition: mechanism.c:320
void MechanismStoreRigidAtoms(FILE *f, THTransform *tl, Tmechanism *m)
Auxiliary function for WorldStoreRigidGroups.
Definition: mechanism.c:335
void DeleteJoint(Tjoint *j)
Destructor.
Definition: joint.c:3461
void CopyJoint(Tjoint *j_dst, Tjoint *j_src)
Copy constructor.
Definition: joint.c:873
void Start3dBlock(Tplot3d *p)
Starts a block of commands.
Definition: plot3d.c:141
unsigned int JointToID(Tjoint *j)
Gets the identifier of the second link involved in the joint.
Definition: joint.c:951
void RegenerateMechanismSolution(Tparameters *p, TCuikSystem *cs, double *sol, Tmechanism *m)
Computes the values for the non-system variables used to represent the rotation matrices for all link...
Definition: mechanism.c:242
void RegenerateJointBox(Tparameters *p, TCuikSystem *cs, Tbox *b, Tjoint *j)
Computes the values for the dummy variables used in the joint equations.
Definition: joint.c:2292