2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * - Optimize for memory consumption
31 * - Profile for computation speed
32 * - Target polygon count/detail
35 /* For GPQueue comparators */
36 static gint tri_cmp(RoamTriangle *a, RoamTriangle *b, gpointer data)
38 if (a->error < b->error) return 1;
39 else if (a->error > b->error) return -1;
42 static gint dia_cmp(RoamDiamond *a, RoamDiamond *b, gpointer data)
44 if (a->error < b->error) return -1;
45 else if (a->error > b->error) return 1;
53 RoamPoint *roam_point_new(gdouble lat, gdouble lon, gdouble elev)
55 RoamPoint *point = g_new0(RoamPoint, 1);
59 /* For get_intersect */
60 lle2xyz(lat, lon, elev, &point->x, &point->y, &point->z);
63 RoamPoint *roam_point_dup(RoamPoint *point)
65 RoamPoint *new = g_memdup(point, sizeof(RoamPoint));
69 void roam_point_add_triangle(RoamPoint *point, RoamTriangle *triangle)
71 for (int i = 0; i < 3; i++) {
72 point->norm[i] *= point->tris;
73 point->norm[i] += triangle->norm[i];
76 for (int i = 0; i < 3; i++)
77 point->norm[i] /= point->tris;
79 void roam_point_remove_triangle(RoamPoint *point, RoamTriangle *triangle)
81 for (int i = 0; i < 3; i++) {
82 point->norm[i] *= point->tris;
83 point->norm[i] -= triangle->norm[i];
87 for (int i = 0; i < 3; i++)
88 point->norm[i] /= point->tris;
90 void roam_point_update_height(RoamPoint *point)
92 if (point->height_func) {
93 gdouble elev = point->height_func(
94 point->lat, point->lon, point->height_data);
95 lle2xyz(point->lat, point->lon, elev,
96 &point->x, &point->y, &point->z);
99 void roam_point_update_projection(RoamPoint *point, RoamSphere *sphere)
101 static int count = 0;
102 static int version = 0;
103 if (version != sphere->view->version) {
104 g_debug("RoamPoint: Projected %d points", count);
106 version = sphere->view->version;
109 if (point->pversion != sphere->view->version) {
110 /* Cache projection */
111 gluProject(point->x, point->y, point->z,
112 sphere->view->model, sphere->view->proj, sphere->view->view,
113 &point->px, &point->py, &point->pz);
114 point->pversion = sphere->view->version;
122 RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r)
124 RoamTriangle *triangle = g_new0(RoamTriangle, 1);
130 triangle->split = roam_point_new(
132 (ABS(l->lat) == 90 ? r->lon :
133 ABS(r->lat) == 90 ? l->lon :
134 lon_avg(l->lon, r->lon)),
135 (l->elev + r->elev)/2);
136 /* TODO: Move this back to sphere, or actually use the nesting */
137 triangle->split->height_func = m->height_func;
138 triangle->split->height_data = m->height_data;
139 roam_point_update_height(triangle->split);
140 //if ((float)triangle->split->lat > 44 && (float)triangle->split->lat < 46)
141 // g_debug("RoamTriangle: new - (l,m,r,split).lats = %7.2f %7.2f %7.2f %7.2f",
142 // l->lat, m->lat, r->lat, triangle->split->lat);
143 //if ((float)l->lat == (float)r->lat &&
144 // (float)triangle->split->lat != (float)l->lat)
145 // g_warning("RoamTriangle: new - split.lat=%f != (l=r).lat=%f",
146 // triangle->split->lat, l->lat);
151 pa[0] = triangle->p.l->x - triangle->p.m->x;
152 pa[1] = triangle->p.l->y - triangle->p.m->y;
153 pa[2] = triangle->p.l->z - triangle->p.m->z;
155 pb[0] = triangle->p.r->x - triangle->p.m->x;
156 pb[1] = triangle->p.r->y - triangle->p.m->y;
157 pb[2] = triangle->p.r->z - triangle->p.m->z;
159 triangle->norm[0] = pa[1] * pb[2] - pa[2] * pb[1];
160 triangle->norm[1] = pa[2] * pb[0] - pa[0] * pb[2];
161 triangle->norm[2] = pa[0] * pb[1] - pa[1] * pb[0];
163 double total = sqrt(triangle->norm[0] * triangle->norm[0] +
164 triangle->norm[1] * triangle->norm[1] +
165 triangle->norm[2] * triangle->norm[2]);
167 triangle->norm[0] /= total;
168 triangle->norm[1] /= total;
169 triangle->norm[2] /= total;
171 /* Store bounding box, for get_intersect */
172 RoamPoint *p[] = {l,m,r};
173 triangle->edge.n = -90; triangle->edge.s = 90;
174 triangle->edge.e = -180; triangle->edge.w = 180;
175 gboolean maxed = FALSE;
176 for (int i = 0; i < G_N_ELEMENTS(p); i++) {
177 triangle->edge.n = MAX(triangle->edge.n, p[i]->lat);
178 triangle->edge.s = MIN(triangle->edge.s, p[i]->lat);
179 if (p[i]->lat == 90 || p[i]->lat == -90)
181 if (p[i]->lon == 180) {
185 triangle->edge.e = MAX(triangle->edge.e, p[i]->lon);
186 triangle->edge.w = MIN(triangle->edge.w, p[i]->lon);
189 if (triangle->edge.e < 0)
190 triangle->edge.w = -180;
192 triangle->edge.e = 180;
195 //g_message("roam_triangle_new: %p", triangle);
199 void roam_triangle_free(RoamTriangle *triangle)
201 g_free(triangle->split);
205 void roam_triangle_add(RoamTriangle *triangle,
206 RoamTriangle *left, RoamTriangle *base, RoamTriangle *right,
209 triangle->t.l = left;
210 triangle->t.b = base;
211 triangle->t.r = right;
213 roam_point_add_triangle(triangle->p.l, triangle);
214 roam_point_add_triangle(triangle->p.m, triangle);
215 roam_point_add_triangle(triangle->p.r, triangle);
218 roam_triangle_update_errors(triangle, sphere);
220 triangle->handle = g_pqueue_push(sphere->triangles, triangle);
223 void roam_triangle_remove(RoamTriangle *triangle, RoamSphere *sphere)
225 /* Update vertex normals */
226 roam_point_remove_triangle(triangle->p.l, triangle);
227 roam_point_remove_triangle(triangle->p.m, triangle);
228 roam_point_remove_triangle(triangle->p.r, triangle);
230 g_pqueue_remove(sphere->triangles, triangle->handle);
233 void roam_triangle_sync_neighbors(RoamTriangle *new, RoamTriangle *old, RoamTriangle *neigh)
235 if (neigh->t.l == old) neigh->t.l = new;
236 else if (neigh->t.b == old) neigh->t.b = new;
237 else if (neigh->t.r == old) neigh->t.r = new;
238 else g_assert_not_reached();
241 gboolean roam_point_visible(RoamPoint *triangle, RoamSphere *sphere)
243 gint *view = sphere->view->view;
244 return triangle->px > view[0] && triangle->px < view[2] &&
245 triangle->py > view[1] && triangle->py < view[3] &&
246 triangle->pz > 0 && triangle->pz < 1;
248 gboolean roam_triangle_visible(RoamTriangle *triangle, RoamSphere *sphere)
250 /* Do this with a bounding box */
251 return roam_point_visible(triangle->p.l, sphere) ||
252 roam_point_visible(triangle->p.m, sphere) ||
253 roam_point_visible(triangle->p.r, sphere);
256 void roam_triangle_update_errors(RoamTriangle *triangle, RoamSphere *sphere)
259 roam_point_update_projection(triangle->p.l, sphere);
260 roam_point_update_projection(triangle->p.m, sphere);
261 roam_point_update_projection(triangle->p.r, sphere);
263 /* Not exactly correct, could be out on both sides (middle in) */
264 if (!roam_triangle_visible(triangle, sphere)) {
265 triangle->error = -1;
267 roam_point_update_projection(triangle->split, sphere);
268 RoamPoint *l = triangle->p.l;
269 RoamPoint *m = triangle->p.m;
270 RoamPoint *r = triangle->p.r;
271 RoamPoint *split = triangle->split;
273 /* l-r midpoint projected l-r midpoint */
274 gdouble pxdist = (l->px + r->px)/2 - split->px;
275 gdouble pydist = (l->py + r->py)/2 - split->py;
277 triangle->error = sqrt(pxdist*pxdist + pydist*pydist);
279 /* Multiply by size of triangle */
280 double size = -( l->px * (m->py - r->py) +
281 m->px * (r->py - l->py) +
282 r->px * (l->py - m->py) ) / 2.0;
284 /* Size < 0 == backface */
286 // triangle->error *= -1;
287 triangle->error *= size;
291 void roam_triangle_split(RoamTriangle *triangle, RoamSphere *sphere)
293 //g_message("roam_triangle_split: %p, e=%f\n", triangle, triangle->error);
297 if (triangle != triangle->t.b->t.b)
298 roam_triangle_split(triangle->t.b, sphere);
299 if (triangle != triangle->t.b->t.b)
300 g_assert_not_reached();
302 RoamTriangle *base = triangle->t.b;
304 /* Add new triangles */
305 RoamPoint *mid = triangle->split;
306 RoamTriangle *sl = triangle->kids[0] = roam_triangle_new(triangle->p.m, mid, triangle->p.l); // triangle Left
307 RoamTriangle *sr = triangle->kids[1] = roam_triangle_new(triangle->p.r, mid, triangle->p.m); // triangle Right
308 RoamTriangle *bl = base->kids[0] = roam_triangle_new(base->p.m, mid, base->p.l); // Base Left
309 RoamTriangle *br = base->kids[1] = roam_triangle_new(base->p.r, mid, base->p.m); // Base Right
311 /* triangle,l, base, r, sphere */
312 roam_triangle_add(sl, sr, triangle->t.l, br, sphere);
313 roam_triangle_add(sr, bl, triangle->t.r, sl, sphere);
314 roam_triangle_add(bl, br, base->t.l, sr, sphere);
315 roam_triangle_add(br, sl, base->t.r, bl, sphere);
317 roam_triangle_sync_neighbors(sl, triangle, triangle->t.l);
318 roam_triangle_sync_neighbors(sr, triangle, triangle->t.r);
319 roam_triangle_sync_neighbors(bl, base, base->t.l);
320 roam_triangle_sync_neighbors(br, base, base->t.r);
322 /* Remove old triangles */
323 roam_triangle_remove(triangle, sphere);
324 roam_triangle_remove(base, sphere);
326 /* Add/Remove diamonds */
327 RoamDiamond *diamond = roam_diamond_new(triangle, base, sl, sr, bl, br);
328 roam_diamond_update_errors(diamond, sphere);
329 roam_diamond_add(diamond, sphere);
330 roam_diamond_remove(triangle->parent, sphere);
331 roam_diamond_remove(base->parent, sphere);
334 void roam_triangle_draw(RoamTriangle *triangle)
336 glBegin(GL_TRIANGLES);
337 glNormal3dv(triangle->p.r->norm); glVertex3dv((double*)triangle->p.r);
338 glNormal3dv(triangle->p.m->norm); glVertex3dv((double*)triangle->p.m);
339 glNormal3dv(triangle->p.l->norm); glVertex3dv((double*)triangle->p.l);
344 void roam_triangle_draw_normal(RoamTriangle *triangle)
347 (triangle->p.l->x + triangle->p.m->x + triangle->p.r->x)/3.0,
348 (triangle->p.l->y + triangle->p.m->y + triangle->p.r->y)/3.0,
349 (triangle->p.l->z + triangle->p.m->z + triangle->p.r->z)/3.0,
352 center[0]+triangle->norm[0]*2000000,
353 center[1]+triangle->norm[1]*2000000,
354 center[2]+triangle->norm[2]*2000000,
365 RoamDiamond *roam_diamond_new(
366 RoamTriangle *parent0, RoamTriangle *parent1,
367 RoamTriangle *kid0, RoamTriangle *kid1,
368 RoamTriangle *kid2, RoamTriangle *kid3)
370 RoamDiamond *diamond = g_new0(RoamDiamond, 1);
372 diamond->kids[0] = kid0;
373 diamond->kids[1] = kid1;
374 diamond->kids[2] = kid2;
375 diamond->kids[3] = kid3;
377 kid0->parent = diamond;
378 kid1->parent = diamond;
379 kid2->parent = diamond;
380 kid3->parent = diamond;
382 diamond->parents[0] = parent0;
383 diamond->parents[1] = parent1;
387 void roam_diamond_add(RoamDiamond *diamond, RoamSphere *sphere)
389 diamond->active = TRUE;
390 diamond->error = MAX(diamond->parents[0]->error, diamond->parents[1]->error);
391 diamond->handle = g_pqueue_push(sphere->diamonds, diamond);
393 void roam_diamond_remove(RoamDiamond *diamond, RoamSphere *sphere)
395 if (diamond && diamond->active) {
396 diamond->active = FALSE;
397 g_pqueue_remove(sphere->diamonds, diamond->handle);
400 void roam_diamond_merge(RoamDiamond *diamond, RoamSphere *sphere)
402 //g_message("roam_diamond_merge: %p, e=%f\n", diamond, diamond->error);
406 /* TODO: pick the best split */
407 RoamTriangle **kids = diamond->kids;
409 /* Create triangles */
410 RoamTriangle *triangle = diamond->parents[0];
411 RoamTriangle *base = diamond->parents[1];
413 roam_triangle_add(triangle, kids[0]->t.b, base, kids[1]->t.b, sphere);
414 roam_triangle_add(base, kids[2]->t.b, triangle, kids[3]->t.b, sphere);
416 roam_triangle_sync_neighbors(triangle, kids[0], kids[0]->t.b);
417 roam_triangle_sync_neighbors(triangle, kids[1], kids[1]->t.b);
418 roam_triangle_sync_neighbors(base, kids[2], kids[2]->t.b);
419 roam_triangle_sync_neighbors(base, kids[3], kids[3]->t.b);
421 /* Remove triangles */
422 roam_triangle_remove(kids[0], sphere);
423 roam_triangle_remove(kids[1], sphere);
424 roam_triangle_remove(kids[2], sphere);
425 roam_triangle_remove(kids[3], sphere);
428 triangle->kids[0] = triangle->kids[1] = NULL;
429 base->kids[0] = base->kids[1] = NULL;
431 /* Add/Remove triangles */
432 if (triangle->t.l->t.l == triangle->t.r->t.r &&
433 triangle->t.l->t.l != triangle && triangle->parent) {
434 roam_diamond_update_errors(triangle->parent, sphere);
435 roam_diamond_add(triangle->parent, sphere);
438 if (base->t.l->t.l == base->t.r->t.r &&
439 base->t.l->t.l != base && base->parent) {
440 roam_diamond_update_errors(base->parent, sphere);
441 roam_diamond_add(base->parent, sphere);
444 /* Remove and free diamond and child triangles */
445 roam_diamond_remove(diamond, sphere);
446 g_assert(diamond->kids[0]->p.m == diamond->kids[1]->p.m &&
447 diamond->kids[1]->p.m == diamond->kids[2]->p.m &&
448 diamond->kids[2]->p.m == diamond->kids[3]->p.m);
449 g_assert(diamond->kids[0]->p.m->tris == 0);
450 roam_triangle_free(diamond->kids[0]);
451 roam_triangle_free(diamond->kids[1]);
452 roam_triangle_free(diamond->kids[2]);
453 roam_triangle_free(diamond->kids[3]);
456 void roam_diamond_update_errors(RoamDiamond *diamond, RoamSphere *sphere)
458 roam_triangle_update_errors(diamond->parents[0], sphere);
459 roam_triangle_update_errors(diamond->parents[1], sphere);
460 diamond->error = MAX(diamond->parents[0]->error, diamond->parents[1]->error);
466 RoamSphere *roam_sphere_new()
468 RoamSphere *sphere = g_new0(RoamSphere, 1);
470 sphere->triangles = g_pqueue_new((GCompareDataFunc)tri_cmp, NULL);
471 sphere->diamonds = g_pqueue_new((GCompareDataFunc)dia_cmp, NULL);
473 RoamPoint *vertexes[] = {
474 roam_point_new( 90, 0, 0), // 0 (North)
475 roam_point_new(-90, 0, 0), // 1 (South)
476 roam_point_new( 0, 0, 0), // 2 (Europe/Africa)
477 roam_point_new( 0, 90, 0), // 3 (Asia,East)
478 roam_point_new( 0, 180, 0), // 4 (Pacific)
479 roam_point_new( 0, -90, 0), // 5 (Americas,West)
481 int _triangles[][2][3] = {
482 /*lv mv rv ln, bn, rn */
483 {{2,0,3}, {3, 4, 1}}, // 0
484 {{3,0,4}, {0, 5, 2}}, // 1
485 {{4,0,5}, {1, 6, 3}}, // 2
486 {{5,0,2}, {2, 7, 0}}, // 3
487 {{3,1,2}, {5, 0, 7}}, // 4
488 {{4,1,3}, {6, 1, 4}}, // 5
489 {{5,1,4}, {7, 2, 5}}, // 6
490 {{2,1,5}, {4, 3, 6}}, // 7
493 for (int i = 0; i < 6; i++)
494 roam_point_update_height(vertexes[i]);
495 for (int i = 0; i < 8; i++)
496 sphere->roots[i] = roam_triangle_new(
497 vertexes[_triangles[i][0][0]],
498 vertexes[_triangles[i][0][1]],
499 vertexes[_triangles[i][0][2]]);
500 for (int i = 0; i < 8; i++)
501 roam_triangle_add(sphere->roots[i],
502 sphere->roots[_triangles[i][1][0]],
503 sphere->roots[_triangles[i][1][1]],
504 sphere->roots[_triangles[i][1][2]],
506 for (int i = 0; i < 8; i++)
507 g_debug("RoamSphere: new - %p edge=%f,%f,%f,%f", sphere->roots[i],
508 sphere->roots[i]->edge.n,
509 sphere->roots[i]->edge.s,
510 sphere->roots[i]->edge.e,
511 sphere->roots[i]->edge.w);
515 void roam_sphere_update_view(RoamSphere *sphere)
518 sphere->view = g_new0(RoamView, 1);
519 glGetDoublev (GL_MODELVIEW_MATRIX, sphere->view->model);
520 glGetDoublev (GL_PROJECTION_MATRIX, sphere->view->proj);
521 glGetIntegerv(GL_VIEWPORT, sphere->view->view);
522 sphere->view->version++;
524 void roam_sphere_update_errors(RoamSphere *sphere)
526 g_debug("RoamSphere: update_errors - polys=%d", sphere->polys);
527 GPtrArray *tris = g_pqueue_get_array(sphere->triangles);
528 GPtrArray *dias = g_pqueue_get_array(sphere->diamonds);
530 roam_sphere_update_view(sphere);
532 for (int i = 0; i < tris->len; i++) {
533 RoamTriangle *triangle = tris->pdata[i];
534 roam_triangle_update_errors(triangle, sphere);
535 g_pqueue_priority_changed(sphere->triangles, triangle->handle);
538 for (int i = 0; i < dias->len; i++) {
539 RoamDiamond *diamond = dias->pdata[i];
540 roam_diamond_update_errors(diamond, sphere);
541 g_pqueue_priority_changed(sphere->diamonds, diamond->handle);
544 g_ptr_array_free(tris, TRUE);
545 g_ptr_array_free(dias, TRUE);
548 void roam_sphere_split_one(RoamSphere *sphere)
550 RoamTriangle *to_split = g_pqueue_peek(sphere->triangles);
551 if (!to_split) return;
552 roam_triangle_split(to_split, sphere);
554 void roam_sphere_merge_one(RoamSphere *sphere)
556 RoamDiamond *to_merge = g_pqueue_peek(sphere->diamonds);
557 if (!to_merge) return;
558 roam_diamond_merge(to_merge, sphere);
560 gint roam_sphere_split_merge(RoamSphere *sphere)
562 gint iters = 0, max_iters = 500;
563 //gint target = 4000;
570 if (target - sphere->polys > 100) {
571 //g_debug("RoamSphere: split_merge - Splitting %d - %d > 100", target, sphere->polys);
572 while (sphere->polys < target && iters++ < max_iters)
573 roam_sphere_split_one(sphere);
576 if (sphere->polys - target > 100) {
577 //g_debug("RoamSphere: split_merge - Merging %d - %d > 100", sphere->polys, target);
578 while (sphere->polys > target && iters++ < max_iters)
579 roam_sphere_merge_one(sphere);
582 while (((RoamTriangle*)g_pqueue_peek(sphere->triangles))->error >
583 ((RoamDiamond *)g_pqueue_peek(sphere->diamonds ))->error &&
584 iters++ < max_iters) {
585 //g_debug("RoamSphere: split_merge - Fixing 1 %f > %f && %d < %d",
586 // ((RoamTriangle*)g_pqueue_peek(sphere->triangles))->error,
587 // ((RoamDiamond *)g_pqueue_peek(sphere->diamonds ))->error,
588 // iters-1, max_iters);
589 roam_sphere_merge_one(sphere);
590 roam_sphere_split_one(sphere);
595 void roam_sphere_draw(RoamSphere *sphere)
597 g_debug("RoamSphere: draw");
598 g_pqueue_foreach(sphere->triangles, (GFunc)roam_triangle_draw, NULL);
600 void roam_sphere_draw_normals(RoamSphere *sphere)
602 g_debug("RoamSphere: draw_normal");
603 g_pqueue_foreach(sphere->triangles, (GFunc)roam_triangle_draw_normal, NULL);
605 static GList *_roam_sphere_get_leaves(RoamTriangle *triangle, GList *list, gboolean all)
607 if (triangle->kids[0] && triangle->kids[1]) {
608 if (all) list = g_list_prepend(list, triangle);
609 list = _roam_sphere_get_leaves(triangle->kids[0], list, all);
610 list = _roam_sphere_get_leaves(triangle->kids[1], list, all);
613 return g_list_prepend(list, triangle);
616 static GList *_roam_sphere_get_intersect_rec(RoamTriangle *triangle, GList *list,
617 gboolean all, gdouble n, gdouble s, gdouble e, gdouble w)
619 gdouble tn = triangle->edge.n;
620 gdouble ts = triangle->edge.s;
621 gdouble te = triangle->edge.e;
622 gdouble tw = triangle->edge.w;
624 gboolean debug = FALSE &&
625 n==90 && s==45 && e==-90 && w==-180 &&
629 g_message("t=%p: %f < %f || %f > %f || %f < %f || %f > %f",
630 triangle, tn, s, ts, n, te, w, tw, e);
631 if (tn < s || ts > n || te < w || tw > e) {
633 if (debug) g_message("no intersect");
635 } else if (tn <= n && ts >= s && te <= e && tw >= w) {
636 /* Triangle is completely contained */
637 if (debug) g_message("contained");
638 if (all) list = g_list_prepend(list, triangle);
639 return _roam_sphere_get_leaves(triangle, list, all);
640 } else if (triangle->kids[0] && triangle->kids[1]) {
641 /* Paritial intersect with children */
642 if (debug) g_message("edge w/ child");
643 if (all) list = g_list_prepend(list, triangle);
644 list = _roam_sphere_get_intersect_rec(triangle->kids[0], list, all, n, s, e, w);
645 list = _roam_sphere_get_intersect_rec(triangle->kids[1], list, all, n, s, e, w);
648 /* This triangle is an edge case */
649 if (debug) g_message("edge");
650 return g_list_prepend(list, triangle);
653 /* Warning: This grabs pointers to triangles which can be changed by other
654 * calls, e.g. split_merge. If you use this, you need to do some locking to
655 * prevent the returned list from becomming stale. */
656 GList *roam_sphere_get_intersect(RoamSphere *sphere, gboolean all,
657 gdouble n, gdouble s, gdouble e, gdouble w)
659 /* I think this is correct..
660 * i_cost = cost for triangle-rectagnle intersect test
661 * time = n_tiles * 2*tris_per_tile * i_cost
662 * time = 30 * 2*333 * i_cost = 20000 * i_cost */
664 for (int i = 0; i < G_N_ELEMENTS(sphere->roots); i++)
665 list = _roam_sphere_get_intersect_rec(sphere->roots[i],
666 list, all, n, s, e, w);
669 void roam_sphere_free_tri(RoamTriangle *triangle)
671 if (--triangle->p.l->tris == 0) g_free(triangle->p.l);
672 if (--triangle->p.m->tris == 0) g_free(triangle->p.m);
673 if (--triangle->p.r->tris == 0) g_free(triangle->p.r);
674 roam_triangle_free(triangle);
676 void roam_sphere_free(RoamSphere *sphere)
678 g_debug("RoamSphere: free");
679 /* Slow method, but it should work */
680 while (sphere->polys > 8)
681 roam_sphere_merge_one(sphere);
682 /* TODO: free points */
683 g_pqueue_foreach(sphere->triangles, (GFunc)roam_sphere_free_tri, NULL);
684 g_pqueue_free(sphere->triangles);
685 g_pqueue_free(sphere->diamonds);
686 g_free(sphere->view);