From: Barak Itkin Date: Thu, 26 Apr 2012 21:57:27 +0000 (+0300) Subject: Various small API additions X-Git-Tag: p2tc-0.1.0~97 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=793188d555f4f17cacf97b43d986057efaf93382;p=poly2tri-c Various small API additions --- diff --git a/refine/math.c b/refine/math.c index a2e32d7..4a6cc68 100644 --- a/refine/math.c +++ b/refine/math.c @@ -1,15 +1,11 @@ #include #include "math.h" -#define EDGE_LEN_SQ(X,Y) ((X) * (X) + (Y) * (Y)) -#define VECTOR2_LEN_SQ(V) (EDGE_LEN_SQ((V)->x, (V)->y)) -#define VECTOR2_DOT(V1,V2) ((V1)->x * (V2)->x + (V1)->y * (V2)->y) - gdouble p2tr_math_length_sq (gdouble x1, gdouble y1, gdouble x2, gdouble y2) { - return EDGE_LEN_SQ (x1 - x2, y1 - y2); + return P2TR_VECTOR2_DISTANCE_SQ2 (x1, y1, x2, y2); } gdouble @@ -80,11 +76,11 @@ p2tr_math_triangle_barcycentric (const P2trVector2 *A, p2tr_vector2_sub(P, A, &v2); /* Compute dot products */ - dot00 = VECTOR2_DOT(&v0, &v0); - dot01 = VECTOR2_DOT(&v0, &v1); - dot02 = VECTOR2_DOT(&v0, &v2); - dot11 = VECTOR2_DOT(&v1, &v1); - dot12 = VECTOR2_DOT(&v1, &v2); + dot00 = P2TR_VECTOR2_DOT(&v0, &v0); + dot01 = P2TR_VECTOR2_DOT(&v0, &v1); + dot02 = P2TR_VECTOR2_DOT(&v0, &v2); + dot11 = P2TR_VECTOR2_DOT(&v1, &v1); + dot12 = P2TR_VECTOR2_DOT(&v1, &v2); /* Compute barycentric coordinates */ invDenom = 1 / (dot00 * dot11 - dot01 * dot01); @@ -160,10 +156,10 @@ p2tr_math_incircle (const P2trVector2 *A, * |Dx Dy Dx^2+Dy^2 1| */ gdouble result = p2tr_matrix_det4 ( - A->x, A->y, VECTOR2_LEN_SQ(A), 1, - B->x, B->y, VECTOR2_LEN_SQ(B), 1, - C->x, C->y, VECTOR2_LEN_SQ(C), 1, - D->x, D->y, VECTOR2_LEN_SQ(D), 1 + A->x, A->y, P2TR_VECTOR2_LEN_SQ(A), 1, + B->x, B->y, P2TR_VECTOR2_LEN_SQ(B), 1, + C->x, C->y, P2TR_VECTOR2_LEN_SQ(C), 1, + D->x, D->y, P2TR_VECTOR2_LEN_SQ(D), 1 ); if (result > INCIRCLE_EPSILON) @@ -216,6 +212,6 @@ p2tr_math_diametral_lens_contains (const P2trVector2 *X, p2tr_vector2_sub(X, W, &WX); p2tr_vector2_sub(Y, W, &WY); - return VECTOR2_DOT(&WX, &WY) + return P2TR_VECTOR2_DOT(&WX, &WY) <= 0.5 * p2tr_vector2_norm(&WX) * p2tr_vector2_norm(&WY); } diff --git a/refine/point.c b/refine/point.c index 7d92065..e4890b8 100644 --- a/refine/point.c +++ b/refine/point.c @@ -4,10 +4,17 @@ P2trPoint* p2tr_point_new (const P2trVector2 *c) +{ + return p2tr_point_new2 (c->x, c->y); +} + +P2trPoint* +p2tr_point_new2 (gdouble x, gdouble y) { P2trPoint *self = g_slice_new (P2trPoint); - p2tr_vector2_copy (&self->c, c); + self->c.x = x; + self->c.y = y; self->mesh = NULL; self->outgoing_edges = NULL; self->refcount = 1; @@ -39,12 +46,13 @@ p2tr_point_free (P2trPoint *self) g_slice_free (P2trPoint, self); } -static P2trEdge* -_p2tr_point_existing_edge_to (P2trPoint* self, P2trPoint *end) +P2trEdge* +p2tr_point_has_edge_to (P2trPoint *start, + P2trPoint *end) { GList *iter; - for (iter = self->outgoing_edges; iter != NULL; iter = iter->next) + for (iter = start->outgoing_edges; iter != NULL; iter = iter->next) { P2trEdge *edge = (P2trEdge*) iter->data; if (edge->end == end) @@ -58,7 +66,7 @@ P2trEdge* p2tr_point_get_edge_to (P2trPoint *start, P2trPoint *end) { - P2trEdge* result = _p2tr_point_existing_edge_to (start, end); + P2trEdge* result = p2tr_point_has_edge_to (start, end); if (result == NULL) p2tr_exception_programmatic ("Tried to get an edge that doesn't exist!"); else diff --git a/refine/point.h b/refine/point.h index 81d513a..ad16412 100644 --- a/refine/point.h +++ b/refine/point.h @@ -29,6 +29,8 @@ struct P2trPoint_ P2trPoint* p2tr_point_new (const P2trVector2 *c); +P2trPoint* p2tr_point_new2 (gdouble x, gdouble y); + void p2tr_point_ref (P2trPoint *self); void p2tr_point_unref (P2trPoint *self); diff --git a/refine/vector2.c b/refine/vector2.c index 4853567..45a6b7f 100644 --- a/refine/vector2.c +++ b/refine/vector2.c @@ -6,7 +6,7 @@ gdouble p2tr_vector2_dot (const P2trVector2 *a, const P2trVector2 *b) { - return a->x * b->x + a->y * b->y; + return P2TR_VECTOR2_DOT (a, b); } gboolean @@ -40,7 +40,7 @@ p2tr_vector2_center (const P2trVector2 *a, gdouble p2tr_vector2_norm (const P2trVector2 *v) { - return sqrt (v->x * v->x + v->y * v->y); + return sqrt (P2TR_VECTOR2_LEN_SQ (v)); } void diff --git a/refine/vector2.h b/refine/vector2.h index 8fe7a61..08386ad 100644 --- a/refine/vector2.h +++ b/refine/vector2.h @@ -14,6 +14,21 @@ typedef struct { gdouble y; } P2trVector2; +#define P2TR_VECTOR2_LEN_SQ2(X, Y) \ + ((X) * (X) + (Y) * (Y)) + +#define P2TR_VECTOR2_LEN_SQ(V) \ + (P2TR_VECTOR2_LEN_SQ2((V)->x, (V)->y)) + +#define P2TR_VECTOR2_DOT(V1,V2) \ + ((V1)->x * (V2)->x + (V1)->y * (V2)->y) + +#define P2TR_VECTOR2_DISTANCE_SQ2(X1,Y1,X2,Y2) \ + (P2TR_VECTOR2_LEN_SQ2((X1) - (X2), (Y1) - (Y2))) + +#define P2TR_VECTOR2_DISTANCE_SQ(V1,V2) \ + (P2TR_VECTOR2_DISTANCE_SQ2((V1)->x, (V1)->y, (V2)->x, (V2)->y)) + /** Compute the dot product of two vectors */ gdouble p2tr_vector2_dot (const P2trVector2 *a, const P2trVector2 *b);