par_bubbles.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. // BUBBLES :: https://github.com/prideout/par
  2. // Simple C library for packing circles into hierarchical (or flat) diagrams.
  3. //
  4. // Implements "Visualization of Large Hierarchical Data by Circle Packing" from
  5. // Wang et al (2006).
  6. //
  7. // Also contains an implementation of Emo Welzl's "Smallest enclosing disks"
  8. // algorithm (1991) for enclosing points with circles, and a related method
  9. // from Mike Bostock for enclosing circles with the smallest possible circle.
  10. //
  11. // The API is divided into four sections:
  12. //
  13. // - Enclosing. Compute the smallest bounding circle for points or circles.
  14. // - Packing. Pack circles together, or into other circles.
  15. // - Queries. Given a touch point, pick a circle from a hierarchy, etc.
  16. // - Deep Zoom. Uses relative coordinate systems to support arbitrary depth.
  17. //
  18. // In addition to the comment block above each function declaration, the API
  19. // has informal documentation here:
  20. //
  21. // http://github.prideout.net/bubbles/
  22. //
  23. // The MIT License
  24. // Copyright (c) 2015 Philip Rideout
  25. #ifndef PAR_BUBBLES_H
  26. #define PAR_BUBBLES_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include <stdbool.h>
  31. #include <stdint.h>
  32. // This can be any signed integer type.
  33. #ifndef PAR_BUBBLES_INT
  34. #define PAR_BUBBLES_INT int32_t
  35. #endif
  36. // This must be "float" or "double" or "long double". Note that you should not
  37. // need high precision if you use the relative coordinate systems API.
  38. #ifndef PAR_BUBBLES_FLT
  39. #define PAR_BUBBLES_FLT double
  40. #endif
  41. // Enclosing / Touching --------------------------------------------------------
  42. // Read an array of (x,y) coordinates, write a single 3-tuple (x,y,radius).
  43. void par_bubbles_enclose_points(PAR_BUBBLES_FLT const* xy, PAR_BUBBLES_INT npts,
  44. PAR_BUBBLES_FLT* result);
  45. // Read an array of 3-tuples (x,y,radius), write a 3-tuple (x,y,radius).
  46. void par_bubbles_enclose_disks(PAR_BUBBLES_FLT const* xyr,
  47. PAR_BUBBLES_INT ndisks, PAR_BUBBLES_FLT* result);
  48. // Find the circle (x,y,radius) that is tangent to 3 points (x,y).
  49. void par_bubbles_touch_three_points(PAR_BUBBLES_FLT const* xy,
  50. PAR_BUBBLES_FLT* result);
  51. // Find a position for disk "c" that makes it tangent to "a" and "b".
  52. // Note that the ordering of a and b can affect where c will land.
  53. // All three arguments are pointers to three-tuples (x,y,radius).
  54. void par_bubbles_touch_two_disks(PAR_BUBBLES_FLT* c, PAR_BUBBLES_FLT const* a,
  55. PAR_BUBBLES_FLT const* b);
  56. // Returns the smallest circle that intersects the three specified circles.
  57. // This is the problem of Problem of Apollonius!
  58. void par_bubbles_touch_three_disks(PAR_BUBBLES_FLT const* xyr1,
  59. PAR_BUBBLES_FLT const* xyr2, PAR_BUBBLES_FLT const* xyr3,
  60. PAR_BUBBLES_FLT* result);
  61. // Packing ---------------------------------------------------------------------
  62. // Tiny POD structure returned by all packing functions. Private data is
  63. // attached after the public fields, so clients should call the provided
  64. // free function rather than freeing the memory manually.
  65. typedef struct {
  66. PAR_BUBBLES_FLT* xyr; // array of 3-tuples (x y radius) in input order
  67. PAR_BUBBLES_INT count; // number of 3-tuples in "xyr"
  68. PAR_BUBBLES_INT* ids; // populated by par_bubbles_cull
  69. } par_bubbles_t;
  70. void par_bubbles_free_result(par_bubbles_t*);
  71. // Entry point for unbounded non-hierarchical packing. Takes a list of radii.
  72. par_bubbles_t* par_bubbles_pack(PAR_BUBBLES_FLT const* radiuses,
  73. PAR_BUBBLES_INT nradiuses);
  74. // Consume a hierarchy defined by a list of integers. Each integer is an index
  75. // to its parent. The root node is its own parent, and it must be the first node
  76. // in the list. Clients do not have control over individual radiuses, only the
  77. // radius of the outermost enclosing disk.
  78. par_bubbles_t* par_bubbles_hpack_circle(PAR_BUBBLES_INT* nodes,
  79. PAR_BUBBLES_INT nnodes, PAR_BUBBLES_FLT radius);
  80. // Queries ---------------------------------------------------------------------
  81. // Find the node at the given position. Children are on top of their parents.
  82. // If the result is -1, there is no node at the given pick coordinate.
  83. PAR_BUBBLES_INT par_bubbles_pick(par_bubbles_t const*, PAR_BUBBLES_FLT x,
  84. PAR_BUBBLES_FLT y);
  85. // Get bounding box; take a pointer to 4 floats and set them to min xy, max xy.
  86. void par_bubbles_compute_aabb(par_bubbles_t const*, PAR_BUBBLES_FLT* aabb);
  87. // Check if the given circle (3-tuple) intersects the given aabb (4-tuple).
  88. bool par_bubbles_check_aabb(PAR_BUBBLES_FLT const* disk,
  89. PAR_BUBBLES_FLT const* aabb);
  90. // Clip the bubble diagram to the given AABB (4-tuple of left,bottom,right,top)
  91. // and return the result. Circles smaller than the given world-space
  92. // "minradius" are removed. Optionally, an existing diagram (dst) can be passed
  93. // in to receive the culled dataset, which reduces the number of memory allocs
  94. // when calling this function frequently. Pass null to "dst" to create a new
  95. // culled diagram.
  96. par_bubbles_t* par_bubbles_cull(par_bubbles_t const* src,
  97. PAR_BUBBLES_FLT const* aabb, PAR_BUBBLES_FLT minradius, par_bubbles_t* dst);
  98. // Dump out a SVG file for diagnostic purposes.
  99. void par_bubbles_export(par_bubbles_t const* bubbles, char const* filename);
  100. // Returns a pointer to a list of children nodes.
  101. void par_bubbles_get_children(par_bubbles_t const* bubbles, PAR_BUBBLES_INT idx,
  102. PAR_BUBBLES_INT** pchildren, PAR_BUBBLES_INT* nchildren);
  103. // Returns the given node's parent, or 0 if it's the root.
  104. PAR_BUBBLES_INT par_bubbles_get_parent(par_bubbles_t const* bubbles,
  105. PAR_BUBBLES_INT idx);
  106. // Finds the height of the tree and returns one of its deepest leaves.
  107. void par_bubbles_get_maxdepth(par_bubbles_t const* bubbles,
  108. PAR_BUBBLES_INT* maxdepth, PAR_BUBBLES_INT* leaf);
  109. // Finds the height of the tree at a certain node.
  110. PAR_BUBBLES_INT par_bubbles_get_depth(par_bubbles_t const* bubbles,
  111. PAR_BUBBLES_INT node);
  112. // Returns a 4-tuple (min xy, max xy) for the given node.
  113. void par_bubbles_compute_aabb_for_node(par_bubbles_t const* bubbles,
  114. PAR_BUBBLES_INT node, PAR_BUBBLES_FLT* aabb);
  115. // Find the deepest node that is an ancestor of both A and B. Classic!
  116. PAR_BUBBLES_INT par_bubbles_lowest_common_ancestor(par_bubbles_t const* bubbles,
  117. PAR_BUBBLES_INT node_a, PAR_BUBBLES_INT node_b);
  118. // Deep Zoom -------------------------------------------------------------------
  119. // Similar to hpack, but maintains precision by storing disk positions within
  120. // the local coordinate system of their parent. After calling this function,
  121. // clients can use cull_local to flatten the coordinate systems.
  122. par_bubbles_t* par_bubbles_hpack_local(PAR_BUBBLES_INT* nodes,
  123. PAR_BUBBLES_INT nnodes);
  124. // Similar to par_bubbles_cull, but takes a root node rather than an AABB,
  125. // and returns a result within the local coordinate system of the new root.
  126. // In other words, the new root will have radius 1, centered at (0,0). The
  127. // minradius is also expressed in this coordinate system.
  128. par_bubbles_t* par_bubbles_cull_local(par_bubbles_t const* src,
  129. PAR_BUBBLES_FLT const* aabb, PAR_BUBBLES_FLT minradius,
  130. PAR_BUBBLES_INT root, par_bubbles_t* dst);
  131. // Finds the smallest node in the given bubble diagram that completely encloses
  132. // the given axis-aligned bounding box (min xy, max xy). The AABB coordinates
  133. // are expressed in the local coordinate system of the given root node.
  134. PAR_BUBBLES_INT par_bubbles_find_local(par_bubbles_t const* src,
  135. PAR_BUBBLES_FLT const* aabb, PAR_BUBBLES_INT root);
  136. // Similar to pick, but expects (x,y) to be in the coordinate system of the
  137. // given root node.
  138. PAR_BUBBLES_INT par_bubbles_pick_local(par_bubbles_t const*, PAR_BUBBLES_FLT x,
  139. PAR_BUBBLES_FLT y, PAR_BUBBLES_INT root, PAR_BUBBLES_FLT minradius);
  140. // Obtains the scale and translation (which should be applied in that order)
  141. // that can move a point from the node0 coord system to the node1 coord system.
  142. // The "xform" argument should point to three floats, which will be populated
  143. // with: x translation, y translation, and scale.
  144. bool par_bubbles_transform_local(par_bubbles_t const* bubbles,
  145. PAR_BUBBLES_FLT* xform, PAR_BUBBLES_INT node0, PAR_BUBBLES_INT node1);
  146. // Dump out a SVG file for diagnostic purposes.
  147. void par_bubbles_export_local(par_bubbles_t const* bubbles,
  148. PAR_BUBBLES_INT idx, char const* filename);
  149. typedef enum {
  150. PAR_BUBBLES_FILTER_DEFAULT,
  151. PAR_BUBBLES_FILTER_DISCARD_LAST_CHILD,
  152. PAR_BUBBLES_FILTER_KEEP_ONLY_LAST_CHILD
  153. } par_bubbles_filter;
  154. // Special-case function that affects the behavior of subsequent calls to
  155. // cull_local. Allows clients to filter the children list of each non-leaf
  156. // node, which is especially useful when using placeholder bubbles for labels.
  157. void par_bubbles_set_filter(par_bubbles_t* src, par_bubbles_filter f);
  158. typedef enum {
  159. PAR_BUBBLES_HORIZONTAL,
  160. PAR_BUBBLES_VERTICAL
  161. } par_bubbles_orientation;
  162. // Sets some global state that affect subsequent calls to hpack. The first two
  163. // children can either be placed horizontally (default) or vertically. The
  164. // effect of this is subtle, since overall layout is obviously circular.
  165. void par_bubbles_set_orientation(par_bubbles_orientation );
  166. #ifndef PAR_PI
  167. #define PAR_PI (3.14159265359)
  168. #define PAR_MIN(a, b) (a > b ? b : a)
  169. #define PAR_MAX(a, b) (a > b ? a : b)
  170. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  171. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  172. #define PAR_SQR(a) ((a) * (a))
  173. #endif
  174. #ifndef PAR_MALLOC
  175. #define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
  176. #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
  177. #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
  178. #define PAR_FREE(BUF) free(BUF)
  179. #endif
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. // -----------------------------------------------------------------------------
  184. // END PUBLIC API
  185. // -----------------------------------------------------------------------------
  186. #ifdef PAR_BUBBLES_IMPLEMENTATION
  187. #define PARINT PAR_BUBBLES_INT
  188. #define PARFLT PAR_BUBBLES_FLT
  189. #include <math.h>
  190. #include <stdio.h>
  191. #include <stdlib.h>
  192. #include <float.h>
  193. #include <assert.h>
  194. static par_bubbles_orientation par_bubbles__ostate = PAR_BUBBLES_HORIZONTAL;
  195. typedef struct {
  196. PARINT prev;
  197. PARINT next;
  198. } par_bubbles__node;
  199. typedef struct {
  200. PARFLT* xyr; // results array
  201. PARINT count; // client-provided count
  202. PARINT* ids; // populated by par_bubbles_cull
  203. PARFLT const* radiuses; // client-provided radius list
  204. par_bubbles__node* chain; // counterclockwise enveloping chain
  205. PARINT const* graph_parents; // client-provided parent indices
  206. PARINT* graph_children; // flat list of children indices
  207. PARINT* graph_heads; // list of "pointers" to first child
  208. PARINT* graph_tails; // list of "pointers" to one-past-last child
  209. PARINT npacked;
  210. PARINT maxwidth;
  211. PARINT capacity;
  212. par_bubbles_filter filter;
  213. } par_bubbles__t;
  214. static PARFLT par_bubbles__len2(PARFLT const* a)
  215. {
  216. return a[0] * a[0] + a[1] * a[1];
  217. }
  218. static void par_bubbles__initgraph(par_bubbles__t* bubbles)
  219. {
  220. PARINT const* parents = bubbles->graph_parents;
  221. PARINT* nchildren = PAR_CALLOC(PARINT, bubbles->count);
  222. for (PARINT i = 0; i < bubbles->count; i++) {
  223. nchildren[parents[i]]++;
  224. }
  225. PARINT c = 0;
  226. bubbles->graph_heads = PAR_CALLOC(PARINT, bubbles->count * 2);
  227. bubbles->graph_tails = bubbles->graph_heads + bubbles->count;
  228. for (PARINT i = 0; i < bubbles->count; i++) {
  229. bubbles->maxwidth = PAR_MAX(bubbles->maxwidth, nchildren[i]);
  230. bubbles->graph_heads[i] = bubbles->graph_tails[i] = c;
  231. c += nchildren[i];
  232. }
  233. bubbles->graph_heads[0] = bubbles->graph_tails[0] = 1;
  234. bubbles->graph_children = PAR_MALLOC(PARINT, c);
  235. for (PARINT i = 1; i < bubbles->count; i++) {
  236. PARINT parent = parents[i];
  237. bubbles->graph_children[bubbles->graph_tails[parent]++] = i;
  238. }
  239. PAR_FREE(nchildren);
  240. }
  241. static void par_bubbles__initflat(par_bubbles__t* bubbles)
  242. {
  243. PARFLT* xyr = bubbles->xyr;
  244. PARFLT const* radii = bubbles->radiuses;
  245. par_bubbles__node* chain = bubbles->chain;
  246. PARFLT x0, y0, x1, y1;
  247. if (par_bubbles__ostate == PAR_BUBBLES_HORIZONTAL) {
  248. x0 = -radii[0];
  249. y0 = 0;
  250. x1 = radii[1];
  251. y1 = 0;
  252. } else {
  253. x0 = 0;
  254. y0 = -radii[0];
  255. x1 = 0;
  256. y1 = radii[1];
  257. }
  258. *xyr++ = x0;
  259. *xyr++ = y0;
  260. *xyr++ = *radii++;
  261. if (bubbles->count == ++bubbles->npacked) {
  262. return;
  263. }
  264. *xyr++ = x1;
  265. *xyr++ = y1;
  266. *xyr++ = *radii++;
  267. if (bubbles->count == ++bubbles->npacked) {
  268. return;
  269. }
  270. xyr[2] = *radii;
  271. par_bubbles_touch_two_disks(xyr, xyr - 6, xyr - 3);
  272. if (bubbles->count == ++bubbles->npacked) {
  273. return;
  274. }
  275. chain[0].prev = 2;
  276. chain[0].next = 1;
  277. chain[1].prev = 0;
  278. chain[1].next = 2;
  279. chain[2].prev = 1;
  280. chain[2].next = 0;
  281. }
  282. // March forward or backward along the enveloping chain, starting with the
  283. // node at "cn" and testing for collision against the node at "ci".
  284. static PARINT par_bubbles__collide(par_bubbles__t* bubbles, PARINT ci,
  285. PARINT cn, PARINT* cj, PARINT direction)
  286. {
  287. PARFLT const* ci_xyr = bubbles->xyr + ci * 3;
  288. par_bubbles__node* chain = bubbles->chain;
  289. PARINT nsteps = 1;
  290. if (direction > 0) {
  291. for (PARINT i = chain[cn].next; i != cn; i = chain[i].next, ++nsteps) {
  292. PARFLT const* i_xyr = bubbles->xyr + i * 3;
  293. PARFLT dx = i_xyr[0] - ci_xyr[0];
  294. PARFLT dy = i_xyr[1] - ci_xyr[1];
  295. PARFLT dr = i_xyr[2] + ci_xyr[2];
  296. if (0.999 * dr * dr > dx * dx + dy * dy) {
  297. *cj = i;
  298. return nsteps;
  299. }
  300. }
  301. return 0;
  302. }
  303. for (PARINT i = chain[cn].prev; i != cn; i = chain[i].prev, ++nsteps) {
  304. PARFLT const* i_xyr = bubbles->xyr + i * 3;
  305. PARFLT dx = i_xyr[0] - ci_xyr[0];
  306. PARFLT dy = i_xyr[1] - ci_xyr[1];
  307. PARFLT dr = i_xyr[2] + ci_xyr[2];
  308. if (0.999 * dr * dr > dx * dx + dy * dy) {
  309. *cj = i;
  310. return nsteps;
  311. }
  312. }
  313. return 0;
  314. }
  315. static void par_bubbles__packflat(par_bubbles__t* bubbles)
  316. {
  317. PARFLT const* radii = bubbles->radiuses;
  318. PARFLT* xyr = bubbles->xyr;
  319. par_bubbles__node* chain = bubbles->chain;
  320. // Find the circle closest to the origin, known as "Cm" in the paper.
  321. PARINT cm = 0;
  322. PARFLT mindist = par_bubbles__len2(xyr + 0);
  323. PARFLT dist = par_bubbles__len2(xyr + 3);
  324. if (dist > mindist) {
  325. cm = 1;
  326. }
  327. dist = par_bubbles__len2(xyr + 6);
  328. if (dist > mindist) {
  329. cm = 2;
  330. }
  331. // In the paper, "Cn" is always the node that follows "Cm".
  332. PARINT ci, cn = chain[cm].next;
  333. for (ci = bubbles->npacked; ci < bubbles->count; ) {
  334. PARFLT* ci_xyr = xyr + ci * 3;
  335. ci_xyr[2] = radii[ci];
  336. PARFLT* cm_xyr = xyr + cm * 3;
  337. PARFLT* cn_xyr = xyr + cn * 3;
  338. par_bubbles_touch_two_disks(ci_xyr, cn_xyr, cm_xyr);
  339. // Check for a collision. In the paper, "Cj" is the intersecting node.
  340. PARINT cj_f;
  341. PARINT nfsteps = par_bubbles__collide(bubbles, ci, cn, &cj_f, +1);
  342. if (!nfsteps) {
  343. chain[cm].next = ci;
  344. chain[ci].prev = cm;
  345. chain[ci].next = cn;
  346. chain[cn].prev = ci;
  347. cm = ci++;
  348. continue;
  349. }
  350. // Search backwards for a collision, in case it is closer.
  351. PARINT cj_b;
  352. PARINT nbsteps = par_bubbles__collide(bubbles, ci, cm, &cj_b, -1);
  353. // Intersection occurred after Cn.
  354. if (nfsteps <= nbsteps) {
  355. cn = cj_f;
  356. chain[cm].next = cn;
  357. chain[cn].prev = cm;
  358. continue;
  359. }
  360. // Intersection occurred before Cm.
  361. cm = cj_b;
  362. chain[cm].next = cn;
  363. chain[cn].prev = cm;
  364. }
  365. bubbles->npacked = bubbles->count;
  366. }
  367. static void par__disk_from_two_points(PARFLT const* xy1, PARFLT const* xy2,
  368. PARFLT* result)
  369. {
  370. PARFLT dx = xy1[0] - xy2[0];
  371. PARFLT dy = xy1[1] - xy2[1];
  372. result[0] = 0.5 * (xy1[0] + xy2[0]);
  373. result[1] = 0.5 * (xy1[1] + xy2[1]);
  374. result[2] = sqrt(dx * dx + dy * dy) / 2.0;
  375. }
  376. static PARINT par__disk_contains_point(PARFLT const* xyr, PARFLT const* xy)
  377. {
  378. PARFLT dx = xyr[0] - xy[0];
  379. PARFLT dy = xyr[1] - xy[1];
  380. return dx * dx + dy * dy <= PAR_SQR(xyr[2]);
  381. }
  382. static void par__easydisk_from_points(PARFLT* disk, PARFLT const* edgepts,
  383. PARINT nedgepts)
  384. {
  385. if (nedgepts == 0) {
  386. disk[0] = 0;
  387. disk[1] = 0;
  388. disk[2] = 0;
  389. return;
  390. }
  391. if (nedgepts == 1) {
  392. disk[0] = edgepts[0];
  393. disk[1] = edgepts[1];
  394. disk[2] = 0;
  395. return;
  396. }
  397. par__disk_from_two_points(edgepts, edgepts + 2, disk);
  398. if (nedgepts == 2 || par__disk_contains_point(disk, edgepts + 4)) {
  399. return;
  400. }
  401. par__disk_from_two_points(edgepts, edgepts + 4, disk);
  402. if (par__disk_contains_point(disk, edgepts + 2)) {
  403. return;
  404. }
  405. par__disk_from_two_points(edgepts + 2, edgepts + 4, disk);
  406. if (par__disk_contains_point(disk, edgepts)) {
  407. return;
  408. }
  409. par_bubbles_touch_three_points(edgepts, disk);
  410. }
  411. static void par__minidisk_points(PARFLT* disk, PARFLT const* pts, PARINT npts,
  412. PARFLT const* edgepts, PARINT nedgepts)
  413. {
  414. if (npts == 0 || nedgepts == 3) {
  415. par__easydisk_from_points(disk, edgepts, nedgepts);
  416. return;
  417. }
  418. PARFLT const* pt = pts + (--npts) * 2;
  419. par__minidisk_points(disk, pts, npts, edgepts, nedgepts);
  420. if (!par__disk_contains_point(disk, pt)) {
  421. PARFLT edgepts1[6];
  422. for (PARINT i = 0; i < nedgepts * 2; i += 2) {
  423. edgepts1[i] = edgepts[i];
  424. edgepts1[i + 1] = edgepts[i + 1];
  425. }
  426. edgepts1[2 * nedgepts] = pt[0];
  427. edgepts1[2 * nedgepts + 1] = pt[1];
  428. par__minidisk_points(disk, pts, npts, edgepts1, ++nedgepts);
  429. }
  430. }
  431. // Returns true if the specified circle1 contains the specified circle2.
  432. static bool par__disk_contains_disk(PARFLT const* xyr1, PARFLT const* xyr2)
  433. {
  434. PARFLT xc0 = xyr1[0] - xyr2[0];
  435. PARFLT yc0 = xyr1[1] - xyr2[1];
  436. return sqrt(xc0 * xc0 + yc0 * yc0) < xyr1[2] - xyr2[2] + 1e-6;
  437. }
  438. // Returns the smallest circle that intersects the two specified circles.
  439. static void par__disk_from_two_disks(PARFLT const* xyr1, PARFLT const* xyr2,
  440. PARFLT* result)
  441. {
  442. PARFLT x1 = xyr1[0], y1 = xyr1[1], r1 = xyr1[2];
  443. PARFLT x2 = xyr2[0], y2 = xyr2[1], r2 = xyr2[2];
  444. PARFLT x12 = x2 - x1, y12 = y2 - y1, r12 = r2 - r1;
  445. PARFLT l = sqrt(x12 * x12 + y12 * y12);
  446. result[0] = (x1 + x2 + x12 / l * r12) / 2;
  447. result[1] = (y1 + y2 + y12 / l * r12) / 2;
  448. result[2] = (l + r1 + r2) / 2;
  449. }
  450. static void par__easydisk_from_disks(PARFLT* disk, PARFLT const* edgedisks,
  451. PARINT nedgedisks)
  452. {
  453. assert(nedgedisks <= 3);
  454. if (nedgedisks == 0) {
  455. disk[0] = 0;
  456. disk[1] = 0;
  457. disk[2] = 0;
  458. return;
  459. }
  460. if (nedgedisks == 1) {
  461. disk[0] = edgedisks[0];
  462. disk[1] = edgedisks[1];
  463. disk[2] = edgedisks[2];
  464. return;
  465. }
  466. if (nedgedisks == 2) {
  467. par__disk_from_two_disks(edgedisks, edgedisks + 3, disk);
  468. return;
  469. }
  470. par_bubbles_touch_three_disks(edgedisks, edgedisks + 3, edgedisks + 6,
  471. disk);
  472. }
  473. static void par__minidisk_disks(PARFLT* result, PARFLT const* disks,
  474. PARINT ndisks, PARFLT const* edgedisks, PARINT nedgedisks)
  475. {
  476. if (ndisks == 0 || nedgedisks == 3) {
  477. par__easydisk_from_disks(result, edgedisks, nedgedisks);
  478. return;
  479. }
  480. PARFLT const* disk = disks + (--ndisks) * 3;
  481. par__minidisk_disks(result, disks, ndisks, edgedisks, nedgedisks);
  482. if (!par__disk_contains_disk(result, disk)) {
  483. PARFLT edgedisks1[9];
  484. for (PARINT i = 0; i < nedgedisks * 3; i += 3) {
  485. edgedisks1[i] = edgedisks[i];
  486. edgedisks1[i + 1] = edgedisks[i + 1];
  487. edgedisks1[i + 2] = edgedisks[i + 2];
  488. }
  489. edgedisks1[3 * nedgedisks] = disk[0];
  490. edgedisks1[3 * nedgedisks + 1] = disk[1];
  491. edgedisks1[3 * nedgedisks + 2] = disk[2];
  492. par__minidisk_disks(result, disks, ndisks, edgedisks1, ++nedgedisks);
  493. }
  494. }
  495. static void par_bubbles__copy_disk(par_bubbles__t const* src,
  496. par_bubbles__t* dst, PARINT parent)
  497. {
  498. PARINT i = dst->count++;
  499. if (dst->capacity < dst->count) {
  500. dst->capacity = PAR_MAX(16, dst->capacity) * 2;
  501. dst->xyr = PAR_REALLOC(PARFLT, dst->xyr, 3 * dst->capacity);
  502. dst->ids = PAR_REALLOC(PARINT, dst->ids, dst->capacity);
  503. }
  504. PARFLT const* xyr = src->xyr + parent * 3;
  505. dst->xyr[i * 3] = xyr[0];
  506. dst->xyr[i * 3 + 1] = xyr[1];
  507. dst->xyr[i * 3 + 2] = xyr[2];
  508. dst->ids[i] = parent;
  509. }
  510. void par_bubbles_enclose_points(PARFLT const* xy, PARINT npts, PARFLT* result)
  511. {
  512. par__minidisk_points(result, xy, npts, 0, 0);
  513. }
  514. void par_bubbles_enclose_disks(PARFLT const* xyr, PARINT ndisks, PARFLT* result)
  515. {
  516. par__minidisk_disks(result, xyr, ndisks, 0, 0);
  517. }
  518. void par_bubbles_touch_three_points(PARFLT const* xy, PARFLT* xyr)
  519. {
  520. // Many thanks to Stephen Schmitts:
  521. // http://www.abecedarical.com/zenosamples/zs_circle3pts.html
  522. PARFLT p1x = xy[0], p1y = xy[1];
  523. PARFLT p2x = xy[2], p2y = xy[3];
  524. PARFLT p3x = xy[4], p3y = xy[5];
  525. PARFLT a = p2x - p1x, b = p2y - p1y;
  526. PARFLT c = p3x - p1x, d = p3y - p1y;
  527. PARFLT e = a * (p2x + p1x) * 0.5 + b * (p2y + p1y) * 0.5;
  528. PARFLT f = c * (p3x + p1x) * 0.5 + d * (p3y + p1y) * 0.5;
  529. PARFLT det = a*d - b*c;
  530. PARFLT cx = xyr[0] = (d*e - b*f) / det;
  531. PARFLT cy = xyr[1] = (-c*e + a*f) / det;
  532. xyr[2] = sqrt((p1x - cx)*(p1x - cx) + (p1y - cy)*(p1y - cy));
  533. }
  534. void par_bubbles_touch_two_disks(PARFLT* c, PARFLT const* a, PARFLT const* b)
  535. {
  536. PARFLT db = a[2] + c[2], dx = b[0] - a[0], dy = b[1] - a[1];
  537. if (db && (dx || dy)) {
  538. PARFLT da = b[2] + c[2], dc = dx * dx + dy * dy;
  539. da *= da;
  540. db *= db;
  541. PARFLT x = 0.5 + (db - da) / (2 * dc);
  542. PARFLT db1 = db - dc;
  543. PARFLT y0 = PAR_MAX(0, 2 * da * (db + dc) - db1 * db1 - da * da);
  544. PARFLT y = sqrt(y0) / (2 * dc);
  545. c[0] = a[0] + x * dx + y * dy;
  546. c[1] = a[1] + x * dy - y * dx;
  547. } else {
  548. c[0] = a[0] + db;
  549. c[1] = a[1];
  550. }
  551. }
  552. void par_bubbles_touch_three_disks(PARFLT const* xyr1, PARFLT const* xyr2,
  553. PARFLT const* xyr3, PARFLT* result)
  554. {
  555. PARFLT x1 = xyr1[0], y1 = xyr1[1], r1 = xyr1[2],
  556. x2 = xyr2[0], y2 = xyr2[1], r2 = xyr2[2],
  557. x3 = xyr3[0], y3 = xyr3[1], r3 = xyr3[2],
  558. a2 = 2 * (x1 - x2),
  559. b2 = 2 * (y1 - y2),
  560. c2 = 2 * (r2 - r1),
  561. d2 = x1 * x1 + y1 * y1 - r1 * r1 - x2 * x2 - y2 * y2 + r2 * r2,
  562. a3 = 2 * (x1 - x3),
  563. b3 = 2 * (y1 - y3),
  564. c3 = 2 * (r3 - r1),
  565. d3 = x1 * x1 + y1 * y1 - r1 * r1 - x3 * x3 - y3 * y3 + r3 * r3,
  566. ab = a3 * b2 - a2 * b3,
  567. xa = (b2 * d3 - b3 * d2) / ab - x1,
  568. xb = (b3 * c2 - b2 * c3) / ab,
  569. ya = (a3 * d2 - a2 * d3) / ab - y1,
  570. yb = (a2 * c3 - a3 * c2) / ab,
  571. A = xb * xb + yb * yb - 1,
  572. B = 2 * (xa * xb + ya * yb + r1),
  573. C = xa * xa + ya * ya - r1 * r1,
  574. r = (-B - sqrt(B * B - 4 * A * C)) / (2 * A);
  575. result[0] = xa + xb * r + x1;
  576. result[1] = ya + yb * r + y1;
  577. result[2] = r;
  578. }
  579. void par_bubbles_free_result(par_bubbles_t* pubbub)
  580. {
  581. par_bubbles__t* bubbles = (par_bubbles__t*) pubbub;
  582. PAR_FREE(bubbles->graph_children);
  583. PAR_FREE(bubbles->graph_heads);
  584. PAR_FREE(bubbles->chain);
  585. PAR_FREE(bubbles->xyr);
  586. PAR_FREE(bubbles->ids);
  587. PAR_FREE(bubbles);
  588. }
  589. par_bubbles_t* par_bubbles_pack(PARFLT const* radiuses, PARINT nradiuses)
  590. {
  591. par_bubbles__t* bubbles = PAR_CALLOC(par_bubbles__t, 1);
  592. if (nradiuses > 0) {
  593. bubbles->radiuses = radiuses;
  594. bubbles->count = nradiuses;
  595. bubbles->chain = PAR_MALLOC(par_bubbles__node, nradiuses);
  596. bubbles->xyr = PAR_MALLOC(PARFLT, 3 * nradiuses);
  597. par_bubbles__initflat(bubbles);
  598. par_bubbles__packflat(bubbles);
  599. }
  600. return (par_bubbles_t*) bubbles;
  601. }
  602. // Assigns a radius to every node according to its number of descendants.
  603. void par_bubbles__generate_radii(par_bubbles__t* bubbles,
  604. par_bubbles__t* worker, PARINT parent)
  605. {
  606. PARINT head = bubbles->graph_heads[parent];
  607. PARINT tail = bubbles->graph_tails[parent];
  608. PARINT nchildren = tail - head;
  609. PARINT pr = parent * 3 + 2;
  610. bubbles->xyr[pr] = 1;
  611. if (nchildren == 0) {
  612. return;
  613. }
  614. for (PARINT cindex = head; cindex != tail; cindex++) {
  615. PARINT child = bubbles->graph_children[cindex];
  616. par_bubbles__generate_radii(bubbles, worker, child);
  617. bubbles->xyr[pr] += bubbles->xyr[child * 3 + 2];
  618. }
  619. // The following square root seems to produce a nicer, more space-filling,
  620. // distribution of radiuses in randomly-generated trees.
  621. bubbles->xyr[pr] = sqrtf(bubbles->xyr[pr]);
  622. }
  623. void par_bubbles__hpack(par_bubbles__t* bubbles, par_bubbles__t* worker,
  624. PARINT parent, bool local)
  625. {
  626. PARINT head = bubbles->graph_heads[parent];
  627. PARINT tail = bubbles->graph_tails[parent];
  628. PARINT nchildren = tail - head;
  629. if (nchildren == 0) {
  630. return;
  631. }
  632. // Cast away const because we're using the worker as a cache to avoid
  633. // a kazillion malloc / free calls.
  634. PARFLT* radiuses = (PARFLT*) worker->radiuses;
  635. // We perform flat layout twice: once without padding (to determine scale)
  636. // and then again with scaled padding.
  637. PARFLT enclosure[3];
  638. PARFLT px = bubbles->xyr[parent * 3 + 0];
  639. PARFLT py = bubbles->xyr[parent * 3 + 1];
  640. PARFLT pr = bubbles->xyr[parent * 3 + 2];
  641. const PARFLT PAR_HPACK_PADDING1 = 0.15;
  642. const PARFLT PAR_HPACK_PADDING2 = 0.025;
  643. PARFLT scaled_padding = 0.0;
  644. while (1) {
  645. worker->npacked = 0;
  646. worker->count = nchildren;
  647. PARINT c = 0;
  648. for (PARINT cindex = head; cindex != tail; cindex++) {
  649. PARINT child = bubbles->graph_children[cindex];
  650. radiuses[c++] = bubbles->xyr[child * 3 + 2] + scaled_padding;
  651. }
  652. par_bubbles__initflat(worker);
  653. par_bubbles__packflat(worker);
  654. // Using Welzl's algorithm instead of a simple AABB enclosure is
  655. // slightly slower and doesn't yield much aesthetic improvement.
  656. #if PAR_BUBBLES_HPACK_WELZL
  657. par_bubbles_enclose_disks(worker->xyr, nchildren, enclosure);
  658. #else
  659. PARFLT aabb[6];
  660. par_bubbles_compute_aabb((par_bubbles_t const*) worker, aabb);
  661. enclosure[0] = 0.5 * (aabb[0] + aabb[2]);
  662. enclosure[1] = 0.5 * (aabb[1] + aabb[3]);
  663. enclosure[2] = 0;
  664. for (PARINT c = 0; c < nchildren; c++) {
  665. PARFLT x = worker->xyr[c * 3 + 0] - enclosure[0];
  666. PARFLT y = worker->xyr[c * 3 + 1] - enclosure[1];
  667. PARFLT r = worker->xyr[c * 3 + 2];
  668. enclosure[2] = PAR_MAX(enclosure[2], r + sqrtf(x * x + y * y));
  669. }
  670. #endif
  671. if (scaled_padding || !PAR_HPACK_PADDING1) {
  672. break;
  673. } else {
  674. scaled_padding = PAR_HPACK_PADDING1 / enclosure[2];
  675. }
  676. }
  677. PARFLT cx = enclosure[0], cy = enclosure[1], cr = enclosure[2];
  678. scaled_padding *= cr;
  679. cr += PAR_HPACK_PADDING2 * cr;
  680. // Transform the children to fit nicely into either (a) the unit circle,
  681. // or (b) their parent. The former is used if "local" is true.
  682. PARFLT scale, tx, ty;
  683. if (local) {
  684. scale = 1.0 / cr;
  685. tx = 0;
  686. ty = 0;
  687. } else {
  688. scale = pr / cr;
  689. tx = px;
  690. ty = py;
  691. }
  692. PARFLT const* src = worker->xyr;
  693. for (PARINT cindex = head; cindex != tail; cindex++, src += 3) {
  694. PARFLT* dst = bubbles->xyr + 3 * bubbles->graph_children[cindex];
  695. dst[0] = tx + scale * (src[0] - cx);
  696. dst[1] = ty + scale * (src[1] - cy);
  697. dst[2] = scale * (src[2] - scaled_padding);
  698. }
  699. // Recursion. TODO: It might be better to use our own stack here.
  700. for (PARINT cindex = head; cindex != tail; cindex++) {
  701. par_bubbles__hpack(bubbles, worker, bubbles->graph_children[cindex],
  702. local);
  703. }
  704. }
  705. par_bubbles_t* par_bubbles_hpack_circle(PARINT* nodes, PARINT nnodes,
  706. PARFLT radius)
  707. {
  708. par_bubbles__t* bubbles = PAR_CALLOC(par_bubbles__t, 1);
  709. if (nnodes > 0) {
  710. bubbles->graph_parents = nodes;
  711. bubbles->count = nnodes;
  712. bubbles->chain = PAR_MALLOC(par_bubbles__node, nnodes);
  713. bubbles->xyr = PAR_MALLOC(PARFLT, 3 * nnodes);
  714. par_bubbles__initgraph(bubbles);
  715. par_bubbles__t* worker = PAR_CALLOC(par_bubbles__t, 1);
  716. worker->radiuses = PAR_MALLOC(PARFLT, bubbles->maxwidth);
  717. worker->chain = PAR_MALLOC(par_bubbles__node, bubbles->maxwidth);
  718. worker->xyr = PAR_MALLOC(PARFLT, 3 * bubbles->maxwidth);
  719. par_bubbles__generate_radii(bubbles, worker, 0);
  720. bubbles->xyr[0] = 0;
  721. bubbles->xyr[1] = 0;
  722. bubbles->xyr[2] = radius;
  723. par_bubbles__hpack(bubbles, worker, 0, false);
  724. par_bubbles_free_result((par_bubbles_t*) worker);
  725. }
  726. return (par_bubbles_t*) bubbles;
  727. }
  728. // TODO: use a stack instead of recursion
  729. static PARINT par_bubbles__pick(par_bubbles__t const* bubbles, PARINT parent,
  730. PARFLT x, PARFLT y)
  731. {
  732. PARFLT const* xyr = bubbles->xyr + parent * 3;
  733. PARFLT d2 = PAR_SQR(x - xyr[0]) + PAR_SQR(y - xyr[1]);
  734. if (d2 > PAR_SQR(xyr[2])) {
  735. return -1;
  736. }
  737. PARINT head = bubbles->graph_heads[parent];
  738. PARINT tail = bubbles->graph_tails[parent];
  739. for (PARINT cindex = head; cindex != tail; cindex++) {
  740. PARINT child = bubbles->graph_children[cindex];
  741. PARINT result = par_bubbles__pick(bubbles, child, x, y);
  742. if (result > -1) {
  743. return result;
  744. }
  745. }
  746. return parent;
  747. }
  748. PARINT par_bubbles_pick(par_bubbles_t const* cbubbles, PARFLT x, PARFLT y)
  749. {
  750. par_bubbles__t const* bubbles = (par_bubbles__t const*) cbubbles;
  751. if (bubbles->count == 0) {
  752. return -1;
  753. }
  754. return par_bubbles__pick(bubbles, 0, x, y);
  755. }
  756. void par_bubbles_compute_aabb(par_bubbles_t const* bubbles, PARFLT* aabb)
  757. {
  758. if (bubbles->count == 0) {
  759. return;
  760. }
  761. PARFLT const* xyr = bubbles->xyr;
  762. aabb[0] = aabb[2] = xyr[0];
  763. aabb[1] = aabb[3] = xyr[1];
  764. for (PARINT i = 0; i < bubbles->count; i++, xyr += 3) {
  765. aabb[0] = PAR_MIN(xyr[0] - xyr[2], aabb[0]);
  766. aabb[1] = PAR_MIN(xyr[1] - xyr[2], aabb[1]);
  767. aabb[2] = PAR_MAX(xyr[0] + xyr[2], aabb[2]);
  768. aabb[3] = PAR_MAX(xyr[1] + xyr[2], aabb[3]);
  769. }
  770. }
  771. bool par_bubbles_check_aabb(PARFLT const* disk, PARFLT const* aabb)
  772. {
  773. PARFLT cx = PAR_CLAMP(disk[0], aabb[0], aabb[2]);
  774. PARFLT cy = PAR_CLAMP(disk[1], aabb[1], aabb[3]);
  775. PARFLT dx = disk[0] - cx;
  776. PARFLT dy = disk[1] - cy;
  777. PARFLT d2 = dx * dx + dy * dy;
  778. return d2 < (disk[2] * disk[2]);
  779. }
  780. static void par_bubbles__cull(par_bubbles__t const* src, PARFLT const* aabb,
  781. PARFLT minradius, par_bubbles__t* dst, PARINT parent)
  782. {
  783. PARFLT const* xyr = src->xyr + parent * 3;
  784. if (xyr[2] < minradius || !par_bubbles_check_aabb(xyr, aabb)) {
  785. return;
  786. }
  787. par_bubbles__copy_disk(src, dst, parent);
  788. PARINT head = src->graph_heads[parent];
  789. PARINT tail = src->graph_tails[parent];
  790. for (PARINT cindex = head; cindex != tail; cindex++) {
  791. PARINT child = src->graph_children[cindex];
  792. par_bubbles__cull(src, aabb, minradius, dst, child);
  793. }
  794. }
  795. par_bubbles_t* par_bubbles_cull(par_bubbles_t const* psrc,
  796. PARFLT const* aabb, PARFLT minradius, par_bubbles_t* pdst)
  797. {
  798. par_bubbles__t const* src = (par_bubbles__t const*) psrc;
  799. par_bubbles__t* dst = (par_bubbles__t*) pdst;
  800. if (!dst) {
  801. dst = PAR_CALLOC(par_bubbles__t, 1);
  802. pdst = (par_bubbles_t*) dst;
  803. } else {
  804. dst->count = 0;
  805. }
  806. if (src->count == 0) {
  807. return pdst;
  808. }
  809. par_bubbles__cull(src, aabb, minradius, dst, 0);
  810. return pdst;
  811. }
  812. void par_bubbles_export(par_bubbles_t const* bubbles, char const* filename)
  813. {
  814. PARFLT aabb[4];
  815. par_bubbles_compute_aabb(bubbles, aabb);
  816. PARFLT maxextent = PAR_MAX(aabb[2] - aabb[0], aabb[3] - aabb[1]);
  817. PARFLT padding = 0.05 * maxextent;
  818. FILE* svgfile = fopen(filename, "wt");
  819. fprintf(svgfile,
  820. "<svg viewBox='%f %f %f %f' width='640px' height='640px' "
  821. "version='1.1' "
  822. "xmlns='http://www.w3.org/2000/svg'>\n"
  823. "<g stroke-width='0.5' stroke-opacity='0.5' stroke='black' "
  824. "fill-opacity='0.2' fill='#2A8BB6'>\n"
  825. "<rect fill-opacity='0.1' stroke='none' fill='#2A8BB6' x='%f' y='%f' "
  826. "width='100%%' height='100%%'/>\n",
  827. aabb[0] - padding, aabb[1] - padding,
  828. aabb[2] - aabb[0] + 2 * padding, aabb[3] - aabb[1] + 2 * padding,
  829. aabb[0] - padding, aabb[1] - padding);
  830. PARFLT const* xyr = bubbles->xyr;
  831. for (PARINT i = 0; i < bubbles->count; i++, xyr += 3) {
  832. fprintf(svgfile, "<circle stroke-width='%f' cx='%f' cy='%f' r='%f'/>\n",
  833. xyr[2] * 0.01, xyr[0], xyr[1], xyr[2]);
  834. fprintf(svgfile, "<text text-anchor='middle' stroke='none' "
  835. "x='%f' y='%f' font-size='%f'>%d</text>\n",
  836. xyr[0], xyr[1] + xyr[2] * 0.125, xyr[2] * 0.5, (int) i);
  837. }
  838. fputs("</g>\n</svg>", svgfile);
  839. fclose(svgfile);
  840. }
  841. void par_bubbles_get_children(par_bubbles_t const* pbubbles, PARINT node,
  842. PARINT** pchildren, PARINT* nchildren)
  843. {
  844. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  845. *pchildren = bubbles->graph_children + bubbles->graph_heads[node];
  846. *nchildren = bubbles->graph_tails[node] - bubbles->graph_heads[node];
  847. }
  848. PARINT par_bubbles_get_parent(par_bubbles_t const* pbubbles, PARINT node)
  849. {
  850. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  851. return bubbles->graph_parents[node];
  852. }
  853. void par_bubbles__get_maxdepth(par_bubbles__t const* bubbles, PARINT* maxdepth,
  854. PARINT* leaf, PARINT parent, PARINT depth)
  855. {
  856. if (depth > *maxdepth) {
  857. *leaf = parent;
  858. *maxdepth = depth;
  859. }
  860. PARINT* children;
  861. PARINT nchildren;
  862. par_bubbles_t const* pbubbles = (par_bubbles_t const*) bubbles;
  863. par_bubbles_get_children(pbubbles, parent, &children, &nchildren);
  864. for (PARINT c = 0; c < nchildren; c++) {
  865. par_bubbles__get_maxdepth(bubbles, maxdepth, leaf, children[c],
  866. depth + 1);
  867. }
  868. }
  869. void par_bubbles_get_maxdepth(par_bubbles_t const* pbubbles, PARINT* maxdepth,
  870. PARINT* leaf)
  871. {
  872. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  873. *maxdepth = -1;
  874. *leaf = -1;
  875. return par_bubbles__get_maxdepth(bubbles, maxdepth, leaf, 0, 0);
  876. }
  877. PARINT par_bubbles_get_depth(par_bubbles_t const* pbubbles, PARINT node)
  878. {
  879. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  880. PARINT const* parents = bubbles->graph_parents;
  881. PARINT depth = 0;
  882. while (node) {
  883. node = parents[node];
  884. depth++;
  885. }
  886. return depth;
  887. }
  888. void par_bubbles_compute_aabb_for_node(par_bubbles_t const* bubbles,
  889. PAR_BUBBLES_INT node, PAR_BUBBLES_FLT* aabb)
  890. {
  891. PARFLT const* xyr = bubbles->xyr + 3 * node;
  892. aabb[0] = aabb[2] = xyr[0];
  893. aabb[1] = aabb[3] = xyr[1];
  894. aabb[0] = PAR_MIN(xyr[0] - xyr[2], aabb[0]);
  895. aabb[1] = PAR_MIN(xyr[1] - xyr[2], aabb[1]);
  896. aabb[2] = PAR_MAX(xyr[0] + xyr[2], aabb[2]);
  897. aabb[3] = PAR_MAX(xyr[1] + xyr[2], aabb[3]);
  898. }
  899. PARINT par_bubbles_lowest_common_ancestor(par_bubbles_t const* bubbles,
  900. PARINT node_a, PARINT node_b)
  901. {
  902. if (node_a == node_b) {
  903. return node_a;
  904. }
  905. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  906. PARINT depth_a = par_bubbles_get_depth(bubbles, node_a);
  907. PARINT* chain_a = PAR_MALLOC(PARINT, depth_a);
  908. for (PARINT i = depth_a - 1; i >= 0; i--) {
  909. chain_a[i] = node_a;
  910. node_a = src->graph_parents[node_a];
  911. }
  912. PARINT depth_b = par_bubbles_get_depth(bubbles, node_b);
  913. PARINT* chain_b = PAR_MALLOC(PARINT, depth_b);
  914. for (PARINT i = depth_b - 1; i >= 0; i--) {
  915. chain_b[i] = node_b;
  916. node_b = src->graph_parents[node_b];
  917. }
  918. PARINT lca = 0;
  919. for (PARINT i = 1; i < PAR_MIN(depth_a, depth_b); i++) {
  920. if (chain_a[i] != chain_b[i]) {
  921. break;
  922. }
  923. lca = chain_a[i];
  924. }
  925. PAR_FREE(chain_a);
  926. PAR_FREE(chain_b);
  927. return lca;
  928. }
  929. void par_bubbles_export_local(par_bubbles_t const* bubbles,
  930. PAR_BUBBLES_INT root, char const* filename)
  931. {
  932. par_bubbles_t* clone = par_bubbles_cull_local(bubbles, 0, 0, root, 0);
  933. FILE* svgfile = fopen(filename, "wt");
  934. fprintf(svgfile,
  935. "<svg viewBox='%f %f %f %f' width='640px' height='640px' "
  936. "version='1.1' "
  937. "xmlns='http://www.w3.org/2000/svg'>\n"
  938. "<g stroke-width='0.5' stroke-opacity='0.5' stroke='black' "
  939. "fill-opacity='0.2' fill='#2A8BB6'>\n"
  940. "<rect fill-opacity='0.1' stroke='none' fill='#2AB68B' x='%f' y='%f' "
  941. "width='100%%' height='100%%'/>\n",
  942. -1.0, -1.0, 2.0, 2.0, -1.0, -1.0);
  943. PARFLT const* xyr = clone->xyr;
  944. for (PARINT i = 0; i < clone->count; i++, xyr += 3) {
  945. fprintf(svgfile, "<circle stroke-width='%f' cx='%f' cy='%f' r='%f'/>\n",
  946. xyr[2] * 0.01, xyr[0], xyr[1], xyr[2]);
  947. }
  948. fputs("</g>\n</svg>", svgfile);
  949. fclose(svgfile);
  950. par_bubbles_free_result(clone);
  951. }
  952. void par_bubbles_set_filter(par_bubbles_t* bubbles, par_bubbles_filter f)
  953. {
  954. par_bubbles__t* src = (par_bubbles__t*) bubbles;
  955. src->filter = f;
  956. }
  957. static void par_bubbles__copy_disk_local(par_bubbles__t const* src,
  958. par_bubbles__t* dst, PARINT parent, PARFLT const* xform)
  959. {
  960. PARINT i = dst->count++;
  961. if (dst->capacity < dst->count) {
  962. dst->capacity = PAR_MAX(16, dst->capacity) * 2;
  963. dst->xyr = PAR_REALLOC(PARFLT, dst->xyr, 3 * dst->capacity);
  964. dst->ids = PAR_REALLOC(PARINT, dst->ids, dst->capacity);
  965. }
  966. PARFLT const* xyr = src->xyr + parent * 3;
  967. dst->xyr[i * 3] = xyr[0] * xform[2] + xform[0];
  968. dst->xyr[i * 3 + 1] = xyr[1] * xform[2] + xform[1];
  969. dst->xyr[i * 3 + 2] = xyr[2] * xform[2];
  970. dst->ids[i] = parent;
  971. }
  972. static void par_bubbles__cull_local(par_bubbles__t const* src,
  973. PARFLT const* aabb, PARFLT const* xform, PARFLT minradius,
  974. par_bubbles__t* dst, PARINT parent)
  975. {
  976. PARFLT const* xyr = src->xyr + parent * 3;
  977. PARFLT child_xform[3] = {
  978. xform[0] + xform[2] * xyr[0],
  979. xform[1] + xform[2] * xyr[1],
  980. xform[2] * xyr[2]
  981. };
  982. if (aabb && !par_bubbles_check_aabb(child_xform, aabb)) {
  983. return;
  984. }
  985. if (child_xform[2] < minradius) {
  986. return;
  987. }
  988. par_bubbles__copy_disk_local(src, dst, parent, xform);
  989. xform = child_xform;
  990. PARINT head = src->graph_heads[parent];
  991. PARINT tail = src->graph_tails[parent];
  992. if (src->filter == PAR_BUBBLES_FILTER_DISCARD_LAST_CHILD) {
  993. tail--;
  994. } else if (src->filter == PAR_BUBBLES_FILTER_KEEP_ONLY_LAST_CHILD) {
  995. head = PAR_MAX(head, tail - 1);
  996. }
  997. for (PARINT cindex = head; cindex < tail; cindex++) {
  998. PARINT child = src->graph_children[cindex];
  999. par_bubbles__cull_local(src, aabb, xform, minradius, dst, child);
  1000. }
  1001. }
  1002. par_bubbles_t* par_bubbles_cull_local(par_bubbles_t const* psrc,
  1003. PAR_BUBBLES_FLT const* aabb, PAR_BUBBLES_FLT minradius,
  1004. PAR_BUBBLES_INT root, par_bubbles_t* pdst)
  1005. {
  1006. par_bubbles__t const* src = (par_bubbles__t const*) psrc;
  1007. par_bubbles__t* dst = (par_bubbles__t*) pdst;
  1008. if (!dst) {
  1009. dst = PAR_CALLOC(par_bubbles__t, 1);
  1010. pdst = (par_bubbles_t*) dst;
  1011. } else {
  1012. dst->count = 0;
  1013. }
  1014. if (src->count == 0) {
  1015. return pdst;
  1016. }
  1017. PARFLT xform[3] = {0, 0, 1};
  1018. par_bubbles__copy_disk_local(src, dst, root, xform);
  1019. dst->xyr[0] = dst->xyr[1] = 0;
  1020. dst->xyr[2] = 1;
  1021. PARINT head = src->graph_heads[root];
  1022. PARINT tail = src->graph_tails[root];
  1023. if (src->filter == PAR_BUBBLES_FILTER_DISCARD_LAST_CHILD) {
  1024. tail--;
  1025. } else if (src->filter == PAR_BUBBLES_FILTER_KEEP_ONLY_LAST_CHILD) {
  1026. head = PAR_MAX(head, tail - 1);
  1027. }
  1028. for (PARINT cindex = head; cindex < tail; cindex++) {
  1029. PARINT child = src->graph_children[cindex];
  1030. par_bubbles__cull_local(src, aabb, xform, minradius, dst, child);
  1031. }
  1032. return pdst;
  1033. }
  1034. par_bubbles_t* par_bubbles_hpack_local(PARINT* nodes, PARINT nnodes)
  1035. {
  1036. par_bubbles__t* bubbles = PAR_CALLOC(par_bubbles__t, 1);
  1037. if (nnodes > 0) {
  1038. bubbles->graph_parents = nodes;
  1039. bubbles->count = nnodes;
  1040. bubbles->chain = PAR_MALLOC(par_bubbles__node, nnodes);
  1041. bubbles->xyr = PAR_MALLOC(PARFLT, 3 * nnodes);
  1042. par_bubbles__initgraph(bubbles);
  1043. par_bubbles__t* worker = PAR_CALLOC(par_bubbles__t, 1);
  1044. worker->radiuses = PAR_MALLOC(PARFLT, bubbles->maxwidth);
  1045. worker->chain = PAR_MALLOC(par_bubbles__node, bubbles->maxwidth);
  1046. worker->xyr = PAR_MALLOC(PARFLT, 3 * bubbles->maxwidth);
  1047. par_bubbles__generate_radii(bubbles, worker, 0);
  1048. bubbles->xyr[0] = 0;
  1049. bubbles->xyr[1] = 0;
  1050. bubbles->xyr[2] = 1;
  1051. par_bubbles__hpack(bubbles, worker, 0, true);
  1052. par_bubbles_free_result((par_bubbles_t*) worker);
  1053. }
  1054. return (par_bubbles_t*) bubbles;
  1055. }
  1056. static bool par_bubbles__disk_encloses_aabb(PAR_BUBBLES_FLT cx,
  1057. PAR_BUBBLES_FLT cy, PAR_BUBBLES_FLT r, PAR_BUBBLES_FLT const* aabb)
  1058. {
  1059. PAR_BUBBLES_FLT x, y;
  1060. PAR_BUBBLES_FLT r2 = r * r;
  1061. x = aabb[0]; y = aabb[1];
  1062. if (PAR_SQR(x - cx) + PAR_SQR(y - cy) > r2) {
  1063. return false;
  1064. }
  1065. x = aabb[2]; y = aabb[1];
  1066. if (PAR_SQR(x - cx) + PAR_SQR(y - cy) > r2) {
  1067. return false;
  1068. }
  1069. x = aabb[0]; y = aabb[3];
  1070. if (PAR_SQR(x - cx) + PAR_SQR(y - cy) > r2) {
  1071. return false;
  1072. }
  1073. x = aabb[2]; y = aabb[3];
  1074. return PAR_SQR(x - cx) + PAR_SQR(y - cy) <= r2;
  1075. }
  1076. static bool par_bubbles__get_local(par_bubbles__t const* src, PARFLT* xform,
  1077. PARINT parent, PARINT node);
  1078. static bool par_bubbles_transform_parent(par_bubbles__t const* src,
  1079. PARFLT* xform, PARINT node0)
  1080. {
  1081. PARINT node1 = src->graph_parents[node0];
  1082. xform[0] = 0;
  1083. xform[1] = 0;
  1084. xform[2] = 1;
  1085. PARINT head = src->graph_heads[node1];
  1086. PARINT tail = src->graph_tails[node1];
  1087. for (PARINT cindex = head; cindex != tail; cindex++) {
  1088. PARINT child = src->graph_children[cindex];
  1089. if (par_bubbles__get_local(src, xform, child, node0)) {
  1090. return true;
  1091. }
  1092. }
  1093. return false;
  1094. }
  1095. PARINT par_bubbles__find_local(par_bubbles__t const* src,
  1096. PARFLT const* xform, PARFLT const* aabb, PARINT parent)
  1097. {
  1098. PARFLT const* xyr = src->xyr + parent * 3;
  1099. PARFLT child_xform[3] = {
  1100. xform[2] * xyr[0] + xform[0],
  1101. xform[2] * xyr[1] + xform[1],
  1102. xform[2] * xyr[2]
  1103. };
  1104. xform = child_xform;
  1105. if (!par_bubbles__disk_encloses_aabb(xform[0], xform[1], xform[2], aabb)) {
  1106. return -1;
  1107. }
  1108. PARFLT maxrad = 0;
  1109. PARINT head = src->graph_heads[parent];
  1110. PARINT tail = src->graph_tails[parent];
  1111. for (PARINT cindex = head; cindex != tail; cindex++) {
  1112. PARINT child = src->graph_children[cindex];
  1113. PARFLT const* xyr = src->xyr + child * 3;
  1114. maxrad = PAR_MAX(maxrad, xyr[2]);
  1115. }
  1116. PARFLT maxext = PAR_MAX(aabb[2] - aabb[0], aabb[3] - aabb[1]);
  1117. if (2 * maxrad < maxext) {
  1118. return parent;
  1119. }
  1120. for (PARINT cindex = head; cindex != tail; cindex++) {
  1121. PARINT child = src->graph_children[cindex];
  1122. PARINT cresult = par_bubbles__find_local(src, xform, aabb, child);
  1123. if (cresult > -1) {
  1124. return cresult;
  1125. }
  1126. }
  1127. return parent;
  1128. }
  1129. // This finds the deepest node that completely encloses the box.
  1130. PARINT par_bubbles_find_local(par_bubbles_t const* bubbles, PARFLT const* aabb,
  1131. PARINT root)
  1132. {
  1133. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  1134. // Since the aabb is expressed in the coordinate system of the given root,
  1135. // we can do a trivial rejection right away, using the unit circle.
  1136. if (!par_bubbles__disk_encloses_aabb(0, 0, 1, aabb)) {
  1137. if (root == 0) {
  1138. return -1;
  1139. }
  1140. PARFLT xform[3];
  1141. par_bubbles_transform_parent(src, xform, root);
  1142. PARFLT width = aabb[2] - aabb[0];
  1143. PARFLT height = aabb[3] - aabb[1];
  1144. PARFLT cx = 0.5 * (aabb[0] + aabb[2]);
  1145. PARFLT cy = 0.5 * (aabb[1] + aabb[3]);
  1146. width *= xform[2];
  1147. height *= xform[2];
  1148. cx = cx * xform[2] + xform[0];
  1149. cy = cy * xform[2] + xform[1];
  1150. PARFLT new_aabb[4] = {
  1151. cx - width * 0.5,
  1152. cy - height * 0.5,
  1153. cx + width * 0.5,
  1154. cy + height * 0.5
  1155. };
  1156. PARINT parent = src->graph_parents[root];
  1157. return par_bubbles_find_local(bubbles, new_aabb, parent);
  1158. }
  1159. PARFLT maxrad = 0;
  1160. PARINT head = src->graph_heads[root];
  1161. PARINT tail = src->graph_tails[root];
  1162. for (PARINT cindex = head; cindex != tail; cindex++) {
  1163. PARINT child = src->graph_children[cindex];
  1164. PARFLT const* xyr = src->xyr + child * 3;
  1165. maxrad = PAR_MAX(maxrad, xyr[2]);
  1166. }
  1167. PARFLT maxext = PAR_MAX(aabb[2] - aabb[0], aabb[3] - aabb[1]);
  1168. if (2 * maxrad < maxext) {
  1169. return root;
  1170. }
  1171. PARFLT xform[3] = {0, 0, 1};
  1172. for (PARINT cindex = head; cindex != tail; cindex++) {
  1173. PARINT child = src->graph_children[cindex];
  1174. PARINT cresult = par_bubbles__find_local(src, xform, aabb, child);
  1175. if (cresult > -1) {
  1176. return cresult;
  1177. }
  1178. }
  1179. return root;
  1180. }
  1181. // This could be implemented much more efficiently, but for now it simply
  1182. // calls find_local with a zero-size AABB, then ensures that the result
  1183. // has a radius that is greater than or equal to minradius.
  1184. PARINT par_bubbles_pick_local(par_bubbles_t const* bubbles, PARFLT x, PARFLT y,
  1185. PARINT root, PARFLT minradius)
  1186. {
  1187. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  1188. PARFLT aabb[] = { x, y, x, y };
  1189. PARINT result = par_bubbles_find_local(bubbles, aabb, root);
  1190. if (result == -1) {
  1191. return result;
  1192. }
  1193. PARINT depth = par_bubbles_get_depth(bubbles, result);
  1194. PARINT* chain = PAR_MALLOC(PARINT, depth);
  1195. PARINT node = result;
  1196. for (PARINT i = depth - 1; i >= 0; i--) {
  1197. chain[i] = node;
  1198. node = src->graph_parents[node];
  1199. }
  1200. PARFLT radius = 1;
  1201. for (PARINT i = 1; i < depth; i++) {
  1202. PARINT node = chain[i];
  1203. radius *= src->xyr[node * 3 + 2];
  1204. if (radius < minradius) {
  1205. result = chain[i - 1];
  1206. break;
  1207. }
  1208. }
  1209. PAR_FREE(chain);
  1210. return result;
  1211. }
  1212. static bool par_bubbles__get_local(par_bubbles__t const* src, PARFLT* xform,
  1213. PARINT parent, PARINT node)
  1214. {
  1215. PARFLT const* xyr = src->xyr + parent * 3;
  1216. PARFLT child_xform[3] = {
  1217. xform[2] * xyr[0] + xform[0],
  1218. xform[2] * xyr[1] + xform[1],
  1219. xform[2] * xyr[2]
  1220. };
  1221. if (parent == node) {
  1222. xform[0] = child_xform[0];
  1223. xform[1] = child_xform[1];
  1224. xform[2] = child_xform[2];
  1225. return true;
  1226. }
  1227. PARINT head = src->graph_heads[parent];
  1228. PARINT tail = src->graph_tails[parent];
  1229. for (PARINT cindex = head; cindex != tail; cindex++) {
  1230. PARINT child = src->graph_children[cindex];
  1231. if (par_bubbles__get_local(src, child_xform, child, node)) {
  1232. xform[0] = child_xform[0];
  1233. xform[1] = child_xform[1];
  1234. xform[2] = child_xform[2];
  1235. return true;
  1236. }
  1237. }
  1238. return false;
  1239. }
  1240. // Obtains the scale and translation (which should be applied in that order)
  1241. // that can move a point from the node0 coord system to the node1 coord system.
  1242. // The "xform" argument should point to three floats, which will be populated
  1243. // with: x translation, y translation, and scale.
  1244. bool par_bubbles_transform_local(par_bubbles_t const* bubbles, PARFLT* xform,
  1245. PARINT node0, PARINT node1)
  1246. {
  1247. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  1248. xform[0] = 0;
  1249. xform[1] = 0;
  1250. xform[2] = 1;
  1251. if (node0 == node1) {
  1252. return true;
  1253. }
  1254. if (node1 == src->graph_parents[node0]) {
  1255. return par_bubbles_transform_parent(src, xform, node0);
  1256. }
  1257. // First try the case where node1 is a descendant of node0
  1258. PARINT head = src->graph_heads[node0];
  1259. PARINT tail = src->graph_tails[node0];
  1260. for (PARINT cindex = head; cindex != tail; cindex++) {
  1261. PARINT child = src->graph_children[cindex];
  1262. if (par_bubbles__get_local(src, xform, child, node1)) {
  1263. float tx = xform[0];
  1264. float ty = xform[1];
  1265. float s = xform[2];
  1266. xform[0] = -tx / s;
  1267. xform[1] = -ty / s;
  1268. xform[2] = 1.0 / s;
  1269. return true;
  1270. }
  1271. }
  1272. // Next, try the case where node0 is a descendant of node1
  1273. head = src->graph_heads[node1];
  1274. tail = src->graph_tails[node1];
  1275. for (PARINT cindex = head; cindex != tail; cindex++) {
  1276. PARINT child = src->graph_children[cindex];
  1277. if (par_bubbles__get_local(src, xform, child, node0)) {
  1278. return true;
  1279. }
  1280. }
  1281. // If we reach here, then node0 is neither an ancestor nor a descendant, so
  1282. // do something hacky and return false. It would be best to find the lowest
  1283. // common ancestor, but let's just assume the lowest common ancestor is 0.
  1284. PARFLT xform2[3] = {0, 0, 1};
  1285. par_bubbles_transform_local(bubbles, xform, node0, 0);
  1286. par_bubbles_transform_local(bubbles, xform2, 0, node1);
  1287. xform[0] *= xform2[2];
  1288. xform[1] *= xform2[2];
  1289. xform[2] *= xform2[2];
  1290. xform[0] += xform2[0];
  1291. xform[1] += xform2[1];
  1292. return false;
  1293. }
  1294. void par_bubbles_set_orientation(par_bubbles_orientation ostate)
  1295. {
  1296. par_bubbles__ostate = ostate;
  1297. }
  1298. #undef PARINT
  1299. #undef PARFLT
  1300. #endif // PAR_BUBBLES_IMPLEMENTATION
  1301. #endif // PAR_BUBBLES_H