From: Barak Itkin Date: Sat, 12 May 2012 18:02:55 +0000 (+0300) Subject: Make the C port of the original poly2tri library ANSI C compatiable X-Git-Tag: p2tc-0.1.0~78 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8bb24fd6222152b42c22c49410111df5ee141139;p=poly2tri-c Make the C port of the original poly2tri library ANSI C compatiable --- diff --git a/p2t/common/shapes.c b/p2t/common/shapes.c index f211148..93f2e38 100755 --- a/p2t/common/shapes.c +++ b/p2t/common/shapes.c @@ -36,7 +36,7 @@ #include #include -/// Default constructor does nothing (for performance). +/** Default constructor does nothing (for performance). */ void p2t_point_init (P2tPoint* THIS) @@ -54,7 +54,7 @@ p2t_point_new () return THIS; } -/// Construct using coordinates. +/** Construct using coordinates. */ void p2t_point_init_dd (P2tPoint* THIS, double x, double y) @@ -85,7 +85,7 @@ p2t_point_free (P2tPoint* THIS) g_slice_free (P2tPoint, THIS); } -/// Constructor +/** Constructor */ void p2t_edge_init (P2tEdge* THIS, P2tPoint* p1, P2tPoint* p2) @@ -106,7 +106,7 @@ p2t_edge_init (P2tEdge* THIS, P2tPoint* p1, P2tPoint* p2) } else if (p1->x == p2->x) { - // Repeat points + /* Repeat points */ assert (FALSE); } } @@ -154,7 +154,7 @@ p2t_triangle_init (P2tTriangle* THIS, P2tPoint* a, P2tPoint* b, P2tPoint* c) THIS->interior_ = FALSE; } -// Update neighbor pointers +/* Update neighbor pointers */ void p2t_triangle_mark_neighbor_pt_pt_tr (P2tTriangle* THIS, P2tPoint* p1, P2tPoint* p2, P2tTriangle* t) @@ -169,7 +169,7 @@ p2t_triangle_mark_neighbor_pt_pt_tr (P2tTriangle* THIS, P2tPoint* p1, P2tPoint* assert (0); } -// Exhaustive search to update neighbor pointers +/* Exhaustive search to update neighbor pointers */ void p2t_triangle_mark_neighbor_tr (P2tTriangle* THIS, P2tTriangle *t) @@ -246,15 +246,15 @@ P2tPoint* p2t_triangle_opposite_point (P2tTriangle* THIS, P2tTriangle* t, P2tPoint* p) { P2tPoint *cw = p2t_triangle_point_cw (t, p); - double x = cw->x; + /*double x = cw->x; double y = cw->y; x = p->x; y = p->y; - P2tPoint* ham = p2t_triangle_point_cw (THIS, cw); + P2tPoint* ham = */p2t_triangle_point_cw (THIS, cw); return p2t_triangle_point_cw (THIS, cw); } -// Legalized triangle by rotating clockwise around point(0) +/* Legalized triangle by rotating clockwise around point(0) */ void p2t_triangle_legalize_pt (P2tTriangle* THIS, P2tPoint *point) @@ -264,7 +264,7 @@ p2t_triangle_legalize_pt (P2tTriangle* THIS, P2tPoint *point) THIS->points_[2] = point; } -// Legalize triagnle by rotating clockwise around oPoint +/* Legalize triagnle by rotating clockwise around oPoint */ void p2t_triangle_legalize_pt_pt (P2tTriangle* THIS, P2tPoint *opoint, P2tPoint *npoint) @@ -362,7 +362,7 @@ p2t_triangle_mark_constrained_edge_ed (P2tTriangle* THIS, P2tEdge* edge) p2t_triangle_mark_constrained_edge_pt_pt (THIS, edge->p, edge->q); } -// Mark edge as constrained +/* Mark edge as constrained */ void p2t_triangle_mark_constrained_edge_pt_pt (P2tTriangle* THIS, P2tPoint* p, P2tPoint* q) @@ -381,7 +381,7 @@ p2t_triangle_mark_constrained_edge_pt_pt (P2tTriangle* THIS, P2tPoint* p, P2tPoi } } -// The point counter-clockwise to given point +/* The point counter-clockwise to given point */ P2tPoint* p2t_triangle_point_cw (P2tTriangle* THIS, P2tPoint* point) @@ -401,7 +401,7 @@ p2t_triangle_point_cw (P2tTriangle* THIS, P2tPoint* point) assert (0); } -// The point counter-clockwise to given point +/* The point counter-clockwise to given point */ P2tPoint* p2t_triangle_point_ccw (P2tTriangle* THIS, P2tPoint* point) @@ -421,7 +421,7 @@ p2t_triangle_point_ccw (P2tTriangle* THIS, P2tPoint* point) assert (0); } -// The neighbor clockwise to given point +/* The neighbor clockwise to given point */ P2tTriangle* p2t_triangle_neighbor_cw (P2tTriangle* THIS, P2tPoint* point) @@ -437,7 +437,7 @@ p2t_triangle_neighbor_cw (P2tTriangle* THIS, P2tPoint* point) return THIS->neighbors_[0]; } -// The neighbor counter-clockwise to given point +/* The neighbor counter-clockwise to given point */ P2tTriangle* p2t_triangle_neighbor_ccw (P2tTriangle* THIS, P2tPoint* point) @@ -577,7 +577,7 @@ p2t_triangle_set_delunay_edge_cw (P2tTriangle* THIS, P2tPoint* p, gboolean e) } } -// The neighbor across to given point +/* The neighbor across to given point */ P2tTriangle* p2t_triangle_neighbor_across (P2tTriangle* THIS, P2tPoint* opoint) @@ -601,8 +601,8 @@ p2t_triangle_debug_print (P2tTriangle* THIS) printf ("%f,%f\n", THIS->points_[2]->x, THIS->points_[2]->y); } -// WARNING! the function for sorting a g_ptr_array expects to recieve -// pointers to the pointers (double indirection)! +/* WARNING! the function for sorting a g_ptr_array expects to recieve + * pointers to the pointers (double indirection)! */ gint p2t_point_cmp (gconstpointer a, gconstpointer b) @@ -614,7 +614,7 @@ p2t_point_cmp (gconstpointer a, gconstpointer b) } else if (ap->y == bp->y) { - // Make sure q is point with greater x value + /* Make sure q is point with greater x value */ if (ap->x < bp->x) { return -1; @@ -625,73 +625,13 @@ p2t_point_cmp (gconstpointer a, gconstpointer b) return 1; } -// /// Add two points_ component-wise. -// -// Point operator + (const Point& a, const Point& b) -// { -// return Point (a.x + b.x, a.y + b.y); -// } -// -// /// Subtract two points_ component-wise. -// -// Point operator - (const Point& a, const Point& b) -// { -// return Point (a.x - b.x, a.y - b.y); -// } -// -// /// Multiply point by scalar -// -// Point operator * (double s, const Point& a) -// { -// return Point (s * a.x, s * a.y); -// } - -// gboolean operator == (const Point& a, const Point& b) +/* gboolean operator == (const Point& a, const Point& b) */ gboolean p2t_point_equals (const P2tPoint* a, const P2tPoint* b) { return a->x == b->x && a->y == b->y; } -// -// gboolean operator != (const Point& a, const Point& b) -// { -// return a.x != b.x && a.y != b.y; -// } - -// /// Peform the dot product on two vectors. -// -// double -// Dot (const Point& a, const Point& b) -// { -// return a.x * b.x + a.y * b.y; -// } -// -// /// Perform the cross product on two vectors. In 2D this produces a scalar. -// -// double -// Cross (const Point& a, const Point& b) -// { -// return a.x * b.y - a.y * b.x; -// } -// -// /// Perform the cross product on a point and a scalar. In 2D this produces -// /// a point. -// -// Point -// Cross (const Point& a, double s) -// { -// return Point (s * a.y, -s * a.x); -// } -// -// /// Perform the cross product on a scalar and a point. In 2D this produces -// /// a point. -// -// Point -// Cross (const double s, const Point& a) -// { -// return Point (-s * a.y, s * a.x); -// } P2tPoint* p2t_triangle_get_point (P2tTriangle* THIS, const int index) diff --git a/p2t/common/utils.c b/p2t/common/utils.c index 24ce71b..9b2b5d9 100755 --- a/p2t/common/utils.c +++ b/p2t/common/utils.c @@ -74,17 +74,20 @@ p2t_utils_in_scan_area (P2tPoint* pa, P2tPoint* pb, P2tPoint* pc, P2tPoint* pd) double bdxady = bdx * ady; double oabd = adxbdy - bdxady; + double cdx, cdy; + double cdxady, adxcdy, ocad; + if (oabd <= EPSILON) { return FALSE; } - double cdx = pc->x - pdx; - double cdy = pc->y - pdy; + cdx = pc->x - pdx; + cdy = pc->y - pdy; - double cdxady = cdx * ady; - double adxcdy = adx * cdy; - double ocad = cdxady - adxcdy; + cdxady = cdx * ady; + adxcdy = adx * cdy; + ocad = cdxady - adxcdy; if (ocad <= EPSILON) { diff --git a/p2t/common/utils.h b/p2t/common/utils.h index 18d42b9..014fc16 100755 --- a/p2t/common/utils.h +++ b/p2t/common/utils.h @@ -37,7 +37,7 @@ #include "cutils.h" #include "shapes.h" -#define PI_3div4 (3 * M_PI / 4) +#define PI_3div4 (3 * G_PI / 4) #define EPSILON (1e-6) typedef enum diff --git a/p2t/sweep/advancing_front.c b/p2t/sweep/advancing_front.c index ca3c993..ff3763c 100755 --- a/p2t/sweep/advancing_front.c +++ b/p2t/sweep/advancing_front.c @@ -134,7 +134,7 @@ p2t_advancingfront_locate_node (P2tAdvancingFront *THIS, const double x) P2tNode* p2t_advancingfront_find_search_node (P2tAdvancingFront *THIS, const double x) { - // TODO: implement BST index + /* TODO: implement BST index */ return THIS->search_node_; } @@ -149,7 +149,7 @@ p2t_advancingfront_locate_point (P2tAdvancingFront *THIS, const P2tPoint* point) { if (point != node->point) { - // We might have two nodes with same x value for a short time + /* We might have two nodes with same x value for a short time */ if (point == node->prev->point) { node = node->prev; diff --git a/p2t/sweep/sweep.c b/p2t/sweep/sweep.c index 147191c..3a63399 100755 --- a/p2t/sweep/sweep.c +++ b/p2t/sweep/sweep.c @@ -29,6 +29,8 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include "sweep.h" #include "sweep_context.h" #include "advancing_front.h" @@ -56,7 +58,7 @@ void p2t_sweep_destroy (P2tSweep* THIS) { int i; - // Clean up memory + /* Clean up memory */ for (i = 0; i < THIS->nodes_->len; i++) { p2t_node_free (node_index (THIS->nodes_, i)); @@ -72,16 +74,16 @@ p2t_sweep_free (P2tSweep* THIS) g_free (THIS); } -// Triangulate simple polygon with holes +/* Triangulate simple polygon with holes */ void p2t_sweep_triangulate (P2tSweep *THIS, P2tSweepContext *tcx) { p2t_sweepcontext_init_triangulation (tcx); p2t_sweepcontext_create_advancingfront (tcx, THIS->nodes_); - // Sweep points; build mesh + /* Sweep points; build mesh */ p2t_sweep_sweep_points (THIS, tcx); - // Clean up + /* Clean up */ p2t_sweep_finalization_polygon (THIS, tcx); } @@ -103,7 +105,7 @@ p2t_sweep_sweep_points (P2tSweep *THIS, P2tSweepContext *tcx) void p2t_sweep_finalization_polygon (P2tSweep *THIS, P2tSweepContext *tcx) { - // Get an Internal triangle to start with + /* Get an Internal triangle to start with */ P2tTriangle* t = p2t_advancingfront_head (p2t_sweepcontext_front (tcx))->next->triangle; P2tPoint* p = p2t_advancingfront_head (p2t_sweepcontext_front (tcx))->next->point; while (!p2t_triangle_get_constrained_edge_cw (t, p)) @@ -111,7 +113,7 @@ p2t_sweep_finalization_polygon (P2tSweep *THIS, P2tSweepContext *tcx) t = p2t_triangle_neighbor_ccw (t, p); } - // Collect interior triangles constrained by edges + /* Collect interior triangles constrained by edges */ p2t_sweepcontext_mesh_clean (tcx, t); } @@ -121,14 +123,14 @@ p2t_sweep_point_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* point) P2tNode* node = p2t_sweepcontext_locate_node (tcx, point); P2tNode* new_node = p2t_sweep_new_front_triangle (THIS, tcx, point, node); - // Only need to check +epsilon since point never have smaller - // x value than node due to how we fetch nodes from the front + /* Only need to check +epsilon since point never have smaller + * x value than node due to how we fetch nodes from the front */ if (point->x <= node->point->x + EPSILON) { p2t_sweep_fill (THIS, tcx, node); } - //tcx.AddNode(new_node); + /*tcx.AddNode(new_node); */ p2t_sweep_fill_advancingfront (THIS, tcx, new_node); return new_node; @@ -145,9 +147,10 @@ p2t_sweep_edge_event_ed_n (P2tSweep *THIS, P2tSweepContext *tcx, P2tEdge* edge, return; } - // For now we will do all needed filling - // TODO: integrate with flip process might give some better performance - // but for now this avoid the issue with cases that needs both flips and fills + /* For now we will do all needed filling + * TODO: integrate with flip process might give some better performance + * but for now this avoid the issue with cases that needs both flips and fills + */ p2t_sweep_fill_edge_event (THIS, tcx, edge, node); p2t_sweep_edge_event_pt_pt_tr_pt (THIS, tcx, edge->p, edge->q, node->triangle, edge->q); } @@ -155,20 +158,24 @@ p2t_sweep_edge_event_ed_n (P2tSweep *THIS, P2tSweepContext *tcx, P2tEdge* edge, void p2t_sweep_edge_event_pt_pt_tr_pt (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* ep, P2tPoint* eq, P2tTriangle* triangle, P2tPoint* point) { + P2tPoint *p1, *p2; + P2tOrientation o1, o2; + if (p2t_sweep_is_edge_side_of_triangle (THIS, triangle, ep, eq)) { return; } - P2tPoint* p1 = p2t_triangle_point_ccw (triangle, point); - P2tOrientation o1 = p2t_orient2d (eq, p1, ep); + p1 = p2t_triangle_point_ccw (triangle, point); + o1 = p2t_orient2d (eq, p1, ep); if (o1 == COLLINEAR) { if (p2t_triangle_contains_pt_pt (triangle, eq, p1)) { p2t_triangle_mark_constrained_edge_pt_pt (triangle, eq, p1); - // We are modifying the constraint maybe it would be better to - // not change the given constraint and just keep a variable for the new constraint + /* We are modifying the constraint maybe it would be better to + * not change the given constraint and just keep a variable for the new constraint + */ tcx->edge_event.constrained_edge->q = p1; triangle = p2t_triangle_neighbor_across (triangle, point); p2t_sweep_edge_event_pt_pt_tr_pt (THIS, tcx, ep, p1, triangle, p1); @@ -180,15 +187,16 @@ p2t_sweep_edge_event_pt_pt_tr_pt (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint return; } - P2tPoint* p2 = p2t_triangle_point_cw (triangle, point); - P2tOrientation o2 = p2t_orient2d (eq, p2, ep); + p2 = p2t_triangle_point_cw (triangle, point); + o2 = p2t_orient2d (eq, p2, ep); if (o2 == COLLINEAR) { if (p2t_triangle_contains_pt_pt (triangle, eq, p2)) { p2t_triangle_mark_constrained_edge_pt_pt (triangle, eq, p2); - // We are modifying the constraint maybe it would be better to - // not change the given constraint and just keep a variable for the new constraint + /* We are modifying the constraint maybe it would be better to + * not change the given constraint and just keep a variable for the new constraint + */ tcx->edge_event.constrained_edge->q = p2; triangle = p2t_triangle_neighbor_across (triangle, point); p2t_sweep_edge_event_pt_pt_tr_pt (THIS, tcx, ep, p2, triangle, p2); @@ -202,8 +210,8 @@ p2t_sweep_edge_event_pt_pt_tr_pt (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint if (o1 == o2) { - // Need to decide if we are rotating CW or CCW to get to a triangle - // that will cross edge + /* Need to decide if we are rotating CW or CCW to get to a triangle + * that will cross edge */ if (o1 == CW) { triangle = p2t_triangle_neighbor_ccw (triangle, point); @@ -216,7 +224,7 @@ p2t_sweep_edge_event_pt_pt_tr_pt (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint } else { - // This triangle crosses constraint so lets flippin start! + /* This triangle crosses constraint so lets flippin start! */ p2t_sweep_flip_edge_event (THIS, tcx, ep, eq, triangle, point); } } @@ -228,8 +236,9 @@ p2t_sweep_is_edge_side_of_triangle (P2tSweep *THIS, P2tTriangle *triangle, P2tPo if (index != -1) { + P2tTriangle *t; p2t_triangle_mark_constrained_edge_i (triangle, index); - P2tTriangle* t = p2t_triangle_get_neighbor (triangle, index); + t = p2t_triangle_get_neighbor (triangle, index); if (t) { p2t_triangle_mark_constrained_edge_pt_pt (t, ep, eq); @@ -243,11 +252,12 @@ P2tNode* p2t_sweep_new_front_triangle (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* point, P2tNode *node) { P2tTriangle* triangle = p2t_triangle_new (point, node->point, node->next->point); + P2tNode *new_node; p2t_triangle_mark_neighbor_tr (triangle, node->triangle); p2t_sweepcontext_add_to_map (tcx, triangle); - P2tNode* new_node = p2t_node_new_pt (point); + new_node = p2t_node_new_pt (point); g_ptr_array_add (THIS->nodes_, new_node); new_node->next = node->next; @@ -268,18 +278,18 @@ p2t_sweep_fill (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) { P2tTriangle* triangle = p2t_triangle_new (node->prev->point, node->point, node->next->point); - // TODO: should copy the constrained_edge value from neighbor triangles - // for now constrained_edge values are copied during the legalize + /* TODO: should copy the constrained_edge value from neighbor triangles + * for now constrained_edge values are copied during the legalize */ p2t_triangle_mark_neighbor_tr (triangle, node->prev->triangle); p2t_triangle_mark_neighbor_tr (triangle, node->triangle); p2t_sweepcontext_add_to_map (tcx, triangle); - // Update the advancing front + /* Update the advancing front */ node->prev->next = node->next; node->next->prev = node->prev; - // If it was legalized the triangle has already been mapped + /* If it was legalized the triangle has already been mapped */ if (!p2t_sweep_legalize (THIS, tcx, triangle)) { p2t_sweepcontext_map_triangle_to_nodes (tcx, triangle); @@ -291,29 +301,29 @@ void p2t_sweep_fill_advancingfront (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* n) { - // Fill right holes + /* Fill right holes */ P2tNode* node = n->next; while (node->next) { double angle = p2t_sweep_hole_angle (THIS, node); - if (angle > M_PI_2 || angle < -M_PI_2) break; + if (angle > G_PI_2 || angle < -G_PI_2) break; p2t_sweep_fill (THIS, tcx, node); node = node->next; } - // Fill left holes + /* Fill left holes */ node = n->prev; while (node->prev) { double angle = p2t_sweep_hole_angle (THIS, node); - if (angle > M_PI_2 || angle < -M_PI_2) break; + if (angle > G_PI_2 || angle < -G_PI_2) break; p2t_sweep_fill (THIS, tcx, node); node = node->prev; } - // Fill right basins + /* Fill right basins */ if (n->next && n->next->next) { double angle = p2t_sweep_basin_angle (THIS, n); @@ -354,45 +364,49 @@ gboolean p2t_sweep_legalize (P2tSweep *THIS, P2tSweepContext *tcx, P2tTriangle *t) { int i; - // To legalize a triangle we start by finding if any of the three edges - // violate the Delaunay condition + /* To legalize a triangle we start by finding if any of the three edges + * violate the Delaunay condition */ for (i = 0; i < 3; i++) { + P2tTriangle *ot; + if (t->delaunay_edge[i]) continue; - P2tTriangle* ot = p2t_triangle_get_neighbor (t, i); + ot = p2t_triangle_get_neighbor (t, i); if (ot) { P2tPoint* p = p2t_triangle_get_point (t, i); P2tPoint* op = p2t_triangle_opposite_point (ot, t, p); int oi = p2t_triangle_index (ot, op); + gboolean inside; - // If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization) - // then we should not try to legalize + /* If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization) + * then we should not try to legalize */ if (ot->constrained_edge[oi] || ot->delaunay_edge[oi]) { t->constrained_edge[i] = ot->constrained_edge[oi]; continue; } - gboolean inside = p2t_sweep_incircle (THIS, p, p2t_triangle_point_ccw (t, p), p2t_triangle_point_cw (t, p), op); + inside = p2t_sweep_incircle (THIS, p, p2t_triangle_point_ccw (t, p), p2t_triangle_point_cw (t, p), op); if (inside) { - // Lets mark this shared edge as Delaunay + gboolean not_legalized; + /* Lets mark this shared edge as Delaunay */ t->delaunay_edge[i] = TRUE; ot->delaunay_edge[oi] = TRUE; - // Lets rotate shared edge one vertex CW to legalize it + /* Lets rotate shared edge one vertex CW to legalize it */ p2t_sweep_rotate_triangle_pair (THIS, t, p, ot, op); - // We now got one valid Delaunay Edge shared by two triangles - // This gives us 4 new edges to check for Delaunay + /* We now got one valid Delaunay Edge shared by two triangles + * This gives us 4 new edges to check for Delaunay */ - // Make sure that triangle to node mapping is done only one time for a specific triangle - gboolean not_legalized = !p2t_sweep_legalize (THIS, tcx, t); + /* Make sure that triangle to node mapping is done only one time for a specific triangle */ + not_legalized = !p2t_sweep_legalize (THIS, tcx, t); if (not_legalized) { p2t_sweepcontext_map_triangle_to_nodes (tcx, t); @@ -402,15 +416,15 @@ p2t_sweep_legalize (P2tSweep *THIS, P2tSweepContext *tcx, P2tTriangle *t) if (not_legalized) p2t_sweepcontext_map_triangle_to_nodes (tcx, ot); - // Reset the Delaunay edges, since they only are valid Delaunay edges - // until we add a new triangle or point. - // XXX: need to think about this. Can these edges be tried after we - // return to previous recursive level? + /* Reset the Delaunay edges, since they only are valid Delaunay edges + * until we add a new triangle or point. + * XXX: need to think about this. Can these edges be tried after we + * return to previous recursive level? */ t->delaunay_edge[i] = FALSE; ot->delaunay_edge[oi] = FALSE; - // If triangle have been legalized no need to check the other edges since - // the recursive legalization will handles those so we can end here. + /* If triangle have been legalized no need to check the other edges since + * the recursive legalization will handles those so we can end here.*/ return TRUE; } } @@ -430,27 +444,34 @@ p2t_sweep_incircle (P2tSweep *THIS, P2tPoint* pa, P2tPoint* pb, P2tPoint* pc, P2 double bdxady = bdx * ady; double oabd = adxbdy - bdxady; + double cdx, cdy; + double cdxady, adxcdy, ocad; + + double bdxcdy, cdxbdy; + double alift, blift, clift; + double det; + if (oabd <= 0) return FALSE; - double cdx = pc->x - pd->x; - double cdy = pc->y - pd->y; + cdx = pc->x - pd->x; + cdy = pc->y - pd->y; - double cdxady = cdx * ady; - double adxcdy = adx * cdy; - double ocad = cdxady - adxcdy; + cdxady = cdx * ady; + adxcdy = adx * cdy; + ocad = cdxady - adxcdy; if (ocad <= 0) return FALSE; - double bdxcdy = bdx * cdy; - double cdxbdy = cdx * bdy; + bdxcdy = bdx * cdy; + cdxbdy = cdx * bdy; - double alift = adx * adx + ady * ady; - double blift = bdx * bdx + bdy * bdy; - double clift = cdx * cdx + cdy * cdy; + alift = adx * adx + ady * ady; + blift = bdx * bdx + bdy * bdy; + clift = cdx * cdx + cdy * cdy; - double det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd; + det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd; return det > 0; } @@ -458,19 +479,20 @@ p2t_sweep_incircle (P2tSweep *THIS, P2tPoint* pa, P2tPoint* pb, P2tPoint* pc, P2 void p2t_sweep_rotate_triangle_pair (P2tSweep *THIS, P2tTriangle *t, P2tPoint* p, P2tTriangle *ot, P2tPoint* op) { - P2tTriangle* n1, *n2, *n3, *n4; + P2tTriangle *n1, *n2, *n3, *n4; + gboolean ce1, ce2, ce3, ce4; + gboolean de1, de2, de3, de4; + n1 = p2t_triangle_neighbor_ccw (t, p); n2 = p2t_triangle_neighbor_cw (t, p); n3 = p2t_triangle_neighbor_ccw (ot, op); n4 = p2t_triangle_neighbor_cw (ot, op); - gboolean ce1, ce2, ce3, ce4; ce1 = p2t_triangle_get_constrained_edge_ccw (t, p); ce2 = p2t_triangle_get_constrained_edge_cw (t, p); ce3 = p2t_triangle_get_constrained_edge_ccw (ot, op); ce4 = p2t_triangle_get_constrained_edge_cw (ot, op); - gboolean de1, de2, de3, de4; de1 = p2t_triangle_get_delunay_edge_ccw (t, p); de2 = p2t_triangle_get_delunay_edge_cw (t, p); de3 = p2t_triangle_get_delunay_edge_ccw (ot, op); @@ -479,23 +501,23 @@ p2t_sweep_rotate_triangle_pair (P2tSweep *THIS, P2tTriangle *t, P2tPoint* p, P2t p2t_triangle_legalize_pt_pt (t, p, op); p2t_triangle_legalize_pt_pt (ot, op, p); - // Remap delaunay_edge + /* Remap delaunay_edge */ p2t_triangle_set_delunay_edge_ccw (ot, p, de1); p2t_triangle_set_delunay_edge_cw (t, p, de2); p2t_triangle_set_delunay_edge_ccw (t, op, de3); p2t_triangle_set_delunay_edge_cw (ot, op, de4); - // Remap constrained_edge + /* Remap constrained_edge */ p2t_triangle_set_constrained_edge_ccw (ot, p, ce1); p2t_triangle_set_constrained_edge_cw (t, p, ce2); p2t_triangle_set_constrained_edge_ccw (t, op, ce3); p2t_triangle_set_constrained_edge_cw (ot, op, ce4); - // Remap neighbors - // XXX: might optimize the markNeighbor by keeping track of - // what side should be assigned to what neighbor after the - // rotation. Now mark neighbor does lots of testing to find - // the right side. + /* Remap neighbors + * XXX: might optimize the markNeighbor by keeping track of + * what side should be assigned to what neighbor after the + * rotation. Now mark neighbor does lots of testing to find + * the right side. */ p2t_triangle_clear_neighbors (t); p2t_triangle_clear_neighbors (ot); if (n1) p2t_triangle_mark_neighbor_tr (ot, n1); @@ -517,7 +539,7 @@ p2t_sweep_fill_basin (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) tcx->basin.left_node = node->next; } - // Find the bottom and right node + /* Find the bottom and right node */ tcx->basin.bottom_node = tcx->basin.left_node; while (tcx->basin.bottom_node->next && tcx->basin.bottom_node->point->y >= tcx->basin.bottom_node->next->point->y) @@ -526,7 +548,7 @@ p2t_sweep_fill_basin (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) } if (tcx->basin.bottom_node == tcx->basin.left_node) { - // No valid basin + /* No valid basin */ return; } @@ -538,7 +560,7 @@ p2t_sweep_fill_basin (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) } if (tcx->basin.right_node == tcx->basin.bottom_node) { - // No valid basins + /* No valid basins */ return; } @@ -551,7 +573,7 @@ p2t_sweep_fill_basin (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) void p2t_sweep_fill_basin_req (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) { - // if shallow stop filling + /* if shallow stop filling */ if (p2t_sweep_is_shallow (THIS, tcx, node)) { return; @@ -583,7 +605,7 @@ p2t_sweep_fill_basin_req (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) } else { - // Continue with the neighbor node with lowest Y value + /* Continue with the neighbor node with lowest Y value */ if (node->prev->point->y < node->next->point->y) { node = node->prev; @@ -611,7 +633,7 @@ p2t_sweep_is_shallow (P2tSweep *THIS, P2tSweepContext *tcx, P2tNode* node) height = tcx->basin.right_node->point->y - node->point->y; } - // if shallow stop filling + /* if shallow stop filling */ if (tcx->basin.width > height) { return TRUE; @@ -637,7 +659,7 @@ p2t_sweep_fill_right_above_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2t { while (node->next->point->x < edge->p->x) { - // Check if next node is below the edge + /* Check if next node is below the edge */ if (p2t_orient2d (edge->q, node->next->point, edge->p) == CCW) { p2t_sweep_fill_right_below_edge_event (THIS, tcx, edge, node); @@ -656,14 +678,14 @@ p2t_sweep_fill_right_below_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2t { if (p2t_orient2d (node->point, node->next->point, node->next->next->point) == CCW) { - // Concave + /* Concave */ p2t_sweep_fill_right_concave_edge_event (THIS, tcx, edge, node); } else { - // Convex + /* Convex */ p2t_sweep_fill_right_convex_edge_event (THIS, tcx, edge, node); - // Retry this one + /* Retry this one */ p2t_sweep_fill_right_below_edge_event (THIS, tcx, edge, node); } } @@ -675,18 +697,18 @@ p2t_sweep_fill_right_concave_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P p2t_sweep_fill (THIS, tcx, node->next); if (node->next->point != edge->p) { - // Next above or below edge? + /* Next above or below edge? */ if (p2t_orient2d (edge->q, node->next->point, edge->p) == CCW) { - // Below + /* Below */ if (p2t_orient2d (node->point, node->next->point, node->next->next->point) == CCW) { - // Next is concave + /* Next is concave */ p2t_sweep_fill_right_concave_edge_event (THIS, tcx, edge, node); } else { - // Next is convex + /* Next is convex */ } } } @@ -696,24 +718,24 @@ p2t_sweep_fill_right_concave_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P void p2t_sweep_fill_right_convex_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tEdge* edge, P2tNode* node) { - // Next concave or convex? + /* Next concave or convex? */ if (p2t_orient2d (node->next->point, node->next->next->point, node->next->next->next->point) == CCW) { - // Concave + /* Concave */ p2t_sweep_fill_right_concave_edge_event (THIS, tcx, edge, node->next); } else { - // Convex - // Next above or below edge? + /* Convex + * Next above or below edge? */ if (p2t_orient2d (edge->q, node->next->next->point, edge->p) == CCW) { - // Below + /* Below */ p2t_sweep_fill_right_convex_edge_event (THIS, tcx, edge, node->next); } else { - // Above + /* Above */ } } } @@ -723,7 +745,7 @@ p2t_sweep_fill_left_above_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tE { while (node->prev->point->x > edge->p->x) { - // Check if next node is below the edge + /* Check if next node is below the edge */ if (p2t_orient2d (edge->q, node->prev->point, edge->p) == CW) { p2t_sweep_fill_left_below_edge_event (THIS, tcx, edge, node); @@ -742,14 +764,14 @@ p2t_sweep_fill_left_below_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tE { if (p2t_orient2d (node->point, node->prev->point, node->prev->prev->point) == CW) { - // Concave + /* Concave */ p2t_sweep_fill_left_concave_edge_event (THIS, tcx, edge, node); } else { - // Convex + /* Convex */ p2t_sweep_fill_left_convex_edge_event (THIS, tcx, edge, node); - // Retry this one + /* Retry this one */ p2t_sweep_fill_left_below_edge_event (THIS, tcx, edge, node); } } @@ -758,24 +780,24 @@ p2t_sweep_fill_left_below_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tE void p2t_sweep_fill_left_convex_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tEdge* edge, P2tNode* node) { - // Next concave or convex? + /* Next concave or convex? */ if (p2t_orient2d (node->prev->point, node->prev->prev->point, node->prev->prev->prev->point) == CW) { - // Concave + /* Concave */ p2t_sweep_fill_left_concave_edge_event (THIS, tcx, edge, node->prev); } else { - // Convex - // Next above or below edge? + /* Convex + * Next above or below edge? */ if (p2t_orient2d (edge->q, node->prev->prev->point, edge->p) == CW) { - // Below + /* Below */ p2t_sweep_fill_left_convex_edge_event (THIS, tcx, edge, node->prev); } else { - // Above + /* Above */ } } } @@ -786,18 +808,18 @@ p2t_sweep_fill_left_concave_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2 p2t_sweep_fill (THIS, tcx, node->prev); if (node->prev->point != edge->p) { - // Next above or below edge? + /* Next above or below edge? */ if (p2t_orient2d (edge->q, node->prev->point, edge->p) == CW) { - // Below + /* Below */ if (p2t_orient2d (node->point, node->prev->point, node->prev->prev->point) == CW) { - // Next is concave + /* Next is concave */ p2t_sweep_fill_left_concave_edge_event (THIS, tcx, edge, node); } else { - // Next is convex + /* Next is convex */ } } } @@ -812,15 +834,16 @@ p2t_sweep_flip_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* ep, P if (ot == NULL) { - // If we want to integrate the fillEdgeEvent do it here - // With current implementation we should never get here - //throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle"); + /* If we want to integrate the fillEdgeEvent do it here + * With current implementation we should never get here + *throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle"); + */ assert (0); } if (p2t_utils_in_scan_area (p, p2t_triangle_point_ccw (t, p), p2t_triangle_point_cw (t, p), op)) { - // Lets rotate shared edge one vertex CW + /* Lets rotate shared edge one vertex CW */ p2t_sweep_rotate_triangle_pair (THIS, t, p, ot, op); p2t_sweepcontext_map_triangle_to_nodes (tcx, t); p2t_sweepcontext_map_triangle_to_nodes (tcx, ot); @@ -836,7 +859,7 @@ p2t_sweep_flip_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* ep, P } else { - // XXX: I think one of the triangles should be legalized here? + /* XXX: I think one of the triangles should be legalized here? */ } } else @@ -857,9 +880,11 @@ p2t_sweep_flip_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* ep, P P2tTriangle* p2t_sweep_next_flip_triangle (P2tSweep *THIS, P2tSweepContext *tcx, int o, P2tTriangle *t, P2tTriangle *ot, P2tPoint* p, P2tPoint* op) { + int edge_index; + if (o == CCW) { - // ot is not crossing edge after flip + /* ot is not crossing edge after flip */ int edge_index = p2t_triangle_edge_index (ot, p, op); ot->delaunay_edge[edge_index] = TRUE; p2t_sweep_legalize (THIS, tcx, ot); @@ -867,8 +892,8 @@ p2t_sweep_next_flip_triangle (P2tSweep *THIS, P2tSweepContext *tcx, int o, P2tTr return t; } - // t is not crossing edge after flip - int edge_index = p2t_triangle_edge_index (t, p, op); + /* t is not crossing edge after flip */ + edge_index = p2t_triangle_edge_index (t, p, op); t->delaunay_edge[edge_index] = TRUE; p2t_sweep_legalize (THIS, tcx, t); @@ -882,17 +907,17 @@ p2t_sweep_next_flip_point (P2tSweep *THIS, P2tPoint* ep, P2tPoint* eq, P2tTriang P2tOrientation o2d = p2t_orient2d (eq, op, ep); if (o2d == CW) { - // Right + /* Right */ return p2t_triangle_point_ccw (ot, op); } else if (o2d == CCW) { - // Left + /* Left */ return p2t_triangle_point_cw (ot, op); } else { - //throw new RuntimeException("[Unsupported] Opposing point on constrained edge"); + /*throw new RuntimeException("[Unsupported] Opposing point on constrained edge");*/ assert (0); } } @@ -906,23 +931,24 @@ p2t_sweep_flip_scan_edge_event (P2tSweep *THIS, P2tSweepContext *tcx, P2tPoint* if (p2t_triangle_neighbor_across (t, p) == NULL) { - // If we want to integrate the fillEdgeEvent do it here - // With current implementation we should never get here - //throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle"); + /* If we want to integrate the fillEdgeEvent do it here + * With current implementation we should never get here + *throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle"); + */ assert (0); } if (p2t_utils_in_scan_area (eq, p2t_triangle_point_ccw (flip_triangle, eq), p2t_triangle_point_cw (flip_triangle, eq), op)) { - // flip with new edge op->eq + /* flip with new edge op->eq */ p2t_sweep_flip_edge_event (THIS, tcx, eq, op, ot, op); - // TODO: Actually I just figured out that it should be possible to - // improve this by getting the next ot and op before the the above - // flip and continue the flipScanEdgeEvent here - // set new ot and op here and loop back to inScanArea test - // also need to set a new flip_triangle first - // Turns out at first glance that this is somewhat complicated - // so it will have to wait. + /* TODO: Actually I just figured out that it should be possible to + * improve this by getting the next ot and op before the the above + * flip and continue the flipScanEdgeEvent here + * set new ot and op here and loop back to inScanArea test + * also need to set a new flip_triangle first + * Turns out at first glance that this is somewhat complicated + * so it will have to wait. */ } else { diff --git a/p2t/sweep/sweep_context.c b/p2t/sweep/sweep_context.c index 0c827c0..63b5618 100755 --- a/p2t/sweep/sweep_context.c +++ b/p2t/sweep/sweep_context.c @@ -85,7 +85,7 @@ p2t_sweepcontext_destroy (P2tSweepContext* THIS) { GList* iter; int i; - // Clean up memory + /* Clean up memory */ p2t_point_free (THIS->head_); p2t_point_free (THIS->tail_); @@ -155,8 +155,9 @@ p2t_sweepcontext_init_triangulation (P2tSweepContext *THIS) int i; double xmax = point_index (THIS->points_, 0)->x, xmin = point_index (THIS->points_, 0)->x; double ymax = point_index (THIS->points_, 0)->y, ymin = point_index (THIS->points_, 0)->y; + double dx, dy; - // Calculate bounds. + /* Calculate bounds. */ for (i = 0; i < THIS->points_->len; i++) { P2tPoint* p = point_index (THIS->points_, i); @@ -170,12 +171,12 @@ p2t_sweepcontext_init_triangulation (P2tSweepContext *THIS) ymin = p->y; } - double dx = kAlpha * (xmax - xmin); - double dy = kAlpha * (ymax - ymin); + dx = kAlpha * (xmax - xmin); + dy = kAlpha * (ymax - ymin); THIS->head_ = p2t_point_new_dd (xmax + dx, ymin - dy); THIS->tail_ = p2t_point_new_dd (xmin - dx, ymin - dy); - // Sort points along y-axis + /* Sort points along y-axis */ g_ptr_array_sort (THIS->points_, p2t_point_cmp); } @@ -184,7 +185,7 @@ p2t_sweepcontext_init_edges (P2tSweepContext *THIS, P2tPointPtrArray polyline) { int i; int num_points = polyline->len; - g_ptr_array_set_size (THIS->edge_list, THIS->edge_list->len + num_points); // C-OPTIMIZATION + g_ptr_array_set_size (THIS->edge_list, THIS->edge_list->len + num_points); /* C-OPTIMIZATION */ for (i = 0; i < num_points; i++) { int j = i < num_points - 1 ? i + 1 : 0; @@ -207,15 +208,14 @@ p2t_sweepcontext_add_to_map (P2tSweepContext *THIS, P2tTriangle* triangle) P2tNode* p2t_sweepcontext_locate_node (P2tSweepContext *THIS, P2tPoint* point) { - // TODO implement search tree + /* TODO implement search tree */ return p2t_advancingfront_locate_node (THIS->front_, point->x); } void p2t_sweepcontext_create_advancingfront (P2tSweepContext *THIS, P2tNodePtrArray nodes) { - - // Initial triangle + /* Initial triangle */ P2tTriangle* triangle = p2t_triangle_new (point_index (THIS->points_, 0), THIS->tail_, THIS->head_); THIS->map_ = g_list_append (THIS->map_, triangle); @@ -225,8 +225,8 @@ p2t_sweepcontext_create_advancingfront (P2tSweepContext *THIS, P2tNodePtrArray n THIS->af_tail_ = p2t_node_new_pt (p2t_triangle_get_point (triangle, 2)); THIS->front_ = p2t_advancingfront_new (THIS->af_head_, THIS->af_tail_); - // TODO: More intuitive if head is middles next and not previous? - // so swap head and tail + /* TODO: More intuitiv if head is middles next and not previous? + * so swap head and tail */ THIS->af_head_->next = THIS->af_middle_; THIS->af_middle_->next = THIS->af_tail_; THIS->af_middle_->prev = THIS->af_head_;