par_bubbles.h 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. if (!svgfile) {
  819. fprintf(stderr, "Unable to open %s for writing.\n", filename);
  820. return;
  821. }
  822. fprintf(svgfile,
  823. "<svg viewBox='%f %f %f %f' width='640px' height='640px' "
  824. "version='1.1' "
  825. "xmlns='http://www.w3.org/2000/svg'>\n"
  826. "<g stroke-width='0.5' stroke-opacity='0.5' stroke='black' "
  827. "fill-opacity='0.2' fill='#2A8BB6'>\n"
  828. "<rect fill-opacity='0.1' stroke='none' fill='#2A8BB6' x='%f' y='%f' "
  829. "width='100%%' height='100%%'/>\n",
  830. aabb[0] - padding, aabb[1] - padding,
  831. aabb[2] - aabb[0] + 2 * padding, aabb[3] - aabb[1] + 2 * padding,
  832. aabb[0] - padding, aabb[1] - padding);
  833. PARFLT const* xyr = bubbles->xyr;
  834. for (PARINT i = 0; i < bubbles->count; i++, xyr += 3) {
  835. fprintf(svgfile, "<circle stroke-width='%f' cx='%f' cy='%f' r='%f'/>\n",
  836. xyr[2] * 0.01, xyr[0], xyr[1], xyr[2]);
  837. fprintf(svgfile, "<text text-anchor='middle' stroke='none' "
  838. "x='%f' y='%f' font-size='%f'>%d</text>\n",
  839. xyr[0], xyr[1] + xyr[2] * 0.125, xyr[2] * 0.5, (int) i);
  840. }
  841. fputs("</g>\n</svg>", svgfile);
  842. fclose(svgfile);
  843. }
  844. void par_bubbles_get_children(par_bubbles_t const* pbubbles, PARINT node,
  845. PARINT** pchildren, PARINT* nchildren)
  846. {
  847. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  848. *pchildren = bubbles->graph_children + bubbles->graph_heads[node];
  849. *nchildren = bubbles->graph_tails[node] - bubbles->graph_heads[node];
  850. }
  851. PARINT par_bubbles_get_parent(par_bubbles_t const* pbubbles, PARINT node)
  852. {
  853. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  854. return bubbles->graph_parents[node];
  855. }
  856. void par_bubbles__get_maxdepth(par_bubbles__t const* bubbles, PARINT* maxdepth,
  857. PARINT* leaf, PARINT parent, PARINT depth)
  858. {
  859. if (depth > *maxdepth) {
  860. *leaf = parent;
  861. *maxdepth = depth;
  862. }
  863. PARINT* children;
  864. PARINT nchildren;
  865. par_bubbles_t const* pbubbles = (par_bubbles_t const*) bubbles;
  866. par_bubbles_get_children(pbubbles, parent, &children, &nchildren);
  867. for (PARINT c = 0; c < nchildren; c++) {
  868. par_bubbles__get_maxdepth(bubbles, maxdepth, leaf, children[c],
  869. depth + 1);
  870. }
  871. }
  872. void par_bubbles_get_maxdepth(par_bubbles_t const* pbubbles, PARINT* maxdepth,
  873. PARINT* leaf)
  874. {
  875. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  876. *maxdepth = -1;
  877. *leaf = -1;
  878. return par_bubbles__get_maxdepth(bubbles, maxdepth, leaf, 0, 0);
  879. }
  880. PARINT par_bubbles_get_depth(par_bubbles_t const* pbubbles, PARINT node)
  881. {
  882. par_bubbles__t const* bubbles = (par_bubbles__t const*) pbubbles;
  883. PARINT const* parents = bubbles->graph_parents;
  884. PARINT depth = 0;
  885. while (node) {
  886. node = parents[node];
  887. depth++;
  888. }
  889. return depth;
  890. }
  891. void par_bubbles_compute_aabb_for_node(par_bubbles_t const* bubbles,
  892. PAR_BUBBLES_INT node, PAR_BUBBLES_FLT* aabb)
  893. {
  894. PARFLT const* xyr = bubbles->xyr + 3 * node;
  895. aabb[0] = aabb[2] = xyr[0];
  896. aabb[1] = aabb[3] = xyr[1];
  897. aabb[0] = PAR_MIN(xyr[0] - xyr[2], aabb[0]);
  898. aabb[1] = PAR_MIN(xyr[1] - xyr[2], aabb[1]);
  899. aabb[2] = PAR_MAX(xyr[0] + xyr[2], aabb[2]);
  900. aabb[3] = PAR_MAX(xyr[1] + xyr[2], aabb[3]);
  901. }
  902. PARINT par_bubbles_lowest_common_ancestor(par_bubbles_t const* bubbles,
  903. PARINT node_a, PARINT node_b)
  904. {
  905. if (node_a == node_b) {
  906. return node_a;
  907. }
  908. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  909. PARINT depth_a = par_bubbles_get_depth(bubbles, node_a);
  910. PARINT* chain_a = PAR_MALLOC(PARINT, depth_a);
  911. for (PARINT i = depth_a - 1; i >= 0; i--) {
  912. chain_a[i] = node_a;
  913. node_a = src->graph_parents[node_a];
  914. }
  915. PARINT depth_b = par_bubbles_get_depth(bubbles, node_b);
  916. PARINT* chain_b = PAR_MALLOC(PARINT, depth_b);
  917. for (PARINT i = depth_b - 1; i >= 0; i--) {
  918. chain_b[i] = node_b;
  919. node_b = src->graph_parents[node_b];
  920. }
  921. PARINT lca = 0;
  922. for (PARINT i = 1; i < PAR_MIN(depth_a, depth_b); i++) {
  923. if (chain_a[i] != chain_b[i]) {
  924. break;
  925. }
  926. lca = chain_a[i];
  927. }
  928. PAR_FREE(chain_a);
  929. PAR_FREE(chain_b);
  930. return lca;
  931. }
  932. void par_bubbles_export_local(par_bubbles_t const* bubbles,
  933. PAR_BUBBLES_INT root, char const* filename)
  934. {
  935. par_bubbles_t* clone = par_bubbles_cull_local(bubbles, 0, 0, root, 0);
  936. FILE* svgfile = fopen(filename, "wt");
  937. fprintf(svgfile,
  938. "<svg viewBox='%f %f %f %f' width='640px' height='640px' "
  939. "version='1.1' "
  940. "xmlns='http://www.w3.org/2000/svg'>\n"
  941. "<g stroke-width='0.5' stroke-opacity='0.5' stroke='black' "
  942. "fill-opacity='0.2' fill='#2A8BB6'>\n"
  943. "<rect fill-opacity='0.1' stroke='none' fill='#2AB68B' x='%f' y='%f' "
  944. "width='100%%' height='100%%'/>\n",
  945. -1.0, -1.0, 2.0, 2.0, -1.0, -1.0);
  946. PARFLT const* xyr = clone->xyr;
  947. for (PARINT i = 0; i < clone->count; i++, xyr += 3) {
  948. fprintf(svgfile, "<circle stroke-width='%f' cx='%f' cy='%f' r='%f'/>\n",
  949. xyr[2] * 0.01, xyr[0], xyr[1], xyr[2]);
  950. }
  951. fputs("</g>\n</svg>", svgfile);
  952. fclose(svgfile);
  953. par_bubbles_free_result(clone);
  954. }
  955. void par_bubbles_set_filter(par_bubbles_t* bubbles, par_bubbles_filter f)
  956. {
  957. par_bubbles__t* src = (par_bubbles__t*) bubbles;
  958. src->filter = f;
  959. }
  960. static void par_bubbles__copy_disk_local(par_bubbles__t const* src,
  961. par_bubbles__t* dst, PARINT parent, PARFLT const* xform)
  962. {
  963. PARINT i = dst->count++;
  964. if (dst->capacity < dst->count) {
  965. dst->capacity = PAR_MAX(16, dst->capacity) * 2;
  966. dst->xyr = PAR_REALLOC(PARFLT, dst->xyr, 3 * dst->capacity);
  967. dst->ids = PAR_REALLOC(PARINT, dst->ids, dst->capacity);
  968. }
  969. PARFLT const* xyr = src->xyr + parent * 3;
  970. dst->xyr[i * 3] = xyr[0] * xform[2] + xform[0];
  971. dst->xyr[i * 3 + 1] = xyr[1] * xform[2] + xform[1];
  972. dst->xyr[i * 3 + 2] = xyr[2] * xform[2];
  973. dst->ids[i] = parent;
  974. }
  975. static void par_bubbles__cull_local(par_bubbles__t const* src,
  976. PARFLT const* aabb, PARFLT const* xform, PARFLT minradius,
  977. par_bubbles__t* dst, PARINT parent)
  978. {
  979. PARFLT const* xyr = src->xyr + parent * 3;
  980. PARFLT child_xform[3] = {
  981. xform[0] + xform[2] * xyr[0],
  982. xform[1] + xform[2] * xyr[1],
  983. xform[2] * xyr[2]
  984. };
  985. if (aabb && !par_bubbles_check_aabb(child_xform, aabb)) {
  986. return;
  987. }
  988. if (child_xform[2] < minradius) {
  989. return;
  990. }
  991. par_bubbles__copy_disk_local(src, dst, parent, xform);
  992. xform = child_xform;
  993. PARINT head = src->graph_heads[parent];
  994. PARINT tail = src->graph_tails[parent];
  995. if (src->filter == PAR_BUBBLES_FILTER_DISCARD_LAST_CHILD) {
  996. tail--;
  997. } else if (src->filter == PAR_BUBBLES_FILTER_KEEP_ONLY_LAST_CHILD) {
  998. head = PAR_MAX(head, tail - 1);
  999. }
  1000. for (PARINT cindex = head; cindex < tail; cindex++) {
  1001. PARINT child = src->graph_children[cindex];
  1002. par_bubbles__cull_local(src, aabb, xform, minradius, dst, child);
  1003. }
  1004. }
  1005. par_bubbles_t* par_bubbles_cull_local(par_bubbles_t const* psrc,
  1006. PAR_BUBBLES_FLT const* aabb, PAR_BUBBLES_FLT minradius,
  1007. PAR_BUBBLES_INT root, par_bubbles_t* pdst)
  1008. {
  1009. par_bubbles__t const* src = (par_bubbles__t const*) psrc;
  1010. par_bubbles__t* dst = (par_bubbles__t*) pdst;
  1011. if (!dst) {
  1012. dst = PAR_CALLOC(par_bubbles__t, 1);
  1013. pdst = (par_bubbles_t*) dst;
  1014. } else {
  1015. dst->count = 0;
  1016. }
  1017. if (src->count == 0) {
  1018. return pdst;
  1019. }
  1020. PARFLT xform[3] = {0, 0, 1};
  1021. par_bubbles__copy_disk_local(src, dst, root, xform);
  1022. dst->xyr[0] = dst->xyr[1] = 0;
  1023. dst->xyr[2] = 1;
  1024. PARINT head = src->graph_heads[root];
  1025. PARINT tail = src->graph_tails[root];
  1026. if (src->filter == PAR_BUBBLES_FILTER_DISCARD_LAST_CHILD) {
  1027. tail--;
  1028. } else if (src->filter == PAR_BUBBLES_FILTER_KEEP_ONLY_LAST_CHILD) {
  1029. head = PAR_MAX(head, tail - 1);
  1030. }
  1031. for (PARINT cindex = head; cindex < tail; cindex++) {
  1032. PARINT child = src->graph_children[cindex];
  1033. par_bubbles__cull_local(src, aabb, xform, minradius, dst, child);
  1034. }
  1035. return pdst;
  1036. }
  1037. par_bubbles_t* par_bubbles_hpack_local(PARINT* nodes, PARINT nnodes)
  1038. {
  1039. par_bubbles__t* bubbles = PAR_CALLOC(par_bubbles__t, 1);
  1040. if (nnodes > 0) {
  1041. bubbles->graph_parents = nodes;
  1042. bubbles->count = nnodes;
  1043. bubbles->chain = PAR_MALLOC(par_bubbles__node, nnodes);
  1044. bubbles->xyr = PAR_MALLOC(PARFLT, 3 * nnodes);
  1045. par_bubbles__initgraph(bubbles);
  1046. par_bubbles__t* worker = PAR_CALLOC(par_bubbles__t, 1);
  1047. worker->radiuses = PAR_MALLOC(PARFLT, bubbles->maxwidth);
  1048. worker->chain = PAR_MALLOC(par_bubbles__node, bubbles->maxwidth);
  1049. worker->xyr = PAR_MALLOC(PARFLT, 3 * bubbles->maxwidth);
  1050. par_bubbles__generate_radii(bubbles, worker, 0);
  1051. bubbles->xyr[0] = 0;
  1052. bubbles->xyr[1] = 0;
  1053. bubbles->xyr[2] = 1;
  1054. par_bubbles__hpack(bubbles, worker, 0, true);
  1055. par_bubbles_free_result((par_bubbles_t*) worker);
  1056. }
  1057. return (par_bubbles_t*) bubbles;
  1058. }
  1059. static bool par_bubbles__disk_encloses_aabb(PAR_BUBBLES_FLT cx,
  1060. PAR_BUBBLES_FLT cy, PAR_BUBBLES_FLT r, PAR_BUBBLES_FLT const* aabb)
  1061. {
  1062. PAR_BUBBLES_FLT x, y;
  1063. PAR_BUBBLES_FLT r2 = r * r;
  1064. x = aabb[0]; y = aabb[1];
  1065. if (PAR_SQR(x - cx) + PAR_SQR(y - cy) > r2) {
  1066. return false;
  1067. }
  1068. x = aabb[2]; y = aabb[1];
  1069. if (PAR_SQR(x - cx) + PAR_SQR(y - cy) > r2) {
  1070. return false;
  1071. }
  1072. x = aabb[0]; y = aabb[3];
  1073. if (PAR_SQR(x - cx) + PAR_SQR(y - cy) > r2) {
  1074. return false;
  1075. }
  1076. x = aabb[2]; y = aabb[3];
  1077. return PAR_SQR(x - cx) + PAR_SQR(y - cy) <= r2;
  1078. }
  1079. static bool par_bubbles__get_local(par_bubbles__t const* src, PARFLT* xform,
  1080. PARINT parent, PARINT node);
  1081. static bool par_bubbles_transform_parent(par_bubbles__t const* src,
  1082. PARFLT* xform, PARINT node0)
  1083. {
  1084. PARINT node1 = src->graph_parents[node0];
  1085. xform[0] = 0;
  1086. xform[1] = 0;
  1087. xform[2] = 1;
  1088. PARINT head = src->graph_heads[node1];
  1089. PARINT tail = src->graph_tails[node1];
  1090. for (PARINT cindex = head; cindex != tail; cindex++) {
  1091. PARINT child = src->graph_children[cindex];
  1092. if (par_bubbles__get_local(src, xform, child, node0)) {
  1093. return true;
  1094. }
  1095. }
  1096. return false;
  1097. }
  1098. PARINT par_bubbles__find_local(par_bubbles__t const* src,
  1099. PARFLT const* xform, PARFLT const* aabb, PARINT parent)
  1100. {
  1101. PARFLT const* xyr = src->xyr + parent * 3;
  1102. PARFLT child_xform[3] = {
  1103. xform[2] * xyr[0] + xform[0],
  1104. xform[2] * xyr[1] + xform[1],
  1105. xform[2] * xyr[2]
  1106. };
  1107. xform = child_xform;
  1108. if (!par_bubbles__disk_encloses_aabb(xform[0], xform[1], xform[2], aabb)) {
  1109. return -1;
  1110. }
  1111. PARFLT maxrad = 0;
  1112. PARINT head = src->graph_heads[parent];
  1113. PARINT tail = src->graph_tails[parent];
  1114. for (PARINT cindex = head; cindex != tail; cindex++) {
  1115. PARINT child = src->graph_children[cindex];
  1116. PARFLT const* xyr = src->xyr + child * 3;
  1117. maxrad = PAR_MAX(maxrad, xyr[2]);
  1118. }
  1119. PARFLT maxext = PAR_MAX(aabb[2] - aabb[0], aabb[3] - aabb[1]);
  1120. if (2 * maxrad < maxext) {
  1121. return parent;
  1122. }
  1123. for (PARINT cindex = head; cindex != tail; cindex++) {
  1124. PARINT child = src->graph_children[cindex];
  1125. PARINT cresult = par_bubbles__find_local(src, xform, aabb, child);
  1126. if (cresult > -1) {
  1127. return cresult;
  1128. }
  1129. }
  1130. return parent;
  1131. }
  1132. // This finds the deepest node that completely encloses the box.
  1133. PARINT par_bubbles_find_local(par_bubbles_t const* bubbles, PARFLT const* aabb,
  1134. PARINT root)
  1135. {
  1136. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  1137. // Since the aabb is expressed in the coordinate system of the given root,
  1138. // we can do a trivial rejection right away, using the unit circle.
  1139. if (!par_bubbles__disk_encloses_aabb(0, 0, 1, aabb)) {
  1140. if (root == 0) {
  1141. return -1;
  1142. }
  1143. PARFLT xform[3];
  1144. par_bubbles_transform_parent(src, xform, root);
  1145. PARFLT width = aabb[2] - aabb[0];
  1146. PARFLT height = aabb[3] - aabb[1];
  1147. PARFLT cx = 0.5 * (aabb[0] + aabb[2]);
  1148. PARFLT cy = 0.5 * (aabb[1] + aabb[3]);
  1149. width *= xform[2];
  1150. height *= xform[2];
  1151. cx = cx * xform[2] + xform[0];
  1152. cy = cy * xform[2] + xform[1];
  1153. PARFLT new_aabb[4] = {
  1154. cx - width * 0.5,
  1155. cy - height * 0.5,
  1156. cx + width * 0.5,
  1157. cy + height * 0.5
  1158. };
  1159. PARINT parent = src->graph_parents[root];
  1160. return par_bubbles_find_local(bubbles, new_aabb, parent);
  1161. }
  1162. PARFLT maxrad = 0;
  1163. PARINT head = src->graph_heads[root];
  1164. PARINT tail = src->graph_tails[root];
  1165. for (PARINT cindex = head; cindex != tail; cindex++) {
  1166. PARINT child = src->graph_children[cindex];
  1167. PARFLT const* xyr = src->xyr + child * 3;
  1168. maxrad = PAR_MAX(maxrad, xyr[2]);
  1169. }
  1170. PARFLT maxext = PAR_MAX(aabb[2] - aabb[0], aabb[3] - aabb[1]);
  1171. if (2 * maxrad < maxext) {
  1172. return root;
  1173. }
  1174. PARFLT xform[3] = {0, 0, 1};
  1175. for (PARINT cindex = head; cindex != tail; cindex++) {
  1176. PARINT child = src->graph_children[cindex];
  1177. PARINT cresult = par_bubbles__find_local(src, xform, aabb, child);
  1178. if (cresult > -1) {
  1179. return cresult;
  1180. }
  1181. }
  1182. return root;
  1183. }
  1184. // This could be implemented much more efficiently, but for now it simply
  1185. // calls find_local with a zero-size AABB, then ensures that the result
  1186. // has a radius that is greater than or equal to minradius.
  1187. PARINT par_bubbles_pick_local(par_bubbles_t const* bubbles, PARFLT x, PARFLT y,
  1188. PARINT root, PARFLT minradius)
  1189. {
  1190. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  1191. PARFLT aabb[] = { x, y, x, y };
  1192. PARINT result = par_bubbles_find_local(bubbles, aabb, root);
  1193. if (result == -1) {
  1194. return result;
  1195. }
  1196. PARINT depth = par_bubbles_get_depth(bubbles, result);
  1197. PARINT* chain = PAR_MALLOC(PARINT, depth);
  1198. PARINT node = result;
  1199. for (PARINT i = depth - 1; i >= 0; i--) {
  1200. chain[i] = node;
  1201. node = src->graph_parents[node];
  1202. }
  1203. PARFLT radius = 1;
  1204. for (PARINT i = 1; i < depth; i++) {
  1205. PARINT node = chain[i];
  1206. radius *= src->xyr[node * 3 + 2];
  1207. if (radius < minradius) {
  1208. result = chain[i - 1];
  1209. break;
  1210. }
  1211. }
  1212. PAR_FREE(chain);
  1213. return result;
  1214. }
  1215. static bool par_bubbles__get_local(par_bubbles__t const* src, PARFLT* xform,
  1216. PARINT parent, PARINT node)
  1217. {
  1218. PARFLT const* xyr = src->xyr + parent * 3;
  1219. PARFLT child_xform[3] = {
  1220. xform[2] * xyr[0] + xform[0],
  1221. xform[2] * xyr[1] + xform[1],
  1222. xform[2] * xyr[2]
  1223. };
  1224. if (parent == node) {
  1225. xform[0] = child_xform[0];
  1226. xform[1] = child_xform[1];
  1227. xform[2] = child_xform[2];
  1228. return true;
  1229. }
  1230. PARINT head = src->graph_heads[parent];
  1231. PARINT tail = src->graph_tails[parent];
  1232. for (PARINT cindex = head; cindex != tail; cindex++) {
  1233. PARINT child = src->graph_children[cindex];
  1234. if (par_bubbles__get_local(src, child_xform, child, node)) {
  1235. xform[0] = child_xform[0];
  1236. xform[1] = child_xform[1];
  1237. xform[2] = child_xform[2];
  1238. return true;
  1239. }
  1240. }
  1241. return false;
  1242. }
  1243. // Obtains the scale and translation (which should be applied in that order)
  1244. // that can move a point from the node0 coord system to the node1 coord system.
  1245. // The "xform" argument should point to three floats, which will be populated
  1246. // with: x translation, y translation, and scale.
  1247. bool par_bubbles_transform_local(par_bubbles_t const* bubbles, PARFLT* xform,
  1248. PARINT node0, PARINT node1)
  1249. {
  1250. par_bubbles__t const* src = (par_bubbles__t const*) bubbles;
  1251. xform[0] = 0;
  1252. xform[1] = 0;
  1253. xform[2] = 1;
  1254. if (node0 == node1) {
  1255. return true;
  1256. }
  1257. if (node1 == src->graph_parents[node0]) {
  1258. return par_bubbles_transform_parent(src, xform, node0);
  1259. }
  1260. // First try the case where node1 is a descendant of node0
  1261. PARINT head = src->graph_heads[node0];
  1262. PARINT tail = src->graph_tails[node0];
  1263. for (PARINT cindex = head; cindex != tail; cindex++) {
  1264. PARINT child = src->graph_children[cindex];
  1265. if (par_bubbles__get_local(src, xform, child, node1)) {
  1266. float tx = xform[0];
  1267. float ty = xform[1];
  1268. float s = xform[2];
  1269. xform[0] = -tx / s;
  1270. xform[1] = -ty / s;
  1271. xform[2] = 1.0 / s;
  1272. return true;
  1273. }
  1274. }
  1275. // Next, try the case where node0 is a descendant of node1
  1276. head = src->graph_heads[node1];
  1277. tail = src->graph_tails[node1];
  1278. for (PARINT cindex = head; cindex != tail; cindex++) {
  1279. PARINT child = src->graph_children[cindex];
  1280. if (par_bubbles__get_local(src, xform, child, node0)) {
  1281. return true;
  1282. }
  1283. }
  1284. // If we reach here, then node0 is neither an ancestor nor a descendant, so
  1285. // do something hacky and return false. It would be best to find the lowest
  1286. // common ancestor, but let's just assume the lowest common ancestor is 0.
  1287. PARFLT xform2[3] = {0, 0, 1};
  1288. par_bubbles_transform_local(bubbles, xform, node0, 0);
  1289. par_bubbles_transform_local(bubbles, xform2, 0, node1);
  1290. xform[0] *= xform2[2];
  1291. xform[1] *= xform2[2];
  1292. xform[2] *= xform2[2];
  1293. xform[0] += xform2[0];
  1294. xform[1] += xform2[1];
  1295. return false;
  1296. }
  1297. void par_bubbles_set_orientation(par_bubbles_orientation ostate)
  1298. {
  1299. par_bubbles__ostate = ostate;
  1300. }
  1301. #undef PARINT
  1302. #undef PARFLT
  1303. #endif // PAR_BUBBLES_IMPLEMENTATION
  1304. #endif // PAR_BUBBLES_H
  1305. // par_bubbles is distributed under the MIT license:
  1306. //
  1307. // Copyright (c) 2019 Philip Rideout
  1308. //
  1309. // Permission is hereby granted, free of charge, to any person obtaining a copy
  1310. // of this software and associated documentation files (the "Software"), to deal
  1311. // in the Software without restriction, including without limitation the rights
  1312. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1313. // copies of the Software, and to permit persons to whom the Software is
  1314. // furnished to do so, subject to the following conditions:
  1315. //
  1316. // The above copyright notice and this permission notice shall be included in
  1317. // all copies or substantial portions of the Software.
  1318. //
  1319. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1320. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1321. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1322. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1323. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1324. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1325. // SOFTWARE.