par_sprune.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPRUNE :: https://github.com/prideout/par
  2. // Sweep and Prune library for detecting axis-aligned box collisions in 2D.
  3. //
  4. // In addition to the comment block above each function declaration, the API
  5. // has informal documentation here:
  6. //
  7. // http://github.prideout.net/work-in-progress/
  8. //
  9. // The MIT License
  10. // Copyright (c) 2015 Philip Rideout
  11. #ifndef PAR_SPRUNE_H
  12. #define PAR_SPRUNE_H
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #include <stdint.h>
  17. #ifndef PAR_SPRUNE_INT
  18. #define PAR_SPRUNE_INT int32_t
  19. #endif
  20. #ifndef PAR_SPRUNE_FLT
  21. #define PAR_SPRUNE_FLT float
  22. #endif
  23. // -----------------------------------------------------------------------------
  24. // BEGIN PUBLIC API
  25. // -----------------------------------------------------------------------------
  26. typedef struct {
  27. PAR_SPRUNE_INT const* const collision_pairs; // list of two-tuples
  28. PAR_SPRUNE_INT const ncollision_pairs; // number of two-tuples
  29. PAR_SPRUNE_INT const* const culled; // filled by par_sprune_cull
  30. PAR_SPRUNE_INT const nculled; // set by par_sprune_cull
  31. } par_sprune_context;
  32. void par_sprune_free_context(par_sprune_context* context);
  33. // Takes an array of 4-tuples (minx miny maxx maxy) and performs SaP. Populates
  34. // "collision_pairs" and "ncollision_pairs". Optionally takes an existing
  35. // context to avoid memory churn; pass NULL for initial construction.
  36. par_sprune_context* par_sprune_overlap(PAR_SPRUNE_FLT const* aabbs,
  37. PAR_SPRUNE_INT naabbs, par_sprune_context* previous);
  38. // Reads new aabb data from the same pointer that was passed to the overlap
  39. // function and refreshes the "collision_pairs" field. Exploits temporal
  40. // coherence so it's very efficient for animation.
  41. void par_sprune_update(par_sprune_context* ctx);
  42. // Examines all collision groups and creates a culling set such that no
  43. // boxes would overlap if the culled boxes are removed. This function
  44. // populates the "culled" and "nculled" fields in par_sprune_context.
  45. // This is useful for hiding labels in GIS applications.
  46. void par_sprune_cull(par_sprune_context* context);
  47. // -----------------------------------------------------------------------------
  48. // END PUBLIC API
  49. // -----------------------------------------------------------------------------
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #ifdef PAR_SPRUNE_IMPLEMENTATION
  54. #define PARINT PAR_SPRUNE_INT
  55. #define PARFLT PAR_SPRUNE_FLT
  56. #include <stdlib.h>
  57. #include <assert.h>
  58. #include <stdbool.h>
  59. #ifndef PAR_PI
  60. #define PAR_PI (3.14159265359)
  61. #define PAR_MIN(a, b) (a > b ? b : a)
  62. #define PAR_MAX(a, b) (a > b ? a : b)
  63. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  64. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  65. #define PAR_SQR(a) ((a) * (a))
  66. #endif
  67. #ifndef PAR_MALLOC
  68. #define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
  69. #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
  70. #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
  71. #define PAR_FREE(BUF) free(BUF)
  72. #endif
  73. #ifndef PAR_ARRAY
  74. #define pa_free(a) ((a) ? PAR_FREE(pa___raw(a)), 0 : 0)
  75. #define pa_push(a, v) (pa___maybegrow(a, 1), (a)[pa___n(a)++] = (v))
  76. #define pa_count(a) ((a) ? pa___n(a) : 0)
  77. #define pa_add(a, n) (pa___maybegrow(a, n), pa___n(a) += (n))
  78. #define pa_last(a) ((a)[pa___n(a) - 1])
  79. #define pa_end(a) (a + pa_count(a))
  80. #define pa_clear(arr) if (arr) pa___n(arr) = 0
  81. #define pa___raw(a) ((int*) (a) -2)
  82. #define pa___m(a) pa___raw(a)[0]
  83. #define pa___n(a) pa___raw(a)[1]
  84. #define pa___needgrow(a, n) ((a) == 0 || pa___n(a) + (n) >= pa___m(a))
  85. #define pa___maybegrow(a, n) (pa___needgrow(a, (n)) ? pa___grow(a, n) : 0)
  86. #define pa___grow(a, n) (*((void**)& (a)) = pa___growf((void*) (a), (n), \
  87. sizeof(*(a))))
  88. static void* pa___growf(void* arr, int increment, int itemsize)
  89. {
  90. int dbl_cur = arr ? 2 * pa___m(arr) : 0;
  91. int min_needed = pa_count(arr) + increment;
  92. int m = dbl_cur > min_needed ? dbl_cur : min_needed;
  93. int* p = PAR_REALLOC(int, arr ? pa___raw(arr) : 0,
  94. itemsize * m / sizeof(int) + 2);
  95. if (p) {
  96. if (!arr) {
  97. p[1] = 0;
  98. }
  99. p[0] = m;
  100. return p + 2;
  101. }
  102. return (void*) (2 * sizeof(int));
  103. }
  104. #endif
  105. typedef struct {
  106. // Public:
  107. PARINT* collision_pairs;
  108. PARINT ncollision_pairs;
  109. PARINT* culled;
  110. PARINT nculled;
  111. // Private:
  112. PARFLT const* aabbs;
  113. PARINT naabbs;
  114. PARINT* sorted_indices[2];
  115. bool* overlap_flags[2];
  116. } par_sprune__context;
  117. static inline int par_qsort_cmpswap(char *__restrict a, char *__restrict b,
  118. size_t w,
  119. int (*compar)(const void *_a, const void *_b,
  120. void *_arg),
  121. void *arg)
  122. {
  123. char tmp, *end = a+w;
  124. if (compar(a, b, arg) > 0) {
  125. for(; a < end; a++, b++) { tmp = *a; *a = *b; *b = tmp; }
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. // qsort doesn't take a context, so we have our own portable implementation.
  131. // Parameters:
  132. // base is the array to be sorted
  133. // nel is the number of elements in the array
  134. // w is the size in bytes of each element of the array
  135. // compar is the comparison function
  136. // arg is a pointer to be passed to the comparison function
  137. //
  138. static inline void par_qsort(
  139. void *base,
  140. size_t nel,
  141. size_t w,
  142. int (*compar)(const void *_a, const void *_b, void *_arg),
  143. void *arg)
  144. {
  145. char *b = (char*) base, *end = (char*) (b + nel * w);
  146. if (nel < 7) {
  147. char *pi, *pj;
  148. for (pi = b+w; pi < end; pi += w) {
  149. for (pj = pi; pj > b && par_qsort_cmpswap(pj-w, pj, w, compar, arg);
  150. pj -= w) {}
  151. }
  152. return;
  153. }
  154. char *x, *y, *xend, ch;
  155. char *pl, *pr;
  156. char *last = b+w*(nel-1), *tmp;
  157. char *l[3];
  158. l[0] = b;
  159. l[1] = b+w*(nel/2);
  160. l[2] = last;
  161. if (compar(l[0],l[1],arg) > 0) {
  162. tmp=l[0]; l[0]=l[1]; l[1]=tmp;
  163. }
  164. if (compar(l[1],l[2],arg) > 0) {
  165. tmp=l[1]; l[1]=l[2]; l[2]=tmp;
  166. if (compar(l[0],l[1],arg) > 0) {
  167. tmp=l[0]; l[0]=l[1]; l[1]=tmp;
  168. }
  169. }
  170. for(x = l[1], y = last, xend = x+w; x<xend; x++, y++) {
  171. ch = *x; *x = *y; *y = ch;
  172. }
  173. pl = b;
  174. pr = last;
  175. while (pl < pr) {
  176. for (; pl < pr; pl += w) {
  177. if (par_qsort_cmpswap(pl, pr, w, compar, arg)) {
  178. pr -= w;
  179. break;
  180. }
  181. }
  182. for (; pl < pr; pr -= w) {
  183. if (par_qsort_cmpswap(pl, pr, w, compar, arg)) {
  184. pl += w;
  185. break;
  186. }
  187. }
  188. }
  189. par_qsort(b, (pl-b) / w, w, compar, arg);
  190. par_qsort(pl+w, (end - (pl+w)) / w, w, compar, arg);
  191. }
  192. void par_sprune_free_context(par_sprune_context* context)
  193. {
  194. par_sprune__context* ctx = (par_sprune__context*) context;
  195. pa_free(ctx->sorted_indices[0]);
  196. pa_free(ctx->sorted_indices[1]);
  197. pa_free(ctx->overlap_flags[0]);
  198. pa_free(ctx->overlap_flags[1]);
  199. pa_free(ctx->collision_pairs);
  200. PAR_FREE(ctx);
  201. }
  202. static void par_sprune__remove(PARINT* arr, PARINT val)
  203. {
  204. int i = pa_count(arr) - 1;
  205. for (; i >= 0; i--) {
  206. if (arr[i] == val) {
  207. break;
  208. }
  209. }
  210. assert(i >= 0);
  211. for (++i; i < pa_count(arr); i++) {
  212. PAR_SWAP(PARINT, arr[i - 1], arr[i]);
  213. }
  214. pa___n(arr)--;
  215. }
  216. typedef struct {
  217. PARFLT const* aabbs;
  218. } par__sprune_sorter;
  219. int par__cmpinds(const void* pa, const void* pb, void* psorter)
  220. {
  221. PARINT a = *((const PARINT*) pa);
  222. PARINT b = *((const PARINT*) pb);
  223. par__sprune_sorter* sorter = (par__sprune_sorter*) psorter;
  224. PARFLT const* aabbs = sorter->aabbs;
  225. PARFLT vala = aabbs[a];
  226. PARFLT valb = aabbs[b];
  227. if (vala > valb) return 1;
  228. if (vala < valb) return -1;
  229. if (a > b) return 1;
  230. if (a < b) return -1;
  231. return 0;
  232. }
  233. int par__cmppairs(const void* pa, const void* pb, void* unused)
  234. {
  235. PARINT a = *((const PARINT*) pa);
  236. PARINT b = *((const PARINT*) pb);
  237. if (a > b) return 1;
  238. if (a < b) return -1;
  239. a = *(1 + (const PARINT*) pa);
  240. b = *(1 + (const PARINT*) pb);
  241. if (a > b) return 1;
  242. if (a < b) return -1;
  243. return 0;
  244. }
  245. int par__cmpfind(const void* pa, const void* pb)
  246. {
  247. PARINT a = *((const PARINT*) pa);
  248. PARINT b = *((const PARINT*) pb);
  249. if (a > b) return 1;
  250. if (a < b) return -1;
  251. a = *(1 + (const PARINT*) pa);
  252. b = *(1 + (const PARINT*) pb);
  253. if (a > b) return 1;
  254. if (a < b) return -1;
  255. return 0;
  256. }
  257. par_sprune_context* par_sprune_overlap(PARFLT const* aabbs, PARINT naabbs,
  258. par_sprune_context* previous)
  259. {
  260. par_sprune__context* ctx = (par_sprune__context*) previous;
  261. if (!ctx) {
  262. ctx = PAR_CALLOC(par_sprune__context, 1);
  263. }
  264. ctx->aabbs = aabbs;
  265. ctx->naabbs = naabbs;
  266. for (int axis = 0; axis < 2; axis++) {
  267. pa_clear(ctx->sorted_indices[axis]);
  268. pa_add(ctx->sorted_indices[axis], naabbs * 2);
  269. pa_clear(ctx->overlap_flags[axis]);
  270. pa_add(ctx->overlap_flags[axis], naabbs * 2);
  271. }
  272. for (PARINT i = 0; i < naabbs; i++) {
  273. ctx->sorted_indices[0][i * 2 + 0] = i * 4 + 0;
  274. ctx->sorted_indices[1][i * 2 + 0] = i * 4 + 1;
  275. ctx->sorted_indices[0][i * 2 + 1] = i * 4 + 2;
  276. ctx->sorted_indices[1][i * 2 + 1] = i * 4 + 3;
  277. ctx->overlap_flags[0][i] = false;
  278. ctx->overlap_flags[1][i] = false;
  279. }
  280. par__sprune_sorter sorter;
  281. sorter.aabbs = ctx->aabbs;
  282. PARINT* active = 0;
  283. PARINT* pairs[2] = {0};
  284. for (int axis = 0; axis < 2; axis++) {
  285. PARINT* indices = ctx->sorted_indices[axis];
  286. par_qsort(indices, naabbs * 2, sizeof(PARINT), par__cmpinds, &sorter);
  287. pa_clear(active);
  288. for (PARINT i = 0; i < naabbs * 2; i++) {
  289. PARINT fltindex = indices[i];
  290. PARINT boxindex = fltindex / 4;
  291. bool ismin = ((fltindex - axis) % 4) == 0;
  292. if (ismin) {
  293. for (int j = 0; j < pa_count(active); j++) {
  294. PARINT a = PAR_MIN(active[j], boxindex);
  295. PARINT b = PAR_MAX(active[j], boxindex);
  296. pa_push(pairs[axis], a);
  297. pa_push(pairs[axis], b);
  298. }
  299. pa_push(active, boxindex);
  300. } else {
  301. par_sprune__remove(active, boxindex);
  302. }
  303. }
  304. }
  305. pa_free(active);
  306. par_qsort(pairs[0], pa_count(pairs[0]) / 2, 2 * sizeof(PARINT),
  307. par__cmppairs, 0);
  308. par_qsort(pairs[1], pa_count(pairs[1]) / 2, 2 * sizeof(PARINT),
  309. par__cmppairs, 0);
  310. pa_clear(ctx->collision_pairs);
  311. for (int i = 0; i < pa_count(pairs[0]); i += 2) {
  312. PARINT* key = pairs[0] + i;
  313. void* found = bsearch(key, pairs[1], pa_count(pairs[1]) / 2,
  314. sizeof(PARINT) * 2, par__cmpfind);
  315. if (found) {
  316. pa_push(ctx->collision_pairs, key[0]);
  317. pa_push(ctx->collision_pairs, key[1]);
  318. }
  319. }
  320. ctx->ncollision_pairs = pa_count(ctx->collision_pairs) / 2;
  321. pa_free(pairs[0]);
  322. pa_free(pairs[1]);
  323. return (par_sprune_context*) ctx;
  324. }
  325. #undef PARINT
  326. #undef PARFLT
  327. #endif // PAR_SPRUNE_IMPLEMENTATION
  328. #endif // PAR_SPRUNE_H