summaryrefslogtreecommitdiff
path: root/navit/map/csv/quadtree.c
blob: 1d1460f7280f0aa8889b520b4af1153099da4f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

#include <stdlib.h>
#include <glib.h>

#include "quadtree.h"

#define QUADTREE_NODE_CAPACITY 10

#define MAX_DOUBLE 9999999

static double 
dist_sq(double x1,double y1,double x2,double y2)
{
  return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}

struct quadtree_node*
quadtree_node_new(struct quadtree_node* parent, double xmin, double xmax, double ymin, double ymax ) {
    struct quadtree_node*ret = g_new0(struct quadtree_node,1);
    ret->xmin = xmin;
    ret->xmax = xmax;
    ret->ymin = ymin;
    ret->ymax = ymax;
    ret->is_leaf = 1;
    ret->parent = parent;
    return ret;
}

/*
 * searches all four subnodes recursively for the list of items within a rectangle
 */
void 
quadtree_find_rect_items(struct quadtree_node* this_, double dXMin, double dXMax, double dYMin, double dYMax, GList**out) {

    struct quadtree_node* nodes[4] = { this_->aa, this_->ab, this_->ba, this_->bb };
    if( this_->is_leaf ) { 
        int i;
        //double distance_sq = current_max;
        for(i=0;i<this_->node_num;++i) {	//select only items within input rectangle
		if(dXMin<=this_->items[i].longitude && this_->items[i].longitude<=dXMax &&
		   dYMin<=this_->items[i].latitude && this_->items[i].latitude<=dYMax
		  ) {
		    *out=g_list_prepend(*out,&this_->items[i]);
		}
        }
    }
    else {
      int i;
      for( i=0;i<4;++i) {
        if(nodes[i] ) {
          //limit flooding
	  if(nodes[i]->xmax<dXMin || dXMax<nodes[i]->xmin ||
	     nodes[i]->ymax<dYMin || dYMax<nodes[i]->ymin
	    ) {
	  	continue;
	  }
	  //recurse into subtiles if there is at least one subtile corner within input rectangle
          quadtree_find_rect_items(nodes[i],dXMin,dXMax,dYMin,dYMax,out);
	} 
      }
    }
}

/*
 * searches all four subnodes recursively for the closest item
 */
struct quadtree_item*
quadtree_find_nearest_flood(struct quadtree_node* this_, struct quadtree_item* item, double current_max, struct quadtree_node* toSkip) {
    struct quadtree_node* nodes[4] = { this_->aa, this_->ab, this_->ba, this_->bb };
    struct quadtree_item*res = NULL;
    if( this_->is_leaf ) { 
        int i;
        double distance_sq = current_max;
        for(i=0;i<this_->node_num;++i) {
            double curr_dist_sq = dist_sq(item->longitude,item->latitude,this_->items[i].longitude,this_->items[i].latitude);
            if(curr_dist_sq<distance_sq) {
                distance_sq = curr_dist_sq;
                res = &this_->items[i];
            }
        }
    }
    else {
      int i;
      for( i=0;i<4;++i) {
        if(nodes[i] && nodes[i]!=toSkip) {
          //limit flooding
          struct quadtree_item*res_tmp = NULL;
	  if(
             dist_sq(nodes[i]->xmin,nodes[i]->ymin,item->longitude,item->latitude)<current_max || 
	     dist_sq(nodes[i]->xmax,nodes[i]->ymin,item->longitude,item->latitude)<current_max ||
	     dist_sq(nodes[i]->xmax,nodes[i]->ymax,item->longitude,item->latitude)<current_max ||
	     dist_sq(nodes[i]->xmin,nodes[i]->ymax,item->longitude,item->latitude)<current_max 
            ) {
            res_tmp = quadtree_find_nearest_flood(nodes[i],item,current_max,NULL);
	  }
	  if(res_tmp) {
          double curr_dist_sq;
          res = res_tmp;
          curr_dist_sq = dist_sq(item->longitude,item->latitude,res->longitude,res->latitude);
            if(curr_dist_sq<current_max) {
                current_max = curr_dist_sq;
            }
 	  }
	} 
      }
    }
  return res;
}

/*
 * tries to find an item exactly
 */
