par_bubbles.h 50 KB

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