29 pthread_rwlock_init(&(tree->
rwlock), NULL);
43 n_log(
LOG_ERR,
"Failed to allocate memory for tree node.\n");
63 if (parent == NULL || child == NULL) {
74 if( node -> destroy_func != NULL )
76 node -> destroy_func( node -> ptr );
79 n_log(
LOG_ERR,
"Failed to create parent->children list");
96 if (tree == NULL || node == NULL) {
105 child_node = child_node->
next;
132 if (tree == NULL || *tree == NULL) {
141 pthread_rwlock_destroy(&((*tree)->rwlock));
164 return (a.f > b.f) - (a.f < b.f);
174 return (a.d > b.d) - (a.d < b.d);
199 printf(
"%lf", val.d);
212 switch (coord_type) {
263 if (qt->
compare(x, (*root)->x) < 0 && qt->
compare(y, (*root)->y) < 0) {
264 insert(qt, &((*root)->sw), x, y, data_ptr);
265 }
else if (qt->
compare(x, (*root)->x) < 0 && qt->
compare(y, (*root)->y) >= 0) {
266 insert(qt, &((*root)->nw), x, y, data_ptr);
267 }
else if (qt->
compare(x, (*root)->x) >= 0 && qt->
compare(y, (*root)->y) < 0) {
268 insert(qt, &((*root)->se), x, y, data_ptr);
269 }
else if (qt->
compare(x, (*root)->x) >= 0 && qt->
compare(y, (*root)->y) >= 0) {
270 insert(qt, &((*root)->ne), x, y, data_ptr);
283 if (root == NULL || (qt->
compare(root->
x, x) == 0 && qt->
compare(root->
y, y) == 0)) {
305 if (root == NULL)
return;
326 for (
int i = 0; i < 8; i++) {
356 if (type == COORD_INT) {
357 if (point.
x.i >= center.
x.i) octant |= 4;
358 if (point.
y.i >= center.
y.i) octant |= 2;
359 if (point.
z.i >= center.
z.i) octant |= 1;
360 }
else if (type == COORD_FLOAT) {
361 if (point.
x.f >= center.
x.f) octant |= 4;
362 if (point.
y.f >= center.
y.f) octant |= 2;
363 if (point.
z.f >= center.
z.f) octant |= 1;
364 }
else if (type == COORD_DOUBLE) {
365 if (point.
x.d >= center.
x.d) octant |= 4;
366 if (point.
y.d >= center.
y.d) octant |= 2;
367 if (point.
z.d >= center.
z.d) octant |= 1;
408 for (
int i = 0; i < 8; i++) {
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define Free(__ptr)
Free Handler to get errors.
void * ptr
void pointer to store
LIST_NODE * start
pointer to the start of the list
struct LIST_NODE * next
pointer to the next node
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
LIST_NODE * new_list_node(void *ptr, void(*destructor)(void *ptr))
Allocate a new node to link in a list.
void * remove_list_node_f(LIST *list, LIST_NODE *node)
Internal function called each time we need to get a node out of a list.
LIST * new_generic_list(int max_items)
Initialiaze a generic list container to max_items pointers.
Structure of a generic list node.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
print_func print
pointer to print function
compare_func compare
pointer to comparison function
TREE_NODE * root
pointer to first node
pthread_rwlock_t rwlock
mutex for thread safety (optional)
struct QUADTREE_NODE * nw
North-West child.
int coord_type
type of coordinate used in the quad tree
size_t nb_nodes
number of nodes in the tree
COORD_VALUE z
z coordinate
void * data_ptr
Pointer to additional data, can be NULL.
OCTREE_NODE * root
tree list first node
COORD_VALUE y
Y coordinate.
QUADTREE_NODE * root
tree list first node
union NODE_DATA_TYPES value
node value
NODE_DATA data
structure holding values for node
struct QUADTREE_NODE * sw
South-West child.
COORD_VALUE x
x coordinate
void * data_ptr
Pointer to data, can be NULL.
struct QUADTREE_NODE * se
South-East child.
POINT3D point
Point represented by this node.
struct QUADTREE_NODE * ne
North-East child.
void(* destroy_func)(void *ptr)
value destructor if of type ptr and specified, else a simple free will be used
COORD_VALUE y
y coordinate
COORD_VALUE x
X coordinate.
struct OCTREE_NODE * children[8]
Child nodes.
LIST_NODE * parent_list_node
pointer to parent container of the TREE_NODE, LIST_NODE
struct TREE_NODE * parent
pointer to parent
size_t height
height of the tree
LIST * children
ordered list of children
int coord_type
Coordinate type for the entire tree.
void free_octree_node(OCTREE_NODE *node)
recursive function to free an OCTREE node and its children
void insert_octree(OCTREE *octree, POINT3D point, void *data_ptr)
Insert a point into the OCTREE.
int tree_insert_child(TREE_NODE *parent, TREE_NODE *child)
insert a child node into the parent node
void free_quadtree(QUADTREE_NODE *root)
Function to free the quad tree.
TREE * new_tree()
create a new TREE
TREE_NODE * tree_create_node(NODE_DATA value, void(*destroy_func)(void *ptr))
create a TREE node
QUADTREE_NODE * search(QUADTREE *qt, QUADTREE_NODE *root, COORD_VALUE x, COORD_VALUE y)
Function to search for a point in the quad tree.
void free_octree(OCTREE *octree)
free the OCTREE
void tree_destroy(TREE **tree)
destroy a TREE
OCTREE_NODE * create_octree_node(POINT3D point, void *data_ptr)
create and OCTREE node
OCTREE * create_octree(int type)
Create a new OCTREE with a specified coordinate type.
int tree_delete_node(TREE *tree, TREE_NODE *node)
delete a TREE node
void insert(QUADTREE *qt, QUADTREE_NODE **root, COORD_VALUE x, COORD_VALUE y, void *data_ptr)
Function to insert a point into the quad tree.
QUADTREE * create_quadtree(int coord_type)
Function to create a new quad tree.
QUADTREE_NODE * create_node(COORD_VALUE x, COORD_VALUE y, void *data_ptr)
function to create a new quad tree node
structure of a TREE node data
structure of an OCTREE node
Structure for a POINT3D in the 3D space.
structure of a quad tree node
structure of a n-ary TREE node
Union to store the coordinate values.
Common headers and low-level hugly functions & define.
N_STR and string function declaration.
int determine_octant(POINT3D point, POINT3D center, int type)
function to determine the octant for the given point relative to the node
void insert_octree_node(OCTREE_NODE *node, POINT3D point, void *data_ptr, int type)
recursive function to insert a point into the OCTREE
void print_int(COORD_VALUE val)
int compare_float(COORD_VALUE a, COORD_VALUE b)
float comparison function
int compare_double(COORD_VALUE a, COORD_VALUE b)
double comparison functions
void print_double(COORD_VALUE val)
int compare_int(COORD_VALUE a, COORD_VALUE b)
int comparison function
void print_float(COORD_VALUE val)