Debian Codegraph @

src/cpPolyline.c

raw symbols SCIP
1 // Copyright 2013 Howling Moon Software. All rights reserved.cxx . . $ `<file>/src/cpPolyline.c`/
2 // See http://chipmunk2d.net/legal.php for more information.
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <math.h>
8
9 #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
10 #include "chipmunk/cpPolyline.h"cxx . . $ `<file>/include/chipmunk/cpPolyline.h`/
11
12
13 static inline int Nextcxx . . $ Next(9b79fb6aee4c0440).(int i, int count){return (i+1)%count;}
14
15 //MARK: Polylines
16
17 #define DEFAULT_POLYLINE_CAPACITYcxx . . $ `src/cpPolyline.c:17:9`! 16
18
19 static int
20 cpPolylineSizeForCapacitycxx . . $ cpPolylineSizeForCapacity(7864480464b09eea).(int capacity)
21 {
22 return sizeof(cpPolylinecxx . . $ cpPolyline#) + capacity*sizeof(cpVectcxx . . $ cpVect#);
23 }
24
25 static cpPolylinecxx . . $ cpPolyline# *
26 cpPolylineMakecxx . . $ cpPolylineMake(581c5b8a10822866).(int capacity)
27 {
28 capacity = (capacity > DEFAULT_POLYLINE_CAPACITYcxx . . $ `src/cpPolyline.c:17:9`! ? capacity : DEFAULT_POLYLINE_CAPACITYcxx . . $ `src/cpPolyline.c:17:9`!);
29
30 cpPolylinecxx . . $ cpPolyline# *line = (cpPolylinecxx . . $ cpPolyline# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, cpPolylineSizeForCapacitycxx . . $ cpPolylineSizeForCapacity(7864480464b09eea).(capacity));
31 line->countcxx . . $ cpPolyline#count. = 0;
32 line->capacitycxx . . $ cpPolyline#capacity. = capacity;
33
34 return line;
35 }
36
37 static cpPolylinecxx . . $ cpPolyline# *
38 cpPolylineMake2cxx . . $ cpPolylineMake2(309fab521f8e9e86).(int capacity, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b)
39 {
40 cpPolylinecxx . . $ cpPolyline# *line = cpPolylineMakecxx . . $ cpPolylineMake(581c5b8a10822866).(capacity);
41 line->countcxx . . $ cpPolyline#count. = 2;
42 line->vertscxx . . $ cpPolyline#verts.[0] = a;
43 line->vertscxx . . $ cpPolyline#verts.[1] = b;
44
45 return line;
46 }
47
48 static cpPolylinecxx . . $ cpPolyline# *
49 cpPolylineShrinkcxx . . $ cpPolylineShrink(d2e3e887f578993).(cpPolylinecxx . . $ cpPolyline# *line)
50 {
51 line->capacitycxx . . $ cpPolyline#capacity. = line->countcxx . . $ cpPolyline#count.;
52 return (cpPolylinecxx . . $ cpPolyline#*) cprealloccxx . . $ `include/chipmunk/chipmunk.h:77:10`!(line, cpPolylineSizeForCapacitycxx . . $ cpPolylineSizeForCapacity(7864480464b09eea).(line->countcxx . . $ cpPolyline#count.));
53 }
54
55 void
56 cpPolylineFreecxx . . $ cpPolylineFree(b605ba1ee67b0a1c).(cpPolylinecxx . . $ cpPolyline# *line)
57 {
58 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(line);
59 }
60
61 // Grow the allocated memory for a polyline.
62 static cpPolylinecxx . . $ cpPolyline# *
63 cpPolylineGrowcxx . . $ cpPolylineGrow(e178edfe472fe61).(cpPolylinecxx . . $ cpPolyline# *line, int count)
64 {
65 line->countcxx . . $ cpPolyline#count. += count;
66
67 int capacity = line->capacitycxx . . $ cpPolyline#capacity.;
68 while(line->countcxx . . $ cpPolyline#count. > capacity) capacity *= 2;
69
70 if(line->capacitycxx . . $ cpPolyline#capacity. < capacity){
71 line->capacitycxx . . $ cpPolyline#capacity. = capacity;
72 line = (cpPolylinecxx . . $ cpPolyline#*) cprealloccxx . . $ `include/chipmunk/chipmunk.h:77:10`!(line, cpPolylineSizeForCapacitycxx . . $ cpPolylineSizeForCapacity(7864480464b09eea).(capacity));
73 }
74
75 return line;
76 }
77
78 // Push v onto the end of line.
79 static cpPolylinecxx . . $ cpPolyline# *
80 cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(cpPolylinecxx . . $ cpPolyline# *line, cpVectcxx . . $ cpVect# v)
81 {
82 int count = line->countcxx . . $ cpPolyline#count.;
83 line = cpPolylineGrowcxx . . $ cpPolylineGrow(e178edfe472fe61).(line, 1);
84 line->vertscxx . . $ cpPolyline#verts.[count] = v;
85
86 return line;
87 }
88
89 // Push v onto the beginning of line.
90 static cpPolylinecxx . . $ cpPolyline# *
91 cpPolylineEnqueuecxx . . $ cpPolylineEnqueue(233dbd05b13d63c3).(cpPolylinecxx . . $ cpPolyline# *line, cpVectcxx . . $ cpVect# v)
92 {
93 // TODO could optimize this to grow in both directions.
94 // Probably doesn't matter though.
95 int count = line->countcxx . . $ cpPolyline#count.;
96 line = cpPolylineGrowcxx . . $ cpPolylineGrow(e178edfe472fe61).(line, 1);
97 memmovecxx . . $ memmove(cc817cb175f282d).(line->vertscxx . . $ cpPolyline#verts. + 1, line->vertscxx . . $ cpPolyline#verts., count*sizeof(cpVectcxx . . $ cpVect#));
98 line->vertscxx . . $ cpPolyline#verts.[0] = v;
99
100 return line;
101 }
102
103 // Returns true if the polyline starts and ends with the same vertex.
104 cpBoolcxx . . $ cpBool#
105 cpPolylineIsClosedcxx . . $ cpPolylineIsClosed(a88d4bbbf091399e).(cpPolylinecxx . . $ cpPolyline# *line)
106 {
107 return (line->countcxx . . $ cpPolyline#count. > 1 && cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(line->vertscxx . . $ cpPolyline#verts.[0], line->vertscxx . . $ cpPolyline#verts.[line->countcxx . . $ cpPolyline#count.-1]));
108 }
109
110 // Check if a cpPolyline is longer than a certain length
111 // Takes a range which can wrap around if the polyline is looped.
112 static cpBoolcxx . . $ cpBool#
113 cpPolylineIsShortcxx . . $ cpPolylineIsShort(c44603fbcd17884e).(cpVectcxx . . $ cpVect# *points, int count, int start, int end, cpFloatcxx . . $ cpFloat# min)
114 {
115 cpFloatcxx . . $ cpFloat# length = 0.0f;
116 for(int i=start; i!=end; i=Nextcxx . . $ Next(9b79fb6aee4c0440).(i, count)){
117 length += cpvdistcxx . . $ cpvdist(7d57c1c4dd559d53).(points[i], points[Nextcxx . . $ Next(9b79fb6aee4c0440).(i, count)]);
118 if(length > min) return cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!;
119 }
120
121 return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!;
122 }
123
124 //MARK: Polyline Simplification
125
126 static inline cpFloatcxx . . $ cpFloat#
127 Sharpnesscxx . . $ Sharpness(85bd79abb1677227).(cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpVectcxx . . $ cpVect# c)
128 {
129 // TODO could speed this up by caching the normals instead of calculating each twice.
130 return cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(a, b)), cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(c, b)));
131 }
132
133 // Join similar adjacent line segments together. Works well for hard edged shapes.
134 // 'tol' is the minimum anglular difference in radians of a vertex.
135 cpPolylinecxx . . $ cpPolyline# *
136 cpPolylineSimplifyVertexescxx . . $ cpPolylineSimplifyVertexes(d6e967f446f009d1).(cpPolylinecxx . . $ cpPolyline# *line, cpFloatcxx . . $ cpFloat# tol)
137 {
138 cpPolylinecxx . . $ cpPolyline# *reduced = cpPolylineMake2cxx . . $ cpPolylineMake2(309fab521f8e9e86).(0, line->vertscxx . . $ cpPolyline#verts.[0], line->vertscxx . . $ cpPolyline#verts.[1]);
139
140 cpFloatcxx . . $ cpFloat# minSharp = -cpfcoscxx . . $ `include/chipmunk/chipmunk_types.h:70:10`!(tol);
141
142 for(int i=2; i<line->countcxx . . $ cpPolyline#count.; i++){
143 cpVectcxx . . $ cpVect# vert = line->vertscxx . . $ cpPolyline#verts.[i];
144 cpFloatcxx . . $ cpFloat# sharp = Sharpnesscxx . . $ Sharpness(85bd79abb1677227).(reduced->vertscxx . . $ cpPolyline#verts.[reduced->countcxx . . $ cpPolyline#count. - 2], reduced->vertscxx . . $ cpPolyline#verts.[reduced->countcxx . . $ cpPolyline#count. - 1], vert);
145
146 if(sharp <= minSharp){
147 reduced->vertscxx . . $ cpPolyline#verts.[reduced->countcxx . . $ cpPolyline#count. - 1] = vert;
148 } else {
149 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, vert);
150 }
151 }
152
153 if(
154 cpPolylineIsClosedcxx . . $ cpPolylineIsClosed(a88d4bbbf091399e).(line) &&
155 Sharpnesscxx . . $ Sharpness(85bd79abb1677227).(reduced->vertscxx . . $ cpPolyline#verts.[reduced->countcxx . . $ cpPolyline#count. - 2], reduced->vertscxx . . $ cpPolyline#verts.[0], reduced->vertscxx . . $ cpPolyline#verts.[1]) < minSharp
156 ){
157 reduced->vertscxx . . $ cpPolyline#verts.[0] = reduced->vertscxx . . $ cpPolyline#verts.[reduced->countcxx . . $ cpPolyline#count. - 2];
158 reduced->countcxx . . $ cpPolyline#count.--;
159 }
160
161 // TODO shrink
162 return reduced;
163 }
164
165 // Recursive function used by cpPolylineSimplifyCurves().
166 static cpPolylinecxx . . $ cpPolyline# *
167 DouglasPeuckercxx . . $ DouglasPeucker(6b6cec79290bc918).(
168 cpVectcxx . . $ cpVect# *verts, cpPolylinecxx . . $ cpPolyline# *reduced,
169 int length, int start, int end,
170 cpFloatcxx . . $ cpFloat# min, cpFloatcxx . . $ cpFloat# tol
171 ){
172 // Early exit if the points are adjacent
173 if((end - start + length)%length < 2) return reduced;
174
175 cpVectcxx . . $ cpVect# a = verts[start];
176 cpVectcxx . . $ cpVect# b = verts[end];
177
178 // Check if the length is below the threshold
179 if(cpvnearcxx . . $ cpvnear(69cc569a1d96b8bc).(a, b, min) && cpPolylineIsShortcxx . . $ cpPolylineIsShort(c44603fbcd17884e).(verts, length, start, end, min)) return reduced;
180
181 // Find the maximal vertex to split and recurse on
182 cpFloatcxx . . $ cpFloat# max = 0.0;
183 int maxi = start;
184
185 cpVectcxx . . $ cpVect# n = cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(b, a)));
186 cpFloatcxx . . $ cpFloat# d = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, a);
187
188 for(int i=Nextcxx . . $ Next(9b79fb6aee4c0440).(start, length); i!=end; i=Nextcxx . . $ Next(9b79fb6aee4c0440).(i, length)){
189 cpFloatcxx . . $ cpFloat# dist = fabs(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, verts[i]) - d);
190
191 if(dist > max){
192 max = dist;
193 maxi = i;
194 }
195 }
196
197 if(max > tol){
198 reduced = DouglasPeuckercxx . . $ DouglasPeucker(6b6cec79290bc918).(verts, reduced, length, start, maxi, min, tol);
199 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, verts[maxi]);
200 reduced = DouglasPeuckercxx . . $ DouglasPeucker(6b6cec79290bc918).(verts, reduced, length, maxi, end, min, tol);
201 }
202
203 return reduced;
204 }
205
206 // Recursively reduce the vertex count on a polyline. Works best for smooth shapes.
207 // 'tol' is the maximum error for the reduction.
208 // The reduced polyline will never be farther than this distance from the original polyline.
209 cpPolylinecxx . . $ cpPolyline# *
210 cpPolylineSimplifyCurvescxx . . $ cpPolylineSimplifyCurves(d6e967f446f009d1).(cpPolylinecxx . . $ cpPolyline# *line, cpFloatcxx . . $ cpFloat# tol)
211 {
212 cpPolylinecxx . . $ cpPolyline# *reduced = cpPolylineMakecxx . . $ cpPolylineMake(581c5b8a10822866).(line->countcxx . . $ cpPolyline#count.);
213
214 cpFloatcxx . . $ cpFloat# min = tol/2.0f;
215
216 if(cpPolylineIsClosedcxx . . $ cpPolylineIsClosed(a88d4bbbf091399e).(line)){
217 int start, end;
218 cpLoopIndexescxx . . $ cpLoopIndexes(51f870a09d38702c).(line->vertscxx . . $ cpPolyline#verts., line->countcxx . . $ cpPolyline#count. - 1, &start, &end);
219
220 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, line->vertscxx . . $ cpPolyline#verts.[start]);
221 reduced = DouglasPeuckercxx . . $ DouglasPeucker(6b6cec79290bc918).(line->vertscxx . . $ cpPolyline#verts., reduced, line->countcxx . . $ cpPolyline#count. - 1, start, end, min, tol);
222 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, line->vertscxx . . $ cpPolyline#verts.[end]);
223 reduced = DouglasPeuckercxx . . $ DouglasPeucker(6b6cec79290bc918).(line->vertscxx . . $ cpPolyline#verts., reduced, line->countcxx . . $ cpPolyline#count. - 1, end, start, min, tol);
224 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, line->vertscxx . . $ cpPolyline#verts.[start]);
225 } else {
226 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, line->vertscxx . . $ cpPolyline#verts.[0]);
227 reduced = DouglasPeuckercxx . . $ DouglasPeucker(6b6cec79290bc918).(line->vertscxx . . $ cpPolyline#verts., reduced, line->countcxx . . $ cpPolyline#count., 0, line->countcxx . . $ cpPolyline#count. - 1, min, tol);
228 reduced = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(reduced, line->vertscxx . . $ cpPolyline#verts.[line->countcxx . . $ cpPolyline#count. - 1]);
229 }
230
231 return cpPolylineShrinkcxx . . $ cpPolylineShrink(d2e3e887f578993).(reduced);
232 }
233
234 //MARK: Polyline Sets
235
236 cpPolylineSetcxx . . $ cpPolylineSet# *
237 cpPolylineSetAlloccxx . . $ cpPolylineSetAlloc(3289914676e7b944).(void)
238 {
239 return (cpPolylineSetcxx . . $ cpPolylineSet# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpPolylineSetcxx . . $ cpPolylineSet#));
240 }
241
242 cpPolylineSetcxx . . $ cpPolylineSet# *
243 cpPolylineSetInitcxx . . $ cpPolylineSetInit(eb2a7000c29fcdee).(cpPolylineSetcxx . . $ cpPolylineSet# *set)
244 {
245 set->countcxx . . $ cpPolylineSet#count. = 0;
246 set->capacitycxx . . $ cpPolylineSet#capacity. = 8;
247 set->linescxx . . $ cpPolylineSet#lines. = (cpPolylinecxx . . $ cpPolyline#**) cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(set->capacitycxx . . $ cpPolylineSet#capacity., sizeof(cpPolylinecxx . . $ cpPolyline#));
248
249 return set;
250 }
251
252
253 cpPolylineSetcxx . . $ cpPolylineSet# *
254 cpPolylineSetNewcxx . . $ cpPolylineSetNew(3289914676e7b944).(void)
255 {
256 return cpPolylineSetInitcxx . . $ cpPolylineSetInit(eb2a7000c29fcdee).(cpPolylineSetAlloccxx . . $ cpPolylineSetAlloc(3289914676e7b944).());
257 }
258
259 void
260 cpPolylineSetDestroycxx . . $ cpPolylineSetDestroy(5763e47a182a8e5e).(cpPolylineSetcxx . . $ cpPolylineSet# *set, cpBoolcxx . . $ cpBool# freePolylines)
261 {
262 if(freePolylines){
263 for(int i=0; i<set->countcxx . . $ cpPolylineSet#count.; i++){
264 cpPolylineFreecxx . . $ cpPolylineFree(b605ba1ee67b0a1c).(set->linescxx . . $ cpPolylineSet#lines.[i]);
265 }
266 }
267
268 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(set->linescxx . . $ cpPolylineSet#lines.);
269 }
270
271
272 void
273 cpPolylineSetFreecxx . . $ cpPolylineSetFree(5763e47a182a8e5e).(cpPolylineSetcxx . . $ cpPolylineSet# *set, cpBoolcxx . . $ cpBool# freePolylines)
274 {
275 if(set){
276 cpPolylineSetDestroycxx . . $ cpPolylineSetDestroy(5763e47a182a8e5e).(set, freePolylines);
277 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(set);
278 }
279 }
280
281 // Find the polyline that ends with v.
282 static int
283 cpPolylineSetFindEndscxx . . $ cpPolylineSetFindEnds(248794606b956efc).(cpPolylineSetcxx . . $ cpPolylineSet# *set, cpVectcxx . . $ cpVect# v){
284 int count = set->countcxx . . $ cpPolylineSet#count.;
285 cpPolylinecxx . . $ cpPolyline# **lines = set->linescxx . . $ cpPolylineSet#lines.;
286
287 for(int i=0; i<count; i++){
288 cpPolylinecxx . . $ cpPolyline# *line = lines[i];
289 if(cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(line->vertscxx . . $ cpPolyline#verts.[line->countcxx . . $ cpPolyline#count. - 1], v)) return i;
290 }
291
292 return -1;
293 }
294
295 // Find the polyline that starts with v.
296 static int
297 cpPolylineSetFindStartscxx . . $ cpPolylineSetFindStarts(248794606b956efc).(cpPolylineSetcxx . . $ cpPolylineSet# *set, cpVectcxx . . $ cpVect# v){
298 int count = set->countcxx . . $ cpPolylineSet#count.;
299 cpPolylinecxx . . $ cpPolyline# **lines = set->linescxx . . $ cpPolylineSet#lines.;
300
301 for(int i=0; i<count; i++){
302 if(cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(lines[i]->vertscxx . . $ cpPolyline#verts.[0], v)) return i;
303 }
304
305 return -1;
306 }
307
308 // Add a new polyline to a polyline set.
309 static void
310 cpPolylineSetPushcxx . . $ cpPolylineSetPush(3981803c4afd9329).(cpPolylineSetcxx . . $ cpPolylineSet# *set, cpPolylinecxx . . $ cpPolyline# *line)
311 {
312 // grow set
313 set->countcxx . . $ cpPolylineSet#count.++;
314 if(set->countcxx . . $ cpPolylineSet#count. > set->capacitycxx . . $ cpPolylineSet#capacity.){
315 set->capacitycxx . . $ cpPolylineSet#capacity. *= 2;
316 set->linescxx . . $ cpPolylineSet#lines. = (cpPolylinecxx . . $ cpPolyline#**) cprealloccxx . . $ `include/chipmunk/chipmunk.h:77:10`!(set->linescxx . . $ cpPolylineSet#lines., set->capacitycxx . . $ cpPolylineSet#capacity.*sizeof(cpPolylinecxx . . $ cpPolyline#));
317 }
318
319 set->linescxx . . $ cpPolylineSet#lines.[set->countcxx . . $ cpPolylineSet#count. - 1] = line;
320 }
321
322 // Add a new polyline to a polyline set.
323 static void
324 cpPolylineSetAddcxx . . $ cpPolylineSetAdd(fdcb5c1d8cf0b7ed).(cpPolylineSetcxx . . $ cpPolylineSet# *set, cpVectcxx . . $ cpVect# v0, cpVectcxx . . $ cpVect# v1)
325 {
326 cpPolylineSetPushcxx . . $ cpPolylineSetPush(3981803c4afd9329).(set, cpPolylineMake2cxx . . $ cpPolylineMake2(309fab521f8e9e86).(DEFAULT_POLYLINE_CAPACITYcxx . . $ `src/cpPolyline.c:17:9`!, v0, v1));
327 }
328
329 // Join two cpPolylines in a polyline set together.
330 static void
331 cpPolylineSetJoincxx . . $ cpPolylineSetJoin(67e79f3a1e0159d7).(cpPolylineSetcxx . . $ cpPolylineSet# *set, int before, int after)
332 {
333 cpPolylinecxx . . $ cpPolyline# *lbefore = set->linescxx . . $ cpPolylineSet#lines.[before];
334 cpPolylinecxx . . $ cpPolyline# *lafter = set->linescxx . . $ cpPolylineSet#lines.[after];
335
336 // append
337 int count = lbefore->countcxx . . $ cpPolyline#count.;
338 lbefore = cpPolylineGrowcxx . . $ cpPolylineGrow(e178edfe472fe61).(lbefore, lafter->countcxx . . $ cpPolyline#count.);
339 memmovecxx . . $ memmove(cc817cb175f282d).(lbefore->vertscxx . . $ cpPolyline#verts. + count, lafter->vertscxx . . $ cpPolyline#verts., lafter->countcxx . . $ cpPolyline#count.*sizeof(cpVectcxx . . $ cpVect#));
340 set->linescxx . . $ cpPolylineSet#lines.[before] = lbefore;
341
342 // delete lafter
343 set->countcxx . . $ cpPolylineSet#count.--;
344 cpPolylineFreecxx . . $ cpPolylineFree(b605ba1ee67b0a1c).(set->linescxx . . $ cpPolylineSet#lines.[after]);
345 set->linescxx . . $ cpPolylineSet#lines.[after] = set->linescxx . . $ cpPolylineSet#lines.[set->countcxx . . $ cpPolylineSet#count.];
346 }
347
348 // Add a segment to a polyline set.
349 // A segment will either start a new polyline, join two others, or add to or loop an existing polyline.
350 void
351 cpPolylineSetCollectSegmentcxx . . $ cpPolylineSetCollectSegment(4e74ed7228dafb9).(cpVectcxx . . $ cpVect# v0, cpVectcxx . . $ cpVect# v1, cpPolylineSetcxx . . $ cpPolylineSet# *lines)
352 {
353 int before = cpPolylineSetFindEndscxx . . $ cpPolylineSetFindEnds(248794606b956efc).(lines, v0);
354 int after = cpPolylineSetFindStartscxx . . $ cpPolylineSetFindStarts(248794606b956efc).(lines, v1);
355
356 if(before >= 0 && after >= 0){
357 if(before == after){
358 // loop by pushing v1 onto before
359 lines->linescxx . . $ cpPolylineSet#lines.[before] = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(lines->linescxx . . $ cpPolylineSet#lines.[before], v1);
360 } else {
361 // join before and after
362 cpPolylineSetJoincxx . . $ cpPolylineSetJoin(67e79f3a1e0159d7).(lines, before, after);
363 }
364 } else if(before >= 0){
365 // push v1 onto before
366 lines->linescxx . . $ cpPolylineSet#lines.[before] = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(lines->linescxx . . $ cpPolylineSet#lines.[before], v1);
367 } else if(after >= 0){
368 // enqueue v0 onto after
369 lines->linescxx . . $ cpPolylineSet#lines.[after] = cpPolylineEnqueuecxx . . $ cpPolylineEnqueue(233dbd05b13d63c3).(lines->linescxx . . $ cpPolylineSet#lines.[after], v0);
370 } else {
371 // create new line from v0 and v1
372 cpPolylineSetAddcxx . . $ cpPolylineSetAdd(fdcb5c1d8cf0b7ed).(lines, v0, v1);
373 }
374 }
375
376 //MARK: Convex Hull Functions
377
378 cpPolylinecxx . . $ cpPolyline# *
379 cpPolylineToConvexHullcxx . . $ cpPolylineToConvexHull(d6e967f446f009d1).(cpPolylinecxx . . $ cpPolyline# *line, cpFloatcxx . . $ cpFloat# tol)
380 {
381 cpPolylinecxx . . $ cpPolyline# *hull = cpPolylineMakecxx . . $ cpPolylineMake(581c5b8a10822866).(line->countcxx . . $ cpPolyline#count. + 1);
382 hull->countcxx . . $ cpPolyline#count. = cpConvexHullcxx . . $ cpConvexHull(ca3a36ade7aeec92).(line->countcxx . . $ cpPolyline#count., line->vertscxx . . $ cpPolyline#verts., hull->vertscxx . . $ cpPolyline#verts., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, tol);
383 hull = cpPolylinePushcxx . . $ cpPolylinePush(233dbd05b13d63c3).(hull, hull->vertscxx . . $ cpPolyline#verts.[0]);
384
385 return cpPolylineShrinkcxx . . $ cpPolylineShrink(d2e3e887f578993).(hull);
386 }
387
388 //MARK: Approximate Concave Decompostition
389
390 struct Notchcxx . . $ Notch# {
391 int icxx . . $ Notch#i.;
392 cpFloatcxx . . $ cpFloat# dcxx . . $ Notch#d.;
393 cpVectcxx . . $ cpVect# vcxx . . $ Notch#v.;
394 cpVectcxx . . $ cpVect# ncxx . . $ Notch#n.;
395 };
396
397 static cpFloatcxx . . $ cpFloat#
398 FindSteinercxx . . $ FindSteiner(8e1aa0473e149ba7).(int count, cpVectcxx . . $ cpVect# *verts, struct Notchcxx . . $ Notch# notch)
399 {
400 cpFloatcxx . . $ cpFloat# min = INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!;
401 cpFloatcxx . . $ cpFloat# feature = -1.0;
402
403 for(int i=1; i<count-1; i++){
404 int index = (notch.icxx . . $ Notch#i. + i)%count;
405
406 cpVectcxx . . $ cpVect# seg_a = verts[index];
407 cpVectcxx . . $ cpVect# seg_b = verts[Nextcxx . . $ Next(9b79fb6aee4c0440).(index, count)];
408
409 cpFloatcxx . . $ cpFloat# thing_a = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(notch.ncxx . . $ Notch#n., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(seg_a, notch.vcxx . . $ Notch#v.));
410 cpFloatcxx . . $ cpFloat# thing_b = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(notch.ncxx . . $ Notch#n., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(seg_b, notch.vcxx . . $ Notch#v.));
411 if(thing_a*thing_b <= 0.0){
412 cpFloatcxx . . $ cpFloat# t = thing_a/(thing_a - thing_b);
413 cpFloatcxx . . $ cpFloat# dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(notch.ncxx . . $ Notch#n., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(seg_a, seg_b, t), notch.vcxx . . $ Notch#v.));
414
415 if(dist >= 0.0 && dist <= min){
416 min = dist;
417 feature = index + t;
418 }
419 }
420 }
421
422 return feature;
423 }
424
425 //static cpFloat
426 //FindSteiner2(cpVect *verts, int count, struct Notch notch)
427 //{
428 // cpVect a = verts[(notch.i + count - 1)%count];
429 // cpVect b = verts[(notch.i + 1)%count];
430 // cpVect n = cpvnormalize(cpvadd(cpvnormalize(cpvsub(notch.v, a)), cpvnormalize(cpvsub(notch.v, b))));
431 //
432 // cpFloat min = INFINITY;
433 // cpFloat feature = -1.0;
434 //
435 // for(int i=1; i<count-1; i++){
436 // int index = (notch.i + i)%count;
437 //
438 // cpVect seg_a = verts[index];
439 // cpVect seg_b = verts[Next(index, count)];
440 //
441 // cpFloat thing_a = cpvcross(n, cpvsub(seg_a, notch.v));
442 // cpFloat thing_b = cpvcross(n, cpvsub(seg_b, notch.v));
443 // if(thing_a*thing_b <= 0.0){
444 // cpFloat t = thing_a/(thing_a - thing_b);
445 // cpFloat dist = cpvdot(n, cpvsub(cpvlerp(seg_a, seg_b, t), notch.v));
446 //
447 // if(dist >= 0.0 && dist <= min){
448 // min = dist;
449 // feature = index + t;
450 // }
451 // }
452 // }
453 //
454 // cpAssertSoft(feature >= 0.0, "No closest features detected. This is likely due to a self intersecting polygon.");
455 // return feature;
456 //}
457
458 //struct Range {cpFloat min, max;};
459 //static inline struct Range
460 //clip_range(cpVect delta_a, cpVect delta_b, cpVect clip)
461 //{
462 // cpFloat da = cpvcross(delta_a, clip);
463 // cpFloat db = cpvcross(delta_b, clip);
464 // cpFloat clamp = da/(da - db);
465 // if(da > db){
466 // return (struct Range){-INFINITY, clamp};
467 // } else if(da < db){
468 // return (struct Range){clamp, INFINITY};
469 // } else {
470 // return (struct Range){-INFINITY, INFINITY};
471 // }
472 //}
473 //
474 //static cpFloat
475 //FindSteiner3(cpVect *verts, int count, struct Notch notch)
476 //{
477 // cpFloat min = INFINITY;
478 // cpFloat feature = -1.0;
479 //
480 // cpVect support_a = verts[(notch.i - 1 + count)%count];
481 // cpVect support_b = verts[(notch.i + 1)%count];
482 //
483 // cpVect clip_a = cpvlerp(support_a, support_b, 0.1);
484 // cpVect clip_b = cpvlerp(support_b, support_b, 0.9);
485 //
486 // for(int i=1; i<count - 1; i++){
487 // int index = (notch.i + i)%count;
488 // cpVect seg_a = verts[index];
489 // cpVect seg_b = verts[Next(index, count)];
490 //
491 // cpVect delta_a = cpvsub(seg_a, notch.v);
492 // cpVect delta_b = cpvsub(seg_b, notch.v);
493 //
494 // // Ignore if the segment faces away from the point.
495 // if(cpvcross(delta_b, delta_a) > 0.0){
496 // struct Range range1 = clip_range(delta_a, delta_b, cpvsub(notch.v, clip_a));
497 // struct Range range2 = clip_range(delta_a, delta_b, cpvsub(clip_b, notch.v));
498 //
499 // cpFloat min_t = cpfmax(0.0, cpfmax(range1.min, range2.min));
500 // cpFloat max_t = cpfmin(1.0, cpfmin(range1.max, range2.max));
501 //
502 // // Ignore if the segment has been completely clipped away.
503 // if(min_t < max_t){
504 // cpVect seg_delta = cpvsub(seg_b, seg_a);
505 // cpFloat closest_t = cpfclamp(cpvdot(seg_delta, cpvsub(notch.v, seg_a))/cpvlengthsq(seg_delta), min_t, max_t);
506 // cpVect closest = cpvlerp(seg_a, seg_b, closest_t);
507 //
508 // cpFloat dist = cpvdistsq(notch.v, closest);
509 // if(dist < min){
510 // min = dist;
511 // feature = index + closest_t;
512 // }
513 // }
514 // }
515 // }
516 //
517 // cpAssertWarn(feature >= 0.0, "Internal Error: No closest features detected.");
518 // return feature;
519 //}
520
521 //static cpBool
522 //VertexUnobscured(int count, cpVect *verts, int index, int notch_i)
523 //{
524 // cpVect v = verts[notch_i];
525 // cpVect n = cpvnormalize(cpvsub(verts[index], v));
526 //
527 // for(int i=0; i<count; i++){
528 // if(i == index || i == Next(i, count) || i == notch_i || i == Next(notch_i, count)) continue;
529 //
530 // cpVect seg_a = verts[i];
531 // cpVect seg_b = verts[Next(i, count)];
532 //
533 // cpFloat thing_a = cpvcross(n, cpvsub(seg_a, v));
534 // cpFloat thing_b = cpvcross(n, cpvsub(seg_b, v));
535 // if(thing_a*thing_b <= 0.0) return cpTrue;
536 // }
537 //
538 // return cpFalse;
539 //}
540 //
541 //static cpFloat
542 //FindSteiner4(int count, cpVect *verts, struct Notch notch, cpFloat *convexity)
543 //{
544 // cpFloat min = INFINITY;
545 // cpFloat feature = -1.0;
546 //
547 // for(int i=Next(notch.b, count); i!=notch.a; i=Next(i, count)){
548 // cpVect v = verts[i];
549 // cpFloat weight = (1.0 + 0.1*convexity[i])/(1.0*cpvdist(notch.v, v));
550 //
551 // if(weight <= min && VertexUnobscured(count, verts, i, notch.i)){
552 // min = weight;
553 // feature = i;
554 // }
555 // }
556 //
557 // cpAssertSoft(feature >= 0.0, "No closest features detected. This is likely due to a self intersecting polygon.");
558 // return feature;
559 //}
560
561 static struct Notchcxx . . $ Notch#
562 DeepestNotchcxx . . $ DeepestNotch(8e63930da44431ef).(int count, cpVectcxx . . $ cpVect# *verts, int hullCount, cpVectcxx . . $ cpVect# *hullVerts, int first, cpFloatcxx . . $ cpFloat# tol)
563 {
564 struct Notchcxx . . $ Notch# notch = {};
565 int j = Nextcxx . . $ Next(9b79fb6aee4c0440).(first, count);
566
567 for(int i=0; i<hullCount; i++){
568 cpVectcxx . . $ cpVect# a = hullVerts[i];
569 cpVectcxx . . $ cpVect# b = hullVerts[Nextcxx . . $ Next(9b79fb6aee4c0440).(i, hullCount)];
570
571 // TODO use a cross check instead?
572 cpVectcxx . . $ cpVect# n = cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvrperpcxx . . $ cpvrperp(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(a, b)));
573 cpFloatcxx . . $ cpFloat# d = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, a);
574
575 cpVectcxx . . $ cpVect# v = verts[j];
576 while(!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(v, b)){
577 cpFloatcxx . . $ cpFloat# depth = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, v) - d;
578
579 if(depth > notch.dcxx . . $ Notch#d.){
580 notch.dcxx . . $ Notch#d. = depth;
581 notch.icxx . . $ Notch#i. = j;
582 notch.vcxx . . $ Notch#v. = v;
583 notch.ncxx . . $ Notch#n. = n;
584 }
585
586 j = Nextcxx . . $ Next(9b79fb6aee4c0440).(j, count);
587 v = verts[j];
588 }
589
590 j = Nextcxx . . $ Next(9b79fb6aee4c0440).(j, count);
591 }
592
593 return notch;
594 }
595
596 static inline int IMAXcxx . . $ IMAX(9b79fb6aee4c0440).(int a, int b){return (a > b ? a : b);}
597
598 static void
599 ApproximateConcaveDecompositioncxx . . $ ApproximateConcaveDecomposition(c6096b9cb251eba).(cpVectcxx . . $ cpVect# *verts, int count, cpFloatcxx . . $ cpFloat# tol, cpPolylineSetcxx . . $ cpPolylineSet# *set)
600 {
601 int first;
602 cpVectcxx . . $ cpVect# *hullVerts = (cpVectcxx . . $ cpVect#*) allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!(count*sizeof(cpVect));
603 int hullCount = cpConvexHullcxx . . $ cpConvexHull(ca3a36ade7aeec92).(count, verts, hullVerts, &first, 0.0);
604
605 if(hullCount != count){
606 struct Notchcxx . . $ Notch# notch = DeepestNotchcxx . . $ DeepestNotch(8e63930da44431ef).(count, verts, hullCount, hullVerts, first, tol);
607
608 if(notch.dcxx . . $ Notch#d. > tol){
609 cpFloatcxx . . $ cpFloat# steiner_it = FindSteinercxx . . $ FindSteiner(8e1aa0473e149ba7).(count, verts, notch);
610
611 if(steiner_it >= 0.0){
612 int steiner_i = (int)steiner_it;
613 cpVectcxx . . $ cpVect# steiner = cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(verts[steiner_i], verts[Nextcxx . . $ Next(9b79fb6aee4c0440).(steiner_i, count)], steiner_it - steiner_i);
614
615 // Vertex counts NOT including the steiner point.
616 int sub1_count = (steiner_i - notch.icxx . . $ Notch#i. + count)%count + 1;
617 int sub2_count = count - (steiner_i - notch.icxx . . $ Notch#i. + count)%count;
618 cpVectcxx . . $ cpVect# *scratch = (cpVectcxx . . $ cpVect#*) allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!((IMAX(sub1_count, sub2_count) + 1)*sizeof(cpVect));
619
620 for(int i=0; i<sub1_count; i++) scratch[i] = verts[(notch.icxx . . $ Notch#i. + i)%count];
621 scratch[sub1_count] = steiner;
622 ApproximateConcaveDecompositioncxx . . $ ApproximateConcaveDecomposition(c6096b9cb251eba).(scratch, sub1_count + 1, tol, set);
623
624 for(int i=0; i<sub2_count; i++) scratch[i] = verts[(steiner_i + 1 + i)%count];
625 scratch[sub2_count] = steiner;
626 ApproximateConcaveDecompositioncxx . . $ ApproximateConcaveDecomposition(c6096b9cb251eba).(scratch, sub2_count + 1, tol, set);
627
628 return;
629 }
630 }
631 }
632
633 cpPolylinecxx . . $ cpPolyline# *hull = cpPolylineMakecxx . . $ cpPolylineMake(581c5b8a10822866).(hullCount + 1);
634
635 memcpycxx . . $ memcpy(cc817cb175f282d).(hull->vertscxx . . $ cpPolyline#verts., hullVerts, hullCount*sizeof(cpVectcxx . . $ cpVect#));
636 hull->vertscxx . . $ cpPolyline#verts.[hullCount] = hullVerts[0];
637 hull->countcxx . . $ cpPolyline#count. = hullCount + 1;
638
639 cpPolylineSetPushcxx . . $ cpPolylineSetPush(3981803c4afd9329).(set, hull);
640 }
641
642 cpPolylineSetcxx . . $ cpPolylineSet# *
643 cpPolylineConvexDecomposition_BETAcxx . . $ `include/chipmunk/cpPolyline.h:73:9`!(cpPolylinecxx . . $ cpPolyline# *line, cpFloatcxx . . $ cpFloat# tol)
644 {
645 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(cpPolylineIsClosed(line), "Cannot decompose an open polygon.");
646 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(cpAreaForPoly(line->count, line->verts, 0.0) >= 0.0, "Winding is backwards. (Are you passing a hole?)");
647
648 cpPolylineSetcxx . . $ cpPolylineSet# *set = cpPolylineSetNewcxx . . $ cpPolylineSetNew(3289914676e7b944).();
649 ApproximateConcaveDecompositioncxx . . $ ApproximateConcaveDecomposition(c6096b9cb251eba).(line->vertscxx . . $ cpPolyline#verts., line->countcxx . . $ cpPolyline#count. - 1, tol, set);
650
651 return set;
652 }