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 |
#include "point.h"
|
35 |
#include "edge.h"
|
36 |
#include "mesh.h"
|
37 |
|
38 |
P2trPoint*
|
39 |
p2tr_point_new (const P2trVector2 *c)
|
40 |
{
|
41 |
return p2tr_point_new2 (c->x, c->y);
|
42 |
}
|
43 |
|
44 |
P2trPoint*
|
45 |
p2tr_point_new2 (gdouble x, gdouble y)
|
46 |
{
|
47 |
P2trPoint *self = g_slice_new (P2trPoint);
|
48 |
|
49 |
self->c.x = x;
|
50 |
self->c.y = y;
|
51 |
self->mesh = NULL;
|
52 |
self->outgoing_edges = NULL;
|
53 |
self->refcount = 1;
|
54 |
|
55 |
return self;
|
56 |
}
|
57 |
|
58 |
void
|
59 |
p2tr_point_remove (P2trPoint *self)
|
60 |
{
|
61 |
/* We can not iterate over the list of edges while removing the edges,
|
62 |
* because the removal action will modify the list. Instead we will
|
63 |
* simply look at the first edge untill the list is emptied. */
|
64 |
while (self->outgoing_edges != NULL)
|
65 |
p2tr_edge_remove ((P2trEdge*) self->outgoing_edges->data);
|
66 |
|
67 |
if (self->mesh != NULL)
|
68 |
p2tr_mesh_on_point_removed (self->mesh, self);
|
69 |
}
|
70 |
|
71 |
void
|
72 |
p2tr_point_free (P2trPoint *self)
|
73 |
{
|
74 |
p2tr_point_remove (self);
|
75 |
g_slice_free (P2trPoint, self);
|
76 |
}
|
77 |
|
78 |
P2trEdge*
|
79 |
p2tr_point_has_edge_to (P2trPoint *start,
|
80 |
P2trPoint *end)
|
81 |
{
|
82 |
GList *iter;
|
83 |
|
84 |
for (iter = start->outgoing_edges; iter != NULL; iter = iter->next)
|
85 |
{
|
86 |
P2trEdge *edge = (P2trEdge*) iter->data;
|
87 |
if (edge->end == end)
|
88 |
return edge;
|
89 |
}
|
90 |
|
91 |
return NULL;
|
92 |
}
|
93 |
|
94 |
P2trEdge*
|
95 |
p2tr_point_get_edge_to (P2trPoint *start,
|
96 |
P2trPoint *end,
|
97 |
gboolean do_ref)
|
98 |
{
|
99 |
P2trEdge* result = p2tr_point_has_edge_to (start, end);
|
100 |
if (result == NULL)
|
101 |
p2tr_exception_programmatic ("Tried to get an edge that doesn't exist!");
|
102 |
else
|
103 |
return do_ref ? p2tr_edge_ref (result) : result;
|
104 |
}
|
105 |
|
106 |
void
|
107 |
_p2tr_point_insert_edge (P2trPoint *self, P2trEdge *e)
|
108 |
{
|
109 |
GList *iter = self->outgoing_edges;
|
110 |
|
111 |
/* Remember: Edges are sorted in ASCENDING angle! */
|
112 |
while (iter != NULL && ((P2trEdge*)iter->data)->angle < e->angle)
|
113 |
iter = iter->next;
|
114 |
|
115 |
self->outgoing_edges =
|
116 |
g_list_insert_before (self->outgoing_edges, iter, e);
|
117 |
|
118 |
p2tr_edge_ref (e);
|
119 |
}
|
120 |
|
121 |
void
|
122 |
_p2tr_point_remove_edge (P2trPoint *self, P2trEdge* e)
|
123 |
{
|
124 |
GList *node;
|
125 |
|
126 |
if (P2TR_EDGE_START(e) != self)
|
127 |
p2tr_exception_programmatic ("Could not remove the given outgoing "
|
128 |
"edge because doesn't start on this point!");
|
129 |
|
130 |
node = g_list_find (self->outgoing_edges, e);
|
131 |
if (node == NULL)
|
132 |
p2tr_exception_programmatic ("Could not remove the given outgoing "
|
133 |
"edge because it's not present in the outgoing-edges list!");
|
134 |
|
135 |
self->outgoing_edges = g_list_delete_link (self->outgoing_edges, node);
|
136 |
|
137 |
p2tr_edge_unref (e);
|
138 |
}
|
139 |
|
140 |
P2trEdge*
|
141 |
p2tr_point_edge_ccw (P2trPoint *self,
|
142 |
P2trEdge *e)
|
143 |
{
|
144 |
GList *node;
|
145 |
P2trEdge *result;
|
146 |
|
147 |
if (P2TR_EDGE_START(e) != self)
|
148 |
p2tr_exception_programmatic ("Not an edge of this point!");
|
149 |
|
150 |
node = g_list_find (self->outgoing_edges, e);
|
151 |
if (node == NULL)
|
152 |
p2tr_exception_programmatic ("Could not find the CCW sibling edge"
|
153 |
"because the edge is not present in the outgoing-edges list!");
|
154 |
|
155 |
result = (P2trEdge*) g_list_cyclic_next (self->outgoing_edges, node)->data;
|
156 |
return p2tr_edge_ref (result);
|
157 |
}
|
158 |
|
159 |
P2trEdge*
|
160 |
p2tr_point_edge_cw (P2trPoint* self,
|
161 |
P2trEdge *e)
|
162 |
{
|
163 |
GList *node;
|
164 |
P2trEdge *result;
|
165 |
|
166 |
if (P2TR_EDGE_START(e) != self)
|
167 |
p2tr_exception_programmatic ("Not an edge of this point!");
|
168 |
|
169 |
node = g_list_find (self->outgoing_edges, e);
|
170 |
if (node == NULL)
|
171 |
p2tr_exception_programmatic ("Could not find the CW sibling edge"
|
172 |
"because the edge is not present in the outgoing-edges list!");
|
173 |
|
174 |
result = (P2trEdge*) g_list_cyclic_prev (self->outgoing_edges, node)->data;
|
175 |
return p2tr_edge_ref (result);
|
176 |
}
|
177 |
|
178 |
gboolean
|
179 |
p2tr_point_is_fully_in_domain (P2trPoint *self)
|
180 |
{
|
181 |
GList *iter;
|
182 |
for (iter = self->outgoing_edges; iter != NULL; iter = iter->next)
|
183 |
if (((P2trEdge*) iter->data)->tri == NULL)
|
184 |
return FALSE;
|
185 |
|
186 |
return TRUE;
|
187 |
}
|
188 |
|
189 |
gboolean
|
190 |
p2tr_point_has_constrained_edge (P2trPoint *self)
|
191 |
{
|
192 |
GList *iter;
|
193 |
for (iter = self->outgoing_edges; iter != NULL; iter = iter->next)
|
194 |
if (((P2trEdge*) iter->data)->constrained)
|
195 |
return TRUE;
|
196 |
|
197 |
return FALSE;
|
198 |
}
|
199 |
|
200 |
/**
|
201 |
* Increase the reference count of the given input point
|
202 |
* @param self - The point to ref
|
203 |
* @return The point given
|
204 |
*/
|
205 |
P2trPoint*
|
206 |
p2tr_point_ref (P2trPoint *self)
|
207 |
{
|
208 |
++self->refcount;
|
209 |
return self;
|
210 |
}
|
211 |
|
212 |
void
|
213 |
p2tr_point_unref (P2trPoint *self)
|
214 |
{
|
215 |
g_assert (self->refcount > 0);
|
216 |
if (--self->refcount == 0)
|
217 |
p2tr_point_free (self);
|
218 |
}
|
219 |
|
220 |
P2trMesh*
|
221 |
p2tr_point_get_mesh (P2trPoint *self)
|
222 |
{
|
223 |
if (self->mesh)
|
224 |
return p2tr_mesh_ref (self->mesh);
|
225 |
else
|
226 |
return NULL;
|
227 |
}
|