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 <math.h>
|
34 |
#include <glib.h>
|
35 |
|
36 |
#include "point.h"
|
37 |
#include "edge.h"
|
38 |
#include "vedge.h"
|
39 |
#include "mesh.h"
|
40 |
|
41 |
static void
|
42 |
p2tr_vedge_init (P2trVEdge *self,
|
43 |
P2trPoint *start,
|
44 |
P2trPoint *end,
|
45 |
gboolean constrained)
|
46 |
{
|
47 |
self->start = start;
|
48 |
self->end = end;
|
49 |
self->constrained = constrained;
|
50 |
self->refcount = 0;
|
51 |
}
|
52 |
|
53 |
P2trVEdge*
|
54 |
p2tr_vedge_new (P2trPoint *start,
|
55 |
P2trPoint *end,
|
56 |
gboolean constrained)
|
57 |
{
|
58 |
P2trVEdge *self = g_slice_new (P2trVEdge);
|
59 |
|
60 |
p2tr_vedge_init (self, start, end, constrained);
|
61 |
|
62 |
p2tr_point_ref (start);
|
63 |
p2tr_point_ref (end);
|
64 |
|
65 |
++self->refcount;
|
66 |
return self;
|
67 |
}
|
68 |
|
69 |
P2trVEdge*
|
70 |
p2tr_vedge_new2 (P2trEdge *real)
|
71 |
{
|
72 |
return p2tr_vedge_new (P2TR_EDGE_START (real), real->end,
|
73 |
real->constrained);
|
74 |
}
|
75 |
|
76 |
P2trVEdge*
|
77 |
p2tr_vedge_ref (P2trVEdge *self)
|
78 |
{
|
79 |
++self->refcount;
|
80 |
return self;
|
81 |
}
|
82 |
|
83 |
void
|
84 |
p2tr_vedge_unref (P2trVEdge *self)
|
85 |
{
|
86 |
g_assert (self->refcount > 0);
|
87 |
if (--self->refcount == 0)
|
88 |
p2tr_vedge_free (self);
|
89 |
}
|
90 |
|
91 |
P2trEdge*
|
92 |
p2tr_vedge_is_real (P2trVEdge *self)
|
93 |
{
|
94 |
return p2tr_point_has_edge_to (self->start, self->end);
|
95 |
}
|
96 |
|
97 |
void
|
98 |
p2tr_vedge_create (P2trVEdge *self)
|
99 |
{
|
100 |
P2trMesh *mesh;
|
101 |
P2trEdge *edge;
|
102 |
|
103 |
g_assert (! p2tr_vedge_is_real (self));
|
104 |
|
105 |
mesh = p2tr_vedge_get_mesh (self);
|
106 |
if (mesh != NULL)
|
107 |
{
|
108 |
edge = p2tr_mesh_new_edge (mesh, self->start, self->end, self->constrained);
|
109 |
p2tr_mesh_unref (mesh);
|
110 |
}
|
111 |
else
|
112 |
edge = p2tr_edge_new (self->start, self->end, self->constrained);
|
113 |
|
114 |
p2tr_edge_unref (edge);
|
115 |
}
|
116 |
|
117 |
void
|
118 |
p2tr_vedge_remove (P2trVEdge *self)
|
119 |
{
|
120 |
P2trEdge *edge = p2tr_vedge_is_real (self);
|
121 |
|
122 |
g_assert (edge != NULL);
|
123 |
|
124 |
p2tr_edge_remove (edge);
|
125 |
}
|
126 |
|
127 |
void
|
128 |
p2tr_vedge_free (P2trVEdge *self)
|
129 |
{
|
130 |
p2tr_point_unref (self->start);
|
131 |
p2tr_point_unref (self->end);
|
132 |
g_slice_free (P2trVEdge, self);
|
133 |
}
|
134 |
|
135 |
P2trMesh*
|
136 |
p2tr_vedge_get_mesh (P2trVEdge *self)
|
137 |
{
|
138 |
return p2tr_point_get_mesh (self->end);
|
139 |
}
|
140 |
|
141 |
P2trEdge*
|
142 |
p2tr_vedge_get (P2trVEdge *self)
|
143 |
{
|
144 |
return p2tr_point_get_edge_to (self->start, self->end, TRUE);
|
145 |
}
|
146 |
|
147 |
gboolean
|
148 |
p2tr_vedge_try_get_and_unref (P2trVEdge *self,
|
149 |
P2trEdge **real)
|
150 |
{
|
151 |
P2trEdge *real_one = p2tr_vedge_is_real (self);
|
152 |
if (real_one)
|
153 |
p2tr_edge_ref (real_one);
|
154 |
p2tr_vedge_unref (self);
|
155 |
return (*real = real_one) != NULL;
|
156 |
}
|
157 |
|
158 |
P2trVEdgeSet*
|
159 |
p2tr_vedge_set_new ()
|
160 |
{
|
161 |
return p2tr_hash_set_new (
|
162 |
(GHashFunc) p2tr_vedge_undirected_hash,
|
163 |
(GEqualFunc) p2tr_vedge_undirected_equals,
|
164 |
NULL
|
165 |
);
|
166 |
}
|
167 |
|
168 |
void
|
169 |
p2tr_vedge_set_add (P2trVEdgeSet *self,
|
170 |
P2trEdge *to_flip)
|
171 |
{
|
172 |
p2tr_vedge_set_add2 (self, p2tr_vedge_new2 (to_flip));
|
173 |
p2tr_edge_unref (to_flip);
|
174 |
}
|
175 |
|
176 |
void
|
177 |
p2tr_vedge_set_add2 (P2trVEdgeSet *self,
|
178 |
P2trVEdge *to_flip)
|
179 |
{
|
180 |
if (p2tr_hash_set_contains (self, to_flip))
|
181 |
p2tr_vedge_unref (to_flip);
|
182 |
else
|
183 |
p2tr_hash_set_insert (self, to_flip);
|
184 |
}
|
185 |
|
186 |
gboolean
|
187 |
p2tr_vedge_set_pop (P2trVEdgeSet *self,
|
188 |
P2trVEdge **value)
|
189 |
{
|
190 |
P2trHashSetIter iter;
|
191 |
p2tr_hash_set_iter_init (&iter, self);
|
192 |
if (p2tr_hash_set_iter_next (&iter, (gpointer*) value))
|
193 |
{
|
194 |
p2tr_hash_set_remove (self, *value);
|
195 |
return TRUE;
|
196 |
}
|
197 |
else
|
198 |
return FALSE;
|
199 |
}
|
200 |
|
201 |
guint
|
202 |
p2tr_vedge_set_size (P2trVEdgeSet *self)
|
203 |
{
|
204 |
return p2tr_hash_set_size (self);
|
205 |
}
|
206 |
|
207 |
void
|
208 |
p2tr_vedge_set_free (P2trVEdgeSet *self)
|
209 |
{
|
210 |
g_assert (p2tr_hash_set_size (self) == 0);
|
211 |
p2tr_hash_set_free (self);
|
212 |
}
|
213 |
|
214 |
gboolean
|
215 |
p2tr_vedge_undirected_equals (const P2trVEdge *e1,
|
216 |
const P2trVEdge *e2)
|
217 |
{
|
218 |
return ((e1 == NULL) == (e2 == NULL)) &&
|
219 |
(e1 == e2
|
220 |
|| (e1->start == e2->start && e1->end == e2->end)
|
221 |
|| (e1->end == e2->start && e1->start == e2->end));
|
222 |
}
|
223 |
|
224 |
guint
|
225 |
p2tr_vedge_undirected_hash (const P2trVEdge *edge)
|
226 |
{
|
227 |
return g_direct_hash (edge->start) ^ g_direct_hash (edge->end);
|
228 |
}
|