/*
- * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
*
* 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
#include <GL/gl.h>
#include <GL/glu.h>
-#include "gis-world.h"
-
+#include "gis-util.h"
#include "roam.h"
-
/**
* TODO:
* - Optimize for memory consumption
* - Target polygon count/detail
*/
-/* Misc */
-RoamView *roam_view_new()
-{
- return g_new0(RoamView, 1);
-}
-void roam_view_update(RoamView *view)
-{
- glGetDoublev (GL_MODELVIEW_MATRIX, view->model);
- glGetDoublev (GL_PROJECTION_MATRIX, view->proj);
- glGetIntegerv(GL_VIEWPORT, view->view);
- view->version++;
-}
-
/* For GPQueue comparators */
static gint tri_cmp(RoamTriangle *a, RoamTriangle *b, gpointer data)
{
else return 0;
}
+
/*************
* RoamPoint *
*************/
-RoamPoint *roam_point_new(gdouble x, gdouble y, gdouble z)
+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;
+ self->lat = lat;
+ self->lon = lon;
+ self->elev = elev;
/* For get_intersect */
- xyz2ll(x, y, z, &self->lat, &self->lon);
+ lle2xyz(lat, lon, elev, &self->x, &self->y, &self->z);
return self;
}
RoamPoint *roam_point_dup(RoamPoint *self)
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;
}
}
void roam_point_update_projection(RoamPoint *self, RoamSphere *sphere)
static int count = 0;
static int version = 0;
if (version != sphere->view->version) {
- g_debug("Projected %d points", count);
+ g_debug("RoamPoint: Projected %d points", count);
count = 0;
version = sphere->view->version;
}
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);
+ (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)
+ // 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);
+ //if ((float)l->lat == (float)r->lat &&
+ // (float)self->split->lat != (float)l->lat)
+ // g_warning("RoamTriangle: new - split.lat=%f != (l=r).lat=%f",
+ // self->split->lat, l->lat);
/* Update normal */
double pa[3];
self->norm[1] /= total;
self->norm[2] /= total;
+ /* Store bounding box, for get_intersect */
+ RoamPoint *p[] = {l,m,r};
+ self->edge.n = -90; self->edge.s = 90;
+ self->edge.e = -180; self->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);
+ 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);
+ }
+ if (maxed) {
+ if (self->edge.e < 0)
+ self->edge.w = -180;
+ else
+ self->edge.e = 180;
+ }
+
//g_message("roam_triangle_new: %p", self);
return self;
}
if (self != self->t.b->t.b)
roam_triangle_split(self->t.b, sphere);
+ if (self != self->t.b->t.b)
+ g_assert_not_reached();
RoamTriangle *base = self->t.b;
/* Add new triangles */
RoamPoint *mid = self->split;
- RoamTriangle *sl = roam_triangle_new(self->p.m, mid, self->p.l); // Self Left
- RoamTriangle *sr = roam_triangle_new(self->p.r, mid, self->p.m); // Self Right
- RoamTriangle *bl = roam_triangle_new(base->p.m, mid, base->p.l); // Base Left
- RoamTriangle *br = roam_triangle_new(base->p.r, mid, base->p.m); // Base Right
+ 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_remove(kids[2], sphere);
roam_triangle_remove(kids[3], sphere);
+ /* Clear kids */
+ tri->kids[0] = tri->kids[1] = NULL;
+ base->kids[0] = base->kids[1] = NULL;
+
/* Add/Remove triangles */
if (tri->t.l->t.l == tri->t.r->t.r &&
tri->t.l->t.l != tri && tri->parent) {
self->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 */
self->roots[_triangles[i][1][1]],
self->roots[_triangles[i][1][2]],
self);
+ 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);
return self;
}
+void roam_sphere_update_view(RoamSphere *self)
+{
+ 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++;
+}
void roam_sphere_update_errors(RoamSphere *self)
{
g_debug("RoamSphere: update_errors - polys=%d", self->polys);
- if (!self->view)
- self->view = roam_view_new();
- roam_view_update(self->view);
-
GPtrArray *tris = g_pqueue_get_array(self->triangles);
GPtrArray *dias = g_pqueue_get_array(self->diamonds);
+ roam_sphere_update_view(self);
+
for (int i = 0; i < tris->len; i++) {
RoamTriangle *tri = tris->pdata[i];
roam_triangle_update_errors(tri, self);
g_debug("RoamSphere: draw_normal");
g_pqueue_foreach(self->triangles, (GFunc)roam_triangle_draw_normal, NULL);
}
+static GList *_roam_sphere_get_leaves(RoamTriangle *tri, GList *list, gboolean all)
+{
+ if (tri->kids[0] && tri->kids[1]) {
+ if (all) list = g_list_prepend(list, tri);
+ list = _roam_sphere_get_leaves(tri->kids[0], list, all);
+ list = _roam_sphere_get_leaves(tri->kids[1], list, all);
+ return list;
+ } else {
+ return g_list_prepend(list, tri);
+ }
+}
+static GList *_roam_sphere_get_intersect_rec(RoamTriangle *tri, 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;
+
+ gboolean debug = FALSE &&
+ n==90 && s==45 && e==-90 && w==-180 &&
+ ts > 44 && ts < 46;
+
+ 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) {
+ /* No intersect */
+ if (debug) g_message("no intersect");
+ return list;
+ } else if (tn <= n && ts >= s && te <= e && tw >= w) {
+ /* Triangle is completely contained */
+ if (debug) g_message("contained");
+ if (all) list = g_list_prepend(list, tri);
+ return _roam_sphere_get_leaves(tri, list, all);
+ } else if (tri->kids[0] && tri->kids[1]) {
+ /* Paritial intersect with children */
+ if (debug) g_message("edge w/ child");
+ if (all) list = g_list_prepend(list, tri);
+ list = _roam_sphere_get_intersect_rec(tri->kids[0], list, all, n, s, e, w);
+ list = _roam_sphere_get_intersect_rec(tri->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_prepend(list, tri);
+ }
+}
+/* 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 *self, gboolean all,
+ gdouble n, gdouble s, gdouble e, gdouble w)
+{
+ /* I think this is correct..
+ * i_cost = cost for triangle-rectagnle intersect test
+ * 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, all, n, s, e, w);
+ return list;
+}
void roam_sphere_free_tri(RoamTriangle *tri)
{
if (--tri->p.l->tris == 0) g_free(tri->p.l);
}
void roam_sphere_free(RoamSphere *self)
{
+ g_debug("RoamSphere: free");
/* Slow method, but it should work */
while (self->polys > 8)
roam_sphere_merge_one(self);