| 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 "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/ |
| 23 | |
| 24 | static inline cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# *Klasscxx . . $ Klass(292ec58cde6c1067).(); |
| 25 | |
| 26 | //MARK: Basic Structures |
| 27 | |
| 28 | typedef struct Boundscxx . . $ Bounds# { |
| 29 | cpFloatcxx . . $ cpFloat# mincxx . . $ Bounds#min., maxcxx . . $ Bounds#max.; |
| 30 | } Boundscxx . . $ Bounds#; |
| 31 | |
| 32 | typedef struct TableCellcxx . . $ TableCell# { |
| 33 | void *objcxx . . $ TableCell#obj.; |
| 34 | Boundscxx . . $ Bounds# boundscxx . . $ TableCell#bounds.; |
| 35 | } TableCellcxx . . $ TableCell#; |
| 36 | |
| 37 | struct cpSweep1Dcxx . . $ cpSweep1D# |
| 38 | { |
| 39 | cpSpatialIndexcxx . . $ cpSpatialIndex# spatialIndexcxx . . $ cpSweep1D#spatialIndex.; |
| 40 | |
| 41 | int numcxx . . $ cpSweep1D#num.; |
| 42 | int maxcxx . . $ cpSweep1D#max.; |
| 43 | TableCellcxx . . $ TableCell# *tablecxx . . $ cpSweep1D#table.; |
| 44 | }; |
| 45 | |
| 46 | static inline cpBoolcxx . . $ cpBool# |
| 47 | BoundsOverlapcxx . . $ BoundsOverlap(7e8443c6e57c71a0).(Boundscxx . . $ Bounds# a, Boundscxx . . $ Bounds# b) |
| 48 | { |
| 49 | return (a.mincxx . . $ Bounds#min. <= b.maxcxx . . $ Bounds#max. && b.mincxx . . $ Bounds#min. <= a.maxcxx . . $ Bounds#max.); |
| 50 | } |
| 51 | |
| 52 | static inline Boundscxx . . $ Bounds# |
| 53 | BBToBoundscxx . . $ BBToBounds(d7d0464bc71e31a3).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, cpBBcxx . . $ cpBB# bb) |
| 54 | { |
| 55 | Boundscxx . . $ Bounds# bounds = {bb.lcxx . . $ cpBB#l., bb.rcxx . . $ cpBB#r.}; |
| 56 | return bounds; |
| 57 | } |
| 58 | |
| 59 | static inline TableCellcxx . . $ TableCell# |
| 60 | MakeTableCellcxx . . $ MakeTableCell(fde683c04a267e9f).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj) |
| 61 | { |
| 62 | TableCellcxx . . $ TableCell# cell = {obj, BBToBoundscxx . . $ BBToBounds(d7d0464bc71e31a3).(sweep, sweep->spatialIndexcxx . . $ cpSweep1D#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(obj))}; |
| 63 | return cell; |
| 64 | } |
| 65 | |
| 66 | //MARK: Memory Management Functions |
| 67 | |
| 68 | cpSweep1Dcxx . . $ cpSweep1D# * |
| 69 | cpSweep1DAlloccxx . . $ cpSweep1DAlloc(1dc616b58e91900b).(void) |
| 70 | { |
| 71 | return (cpSweep1Dcxx . . $ cpSweep1D# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpSweep1Dcxx . . $ cpSweep1D#)); |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | ResizeTablecxx . . $ ResizeTable(a2abb8dfa4bbfb7).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, int size) |
| 76 | { |
| 77 | sweep->maxcxx . . $ cpSweep1D#max. = size; |
| 78 | sweep->tablecxx . . $ cpSweep1D#table. = (TableCellcxx . . $ TableCell# *)cprealloccxx . . $ `include/chipmunk/chipmunk.h:77:10`!(sweep->tablecxx . . $ cpSweep1D#table., size*sizeof(TableCellcxx . . $ TableCell#)); |
| 79 | } |
| 80 | |
| 81 | cpSpatialIndexcxx . . $ cpSpatialIndex# * |
| 82 | cpSweep1DInitcxx . . $ cpSweep1DInit(bc36df65b33568aa).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, cpSpatialIndexBBFunccxx . . $ cpSpatialIndexBBFunc# bbfunc, cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex) |
| 83 | { |
| 84 | cpSpatialIndexInitcxx . . $ cpSpatialIndexInit(57a924cefa208dd7).((cpSpatialIndexcxx . . $ cpSpatialIndex# *)sweep, Klasscxx . . $ Klass(292ec58cde6c1067).(), bbfunc, staticIndex); |
| 85 | |
| 86 | sweep->numcxx . . $ cpSweep1D#num. = 0; |
| 87 | ResizeTablecxx . . $ ResizeTable(a2abb8dfa4bbfb7).(sweep, 32); |
| 88 | |
| 89 | return (cpSpatialIndexcxx . . $ cpSpatialIndex# *)sweep; |
| 90 | } |
| 91 | |
| 92 | cpSpatialIndexcxx . . $ cpSpatialIndex# * |
| 93 | cpSweep1DNewcxx . . $ cpSweep1DNew(ccce0ca6945a4614).(cpSpatialIndexBBFunccxx . . $ cpSpatialIndexBBFunc# bbfunc, cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex) |
| 94 | { |
| 95 | return cpSweep1DInitcxx . . $ cpSweep1DInit(bc36df65b33568aa).(cpSweep1DAlloccxx . . $ cpSweep1DAlloc(1dc616b58e91900b).(), bbfunc, staticIndex); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | cpSweep1DDestroycxx . . $ cpSweep1DDestroy(59c59f9a6871e987).(cpSweep1Dcxx . . $ cpSweep1D# *sweep) |
| 100 | { |
| 101 | cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(sweep->tablecxx . . $ cpSweep1D#table.); |
| 102 | sweep->tablecxx . . $ cpSweep1D#table. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 103 | } |
| 104 | |
| 105 | //MARK: Misc |
| 106 | |
| 107 | static int |
| 108 | cpSweep1DCountcxx . . $ cpSweep1DCount(502b71db7b45eeec).(cpSweep1Dcxx . . $ cpSweep1D# *sweep) |
| 109 | { |
| 110 | return sweep->numcxx . . $ cpSweep1D#num.; |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | cpSweep1DEachcxx . . $ cpSweep1DEach(76ef9c24e2d40446).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, cpSpatialIndexIteratorFunccxx . . $ cpSpatialIndexIteratorFunc# func, void *data) |
| 115 | { |
| 116 | TableCellcxx . . $ TableCell# *table = sweep->tablecxx . . $ cpSweep1D#table.; |
| 117 | for(int i=0, count=sweep->numcxx . . $ cpSweep1D#num.; i<count; i++) func(table[i].objcxx . . $ TableCell#obj., data); |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | cpSweep1DContainscxx . . $ cpSweep1DContains(62817dfe59e93a4e).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj, cpHashValuecxx . . $ cpHashValue# hashid) |
| 122 | { |
| 123 | TableCellcxx . . $ TableCell# *table = sweep->tablecxx . . $ cpSweep1D#table.; |
| 124 | for(int i=0, count=sweep->numcxx . . $ cpSweep1D#num.; i<count; i++){ |
| 125 | if(table[i].objcxx . . $ TableCell#obj. == obj) return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!; |
| 126 | } |
| 127 | |
| 128 | return cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!; |
| 129 | } |
| 130 | |
| 131 | //MARK: Basic Operations |
| 132 | |
| 133 | static void |
| 134 | cpSweep1DInsertcxx . . $ cpSweep1DInsert(51d5821ac12aa346).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj, cpHashValuecxx . . $ cpHashValue# hashid) |
| 135 | { |
| 136 | if(sweep->numcxx . . $ cpSweep1D#num. == sweep->maxcxx . . $ cpSweep1D#max.) ResizeTablecxx . . $ ResizeTable(a2abb8dfa4bbfb7).(sweep, sweep->maxcxx . . $ cpSweep1D#max.*2); |
| 137 | |
| 138 | sweep->tablecxx . . $ cpSweep1D#table.[sweep->numcxx . . $ cpSweep1D#num.] = MakeTableCellcxx . . $ MakeTableCell(fde683c04a267e9f).(sweep, obj); |
| 139 | sweep->numcxx . . $ cpSweep1D#num.++; |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | cpSweep1DRemovecxx . . $ cpSweep1DRemove(51d5821ac12aa346).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj, cpHashValuecxx . . $ cpHashValue# hashid) |
| 144 | { |
| 145 | TableCellcxx . . $ TableCell# *table = sweep->tablecxx . . $ cpSweep1D#table.; |
| 146 | for(int i=0, count=sweep->numcxx . . $ cpSweep1D#num.; i<count; i++){ |
| 147 | if(table[i].objcxx . . $ TableCell#obj. == obj){ |
| 148 | int num = --sweep->numcxx . . $ cpSweep1D#num.; |
| 149 | |
| 150 | table[i] = table[num]; |
| 151 | table[num].objcxx . . $ TableCell#obj. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 152 | |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | //MARK: Reindexing Functions |
| 159 | |
| 160 | static void |
| 161 | cpSweep1DReindexObjectcxx . . $ cpSweep1DReindexObject(51d5821ac12aa346).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj, cpHashValuecxx . . $ cpHashValue# hashid) |
| 162 | { |
| 163 | // Nothing to do here |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | cpSweep1DReindexcxx . . $ cpSweep1DReindex(59c59f9a6871e987).(cpSweep1Dcxx . . $ cpSweep1D# *sweep) |
| 168 | { |
| 169 | // Nothing to do here |
| 170 | // Could perform a sort, but queries are not accelerated anyway. |
| 171 | } |
| 172 | |
| 173 | //MARK: Query Functions |
| 174 | |
| 175 | static void |
| 176 | cpSweep1DQuerycxx . . $ cpSweep1DQuery(d85990191b060619).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj, cpBBcxx . . $ cpBB# bb, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data) |
| 177 | { |
| 178 | // Implementing binary search here would allow you to find an upper limit |
| 179 | // but not a lower limit. Probably not worth the hassle. |
| 180 | |
| 181 | Boundscxx . . $ Bounds# bounds = BBToBoundscxx . . $ BBToBounds(d7d0464bc71e31a3).(sweep, bb); |
| 182 | |
| 183 | TableCellcxx . . $ TableCell# *table = sweep->tablecxx . . $ cpSweep1D#table.; |
| 184 | for(int i=0, count=sweep->numcxx . . $ cpSweep1D#num.; i<count; i++){ |
| 185 | TableCellcxx . . $ TableCell# cell = table[i]; |
| 186 | if(BoundsOverlapcxx . . $ BoundsOverlap(7e8443c6e57c71a0).(bounds, cell.boundscxx . . $ TableCell#bounds.) && obj != cell.objcxx . . $ TableCell#obj.) func(obj, cell.objcxx . . $ TableCell#obj., 0, data); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | cpSweep1DSegmentQuerycxx . . $ cpSweep1DSegmentQuery(ed9d650309f3d48d).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, void *obj, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# t_exit, cpSpatialIndexSegmentQueryFunccxx . . $ cpSpatialIndexSegmentQueryFunc# func, void *data) |
| 192 | { |
| 193 | cpBBcxx . . $ cpBB# bb = cpBBExpandcxx . . $ cpBBExpand(724ddf18c4afacd8).(cpBBNewcxx . . $ cpBBNew(f05aa3203068e6d3).(a.xcxx . . $ cpVect#x., a.ycxx . . $ cpVect#y., a.xcxx . . $ cpVect#x., a.ycxx . . $ cpVect#y.), b); |
| 194 | Boundscxx . . $ Bounds# bounds = BBToBoundscxx . . $ BBToBounds(d7d0464bc71e31a3).(sweep, bb); |
| 195 | |
| 196 | TableCellcxx . . $ TableCell# *table = sweep->tablecxx . . $ cpSweep1D#table.; |
| 197 | for(int i=0, count=sweep->numcxx . . $ cpSweep1D#num.; i<count; i++){ |
| 198 | TableCellcxx . . $ TableCell# cell = table[i]; |
| 199 | if(BoundsOverlapcxx . . $ BoundsOverlap(7e8443c6e57c71a0).(bounds, cell.boundscxx . . $ TableCell#bounds.)) func(obj, cell.objcxx . . $ TableCell#obj., data); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | //MARK: Reindex/Query |
| 204 | |
| 205 | static int |
| 206 | TableSortcxx . . $ TableSort(f8d162e2778818f5).(TableCellcxx . . $ TableCell# *a, TableCellcxx . . $ TableCell# *b) |
| 207 | { |
| 208 | return (a->boundscxx . . $ TableCell#bounds..mincxx . . $ Bounds#min. < b->boundscxx . . $ TableCell#bounds..mincxx . . $ Bounds#min. ? -1 : (a->boundscxx . . $ TableCell#bounds..mincxx . . $ Bounds#min. > b->boundscxx . . $ TableCell#bounds..mincxx . . $ Bounds#min. ? 1 : 0)); |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | cpSweep1DReindexQuerycxx . . $ cpSweep1DReindexQuery(ba65701e3f15ee1f).(cpSweep1Dcxx . . $ cpSweep1D# *sweep, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data) |
| 213 | { |
| 214 | TableCellcxx . . $ TableCell# *table = sweep->tablecxx . . $ cpSweep1D#table.; |
| 215 | int count = sweep->numcxx . . $ cpSweep1D#num.; |
| 216 | |
| 217 | // Update bounds and sort |
| 218 | for(int i=0; i<count; i++) table[i] = MakeTableCellcxx . . $ MakeTableCell(fde683c04a267e9f).(sweep, table[i].objcxx . . $ TableCell#obj.); |
| 219 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(table, count, sizeof(TableCellcxx . . $ TableCell#), (int (*)(const void *, const void *))TableSortcxx . . $ TableSort(f8d162e2778818f5).); // TODO: use insertion sort instead |
| 220 | |
| 221 | for(int i=0; i<count; i++){ |
| 222 | TableCellcxx . . $ TableCell# cell = table[i]; |
| 223 | cpFloatcxx . . $ cpFloat# max = cell.boundscxx . . $ TableCell#bounds..maxcxx . . $ Bounds#max.; |
| 224 | |
| 225 | for(int j=i+1; table[j].boundscxx . . $ TableCell#bounds..mincxx . . $ Bounds#min. < max && j<count; j++){ |
| 226 | func(cell.objcxx . . $ TableCell#obj., table[j].objcxx . . $ TableCell#obj., 0, data); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Reindex query is also responsible for colliding against the static index. |
| 231 | // Fortunately there is a helper function for that. |
| 232 | cpSpatialIndexCollideStaticcxx . . $ cpSpatialIndexCollideStatic(3c6f473dc08bdfa1).((cpSpatialIndexcxx . . $ cpSpatialIndex# *)sweep, sweep->spatialIndexcxx . . $ cpSweep1D#spatialIndex..staticIndexcxx . . $ cpSpatialIndex#staticIndex., func, data); |
| 233 | } |
| 234 | |
| 235 | static cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# klasscxx . . $ klass. = { |
| 236 | (cpSpatialIndexDestroyImplcxx . . $ cpSpatialIndexDestroyImpl#)cpSweep1DDestroycxx . . $ cpSweep1DDestroy(59c59f9a6871e987)., |
| 237 | |
| 238 | (cpSpatialIndexCountImplcxx . . $ cpSpatialIndexCountImpl#)cpSweep1DCountcxx . . $ cpSweep1DCount(502b71db7b45eeec)., |
| 239 | (cpSpatialIndexEachImplcxx . . $ cpSpatialIndexEachImpl#)cpSweep1DEachcxx . . $ cpSweep1DEach(76ef9c24e2d40446)., |
| 240 | (cpSpatialIndexContainsImplcxx . . $ cpSpatialIndexContainsImpl#)cpSweep1DContainscxx . . $ cpSweep1DContains(62817dfe59e93a4e)., |
| 241 | |
| 242 | (cpSpatialIndexInsertImplcxx . . $ cpSpatialIndexInsertImpl#)cpSweep1DInsertcxx . . $ cpSweep1DInsert(51d5821ac12aa346)., |
| 243 | (cpSpatialIndexRemoveImplcxx . . $ cpSpatialIndexRemoveImpl#)cpSweep1DRemovecxx . . $ cpSweep1DRemove(51d5821ac12aa346)., |
| 244 | |
| 245 | (cpSpatialIndexReindexImplcxx . . $ cpSpatialIndexReindexImpl#)cpSweep1DReindexcxx . . $ cpSweep1DReindex(59c59f9a6871e987)., |
| 246 | (cpSpatialIndexReindexObjectImplcxx . . $ cpSpatialIndexReindexObjectImpl#)cpSweep1DReindexObjectcxx . . $ cpSweep1DReindexObject(51d5821ac12aa346)., |
| 247 | (cpSpatialIndexReindexQueryImplcxx . . $ cpSpatialIndexReindexQueryImpl#)cpSweep1DReindexQuerycxx . . $ cpSweep1DReindexQuery(ba65701e3f15ee1f)., |
| 248 | |
| 249 | (cpSpatialIndexQueryImplcxx . . $ cpSpatialIndexQueryImpl#)cpSweep1DQuerycxx . . $ cpSweep1DQuery(d85990191b060619)., |
| 250 | (cpSpatialIndexSegmentQueryImplcxx . . $ cpSpatialIndexSegmentQueryImpl#)cpSweep1DSegmentQuerycxx . . $ cpSweep1DSegmentQuery(ed9d650309f3d48d)., |
| 251 | }; |
| 252 | |
| 253 | static inline cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# *Klasscxx . . $ Klass(292ec58cde6c1067).(){return &klasscxx . . $ klass.;} |
| 254 |