1 |
/*
|
2 |
* This file is a part of Poly2Tri-C
|
3 |
* (c) Barak Itkin <lightningismyname@gmail.com>
|
4 |
* http://code.google.com/p/poly2tri-c/
|
5 |
*
|
6 |
* All rights reserved.
|
7 |
*
|
8 |
* Redistribution and use in source and binary forms, with or without modification,
|
9 |
* are permitted provided that the following conditions are met:
|
10 |
*
|
11 |
* * Redistributions of source code must retain the above copyright notice,
|
12 |
* this list of conditions and the following disclaimer.
|
13 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
14 |
* this list of conditions and the following disclaimer in the documentation
|
15 |
* and/or other materials provided with the distribution.
|
16 |
* * Neither the name of Poly2Tri nor the names of its contributors may be
|
17 |
* used to endorse or promote products derived from this software without specific
|
18 |
* prior written permission.
|
19 |
*
|
20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31 |
*/
|
32 |
|
33 |
#include <glib.h>
|
34 |
|
35 |
#include "point.h"
|
36 |
#include "edge.h"
|
37 |
#include "triangle.h"
|
38 |
#include "mesh.h"
|
39 |
|
40 |
#include "vtriangle.h"
|
41 |
|
42 |
P2trVTriangle*
|
43 |
p2tr_vtriangle_new (P2trTriangle *tri)
|
44 |
{
|
45 |
P2trVTriangle *self = g_slice_new (P2trVTriangle);
|
46 |
|
47 |
self->points[0] = p2tr_point_ref (tri->edges[0]->end);
|
48 |
self->points[1] = p2tr_point_ref (tri->edges[1]->end);
|
49 |
self->points[2] = p2tr_point_ref (tri->edges[2]->end);
|
50 |
|
51 |
self->refcount = 1;
|
52 |
|
53 |
return self;
|
54 |
}
|
55 |
|
56 |
P2trVTriangle*
|
57 |
p2tr_vtriangle_ref (P2trVTriangle *self)
|
58 |
{
|
59 |
++self->refcount;
|
60 |
return self;
|
61 |
}
|
62 |
|
63 |
void
|
64 |
p2tr_vtriangle_unref (P2trVTriangle *self)
|
65 |
{
|
66 |
g_assert (self->refcount > 0);
|
67 |
if (--self->refcount == 0)
|
68 |
p2tr_vtriangle_free (self);
|
69 |
}
|
70 |
|
71 |
void
|
72 |
p2tr_vtriangle_free (P2trVTriangle *self)
|
73 |
{
|
74 |
p2tr_point_unref (self->points[0]);
|
75 |
p2tr_point_unref (self->points[1]);
|
76 |
p2tr_point_unref (self->points[2]);
|
77 |
g_slice_free (P2trVTriangle, self);
|
78 |
}
|
79 |
|
80 |
P2trMesh*
|
81 |
p2tr_vtriangle_get_mesh (P2trVTriangle *self)
|
82 |
{
|
83 |
return p2tr_point_get_mesh (self->points[0]);
|
84 |
}
|
85 |
|
86 |
P2trTriangle*
|
87 |
p2tr_vtriangle_is_real (P2trVTriangle *self)
|
88 |
{
|
89 |
P2trEdge *e0, *e1, *e2;
|
90 |
|
91 |
/* The triangle exists if and only if all the edges
|
92 |
* still exist and they all are a part of the same
|
93 |
* triangle. */
|
94 |
if ((e0 = p2tr_point_has_edge_to (self->points[0], self->points[1])) &&
|
95 |
(e1 = p2tr_point_has_edge_to (self->points[1], self->points[2])) &&
|
96 |
(e2 = p2tr_point_has_edge_to (self->points[2], self->points[0])) &&
|
97 |
e0->tri == e1->tri && e1->tri == e2->tri)
|
98 |
return e0->tri;
|
99 |
else
|
100 |
return NULL;
|
101 |
}
|
102 |
|
103 |
void
|
104 |
p2tr_vtriangle_create (P2trVTriangle *self)
|
105 |
{
|
106 |
P2trMesh *mesh;
|
107 |
P2trEdge *e1, *e2, *e3;
|
108 |
P2trTriangle *tri;
|
109 |
|
110 |
g_assert (! p2tr_vtriangle_is_real (self));
|
111 |
|
112 |
mesh = p2tr_vtriangle_get_mesh (self);
|
113 |
e1 = p2tr_point_get_edge_to (self->points[0], self->points[1], FALSE);
|
114 |
e2 = p2tr_point_get_edge_to (self->points[1], self->points[2], FALSE);
|
115 |
e3 = p2tr_point_get_edge_to (self->points[2], self->points[0], FALSE);
|
116 |
|
117 |
if (mesh != NULL)
|
118 |
{
|
119 |
tri = p2tr_mesh_new_triangle (mesh, e1, e2, e3);
|
120 |
p2tr_mesh_unref (mesh);
|
121 |
}
|
122 |
else
|
123 |
tri = p2tr_triangle_new (e1, e2, e3);
|
124 |
|
125 |
p2tr_triangle_unref (tri);
|
126 |
}
|
127 |
|
128 |
void
|
129 |
p2tr_vtriangle_remove (P2trVTriangle *self)
|
130 |
{
|
131 |
P2trTriangle *tri = p2tr_vtriangle_is_real (self);
|
132 |
|
133 |
g_assert (tri != NULL);
|
134 |
|
135 |
p2tr_triangle_remove (tri);
|
136 |
}
|
137 |
|
138 |
P2trTriangle*
|
139 |
p2tr_vtriangle_get (P2trVTriangle *self)
|
140 |
{
|
141 |
P2trTriangle *real = p2tr_vtriangle_is_real (self);
|
142 |
g_assert (real != NULL);
|
143 |
return p2tr_triangle_ref (real);
|
144 |
}
|
145 |
|