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  if (!EmptyPolyhedron(o))
23  {
24  char *namei;
25  Tpolyhedron *pi;
26  unsigned int k;
27  double mc;
28 
29  NEW(namei,strlen(name)+1,char);
30  strcpy(namei,name);
31  k=NewVectorElement(&namei,&(e->names));
32 
33  NEW(pi,1,Tpolyhedron);
34  CopyPolyhedron(pi,o);
35  SetVectorElement(k,&pi,&(e->obstacles));
36 
38  if (mc>e->maxCoord)
39  e->maxCoord=mc;
40  }
41 }
42 
44 {
45  return(VectorSize(&(e->obstacles))); /*Equal to the size of 'name' vector*/
46 }
47 
48 unsigned int GetObstacleID(char *name,Tenvironment *e)
49 {
50  char **namei;
51  boolean found;
52  unsigned int i,n;
53 
54  n=VectorSize(&(e->names));
55  found=FALSE;
56  i=0;
57  while((!found)&&(i<n))
58  {
59  namei=(char **)GetVectorElement(i,&(e->names));
60  if (namei!=NULL)
61  {
62  if (strcmp(name,*namei)==0)
63  found=TRUE;
64  }
65  if (!found)
66  i++;
67  }
68  if (found)
69  return(i);
70  else
71  return(NO_UINT);
72 }
73 
74 char *GetObstacleName(unsigned int i,Tenvironment *e)
75 {
76  char **n;
77 
78  n=(char **)GetVectorElement(i,&(e->names));
79  if (n==NULL)
80  return(NULL);
81  else
82  return(*n);
83 }
84 
86 {
87  Tpolyhedron **p;
88 
89  p=(Tpolyhedron **)GetVectorElement(i,&(e->obstacles));
90  if (p==NULL)
91  return(NULL);
92  else
93  return(*p);
94 }
95 
96 unsigned int GetObstacleShapeStatus(unsigned int i,Tenvironment *e)
97 {
98  Tpolyhedron **p;
99 
100  p=(Tpolyhedron **)GetVectorElement(i,&(e->obstacles));
101  if (p==NULL)
102  return(NO_UINT);
103  else
104  return(GetPolyhedronStatus(*p));
105 }
106 
107 void GetObstacleColor(unsigned int i,Tcolor *c,Tenvironment *e)
108 {
109  Tpolyhedron **p;
110 
111  p=(Tpolyhedron **)GetVectorElement(i,&(e->obstacles));
112  if (p==NULL)
113  Error("Undefined body in GetObstacleColor");
114  else
115  GetPolyhedronColor(c,*p);
116 }
117 
119 {
120  return(e->maxCoord);
121 }
122 
123 void PrintEnvironment(FILE *f,char *path,Tenvironment *e)
124 {
125  unsigned int i;
126  unsigned int n;
127  char *name;
128  Tpolyhedron *p;
129 
131  if (n>0)
132  fprintf(f,"[OBSTACLES]\n\n");
133  for(i=0;i<n;i++)
134  {
135  name=GetObstacleName(i,e);
136  p=GetObstacleShape(i,e);
137  if ((name!=NULL)&&(p!=NULL))
138  {
139  fprintf(f," %s : ",name);
140  PrintPolyhedron(f,path,NULL,i,p);
141  }
142  }
143  fprintf(f,"\n");
144 }
145 
147 {
148  unsigned int i;
149  unsigned int n;
150  Tpolyhedron *p;
151  Tcolor c;
152 
154  NewColor(DCP3D_R,DCP3D_G,DCP3D_G,&c); /*Default color. Actually not used since
155  each polyhedron has its own color. */
156  for(i=0;i<n;i++)
157  {
158  p=GetObstacleShape(i,e);
159  if (p!=NULL)
160  {
161  StartNew3dObject(&c,pt);
162  PlotPolyhedron(pt,p);
163  Close3dObject(pt);
164  }
165  }
166  DeleteColor(&c);
167 }
168 
170 {
171  unsigned int i,n;
172  Tpolyhedron *p;
173  char *name;
174 
176  for(i=0;i<n;i++)
177  {
178  p=GetObstacleShape(i,e);
179  if (p!=NULL)
180  {
181  DeletePolyhedron(p);
182  free(p);
183  }
184  name=GetObstacleName(i,e);
185  if (name!=NULL)
186  free(name);
187  }
188  DeleteVector(&(e->obstacles));
189  DeleteVector(&(e->names));
190 }
void DeleteVector(void *vector)
Destructor.
Definition: vector.c:389
unsigned int VectorSize(Tvector *vector)
Gets the number of elements in a vector.
Definition: vector.c:173
#define FALSE
FALSE.
Definition: boolean.h:30
boolean EmptyPolyhedron(Tpolyhedron *p)
Used for objects that failed to initialize.
Definition: polyhedron.c:856
#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:270
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:1463
#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:1574
void Error(const char *s)
General error function.
Definition: error.c:80
A color.
Definition: color.h:86
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:96
void PrintEnvironment(FILE *f, char *path, Tenvironment *e)
Stores the environment information into a file.
Definition: environment.c:123
A polyhedron.
Definition: polyhedron.h:134
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:238
void DeleteEnvironment(Tenvironment *e)
Destructor.
Definition: environment.c:169
void GetPolyhedronColor(Tcolor *c, Tpolyhedron *p)
Gets the color of a polyhedron.
Definition: polyhedron.c:1373
double GetPolyhedronMaxCoordinate(Tpolyhedron *p)
Returns the maximum coordinate value used in a polyhedron.
Definition: polyhedron.c:1458
unsigned int GetPolyhedronStatus(Tpolyhedron *p)
Gets the status of a polyhedron (NORMAL, HIDDEN, DECOR).
Definition: polyhedron.c:1378
#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:74
#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:1692
void DeleteColor(Tcolor *c)
Destructor.
Definition: color.c:143
#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:118
Tpolyhedron * GetObstacleShape(unsigned int i, Tenvironment *e)
Gets the convex polyhedron of an obstacle given its identifier.
Definition: environment.c:85
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:48
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:1259
void GetObstacleColor(unsigned int i, Tcolor *c, Tenvironment *e)
Gets the color of an obstacle given its identifier.
Definition: environment.c:107
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:146
unsigned int NewVectorElement(void *e, Tvector *vector)
Adds an element to the vector.
Definition: vector.c:216
unsigned int GetEnvironmentNObstacles(Tenvironment *e)
Gets the number of obstacles in the environment.
Definition: environment.c:43