PageRenderTime 331ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/blender-2.63a/source/blender/blenlib/intern/boxpack2d.c

#
C | 437 lines | 283 code | 53 blank | 101 comment | 76 complexity | 97cd167bb0f3ece065b7f4e9a688075f MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-3.0, BSD-2-Clause, Apache-2.0, AGPL-1.0
  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. #define UPDATE_V34(b) UPDATE_V34X(b); UPDATE_V34Y(b)
  65. #define SET_BOXLEFT(b, f) (b)->v[TR]->x = f + (b)->w; \
  66. (b)->v[BL]->x = f; \
  67. UPDATE_V34X(b)
  68. #define SET_BOXRIGHT(b, f) (b)->v[BL]->x = f - (b)->w; \
  69. (b)->v[TR]->x = f; \
  70. UPDATE_V34X(b)
  71. #define SET_BOXBOTTOM(b, f) (b)->v[TR]->y = f + (b)->h; \
  72. (b)->v[BL]->y = f; \
  73. UPDATE_V34Y(b)
  74. #define SET_BOXTOP(b, f) (b)->v[BL]->y = f - (b)->h; \
  75. (b)->v[TR]->y = f; \
  76. UPDATE_V34Y(b)
  77. #define BOXINTERSECT(b1, b2) \
  78. !(BOXLEFT(b1) + EPSILON >= BOXRIGHT(b2) || \
  79. BOXBOTTOM(b1) + EPSILON >= BOXTOP(b2) || \
  80. BOXRIGHT(b1) - EPSILON <= BOXLEFT(b2) || \
  81. BOXTOP(b1) - EPSILON <= BOXBOTTOM(b2))
  82. #define MIN2(x,y) ((x) < (y) ? (x) : (y))
  83. #define MAX2(x,y) ((x) > (y) ? (x) : (y))
  84. #if 0
  85. #define BOXDEBUG(b) \
  86. printf("\tBox Debug i %i, w:%.3f h:%.3f x:%.3f y:%.3f\n", \
  87. b->index, b->w, b->h, b->x, b->y)
  88. #endif
  89. /* qsort function - sort largest to smallest */
  90. static int box_areasort(const void *p1, const void *p2)
  91. {
  92. const boxPack *b1 = p1, *b2 = p2;
  93. const float a1 = BOXAREA(b1);
  94. const float a2 = BOXAREA(b2);
  95. if (a1 < a2) return 1;
  96. else if (a1 > a2) return -1;
  97. return 0;
  98. }
  99. /* qsort vertex sorting function
  100. * sorts from lower left to top right It uses the current box's width and height
  101. * as offsets when sorting, this has the result of not placing boxes outside
  102. * the bounds of the existing backed area where possible
  103. * */
  104. static float box_width;
  105. static float box_height;
  106. static boxVert *vertarray;
  107. static int vertex_sort(const void *p1, const void *p2)
  108. {
  109. boxVert *v1, *v2;
  110. float a1, a2;
  111. v1 = vertarray + ((int *)p1)[0];
  112. v2 = vertarray + ((int *)p2)[0];
  113. a1 = MAX2(v1->x+box_width, v1->y+box_height);
  114. a2 = MAX2(v2->x+box_width, v2->y+box_height);
  115. /* sort largest to smallest */
  116. if (a1 > a2) return 1;
  117. else if (a1 < a2) return -1;
  118. return 0;
  119. }
  120. /* Main boxpacking function accessed from other functions
  121. * This sets boxes x,y to positive values, sorting from 0,0 outwards.
  122. * There is no limit to the space boxes may take, only that they will be packed
  123. * tightly into the lower left hand corner (0,0)
  124. *
  125. * boxarray - a pre allocated array of boxes.
  126. * only the 'box->x' and 'box->y' are set, 'box->w' and 'box->h' are used,
  127. * 'box->index' is not used at all, the only reason its there
  128. * is that the box array is sorted by area and programs need to be able
  129. * to have some way of writing the boxes back to the original data.
  130. * len - the number of boxes in the array.
  131. * tot_width and tot_height are set so you can normalize the data.
  132. * */
  133. void boxPack2D(boxPack *boxarray, const int len, float *tot_width, float *tot_height)
  134. {
  135. boxVert *vert; /* the current vert */
  136. int box_index, verts_pack_len, i, j, k, isect;
  137. int quad_flags[4] = {BLF, TRF, TLF, BRF}; /* use for looping */
  138. boxPack *box, *box_test; /*current box and another for intersection tests*/
  139. int *vertex_pack_indices; /*an array of indices used for sorting verts*/
  140. if (!len) {
  141. *tot_width = 0.0f;
  142. *tot_height = 0.0f;
  143. return;
  144. }
  145. /* Sort boxes, biggest first */
  146. qsort(boxarray, len, sizeof(boxPack), box_areasort);
  147. /* add verts to the boxes, these are only used internally */
  148. vert = vertarray = MEM_mallocN(len * 4 * sizeof(boxVert), "boxPack Verts");
  149. vertex_pack_indices = MEM_mallocN(len * 3 * sizeof(int), "boxPack Indices");
  150. for (box = boxarray, box_index = 0, i = 0; box_index < len; box_index++, box++) {
  151. vert->blb = vert->brb = vert->tlb =
  152. vert->isect_cache[0] = vert->isect_cache[1] =
  153. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  154. vert->free = CORNERFLAGS &~ TRF;
  155. vert->trb = box;
  156. vert->index = i; i++;
  157. box->v[BL] = vert; vert++;
  158. vert->trb= vert->brb = vert->tlb =
  159. vert->isect_cache[0] = vert->isect_cache[1] =
  160. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  161. vert->free = CORNERFLAGS &~ BLF;
  162. vert->blb = box;
  163. vert->index = i; i++;
  164. box->v[TR] = vert; vert++;
  165. vert->trb = vert->blb = vert->tlb =
  166. vert->isect_cache[0] = vert->isect_cache[1] =
  167. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  168. vert->free = CORNERFLAGS &~ BRF;
  169. vert->brb = box;
  170. vert->index = i; i++;
  171. box->v[TL] = vert; vert++;
  172. vert->trb = vert->blb = vert->brb =
  173. vert->isect_cache[0] = vert->isect_cache[1] =
  174. vert->isect_cache[2] = vert->isect_cache[3] = NULL;
  175. vert->free = CORNERFLAGS &~ TLF;
  176. vert->tlb = box;
  177. vert->index = i; i++;
  178. box->v[BR] = vert; vert++;
  179. }
  180. vert = NULL;
  181. /* Pack the First box!
  182. * then enter the main box-packing loop */
  183. box = boxarray; /* get the first box */
  184. /* First time, no boxes packed */
  185. box->v[BL]->free = 0; /* Can't use any if these */
  186. box->v[BR]->free &= ~(BLF | BRF);
  187. box->v[TL]->free &= ~(BLF | TLF);
  188. *tot_width = box->w;
  189. *tot_height = box->h;
  190. /* This sets all the vertex locations */
  191. SET_BOXLEFT(box, 0.0f);
  192. SET_BOXBOTTOM(box, 0.0f);
  193. box->x = box->y = 0.0f;
  194. for (i = 0; i < 3; i++)
  195. vertex_pack_indices[i] = box->v[i + 1]->index;
  196. verts_pack_len = 3;
  197. box++; /* next box, needed for the loop below */
  198. /* ...done packing the first box */
  199. /* Main boxpacking loop */
  200. for (box_index = 1; box_index < len; box_index++, box++) {
  201. /* These static floatds are used for sorting */
  202. box_width = box->w;
  203. box_height = box->h;
  204. qsort(vertex_pack_indices, verts_pack_len, sizeof(int), vertex_sort);
  205. /* Pack the box in with the others */
  206. /* sort the verts */
  207. isect = 1;
  208. for (i = 0; i < verts_pack_len && isect; i++) {
  209. vert = vertarray + vertex_pack_indices[i];
  210. /* printf("\ttesting vert %i %i %i %f %f\n", i,
  211. * vert->free, verts_pack_len, vert->x, vert->y); */
  212. /* This vert has a free quadrant
  213. * Test if we can place the box here
  214. * vert->free & quad_flags[j] - Checks
  215. * */
  216. for (j = 0; (j < 4) && isect; j++) {
  217. if (vert->free & quad_flags[j]) {
  218. switch (j) {
  219. case BL:
  220. SET_BOXRIGHT(box, vert->x);
  221. SET_BOXTOP(box, vert->y);
  222. break;
  223. case TR:
  224. SET_BOXLEFT(box, vert->x);
  225. SET_BOXBOTTOM(box, vert->y);
  226. break;
  227. case TL:
  228. SET_BOXRIGHT(box, vert->x);
  229. SET_BOXBOTTOM(box, vert->y);
  230. break;
  231. case BR:
  232. SET_BOXLEFT(box, vert->x);
  233. SET_BOXTOP(box, vert->y);
  234. break;
  235. }
  236. /* Now we need to check that the box intersects
  237. * with any other boxes
  238. * Assume no intersection... */
  239. isect = 0;
  240. if (/* Constrain boxes to positive X/Y values */
  241. BOXLEFT(box) < 0.0f || BOXBOTTOM(box) < 0.0f ||
  242. /* check for last intersected */
  243. ( vert->isect_cache[j] &&
  244. BOXINTERSECT(box, vert->isect_cache[j])))
  245. {
  246. /* Here we check that the last intersected
  247. * box will intersect with this one using
  248. * isect_cache that can store a pointer to a
  249. * box for each quadrant
  250. * big speedup */
  251. isect = 1;
  252. }
  253. else {
  254. /* do a full search for colliding box
  255. * this is really slow, some spatially divided
  256. * data-structure would be better */
  257. for (box_test = boxarray; box_test != box; box_test++) {
  258. if (BOXINTERSECT(box, box_test)) {
  259. /* Store the last intersecting here as cache
  260. * for faster checking next time around */
  261. vert->isect_cache[j] = box_test;
  262. isect = 1;
  263. break;
  264. }
  265. }
  266. }
  267. if (!isect) {
  268. /* maintain the total width and height */
  269. (*tot_width) = MAX2(BOXRIGHT(box), (*tot_width));
  270. (*tot_height) = MAX2(BOXTOP(box), (*tot_height));
  271. /* Place the box */
  272. vert->free &= ~quad_flags[j];
  273. switch (j) {
  274. case TR:
  275. box->v[BL] = vert;
  276. vert->trb = box;
  277. break;
  278. case TL:
  279. box->v[BR] = vert;
  280. vert->tlb = box;
  281. break;
  282. case BR:
  283. box->v[TL] = vert;
  284. vert->brb = box;
  285. break;
  286. case BL:
  287. box->v[TR] = vert;
  288. vert->blb = box;
  289. break;
  290. }
  291. /* Mask free flags for verts that are
  292. * on the bottom or side so we don't get
  293. * boxes outside the given rectangle ares
  294. *
  295. * We can do an else/if here because only the first
  296. * box can be at the very bottom left corner */
  297. if (BOXLEFT(box) <= 0) {
  298. box->v[TL]->free &= ~(TLF | BLF);
  299. box->v[BL]->free &= ~(TLF | BLF);
  300. }
  301. else if (BOXBOTTOM(box) <= 0) {
  302. box->v[BL]->free &= ~(BRF | BLF);
  303. box->v[BR]->free &= ~(BRF | BLF);
  304. }
  305. /* The following block of code does a logical
  306. * check with 2 adjacent boxes, its possible to
  307. * flag verts on one or both of the boxes
  308. * as being used by checking the width or
  309. * height of both boxes */
  310. if (vert->tlb && vert->trb && (box == vert->tlb || box == vert->trb)) {
  311. if (vert->tlb->h > vert->trb->h) {
  312. vert->trb->v[TL]->free &= ~(TLF | BLF);
  313. }
  314. else if (vert->tlb->h < vert->trb->h) {
  315. vert->tlb->v[TR]->free &= ~(TRF | BRF);
  316. }
  317. else { /*same*/
  318. vert->tlb->v[TR]->free &= ~BLF;
  319. vert->trb->v[TL]->free &= ~BRF;
  320. }
  321. }
  322. else if (vert->blb && vert->brb && (box == vert->blb || box == vert->brb)) {
  323. if (vert->blb->h > vert->brb->h) {
  324. vert->brb->v[BL]->free &= ~(TLF | BLF);
  325. }
  326. else if (vert->blb->h < vert->brb->h) {
  327. vert->blb->v[BR]->free &= ~(TRF | BRF);
  328. }
  329. else { /*same*/
  330. vert->blb->v[BR]->free &= ~TRF;
  331. vert->brb->v[BL]->free &= ~TLF;
  332. }
  333. }
  334. /* Horizontal */
  335. if (vert->tlb && vert->blb && (box == vert->tlb || box == vert->blb)) {
  336. if (vert->tlb->w > vert->blb->w) {
  337. vert->blb->v[TL]->free &= ~(TLF | TRF);
  338. }
  339. else if (vert->tlb->w < vert->blb->w) {
  340. vert->tlb->v[BL]->free &= ~(BLF | BRF);
  341. }
  342. else { /*same*/
  343. vert->blb->v[TL]->free &= ~TRF;
  344. vert->tlb->v[BL]->free &= ~BRF;
  345. }
  346. }
  347. else if (vert->trb && vert->brb && (box == vert->trb || box == vert->brb)) {
  348. if (vert->trb->w > vert->brb->w) {
  349. vert->brb->v[TR]->free &= ~(TLF | TRF);
  350. }
  351. else if (vert->trb->w < vert->brb->w) {
  352. vert->trb->v[BR]->free &= ~(BLF | BRF);
  353. }
  354. else { /*same*/
  355. vert->brb->v[TR]->free &= ~TLF;
  356. vert->trb->v[BR]->free &= ~BLF;
  357. }
  358. }
  359. /* End logical check */
  360. for (k = 0; k < 4; k++) {
  361. if (box->v[k] != vert) {
  362. vertex_pack_indices[verts_pack_len] = box->v[k]->index;
  363. verts_pack_len++;
  364. }
  365. }
  366. /* The Box verts are only used internally
  367. * Update the box x and y since thats what external
  368. * functions will see */
  369. box->x = BOXLEFT(box);
  370. box->y = BOXBOTTOM(box);
  371. }
  372. }
  373. }
  374. }
  375. }
  376. /* free all the verts, not really needed because they shouldn't be
  377. * touched anymore but accessing the pointers would crash blender */
  378. for (box_index = 0; box_index < len; box_index++) {
  379. box = boxarray + box_index;
  380. box->v[0] = box->v[1] = box->v[2] = box->v[3] = NULL;
  381. }
  382. MEM_freeN(vertex_pack_indices);
  383. MEM_freeN(vertarray);
  384. }