From 9c44ea9c58dd420277c455b42d7b6446010a6f70 Mon Sep 17 00:00:00 2001 From: Trammell hudson Date: Wed, 28 Feb 2018 18:02:53 -0500 Subject: [PATCH] prune small triangles --- hiddenwire.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/hiddenwire.c b/hiddenwire.c index 2af8fee..1196b97 100644 --- a/hiddenwire.c +++ b/hiddenwire.c @@ -343,6 +343,35 @@ seg_print( ); } +// Compute the length of a line in screen space, ignoring Z +float +v3_dist_2d( + const v3_t * p0, + const v3_t * p1 +) +{ + const float dx = p1->p[0] - p0->p[0]; + const float dy = p1->p[1] - p0->p[1]; + + return sqrt(dx*dx + dy*dy); +} + + +// Compute the 2D area of a triangle in screen space +// using Heron's formula +float +tri_area_2d( + const tri_t * const t +) +{ + const float a = v3_dist_2d(&t->p[0], &t->p[1]); + const float b = v3_dist_2d(&t->p[1], &t->p[2]); + const float c = v3_dist_2d(&t->p[2], &t->p[0]); + const float s = (a + b + c) / 2; + + return sqrt(s * (s-a) * (s-b) * (s-c)); +} + void tri_print( const tri_t * const t @@ -820,6 +849,10 @@ int main( if (do_backface && tri->normal.p[2] <= 0) goto reject; + // prune the small triangles in the screen space + if (tri_area_2d(tri) < prune) + goto reject; + retained++; // it passes the first tests, so insert the triangle