]> Pileus Git - grits/blobdiff - src/roam.c
Revert "Test pure distance based errors"
[grits] / src / roam.c
index 2fe0acfb65a0b9a58883790759b5e6530e0d5138..0f4995e0b56593d7bf3dc03f4d65015afb847e8e 100644 (file)
@@ -313,12 +313,12 @@ void roam_triangle_remove(RoamTriangle *triangle, RoamSphere *sphere)
        g_pqueue_remove(sphere->triangles, triangle->handle);
 }
 
-static 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();
 }
 
 static gboolean roam_triangle_visible(RoamTriangle *triangle, RoamSphere *sphere)
@@ -337,6 +337,20 @@ static gboolean roam_triangle_visible(RoamTriangle *triangle, RoamSphere *sphere
                 l->pz < 1 && m->pz < 1 && r->pz < 1;
 }
 
+static gboolean roam_triangle_backface(RoamTriangle *triangle, RoamSphere *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;
+}
+
 /**
  * roam_triangle_update_errors:
  * @triangle: the triangle
@@ -374,6 +388,12 @@ void roam_triangle_update_errors(RoamTriangle *triangle, RoamSphere *sphere)
 
                /* Size < 0 == backface */
                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;
        }
 }
 
@@ -414,10 +434,10 @@ void roam_triangle_split(RoamTriangle *triangle, RoamSphere *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(sl, s, s->t.l);
-       roam_triangle_sync_neighbors(sr, s, s->t.r);
-       roam_triangle_sync_neighbors(bl, b, b->t.l);
-       roam_triangle_sync_neighbors(br, b, b->t.r);
+       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(s, sphere);
@@ -553,13 +573,18 @@ void roam_diamond_merge(RoamDiamond *diamond, RoamSphere *sphere)
        b->kids[0] = b->kids[1] = NULL;
 
        /* 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_add(s, sl->t.b, b, sr->t.b, sphere);
        roam_triangle_add(b, bl->t.b, s, br->t.b, sphere);
 
-       roam_triangle_sync_neighbors(s, sl, sl->t.b);
-       roam_triangle_sync_neighbors(s, sr, sr->t.b);
-       roam_triangle_sync_neighbors(b, bl, bl->t.b);
-       roam_triangle_sync_neighbors(b, br, br->t.b);
+       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);
 
        /* Remove child triangles */
        roam_triangle_remove(sl, sphere);