X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Froam.c;h=0f4995e0b56593d7bf3dc03f4d65015afb847e8e;hp=4bc294c08097c644ab0fa771e9e25c0b60c13218;hb=22408daaad6f0c7b4075c2b0a725802b0659cfbe;hpb=a2a902d978b6050289f7a92794fb1dc0aa2b5e26 diff --git a/src/roam.c b/src/roam.c index 4bc294c..0f4995e 100644 --- a/src/roam.c +++ b/src/roam.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Andy Spencer + * Copyright (C) 2009-2010 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,17 +15,30 @@ * along with this program. If not, see . */ +/** + * SECTION:roam + * @short_description: Realtime Optimally-Adapting Meshes + * + * A spherical version of the Realtime Optimally-Adapting Meshes (ROAM) + * algorithm is use for drawing the surface of the planet. ROAM provide a + * continuous level-of-detail mesh of the planet which is used by #GisOpenGL + * when drawing surface textures for GisTiles. + * + * This implementation of the ROAM algorithm is based on an octahedron as the + * base model. + */ + #include #include #include -#include "gpqueue.h" #include #include +#include "gpqueue.h" #include "gis-util.h" #include "roam.h" -/** +/* * TODO: * - Optimize for memory consumption * - Profile for computation speed @@ -46,78 +59,107 @@ static gint dia_cmp(RoamDiamond *a, RoamDiamond *b, gpointer data) else return 0; } + /************* * RoamPoint * *************/ -RoamPoint *roam_point_new(gdouble x, gdouble y, gdouble z) +/** + * roam_point_new: + * @lat: the latitude for the point + * @lon: the longitude for the point + * @elev: the elevation for the point + * + * Create a new point at the given locaiton + * + * Returns: the new point + */ +RoamPoint *roam_point_new(gdouble lat, gdouble lon, gdouble elev) { - RoamPoint *self = g_new0(RoamPoint, 1); - self->x = x; - self->y = y; - self->z = z; + RoamPoint *point = g_new0(RoamPoint, 1); + point->lat = lat; + point->lon = lon; + point->elev = elev; /* For get_intersect */ - xyz2ll(x, y, z, &self->lat, &self->lon); - return self; -} -RoamPoint *roam_point_dup(RoamPoint *self) -{ - RoamPoint *new = g_memdup(self, sizeof(RoamPoint)); - new->tris = 0; - return new; + lle2xyz(lat, lon, elev, &point->x, &point->y, &point->z); + return point; } -void roam_point_add_triangle(RoamPoint *self, RoamTriangle *triangle) + +/** + * roam_point_add_triangle: + * @point: the point + * @triangle: the to add + * + * Associating a triangle with a point and update it's vertex normal. + */ +void roam_point_add_triangle(RoamPoint *point, RoamTriangle *triangle) { for (int i = 0; i < 3; i++) { - self->norm[i] *= self->tris; - self->norm[i] += triangle->norm[i]; + point->norm[i] *= point->tris; + point->norm[i] += triangle->norm[i]; } - self->tris++; + point->tris++; for (int i = 0; i < 3; i++) - self->norm[i] /= self->tris; + point->norm[i] /= point->tris; } -void roam_point_remove_triangle(RoamPoint *self, RoamTriangle *triangle) + +/** + * roam_point_remove_triangle: + * @point: the point + * @triangle: the to add + * + * Un-associating a triangle with a point and update it's vertex normal. + */ +void roam_point_remove_triangle(RoamPoint *point, RoamTriangle *triangle) { for (int i = 0; i < 3; i++) { - self->norm[i] *= self->tris; - self->norm[i] -= triangle->norm[i]; + point->norm[i] *= point->tris; + point->norm[i] -= triangle->norm[i]; } - self->tris--; - if (self->tris) + point->tris--; + if (point->tris) for (int i = 0; i < 3; i++) - self->norm[i] /= self->tris; + point->norm[i] /= point->tris; } -void roam_point_update_height(RoamPoint *self) + +/** + * roam_point_update_height: + * @point: the point + * + * Update the height (elevation) of a point based on the current height function + */ +void roam_point_update_height(RoamPoint *point) { - if (self->height_func) { - gdouble elev = self->height_func( - self->lat, self->lon, self->height_data); - lle2xyz(self->lat, self->lon, elev, - &self->x, &self->y, &self->z); - } else { - gdouble dist = sqrt(self->x * self->x + - self->y * self->y + - self->z * self->z); - self->x = self->x/dist * EARTH_R; - self->y = self->y/dist * EARTH_R; - self->z = self->z/dist * EARTH_R; + if (point->height_func) { + gdouble elev = point->height_func( + point->lat, point->lon, point->height_data); + lle2xyz(point->lat, point->lon, elev, + &point->x, &point->y, &point->z); } } -void roam_point_update_projection(RoamPoint *self, RoamSphere *sphere) + +/** + * roam_point_update_projection: + * @point: the point + * @view: the view to use when projecting the point + * + * Updated the screen-space projection of a point. + */ +void roam_point_update_projection(RoamPoint *point, RoamView *view) { static int count = 0; static int version = 0; - if (version != sphere->view->version) { - g_debug("Projected %d points", count); + if (version != view->version) { + g_debug("RoamPoint: Projected %d points", count); count = 0; - version = sphere->view->version; + version = view->version; } - if (self->pversion != sphere->view->version) { + if (point->pversion != view->version) { /* Cache projection */ - gluProject(self->x, self->y, self->z, - sphere->view->model, sphere->view->proj, sphere->view->view, - &self->px, &self->py, &self->pz); - self->pversion = sphere->view->version; + gluProject(point->x, point->y, point->z, + view->model, view->proj, view->view, + &point->px, &point->py, &point->pz); + point->pversion = view->version; count++; } } @@ -125,166 +167,219 @@ void roam_point_update_projection(RoamPoint *self, RoamSphere *sphere) /**************** * RoamTriangle * ****************/ -RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r) -{ - RoamTriangle *self = g_new0(RoamTriangle, 1); - - self->error = 0; - self->p.l = l; - self->p.m = m; - self->p.r = r; - self->split = roam_point_new( - (l->x + r->x)/2, - (l->y + r->y)/2, - (l->z + r->z)/2); +/** + * roam_triangle_new: + * @l: the left point + * @m: the middle point + * @r: the right point + * + * Create a new triangle consisting of three points. + * + * Returns: the new triangle + */ +RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r, + RoamDiamond *parent) +{ + RoamTriangle *triangle = g_new0(RoamTriangle, 1); + + triangle->error = 0; + triangle->p.l = l; + triangle->p.m = m; + triangle->p.r = r; + triangle->parent = parent; + triangle->split = roam_point_new( + (l->lat + r->lat)/2, + (ABS(l->lat) == 90 ? r->lon : + ABS(r->lat) == 90 ? l->lon : + lon_avg(l->lon, r->lon)), + (l->elev + r->elev)/2); /* TODO: Move this back to sphere, or actually use the nesting */ - self->split->height_func = m->height_func; - self->split->height_data = m->height_data; - roam_point_update_height(self->split); - //if ((float)self->split->lat > 44 && (float)self->split->lat < 46) + triangle->split->height_func = m->height_func; + triangle->split->height_data = m->height_data; + roam_point_update_height(triangle->split); + //if ((float)triangle->split->lat > 44 && (float)triangle->split->lat < 46) // g_debug("RoamTriangle: new - (l,m,r,split).lats = %7.2f %7.2f %7.2f %7.2f", - // l->lat, m->lat, r->lat, self->split->lat); + // l->lat, m->lat, r->lat, triangle->split->lat); //if ((float)l->lat == (float)r->lat && - // (float)self->split->lat != (float)l->lat) + // (float)triangle->split->lat != (float)l->lat) // g_warning("RoamTriangle: new - split.lat=%f != (l=r).lat=%f", - // self->split->lat, l->lat); + // triangle->split->lat, l->lat); /* Update normal */ double pa[3]; double pb[3]; - pa[0] = self->p.l->x - self->p.m->x; - pa[1] = self->p.l->y - self->p.m->y; - pa[2] = self->p.l->z - self->p.m->z; + pa[0] = triangle->p.l->x - triangle->p.m->x; + pa[1] = triangle->p.l->y - triangle->p.m->y; + pa[2] = triangle->p.l->z - triangle->p.m->z; - pb[0] = self->p.r->x - self->p.m->x; - pb[1] = self->p.r->y - self->p.m->y; - pb[2] = self->p.r->z - self->p.m->z; + pb[0] = triangle->p.r->x - triangle->p.m->x; + pb[1] = triangle->p.r->y - triangle->p.m->y; + pb[2] = triangle->p.r->z - triangle->p.m->z; - self->norm[0] = pa[1] * pb[2] - pa[2] * pb[1]; - self->norm[1] = pa[2] * pb[0] - pa[0] * pb[2]; - self->norm[2] = pa[0] * pb[1] - pa[1] * pb[0]; + triangle->norm[0] = pa[1] * pb[2] - pa[2] * pb[1]; + triangle->norm[1] = pa[2] * pb[0] - pa[0] * pb[2]; + triangle->norm[2] = pa[0] * pb[1] - pa[1] * pb[0]; - double total = sqrt(self->norm[0] * self->norm[0] + - self->norm[1] * self->norm[1] + - self->norm[2] * self->norm[2]); + double total = sqrt(triangle->norm[0] * triangle->norm[0] + + triangle->norm[1] * triangle->norm[1] + + triangle->norm[2] * triangle->norm[2]); - self->norm[0] /= total; - self->norm[1] /= total; - self->norm[2] /= total; + triangle->norm[0] /= total; + triangle->norm[1] /= total; + triangle->norm[2] /= total; /* Store bounding box, for get_intersect */ - RoamPoint *m1 = roam_point_new((l->x+m->x)/2, (l->y+m->y)/2, (l->z+m->z)/2); - RoamPoint *m2 = roam_point_new((m->x+r->x)/2, (m->y+r->y)/2, (m->z+r->z)/2); - RoamPoint *m3 = roam_point_new((r->x+l->x)/2, (r->y+l->y)/2, (r->z+l->z)/2); - RoamPoint *p[] = {l,m,r,m1,m2,m3}; - self->edge.n = -90; self->edge.s = 90; - self->edge.e = -180; self->edge.w = 180; + RoamPoint *p[] = {l,m,r}; + triangle->edge.n = -90; triangle->edge.s = 90; + triangle->edge.e = -180; triangle->edge.w = 180; gboolean maxed = FALSE; for (int i = 0; i < G_N_ELEMENTS(p); i++) { - self->edge.n = MAX(self->edge.n, p[i]->lat); - self->edge.s = MIN(self->edge.s, p[i]->lat); + triangle->edge.n = MAX(triangle->edge.n, p[i]->lat); + triangle->edge.s = MIN(triangle->edge.s, p[i]->lat); if (p[i]->lat == 90 || p[i]->lat == -90) continue; if (p[i]->lon == 180) { maxed = TRUE; continue; } - self->edge.e = MAX(self->edge.e, p[i]->lon); - self->edge.w = MIN(self->edge.w, p[i]->lon); + triangle->edge.e = MAX(triangle->edge.e, p[i]->lon); + triangle->edge.w = MIN(triangle->edge.w, p[i]->lon); } if (maxed) { - if (self->edge.e < 0) - self->edge.w = -180; + if (triangle->edge.e < 0) + triangle->edge.w = -180; else - self->edge.e = 180; + triangle->edge.e = 180; } - g_free(m1); - g_free(m2); - g_free(m3); - //g_message("roam_triangle_new: %p", self); - return self; + //g_message("roam_triangle_new: %p", triangle); + return triangle; } -void roam_triangle_free(RoamTriangle *self) +/** + * roam_triangle_free: + * @triangle: the triangle + * + * Free data associated with a triangle + */ +void roam_triangle_free(RoamTriangle *triangle) { - g_free(self->split); - g_free(self); + g_free(triangle->split); + g_free(triangle); } -void roam_triangle_add(RoamTriangle *self, +/** + * roam_triangle_add: + * @triangle: the triangle + * @left: the left neighbor + * @base: the base neighbor + * @right: the right neighbor + * @sphere: the sphere to add the triangle to + * + * Add a triangle into the sphere's mesh using the given neighbors. + */ +void roam_triangle_add(RoamTriangle *triangle, RoamTriangle *left, RoamTriangle *base, RoamTriangle *right, RoamSphere *sphere) { - self->t.l = left; - self->t.b = base; - self->t.r = right; + triangle->t.l = left; + triangle->t.b = base; + triangle->t.r = right; - roam_point_add_triangle(self->p.l, self); - roam_point_add_triangle(self->p.m, self); - roam_point_add_triangle(self->p.r, self); + roam_point_add_triangle(triangle->p.l, triangle); + roam_point_add_triangle(triangle->p.m, triangle); + roam_point_add_triangle(triangle->p.r, triangle); if (sphere->view) - roam_triangle_update_errors(self, sphere); + roam_triangle_update_errors(triangle, sphere); - self->handle = g_pqueue_push(sphere->triangles, self); + triangle->handle = g_pqueue_push(sphere->triangles, triangle); } -void roam_triangle_remove(RoamTriangle *self, RoamSphere *sphere) +/** + * roam_triangle_remove: + * @triangle: the triangle + * @sphere: the sphere to remove the triangle from + * + * Remove a triangle from a sphere's mesh. + */ +void roam_triangle_remove(RoamTriangle *triangle, RoamSphere *sphere) { /* Update vertex normals */ - roam_point_remove_triangle(self->p.l, self); - roam_point_remove_triangle(self->p.m, self); - roam_point_remove_triangle(self->p.r, self); + roam_point_remove_triangle(triangle->p.l, triangle); + roam_point_remove_triangle(triangle->p.m, triangle); + roam_point_remove_triangle(triangle->p.r, triangle); - g_pqueue_remove(sphere->triangles, self->handle); + g_pqueue_remove(sphere->triangles, triangle->handle); } -void roam_triangle_sync_neighbors(RoamTriangle *new, RoamTriangle *old, RoamTriangle *neigh) +/* (neight->t.? == old) = new */ +static void roam_triangle_sync_neighbors(RoamTriangle *neigh, RoamTriangle *old, RoamTriangle *new) { if (neigh->t.l == old) neigh->t.l = new; else if (neigh->t.b == old) neigh->t.b = new; else if (neigh->t.r == old) neigh->t.r = new; - else g_assert_not_reached(); } -gboolean roam_point_visible(RoamPoint *self, RoamSphere *sphere) +static gboolean roam_triangle_visible(RoamTriangle *triangle, RoamSphere *sphere) { + RoamPoint *l = triangle->p.l; + RoamPoint *m = triangle->p.m; + RoamPoint *r = triangle->p.r; + gdouble min_x = MIN(MIN(l->px, m->px), r->px); + gdouble max_x = MAX(MAX(l->px, m->px), r->px); + gdouble min_y = MIN(MIN(l->py, m->py), r->py); + gdouble max_y = MAX(MAX(l->py, m->py), r->py); gint *view = sphere->view->view; - return self->px > view[0] && self->px < view[2] && - self->py > view[1] && self->py < view[3] && - self->pz > 0 && self->pz < 1; + return !(max_x < view[0] || min_x > view[2] || + max_y < view[1] || min_y > view[3]) && + l->pz > 0 && m->pz > 0 && r->pz > 0 && + l->pz < 1 && m->pz < 1 && r->pz < 1; } -gboolean roam_triangle_visible(RoamTriangle *self, RoamSphere *sphere) + +static gboolean roam_triangle_backface(RoamTriangle *triangle, RoamSphere *sphere) { - /* Do this with a bounding box */ - return roam_point_visible(self->p.l, sphere) || - roam_point_visible(self->p.m, sphere) || - roam_point_visible(self->p.r, sphere); + RoamPoint *l = triangle->p.l; + RoamPoint *m = triangle->p.m; + RoamPoint *r = triangle->p.r; + roam_point_update_projection(l, sphere->view); + roam_point_update_projection(m, sphere->view); + roam_point_update_projection(r, sphere->view); + double size = -( l->px * (m->py - r->py) + + m->px * (r->py - l->py) + + r->px * (l->py - m->py) ) / 2.0; + return size < 0; } -void roam_triangle_update_errors(RoamTriangle *self, RoamSphere *sphere) +/** + * roam_triangle_update_errors: + * @triangle: the triangle + * @sphere: the sphere to use when updating errors + * + * Update the error value associated with a triangle. Called when the view + * changes. + */ +void roam_triangle_update_errors(RoamTriangle *triangle, RoamSphere *sphere) { /* Update points */ - roam_point_update_projection(self->p.l, sphere); - roam_point_update_projection(self->p.m, sphere); - roam_point_update_projection(self->p.r, sphere); + roam_point_update_projection(triangle->p.l, sphere->view); + roam_point_update_projection(triangle->p.m, sphere->view); + roam_point_update_projection(triangle->p.r, sphere->view); - /* Not exactly correct, could be out on both sides (middle in) */ - if (!roam_triangle_visible(self, sphere)) { - self->error = -1; + if (!roam_triangle_visible(triangle, sphere)) { + triangle->error = -1; } else { - roam_point_update_projection(self->split, sphere); - RoamPoint *l = self->p.l; - RoamPoint *m = self->p.m; - RoamPoint *r = self->p.r; - RoamPoint *split = self->split; + roam_point_update_projection(triangle->split, sphere->view); + RoamPoint *l = triangle->p.l; + RoamPoint *m = triangle->p.m; + RoamPoint *r = triangle->p.r; + RoamPoint *split = triangle->split; /* l-r midpoint projected l-r midpoint */ gdouble pxdist = (l->px + r->px)/2 - split->px; gdouble pydist = (l->py + r->py)/2 - split->py; - self->error = sqrt(pxdist*pxdist + pydist*pydist); + triangle->error = sqrt(pxdist*pxdist + pydist*pydist); /* Multiply by size of triangle */ double size = -( l->px * (m->py - r->py) + @@ -292,74 +387,102 @@ void roam_triangle_update_errors(RoamTriangle *self, RoamSphere *sphere) r->px * (l->py - m->py) ) / 2.0; /* Size < 0 == backface */ - //if (size < 0) - // self->error *= -1; - self->error *= size; + triangle->error *= size; + + /* Give some preference to "edge" faces */ + if (roam_triangle_backface(triangle->t.l, sphere) || + roam_triangle_backface(triangle->t.b, sphere) || + roam_triangle_backface(triangle->t.r, sphere)) + triangle->error *= 500; } } -void roam_triangle_split(RoamTriangle *self, RoamSphere *sphere) +/** + * roam_triangle_split: + * @triangle: the triangle + * @sphere: the sphere + * + * Split a triangle into two child triangles and update the sphere. + * triangle + */ +void roam_triangle_split(RoamTriangle *triangle, RoamSphere *sphere) { - //g_message("roam_triangle_split: %p, e=%f\n", self, self->error); + //g_message("roam_triangle_split: %p, e=%f\n", triangle, triangle->error); sphere->polys += 2; - if (self != self->t.b->t.b) - roam_triangle_split(self->t.b, sphere); + if (triangle != triangle->t.b->t.b) + roam_triangle_split(triangle->t.b, sphere); + if (triangle != triangle->t.b->t.b) + g_assert_not_reached(); + + RoamTriangle *s = triangle; // Self + RoamTriangle *b = triangle->t.b; // Base - RoamTriangle *base = self->t.b; + RoamDiamond *dia = roam_diamond_new(s, b); /* Add new triangles */ - RoamPoint *mid = self->split; - RoamTriangle *sl = self->kids[0] = roam_triangle_new(self->p.m, mid, self->p.l); // Self Left - RoamTriangle *sr = self->kids[1] = roam_triangle_new(self->p.r, mid, self->p.m); // Self Right - RoamTriangle *bl = base->kids[0] = roam_triangle_new(base->p.m, mid, base->p.l); // Base Left - RoamTriangle *br = base->kids[1] = roam_triangle_new(base->p.r, mid, base->p.m); // Base Right - - /* tri,l, base, r, sphere */ - roam_triangle_add(sl, sr, self->t.l, br, sphere); - roam_triangle_add(sr, bl, self->t.r, sl, sphere); - roam_triangle_add(bl, br, base->t.l, sr, sphere); - roam_triangle_add(br, sl, base->t.r, bl, sphere); - - roam_triangle_sync_neighbors(sl, self, self->t.l); - roam_triangle_sync_neighbors(sr, self, self->t.r); - roam_triangle_sync_neighbors(bl, base, base->t.l); - roam_triangle_sync_neighbors(br, base, base->t.r); + RoamPoint *mid = triangle->split; + RoamTriangle *sl = s->kids[0] = roam_triangle_new(s->p.m, mid, s->p.l, dia); // Self Left + RoamTriangle *sr = s->kids[1] = roam_triangle_new(s->p.r, mid, s->p.m, dia); // Self Right + RoamTriangle *bl = b->kids[0] = roam_triangle_new(b->p.m, mid, b->p.l, dia); // Base Left + RoamTriangle *br = b->kids[1] = roam_triangle_new(b->p.r, mid, b->p.m, dia); // Base Right + + /* triangle,l, base, r, sphere */ + roam_triangle_add(sl, sr, s->t.l, br, sphere); + roam_triangle_add(sr, bl, s->t.r, sl, sphere); + roam_triangle_add(bl, br, b->t.l, sr, sphere); + roam_triangle_add(br, sl, b->t.r, bl, sphere); + + roam_triangle_sync_neighbors(s->t.l, s, sl); + roam_triangle_sync_neighbors(s->t.r, s, sr); + roam_triangle_sync_neighbors(b->t.l, b, bl); + roam_triangle_sync_neighbors(b->t.r, b, br); /* Remove old triangles */ - roam_triangle_remove(self, sphere); - roam_triangle_remove(base, sphere); + roam_triangle_remove(s, sphere); + roam_triangle_remove(b, sphere); /* Add/Remove diamonds */ - RoamDiamond *diamond = roam_diamond_new(self, base, sl, sr, bl, br); - roam_diamond_update_errors(diamond, sphere); - roam_diamond_add(diamond, sphere); - roam_diamond_remove(self->parent, sphere); - roam_diamond_remove(base->parent, sphere); + roam_diamond_update_errors(dia, sphere); + roam_diamond_add(dia, sphere); + roam_diamond_remove(s->parent, sphere); + roam_diamond_remove(b->parent, sphere); } -void roam_triangle_draw(RoamTriangle *self) +/** + * roam_triangle_draw: + * @triangle: the triangle + * + * Draw the triangle. Use for debugging. + */ +void roam_triangle_draw(RoamTriangle *triangle) { glBegin(GL_TRIANGLES); - glNormal3dv(self->p.r->norm); glVertex3dv((double*)self->p.r); - glNormal3dv(self->p.m->norm); glVertex3dv((double*)self->p.m); - glNormal3dv(self->p.l->norm); glVertex3dv((double*)self->p.l); + glNormal3dv(triangle->p.r->norm); glVertex3dv((double*)triangle->p.r); + glNormal3dv(triangle->p.m->norm); glVertex3dv((double*)triangle->p.m); + glNormal3dv(triangle->p.l->norm); glVertex3dv((double*)triangle->p.l); glEnd(); return; } -void roam_triangle_draw_normal(RoamTriangle *self) +/** + * roam_triangle_draw_normal: + * @triangle: the triangle + * + * Draw a normal vector for the triangle. Used while debugging. + */ +void roam_triangle_draw_normal(RoamTriangle *triangle) { double center[] = { - (self->p.l->x + self->p.m->x + self->p.r->x)/3.0, - (self->p.l->y + self->p.m->y + self->p.r->y)/3.0, - (self->p.l->z + self->p.m->z + self->p.r->z)/3.0, + (triangle->p.l->x + triangle->p.m->x + triangle->p.r->x)/3.0, + (triangle->p.l->y + triangle->p.m->y + triangle->p.r->y)/3.0, + (triangle->p.l->z + triangle->p.m->z + triangle->p.r->z)/3.0, }; double end[] = { - center[0]+self->norm[0]*2000000, - center[1]+self->norm[1]*2000000, - center[2]+self->norm[2]*2000000, + center[0]+triangle->norm[0]*2000000, + center[1]+triangle->norm[1]*2000000, + center[2]+triangle->norm[2]*2000000, }; glBegin(GL_LINES); glVertex3dv(center); @@ -370,121 +493,170 @@ void roam_triangle_draw_normal(RoamTriangle *self) /*************** * RoamDiamond * ***************/ -RoamDiamond *roam_diamond_new( - RoamTriangle *parent0, RoamTriangle *parent1, - RoamTriangle *kid0, RoamTriangle *kid1, - RoamTriangle *kid2, RoamTriangle *kid3) +/** + * roam_diamond_new: + * @parent0: a parent triangle + * @parent1: a parent triangle + * @kid0: a child triangle + * @kid1: a child triangle + * @kid2: a child triangle + * @kid3: a child triangle + * + * Create a diamond to store information about two split triangles. + * + * Returns: the new diamond + */ +RoamDiamond *roam_diamond_new(RoamTriangle *parent0, RoamTriangle *parent1) { - RoamDiamond *self = g_new0(RoamDiamond, 1); - - self->kids[0] = kid0; - self->kids[1] = kid1; - self->kids[2] = kid2; - self->kids[3] = kid3; - - kid0->parent = self; - kid1->parent = self; - kid2->parent = self; - kid3->parent = self; - - self->parents[0] = parent0; - self->parents[1] = parent1; - - return self; + RoamDiamond *diamond = g_new0(RoamDiamond, 1); + diamond->parents[0] = parent0; + diamond->parents[1] = parent1; + return diamond; } -void roam_diamond_add(RoamDiamond *self, RoamSphere *sphere) + +/** + * roam_diamond_add: + * @diamond: the diamond + * @sphere: the sphere to add the diamond to + * + * Add a diamond into the sphere's pool of diamonds. It will be check for + * possible merges. + */ +void roam_diamond_add(RoamDiamond *diamond, RoamSphere *sphere) { - self->active = TRUE; - self->error = MAX(self->parents[0]->error, self->parents[1]->error); - self->handle = g_pqueue_push(sphere->diamonds, self); + diamond->active = TRUE; + diamond->error = MAX(diamond->parents[0]->error, diamond->parents[1]->error); + diamond->handle = g_pqueue_push(sphere->diamonds, diamond); } -void roam_diamond_remove(RoamDiamond *self, RoamSphere *sphere) + +/** + * roam_diamond_remove: + * @diamond: the diamond + * @sphere: the sphere to remove the diamond from + * + * Remove a diamond from the sphere's pool of diamonds. + */ +void roam_diamond_remove(RoamDiamond *diamond, RoamSphere *sphere) { - if (self && self->active) { - self->active = FALSE; - g_pqueue_remove(sphere->diamonds, self->handle); + if (diamond && diamond->active) { + diamond->active = FALSE; + g_pqueue_remove(sphere->diamonds, diamond->handle); } } -void roam_diamond_merge(RoamDiamond *self, RoamSphere *sphere) + +/** + * roam_diamond_merge: + * @diamond: the diamond + * @sphere: the sphere + * + * "Merge" a diamond back into two parent triangles. The original two triangles + * are added back into the sphere and the four child triangles as well as the + * diamond are removed. + */ +void roam_diamond_merge(RoamDiamond *diamond, RoamSphere *sphere) { - //g_message("roam_diamond_merge: %p, e=%f\n", self, self->error); + //g_message("roam_diamond_merge: %p, e=%f\n", diamond, diamond->error); + /* TODO: pick the best split */ sphere->polys -= 2; - /* TODO: pick the best split */ - RoamTriangle **kids = self->kids; + /* Use nicer temp names */ + RoamTriangle *s = diamond->parents[0]; // Self + RoamTriangle *b = diamond->parents[1]; // Base + + RoamTriangle *sl = s->kids[0]; + RoamTriangle *sr = s->kids[1]; + RoamTriangle *bl = b->kids[0]; + RoamTriangle *br = b->kids[1]; - /* Create triangles */ - RoamTriangle *tri = self->parents[0]; - RoamTriangle *base = self->parents[1]; + s->kids[0] = s->kids[1] = NULL; + b->kids[0] = b->kids[1] = NULL; - roam_triangle_add(tri, kids[0]->t.b, base, kids[1]->t.b, sphere); - roam_triangle_add(base, kids[2]->t.b, tri, kids[3]->t.b, sphere); + /* Add original triangles */ + roam_triangle_sync_neighbors(s->t.l, sl, s); + roam_triangle_sync_neighbors(s->t.r, sr, s); + roam_triangle_sync_neighbors(b->t.l, bl, b); + roam_triangle_sync_neighbors(b->t.r, br, b); - roam_triangle_sync_neighbors(tri, kids[0], kids[0]->t.b); - roam_triangle_sync_neighbors(tri, kids[1], kids[1]->t.b); - roam_triangle_sync_neighbors(base, kids[2], kids[2]->t.b); - roam_triangle_sync_neighbors(base, kids[3], kids[3]->t.b); + roam_triangle_add(s, sl->t.b, b, sr->t.b, sphere); + roam_triangle_add(b, bl->t.b, s, br->t.b, sphere); - /* Remove triangles */ - roam_triangle_remove(kids[0], sphere); - roam_triangle_remove(kids[1], sphere); - roam_triangle_remove(kids[2], sphere); - roam_triangle_remove(kids[3], sphere); + roam_triangle_sync_neighbors(sl->t.b, sl, s); + roam_triangle_sync_neighbors(sr->t.b, sr, s); + roam_triangle_sync_neighbors(bl->t.b, bl, b); + roam_triangle_sync_neighbors(br->t.b, br, b); - /* Clear kids */ - tri->kids[0] = tri->kids[1] = NULL; - base->kids[0] = base->kids[1] = NULL; + /* Remove child triangles */ + roam_triangle_remove(sl, sphere); + roam_triangle_remove(sr, sphere); + roam_triangle_remove(bl, sphere); + roam_triangle_remove(br, sphere); /* Add/Remove triangles */ - if (tri->t.l->t.l == tri->t.r->t.r && - tri->t.l->t.l != tri && tri->parent) { - roam_diamond_update_errors(tri->parent, sphere); - roam_diamond_add(tri->parent, sphere); + if (s->t.l->t.l == s->t.r->t.r && + s->t.l->t.l != s && s->parent) { + roam_diamond_update_errors(s->parent, sphere); + roam_diamond_add(s->parent, sphere); } - if (base->t.l->t.l == base->t.r->t.r && - base->t.l->t.l != base && base->parent) { - roam_diamond_update_errors(base->parent, sphere); - roam_diamond_add(base->parent, sphere); + if (b->t.l->t.l == b->t.r->t.r && + b->t.l->t.l != b && b->parent) { + roam_diamond_update_errors(b->parent, sphere); + roam_diamond_add(b->parent, sphere); } /* Remove and free diamond and child triangles */ - roam_diamond_remove(self, sphere); - g_assert(self->kids[0]->p.m == self->kids[1]->p.m && - self->kids[1]->p.m == self->kids[2]->p.m && - self->kids[2]->p.m == self->kids[3]->p.m); - g_assert(self->kids[0]->p.m->tris == 0); - roam_triangle_free(self->kids[0]); - roam_triangle_free(self->kids[1]); - roam_triangle_free(self->kids[2]); - roam_triangle_free(self->kids[3]); - g_free(self); + roam_diamond_remove(diamond, sphere); + g_assert(sl->p.m == sr->p.m && + sr->p.m == bl->p.m && + bl->p.m == br->p.m); + g_assert(sl->p.m->tris == 0); + roam_triangle_free(sl); + roam_triangle_free(sr); + roam_triangle_free(bl); + roam_triangle_free(br); + g_free(diamond); } -void roam_diamond_update_errors(RoamDiamond *self, RoamSphere *sphere) + +/** + * roam_diamond_update_errors: + * @diamond: the diamond + * @sphere: the sphere to use when updating errors + * + * Update the error value associated with a diamond. Called when the view + * changes. + */ +void roam_diamond_update_errors(RoamDiamond *diamond, RoamSphere *sphere) { - roam_triangle_update_errors(self->parents[0], sphere); - roam_triangle_update_errors(self->parents[1], sphere); - self->error = MAX(self->parents[0]->error, self->parents[1]->error); + roam_triangle_update_errors(diamond->parents[0], sphere); + roam_triangle_update_errors(diamond->parents[1], sphere); + diamond->error = MAX(diamond->parents[0]->error, diamond->parents[1]->error); } /************** * RoamSphere * **************/ +/** + * roam_sphere_new: + * + * Create a new sphere + * + * Returns: the sphere + */ RoamSphere *roam_sphere_new() { - RoamSphere *self = g_new0(RoamSphere, 1); - self->polys = 8; - self->triangles = g_pqueue_new((GCompareDataFunc)tri_cmp, NULL); - self->diamonds = g_pqueue_new((GCompareDataFunc)dia_cmp, NULL); + RoamSphere *sphere = g_new0(RoamSphere, 1); + sphere->polys = 8; + sphere->triangles = g_pqueue_new((GCompareDataFunc)tri_cmp, NULL); + sphere->diamonds = g_pqueue_new((GCompareDataFunc)dia_cmp, NULL); RoamPoint *vertexes[] = { - roam_point_new( 0, 1, 0), // 0 - roam_point_new( 0,-1, 0), // 1 - roam_point_new( 0, 0, 1), // 2 - roam_point_new( 1, 0, 0), // 3 - roam_point_new( 0, 0,-1), // 4 - roam_point_new(-1, 0, 0), // 5 + roam_point_new( 90, 0, 0), // 0 (North) + roam_point_new(-90, 0, 0), // 1 (South) + roam_point_new( 0, 0, 0), // 2 (Europe/Africa) + roam_point_new( 0, 90, 0), // 3 (Asia,East) + roam_point_new( 0, 180, 0), // 4 (Pacific) + roam_point_new( 0, -90, 0), // 5 (Americas,West) }; int _triangles[][2][3] = { /*lv mv rv ln, bn, rn */ @@ -501,132 +673,186 @@ RoamSphere *roam_sphere_new() for (int i = 0; i < 6; i++) roam_point_update_height(vertexes[i]); for (int i = 0; i < 8; i++) - self->roots[i] = roam_triangle_new( + sphere->roots[i] = roam_triangle_new( vertexes[_triangles[i][0][0]], vertexes[_triangles[i][0][1]], - vertexes[_triangles[i][0][2]]); + vertexes[_triangles[i][0][2]], + NULL); for (int i = 0; i < 8; i++) - roam_triangle_add(self->roots[i], - self->roots[_triangles[i][1][0]], - self->roots[_triangles[i][1][1]], - self->roots[_triangles[i][1][2]], - self); + roam_triangle_add(sphere->roots[i], + sphere->roots[_triangles[i][1][0]], + sphere->roots[_triangles[i][1][1]], + sphere->roots[_triangles[i][1][2]], + sphere); for (int i = 0; i < 8; i++) - g_debug("RoamSphere: new - %p edge=%f,%f,%f,%f", self->roots[i], - self->roots[i]->edge.n, - self->roots[i]->edge.s, - self->roots[i]->edge.e, - self->roots[i]->edge.w); + g_debug("RoamSphere: new - %p edge=%f,%f,%f,%f", sphere->roots[i], + sphere->roots[i]->edge.n, + sphere->roots[i]->edge.s, + sphere->roots[i]->edge.e, + sphere->roots[i]->edge.w); - return self; + return sphere; } -void roam_sphere_update_view(RoamSphere *self) + +/** + * roam_sphere_update_view + * @sphere: the sphere + * + * Recreate the sphere's view matrices based on the current OpenGL state. + */ +void roam_sphere_update_view(RoamSphere *sphere) { - if (!self->view) - self->view = g_new0(RoamView, 1); - glGetDoublev (GL_MODELVIEW_MATRIX, self->view->model); - glGetDoublev (GL_PROJECTION_MATRIX, self->view->proj); - glGetIntegerv(GL_VIEWPORT, self->view->view); - self->view->version++; + if (!sphere->view) + sphere->view = g_new0(RoamView, 1); + glGetDoublev (GL_MODELVIEW_MATRIX, sphere->view->model); + glGetDoublev (GL_PROJECTION_MATRIX, sphere->view->proj); + glGetIntegerv(GL_VIEWPORT, sphere->view->view); + sphere->view->version++; } -void roam_sphere_update_errors(RoamSphere *self) + +/** + * roam_sphere_update_errors + * @sphere: the sphere + * + * Update triangle and diamond errors in the sphere. + */ +void roam_sphere_update_errors(RoamSphere *sphere) { - g_debug("RoamSphere: update_errors - polys=%d", self->polys); - GPtrArray *tris = g_pqueue_get_array(self->triangles); - GPtrArray *dias = g_pqueue_get_array(self->diamonds); + g_debug("RoamSphere: update_errors - polys=%d", sphere->polys); + GPtrArray *tris = g_pqueue_get_array(sphere->triangles); + GPtrArray *dias = g_pqueue_get_array(sphere->diamonds); - roam_sphere_update_view(self); + roam_sphere_update_view(sphere); for (int i = 0; i < tris->len; i++) { - RoamTriangle *tri = tris->pdata[i]; - roam_triangle_update_errors(tri, self); - g_pqueue_priority_changed(self->triangles, tri->handle); + RoamTriangle *triangle = tris->pdata[i]; + roam_triangle_update_errors(triangle, sphere); + g_pqueue_priority_changed(sphere->triangles, triangle->handle); } for (int i = 0; i < dias->len; i++) { - RoamDiamond *dia = dias->pdata[i]; - roam_diamond_update_errors(dia, self); - g_pqueue_priority_changed(self->diamonds, dia->handle); + RoamDiamond *diamond = dias->pdata[i]; + roam_diamond_update_errors(diamond, sphere); + g_pqueue_priority_changed(sphere->diamonds, diamond->handle); } g_ptr_array_free(tris, TRUE); g_ptr_array_free(dias, TRUE); } -void roam_sphere_split_one(RoamSphere *self) +/** + * roam_sphere_split_one + * @sphere: the sphere + * + * Split the triangle with the greatest error. + */ +void roam_sphere_split_one(RoamSphere *sphere) { - RoamTriangle *to_split = g_pqueue_peek(self->triangles); + RoamTriangle *to_split = g_pqueue_peek(sphere->triangles); if (!to_split) return; - roam_triangle_split(to_split, self); + roam_triangle_split(to_split, sphere); } -void roam_sphere_merge_one(RoamSphere *self) + +/** + * roam_sphere_merge_one + * @sphere: the sphere + * + * Merge the diamond with the lowest error. + */ +void roam_sphere_merge_one(RoamSphere *sphere) { - RoamDiamond *to_merge = g_pqueue_peek(self->diamonds); + RoamDiamond *to_merge = g_pqueue_peek(sphere->diamonds); if (!to_merge) return; - roam_diamond_merge(to_merge, self); + roam_diamond_merge(to_merge, sphere); } -gint roam_sphere_split_merge(RoamSphere *self) + +/** + * roam_sphere_split_merge + * @sphere: the sphere + * + * Perform a predetermined number split-merge iterations. + * + * Returns: the number splits and merges done + */ +gint roam_sphere_split_merge(RoamSphere *sphere) { gint iters = 0, max_iters = 500; //gint target = 4000; gint target = 2000; //gint target = 500; - if (!self->view) + if (!sphere->view) return 0; - if (target - self->polys > 100) { - //g_debug("RoamSphere: split_merge - Splitting %d - %d > 100", target, self->polys); - while (self->polys < target && iters++ < max_iters) - roam_sphere_split_one(self); + if (target - sphere->polys > 100) { + //g_debug("RoamSphere: split_merge - Splitting %d - %d > 100", target, sphere->polys); + while (sphere->polys < target && iters++ < max_iters) + roam_sphere_split_one(sphere); } - if (self->polys - target > 100) { - //g_debug("RoamSphere: split_merge - Merging %d - %d > 100", self->polys, target); - while (self->polys > target && iters++ < max_iters) - roam_sphere_merge_one(self); + if (sphere->polys - target > 100) { + //g_debug("RoamSphere: split_merge - Merging %d - %d > 100", sphere->polys, target); + while (sphere->polys > target && iters++ < max_iters) + roam_sphere_merge_one(sphere); } - while (((RoamTriangle*)g_pqueue_peek(self->triangles))->error > - ((RoamDiamond *)g_pqueue_peek(self->diamonds ))->error && + while (((RoamTriangle*)g_pqueue_peek(sphere->triangles))->error > + ((RoamDiamond *)g_pqueue_peek(sphere->diamonds ))->error && iters++ < max_iters) { //g_debug("RoamSphere: split_merge - Fixing 1 %f > %f && %d < %d", - // ((RoamTriangle*)g_pqueue_peek(self->triangles))->error, - // ((RoamDiamond *)g_pqueue_peek(self->diamonds ))->error, + // ((RoamTriangle*)g_pqueue_peek(sphere->triangles))->error, + // ((RoamDiamond *)g_pqueue_peek(sphere->diamonds ))->error, // iters-1, max_iters); - roam_sphere_merge_one(self); - roam_sphere_split_one(self); + roam_sphere_merge_one(sphere); + roam_sphere_split_one(sphere); } return iters; } -void roam_sphere_draw(RoamSphere *self) + +/** + * roam_sphere_draw: + * @sphere: the sphere + * + * Draw the sphere. Use for debugging. + */ +void roam_sphere_draw(RoamSphere *sphere) { g_debug("RoamSphere: draw"); - g_pqueue_foreach(self->triangles, (GFunc)roam_triangle_draw, NULL); + g_pqueue_foreach(sphere->triangles, (GFunc)roam_triangle_draw, NULL); } -void roam_sphere_draw_normals(RoamSphere *self) + +/** + * roam_sphere_draw_normals + * @sphere: the sphere + * + * Draw all the surface normal vectors for the sphere. Used while debugging. + */ +void roam_sphere_draw_normals(RoamSphere *sphere) { g_debug("RoamSphere: draw_normal"); - g_pqueue_foreach(self->triangles, (GFunc)roam_triangle_draw_normal, NULL); + g_pqueue_foreach(sphere->triangles, (GFunc)roam_triangle_draw_normal, NULL); } -GList *_roam_sphere_get_leaves(RoamTriangle *tri, GList *list) + +static GList *_roam_sphere_get_leaves(RoamTriangle *triangle, GList *list, gboolean all) { - if (tri->kids[0] && tri->kids[1]) { - list = _roam_sphere_get_leaves(tri->kids[0], list); - list = _roam_sphere_get_leaves(tri->kids[1], list); + if (triangle->kids[0] && triangle->kids[1]) { + if (all) list = g_list_prepend(list, triangle); + list = _roam_sphere_get_leaves(triangle->kids[0], list, all); + list = _roam_sphere_get_leaves(triangle->kids[1], list, all); return list; } else { - return g_list_append(list, tri); + return g_list_prepend(list, triangle); } } -GList *_roam_sphere_get_intersect_rec(RoamTriangle *tri, GList *list, - gdouble n, gdouble s, gdouble e, gdouble w) + +static GList *_roam_sphere_get_intersect_rec(RoamTriangle *triangle, GList *list, + gboolean all, gdouble n, gdouble s, gdouble e, gdouble w) { - gdouble tn = tri->edge.n; - gdouble ts = tri->edge.s; - gdouble te = tri->edge.e; - gdouble tw = tri->edge.w; + gdouble tn = triangle->edge.n; + gdouble ts = triangle->edge.s; + gdouble te = triangle->edge.e; + gdouble tw = triangle->edge.w; gboolean debug = FALSE && n==90 && s==45 && e==-90 && w==-180 && @@ -634,28 +860,47 @@ GList *_roam_sphere_get_intersect_rec(RoamTriangle *tri, GList *list, if (debug) g_message("t=%p: %f < %f || %f > %f || %f < %f || %f > %f", - tri, tn, s, ts, n, te, w, tw, e); - if (tn < s || ts > n || te < w || tw > e) { + triangle, tn, s, ts, n, te, w, tw, e); + if (tn <= s || ts >= n || te <= w || tw >= e) { /* No intersect */ if (debug) g_message("no intersect"); return list; - } else if (tn < n && ts > s && te < e && tw > w) { + } else if (tn <= n && ts >= s && te <= e && tw >= w) { /* Triangle is completely contained */ if (debug) g_message("contained"); - return _roam_sphere_get_leaves(tri, list); - } else if (tri->kids[0] && tri->kids[1]) { + if (all) list = g_list_prepend(list, triangle); + return _roam_sphere_get_leaves(triangle, list, all); + } else if (triangle->kids[0] && triangle->kids[1]) { /* Paritial intersect with children */ if (debug) g_message("edge w/ child"); - list = _roam_sphere_get_intersect_rec(tri->kids[0], list, n, s, e, w); - list = _roam_sphere_get_intersect_rec(tri->kids[1], list, n, s, e, w); + if (all) list = g_list_prepend(list, triangle); + list = _roam_sphere_get_intersect_rec(triangle->kids[0], list, all, n, s, e, w); + list = _roam_sphere_get_intersect_rec(triangle->kids[1], list, all, n, s, e, w); return list; } else { /* This triangle is an edge case */ if (debug) g_message("edge"); - return g_list_append(list, tri); + return g_list_prepend(list, triangle); } } -GList *roam_sphere_get_intersect(RoamSphere *self, + +/** + * roam_sphere_get_intersect + * @sphere: the sphere + * @all: TRUE if non-leaf triangle should be returned as well + * @n: the northern edge + * @s: the southern edge + * @e: the eastern edge + * @w: the western edge + * + * Lookup triangles withing the sphere that intersect a given lat-lon box. + * + * Returns: the list of intersecting triangles. + */ +/* Warning: This grabs pointers to triangles which can be changed by other + * calls, e.g. split_merge. If you use this, you need to do some locking to + * prevent the returned list from becomming stale. */ +GList *roam_sphere_get_intersect(RoamSphere *sphere, gboolean all, gdouble n, gdouble s, gdouble e, gdouble w) { /* I think this is correct.. @@ -663,27 +908,36 @@ GList *roam_sphere_get_intersect(RoamSphere *self, * time = n_tiles * 2*tris_per_tile * i_cost * time = 30 * 2*333 * i_cost = 20000 * i_cost */ GList *list = NULL; - for (int i = 0; i < G_N_ELEMENTS(self->roots); i++) - list = _roam_sphere_get_intersect_rec(self->roots[i], - list, n, s, e, w); + for (int i = 0; i < G_N_ELEMENTS(sphere->roots); i++) + list = _roam_sphere_get_intersect_rec(sphere->roots[i], + list, all, n, s, e, w); return list; } -void roam_sphere_free_tri(RoamTriangle *tri) + +static void roam_sphere_free_tri(RoamTriangle *triangle) { - if (--tri->p.l->tris == 0) g_free(tri->p.l); - if (--tri->p.m->tris == 0) g_free(tri->p.m); - if (--tri->p.r->tris == 0) g_free(tri->p.r); - roam_triangle_free(tri); + if (--triangle->p.l->tris == 0) g_free(triangle->p.l); + if (--triangle->p.m->tris == 0) g_free(triangle->p.m); + if (--triangle->p.r->tris == 0) g_free(triangle->p.r); + roam_triangle_free(triangle); } -void roam_sphere_free(RoamSphere *self) + +/** + * roam_sphere_free + * @sphere: the sphere + * + * Free data associated with a sphere + */ +void roam_sphere_free(RoamSphere *sphere) { + g_debug("RoamSphere: free"); /* Slow method, but it should work */ - while (self->polys > 8) - roam_sphere_merge_one(self); + while (sphere->polys > 8) + roam_sphere_merge_one(sphere); /* TODO: free points */ - g_pqueue_foreach(self->triangles, (GFunc)roam_sphere_free_tri, NULL); - g_pqueue_free(self->triangles); - g_pqueue_free(self->diamonds); - g_free(self->view); - g_free(self); + g_pqueue_foreach(sphere->triangles, (GFunc)roam_sphere_free_tri, NULL); + g_pqueue_free(sphere->triangles); + g_pqueue_free(sphere->diamonds); + g_free(sphere->view); + g_free(sphere); }