Debian Codegraph @

src/chipmunk.c

raw symbols SCIP
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #if defined(ANDROID)
26 # include <android/log.h>
27 #endif
28
29 #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
30
31 void
32 cpMessagecxx . . $ cpMessage(c16ec036e1184f03).(const char *condition, const char *file, int line, int isError, int isHardError, const char *message, ...)
33 {
34 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, (isError ? "Aborting due to Chipmunk error: " : "Chipmunk warning: "));
35
36 va_listcxx . . $ va_list# vargs;
37 va_startcxx . . $ `<external>/8293021631987149590/stdarg.h:51:9`!(vargs, message); {
38 #if defined(ANDROID)
39 __android_log_print( ANDROID_LOG_INFO, "Chipmunk", "%s(%d)", file, line );
40 __android_log_print( ANDROID_LOG_INFO, "Chipmunk", message, vargs );
41 #else
42 vfprintfcxx . . $ vfprintf(bded311e54d67f9c).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, message, vargs);
43 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\n");
44 #endif
45 } va_endcxx . . $ `<external>/8293021631987149590/stdarg.h:53:9`!(vargs);
46
47 #if defined(ANDROID)
48 __android_log_print(ANDROID_LOG_INFO, "Chipmunk", "\tFailed condition: %s\n", condition);
49 __android_log_print(ANDROID_LOG_INFO, "Chipmunk", "\tSource:%s:%d\n", file, line);
50 #else
51 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\tFailed condition: %s\n", condition);
52 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\tSource:%s:%d\n", file, line);
53 #endif
54 }
55
56 #define STRcxx . . $ `src/chipmunk.c:56:9`!(s) #s
57 #define XSTRcxx . . $ `src/chipmunk.c:57:9`!(s) STRcxx . . $ `src/chipmunk.c:56:9`!(s)
58
59 const char *cpVersionStringcxx . . $ cpVersionString. = XSTRcxx . . $ `src/chipmunk.c:57:9`!(CP_VERSION_MAJORcxx . . $ `include/chipmunk/chipmunk.h:129:9`!) "." XSTRcxx . . $ `src/chipmunk.c:57:9`!(CP_VERSION_MINORcxx . . $ `include/chipmunk/chipmunk.h:130:9`!) "." XSTRcxx . . $ `src/chipmunk.c:57:9`!(CP_VERSION_RELEASEcxx . . $ `include/chipmunk/chipmunk.h:131:9`!);
60
61 //MARK: Misc Functions
62
63 cpFloatcxx . . $ cpFloat#
64 cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(cpFloatcxx . . $ cpFloat# m, cpFloatcxx . . $ cpFloat# r1, cpFloatcxx . . $ cpFloat# r2, cpVectcxx . . $ cpVect# offset)
65 {
66 return m*(0.5f*(r1*r1 + r2*r2) + cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(offset));
67 }
68
69 cpFloatcxx . . $ cpFloat#
70 cpAreaForCirclecxx . . $ cpAreaForCircle(d8e7e0733fa41c87).(cpFloatcxx . . $ cpFloat# r1, cpFloatcxx . . $ cpFloat# r2)
71 {
72 return (cpFloatcxx . . $ cpFloat#)CP_PIcxx . . $ `include/chipmunk/chipmunk_types.h:115:9`!*cpfabscxx . . $ cpfabs(447c67c6676f75c3).(r1*r1 - r2*r2);
73 }
74
75 cpFloatcxx . . $ cpFloat#
76 cpMomentForSegmentcxx . . $ cpMomentForSegment(13ad460f360d98d3).(cpFloatcxx . . $ cpFloat# m, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# r)
77 {
78 cpVectcxx . . $ cpVect# offset = cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(a, b, 0.5f);
79
80 // This approximates the shape as a box for rounded segments, but it's quite close.
81 cpFloatcxx . . $ cpFloat# length = cpvdistcxx . . $ cpvdist(7d57c1c4dd559d53).(b, a) + 2.0f*r;
82 return m*((length*length + 4.0f*r*r)/12.0f + cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(offset));
83 }
84
85 cpFloatcxx . . $ cpFloat#
86 cpAreaForSegmentcxx . . $ cpAreaForSegment(753c01a8c1fee84f).(cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# r)
87 {
88 return r*((cpFloatcxx . . $ cpFloat#)CP_PIcxx . . $ `include/chipmunk/chipmunk_types.h:115:9`!*r + 2.0f*cpvdistcxx . . $ cpvdist(7d57c1c4dd559d53).(a, b));
89 }
90
91 cpFloatcxx . . $ cpFloat#
92 cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(cpFloatcxx . . $ cpFloat# m, int count, const cpVectcxx . . $ cpVect# *verts, cpVectcxx . . $ cpVect# offset, cpFloatcxx . . $ cpFloat# r)
93 {
94 // TODO account for radius.
95 if(count == 2) return cpMomentForSegmentcxx . . $ cpMomentForSegment(13ad460f360d98d3).(m, verts[0], verts[1], 0.0f);
96
97 cpFloatcxx . . $ cpFloat# sum1 = 0.0f;
98 cpFloatcxx . . $ cpFloat# sum2 = 0.0f;
99 for(int i=0; i<count; i++){
100 cpVectcxx . . $ cpVect# v1 = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(verts[i], offset);
101 cpVectcxx . . $ cpVect# v2 = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(verts[(i+1)%count], offset);
102
103 cpFloatcxx . . $ cpFloat# a = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(v2, v1);
104 cpFloatcxx . . $ cpFloat# b = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(v1, v1) + cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(v1, v2) + cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(v2, v2);
105
106 sum1 += a*b;
107 sum2 += a;
108 }
109
110 return (m*sum1)/(6.0f*sum2);
111 }
112
113 cpFloatcxx . . $ cpFloat#
114 cpAreaForPolycxx . . $ cpAreaForPoly(c1e6bc8c2660eac5).(const int count, const cpVectcxx . . $ cpVect# *verts, cpFloatcxx . . $ cpFloat# r)
115 {
116 cpFloatcxx . . $ cpFloat# area = 0.0f;
117 cpFloatcxx . . $ cpFloat# perimeter = 0.0f;
118 for(int i=0; i<count; i++){
119 cpVectcxx . . $ cpVect# v1 = verts[i];
120 cpVectcxx . . $ cpVect# v2 = verts[(i+1)%count];
121
122 area += cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(v1, v2);
123 perimeter += cpvdistcxx . . $ cpvdist(7d57c1c4dd559d53).(v1, v2);
124 }
125
126 return r*(CP_PIcxx . . $ `include/chipmunk/chipmunk_types.h:115:9`!*cpfabscxx . . $ cpfabs(447c67c6676f75c3).(r) + perimeter) + area/2.0f;
127 }
128
129 cpVectcxx . . $ cpVect#
130 cpCentroidForPolycxx . . $ cpCentroidForPoly(740f059a8e4cff9d).(const int count, const cpVectcxx . . $ cpVect# *verts)
131 {
132 cpFloatcxx . . $ cpFloat# sum = 0.0f;
133 cpVectcxx . . $ cpVect# vsum = cpvzerocxx . . $ cpvzero.;
134
135 for(int i=0; i<count; i++){
136 cpVectcxx . . $ cpVect# v1 = verts[i];
137 cpVectcxx . . $ cpVect# v2 = verts[(i+1)%count];
138 cpFloatcxx . . $ cpFloat# cross = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(v1, v2);
139
140 sum += cross;
141 vsum = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(vsum, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(v1, v2), cross));
142 }
143
144 return cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(vsum, 1.0f/(3.0f*sum));
145 }
146
147 //void
148 //cpRecenterPoly(const int count, cpVect *verts){
149 // cpVect centroid = cpCentroidForPoly(count, verts);
150 //
151 // for(int i=0; i<count; i++){
152 // verts[i] = cpvsub(verts[i], centroid);
153 // }
154 //}
155
156 cpFloatcxx . . $ cpFloat#
157 cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(cpFloatcxx . . $ cpFloat# m, cpFloatcxx . . $ cpFloat# width, cpFloatcxx . . $ cpFloat# height)
158 {
159 return m*(width*width + height*height)/12.0f;
160 }
161
162 cpFloatcxx . . $ cpFloat#
163 cpMomentForBox2cxx . . $ cpMomentForBox2(6725099f9bd51722).(cpFloatcxx . . $ cpFloat# m, cpBBcxx . . $ cpBB# box)
164 {
165 cpFloatcxx . . $ cpFloat# width = box.rcxx . . $ cpBB#r. - box.lcxx . . $ cpBB#l.;
166 cpFloatcxx . . $ cpFloat# height = box.tcxx . . $ cpBB#t. - box.bcxx . . $ cpBB#b.;
167 cpVectcxx . . $ cpVect# offset = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvcxx . . $ cpv(3142d57d978b2176).(box.lcxx . . $ cpBB#l. + box.rcxx . . $ cpBB#r., box.bcxx . . $ cpBB#b. + box.tcxx . . $ cpBB#t.), 0.5f);
168
169 // TODO: NaN when offset is 0 and m is INFINITY
170 return cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(m, width, height) + m*cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(offset);
171 }
172
173 //MARK: Quick Hull
174
175 void
176 cpLoopIndexescxx . . $ cpLoopIndexes(51f870a09d38702c).(const cpVectcxx . . $ cpVect# *verts, int count, int *start, int *end)
177 {
178 (*start) = (*end) = 0;
179 cpVectcxx . . $ cpVect# min = verts[0];
180 cpVectcxx . . $ cpVect# max = min;
181
182 for(int i=1; i<count; i++){
183 cpVectcxx . . $ cpVect# v = verts[i];
184
185 if(v.xcxx . . $ cpVect#x. < min.xcxx . . $ cpVect#x. || (v.xcxx . . $ cpVect#x. == min.xcxx . . $ cpVect#x. && v.ycxx . . $ cpVect#y. < min.ycxx . . $ cpVect#y.)){
186 min = v;
187 (*start) = i;
188 } else if(v.xcxx . . $ cpVect#x. > max.xcxx . . $ cpVect#x. || (v.xcxx . . $ cpVect#x. == max.xcxx . . $ cpVect#x. && v.ycxx . . $ cpVect#y. > max.ycxx . . $ cpVect#y.)){
189 max = v;
190 (*end) = i;
191 }
192 }
193 }
194
195 #define SWAPcxx . . $ `src/chipmunk.c:195:9`!(__A__, __B__) {cpVect __TMP__ = __A__; __A__ = __B__; __B__ = __TMP__;}
196
197 static int
198 QHullPartitioncxx . . $ QHullPartition(65c89277714feca1).(cpVectcxx . . $ cpVect# *verts, int count, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# tol)
199 {
200 if(count == 0) return 0;
201
202 cpFloatcxx . . $ cpFloat# max = 0;
203 int pivot = 0;
204
205 cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(b, a);
206 cpFloatcxx . . $ cpFloat# valueTol = tol*cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(delta);
207
208 int head = 0;
209 for(int tail = count-1; head <= tail;){
210 cpFloatcxx . . $ cpFloat# value = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(verts[head], a), delta);
211 if(value > valueTol){
212 if(value > max){
213 max = value;
214 pivot = head;
215 }
216
217 head++;
218 } else {
219 SWAPcxx . . $ `src/chipmunk.c:195:9`!(verts[head], verts[tail]);
220 tail--;
221 }
222 }
223
224 // move the new pivot to the front if it's not already there.
225 if(pivot != 0) SWAPcxx . . $ `src/chipmunk.c:195:9`!(verts[0], verts[pivot]);
226 return head;
227 }
228
229 static int
230 QHullReducecxx . . $ QHullReduce(822baf06942dc6f0).(cpFloatcxx . . $ cpFloat# tol, cpVectcxx . . $ cpVect# *verts, int count, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# pivot, cpVectcxx . . $ cpVect# b, cpVectcxx . . $ cpVect# *result)
231 {
232 if(count < 0){
233 return 0;
234 } else if(count == 0) {
235 result[0] = pivot;
236 return 1;
237 } else {
238 int left_count = QHullPartitioncxx . . $ QHullPartition(65c89277714feca1).(verts, count, a, pivot, tol);
239 int index = QHullReducecxx . . $ QHullReduce(822baf06942dc6f0).(tol, verts + 1, left_count - 1, a, verts[0], pivot, result);
240
241 result[index++] = pivot;
242
243 int right_count = QHullPartitioncxx . . $ QHullPartition(65c89277714feca1).(verts + left_count, count - left_count, pivot, b, tol);
244 return index + QHullReducecxx . . $ QHullReduce(822baf06942dc6f0).(tol, verts + left_count + 1, right_count - 1, pivot, verts[left_count], b, result + index);
245 }
246 }
247
248 // QuickHull seemed like a neat algorithm, and efficient-ish for large input sets.
249 // My implementation performs an in place reduction using the result array as scratch space.
250 int
251 cpConvexHullcxx . . $ cpConvexHull(ca3a36ade7aeec92).(int count, const cpVectcxx . . $ cpVect# *verts, cpVectcxx . . $ cpVect# *result, int *first, cpFloatcxx . . $ cpFloat# tol)
252 {
253 if(verts != result){
254 // Copy the line vertexes into the empty part of the result polyline to use as a scratch buffer.
255 memcpycxx . . $ memcpy(cc817cb175f282d).(result, verts, count*sizeof(cpVectcxx . . $ cpVect#));
256 }
257
258 // Degenerate case, all points are the same.
259 int start, end;
260 cpLoopIndexescxx . . $ cpLoopIndexes(51f870a09d38702c).(verts, count, &start, &end);
261 if(start == end){
262 if(first) (*first) = 0;
263 return 1;
264 }
265
266 SWAPcxx . . $ `src/chipmunk.c:195:9`!(result[0], result[start]);
267 SWAPcxx . . $ `src/chipmunk.c:195:9`!(result[1], result[end == 0 ? start : end]);
268
269 cpVectcxx . . $ cpVect# a = result[0];
270 cpVectcxx . . $ cpVect# b = result[1];
271
272 if(first) (*first) = start;
273 return QHullReducecxx . . $ QHullReduce(822baf06942dc6f0).(tol, result + 2, count - 2, a, b, a, result + 1) + 1;
274 }
275
276 //MARK: Alternate Block Iterators
277
278 #if defined(__has_extension)
279 #if __has_extension(blocks)
280
281 static void IteratorFunc(void *ptr, void (^block)(void *ptr)){block(ptr);}
282
283 void cpSpaceEachBody_b(cpSpace *space, void (^block)(cpBody *body)){
284 cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)IteratorFunc, block);
285 }
286
287 void cpSpaceEachShape_b(cpSpace *space, void (^block)(cpShape *shape)){
288 cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)IteratorFunc, block);
289 }
290
291 void cpSpaceEachConstraint_b(cpSpace *space, void (^block)(cpConstraint *constraint)){
292 cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)IteratorFunc, block);
293 }
294
295 static void BodyIteratorFunc(cpBody *body, void *ptr, void (^block)(void *ptr)){block(ptr);}
296
297 void cpBodyEachShape_b(cpBody *body, void (^block)(cpShape *shape)){
298 cpBodyEachShape(body, (cpBodyShapeIteratorFunc)BodyIteratorFunc, block);
299 }
300
301 void cpBodyEachConstraint_b(cpBody *body, void (^block)(cpConstraint *constraint)){
302 cpBodyEachConstraint(body, (cpBodyConstraintIteratorFunc)BodyIteratorFunc, block);
303 }
304
305 void cpBodyEachArbiter_b(cpBody *body, void (^block)(cpArbiter *arbiter)){
306 cpBodyEachArbiter(body, (cpBodyArbiterIteratorFunc)BodyIteratorFunc, block);
307 }
308
309 static void PointQueryIteratorFunc(cpShape *shape, cpVect p, cpFloat d, cpVect g, cpSpacePointQueryBlock block){block(shape, p, d, g);}
310 void cpSpacePointQuery_b(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryBlock block){
311 cpSpacePointQuery(space, point, maxDistance, filter, (cpSpacePointQueryFunc)PointQueryIteratorFunc, block);
312 }
313
314 static void SegmentQueryIteratorFunc(cpShape *shape, cpVect p, cpVect n, cpFloat t, cpSpaceSegmentQueryBlock block){block(shape, p, n, t);}
315 void cpSpaceSegmentQuery_b(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryBlock block){
316 cpSpaceSegmentQuery(space, start, end, radius, filter, (cpSpaceSegmentQueryFunc)SegmentQueryIteratorFunc, block);
317 }
318
319 void cpSpaceBBQuery_b(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryBlock block){
320 cpSpaceBBQuery(space, bb, filter, (cpSpaceBBQueryFunc)IteratorFunc, block);
321 }
322
323 static void ShapeQueryIteratorFunc(cpShape *shape, cpContactPointSet *points, cpSpaceShapeQueryBlock block){block(shape, points);}
324 cpBool cpSpaceShapeQuery_b(cpSpace *space, cpShape *shape, cpSpaceShapeQueryBlock block){
325 return cpSpaceShapeQuery(space, shape, (cpSpaceShapeQueryFunc)ShapeQueryIteratorFunc, block);
326 }
327
328 #endif
329 #endif
330
331 #include "chipmunk/chipmunk_ffi.h"cxx . . $ `<file>/include/chipmunk/chipmunk_ffi.h`/