struct quadtree_item*
quadtree_find_item(struct quadtree_node* this_, struct quadtree_item* item) {
    struct quadtree_item*res = NULL;
    if( ! this_ ) {
      return NULL;
    }
    if( this_->is_leaf ) { 
        int i;
        for(i=0;i<this_->node_num;++i) {
            //TODO equality check may not be correct on float values! maybe it can be replaced by range check with some tolerance
            if(item->longitude==this_->items[i].longitude && item->latitude==this_->items[i].latitude) {
                res = &this_->items[i];
                return res;
            }
        }
        return NULL;	
    }
    else {
        if(
	   this_->aa && 
           this_->aa->xmin<=item->longitude && item->longitude<this_->aa->xmax &&
           this_->aa->ymin<=item->latitude && item->latitude<this_->aa->ymax
           ) {
          res = quadtree_find_item(this_->aa,item);
        }
        else if(
	   this_->ab && 
           this_->ab->xmin<=item->longitude && item->longitude<this_->ab->xmax &&
           this_->ab->ymin<=item->latitude && item->latitude<this_->ab->ymax
           ) {
          res = quadtree_find_item(this_->ab,item);
        }
        else if(
	   this_->ba && 
           this_->ba->xmin<=item->longitude && item->longitude<this_->ba->xmax &&
           this_->ba->ymin<=item->latitude && item->latitude<this_->ba->ymax
           ) {
          res = quadtree_find_item(this_->ba,item);
        }
        else if(
	   this_->bb && 
           this_->bb->xmin<=item->longitude && item->longitude<this_->bb->xmax &&
           this_->bb->ymin<=item->latitude && item->latitude<this_->bb->ymax
           ) {
          res = quadtree_find_item(this_->bb,item);
        }
	else {
          return NULL;
        }
    }
  return res;
}


/*
 * tries to find closest item, first it descend into the quadtree as much as possible, then if no point is found go up n levels and flood
 */
struct quadtree_item*
quadtree_find_nearest(struct quadtree_node* this_, struct quadtree_item* item) {
    struct quadtree_item*res = NULL;
    double distance_sq = MAX_DOUBLE;
    if( ! this_ ) {
      return NULL;
    }
    if( this_->is_leaf ) { 
        int i;
        for(i=0;i<this_->node_num;++i) {
            double curr_dist_sq = dist_sq(item->longitude,item->latitude,this_->items[i].longitude,this_->items[i].latitude);
            if(curr_dist_sq<distance_sq) {
                distance_sq = curr_dist_sq;
                res = &this_->items[i];
            }
        }
      //go up n levels
	  if(!res && this_->parent) {
          struct quadtree_item*res2 = NULL;
	          struct quadtree_node* anchestor = this_->parent;
                  int cnt = 0;
        	  while (anchestor->parent && cnt<4) {
	            anchestor = anchestor->parent;
                    ++cnt;
		  }
	  	res2 = quadtree_find_nearest_flood(anchestor,item,distance_sq,NULL);
		if(res2) {
		  res = res2;
		}
	  }
    }
    else {
        if(
	   this_->aa && 
           this_->aa->xmin<=item->longitude && item->longitude<this_->aa->xmax &&
           this_->aa->ymin<=item->latitude && item->latitude<this_->aa->ymax
           ) {
          res = quadtree_find_nearest(this_->aa,item);
        }
        else if(
	   this_->ab && 
           this_->ab->xmin<=item->longitude && item->longitude<this_->ab->xmax &&
           this_->ab->ymin<=item->latitude && item->latitude<this_->ab->ymax
           ) {
          res = quadtree_find_nearest(this_->ab,item);
        }
        else if(
	   this_->ba && 
           this_->ba->xmin<=item->longitude && item->longitude<this_->ba->xmax &&
           this_->ba->ymin<=item->latitude && item->latitude<this_->ba->ymax
           ) {
          res = quadtree_find_nearest(this_->ba,item);
        }
        else if(
	   this_->bb && 
           this_->bb->xmin<=item->longitude && item->longitude<this_->bb->xmax &&
           this_->bb->ymin<=item->latitude && item->latitude<this_->bb->ymax
           ) {
          res = quadtree_find_nearest(this_->bb,item);
        }
	else {
        if(this_->parent) {
	    //go up two levels
	    struct quadtree_node* anchestor = this_->parent;
            int cnt = 0;
            while (anchestor->parent && cnt<4) {
	      anchestor = anchestor->parent;
              ++cnt;
	    }
        res = quadtree_find_nearest_flood(anchestor,item,distance_sq,NULL);
        }
      }
    }
  return res;
}

