Debian Codegraph @

src/cpBBTree.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 "stdlib.h"
23 #include "stdio.h"
24
25 #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
26
27 static inline cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# *Klasscxx . . $ Klass(292ec58cde6c1067).();
28
29 typedef struct Nodecxx . . $ Node# Nodecxx . . $ Node#;
30 typedef struct Paircxx . . $ Pair# Paircxx . . $ Pair#;
31
32 struct cpBBTreecxx . . $ cpBBTree# {
33 cpSpatialIndexcxx . . $ cpSpatialIndex# spatialIndexcxx . . $ cpBBTree#spatialIndex.;
34 cpBBTreeVelocityFunccxx . . $ cpBBTreeVelocityFunc# velocityFunccxx . . $ cpBBTree#velocityFunc.;
35
36 cpHashSetcxx . . $ cpHashSet# *leavescxx . . $ cpBBTree#leaves.;
37 Nodecxx . . $ Node# *rootcxx . . $ cpBBTree#root.;
38
39 Nodecxx . . $ Node# *pooledNodescxx . . $ cpBBTree#pooledNodes.;
40 Paircxx . . $ Pair# *pooledPairscxx . . $ cpBBTree#pooledPairs.;
41 cpArraycxx . . $ cpArray# *allocatedBufferscxx . . $ cpBBTree#allocatedBuffers.;
42
43 cpTimestampcxx . . $ cpTimestamp# stampcxx . . $ cpBBTree#stamp.;
44 };
45
46 struct Nodecxx . . $ Node# {
47 void *objcxx . . $ Node#obj.;
48 cpBBcxx . . $ cpBB# bbcxx . . $ Node#bb.;
49 Nodecxx . . $ Node# *parentcxx . . $ Node#parent.;
50
51 unioncxx . . $ Node#$anonymous_type_0# {
52 // Internal nodes
53 structcxx . . $ Node#$anonymous_type_0#$anonymous_type_1# { Nodecxx . . $ Node# *acxx . . $ Node#$anonymous_type_0#$anonymous_type_1#a., *bcxx . . $ Node#$anonymous_type_0#$anonymous_type_1#b.; } childrencxx . . $ Node#$anonymous_type_0#children.;
54
55 // Leaves
56 structcxx . . $ Node#$anonymous_type_0#$anonymous_type_2# {
57 cpTimestampcxx . . $ cpTimestamp# stampcxx . . $ Node#$anonymous_type_0#$anonymous_type_2#stamp.;
58 Paircxx . . $ Pair# *pairscxx . . $ Node#$anonymous_type_0#$anonymous_type_2#pairs.;
59 } leafcxx . . $ Node#$anonymous_type_0#leaf.;
60 } nodecxx . . $ Node#node.;
61 };
62
63 // Can't use anonymous unions and still get good x-compiler compatability
64 #define Acxx . . $ `src/cpBBTree.c:64:9`! node.children.a
65 #define Bcxx . . $ `src/cpBBTree.c:65:9`! node.children.b
66 #define STAMPcxx . . $ `src/cpBBTree.c:66:9`! node.leaf.stamp
67 #define PAIRScxx . . $ `src/cpBBTree.c:67:9`! node.leaf.pairs
68
69 typedef struct Threadcxx . . $ Thread# {
70 Paircxx . . $ Pair# *prevcxx . . $ Thread#prev.;
71 Nodecxx . . $ Node# *leafcxx . . $ Thread#leaf.;
72 Paircxx . . $ Pair# *nextcxx . . $ Thread#next.;
73 } Threadcxx . . $ Thread#;
74
75 struct Paircxx . . $ Pair# {
76 Threadcxx . . $ Thread# acxx . . $ Pair#a., bcxx . . $ Pair#b.;
77 cpCollisionIDcxx . . $ cpCollisionID# idcxx . . $ Pair#id.;
78 };
79
80 //MARK: Misc Functions
81
82 static inline cpBBcxx . . $ cpBB#
83 GetBBcxx . . $ GetBB(6a16b80027b3111).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj)
84 {
85 cpBBcxx . . $ cpBB# bb = tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(obj);
86
87 cpBBTreeVelocityFunccxx . . $ cpBBTreeVelocityFunc# velocityFunc = tree->velocityFunccxx . . $ cpBBTree#velocityFunc.;
88 if(velocityFunc){
89 cpFloatcxx . . $ cpFloat# coef = 0.1f;
90 cpFloatcxx . . $ cpFloat# x = (bb.rcxx . . $ cpBB#r. - bb.lcxx . . $ cpBB#l.)*coef;
91 cpFloatcxx . . $ cpFloat# y = (bb.tcxx . . $ cpBB#t. - bb.bcxx . . $ cpBB#b.)*coef;
92
93 cpVectcxx . . $ cpVect# v = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(velocityFunc(obj), 0.1f);
94 return cpBBNewcxx . . $ cpBBNew(f05aa3203068e6d3).(bb.lcxx . . $ cpBB#l. + cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(-x, v.xcxx . . $ cpVect#x.), bb.bcxx . . $ cpBB#b. + cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(-y, v.ycxx . . $ cpVect#y.), bb.rcxx . . $ cpBB#r. + cpfmaxcxx . . $ cpfmax(d8e7e0733fa41c87).(x, v.xcxx . . $ cpVect#x.), bb.tcxx . . $ cpBB#t. + cpfmaxcxx . . $ cpfmax(d8e7e0733fa41c87).(y, v.ycxx . . $ cpVect#y.));
95 } else {
96 return bb;
97 }
98 }
99
100 static inline cpBBTreecxx . . $ cpBBTree# *
101 GetTreecxx . . $ GetTree(4790b17def89f44).(cpSpatialIndexcxx . . $ cpSpatialIndex# *index)
102 {
103 return (index && index->klasscxx . . $ cpSpatialIndex#klass. == Klasscxx . . $ Klass(292ec58cde6c1067).() ? (cpBBTreecxx . . $ cpBBTree# *)index : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
104 }
105
106 static inline Nodecxx . . $ Node# *
107 GetRootIfTreecxx . . $ GetRootIfTree(7ff26f7bdc470982).(cpSpatialIndexcxx . . $ cpSpatialIndex# *index){
108 return (index && index->klasscxx . . $ cpSpatialIndex#klass. == Klasscxx . . $ Klass(292ec58cde6c1067).() ? ((cpBBTreecxx . . $ cpBBTree# *)index)->rootcxx . . $ cpBBTree#root. : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
109 }
110
111 static inline cpBBTreecxx . . $ cpBBTree# *
112 GetMasterTreecxx . . $ GetMasterTree(b05b809c0d2f3f80).(cpBBTreecxx . . $ cpBBTree# *tree)
113 {
114 cpBBTreecxx . . $ cpBBTree# *dynamicTree = GetTreecxx . . $ GetTree(4790b17def89f44).(tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..dynamicIndexcxx . . $ cpSpatialIndex#dynamicIndex.);
115 return (dynamicTree ? dynamicTree : tree);
116 }
117
118 static inline void
119 IncrementStampcxx . . $ IncrementStamp(d63f4d5b2d9b584d).(cpBBTreecxx . . $ cpBBTree# *tree)
120 {
121 cpBBTreecxx . . $ cpBBTree# *dynamicTree = GetTreecxx . . $ GetTree(4790b17def89f44).(tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..dynamicIndexcxx . . $ cpSpatialIndex#dynamicIndex.);
122 if(dynamicTree){
123 dynamicTree->stampcxx . . $ cpBBTree#stamp.++;
124 } else {
125 tree->stampcxx . . $ cpBBTree#stamp.++;
126 }
127 }
128
129 //MARK: Pair/Thread Functions
130
131 static void
132 PairRecyclecxx . . $ PairRecycle(1375cefd4b8e0a0f).(cpBBTreecxx . . $ cpBBTree# *tree, Paircxx . . $ Pair# *pair)
133 {
134 // Share the pool of the master tree.
135 // TODO: would be lovely to move the pairs stuff into an external data structure.
136 tree = GetMasterTreecxx . . $ GetMasterTree(b05b809c0d2f3f80).(tree);
137
138 pair->acxx . . $ Pair#a..nextcxx . . $ Thread#next. = tree->pooledPairscxx . . $ cpBBTree#pooledPairs.;
139 tree->pooledPairscxx . . $ cpBBTree#pooledPairs. = pair;
140 }
141
142 static Paircxx . . $ Pair# *
143 PairFromPoolcxx . . $ PairFromPool(3126c6ed08735ceb).(cpBBTreecxx . . $ cpBBTree# *tree)
144 {
145 // Share the pool of the master tree.
146 // TODO: would be lovely to move the pairs stuff into an external data structure.
147 tree = GetMasterTreecxx . . $ GetMasterTree(b05b809c0d2f3f80).(tree);
148
149 Paircxx . . $ Pair# *pair = tree->pooledPairscxx . . $ cpBBTree#pooledPairs.;
150
151 if(pair){
152 tree->pooledPairscxx . . $ cpBBTree#pooledPairs. = pair->acxx . . $ Pair#a..nextcxx . . $ Thread#next.;
153 return pair;
154 } else {
155 // Pool is exhausted, make more
156 int count = CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!/sizeof(Paircxx . . $ Pair#);
157 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(count, "Internal Error: Buffer size is too small.");
158
159 Paircxx . . $ Pair# *buffer = (Paircxx . . $ Pair# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!);
160 cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(tree->allocatedBufferscxx . . $ cpBBTree#allocatedBuffers., buffer);
161
162 // push all but the first one, return the first instead
163 for(int i=1; i<count; i++) PairRecyclecxx . . $ PairRecycle(1375cefd4b8e0a0f).(tree, buffer + i);
164 return buffer;
165 }
166 }
167
168 static inline void
169 ThreadUnlinkcxx . . $ ThreadUnlink(1ff2621861fb09e0).(Threadcxx . . $ Thread# thread)
170 {
171 Paircxx . . $ Pair# *next = thread.nextcxx . . $ Thread#next.;
172 Paircxx . . $ Pair# *prev = thread.prevcxx . . $ Thread#prev.;
173
174 if(next){
175 if(next->acxx . . $ Pair#a..leafcxx . . $ Thread#leaf. == thread.leafcxx . . $ Thread#leaf.) next->acxx . . $ Pair#a..prevcxx . . $ Thread#prev. = prev; else next->bcxx . . $ Pair#b..prevcxx . . $ Thread#prev. = prev;
176 }
177
178 if(prev){
179 if(prev->acxx . . $ Pair#a..leafcxx . . $ Thread#leaf. == thread.leafcxx . . $ Thread#leaf.) prev->acxx . . $ Pair#a..nextcxx . . $ Thread#next. = next; else prev->bcxx . . $ Pair#b..nextcxx . . $ Thread#next. = next;
180 } else {
181 thread.leafcxx . . $ Thread#leaf.->PAIRScxx . . $ `src/cpBBTree.c:67:9`! = next;
182 }
183 }
184
185 static void
186 PairsClearcxx . . $ PairsClear(e3adc038f4e64d54).(Nodecxx . . $ Node# *leaf, cpBBTreecxx . . $ cpBBTree# *tree)
187 {
188 Paircxx . . $ Pair# *pair = leaf->PAIRScxx . . $ `src/cpBBTree.c:67:9`!;
189 leaf->PAIRScxx . . $ `src/cpBBTree.c:67:9`! = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
190
191 while(pair){
192 if(pair->acxx . . $ Pair#a..leafcxx . . $ Thread#leaf. == leaf){
193 Paircxx . . $ Pair# *next = pair->acxx . . $ Pair#a..nextcxx . . $ Thread#next.;
194 ThreadUnlinkcxx . . $ ThreadUnlink(1ff2621861fb09e0).(pair->bcxx . . $ Pair#b.);
195 PairRecyclecxx . . $ PairRecycle(1375cefd4b8e0a0f).(tree, pair);
196 pair = next;
197 } else {
198 Paircxx . . $ Pair# *next = pair->bcxx . . $ Pair#b..nextcxx . . $ Thread#next.;
199 ThreadUnlinkcxx . . $ ThreadUnlink(1ff2621861fb09e0).(pair->acxx . . $ Pair#a.);
200 PairRecyclecxx . . $ PairRecycle(1375cefd4b8e0a0f).(tree, pair);
201 pair = next;
202 }
203 }
204 }
205
206 static void
207 PairInsertcxx . . $ PairInsert(31dda1a45e8ef13d).(Nodecxx . . $ Node# *a, Nodecxx . . $ Node# *b, cpBBTreecxx . . $ cpBBTree# *tree)
208 {
209 Paircxx . . $ Pair# *nextA = a->PAIRScxx . . $ `src/cpBBTree.c:67:9`!, *nextB = b->PAIRScxx . . $ `src/cpBBTree.c:67:9`!;
210 Paircxx . . $ Pair# *pair = PairFromPoolcxx . . $ PairFromPool(3126c6ed08735ceb).(tree);
211 Paircxx . . $ Pair# temp = {{NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, a, nextA},{NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, b, nextB}, 0};
212
213 a->PAIRScxx . . $ `src/cpBBTree.c:67:9`! = b->PAIRScxx . . $ `src/cpBBTree.c:67:9`! = pair;
214 *pair = temp;
215
216 if(nextA){
217 if(nextA->acxx . . $ Pair#a..leafcxx . . $ Thread#leaf. == a) nextA->acxx . . $ Pair#a..prevcxx . . $ Thread#prev. = pair; else nextA->bcxx . . $ Pair#b..prevcxx . . $ Thread#prev. = pair;
218 }
219
220 if(nextB){
221 if(nextB->acxx . . $ Pair#a..leafcxx . . $ Thread#leaf. == b) nextB->acxx . . $ Pair#a..prevcxx . . $ Thread#prev. = pair; else nextB->bcxx . . $ Pair#b..prevcxx . . $ Thread#prev. = pair;
222 }
223 }
224
225
226 //MARK: Node Functions
227
228 static void
229 NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(cpBBTreecxx . . $ cpBBTree# *tree, Nodecxx . . $ Node# *node)
230 {
231 node->parentcxx . . $ Node#parent. = tree->pooledNodescxx . . $ cpBBTree#pooledNodes.;
232 tree->pooledNodescxx . . $ cpBBTree#pooledNodes. = node;
233 }
234
235 static Nodecxx . . $ Node# *
236 NodeFromPoolcxx . . $ NodeFromPool(5b2105d5feae57df).(cpBBTreecxx . . $ cpBBTree# *tree)
237 {
238 Nodecxx . . $ Node# *node = tree->pooledNodescxx . . $ cpBBTree#pooledNodes.;
239
240 if(node){
241 tree->pooledNodescxx . . $ cpBBTree#pooledNodes. = node->parentcxx . . $ Node#parent.;
242 return node;
243 } else {
244 // Pool is exhausted, make more
245 int count = CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!/sizeof(Nodecxx . . $ Node#);
246 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(count, "Internal Error: Buffer size is too small.");
247
248 Nodecxx . . $ Node# *buffer = (Nodecxx . . $ Node# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!);
249 cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(tree->allocatedBufferscxx . . $ cpBBTree#allocatedBuffers., buffer);
250
251 // push all but the first one, return the first instead
252 for(int i=1; i<count; i++) NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(tree, buffer + i);
253 return buffer;
254 }
255 }
256
257 static inline void
258 NodeSetAcxx . . $ NodeSetA(7c61d1aa32816ddb).(Nodecxx . . $ Node# *node, Nodecxx . . $ Node# *value)
259 {
260 node->Acxx . . $ `src/cpBBTree.c:64:9`! = value;
261 value->parentcxx . . $ Node#parent. = node;
262 }
263
264 static inline void
265 NodeSetBcxx . . $ NodeSetB(7c61d1aa32816ddb).(Nodecxx . . $ Node# *node, Nodecxx . . $ Node# *value)
266 {
267 node->Bcxx . . $ `src/cpBBTree.c:65:9`! = value;
268 value->parentcxx . . $ Node#parent. = node;
269 }
270
271 static Nodecxx . . $ Node# *
272 NodeNewcxx . . $ NodeNew(9bcc4196be3ab861).(cpBBTreecxx . . $ cpBBTree# *tree, Nodecxx . . $ Node# *a, Nodecxx . . $ Node# *b)
273 {
274 Nodecxx . . $ Node# *node = NodeFromPoolcxx . . $ NodeFromPool(5b2105d5feae57df).(tree);
275
276 node->objcxx . . $ Node#obj. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
277 node->bbcxx . . $ Node#bb. = cpBBMergecxx . . $ cpBBMerge(221d2b668d7f518c).(a->bbcxx . . $ Node#bb., b->bbcxx . . $ Node#bb.);
278 node->parentcxx . . $ Node#parent. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
279
280 NodeSetAcxx . . $ NodeSetA(7c61d1aa32816ddb).(node, a);
281 NodeSetBcxx . . $ NodeSetB(7c61d1aa32816ddb).(node, b);
282
283 return node;
284 }
285
286 static inline cpBoolcxx . . $ cpBool#
287 NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(Nodecxx . . $ Node# *node)
288 {
289 return (node->objcxx . . $ Node#obj. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
290 }
291
292 static inline Nodecxx . . $ Node# *
293 NodeOthercxx . . $ NodeOther(60243646e17f514c).(Nodecxx . . $ Node# *node, Nodecxx . . $ Node# *child)
294 {
295 return (node->Acxx . . $ `src/cpBBTree.c:64:9`! == child ? node->Bcxx . . $ `src/cpBBTree.c:65:9`! : node->Acxx . . $ `src/cpBBTree.c:64:9`!);
296 }
297
298 static inline void
299 NodeReplaceChildcxx . . $ NodeReplaceChild(63194d16f74f50f8).(Nodecxx . . $ Node# *parent, Nodecxx . . $ Node# *child, Nodecxx . . $ Node# *value, cpBBTreecxx . . $ cpBBTree# *tree)
300 {
301 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(!NodeIsLeaf(parent), "Internal Error: Cannot replace child of a leaf.");
302 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(child == parent->A || child == parent->B, "Internal Error: Node is not a child of parent.");
303
304 if(parent->Acxx . . $ `src/cpBBTree.c:64:9`! == child){
305 NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(tree, parent->Acxx . . $ `src/cpBBTree.c:64:9`!);
306 NodeSetAcxx . . $ NodeSetA(7c61d1aa32816ddb).(parent, value);
307 } else {
308 NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(tree, parent->Bcxx . . $ `src/cpBBTree.c:65:9`!);
309 NodeSetBcxx . . $ NodeSetB(7c61d1aa32816ddb).(parent, value);
310 }
311
312 for(Nodecxx . . $ Node# *node=parent; node; node = node->parentcxx . . $ Node#parent.){
313 node->bbcxx . . $ Node#bb. = cpBBMergecxx . . $ cpBBMerge(221d2b668d7f518c).(node->Acxx . . $ `src/cpBBTree.c:64:9`!->bbcxx . . $ Node#bb., node->Bcxx . . $ `src/cpBBTree.c:65:9`!->bbcxx . . $ Node#bb.);
314 }
315 }
316
317 //MARK: Subtree Functions
318
319 static inline cpFloatcxx . . $ cpFloat#
320 cpBBProximitycxx . . $ cpBBProximity(291d9f8b2ff41ca7).(cpBBcxx . . $ cpBB# a, cpBBcxx . . $ cpBB# b)
321 {
322 return cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a.lcxx . . $ cpBB#l. + a.rcxx . . $ cpBB#r. - b.lcxx . . $ cpBB#l. - b.rcxx . . $ cpBB#r.) + cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a.bcxx . . $ cpBB#b. + a.tcxx . . $ cpBB#t. - b.bcxx . . $ cpBB#b. - b.tcxx . . $ cpBB#t.);
323 }
324
325 static Nodecxx . . $ Node# *
326 SubtreeInsertcxx . . $ SubtreeInsert(6aefa1b3d00b890c).(Nodecxx . . $ Node# *subtree, Nodecxx . . $ Node# *leaf, cpBBTreecxx . . $ cpBBTree# *tree)
327 {
328 if(subtree == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!){
329 return leaf;
330 } else if(NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(subtree)){
331 return NodeNewcxx . . $ NodeNew(9bcc4196be3ab861).(tree, leaf, subtree);
332 } else {
333 cpFloatcxx . . $ cpFloat# cost_a = cpBBAreacxx . . $ cpBBArea(4dde8643f7518d47).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!->bbcxx . . $ Node#bb.) + cpBBMergedAreacxx . . $ cpBBMergedArea(291d9f8b2ff41ca7).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!->bbcxx . . $ Node#bb., leaf->bbcxx . . $ Node#bb.);
334 cpFloatcxx . . $ cpFloat# cost_b = cpBBAreacxx . . $ cpBBArea(4dde8643f7518d47).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!->bbcxx . . $ Node#bb.) + cpBBMergedAreacxx . . $ cpBBMergedArea(291d9f8b2ff41ca7).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!->bbcxx . . $ Node#bb., leaf->bbcxx . . $ Node#bb.);
335
336 if(cost_a == cost_b){
337 cost_a = cpBBProximitycxx . . $ cpBBProximity(291d9f8b2ff41ca7).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!->bbcxx . . $ Node#bb., leaf->bbcxx . . $ Node#bb.);
338 cost_b = cpBBProximitycxx . . $ cpBBProximity(291d9f8b2ff41ca7).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!->bbcxx . . $ Node#bb., leaf->bbcxx . . $ Node#bb.);
339 }
340
341 if(cost_b < cost_a){
342 NodeSetBcxx . . $ NodeSetB(7c61d1aa32816ddb).(subtree, SubtreeInsertcxx . . $ SubtreeInsert(6aefa1b3d00b890c).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!, leaf, tree));
343 } else {
344 NodeSetAcxx . . $ NodeSetA(7c61d1aa32816ddb).(subtree, SubtreeInsertcxx . . $ SubtreeInsert(6aefa1b3d00b890c).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!, leaf, tree));
345 }
346
347 subtree->bbcxx . . $ Node#bb. = cpBBMergecxx . . $ cpBBMerge(221d2b668d7f518c).(subtree->bbcxx . . $ Node#bb., leaf->bbcxx . . $ Node#bb.);
348 return subtree;
349 }
350 }
351
352 static void
353 SubtreeQuerycxx . . $ SubtreeQuery(d14383f856042020).(Nodecxx . . $ Node# *subtree, void *obj, cpBBcxx . . $ cpBB# bb, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data)
354 {
355 if(cpBBIntersectscxx . . $ cpBBIntersects(aa08641fa2db23dc).(subtree->bbcxx . . $ Node#bb., bb)){
356 if(NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(subtree)){
357 func(obj, subtree->objcxx . . $ Node#obj., 0, data);
358 } else {
359 SubtreeQuerycxx . . $ SubtreeQuery(d14383f856042020).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!, obj, bb, func, data);
360 SubtreeQuerycxx . . $ SubtreeQuery(d14383f856042020).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!, obj, bb, func, data);
361 }
362 }
363 }
364
365
366 static cpFloatcxx . . $ cpFloat#
367 SubtreeSegmentQuerycxx . . $ SubtreeSegmentQuery(b6c825267d3151a7).(Nodecxx . . $ Node# *subtree, void *obj, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# t_exit, cpSpatialIndexSegmentQueryFunccxx . . $ cpSpatialIndexSegmentQueryFunc# func, void *data)
368 {
369 if(NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(subtree)){
370 return func(obj, subtree->objcxx . . $ Node#obj., data);
371 } else {
372 cpFloatcxx . . $ cpFloat# t_a = cpBBSegmentQuerycxx . . $ cpBBSegmentQuery(41f817dc1569e8d5).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!->bbcxx . . $ Node#bb., a, b);
373 cpFloatcxx . . $ cpFloat# t_b = cpBBSegmentQuerycxx . . $ cpBBSegmentQuery(41f817dc1569e8d5).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!->bbcxx . . $ Node#bb., a, b);
374
375 if(t_a < t_b){
376 if(t_a < t_exit) t_exit = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(t_exit, SubtreeSegmentQuerycxx . . $ SubtreeSegmentQuery(b6c825267d3151a7).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!, obj, a, b, t_exit, func, data));
377 if(t_b < t_exit) t_exit = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(t_exit, SubtreeSegmentQuerycxx . . $ SubtreeSegmentQuery(b6c825267d3151a7).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!, obj, a, b, t_exit, func, data));
378 } else {
379 if(t_b < t_exit) t_exit = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(t_exit, SubtreeSegmentQuerycxx . . $ SubtreeSegmentQuery(b6c825267d3151a7).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!, obj, a, b, t_exit, func, data));
380 if(t_a < t_exit) t_exit = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(t_exit, SubtreeSegmentQuerycxx . . $ SubtreeSegmentQuery(b6c825267d3151a7).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!, obj, a, b, t_exit, func, data));
381 }
382
383 return t_exit;
384 }
385 }
386
387 static void
388 SubtreeRecyclecxx . . $ SubtreeRecycle(a08d3bcd32232a53).(cpBBTreecxx . . $ cpBBTree# *tree, Nodecxx . . $ Node# *node)
389 {
390 if(!NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(node)){
391 SubtreeRecyclecxx . . $ SubtreeRecycle(a08d3bcd32232a53).(tree, node->Acxx . . $ `src/cpBBTree.c:64:9`!);
392 SubtreeRecyclecxx . . $ SubtreeRecycle(a08d3bcd32232a53).(tree, node->Bcxx . . $ `src/cpBBTree.c:65:9`!);
393 NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(tree, node);
394 }
395 }
396
397 static inline Nodecxx . . $ Node# *
398 SubtreeRemovecxx . . $ SubtreeRemove(6aefa1b3d00b890c).(Nodecxx . . $ Node# *subtree, Nodecxx . . $ Node# *leaf, cpBBTreecxx . . $ cpBBTree# *tree)
399 {
400 if(leaf == subtree){
401 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
402 } else {
403 Nodecxx . . $ Node# *parent = leaf->parentcxx . . $ Node#parent.;
404 if(parent == subtree){
405 Nodecxx . . $ Node# *other = NodeOthercxx . . $ NodeOther(60243646e17f514c).(subtree, leaf);
406 other->parentcxx . . $ Node#parent. = subtree->parentcxx . . $ Node#parent.;
407 NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(tree, subtree);
408 return other;
409 } else {
410 NodeReplaceChildcxx . . $ NodeReplaceChild(63194d16f74f50f8).(parent->parentcxx . . $ Node#parent., parent, NodeOthercxx . . $ NodeOther(60243646e17f514c).(parent, leaf), tree);
411 return subtree;
412 }
413 }
414 }
415
416 //MARK: Marking Functions
417
418 typedef struct MarkContextcxx . . $ MarkContext# {
419 cpBBTreecxx . . $ cpBBTree# *treecxx . . $ MarkContext#tree.;
420 Nodecxx . . $ Node# *staticRootcxx . . $ MarkContext#staticRoot.;
421 cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# funccxx . . $ MarkContext#func.;
422 void *datacxx . . $ MarkContext#data.;
423 } MarkContextcxx . . $ MarkContext#;
424
425 static void
426 MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(Nodecxx . . $ Node# *subtree, Nodecxx . . $ Node# *leaf, cpBoolcxx . . $ cpBool# left, MarkContextcxx . . $ MarkContext# *context)
427 {
428 if(cpBBIntersectscxx . . $ cpBBIntersects(aa08641fa2db23dc).(leaf->bbcxx . . $ Node#bb., subtree->bbcxx . . $ Node#bb.)){
429 if(NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(subtree)){
430 if(left){
431 PairInsertcxx . . $ PairInsert(31dda1a45e8ef13d).(leaf, subtree, context->treecxx . . $ MarkContext#tree.);
432 } else {
433 if(subtree->STAMPcxx . . $ `src/cpBBTree.c:66:9`! < leaf->STAMPcxx . . $ `src/cpBBTree.c:66:9`!) PairInsertcxx . . $ PairInsert(31dda1a45e8ef13d).(subtree, leaf, context->treecxx . . $ MarkContext#tree.);
434 context->funccxx . . $ MarkContext#func.(leaf->objcxx . . $ Node#obj., subtree->objcxx . . $ Node#obj., 0, context->datacxx . . $ MarkContext#data.);
435 }
436 } else {
437 MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!, leaf, left, context);
438 MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!, leaf, left, context);
439 }
440 }
441 }
442
443 static void
444 MarkLeafcxx . . $ MarkLeaf(58dd54b63b0599de).(Nodecxx . . $ Node# *leaf, MarkContextcxx . . $ MarkContext# *context)
445 {
446 cpBBTreecxx . . $ cpBBTree# *tree = context->treecxx . . $ MarkContext#tree.;
447 if(leaf->STAMPcxx . . $ `src/cpBBTree.c:66:9`! == GetMasterTreecxx . . $ GetMasterTree(b05b809c0d2f3f80).(tree)->stampcxx . . $ cpBBTree#stamp.){
448 Nodecxx . . $ Node# *staticRoot = context->staticRootcxx . . $ MarkContext#staticRoot.;
449 if(staticRoot) MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(staticRoot, leaf, cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!, context);
450
451 for(Nodecxx . . $ Node# *node = leaf; node->parentcxx . . $ Node#parent.; node = node->parentcxx . . $ Node#parent.){
452 if(node == node->parentcxx . . $ Node#parent.->Acxx . . $ `src/cpBBTree.c:64:9`!){
453 MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(node->parentcxx . . $ Node#parent.->Bcxx . . $ `src/cpBBTree.c:65:9`!, leaf, cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!, context);
454 } else {
455 MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(node->parentcxx . . $ Node#parent.->Acxx . . $ `src/cpBBTree.c:64:9`!, leaf, cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!, context);
456 }
457 }
458 } else {
459 Paircxx . . $ Pair# *pair = leaf->PAIRScxx . . $ `src/cpBBTree.c:67:9`!;
460 while(pair){
461 if(leaf == pair->bcxx . . $ Pair#b..leafcxx . . $ Thread#leaf.){
462 pair->idcxx . . $ Pair#id. = context->funccxx . . $ MarkContext#func.(pair->acxx . . $ Pair#a..leafcxx . . $ Thread#leaf.->objcxx . . $ Node#obj., leaf->objcxx . . $ Node#obj., pair->idcxx . . $ Pair#id., context->datacxx . . $ MarkContext#data.);
463 pair = pair->bcxx . . $ Pair#b..nextcxx . . $ Thread#next.;
464 } else {
465 pair = pair->acxx . . $ Pair#a..nextcxx . . $ Thread#next.;
466 }
467 }
468 }
469 }
470
471 static void
472 MarkSubtreecxx . . $ MarkSubtree(58dd54b63b0599de).(Nodecxx . . $ Node# *subtree, MarkContextcxx . . $ MarkContext# *context)
473 {
474 if(NodeIsLeafcxx . . $ NodeIsLeaf(20a95657f105504f).(subtree)){
475 MarkLeafcxx . . $ MarkLeaf(58dd54b63b0599de).(subtree, context);
476 } else {
477 MarkSubtreecxx . . $ MarkSubtree(58dd54b63b0599de).(subtree->Acxx . . $ `src/cpBBTree.c:64:9`!, context);
478 MarkSubtreecxx . . $ MarkSubtree(58dd54b63b0599de).(subtree->Bcxx . . $ `src/cpBBTree.c:65:9`!, context); // TODO: Force TCO here?
479 }
480 }
481
482 //MARK: Leaf Functions
483
484 static Nodecxx . . $ Node# *
485 LeafNewcxx . . $ LeafNew(83387b34a7673499).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpBBcxx . . $ cpBB# bb)
486 {
487 Nodecxx . . $ Node# *node = NodeFromPoolcxx . . $ NodeFromPool(5b2105d5feae57df).(tree);
488 node->objcxx . . $ Node#obj. = obj;
489 node->bbcxx . . $ Node#bb. = GetBBcxx . . $ GetBB(6a16b80027b3111).(tree, obj);
490
491 node->parentcxx . . $ Node#parent. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
492 node->STAMPcxx . . $ `src/cpBBTree.c:66:9`! = 0;
493 node->PAIRScxx . . $ `src/cpBBTree.c:67:9`! = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
494
495 return node;
496 }
497
498 static cpBoolcxx . . $ cpBool#
499 LeafUpdatecxx . . $ LeafUpdate(f5f9d582c894ed28).(Nodecxx . . $ Node# *leaf, cpBBTreecxx . . $ cpBBTree# *tree)
500 {
501 Nodecxx . . $ Node# *root = tree->rootcxx . . $ cpBBTree#root.;
502 cpBBcxx . . $ cpBB# bb = tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(leaf->objcxx . . $ Node#obj.);
503
504 if(!cpBBContainsBBcxx . . $ cpBBContainsBB(aa08641fa2db23dc).(leaf->bbcxx . . $ Node#bb., bb)){
505 leaf->bbcxx . . $ Node#bb. = GetBBcxx . . $ GetBB(6a16b80027b3111).(tree, leaf->objcxx . . $ Node#obj.);
506
507 root = SubtreeRemovecxx . . $ SubtreeRemove(6aefa1b3d00b890c).(root, leaf, tree);
508 tree->rootcxx . . $ cpBBTree#root. = SubtreeInsertcxx . . $ SubtreeInsert(6aefa1b3d00b890c).(root, leaf, tree);
509
510 PairsClearcxx . . $ PairsClear(e3adc038f4e64d54).(leaf, tree);
511 leaf->STAMPcxx . . $ `src/cpBBTree.c:66:9`! = GetMasterTreecxx . . $ GetMasterTree(b05b809c0d2f3f80).(tree)->stampcxx . . $ cpBBTree#stamp.;
512
513 return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!;
514 } else {
515 return cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!;
516 }
517 }
518
519 static cpCollisionIDcxx . . $ cpCollisionID# VoidQueryFunccxx . . $ VoidQueryFunc(cdf29d9ef0944563).(void *obj1, void *obj2, cpCollisionIDcxx . . $ cpCollisionID# id, void *data){return id;}
520
521 static void
522 LeafAddPairscxx . . $ LeafAddPairs(e3adc038f4e64d54).(Nodecxx . . $ Node# *leaf, cpBBTreecxx . . $ cpBBTree# *tree)
523 {
524 cpSpatialIndexcxx . . $ cpSpatialIndex# *dynamicIndex = tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..dynamicIndexcxx . . $ cpSpatialIndex#dynamicIndex.;
525 if(dynamicIndex){
526 Nodecxx . . $ Node# *dynamicRoot = GetRootIfTreecxx . . $ GetRootIfTree(7ff26f7bdc470982).(dynamicIndex);
527 if(dynamicRoot){
528 cpBBTreecxx . . $ cpBBTree# *dynamicTree = GetTreecxx . . $ GetTree(4790b17def89f44).(dynamicIndex);
529 MarkContextcxx . . $ MarkContext# context = {dynamicTree, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
530 MarkLeafQuerycxx . . $ MarkLeafQuery(49259b144c4e3481).(dynamicRoot, leaf, cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!, &context);
531 }
532 } else {
533 Nodecxx . . $ Node# *staticRoot = GetRootIfTreecxx . . $ GetRootIfTree(7ff26f7bdc470982).(tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..staticIndexcxx . . $ cpSpatialIndex#staticIndex.);
534 MarkContextcxx . . $ MarkContext# context = {tree, staticRoot, VoidQueryFunccxx . . $ VoidQueryFunc(cdf29d9ef0944563)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
535 MarkLeafcxx . . $ MarkLeaf(58dd54b63b0599de).(leaf, &context);
536 }
537 }
538
539 //MARK: Memory Management Functions
540
541 cpBBTreecxx . . $ cpBBTree# *
542 cpBBTreeAlloccxx . . $ cpBBTreeAlloc(bfe4e7b8537c582).(void)
543 {
544 return (cpBBTreecxx . . $ cpBBTree# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpBBTreecxx . . $ cpBBTree#));
545 }
546
547 static int
548 leafSetEqlcxx . . $ leafSetEql(43562d0134087de).(void *obj, Nodecxx . . $ Node# *node)
549 {
550 return (obj == node->objcxx . . $ Node#obj.);
551 }
552
553 static void *
554 leafSetTranscxx . . $ leafSetTrans(174608f7652d3fd).(void *obj, cpBBTreecxx . . $ cpBBTree# *tree)
555 {
556 return LeafNewcxx . . $ LeafNew(83387b34a7673499).(tree, obj, tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(obj));
557 }
558
559 cpSpatialIndexcxx . . $ cpSpatialIndex# *
560 cpBBTreeInitcxx . . $ cpBBTreeInit(cb5af89dedf27576).(cpBBTreecxx . . $ cpBBTree# *tree, cpSpatialIndexBBFunccxx . . $ cpSpatialIndexBBFunc# bbfunc, cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex)
561 {
562 cpSpatialIndexInitcxx . . $ cpSpatialIndexInit(57a924cefa208dd7).((cpSpatialIndexcxx . . $ cpSpatialIndex# *)tree, Klasscxx . . $ Klass(292ec58cde6c1067).(), bbfunc, staticIndex);
563
564 tree->velocityFunccxx . . $ cpBBTree#velocityFunc. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
565
566 tree->leavescxx . . $ cpBBTree#leaves. = cpHashSetNewcxx . . $ cpHashSetNew(1aa2f270b6b730e1).(0, (cpHashSetEqlFunccxx . . $ cpHashSetEqlFunc#)leafSetEqlcxx . . $ leafSetEql(43562d0134087de).);
567 tree->rootcxx . . $ cpBBTree#root. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
568
569 tree->pooledNodescxx . . $ cpBBTree#pooledNodes. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
570 tree->allocatedBufferscxx . . $ cpBBTree#allocatedBuffers. = cpArrayNewcxx . . $ cpArrayNew(f90d83c90697c3ae).(0);
571
572 tree->stampcxx . . $ cpBBTree#stamp. = 0;
573
574 return (cpSpatialIndexcxx . . $ cpSpatialIndex# *)tree;
575 }
576
577 void
578 cpBBTreeSetVelocityFunccxx . . $ cpBBTreeSetVelocityFunc(770501a3255b33f).(cpSpatialIndexcxx . . $ cpSpatialIndex# *index, cpBBTreeVelocityFunccxx . . $ cpBBTreeVelocityFunc# func)
579 {
580 if(index->klasscxx . . $ cpSpatialIndex#klass. != Klasscxx . . $ Klass(292ec58cde6c1067).()){
581 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(cpFalse, "Ignoring cpBBTreeSetVelocityFunc() call to non-tree spatial index.");
582 return;
583 }
584
585 ((cpBBTreecxx . . $ cpBBTree# *)index)->velocityFunccxx . . $ cpBBTree#velocityFunc. = func;
586 }
587
588 cpSpatialIndexcxx . . $ cpSpatialIndex# *
589 cpBBTreeNewcxx . . $ cpBBTreeNew(ccce0ca6945a4614).(cpSpatialIndexBBFunccxx . . $ cpSpatialIndexBBFunc# bbfunc, cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex)
590 {
591 return cpBBTreeInitcxx . . $ cpBBTreeInit(cb5af89dedf27576).(cpBBTreeAlloccxx . . $ cpBBTreeAlloc(bfe4e7b8537c582).(), bbfunc, staticIndex);
592 }
593
594 static void
595 cpBBTreeDestroycxx . . $ cpBBTreeDestroy(d63f4d5b2d9b584d).(cpBBTreecxx . . $ cpBBTree# *tree)
596 {
597 cpHashSetFreecxx . . $ cpHashSetFree(a5aef454cd7046d4).(tree->leavescxx . . $ cpBBTree#leaves.);
598
599 if(tree->allocatedBufferscxx . . $ cpBBTree#allocatedBuffers.) cpArrayFreeEachcxx . . $ cpArrayFreeEach(d583da02e3252d4f).(tree->allocatedBufferscxx . . $ cpBBTree#allocatedBuffers., cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!);
600 cpArrayFreecxx . . $ cpArrayFree(9f115453462bfa05).(tree->allocatedBufferscxx . . $ cpBBTree#allocatedBuffers.);
601 }
602
603 //MARK: Insert/Remove
604
605 static void
606 cpBBTreeInsertcxx . . $ cpBBTreeInsert(1f6bc90cb87ee5c3).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
607 {
608 Nodecxx . . $ Node# *leaf = (Nodecxx . . $ Node# *)cpHashSetInsertcxx . . $ cpHashSetInsert(65a72e7022b62d4d).(tree->leavescxx . . $ cpBBTree#leaves., hashid, obj, (cpHashSetTransFunccxx . . $ cpHashSetTransFunc#)leafSetTranscxx . . $ leafSetTrans(174608f7652d3fd)., tree);
609
610 Nodecxx . . $ Node# *root = tree->rootcxx . . $ cpBBTree#root.;
611 tree->rootcxx . . $ cpBBTree#root. = SubtreeInsertcxx . . $ SubtreeInsert(6aefa1b3d00b890c).(root, leaf, tree);
612
613 leaf->STAMPcxx . . $ `src/cpBBTree.c:66:9`! = GetMasterTreecxx . . $ GetMasterTree(b05b809c0d2f3f80).(tree)->stampcxx . . $ cpBBTree#stamp.;
614 LeafAddPairscxx . . $ LeafAddPairs(e3adc038f4e64d54).(leaf, tree);
615 IncrementStampcxx . . $ IncrementStamp(d63f4d5b2d9b584d).(tree);
616 }
617
618 static void
619 cpBBTreeRemovecxx . . $ cpBBTreeRemove(1f6bc90cb87ee5c3).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
620 {
621 Nodecxx . . $ Node# *leaf = (Nodecxx . . $ Node# *)cpHashSetRemovecxx . . $ cpHashSetRemove(82fb1457cc8dfa6c).(tree->leavescxx . . $ cpBBTree#leaves., hashid, obj);
622
623 tree->rootcxx . . $ cpBBTree#root. = SubtreeRemovecxx . . $ SubtreeRemove(6aefa1b3d00b890c).(tree->rootcxx . . $ cpBBTree#root., leaf, tree);
624 PairsClearcxx . . $ PairsClear(e3adc038f4e64d54).(leaf, tree);
625 NodeRecyclecxx . . $ NodeRecycle(a08d3bcd32232a53).(tree, leaf);
626 }
627
628 static cpBoolcxx . . $ cpBool#
629 cpBBTreeContainscxx . . $ cpBBTreeContains(b3538fb662e87058).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
630 {
631 return (cpHashSetFindcxx . . $ cpHashSetFind(82fb1457cc8dfa6c).(tree->leavescxx . . $ cpBBTree#leaves., hashid, obj) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
632 }
633
634 //MARK: Reindex
635
636 static void LeafUpdateWrapcxx . . $ LeafUpdateWrap(e3adc038f4e64d54).(Nodecxx . . $ Node# *leaf, cpBBTreecxx . . $ cpBBTree# *tree) {LeafUpdatecxx . . $ LeafUpdate(f5f9d582c894ed28).(leaf, tree);}
637
638 static void
639 cpBBTreeReindexQuerycxx . . $ cpBBTreeReindexQuery(a539aab076c29e60).(cpBBTreecxx . . $ cpBBTree# *tree, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data)
640 {
641 if(!tree->rootcxx . . $ cpBBTree#root.) return;
642
643 // LeafUpdate() may modify tree->root. Don't cache it.
644 cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(tree->leavescxx . . $ cpBBTree#leaves., (cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc#)LeafUpdateWrapcxx . . $ LeafUpdateWrap(e3adc038f4e64d54)., tree);
645
646 cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex = tree->spatialIndexcxx . . $ cpBBTree#spatialIndex..staticIndexcxx . . $ cpSpatialIndex#staticIndex.;
647 Nodecxx . . $ Node# *staticRoot = (staticIndex && staticIndex->klasscxx . . $ cpSpatialIndex#klass. == Klasscxx . . $ Klass(292ec58cde6c1067).() ? ((cpBBTreecxx . . $ cpBBTree# *)staticIndex)->rootcxx . . $ cpBBTree#root. : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
648
649 MarkContextcxx . . $ MarkContext# context = {tree, staticRoot, func, data};
650 MarkSubtreecxx . . $ MarkSubtree(58dd54b63b0599de).(tree->rootcxx . . $ cpBBTree#root., &context);
651 if(staticIndex && !staticRoot) cpSpatialIndexCollideStaticcxx . . $ cpSpatialIndexCollideStatic(3c6f473dc08bdfa1).((cpSpatialIndexcxx . . $ cpSpatialIndex# *)tree, staticIndex, func, data);
652
653 IncrementStampcxx . . $ IncrementStamp(d63f4d5b2d9b584d).(tree);
654 }
655
656 static void
657 cpBBTreeReindexcxx . . $ cpBBTreeReindex(d63f4d5b2d9b584d).(cpBBTreecxx . . $ cpBBTree# *tree)
658 {
659 cpBBTreeReindexQuerycxx . . $ cpBBTreeReindexQuery(a539aab076c29e60).(tree, VoidQueryFunccxx . . $ VoidQueryFunc(cdf29d9ef0944563)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
660 }
661
662 static void
663 cpBBTreeReindexObjectcxx . . $ cpBBTreeReindexObject(1f6bc90cb87ee5c3).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
664 {
665 Nodecxx . . $ Node# *leaf = (Nodecxx . . $ Node# *)cpHashSetFindcxx . . $ cpHashSetFind(82fb1457cc8dfa6c).(tree->leavescxx . . $ cpBBTree#leaves., hashid, obj);
666 if(leaf){
667 if(LeafUpdatecxx . . $ LeafUpdate(f5f9d582c894ed28).(leaf, tree)) LeafAddPairscxx . . $ LeafAddPairs(e3adc038f4e64d54).(leaf, tree);
668 IncrementStampcxx . . $ IncrementStamp(d63f4d5b2d9b584d).(tree);
669 }
670 }
671
672 //MARK: Query
673
674 static void
675 cpBBTreeSegmentQuerycxx . . $ cpBBTreeSegmentQuery(43cb2d5e85fa3096).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# t_exit, cpSpatialIndexSegmentQueryFunccxx . . $ cpSpatialIndexSegmentQueryFunc# func, void *data)
676 {
677 Nodecxx . . $ Node# *root = tree->rootcxx . . $ cpBBTree#root.;
678 if(root) SubtreeSegmentQuerycxx . . $ SubtreeSegmentQuery(b6c825267d3151a7).(root, obj, a, b, t_exit, func, data);
679 }
680
681 static void
682 cpBBTreeQuerycxx . . $ cpBBTreeQuery(55c16547f2e36a5a).(cpBBTreecxx . . $ cpBBTree# *tree, void *obj, cpBBcxx . . $ cpBB# bb, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data)
683 {
684 if(tree->rootcxx . . $ cpBBTree#root.) SubtreeQuerycxx . . $ SubtreeQuery(d14383f856042020).(tree->rootcxx . . $ cpBBTree#root., obj, bb, func, data);
685 }
686
687 //MARK: Misc
688
689 static int
690 cpBBTreeCountcxx . . $ cpBBTreeCount(7a2111fdc419611e).(cpBBTreecxx . . $ cpBBTree# *tree)
691 {
692 return cpHashSetCountcxx . . $ cpHashSetCount(e246982534155f1a).(tree->leavescxx . . $ cpBBTree#leaves.);
693 }
694
695 typedef struct eachContextcxx . . $ eachContext# {
696 cpSpatialIndexIteratorFunccxx . . $ cpSpatialIndexIteratorFunc# funccxx . . $ eachContext#func.;
697 void *datacxx . . $ eachContext#data.;
698 } eachContextcxx . . $ eachContext#;
699
700 static void each_helpercxx . . $ each_helper(7b0660dd469976fc).(Nodecxx . . $ Node# *node, eachContextcxx . . $ eachContext# *context){context->funccxx . . $ eachContext#func.(node->objcxx . . $ Node#obj., context->datacxx . . $ eachContext#data.);}
701
702 static void
703 cpBBTreeEachcxx . . $ cpBBTreeEach(184954658a6278fd).(cpBBTreecxx . . $ cpBBTree# *tree, cpSpatialIndexIteratorFunccxx . . $ cpSpatialIndexIteratorFunc# func, void *data)
704 {
705 eachContextcxx . . $ eachContext# context = {func, data};
706 cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(tree->leavescxx . . $ cpBBTree#leaves., (cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc#)each_helpercxx . . $ each_helper(7b0660dd469976fc)., &context);
707 }
708
709 static cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# klasscxx . . $ klass. = {
710 (cpSpatialIndexDestroyImplcxx . . $ cpSpatialIndexDestroyImpl#)cpBBTreeDestroycxx . . $ cpBBTreeDestroy(d63f4d5b2d9b584d).,
711
712 (cpSpatialIndexCountImplcxx . . $ cpSpatialIndexCountImpl#)cpBBTreeCountcxx . . $ cpBBTreeCount(7a2111fdc419611e).,
713 (cpSpatialIndexEachImplcxx . . $ cpSpatialIndexEachImpl#)cpBBTreeEachcxx . . $ cpBBTreeEach(184954658a6278fd).,
714
715 (cpSpatialIndexContainsImplcxx . . $ cpSpatialIndexContainsImpl#)cpBBTreeContainscxx . . $ cpBBTreeContains(b3538fb662e87058).,
716 (cpSpatialIndexInsertImplcxx . . $ cpSpatialIndexInsertImpl#)cpBBTreeInsertcxx . . $ cpBBTreeInsert(1f6bc90cb87ee5c3).,
717 (cpSpatialIndexRemoveImplcxx . . $ cpSpatialIndexRemoveImpl#)cpBBTreeRemovecxx . . $ cpBBTreeRemove(1f6bc90cb87ee5c3).,
718
719 (cpSpatialIndexReindexImplcxx . . $ cpSpatialIndexReindexImpl#)cpBBTreeReindexcxx . . $ cpBBTreeReindex(d63f4d5b2d9b584d).,
720 (cpSpatialIndexReindexObjectImplcxx . . $ cpSpatialIndexReindexObjectImpl#)cpBBTreeReindexObjectcxx . . $ cpBBTreeReindexObject(1f6bc90cb87ee5c3).,
721 (cpSpatialIndexReindexQueryImplcxx . . $ cpSpatialIndexReindexQueryImpl#)cpBBTreeReindexQuerycxx . . $ cpBBTreeReindexQuery(a539aab076c29e60).,
722
723 (cpSpatialIndexQueryImplcxx . . $ cpSpatialIndexQueryImpl#)cpBBTreeQuerycxx . . $ cpBBTreeQuery(55c16547f2e36a5a).,
724 (cpSpatialIndexSegmentQueryImplcxx . . $ cpSpatialIndexSegmentQueryImpl#)cpBBTreeSegmentQuerycxx . . $ cpBBTreeSegmentQuery(43cb2d5e85fa3096).,
725 };
726
727 static inline cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# *Klasscxx . . $ Klass(292ec58cde6c1067).(){return &klasscxx . . $ klass.;}
728
729
730 //MARK: Tree Optimization
731
732 static int
733 cpfcomparecxx . . $ cpfcompare(3317d1dc59426d1).(const cpFloatcxx . . $ cpFloat# *a, const cpFloatcxx . . $ cpFloat# *b){
734 return (*a < *b ? -1 : (*b < *a ? 1 : 0));
735 }
736
737 static void
738 fillNodeArraycxx . . $ fillNodeArray(a71707e5ca92eeee).(Nodecxx . . $ Node# *node, Nodecxx . . $ Node# ***cursor){
739 (**cursor) = node;
740 (*cursor)++;
741 }
742
743 static Nodecxx . . $ Node# *
744 partitionNodescxx . . $ partitionNodes(6cdc094fc06cd7e8).(cpBBTreecxx . . $ cpBBTree# *tree, Nodecxx . . $ Node# **nodes, int count)
745 {
746 if(count == 1){
747 return nodes[0];
748 } else if(count == 2) {
749 return NodeNewcxx . . $ NodeNew(9bcc4196be3ab861).(tree, nodes[0], nodes[1]);
750 }
751
752 // Find the AABB for these nodes
753 cpBBcxx . . $ cpBB# bb = nodes[0]->bbcxx . . $ Node#bb.;
754 for(int i=1; i<count; i++) bb = cpBBMergecxx . . $ cpBBMerge(221d2b668d7f518c).(bb, nodes[i]->bbcxx . . $ Node#bb.);
755
756 // Split it on it's longest axis
757 cpBoolcxx . . $ cpBool# splitWidth = (bb.rcxx . . $ cpBB#r. - bb.lcxx . . $ cpBB#l. > bb.tcxx . . $ cpBB#t. - bb.bcxx . . $ cpBB#b.);
758
759 // Sort the bounds and use the median as the splitting point
760 cpFloatcxx . . $ cpFloat# *bounds = (cpFloatcxx . . $ cpFloat# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(count*2, sizeof(cpFloatcxx . . $ cpFloat#));
761 if(splitWidth){
762 for(int i=0; i<count; i++){
763 bounds[2*i + 0] = nodes[i]->bbcxx . . $ Node#bb..lcxx . . $ cpBB#l.;
764 bounds[2*i + 1] = nodes[i]->bbcxx . . $ Node#bb..rcxx . . $ cpBB#r.;
765 }
766 } else {
767 for(int i=0; i<count; i++){
768 bounds[2*i + 0] = nodes[i]->bbcxx . . $ Node#bb..bcxx . . $ cpBB#b.;
769 bounds[2*i + 1] = nodes[i]->bbcxx . . $ Node#bb..tcxx . . $ cpBB#t.;
770 }
771 }
772
773 qsortcxx . . $ qsort(ebb60b13a65e77d7).(bounds, count*2, sizeof(cpFloatcxx . . $ cpFloat#), (int (*)(const void *, const void *))cpfcomparecxx . . $ cpfcompare(3317d1dc59426d1).);
774 cpFloatcxx . . $ cpFloat# split = (bounds[count - 1] + bounds[count])*0.5f; // use the medain as the split
775 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(bounds);
776
777 // Generate the child BBs
778 cpBBcxx . . $ cpBB# a = bb, b = bb;
779 if(splitWidth) a.rcxx . . $ cpBB#r. = b.lcxx . . $ cpBB#l. = split; else a.tcxx . . $ cpBB#t. = b.bcxx . . $ cpBB#b. = split;
780
781 // Partition the nodes
782 int right = count;
783 for(int left=0; left < right;){
784 Nodecxx . . $ Node# *node = nodes[left];
785 if(cpBBMergedAreacxx . . $ cpBBMergedArea(291d9f8b2ff41ca7).(node->bbcxx . . $ Node#bb., b) < cpBBMergedAreacxx . . $ cpBBMergedArea(291d9f8b2ff41ca7).(node->bbcxx . . $ Node#bb., a)){
786 // if(cpBBProximity(node->bb, b) < cpBBProximity(node->bb, a)){
787 right--;
788 nodes[left] = nodes[right];
789 nodes[right] = node;
790 } else {
791 left++;
792 }
793 }
794
795 if(right == count){
796 Nodecxx . . $ Node# *node = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
797 for(int i=0; i<count; i++) node = SubtreeInsertcxx . . $ SubtreeInsert(6aefa1b3d00b890c).(node, nodes[i], tree);
798 return node;
799 }
800
801 // Recurse and build the node!
802 return NodeNewcxx . . $ NodeNew(9bcc4196be3ab861).(tree,
803 partitionNodescxx . . $ partitionNodes(6cdc094fc06cd7e8).(tree, nodes, right),
804 partitionNodescxx . . $ partitionNodes(6cdc094fc06cd7e8).(tree, nodes + right, count - right)
805 );
806 }
807
808 //static void
809 //cpBBTreeOptimizeIncremental(cpBBTree *tree, int passes)
810 //{
811 // for(int i=0; i<passes; i++){
812 // Node *root = tree->root;
813 // Node *node = root;
814 // int bit = 0;
815 // unsigned int path = tree->opath;
816 //
817 // while(!NodeIsLeaf(node)){
818 // node = (path&(1<<bit) ? node->a : node->b);
819 // bit = (bit + 1)&(sizeof(unsigned int)*8 - 1);
820 // }
821 //
822 // root = subtreeRemove(root, node, tree);
823 // tree->root = subtreeInsert(root, node, tree);
824 // }
825 //}
826
827 void
828 cpBBTreeOptimizecxx . . $ cpBBTreeOptimize(4404d1a49466a4e0).(cpSpatialIndexcxx . . $ cpSpatialIndex# *index)
829 {
830 if(index->klasscxx . . $ cpSpatialIndex#klass. != &klasscxx . . $ klass.){
831 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(cpFalse, "Ignoring cpBBTreeOptimize() call to non-tree spatial index.");
832 return;
833 }
834
835 cpBBTreecxx . . $ cpBBTree# *tree = (cpBBTreecxx . . $ cpBBTree# *)index;
836 Nodecxx . . $ Node# *root = tree->rootcxx . . $ cpBBTree#root.;
837 if(!root) return;
838
839 int count = cpBBTreeCountcxx . . $ cpBBTreeCount(7a2111fdc419611e).(tree);
840 Nodecxx . . $ Node# **nodes = (Nodecxx . . $ Node# **)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(count, sizeof(Nodecxx . . $ Node# *));
841 Nodecxx . . $ Node# **cursor = nodes;
842
843 cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(tree->leavescxx . . $ cpBBTree#leaves., (cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc#)fillNodeArraycxx . . $ fillNodeArray(a71707e5ca92eeee)., &cursor);
844
845 SubtreeRecyclecxx . . $ SubtreeRecycle(a08d3bcd32232a53).(tree, root);
846 tree->rootcxx . . $ cpBBTree#root. = partitionNodescxx . . $ partitionNodes(6cdc094fc06cd7e8).(tree, nodes, count);
847 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(nodes);
848 }
849
850 //MARK: Debug Draw
851
852 //#define CP_BBTREE_DEBUG_DRAW
853 #ifdef CP_BBTREE_DEBUG_DRAW
854 #include "OpenGL/gl.h"
855 #include "OpenGL/glu.h"
856 #include <GLUT/glut.h>
857
858 static void
859 NodeRender(Node *node, int depth)
860 {
861 if(!NodeIsLeaf(node) && depth <= 10){
862 NodeRender(node->a, depth + 1);
863 NodeRender(node->b, depth + 1);
864 }
865
866 cpBB bb = node->bb;
867
868 // GLfloat v = depth/2.0f;
869 // glColor3f(1.0f - v, v, 0.0f);
870 glLineWidth(cpfmax(5.0f - depth, 1.0f));
871 glBegin(GL_LINES); {
872 glVertex2f(bb.l, bb.b);
873 glVertex2f(bb.l, bb.t);
874
875 glVertex2f(bb.l, bb.t);
876 glVertex2f(bb.r, bb.t);
877
878 glVertex2f(bb.r, bb.t);
879 glVertex2f(bb.r, bb.b);
880
881 glVertex2f(bb.r, bb.b);
882 glVertex2f(bb.l, bb.b);
883 }; glEnd();
884 }
885
886 void
887 cpBBTreeRenderDebug(cpSpatialIndex *index){
888 if(index->klass != &klass){
889 cpAssertWarn(cpFalse, "Ignoring cpBBTreeRenderDebug() call to non-tree spatial index.");
890 return;
891 }
892
893 cpBBTree *tree = (cpBBTree *)index;
894 if(tree->root) NodeRender(tree->root, 0);
895 }
896 #endif