Initial Import
[glAntsMech.git] / glants_mech / linux / src / octree.c
blob82031998e37a23f900cdce82339b2fddbc9b17ab
1 //
2 // bsp.cpp
3 // This may border on octree but they are similar
4 //
5 // the goal is to use this code with the pheremones
6 // when they are dropped by the ants
7 // if an ant drops 100 pheremones on the way to the nest
8 // 100 * 500, it would be silly to have to test all of these
9 // so some algorithm is needed to save cpu time
11 // - the octree is an array of pointers to seperate lists
12 // - the lists contains the position of the pheremone
13 // - whenever a pheremone is created, it is added to the list
14 // - whenever a pheremone is deleted, is is removed from the list
15 // - whenever the bot moves, he searches the list
16 // - better than brute force, of course
17 // - since we are using a hash function, possibly O(1)
20 #include <stdlib.h>
21 #include <stdio.h>
23 #include <GL/gl.h> // Header File For The OpenGL32 Library
24 #include <GL/glu.h> // Header File For The GLu32 Library
27 #include "objects.h"
28 #include "bot.h"
29 #include "tree.h"
30 #include "gldrawlib.h"
31 #include "plist.h"
32 #include "octree.h"
34 Octree **pheromone_tree;
35 Octree **GenerateOctree(void);
38 // CreateOctree
40 Octree *CreateOctree(void)
42 Octree *ptr;
43 ptr = (Octree *)malloc(sizeof(Octree));
45 return ptr;
47 } // end of the function
50 // DeleteOctree
51 //
52 void DeleteOctree(Octree **tree_ptr)
54 // delete the lists on the tree
55 int i;
56 int max;
57 max = tree_ptr[0]->max_elements;
59 for (i = 0; i < max; i++)
61 DestroyPtrList(tree_ptr[i]->list);
63 } // end of the for
65 // have to delete every node individually
66 for (i = 0; i < max; i++)
68 free(tree_ptr[i]);
69 } // end of the for
71 free(tree_ptr);
73 } // end of the function
76 // GenerateOctree
77 // - struct has xmin,xmax,ymin, ymax
78 // and a pointer to a list
80 Octree **GenerateOctree(void)
83 float i,j;
84 float width = 7.5f;
85 float height = width; // square grid
86 float x_min,x_max, y_min, y_max;
87 int hash_key = 0;
88 int count = 0;
89 int z = 0;
90 int index = 0;
91 Octree **tree_ptr;
93 for (i = -30.0f; i < 30.0f; i+= height)
94 for (j = -30.0f; j < 30.0f; j+= width)
96 count++;
97 } // end of the for
99 // First we have to create an array of pointers
100 tree_ptr = (Octree **)malloc(count * sizeof(Octree *));
102 // Create each node
103 for (index = 0; index < count; index++)
104 tree_ptr[index] = CreateOctree();
107 z = 0; // reset counter
109 // populate the tree with data
110 for (i = -30.0f; i < 30.0f; i+= height)
112 for (j = -30.0f; j < 30.0f; j+= width)
114 x_min = j;
115 y_min = i;
116 x_max = j + width;
117 y_max = i + width;
119 tree_ptr[z]->list = CreatePtrList();
120 tree_ptr[z]->x_max= x_max;
121 tree_ptr[z]->x_min= x_min;
122 tree_ptr[z]->y_max = y_max;
123 tree_ptr[z]->y_min = y_min;
124 tree_ptr[z]->max_elements = count;
126 z++;
127 } // end of the for
129 } // end of the for
131 return tree_ptr;
133 } // end of the function
137 // InsertOctree
138 // - insert a pheremone into the tree
140 void InsertOctree(Octree **tree_ptr, StaticBotPtr bot)
142 int i;
143 float x_min,y_min, x_max, y_max;
144 int max = tree_ptr[0]->max_elements;
146 for (i = 0; i < max; i++)
148 x_min = tree_ptr[i]->x_min;
149 x_max = tree_ptr[i]->x_max;
150 y_min = tree_ptr[i]->y_min;
151 y_max = tree_ptr[i]->y_max;
153 if ((bot->position[0] > x_min) &&
154 (bot->position[0] < x_max) &&
155 (bot->position[2] > y_min) &&
156 (bot->position[2] < y_max))
158 // in the area add to list
159 InsertFront(tree_ptr[i]->list, (StaticBotPtr)bot);
160 return;
161 } // end of the if
163 } // end of the for
165 } // end of the function
169 // SearchListBot
170 // - search the list for a static bot
172 StaticBotPtr SearchListBot(PtrList *list, DriverBotPtr bot)
174 PtrNode *current_ptr;
175 StaticBotPtr x;
176 float x_min, x_max, y_min, y_max;
178 //if (isempty(list))
179 // return NULL;
180 if (list->head == NULL)
181 return NULL;
183 current_ptr = list->head;
185 while(current_ptr != NULL)
187 // interesting
188 x = (StaticBotPtr)current_ptr->ptr;
190 x_min = x->position[0] - (x->size[0]/2.0f);
191 x_max = x->position[0] + (x->size[0]/2.0f);
192 y_min = x->position[2] - (x->size[0]/2.0f);
193 y_max = x->position[2] + (x->size[0]/2.0f);
195 if ((bot->x > x_min) && (bot->x < x_max) &&
196 (bot->y > y_min) && (bot->y < y_max))
198 return x;
199 } // end of the if
201 current_ptr = current_ptr->next;
203 } // end of while
205 return NULL;
207 } // end of the function
210 // SearchOctree
211 // - check if we are in the region
213 StaticBotPtr SearchOctree(Octree **tree_ptr, DriverBotPtr bot)
215 // find out which bin to search
216 int i;
217 float x_min,y_min, x_max, y_max;
218 int max = tree_ptr[0]->max_elements;
220 StaticBotPtr res = NULL;
222 for (i = 0; i < max; i++)
224 x_min = tree_ptr[i]->x_min;
225 x_max = tree_ptr[i]->x_max;
226 y_min = tree_ptr[i]->y_min;
227 y_max = tree_ptr[i]->y_max;
229 if ((bot->x > x_min) &&
230 (bot->x < x_max) &&
231 (bot->y > y_min) &&
232 (bot->y < y_max))
234 // in the area add to list
235 res = SearchListBot(tree_ptr[i]->list, bot);
237 // Search the list in this region
238 return res;
239 } // end of the if
242 } // end of the for
244 return NULL;
246 } // end of the function
249 // DeleteOctree
250 // - delete a node from the tree
252 void DeleteOctreeNode(Octree **tree_ptr, StaticBotPtr bot)
254 // find out which bin to search
255 int i;
256 float x_min,y_min, x_max, y_max;
257 int max = tree_ptr[0]->max_elements;
259 for (i = 0; i < max; i++)
261 x_min = tree_ptr[i]->x_min;
262 x_max = tree_ptr[i]->x_max;
263 y_min = tree_ptr[i]->y_min;
264 y_max = tree_ptr[i]->y_max;
266 if ((bot->position[0] > x_min) &&
267 (bot->position[0] < x_max) &&
268 (bot->position[2] > y_min) &&
269 (bot->position[2] < y_max))
272 // delete the node
273 DeletePtrNode(tree_ptr[i]->list, bot);
275 } // end of the if
278 } // end of the for
280 } // end of the function
284 // Wrapper Functions
285 // - you should probably only have to deal
286 // with these functions unless you want more
287 // trees
288 void pheromoneBuild(void)
290 pheromone_tree = GenerateOctree();
291 } // end of the function
294 // octreeDestroy
295 // - make sure this called
297 void pheromoneDestroy(void)
299 DeleteOctree(pheromone_tree);
300 } // end of the function
303 // pheromoneInsert(StaticBotPtr bot)
305 void pheromoneInsert(StaticBotPtr bot)
307 InsertOctree(pheromone_tree, bot);
308 }// end of the function
311 // pheromoneSearch
313 StaticBotPtr pheromoneSearch(DriverBotPtr bot)
315 StaticBotPtr ptr;
316 ptr = SearchOctree(pheromone_tree, bot);
318 return ptr;
319 } // end of the function
322 // pheromoneDelete
323 // - delete a pheromone
325 void pheromoneDelete(StaticBotPtr bot)
328 DeleteOctreeNode(pheromone_tree, bot);
330 } // end of the function