environment.c
Go to the documentation of this file.
1 #include "environment.h"
2 
3 #include <string.h>
4 
14 {
15  e->maxCoord=0;
18 }
19 
21 {
22  char *namei;
23  Tpolyhedron *pi;
24  unsigned int k;
25  double mc;
26 
27  NEW(namei,strlen(name)+1,char);
28  strcpy(namei,name);
29  k=NewVectorElement(&namei,&(e->names));
30 
31  NEW(pi,1,Tpolyhedron);
32  CopyPolyhedron(pi,o);
33  SetVectorElement(k,&pi,&(e->obstacles));
34 
36  if (mc>e->maxCoord)
37  e->maxCoord=mc;
38 }
39 
41 {
42  return(VectorSize(&(e->obstacles))); /*Equal to the size of 'name' vector*/
43 }
44 
45 unsigned int GetObstacleID(char *name,Tenvironment *e)
46 {
47  char **namei;
48  boolean found;
49  unsigned int i,n;
50 
51  n=VectorSize(&(e->names));
52  found=FALSE;
53  i=0;
54  while((!found)&&(i<n))
55  {
56  namei=(char **)GetVectorElement(i,&(e->names));
57  if (namei!=NULL)
58  {
59  if (strcmp(name,*namei)==0)
60  found=TRUE;
61  }
62  if (!found)
63  i++;
64  }
65  if (found)
66  return(i);
67  else
68  return(NO_UINT);
69 }
70 
71 char *GetObstacleName(unsigned int i,Tenvironment *e)
72 {
73  char **n;
74 
75  n=(char **)GetVectorElement(i,&(e->names));
76  if (n==NULL)
77  return(NULL);
78  else
79  return(*n);
80 }
81 
83 {
84  Tpolyhedron **p;
85 
86  p=(Tpolyhedron **)GetVectorElement(i,&(e->obstacles));
87  if (p==NULL)
88  return(NULL);
89  else
90  return(*p);
91 }
92 
93 unsigned int GetObstacleShapeStatus(unsigned int i,Tenvironment *e)
94 {
95  Tpolyhedron **p;
96 
97  p=(Tpolyhedron **)GetVectorElement(i,&(e->obstacles));
98  if (p==NULL)
99  return(NO_UINT);
100  else
101  return(GetPolyhedronStatus(*p));
102 }
103 
104 void GetObstacleColor(unsigned int i,Tcolor *c,Tenvironment *e)
105 {
106  Tpolyhedron **p;
107 
108  p=(Tpolyhedron **)GetVectorElement(i,&(e->obstacles));
109  if (p==NULL)
110  Error("Undefined body in GetObstacleColor");
111  else
112  GetPolyhedronColor(c,*p);
113 }
114 
116 {
117  return(e->maxCoord);
118 }
119 
120 void PrintEnvironment(FILE *f,char *path,Tenvironment *e)
121 {
122  unsigned int i;
123  unsigned int n;
124  char *name;
125  Tpolyhedron *p;
126 
128  if (n>0)
129  fprintf(f,"[OBSTACLES]\n\n");
130  for(i=0;i<n;i++)
131  {
132  name=GetObstacleName(i,e);
133  p=GetObstacleShape(i,e);
134  if ((name!=NULL)&&(p!=NULL))
135  {
136  fprintf(f," %s : ",name);
137  PrintPolyhedron(f,path,NULL,i,p);
138  }
139  }
140  fprintf(f,"\n");
141 }
142 
144 {
145  unsigned int i;
146  unsigned int n;
147  Tpolyhedron *p;
148  Tcolor c;
149 
151  NewColor(DCP3D_R,DCP3D_G,DCP3D_G,&c); /*Default color. Actually not used since
152  each polyhedron has its own color. */
153  for(i=0;i<n;i++)
154  {
155  p=GetObstacleShape(i,e);
156  if (p!=NULL)
157  {
158  StartNew3dObject(&c,pt);
159  PlotPolyhedron(pt,p);
160  Close3dObject(pt);
161  }
162  }
163  DeleteColor(&c);
164 }
165 
167 {
168  unsigned int i,n;
169  Tpolyhedron *p;
170  char *name;
171 
173  for(i=0;i<n;i++)
174  {
175  p=GetObstacleShape(i,e);
176  if (p!=NULL)
177  {
178  DeletePolyhedron(p);
179  free(p);
180  }
181  name=GetObstacleName(i,e);
182  if (name!=NULL)
183  free(name);
184  }
185  DeleteVector(&(e->obstacles));
186  DeleteVector(&(e->names));
187 }
void DeleteVector(void *vector)
Destructor.
Definition: vector.c:388
unsigned int VectorSize(Tvector *vector)
Gets the number of elements in a vector.
Definition: vector.c:169
#define FALSE
FALSE.
Definition: boolean.h:30
#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
void AddShape2Environment(char *name, Tpolyhedron *o, Tenvironment *e)
Adds an obstacle (i.e., a convex polyhedron) to the environment.
Definition: environment.c:20
void PlotPolyhedron(Tplot3d *pt, Tpolyhedron *p)
Adds the polyhedron to a 3D geometry.
Definition: polyhedron.c:1255
#define TRUE
TRUE.
Definition: boolean.h:21
void PrintPolyhedron(FILE *f, char *path, char *label, unsigned int n, Tpolyhedron *p)
Stores the polyhedron information into a file.
Definition: polyhedron.c:1366
void Error(const char *s)
General error function.
Definition: error.c:80
A color.
Definition: color.h:23
void CopyVoidPtr(void *a, void *b)
Copy constructor for void pointers.
Definition: vector.c:87
unsigned int GetObstacleShapeStatus(unsigned int i, Tenvironment *e)
Gets the status (NORMAL, HIDDEN, DECOR) of an obstacle given its identifier.
Definition: environment.c:93
void PrintEnvironment(FILE *f, char *path, Tenvironment *e)
Stores the environment information into a file.
Definition: environment.c:120
A polyhedron.
Definition: polyhedron.h:124
A 3D plot.
Definition: plot3d.h:54
A collection of obstacles (convex polyhedrons) with their names.
Definition: environment.h:39
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 InitEnvironment(Tenvironment *e)
Constructor.
Definition: environment.c:13
void SetVectorElement(unsigned int i, void *e, Tvector *vector)
Adds an element to the vector in a given position.
Definition: vector.c:234
void DeleteEnvironment(Tenvironment *e)
Destructor.
Definition: environment.c:166
void GetPolyhedronColor(Tcolor *c, Tpolyhedron *p)
Gets the color of a polyhedron.
Definition: polyhedron.c:1165
double GetPolyhedronMaxCoordinate(Tpolyhedron *p)
Returns the maximum coordinate value used in a polyhedron.
Definition: polyhedron.c:1250
unsigned int GetPolyhedronStatus(Tpolyhedron *p)
Gets the status of a polyhedron (NORMAL, HIDDEN, DECOR).
Definition: polyhedron.c:1170
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
char * GetObstacleName(unsigned int i, Tenvironment *e)
Gets the name of an obstacle given its identifier.
Definition: environment.c:71
#define INIT_NUM_OBSTACLES
Initial room for obstacles.
Definition: environment.h:29
Definition of the Tenvironment type and the associated functions.
void DeleteVoidPtr(void *a)
Destructor for void pointers.
Definition: vector.c:92
#define DCP3D_G
Green component of the default color of 3d objects.
Definition: plot3d.h:38
void DeletePolyhedron(Tpolyhedron *p)
Destructor.
Definition: polyhedron.c:1484
void DeleteColor(Tcolor *c)
Destructor.
Definition: color.c:93
#define DCP3D_R
Red component of the default color of 3d objects.
Definition: plot3d.h:30
double GetEnvironmentMaxCoordinate(Tenvironment *e)
Returns the sum of the maximum coordinate value for all the convex polyhedrons in the environment...
Definition: environment.c:115
Tpolyhedron * GetObstacleShape(unsigned int i, Tenvironment *e)
Gets the convex polyhedron of an obstacle given its identifier.
Definition: environment.c:82
void NewColor(double r, double g, double b, Tcolor *c)
Constructor.
Definition: color.c:14
Tvector names
Definition: environment.h:41
unsigned int GetObstacleID(char *name, Tenvironment *e)
Gets the idetifier of an obstacles given its name.
Definition: environment.c:45
double maxCoord
Definition: environment.h:40
Tvector obstacles
Definition: environment.h:42
void CopyPolyhedron(Tpolyhedron *p_dst, Tpolyhedron *p_src)
Copy constructor.
Definition: polyhedron.c:1051
void GetObstacleColor(unsigned int i, Tcolor *c, Tenvironment *e)
Gets the color of an obstacle given its identifier.
Definition: environment.c:104
unsigned int StartNew3dObject(Tcolor *c, Tplot3d *p)
Start a composed object.
Definition: plot3d.c:157
void Close3dObject(Tplot3d *p)
Closes a composed object.
Definition: plot3d.c:171
void PlotEnvironment(Tplot3d *pt, Tenvironment *e)
Displays the obstacles in the environment in a 3D geometry.
Definition: environment.c:143
unsigned int NewVectorElement(void *e, Tvector *vector)
Adds an element to the vector.
Definition: vector.c:212
unsigned int GetEnvironmentNObstacles(Tenvironment *e)
Gets the number of obstacles in the environment.
Definition: environment.c:40