]> Pileus Git - grits/blobdiff - src/objects/gis-tile.c
Rename GisBBox to GisBounds
[grits] / src / objects / gis-tile.c
index fe3ff6ed8d3b2db94f0814620400b4baf949a62b..9ee747bc0666aafbf01c6dd54632b83fd059dfdc 100644 (file)
@@ -33,7 +33,6 @@
  */
 
 #include <config.h>
-#include "gis-util.h"
 #include "gis-tile.h"
 
 gchar *gis_tile_path_table[2][2] = {
@@ -58,11 +57,9 @@ GisTile *gis_tile_new(GisTile *parent,
 {
        GisTile *tile = g_object_new(GIS_TYPE_TILE, NULL);
        tile->parent = parent;
-       tile->edge.n = n;
-       tile->edge.s = s;
-       tile->edge.e = e;
-       tile->edge.w = w;
        tile->atime  = time(NULL);
+       gis_bounds_set_bounds(&tile->coords, 0, 1, 1, 0);
+       gis_bounds_set_bounds(&tile->edge, n, s, e, w);
        return tile;
 }
 
@@ -93,37 +90,37 @@ gchar *gis_tile_get_path(GisTile *child)
        return g_string_free(path, FALSE);
 }
 
-static gdouble _gis_tile_get_min_dist(GisTile *tile,
-               gdouble lat, gdouble lon, gdouble elev)
+static gdouble _gis_tile_get_min_dist(GisPoint *eye, GisBounds *bounds)
 {
-       gdouble tlat  = lat > tile->edge.n ? tile->edge.n :
-                        lat < tile->edge.s ? tile->edge.s : lat;
-       gdouble tlon  = lon > tile->edge.e ? tile->edge.e :
-                       lon < tile->edge.w ? tile->edge.w : lon;
-       gdouble telev = 0; // TODO: elevation at rlat,rlon
-       //if (lat == tlat && lon == tlon)
+       GisPoint pos = {};
+       pos.lat = eye->lat > bounds->n ? bounds->n :
+                 eye->lat < bounds->s ? bounds->s : eye->lat;
+       pos.lon = eye->lon > bounds->e ? bounds->e :
+                 eye->lon < bounds->w ? bounds->w : eye->lon;
+       //if (eye->lat == pos.lat && eye->lon == pos.lon)
        //      return elev; /* Shortcut? */
        gdouble a[3], b[3];
-       lle2xyz( lat,  lon,  elev, a+0, a+1, a+2);
-       lle2xyz(tlat, tlon, telev, b+0, b+1, b+2);
+       lle2xyz(eye->lat, eye->lon, eye->elev, a+0, a+1, a+2);
+       lle2xyz(pos.lat,  pos.lon,  pos.elev,  b+0, b+1, b+2);
        return distd(a, b);
 }
 
-static gboolean _gis_tile_needs_split(GisTile *tile,
-               gdouble max_res, gint width, gint height,
-               gdouble lat, gdouble lon, gdouble elev)
+static gboolean _gis_tile_precise(GisPoint *eye, GisBounds *bounds,
+               gdouble max_res, gint width, gint height)
 {
-       gdouble lat_point = tile->edge.n < 0 ? tile->edge.n :
-                           tile->edge.s > 0 ? tile->edge.s : 0;
-       gdouble min_dist  = _gis_tile_get_min_dist(tile, lat, lon, elev);
+       gdouble min_dist  = _gis_tile_get_min_dist(eye, bounds);
        gdouble view_res  = MPPX(min_dist);
-       gdouble lon_dist  = tile->edge.e - tile->edge.w;
+
+       gdouble lat_point = bounds->n < 0 ? bounds->n :
+                           bounds->s > 0 ? bounds->s : 0;
+       gdouble lon_dist  = bounds->e - bounds->w;
        gdouble tile_res  = ll2m(lon_dist, lat_point)/width;
 
        /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
         * would be drawn */
-       gdouble scale = elev / min_dist;
+       gdouble scale = eye->elev / min_dist;
        view_res /= scale;
+       //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */
        //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
        //          "eye=(%9.1f %9.1f %9.1f) "
        //          "elev=%9.1f / dist=%9.1f = %f",
@@ -131,20 +128,17 @@ static gboolean _gis_tile_needs_split(GisTile *tile,
        //              lat, lon, elev,
        //              elev, min_dist, scale);
 
-       if (tile_res < max_res)
-               return FALSE;
-       return view_res < tile_res;
+       return tile_res < max_res ||
+              tile_res < view_res;
 }
 
 /**
  * gis_tile_update:
  * @root:      the root tile to split
+ * @eye:       the point the tile is viewed from, for calculating distances
  * @res:       a maximum resolution in meters per pixel to split tiles to
  * @width:     width in pixels of the image associated with the tile
  * @height:    height in pixels of the image associated with the tile
- * @lat:       latitude of the eye point
- * @lon:       longitude of the eye point
- * @elev:      elevation of the eye point
  * @load_func: function used to load the image when a new tile is created
  * @user_data: user data to past to the load function
  *
@@ -154,31 +148,36 @@ static gboolean _gis_tile_needs_split(GisTile *tile,
  * the tile is recursively subdivided until a sufficient resolution is
  * achieved.
  */
-void gis_tile_update(GisTile *root,
+void gis_tile_update(GisTile *root, GisPoint *eye,
                gdouble res, gint width, gint height,
-               gdouble lat, gdouble lon, gdouble elev,
                GisTileLoadFunc load_func, gpointer user_data)
 {
        root->atime = time(NULL);
-       //g_debug("GisTile: update - %p->atime = %u", root, (guint)root->atime);
-       gdouble lat_dist = root->edge.n - root->edge.s;
-       gdouble lon_dist = root->edge.e - root->edge.w;
-       if (_gis_tile_needs_split(root, res, width, height, lat, lon, elev)) {
-               gdouble lat_step = lat_dist / G_N_ELEMENTS(root->children);
-               gdouble lon_step = lon_dist / G_N_ELEMENTS(root->children[0]);
-               int x, y;
-               gis_tile_foreach_index(root, x, y) {
-                       if (!root->children[x][y]) {
-                               root->children[x][y] = gis_tile_new(root,
-                                               root->edge.n-(lat_step*(x+0)),
-                                               root->edge.n-(lat_step*(x+1)),
-                                               root->edge.w+(lon_step*(y+1)),
-                                               root->edge.w+(lon_step*(y+0)));
-                               load_func(root->children[x][y], user_data);
+       //g_debug("GisTile: update - %p->atime = %u",
+       //              root, (guint)root->atime);
+       const gdouble rows = G_N_ELEMENTS(root->children);
+       const gdouble cols = G_N_ELEMENTS(root->children[0]);
+       const gdouble lat_dist = root->edge.n - root->edge.s;
+       const gdouble lon_dist = root->edge.e - root->edge.w;
+       const gdouble lat_step = lat_dist / rows;
+       const gdouble lon_step = lon_dist / cols;
+       int row, col;
+       gis_tile_foreach_index(root, row, col) {
+               GisTile **child = &root->children[row][col];
+               GisBounds edge;
+               edge.n = root->edge.n-(lat_step*(row+0));
+               edge.s = root->edge.n-(lat_step*(row+1));
+               edge.e = root->edge.w+(lon_step*(col+1));
+               edge.w = root->edge.w+(lon_step*(col+0));
+               if (!_gis_tile_precise(eye, &edge, res,
+                                       width/cols, height/rows)) {
+                       if (!*child) {
+                               *child = gis_tile_new(root, edge.n, edge.s,
+                                               edge.e, edge.w);
+                               load_func(*child, user_data);
                        }
-                       gis_tile_update(root->children[x][y],
+                       gis_tile_update(*child, eye,
                                        res, width, height,
-                                       lat, lon, elev,
                                        load_func, user_data);
                }
        }