void
quadtree_add(struct quadtree_node* this_, struct quadtree_item* item) {
    if( this_->is_leaf ) {
        this_->items[this_->node_num++] = *item;
        if(QUADTREE_NODE_CAPACITY == this_->node_num) {
            quadtree_split(this_);
        }
    }
    else {
        if(
           this_->xmin<=item->longitude && item->longitude<this_->xmin+(this_->xmax-this_->xmin)/2.0 &&
           this_->ymin<=item->latitude && item->latitude<this_->ymin+(this_->ymax-this_->ymin)/2.0
           ) {
	    if(!this_->aa) {
              this_->aa = quadtree_node_new( this_, this_->xmin, this_->xmin+(this_->xmax-this_->xmin)/2.0 , this_->ymin, this_->ymin+(this_->ymax-this_->ymin)/2.0 );
	    }
          quadtree_add(this_->aa,item);
        }
        else if(
           this_->xmin+(this_->xmax-this_->xmin)/2.0<=item->longitude && item->longitude<this_->xmax &&
           this_->ymin<=item->latitude && item->latitude<this_->ymin+(this_->ymax-this_->ymin)/2.0
           ) {
	    if(!this_->ab) {
              this_->ab = quadtree_node_new( this_, this_->xmin+(this_->xmax-this_->xmin)/2.0, this_->xmax , this_->ymin, this_->ymin+(this_->ymax-this_->ymin)/2.0 );
	    }
          quadtree_add(this_->ab,item);
        }
        else if(
           this_->xmin<=item->longitude && item->longitude<this_->xmin+(this_->xmax-this_->xmin)/2.0 &&
           this_->ymin+(this_->ymax-this_->ymin)/2.0<=item->latitude && item->latitude<this_->ymax
           ) {
	    if(!this_->ba) {
              this_->ba = quadtree_node_new( this_, this_->xmin, this_->xmin+(this_->xmax-this_->xmin)/2.0 , this_->ymin+(this_->ymax-this_->ymin)/2.0 , this_->ymax);
	    }
          quadtree_add(this_->ba,item);
        }
        else if(
           this_->xmin+(this_->xmax-this_->xmin)/2.0<=item->longitude && item->longitude<this_->xmax &&
           this_->ymin+(this_->ymax-this_->ymin)/2.0<=item->latitude && item->latitude<this_->ymax
           ) {
	    if(!this_->bb) {
              this_->bb = quadtree_node_new( this_, this_->xmin+(this_->xmax-this_->xmin)/2.0, this_->xmax , this_->ymin+(this_->ymax-this_->ymin)/2.0 , this_->ymax);
	    }
          quadtree_add(this_->bb,item);
        }
    }
}

void
quadtree_split(struct quadtree_node* this_) {
    int i;
    this_->is_leaf = 0;
    for(i=0;i<this_->node_num;++i) {
        if(
           this_->xmin<=this_->items[i].longitude && this_->items[i].longitude<this_->xmin+(this_->xmax-this_->xmin)/2.0 &&
           this_->ymin<=this_->items[i].latitude && this_->items[i].latitude<this_->ymin+(this_->ymax-this_->ymin)/2.0
           ) {
	  if(!this_->aa) {
            this_->aa = quadtree_node_new( this_, this_->xmin, this_->xmin+(this_->xmax-this_->xmin)/2.0 , this_->ymin, this_->ymin+(this_->ymax-this_->ymin)/2.0 );
	  }
          quadtree_add(this_->aa,&this_->items[i]);
        }
        else if(
           this_->xmin+(this_->xmax-this_->xmin)/2.0<=this_->items[i].longitude && this_->items[i].longitude<this_->xmax &&
           this_->ymin<=this_->items[i].latitude && this_->items[i].latitude<this_->ymin+(this_->ymax-this_->ymin)/2.0
           ) {
	  if(!this_->ab) {
            this_->ab = quadtree_node_new( this_, this_->xmin+(this_->xmax-this_->xmin)/2.0, this_->xmax , this_->ymin, this_->ymin+(this_->ymax-this_->ymin)/2.0 );
	  }
          quadtree_add(this_->ab,&this_->items[i]);
        }
        else if(
           this_->xmin<=this_->items[i].longitude && this_->items[i].longitude<this_->xmin+(this_->xmax-this_->xmin)/2.0 &&
           this_->ymin+(this_->ymax-this_->ymin)/2.0<=this_->items[i].latitude && this_->items[i].latitude<this_->ymax
           ) {
	  if(!this_->ba) {
            this_->ba = quadtree_node_new( this_, this_->xmin, this_->xmin+(this_->xmax-this_->xmin)/2.0 , this_->ymin+(this_->ymax-this_->ymin)/2.0 , this_->ymax);
	  }
          quadtree_add(this_->ba,&this_->items[i]);
        }
        else if(
           this_->xmin+(this_->xmax-this_->xmin)/2.0<=this_->items[i].longitude && this_->items[i].longitude<this_->xmax &&
           this_->ymin+(this_->ymax-this_->ymin)/2.0<=this_->items[i].latitude && this_->items[i].latitude<this_->ymax
           ) {
	  if(!this_->bb) {
            this_->bb = quadtree_node_new( this_, this_->xmin+(this_->xmax-this_->xmin)/2.0, this_->xmax , this_->ymin+(this_->ymax-this_->ymin)/2.0 , this_->ymax);
	  }
          quadtree_add(this_->bb,&this_->items[i]);
        }
    }
    this_->node_num = 0;
}

void
quadtree_destroy(struct quadtree_node* this_) {
    if(this_->aa) {
        quadtree_destroy(this_->aa);
    }
    if(this_->ab) {
        quadtree_destroy(this_->ab);
    }
    if(this_->ba) {
        quadtree_destroy(this_->ba);
    }
    if(this_->bb) {
        quadtree_destroy(this_->bb);
    }
    free(this_);
}