1 module chipmunk.cpSpatialIndex; 2 import core.stdc.config; 3 4 import chipmunk.chipmunk_types; 5 import chipmunk.chipmunk_structs; 6 import chipmunk.cpBB; 7 8 extern (C): 9 10 alias cpBB function (void*) cpSpatialIndexBBFunc; 11 alias void function (void*, void*) cpSpatialIndexIteratorFunc; 12 alias uint function (void*, void*, uint, void*) cpSpatialIndexQueryFunc; 13 alias double function (void*, void*, void*) cpSpatialIndexSegmentQueryFunc; 14 alias cpVect function (void*) cpBBTreeVelocityFunc; 15 alias void function (cpSpatialIndex*) cpSpatialIndexDestroyImpl; 16 alias int function (cpSpatialIndex*) cpSpatialIndexCountImpl; 17 alias void function (cpSpatialIndex*, void function (void*, void*), void*) cpSpatialIndexEachImpl; 18 alias ubyte function (cpSpatialIndex*, void*, c_ulong) cpSpatialIndexContainsImpl; 19 alias void function (cpSpatialIndex*, void*, c_ulong) cpSpatialIndexInsertImpl; 20 alias void function (cpSpatialIndex*, void*, c_ulong) cpSpatialIndexRemoveImpl; 21 alias void function (cpSpatialIndex*) cpSpatialIndexReindexImpl; 22 alias void function (cpSpatialIndex*, void*, c_ulong) cpSpatialIndexReindexObjectImpl; 23 alias void function (cpSpatialIndex*, uint function (void*, void*, uint, void*), void*) cpSpatialIndexReindexQueryImpl; 24 alias void function (cpSpatialIndex*, void*, cpBB, uint function (void*, void*, uint, void*), void*) cpSpatialIndexQueryImpl; 25 alias void function (cpSpatialIndex*, void*, cpVect, cpVect, double, double function (void*, void*, void*), void*) cpSpatialIndexSegmentQueryImpl; 26 27 struct cpSpatialIndex 28 { 29 cpSpatialIndexClass* klass; 30 cpSpatialIndexBBFunc bbfunc; 31 cpSpatialIndex* staticIndex; 32 cpSpatialIndex* dynamicIndex; 33 } 34 35 struct cpSpatialIndexClass 36 { 37 cpSpatialIndexDestroyImpl destroy; 38 cpSpatialIndexCountImpl count; 39 cpSpatialIndexEachImpl each; 40 cpSpatialIndexContainsImpl contains; 41 cpSpatialIndexInsertImpl insert; 42 cpSpatialIndexRemoveImpl remove; 43 cpSpatialIndexReindexImpl reindex; 44 cpSpatialIndexReindexObjectImpl reindexObject; 45 cpSpatialIndexReindexQueryImpl reindexQuery; 46 cpSpatialIndexQueryImpl query; 47 cpSpatialIndexSegmentQueryImpl segmentQuery; 48 } 49 50 struct cpBBTree; 51 52 53 struct cpSweep1D; 54 55 56 struct cpSpaceHash; 57 58 59 cpSpaceHash* cpSpaceHashAlloc (); 60 cpSpatialIndex* cpSpaceHashInit (cpSpaceHash* hash, cpFloat celldim, int numcells, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex* staticIndex); 61 cpSpatialIndex* cpSpaceHashNew (cpFloat celldim, int cells, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex* staticIndex); 62 void cpSpaceHashResize (cpSpaceHash* hash, cpFloat celldim, int numcells); 63 cpBBTree* cpBBTreeAlloc (); 64 cpSpatialIndex* cpBBTreeInit (cpBBTree* tree, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex* staticIndex); 65 cpSpatialIndex* cpBBTreeNew (cpSpatialIndexBBFunc bbfunc, cpSpatialIndex* staticIndex); 66 void cpBBTreeOptimize (cpSpatialIndex* index); 67 void cpBBTreeSetVelocityFunc (cpSpatialIndex* index, cpBBTreeVelocityFunc func); 68 cpSweep1D* cpSweep1DAlloc (); 69 cpSpatialIndex* cpSweep1DInit (cpSweep1D* sweep, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex* staticIndex); 70 cpSpatialIndex* cpSweep1DNew (cpSpatialIndexBBFunc bbfunc, cpSpatialIndex* staticIndex); 71 void cpSpatialIndexFree (cpSpatialIndex* index); 72 void cpSpatialIndexCollideStatic (cpSpatialIndex* dynamicIndex, cpSpatialIndex* staticIndex, cpSpatialIndexQueryFunc func, void* data); 73 74 // inlined in chipmunk headers: 75 76 static void cpSpatialIndexDestroy(cpSpatialIndex *index) 77 { 78 if(index.klass) index.klass.destroy(index); 79 } 80 81 /// Get the number of objects in the spatial index. 82 static int cpSpatialIndexCount(cpSpatialIndex *index) 83 { 84 return index.klass.count(index); 85 } 86 87 /// Iterate the objects in the spatial index. @c func will be called once for each object. 88 static void cpSpatialIndexEach(cpSpatialIndex *index, cpSpatialIndexIteratorFunc func, void *data) 89 { 90 index.klass.each(index, func, data); 91 } 92 93 /// Returns true if the spatial index contains the given object. 94 /// Most spatial indexes use hashed storage, so you must provide a hash value too. 95 static cpBool cpSpatialIndexContains(cpSpatialIndex *index, void *obj, cpHashValue hashid) 96 { 97 return index.klass.contains(index, obj, hashid); 98 } 99 100 /// Add an object to a spatial index. 101 /// Most spatial indexes use hashed storage, so you must provide a hash value too. 102 static void cpSpatialIndexInsert(cpSpatialIndex *index, void *obj, cpHashValue hashid) 103 { 104 index.klass.insert(index, obj, hashid); 105 } 106 107 /// Remove an object from a spatial index. 108 /// Most spatial indexes use hashed storage, so you must provide a hash value too. 109 static void cpSpatialIndexRemove(cpSpatialIndex *index, void *obj, cpHashValue hashid) 110 { 111 index.klass.remove(index, obj, hashid); 112 } 113 114 /// Perform a full reindex of a spatial index. 115 static void cpSpatialIndexReindex(cpSpatialIndex *index) 116 { 117 index.klass.reindex(index); 118 } 119 120 /// Reindex a single object in the spatial index. 121 static void cpSpatialIndexReindexObject(cpSpatialIndex *index, void *obj, cpHashValue hashid) 122 { 123 index.klass.reindexObject(index, obj, hashid); 124 } 125 126 /// Perform a rectangle query against the spatial index, calling @c func for each potential match. 127 static void cpSpatialIndexQuery(cpSpatialIndex *index, void *obj, cpBB bb, cpSpatialIndexQueryFunc func, void *data) 128 { 129 index.klass.query(index, obj, bb, func, data); 130 } 131 132 /// Perform a segment query against the spatial index, calling @c func for each potential match. 133 static void cpSpatialIndexSegmentQuery(cpSpatialIndex *index, void *obj, cpVect a, cpVect b, cpFloat t_exit, cpSpatialIndexSegmentQueryFunc func, void *data) 134 { 135 index.klass.segmentQuery(index, obj, a, b, t_exit, func, data); 136 } 137 138 /// Simultaneously reindex and find all colliding objects. 139 /// @c func will be called once for each potentially overlapping pair of objects found. 140 /// If the spatial index was initialized with a static index, it will collide it's objects against that as well. 141 static void cpSpatialIndexReindexQuery(cpSpatialIndex *index, cpSpatialIndexQueryFunc func, void *data) 142 { 143 index.klass.reindexQuery(index, func, data); 144 }