From e1022b995f02d80c29bd451c7e1973e81c33e0f3 Mon Sep 17 00:00:00 2001 From: Barak Itkin Date: Sat, 30 Jun 2012 17:45:38 +0300 Subject: [PATCH] Add a virtual triangle struct to represent a triangle that may exist --- poly2tri-c/refine/Makefile.am | 4 +- poly2tri-c/refine/refine.h | 1 + poly2tri-c/refine/triangulation.h | 1 + poly2tri-c/refine/vtriangle.c | 110 ++++++++++++++++++++++++++++++ poly2tri-c/refine/vtriangle.h | 66 ++++++++++++++++++ 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 poly2tri-c/refine/vtriangle.c create mode 100644 poly2tri-c/refine/vtriangle.h diff --git a/poly2tri-c/refine/Makefile.am b/poly2tri-c/refine/Makefile.am index 48a269d..c96f466 100644 --- a/poly2tri-c/refine/Makefile.am +++ b/poly2tri-c/refine/Makefile.am @@ -1,6 +1,6 @@ noinst_LTLIBRARIES = libp2tc-refine.la -libp2tc_refine_la_SOURCES = bounded-line.c bounded-line.h cdt.c cdt.h circle.c circle.h cluster.c cluster.h delaunay-terminator.c delaunay-terminator.h edge.c edge.h line.c line.h rmath.c rmath.h mesh.c mesh.h point.c point.h pslg.c pslg.h refine.h refiner.c refiner.h triangle.c triangle.h triangulation.h utils.c utils.h vector2.c vector2.h vedge.c vedge.h visibility.c visibility.h +libp2tc_refine_la_SOURCES = bounded-line.c bounded-line.h cdt.c cdt.h circle.c circle.h cluster.c cluster.h delaunay-terminator.c delaunay-terminator.h edge.c edge.h line.c line.h rmath.c rmath.h mesh.c mesh.h point.c point.h pslg.c pslg.h refine.h refiner.c refiner.h triangle.c triangle.h triangulation.h utils.c utils.h vector2.c vector2.h vedge.c vedge.h vtriangle.c vtriangle.h visibility.c visibility.h P2TC_REFINE_publicdir = $(P2TC_publicdir)/refine -P2TC_REFINE_public_HEADERS = bounded-line.h cdt.h circle.h cluster.h edge.h line.h mesh.h point.h pslg.h refine.h refiner.h rmath.h triangle.h triangulation.h utils.h vector2.h vedge.h visibility.h +P2TC_REFINE_public_HEADERS = bounded-line.h cdt.h circle.h cluster.h edge.h line.h mesh.h point.h pslg.h refine.h refiner.h rmath.h triangle.h triangulation.h utils.h vector2.h vedge.h vtriangle.h visibility.h diff --git a/poly2tri-c/refine/refine.h b/poly2tri-c/refine/refine.h index 3c94553..6ac2a73 100644 --- a/poly2tri-c/refine/refine.h +++ b/poly2tri-c/refine/refine.h @@ -48,6 +48,7 @@ #include "mesh.h" #include "vedge.h" +#include "vtriangle.h" #include "cluster.h" #include "cdt.h" diff --git a/poly2tri-c/refine/triangulation.h b/poly2tri-c/refine/triangulation.h index 17847ad..3ea264f 100644 --- a/poly2tri-c/refine/triangulation.h +++ b/poly2tri-c/refine/triangulation.h @@ -39,4 +39,5 @@ typedef struct P2trTriangle_ P2trTriangle; typedef struct P2trMesh_ P2trMesh; typedef struct P2trVEdge_ P2trVEdge; +typedef struct P2trVTriangle_ P2trVTriangle; #endif diff --git a/poly2tri-c/refine/vtriangle.c b/poly2tri-c/refine/vtriangle.c new file mode 100644 index 0000000..2666479 --- /dev/null +++ b/poly2tri-c/refine/vtriangle.c @@ -0,0 +1,110 @@ +/* + * This file is a part of Poly2Tri-C + * (c) Barak Itkin + * http://code.google.com/p/poly2tri-c/ + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of Poly2Tri nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * 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 "point.h" +#include "edge.h" +#include "triangle.h" +#include "mesh.h" + +#include "vtriangle.h" + +P2trVTriangle* +p2tr_vtriangle_new (P2trTriangle *tri) +{ + P2trVTriangle *self = g_slice_new (P2trVTriangle); + + self->points[0] = p2tr_point_ref (tri->edges[0]->end); + self->points[1] = p2tr_point_ref (tri->edges[1]->end); + self->points[2] = p2tr_point_ref (tri->edges[2]->end); + + self->refcount = 1; + + return self; +} + +P2trVTriangle* +p2tr_vtriangle_ref (P2trVTriangle *self) +{ + ++self->refcount; + return self; +} + +void +p2tr_vtriangle_unref (P2trVTriangle *self) +{ + g_assert (self->refcount > 0); + if (--self->refcount == 0) + p2tr_vtriangle_free (self); +} + +void +p2tr_vtriangle_free (P2trVTriangle *self) +{ + p2tr_point_unref (self->points[0]); + p2tr_point_unref (self->points[1]); + p2tr_point_unref (self->points[2]); + g_slice_free (P2trVTriangle, self); +} + +P2trMesh* +p2tr_vtriangle_get_mesh (P2trVTriangle *self) +{ + return p2tr_point_get_mesh (self->points[0]); +} + +P2trTriangle* +p2tr_vtriangle_is_real (P2trVTriangle *self) +{ + P2trEdge *e0, *e1, *e2; + + /* The triangle exists if and only if all the edges + * still exist and they all are a part of the same + * triangle. */ + if ((e0 = p2tr_point_has_edge_to (self->points[0], self->points[1])) && + (e1 = p2tr_point_has_edge_to (self->points[1], self->points[2])) && + (e2 = p2tr_point_has_edge_to (self->points[2], self->points[0])) && + e0->tri == e1->tri && e1->tri == e2->tri) + return e0->tri; + else + return NULL; +} + +P2trTriangle* +p2tr_vtriangle_get (P2trVTriangle *self) +{ + P2trTriangle *real = p2tr_vtriangle_is_real (self); + g_assert (real != NULL); + return p2tr_triangle_ref (real); +} + diff --git a/poly2tri-c/refine/vtriangle.h b/poly2tri-c/refine/vtriangle.h new file mode 100644 index 0000000..a1dd0a1 --- /dev/null +++ b/poly2tri-c/refine/vtriangle.h @@ -0,0 +1,66 @@ +/* + * This file is a part of Poly2Tri-C + * (c) Barak Itkin + * http://code.google.com/p/poly2tri-c/ + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of Poly2Tri nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __P2TC_REFINE_VTRIANGLE_H__ +#define __P2TC_REFINE_VTRIANGLE_H__ + +#include +#include "rmath.h" +#include "triangulation.h" + +/** + * @struct P2trVTriangle_ + * A struct for representing a potential ("virtual") triangle + * in a triangular mesh + */ +struct P2trVTriangle_ +{ + P2trPoint* points[3]; + + guint refcount; +}; + +P2trVTriangle* p2tr_vtriangle_new (P2trTriangle *tri); + +P2trVTriangle* p2tr_vtriangle_ref (P2trVTriangle *self); + +void p2tr_vtriangle_unref (P2trVTriangle *self); + +void p2tr_vtriangle_free (P2trVTriangle *self); + +P2trMesh* p2tr_vtriangle_get_mesh (P2trVTriangle *self); + +P2trTriangle* p2tr_vtriangle_is_real (P2trVTriangle *self); + +P2trTriangle* p2tr_vtriangle_get (P2trVTriangle *self); + +#endif -- 2.40.0