1 | //------------------------------------------------------------------------------ |
---|
2 | // quad_tree.h - Header for c implementation of the generic quad_tree. The |
---|
3 | // tree stores a set of triangles and then facilitates quickly |
---|
4 | // searching for a triangles in the list containing a given |
---|
5 | // point. |
---|
6 | // Tree has 'new' and 'delete' methods that should be called to |
---|
7 | // make sure memory is allocated and freed after use. |
---|
8 | // author: Padarn Wilson, date: 25/10/120 |
---|
9 | //------------------------------------------------------------------------------- |
---|
10 | #include <stdio.h> /* gets */ |
---|
11 | #include <stdlib.h> /* atoi, malloc */ |
---|
12 | #include <string.h> /* strcpy */ |
---|
13 | #include <math.h> |
---|
14 | |
---|
15 | #ifndef quad_tree_H |
---|
16 | #define quad_tree_H |
---|
17 | |
---|
18 | |
---|
19 | // *********************************************************************** |
---|
20 | // triangle struct - Struct that stores data for triangles being inseted |
---|
21 | // into the tree. Triangles form a linked list for easy |
---|
22 | // storage. |
---|
23 | |
---|
24 | typedef struct triangle{ |
---|
25 | |
---|
26 | // points defining triangle. |
---|
27 | double x1,y1; |
---|
28 | double x2,y2; |
---|
29 | double x3,y3; |
---|
30 | |
---|
31 | // index stores the triangles unique id. |
---|
32 | int index; |
---|
33 | |
---|
34 | // outward normal vectors of triangles sides. |
---|
35 | double nx1,ny1; |
---|
36 | double nx2,ny2; |
---|
37 | double nx3,ny3; |
---|
38 | |
---|
39 | // next triangle turns the struct into a linked list. |
---|
40 | struct triangle * next; |
---|
41 | |
---|
42 | } triangle; |
---|
43 | |
---|
44 | // creates a new triangle and returns a pointer to the malloc'ed memory. |
---|
45 | triangle * new_triangle(int index, double x1, double x2, double x3, |
---|
46 | double y1, double y2, double y3); |
---|
47 | |
---|
48 | // deletes entire list of triangles |
---|
49 | void delete_triangle_list(triangle * T); |
---|
50 | |
---|
51 | // take a point and calculate 'sigma' with the given triangle. returns |
---|
52 | // a pointer to malloc'ed memory of a double array. |
---|
53 | double * calculate_sigma(triangle * T,double x,double y); |
---|
54 | |
---|
55 | // Tests to see if a triangle contains a given point, |
---|
56 | // returns a int value 0 false, 1 true. |
---|
57 | int triangle_contains_point(triangle * T,double pointx,double pointy); |
---|
58 | |
---|
59 | //************************************************************************** |
---|
60 | |
---|
61 | |
---|
62 | //************************************************************************** |
---|
63 | // quad_tree struct - Each quad_tree struct is a node in the tree, and |
---|
64 | // can be treated as an independent quad_tree. |
---|
65 | |
---|
66 | typedef struct quad_tree{ |
---|
67 | |
---|
68 | // rectangular extents of current node |
---|
69 | double xmin,xmax,ymin,ymax; |
---|
70 | int count; |
---|
71 | // parent and children of quad_tree |
---|
72 | struct quad_tree *parent; |
---|
73 | struct quad_tree *q[4]; |
---|
74 | |
---|
75 | // triangle stored in this node - leaves is a linked list of triangles |
---|
76 | triangle * leaves; |
---|
77 | // triangle end_leaves allows easy adding onto end of triangles |
---|
78 | triangle * end_leaves; |
---|
79 | |
---|
80 | } quad_tree; |
---|
81 | |
---|
82 | // returns a new quad_tree pointer with malloc'ed memory. Inputs are the |
---|
83 | // extents of the node. |
---|
84 | quad_tree* new_quad_tree(double xmin, double xmax, double ymin, double ymax); |
---|
85 | |
---|
86 | // delete a search tree - recursively deletes all children. |
---|
87 | void delete_quad_tree(quad_tree * tree); |
---|
88 | |
---|
89 | // add a new triangle to the quad_tree |
---|
90 | void quad_tree_insert_triangle(quad_tree *node,triangle *T); |
---|
91 | |
---|
92 | // returns the quadrant of the quad_tree containing the point, or 0 if intersects |
---|
93 | // center axes |
---|
94 | int trivial_contain_split_point(quad_tree *node, double xp,double yp); |
---|
95 | |
---|
96 | // returns the quadrant of the quad_tree containing the triangle, or 0 if intersects |
---|
97 | // center axes |
---|
98 | int trivial_contain_split(quad_tree *node, triangle *T); |
---|
99 | |
---|
100 | // returns the triangle in the quad_tree's leaves containing the point or NULL |
---|
101 | // if none of the triangles on the current quad_tree contain it. |
---|
102 | triangle * search_triangles_of_quad_tree(quad_tree * node,double xp,double yp); |
---|
103 | |
---|
104 | // search the tree for a triangle containing the given point. returns the triangle |
---|
105 | // if found, and NULL otherwise |
---|
106 | triangle * search(quad_tree * node ,double xp, double yp); |
---|
107 | |
---|
108 | // return number of noes in tree |
---|
109 | int quad_tree_node_count(quad_tree * tree); |
---|
110 | |
---|
111 | // split the node to make 4 children |
---|
112 | void quad_tree_make_children(quad_tree *node); |
---|
113 | |
---|
114 | // add a triangle to the nodes leaves |
---|
115 | void quad_tree_add_triangle_to_list(quad_tree *node,triangle *T); |
---|
116 | |
---|
117 | |
---|
118 | //************************************************************************** |
---|
119 | |
---|
120 | //************************************************************************** |
---|
121 | // quad_tree_ll struct - Used for serialising |
---|
122 | // |
---|
123 | |
---|
124 | typedef struct quad_tree_ll { |
---|
125 | |
---|
126 | void * tree; |
---|
127 | struct quad_tree_ll * next; |
---|
128 | int index; |
---|
129 | |
---|
130 | } quad_tree_ll; |
---|
131 | |
---|
132 | quad_tree_ll * new_quad_tree_ll(quad_tree * start,int index); |
---|
133 | |
---|
134 | //************************************************************************** |
---|
135 | |
---|
136 | //************************************************************************** |
---|
137 | // queue_ll struct - Used for deserialising |
---|
138 | // |
---|
139 | |
---|
140 | typedef struct queue_ll { |
---|
141 | |
---|
142 | int node; |
---|
143 | struct queue_ll * next; |
---|
144 | |
---|
145 | |
---|
146 | } queue_ll; |
---|
147 | |
---|
148 | queue_ll * new_queue_ll(int node); |
---|
149 | |
---|
150 | //************************************************************************** |
---|
151 | |
---|
152 | #endif |
---|