PageRenderTime 82ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/source/blender/blenlib/intern/boxpack2d.c

https://bitbucket.org/brita/blender-gl_debug
C | 439 lines | 281 code | 54 blank | 104 comment | 76 complexity | 402f49684516290577407936a225c20c MD5 | raw file
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): Campbell Barton
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/blenlib/intern/boxpack2d.c
  23. * \ingroup bli
  24. */
  25. #include <stdlib.h> /* for qsort */
  26. #include "MEM_guardedalloc.h"
  27. #include "BLI_boxpack2d.h"
  28. /* BoxPacker for backing 2D rectangles into a square
  29. *
  30. * The defined Below are for internal use only */
  31. typedef struct BoxVert {
  32. float x;
  33. float y;
  34. short free;
  35. struct BoxPack *trb; /* top right box */
  36. struct BoxPack *blb; /* bottom left box */
  37. struct BoxPack *brb; /* bottom right box */
  38. struct BoxPack *tlb; /* top left box */
  39. /* Store last intersecting boxes here
  40. * speedup intersection testing */
  41. struct BoxPack *isect_cache[4];
  42. int index;
  43. } BoxVert;
  44. /* free vert flags */
  45. #define EPSILON 0.0000001f
  46. #define BLF 1
  47. #define TRF 2
  48. #define TLF 4
  49. #define BRF 8
  50. #define CORNERFLAGS (BLF | TRF | TLF | BRF)
  51. #define BL 0
  52. #define TR 1
  53. #define TL 2
  54. #define BR 3
  55. #define BOXLEFT(b) ((b)->v[BL]->x)
  56. #define BOXRIGHT(b) ((b)->v[TR]->x)
  57. #define BOXBOTTOM(b) ((b)->v[BL]->y)
  58. #define BOXTOP(b) ((b)->v[TR]->y)
  59. #define BOXAREA(b) ((b)->w * (b)->h)
  60. #define UPDATE_V34X(b) ((b)->v[TL]->x = (b)->v[BL]->x); \
  61. ((b)->v[BR]->x = (b)->v[TR]->x)
  62. #define UPDATE_V34Y(b) ((b)->v[TL]->y = (b)->v[TR]->y); \
  63. ((b)->v[BR]->y = (b)->v[BL]->y)
  64. /* UNUSED */
  65. // #define UPDATE_V34(b) UPDATE_V34X(b); UPDATE_V34Y(b)
  66. #define SET_BOXLEFT(b, f) (b)->v[TR]->x = f + (b)->w; \
  67. (b)->v[BL]->x = f; \
  68. UPDATE_V34X(b)
  69. #define SET_BOXRIGHT(b, f) (b)->v[BL]->x = f - (b)->w; \
  70. (b)->v[TR]->x = f; \
  71. UPDATE_V34X(b)
  72. #define SET_BOXBOTTOM(b, f) (b)->v[TR]->y = f + (b)->h; \
  73. (b)->v[BL]->y = f; \
  74. UPDATE_V34Y(b)
  75. #define SET_BOXTOP(b, f) (b)->v[BL]->y = f - (b)->h; \
  76. (b)->v[TR]->y = f; \
  77. UPDATE_V34Y(b)
  78. #define BOXINTERSECT(b1, b2) \
  79. !(BOXLEFT(b1) + EPSILON >= BOXRIGHT(b2) || \
  80. BOXBOTTOM(b1) + EPSILON >= BOXTOP(b2) || \
  81. BOXRIGHT(b1) - EPSILON <= BOXLEFT(b2) || \
  82. BOXTOP(b1) - EPSILON <= BOXBOTTOM(b2))
  83. /* compiler should inline */
  84. static float max_ff(const float a, const float b) { return b > a ? b : a; }
  85. #if 0
  86. #define BOXDEBUG(b) \
  87. printf("\tBox Debug i %i, w:%.3f h:%.3f x:%.3f y:%.3f\n", \
  88. b->index, b->w, b->h, b->x, b->y)
  89. #endif
  90. /* qsort function - sort largest to smallest */
  91. static int box_areasort(const void *p1, const void *p2)
  92. {
  93. const BoxPack *b1 = p1, *b2 = p2;
  94. const float a1 = BOXAREA(b1);
  95. const float a2 = BOXAREA(b2);
  96. if (a1 < a2) return 1;
  97. else if (a1 > a2) return -1;
  98. return 0;
  99. }
  100. /* qsort vertex sorting function
  101. * sorts from lower left to top right It uses the current box's width and height
  102. * as offsets when sorting, this has the result of not placing boxes outside
  103. * the bounds of the existing backed area where possible
  104. * */
  105. static float box_width;
  106. static float box_height;
  107. static BoxVert *vertarray;
  108. static int vertex_sort(const void *p1, const void *p2)
  109. {
  110. BoxVert *v1, *v2;
  111. float a1, a2;
  112. v1 = vertarray + ((int *)p1)[0];
  113. v2 = vertarray + ((int *)p2)[0];
  114. a1 = max_ff(v1->x + box_width, v1->y + box_height);
  115. a2 = max_ff(v2->x + box_width, v2->y + box_height);
  116. /* sort largest to smallest */
  117. if (a1 > a2) return 1;
  118. else if (a1 < a2) return -1;
  119. return 0;
  120. }
  121. /* Main boxpacking function accessed from other functions
  122. * This sets boxes x,y to positive values, sorting from 0,0 outwards.
  123. * There is no limit to the space boxes may take, only that they will be packed
  124. * tightly into the lower left hand corner (0,0)
  125. *
  126. * boxarray - a pre allocated array of boxes.
  127. * only the 'box->x' and 'box->y' are set, 'box->w' and 'box->h' are used,
  128. * 'box->index' is not used at all, the only reason its there
  129. * is that the box array is sorted by area and programs need to be able
  130. * to have some way of writing the boxes back to the original data.
  131. * len - the number of boxes in the array.
  132. * tot_width and tot_height are set so you can normalize the data.
  133. * */
  134. void BLI_box_pack_2D(BoxPack *boxarray, const int len, float *tot_width, float *tot_height)
  135. {
  136. BoxVert *vert; /* the current vert */
  137. int box_index, verts_pack_len, i, j, k, isect;
  138. int quad_flags[4] = {BLF, TRF, TLF, BRF}; /* use for looping */
  139. BoxPack *box, *box_test; /*current box and another for intersection tests*/
  140. int *vertex_pack_indices; /*an array of indices used for sorting verts*/
  141. if (!len) {
  142. *tot_width = 0.0f;
  143. *tot_height = 0.0f;
  144. return;
  145. }
  146. /* Sort boxes, biggest first */
  147. qsort(boxarray, len, sizeof(BoxPack), box_areasort);
  148. /* add verts to the boxes, these are only used internally */
  149. vert = vertarray = MEM_mallocN(len * 4 * sizeof(BoxVert), "BoxPack Verts");
  150. vertex_pack_indices = MEM_mallocN(len * 3 * sizeof(int), "BoxPack Indices");
  151. for (box = boxarray, box_index = 0, i = 0; box_index < len; box_index++, box++) {
  152. vert->blb = vert->brb = vert->tlb =
  153. vert->isect_cache[0] = vert->isect_cache[1] =
  154. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  155. vert->free = CORNERFLAGS & ~TRF;
  156. vert->trb = box;
  157. vert->index = i; i++;
  158. box->v[BL] = vert; vert++;
  159. vert->trb = vert->brb = vert->tlb =
  160. vert->isect_cache[0] = vert->isect_cache[1] =
  161. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  162. vert->free = CORNERFLAGS & ~BLF;
  163. vert->blb = box;
  164. vert->index = i; i++;
  165. box->v[TR] = vert; vert++;
  166. vert->trb = vert->blb = vert->tlb =
  167. vert->isect_cache[0] = vert->isect_cache[1] =
  168. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  169. vert->free = CORNERFLAGS & ~BRF;
  170. vert->brb = box;
  171. vert->index = i; i++;
  172. box->v[TL] = vert; vert++;
  173. vert->trb = vert->blb = vert->brb =
  174. vert->isect_cache[0] = vert->isect_cache[1] =
  175. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  176. vert->free = CORNERFLAGS & ~TLF;
  177. vert->tlb = box;
  178. vert->index = i; i++;
  179. box->v[BR] = vert; vert++;
  180. }
  181. vert = NULL;
  182. /* Pack the First box!
  183. * then enter the main box-packing loop */
  184. box = boxarray; /* get the first box */
  185. /* First time, no boxes packed */
  186. box->v[BL]->free = 0; /* Can't use any if these */
  187. box->v[BR]->free &= ~(BLF | BRF);
  188. box->v[TL]->free &= ~(BLF | TLF);
  189. *tot_width = box->w;
  190. *tot_height = box->h;
  191. /* This sets all the vertex locations */
  192. SET_BOXLEFT(box, 0.0f);
  193. SET_BOXBOTTOM(box, 0.0f);
  194. box->x = box->y = 0.0f;
  195. for (i = 0; i < 3; i++)
  196. vertex_pack_indices[i] = box->v[i + 1]->index;
  197. verts_pack_len = 3;
  198. box++; /* next box, needed for the loop below */
  199. /* ...done packing the first box */
  200. /* Main boxpacking loop */
  201. for (box_index = 1; box_index < len; box_index++, box++) {
  202. /* These static floatds are used for sorting */
  203. box_width = box->w;
  204. box_height = box->h;
  205. qsort(vertex_pack_indices, verts_pack_len, sizeof(int), vertex_sort);
  206. /* Pack the box in with the others */
  207. /* sort the verts */
  208. isect = 1;
  209. for (i = 0; i < verts_pack_len && isect; i++) {
  210. vert = vertarray + vertex_pack_indices[i];
  211. /* printf("\ttesting vert %i %i %i %f %f\n", i,
  212. * vert->free, verts_pack_len, vert->x, vert->y); */
  213. /* This vert has a free quadrant
  214. * Test if we can place the box here
  215. * vert->free & quad_flags[j] - Checks
  216. * */
  217. for (j = 0; (j < 4) && isect; j++) {
  218. if (vert->free & quad_flags[j]) {
  219. switch (j) {
  220. case BL:
  221. SET_BOXRIGHT(box, vert->x);
  222. SET_BOXTOP(box, vert->y);
  223. break;
  224. case TR:
  225. SET_BOXLEFT(box, vert->x);
  226. SET_BOXBOTTOM(box, vert->y);
  227. break;
  228. case TL:
  229. SET_BOXRIGHT(box, vert->x);
  230. SET_BOXBOTTOM(box, vert->y);
  231. break;
  232. case BR:
  233. SET_BOXLEFT(box, vert->x);
  234. SET_BOXTOP(box, vert->y);
  235. break;
  236. }
  237. /* Now we need to check that the box intersects
  238. * with any other boxes
  239. * Assume no intersection... */
  240. isect = 0;
  241. if ( /* Constrain boxes to positive X/Y values */
  242. BOXLEFT(box) < 0.0f || BOXBOTTOM(box) < 0.0f ||
  243. /* check for last intersected */
  244. (vert->isect_cache[j] &&
  245. BOXINTERSECT(box, vert->isect_cache[j])))
  246. {
  247. /* Here we check that the last intersected
  248. * box will intersect with this one using
  249. * isect_cache that can store a pointer to a
  250. * box for each quadrant
  251. * big speedup */
  252. isect = 1;
  253. }
  254. else {
  255. /* do a full search for colliding box
  256. * this is really slow, some spatially divided
  257. * data-structure would be better */
  258. for (box_test = boxarray; box_test != box; box_test++) {
  259. if (BOXINTERSECT(box, box_test)) {
  260. /* Store the last intersecting here as cache
  261. * for faster checking next time around */
  262. vert->isect_cache[j] = box_test;
  263. isect = 1;
  264. break;
  265. }
  266. }
  267. }
  268. if (!isect) {
  269. /* maintain the total width and height */
  270. (*tot_width) = max_ff(BOXRIGHT(box), (*tot_width));
  271. (*tot_height) = max_ff(BOXTOP(box), (*tot_height));
  272. /* Place the box */
  273. vert->free &= ~quad_flags[j];
  274. switch (j) {
  275. case TR:
  276. box->v[BL] = vert;
  277. vert->trb = box;
  278. break;
  279. case TL:
  280. box->v[BR] = vert;
  281. vert->tlb = box;
  282. break;
  283. case BR:
  284. box->v[TL] = vert;
  285. vert->brb = box;
  286. break;
  287. case BL:
  288. box->v[TR] = vert;
  289. vert->blb = box;
  290. break;
  291. }
  292. /* Mask free flags for verts that are
  293. * on the bottom or side so we don't get
  294. * boxes outside the given rectangle ares
  295. *
  296. * We can do an else/if here because only the first
  297. * box can be at the very bottom left corner */
  298. if (BOXLEFT(box) <= 0) {
  299. box->v[TL]->free &= ~(TLF | BLF);
  300. box->v[BL]->free &= ~(TLF | BLF);
  301. }
  302. else if (BOXBOTTOM(box) <= 0) {
  303. box->v[BL]->free &= ~(BRF | BLF);
  304. box->v[BR]->free &= ~(BRF | BLF);
  305. }
  306. /* The following block of code does a logical
  307. * check with 2 adjacent boxes, its possible to
  308. * flag verts on one or both of the boxes
  309. * as being used by checking the width or
  310. * height of both boxes */
  311. if (vert->tlb && vert->trb && (box == vert->tlb || box == vert->trb)) {
  312. if (vert->tlb->h > vert->trb->h) {
  313. vert->trb->v[TL]->free &= ~(TLF | BLF);
  314. }
  315. else if (vert->tlb->h < vert->trb->h) {
  316. vert->tlb->v[TR]->free &= ~(TRF | BRF);
  317. }
  318. else { /*same*/
  319. vert->tlb->v[TR]->free &= ~BLF;
  320. vert->trb->v[TL]->free &= ~BRF;
  321. }
  322. }
  323. else if (vert->blb && vert->brb && (box == vert->blb || box == vert->brb)) {
  324. if (vert->blb->h > vert->brb->h) {
  325. vert->brb->v[BL]->free &= ~(TLF | BLF);
  326. }
  327. else if (vert->blb->h < vert->brb->h) {
  328. vert->blb->v[BR]->free &= ~(TRF | BRF);
  329. }
  330. else { /*same*/
  331. vert->blb->v[BR]->free &= ~TRF;
  332. vert->brb->v[BL]->free &= ~TLF;
  333. }
  334. }
  335. /* Horizontal */
  336. if (vert->tlb && vert->blb && (box == vert->tlb || box == vert->blb)) {
  337. if (vert->tlb->w > vert->blb->w) {
  338. vert->blb->v[TL]->free &= ~(TLF | TRF);
  339. }
  340. else if (vert->tlb->w < vert->blb->w) {
  341. vert->tlb->v[BL]->free &= ~(BLF | BRF);
  342. }
  343. else { /*same*/
  344. vert->blb->v[TL]->free &= ~TRF;
  345. vert->tlb->v[BL]->free &= ~BRF;
  346. }
  347. }
  348. else if (vert->trb && vert->brb && (box == vert->trb || box == vert->brb)) {
  349. if (vert->trb->w > vert->brb->w) {
  350. vert->brb->v[TR]->free &= ~(TLF | TRF);
  351. }
  352. else if (vert->trb->w < vert->brb->w) {
  353. vert->trb->v[BR]->free &= ~(BLF | BRF);
  354. }
  355. else { /*same*/
  356. vert->brb->v[TR]->free &= ~TLF;
  357. vert->trb->v[BR]->free &= ~BLF;
  358. }
  359. }
  360. /* End logical check */
  361. for (k = 0; k < 4; k++) {
  362. if (box->v[k] != vert) {
  363. vertex_pack_indices[verts_pack_len] = box->v[k]->index;
  364. verts_pack_len++;
  365. }
  366. }
  367. /* The Box verts are only used internally
  368. * Update the box x and y since thats what external
  369. * functions will see */
  370. box->x = BOXLEFT(box);
  371. box->y = BOXBOTTOM(box);
  372. }
  373. }
  374. }
  375. }
  376. }
  377. /* free all the verts, not really needed because they shouldn't be
  378. * touched anymore but accessing the pointers would crash blender */
  379. for (box_index = 0; box_index < len; box_index++) {
  380. box = boxarray + box_index;
  381. box->v[0] = box->v[1] = box->v[2] = box->v[3] = NULL;
  382. }
  383. MEM_freeN(vertex_pack_indices);
  384. MEM_freeN(vertarray);
  385. }