par_sprune.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPRUNE :: https://github.com/prideout/par
  2. // Sweep and Prune library for detecting axis-aligned box collisions in 2D.
  3. //
  4. // For an emscripten demo of this library, take a look at the following link.
  5. //
  6. // http://github.prideout.net/d3cpp/
  7. //
  8. // The axis-aligned bounding boxes are specified by (minx, miny, maxx, maxy).
  9. // Simple usage example:
  10. //
  11. // float boxes[] = {
  12. // 0.10, 0.10, 0.30, 0.30, // box 0
  13. // 0.20, 0.20, 0.40, 0.40, // box 1
  14. // 0.60, 0.15, 0.70, 0.25, // box 2
  15. // };
  16. // int nboxes = 3;
  17. // par_sprune_context* ctx = par_sprune_overlap(boxes, nboxes, 0);
  18. // int const* pairs = ctx->collision_pairs;
  19. // for (int i = 0; i < ctx->ncollision_pairs * 2; i += 2) {
  20. // printf("box %d overlaps box %d\n", pairs[i], pairs[i + 1]);
  21. // }
  22. // par_sprune_free_context(ctx);
  23. //
  24. //
  25. // The MIT License
  26. // Copyright (c) 2015 Philip Rideout
  27. #ifndef PAR_SPRUNE_H
  28. #define PAR_SPRUNE_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. #ifndef PAR_SPRUNE_INT
  35. #define PAR_SPRUNE_INT int32_t
  36. #endif
  37. #ifndef PAR_SPRUNE_FLT
  38. #define PAR_SPRUNE_FLT float
  39. #endif
  40. // -----------------------------------------------------------------------------
  41. // BEGIN PUBLIC API
  42. // -----------------------------------------------------------------------------
  43. typedef struct {
  44. PAR_SPRUNE_INT const* const collision_pairs; // list of two-tuples
  45. PAR_SPRUNE_INT const ncollision_pairs; // number of two-tuples
  46. PAR_SPRUNE_INT const* const culled; // filled by par_sprune_cull
  47. PAR_SPRUNE_INT const nculled; // set by par_sprune_cull
  48. } par_sprune_context;
  49. void par_sprune_free_context(par_sprune_context* context);
  50. // Takes an array of 4-tuples (minx miny maxx maxy) and performs SaP. Populates
  51. // "collision_pairs" and "ncollision_pairs". Optionally takes an existing
  52. // context to avoid memory churn; pass NULL for initial construction.
  53. par_sprune_context* par_sprune_overlap(PAR_SPRUNE_FLT const* aabbs,
  54. PAR_SPRUNE_INT naabbs, par_sprune_context* previous);
  55. // Reads new aabb data from the same pointer that was passed to the overlap
  56. // function and refreshes the two relevant fields. This function should
  57. // only be used when the number of aabbs remains constant. If this returns
  58. // false, no changes to the collision set were detected.
  59. bool par_sprune_update(par_sprune_context* ctx);
  60. // Examines all collision groups and creates a culling set such that no boxes
  61. // would overlap if the culled boxes are removed. When two boxes collide, the
  62. // box that occurs earlier in the list is more likely to be culled. Populates
  63. // the "culled" and "nculled" fields in par_sprune_context. This is useful for
  64. // hiding labels in GIS applications.
  65. void par_sprune_cull(par_sprune_context* context);
  66. // -----------------------------------------------------------------------------
  67. // END PUBLIC API
  68. // -----------------------------------------------------------------------------
  69. #ifdef __cplusplus
  70. }
  71. #endif
  72. #ifdef PAR_SPRUNE_IMPLEMENTATION
  73. #define PARINT PAR_SPRUNE_INT
  74. #define PARFLT PAR_SPRUNE_FLT
  75. #include <stdlib.h>
  76. #include <assert.h>
  77. #ifndef PAR_PI
  78. #define PAR_PI (3.14159265359)
  79. #define PAR_MIN(a, b) (a > b ? b : a)
  80. #define PAR_MAX(a, b) (a > b ? a : b)
  81. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  82. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  83. #define PAR_SQR(a) ((a) * (a))
  84. #endif
  85. #ifndef PAR_MALLOC
  86. #define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
  87. #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
  88. #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
  89. #define PAR_FREE(BUF) free(BUF)
  90. #endif
  91. #ifndef pa_free
  92. #define pa_free(a) ((a) ? PAR_FREE(pa___raw(a)), 0 : 0)
  93. #define pa_push(a, v) (pa___maybegrow(a, 1), (a)[pa___n(a)++] = (v))
  94. #define pa_pop(a) (pa___n(a)--)
  95. #define pa_count(a) ((a) ? pa___n(a) : 0)
  96. #define pa_add(a, n) (pa___maybegrow(a, n), pa___n(a) += (n))
  97. #define pa_last(a) ((a)[pa___n(a) - 1])
  98. #define pa_end(a) (a + pa_count(a))
  99. #define pa_clear(arr) if (arr) pa___n(arr) = 0
  100. #define pa___raw(a) ((int*) (a) -2)
  101. #define pa___m(a) pa___raw(a)[0]
  102. #define pa___n(a) pa___raw(a)[1]
  103. #define pa___needgrow(a, n) ((a) == 0 || pa___n(a) + (n) >= pa___m(a))
  104. #define pa___maybegrow(a, n) (pa___needgrow(a, (n)) ? pa___grow(a, n) : 0)
  105. #define pa___grow(a, n) (*((void**)& (a)) = pa___growf((void*) (a), (n), \
  106. sizeof(*(a))))
  107. static void* pa___growf(void* arr, int increment, int itemsize)
  108. {
  109. int dbl_cur = arr ? 2 * pa___m(arr) : 0;
  110. int min_needed = pa_count(arr) + increment;
  111. int m = dbl_cur > min_needed ? dbl_cur : min_needed;
  112. int* p = PAR_REALLOC(int, arr ? pa___raw(arr) : 0,
  113. itemsize * m / sizeof(int) + 2);
  114. if (p) {
  115. if (!arr) {
  116. p[1] = 0;
  117. }
  118. p[0] = m;
  119. return p + 2;
  120. }
  121. return (void*) (2 * sizeof(int));
  122. }
  123. #endif
  124. typedef struct {
  125. // Public:
  126. PARINT* collision_pairs;
  127. PARINT ncollision_pairs;
  128. PARINT* culled;
  129. PARINT nculled;
  130. // Private:
  131. PARFLT const* aabbs;
  132. PARINT naabbs;
  133. PARINT* sorted_indices[2];
  134. PARINT* pairs[2];
  135. } par_sprune__context;
  136. static inline int par_qsort_cmpswap(char *__restrict a, char *__restrict b,
  137. size_t w,
  138. int (*compar)(const void *_a, const void *_b,
  139. void *_arg),
  140. void *arg)
  141. {
  142. char tmp, *end = a+w;
  143. if (compar(a, b, arg) > 0) {
  144. for(; a < end; a++, b++) { tmp = *a; *a = *b; *b = tmp; }
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. // qsort doesn't take a context, so we have our own portable implementation.
  150. // Parameters:
  151. // base is the array to be sorted
  152. // nel is the number of elements in the array
  153. // w is the size in bytes of each element of the array
  154. // compar is the comparison function
  155. // arg is a pointer to be passed to the comparison function
  156. //
  157. static inline void par_qsort(
  158. void *base,
  159. size_t nel,
  160. size_t w,
  161. int (*compar)(const void *_a, const void *_b, void *_arg),
  162. void *arg)
  163. {
  164. char *b = (char*) base, *end = (char*) (b + nel * w);
  165. if (nel < 7) {
  166. char *pi, *pj;
  167. for (pi = b+w; pi < end; pi += w) {
  168. for (pj = pi; pj > b && par_qsort_cmpswap(pj-w, pj, w, compar, arg);
  169. pj -= w) {}
  170. }
  171. return;
  172. }
  173. char *x, *y, *xend, ch;
  174. char *pl, *pr;
  175. char *last = b+w*(nel-1), *tmp;
  176. char *l[3];
  177. l[0] = b;
  178. l[1] = b+w*(nel/2);
  179. l[2] = last;
  180. if (compar(l[0],l[1],arg) > 0) {
  181. tmp=l[0]; l[0]=l[1]; l[1]=tmp;
  182. }
  183. if (compar(l[1],l[2],arg) > 0) {
  184. tmp=l[1]; l[1]=l[2]; l[2]=tmp;
  185. if (compar(l[0],l[1],arg) > 0) {
  186. tmp=l[0]; l[0]=l[1]; l[1]=tmp;
  187. }
  188. }
  189. for(x = l[1], y = last, xend = x+w; x<xend; x++, y++) {
  190. ch = *x; *x = *y; *y = ch;
  191. }
  192. pl = b;
  193. pr = last;
  194. while (pl < pr) {
  195. for (; pl < pr; pl += w) {
  196. if (par_qsort_cmpswap(pl, pr, w, compar, arg)) {
  197. pr -= w;
  198. break;
  199. }
  200. }
  201. for (; pl < pr; pr -= w) {
  202. if (par_qsort_cmpswap(pl, pr, w, compar, arg)) {
  203. pl += w;
  204. break;
  205. }
  206. }
  207. }
  208. par_qsort(b, (pl-b) / w, w, compar, arg);
  209. par_qsort(pl+w, (end - (pl+w)) / w, w, compar, arg);
  210. }
  211. void par_sprune_free_context(par_sprune_context* context)
  212. {
  213. par_sprune__context* ctx = (par_sprune__context*) context;
  214. pa_free(ctx->sorted_indices[0]);
  215. pa_free(ctx->sorted_indices[1]);
  216. pa_free(ctx->pairs[0]);
  217. pa_free(ctx->pairs[1]);
  218. pa_free(ctx->collision_pairs);
  219. PAR_FREE(ctx);
  220. }
  221. static void par_sprune__remove(PARINT* arr, PARINT val)
  222. {
  223. int i = pa_count(arr) - 1;
  224. for (; i >= 0; i--) {
  225. if (arr[i] == val) {
  226. break;
  227. }
  228. }
  229. assert(i >= 0);
  230. for (++i; i < pa_count(arr); i++) {
  231. PAR_SWAP(PARINT, arr[i - 1], arr[i]);
  232. }
  233. pa___n(arr)--;
  234. }
  235. typedef struct {
  236. PARFLT const* aabbs;
  237. } par__sprune_sorter;
  238. static int par__cmpinds(const void* pa, const void* pb, void* psorter)
  239. {
  240. PARINT a = *((const PARINT*) pa);
  241. PARINT b = *((const PARINT*) pb);
  242. par__sprune_sorter* sorter = (par__sprune_sorter*) psorter;
  243. PARFLT const* aabbs = sorter->aabbs;
  244. PARFLT vala = aabbs[a];
  245. PARFLT valb = aabbs[b];
  246. if (vala > valb) return 1;
  247. if (vala < valb) return -1;
  248. if (a > b) return 1;
  249. if (a < b) return -1;
  250. return 0;
  251. }
  252. static int par__cmppairs(const void* pa, const void* pb, void* unused)
  253. {
  254. PARINT a = *((const PARINT*) pa);
  255. PARINT b = *((const PARINT*) pb);
  256. if (a > b) return 1;
  257. if (a < b) return -1;
  258. a = *(1 + (const PARINT*) pa);
  259. b = *(1 + (const PARINT*) pb);
  260. if (a > b) return 1;
  261. if (a < b) return -1;
  262. return 0;
  263. }
  264. static int par__cmpfind(const void* pa, const void* pb)
  265. {
  266. PARINT a = *((const PARINT*) pa);
  267. PARINT b = *((const PARINT*) pb);
  268. if (a > b) return 1;
  269. if (a < b) return -1;
  270. a = *(1 + (const PARINT*) pa);
  271. b = *(1 + (const PARINT*) pb);
  272. if (a > b) return 1;
  273. if (a < b) return -1;
  274. return 0;
  275. }
  276. par_sprune_context* par_sprune_overlap(PARFLT const* aabbs, PARINT naabbs,
  277. par_sprune_context* previous)
  278. {
  279. par_sprune__context* ctx = (par_sprune__context*) previous;
  280. if (!ctx) {
  281. ctx = PAR_CALLOC(par_sprune__context, 1);
  282. }
  283. ctx->aabbs = aabbs;
  284. ctx->naabbs = naabbs;
  285. for (int axis = 0; axis < 2; axis++) {
  286. pa_clear(ctx->sorted_indices[axis]);
  287. pa_add(ctx->sorted_indices[axis], naabbs * 2);
  288. pa_clear(ctx->pairs[axis]);
  289. }
  290. for (PARINT i = 0; i < naabbs; i++) {
  291. ctx->sorted_indices[0][i * 2 + 0] = i * 4 + 0;
  292. ctx->sorted_indices[1][i * 2 + 0] = i * 4 + 1;
  293. ctx->sorted_indices[0][i * 2 + 1] = i * 4 + 2;
  294. ctx->sorted_indices[1][i * 2 + 1] = i * 4 + 3;
  295. }
  296. par__sprune_sorter sorter;
  297. sorter.aabbs = ctx->aabbs;
  298. PARINT* active = 0;
  299. // Sweep a plane first across the X-axis, then down through the Y-axis.
  300. for (int axis = 0; axis < 2; axis++) {
  301. PARINT** pairs = &ctx->pairs[axis];
  302. PARINT* indices = ctx->sorted_indices[axis];
  303. par_qsort(indices, naabbs * 2, sizeof(PARINT), par__cmpinds, &sorter);
  304. pa_clear(active);
  305. for (PARINT i = 0; i < naabbs * 2; i++) {
  306. PARINT fltindex = indices[i];
  307. PARINT boxindex = fltindex / 4;
  308. bool ismin = ((fltindex - axis) % 4) == 0;
  309. if (ismin) {
  310. for (int j = 0; j < pa_count(active); j++) {
  311. pa_push(*pairs, active[j]);
  312. pa_push(*pairs, boxindex);
  313. pa_push(*pairs, boxindex);
  314. pa_push(*pairs, active[j]);
  315. }
  316. pa_push(active, boxindex);
  317. } else {
  318. par_sprune__remove(active, boxindex);
  319. }
  320. }
  321. }
  322. // Sort the Y-axis collision pairs to make it easier to intersect it
  323. // with the set of X-axis collision pairs. We also sort the X-axis
  324. // pairs because it's required for subsequent calls to par_sprune_update.
  325. PARINT* xpairs = ctx->pairs[0];
  326. PARINT* ypairs = ctx->pairs[1];
  327. int nxpairs = pa_count(xpairs) / 2;
  328. int nypairs = pa_count(ypairs) / 2;
  329. int pairsize = 2 * sizeof(PARINT);
  330. pa_free(active);
  331. par_qsort(xpairs, nxpairs, pairsize, par__cmppairs, 0);
  332. par_qsort(ypairs, nypairs, pairsize, par__cmppairs, 0);
  333. pa_clear(ctx->collision_pairs);
  334. // Find the intersection of X-axis overlaps and Y-axis overlaps.
  335. for (int i = 0; i < pa_count(xpairs); i += 2) {
  336. PARINT* key = xpairs + i;
  337. if (key[1] < key[0]) {
  338. continue;
  339. }
  340. void* found = bsearch(key, ypairs, nypairs, pairsize, par__cmpfind);
  341. if (found) {
  342. pa_push(ctx->collision_pairs, key[0]);
  343. pa_push(ctx->collision_pairs, key[1]);
  344. }
  345. }
  346. ctx->ncollision_pairs = pa_count(ctx->collision_pairs) / 2;
  347. return (par_sprune_context*) ctx;
  348. }
  349. bool par_sprune_update(par_sprune_context* context)
  350. {
  351. par_sprune__context* ctx = (par_sprune__context*) context;
  352. PARINT* collision_pairs = ctx->collision_pairs;
  353. PARINT ncollision_pairs = ctx->ncollision_pairs;
  354. ctx->collision_pairs = 0;
  355. par_sprune_overlap(ctx->aabbs, ctx->naabbs, context);
  356. bool dirty = ncollision_pairs != ctx->ncollision_pairs;
  357. if (!dirty) {
  358. int pairsize = 2 * sizeof(PARINT);
  359. for (int i = 0; i < ctx->ncollision_pairs; i += 2) {
  360. PARINT* key = ctx->collision_pairs + i;
  361. if (!bsearch(key, collision_pairs, ncollision_pairs,
  362. pairsize, par__cmpfind)) {
  363. dirty = true;
  364. break;
  365. }
  366. }
  367. }
  368. pa_free(collision_pairs);
  369. return dirty;
  370. }
  371. bool par_sprune__is_culled(par_sprune__context* ctx, PARINT key)
  372. {
  373. for (int i = 0; i < pa_count(ctx->culled); i++) {
  374. if (key == ctx->culled[i]) {
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. static int par__cmpfindsingle(const void* pa, const void* pb)
  381. {
  382. PARINT a = *((const PARINT*) pa);
  383. PARINT b = *((const PARINT*) pb);
  384. if (a > b) return 1;
  385. if (a < b) return -1;
  386. return 0;
  387. }
  388. void par_sprune_cull(par_sprune_context* context)
  389. {
  390. par_sprune__context* ctx = (par_sprune__context*) context;
  391. pa_clear(ctx->culled);
  392. PARINT* collision_pairs = ctx->collision_pairs;
  393. PARINT ncollision_pairs = ctx->ncollision_pairs;
  394. int pairsize = 2 * sizeof(PARINT);
  395. for (int i = 0; i < ctx->naabbs; i++) {
  396. PARINT* found = (PARINT*) bsearch(&i, collision_pairs, ncollision_pairs,
  397. pairsize, par__cmpfindsingle);
  398. if (!found) {
  399. continue;
  400. }
  401. if (!par_sprune__is_culled(ctx, found[0]) &&
  402. !par_sprune__is_culled(ctx, found[1])) {
  403. pa_push(ctx->culled, found[0]);
  404. }
  405. }
  406. ctx->nculled = pa_count(ctx->culled);
  407. }
  408. #undef PARINT
  409. #undef PARFLT
  410. #endif // PAR_SPRUNE_IMPLEMENTATION
  411. #endif // PAR_SPRUNE_H