diagram-js_lib_layout_LayoutUtil.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. import {
  2. pointDistance,
  3. pointsOnLine
  4. } from "./chunk-XX4N3DI4.js";
  5. import {
  6. isObject,
  7. sortBy
  8. } from "./chunk-4AK4GF4H.js";
  9. import {
  10. __commonJS,
  11. __toESM
  12. } from "./chunk-2LSFTFF7.js";
  13. // node_modules/.pnpm/path-intersection@2.2.1/node_modules/path-intersection/intersect.js
  14. var require_intersect = __commonJS({
  15. "node_modules/.pnpm/path-intersection@2.2.1/node_modules/path-intersection/intersect.js"(exports, module) {
  16. "use strict";
  17. var p2s = /,?([a-z]),?/gi;
  18. var toFloat = parseFloat;
  19. var math = Math;
  20. var PI = math.PI;
  21. var mmin = math.min;
  22. var mmax = math.max;
  23. var pow = math.pow;
  24. var abs = math.abs;
  25. var pathCommand = /([a-z])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?[\s]*,?[\s]*)+)/ig;
  26. var pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)[\s]*,?[\s]*/ig;
  27. var isArray = Array.isArray || function(o) {
  28. return o instanceof Array;
  29. };
  30. function hasProperty(obj, property) {
  31. return Object.prototype.hasOwnProperty.call(obj, property);
  32. }
  33. function clone(obj) {
  34. if (typeof obj == "function" || Object(obj) !== obj) {
  35. return obj;
  36. }
  37. var res = new obj.constructor();
  38. for (var key in obj) {
  39. if (hasProperty(obj, key)) {
  40. res[key] = clone(obj[key]);
  41. }
  42. }
  43. return res;
  44. }
  45. function repush(array, item) {
  46. for (var i = 0, ii = array.length; i < ii; i++)
  47. if (array[i] === item) {
  48. return array.push(array.splice(i, 1)[0]);
  49. }
  50. }
  51. function cacher(f) {
  52. function newf() {
  53. var arg = Array.prototype.slice.call(arguments, 0), args = arg.join("␀"), cache = newf.cache = newf.cache || {}, count = newf.count = newf.count || [];
  54. if (hasProperty(cache, args)) {
  55. repush(count, args);
  56. return cache[args];
  57. }
  58. count.length >= 1e3 && delete cache[count.shift()];
  59. count.push(args);
  60. cache[args] = f.apply(0, arg);
  61. return cache[args];
  62. }
  63. return newf;
  64. }
  65. function parsePathString(pathString) {
  66. if (!pathString) {
  67. return null;
  68. }
  69. var pth = paths(pathString);
  70. if (pth.arr) {
  71. return clone(pth.arr);
  72. }
  73. var paramCounts = { a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0 }, data = [];
  74. if (isArray(pathString) && isArray(pathString[0])) {
  75. data = clone(pathString);
  76. }
  77. if (!data.length) {
  78. String(pathString).replace(pathCommand, function(a, b, c) {
  79. var params = [], name = b.toLowerCase();
  80. c.replace(pathValues, function(a2, b2) {
  81. b2 && params.push(+b2);
  82. });
  83. if (name == "m" && params.length > 2) {
  84. data.push([b].concat(params.splice(0, 2)));
  85. name = "l";
  86. b = b == "m" ? "l" : "L";
  87. }
  88. while (params.length >= paramCounts[name]) {
  89. data.push([b].concat(params.splice(0, paramCounts[name])));
  90. if (!paramCounts[name]) {
  91. break;
  92. }
  93. }
  94. });
  95. }
  96. data.toString = paths.toString;
  97. pth.arr = clone(data);
  98. return data;
  99. }
  100. function paths(ps) {
  101. var p = paths.ps = paths.ps || {};
  102. if (p[ps]) {
  103. p[ps].sleep = 100;
  104. } else {
  105. p[ps] = {
  106. sleep: 100
  107. };
  108. }
  109. setTimeout(function() {
  110. for (var key in p) {
  111. if (hasProperty(p, key) && key != ps) {
  112. p[key].sleep--;
  113. !p[key].sleep && delete p[key];
  114. }
  115. }
  116. });
  117. return p[ps];
  118. }
  119. function rectBBox(x, y, width, height) {
  120. if (arguments.length === 1) {
  121. y = x.y;
  122. width = x.width;
  123. height = x.height;
  124. x = x.x;
  125. }
  126. return {
  127. x,
  128. y,
  129. width,
  130. height,
  131. x2: x + width,
  132. y2: y + height
  133. };
  134. }
  135. function pathToString() {
  136. return this.join(",").replace(p2s, "$1");
  137. }
  138. function pathClone(pathArray) {
  139. var res = clone(pathArray);
  140. res.toString = pathToString;
  141. return res;
  142. }
  143. function findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
  144. var t1 = 1 - t, t13 = pow(t1, 3), t12 = pow(t1, 2), t2 = t * t, t3 = t2 * t, x = t13 * p1x + t12 * 3 * t * c1x + t1 * 3 * t * t * c2x + t3 * p2x, y = t13 * p1y + t12 * 3 * t * c1y + t1 * 3 * t * t * c2y + t3 * p2y;
  145. return {
  146. x: fixError(x),
  147. y: fixError(y)
  148. };
  149. }
  150. function bezierBBox(points) {
  151. var bbox = curveBBox.apply(null, points);
  152. return rectBBox(
  153. bbox.x0,
  154. bbox.y0,
  155. bbox.x1 - bbox.x0,
  156. bbox.y1 - bbox.y0
  157. );
  158. }
  159. function isPointInsideBBox(bbox, x, y) {
  160. return x >= bbox.x && x <= bbox.x + bbox.width && y >= bbox.y && y <= bbox.y + bbox.height;
  161. }
  162. function isBBoxIntersect(bbox1, bbox2) {
  163. bbox1 = rectBBox(bbox1);
  164. bbox2 = rectBBox(bbox2);
  165. return isPointInsideBBox(bbox2, bbox1.x, bbox1.y) || isPointInsideBBox(bbox2, bbox1.x2, bbox1.y) || isPointInsideBBox(bbox2, bbox1.x, bbox1.y2) || isPointInsideBBox(bbox2, bbox1.x2, bbox1.y2) || isPointInsideBBox(bbox1, bbox2.x, bbox2.y) || isPointInsideBBox(bbox1, bbox2.x2, bbox2.y) || isPointInsideBBox(bbox1, bbox2.x, bbox2.y2) || isPointInsideBBox(bbox1, bbox2.x2, bbox2.y2) || (bbox1.x < bbox2.x2 && bbox1.x > bbox2.x || bbox2.x < bbox1.x2 && bbox2.x > bbox1.x) && (bbox1.y < bbox2.y2 && bbox1.y > bbox2.y || bbox2.y < bbox1.y2 && bbox2.y > bbox1.y);
  166. }
  167. function base3(t, p1, p2, p3, p4) {
  168. var t1 = -3 * p1 + 9 * p2 - 9 * p3 + 3 * p4, t2 = t * t1 + 6 * p1 - 12 * p2 + 6 * p3;
  169. return t * t2 - 3 * p1 + 3 * p2;
  170. }
  171. function bezlen(x1, y1, x2, y2, x3, y3, x4, y4, z) {
  172. if (z == null) {
  173. z = 1;
  174. }
  175. z = z > 1 ? 1 : z < 0 ? 0 : z;
  176. var z2 = z / 2, n = 12, Tvalues = [-0.1252, 0.1252, -0.3678, 0.3678, -0.5873, 0.5873, -0.7699, 0.7699, -0.9041, 0.9041, -0.9816, 0.9816], Cvalues = [0.2491, 0.2491, 0.2335, 0.2335, 0.2032, 0.2032, 0.1601, 0.1601, 0.1069, 0.1069, 0.0472, 0.0472], sum = 0;
  177. for (var i = 0; i < n; i++) {
  178. var ct = z2 * Tvalues[i] + z2, xbase = base3(ct, x1, x2, x3, x4), ybase = base3(ct, y1, y2, y3, y4), comb = xbase * xbase + ybase * ybase;
  179. sum += Cvalues[i] * math.sqrt(comb);
  180. }
  181. return z2 * sum;
  182. }
  183. function intersectLines(x1, y1, x2, y2, x3, y3, x4, y4) {
  184. if (mmax(x1, x2) < mmin(x3, x4) || mmin(x1, x2) > mmax(x3, x4) || mmax(y1, y2) < mmin(y3, y4) || mmin(y1, y2) > mmax(y3, y4)) {
  185. return;
  186. }
  187. var nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4), ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4), denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  188. if (!denominator) {
  189. return;
  190. }
  191. var px = fixError(nx / denominator), py = fixError(ny / denominator), px2 = +px.toFixed(2), py2 = +py.toFixed(2);
  192. if (px2 < +mmin(x1, x2).toFixed(2) || px2 > +mmax(x1, x2).toFixed(2) || px2 < +mmin(x3, x4).toFixed(2) || px2 > +mmax(x3, x4).toFixed(2) || py2 < +mmin(y1, y2).toFixed(2) || py2 > +mmax(y1, y2).toFixed(2) || py2 < +mmin(y3, y4).toFixed(2) || py2 > +mmax(y3, y4).toFixed(2)) {
  193. return;
  194. }
  195. return { x: px, y: py };
  196. }
  197. function fixError(number) {
  198. return Math.round(number * 1e11) / 1e11;
  199. }
  200. function findBezierIntersections(bez1, bez2, justCount) {
  201. var bbox1 = bezierBBox(bez1), bbox2 = bezierBBox(bez2);
  202. if (!isBBoxIntersect(bbox1, bbox2)) {
  203. return justCount ? 0 : [];
  204. }
  205. var l1 = bezlen.apply(0, bez1), l2 = bezlen.apply(0, bez2), n1 = isLine(bez1) ? 1 : ~~(l1 / 5) || 1, n2 = isLine(bez2) ? 1 : ~~(l2 / 5) || 1, dots1 = [], dots2 = [], xy = {}, res = justCount ? 0 : [];
  206. for (var i = 0; i < n1 + 1; i++) {
  207. var p = findDotsAtSegment.apply(0, bez1.concat(i / n1));
  208. dots1.push({ x: p.x, y: p.y, t: i / n1 });
  209. }
  210. for (i = 0; i < n2 + 1; i++) {
  211. p = findDotsAtSegment.apply(0, bez2.concat(i / n2));
  212. dots2.push({ x: p.x, y: p.y, t: i / n2 });
  213. }
  214. for (i = 0; i < n1; i++) {
  215. for (var j = 0; j < n2; j++) {
  216. var di = dots1[i], di1 = dots1[i + 1], dj = dots2[j], dj1 = dots2[j + 1], ci = abs(di1.x - di.x) < 0.01 ? "y" : "x", cj = abs(dj1.x - dj.x) < 0.01 ? "y" : "x", is = intersectLines(di.x, di.y, di1.x, di1.y, dj.x, dj.y, dj1.x, dj1.y), key;
  217. if (is) {
  218. key = is.x.toFixed(9) + "#" + is.y.toFixed(9);
  219. if (xy[key]) {
  220. continue;
  221. }
  222. xy[key] = true;
  223. var t1 = di.t + abs((is[ci] - di[ci]) / (di1[ci] - di[ci])) * (di1.t - di.t), t2 = dj.t + abs((is[cj] - dj[cj]) / (dj1[cj] - dj[cj])) * (dj1.t - dj.t);
  224. if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) {
  225. if (justCount) {
  226. res++;
  227. } else {
  228. res.push({
  229. x: is.x,
  230. y: is.y,
  231. t1,
  232. t2
  233. });
  234. }
  235. }
  236. }
  237. }
  238. }
  239. return res;
  240. }
  241. function findPathIntersections(path1, path2, justCount) {
  242. path1 = pathToCurve(path1);
  243. path2 = pathToCurve(path2);
  244. var x1, y1, x2, y2, x1m, y1m, x2m, y2m, bez1, bez2, res = justCount ? 0 : [];
  245. for (var i = 0, ii = path1.length; i < ii; i++) {
  246. var pi = path1[i];
  247. if (pi[0] == "M") {
  248. x1 = x1m = pi[1];
  249. y1 = y1m = pi[2];
  250. } else {
  251. if (pi[0] == "C") {
  252. bez1 = [x1, y1].concat(pi.slice(1));
  253. x1 = bez1[6];
  254. y1 = bez1[7];
  255. } else {
  256. bez1 = [x1, y1, x1, y1, x1m, y1m, x1m, y1m];
  257. x1 = x1m;
  258. y1 = y1m;
  259. }
  260. for (var j = 0, jj = path2.length; j < jj; j++) {
  261. var pj = path2[j];
  262. if (pj[0] == "M") {
  263. x2 = x2m = pj[1];
  264. y2 = y2m = pj[2];
  265. } else {
  266. if (pj[0] == "C") {
  267. bez2 = [x2, y2].concat(pj.slice(1));
  268. x2 = bez2[6];
  269. y2 = bez2[7];
  270. } else {
  271. bez2 = [x2, y2, x2, y2, x2m, y2m, x2m, y2m];
  272. x2 = x2m;
  273. y2 = y2m;
  274. }
  275. var intr = findBezierIntersections(bez1, bez2, justCount);
  276. if (justCount) {
  277. res += intr;
  278. } else {
  279. for (var k = 0, kk = intr.length; k < kk; k++) {
  280. intr[k].segment1 = i;
  281. intr[k].segment2 = j;
  282. intr[k].bez1 = bez1;
  283. intr[k].bez2 = bez2;
  284. }
  285. res = res.concat(intr);
  286. }
  287. }
  288. }
  289. }
  290. }
  291. return res;
  292. }
  293. function pathToAbsolute(pathArray) {
  294. var pth = paths(pathArray);
  295. if (pth.abs) {
  296. return pathClone(pth.abs);
  297. }
  298. if (!isArray(pathArray) || !isArray(pathArray && pathArray[0])) {
  299. pathArray = parsePathString(pathArray);
  300. }
  301. if (!pathArray || !pathArray.length) {
  302. return [["M", 0, 0]];
  303. }
  304. var res = [], x = 0, y = 0, mx = 0, my = 0, start = 0, pa0;
  305. if (pathArray[0][0] == "M") {
  306. x = +pathArray[0][1];
  307. y = +pathArray[0][2];
  308. mx = x;
  309. my = y;
  310. start++;
  311. res[0] = ["M", x, y];
  312. }
  313. for (var r, pa, i = start, ii = pathArray.length; i < ii; i++) {
  314. res.push(r = []);
  315. pa = pathArray[i];
  316. pa0 = pa[0];
  317. if (pa0 != pa0.toUpperCase()) {
  318. r[0] = pa0.toUpperCase();
  319. switch (r[0]) {
  320. case "A":
  321. r[1] = pa[1];
  322. r[2] = pa[2];
  323. r[3] = pa[3];
  324. r[4] = pa[4];
  325. r[5] = pa[5];
  326. r[6] = +pa[6] + x;
  327. r[7] = +pa[7] + y;
  328. break;
  329. case "V":
  330. r[1] = +pa[1] + y;
  331. break;
  332. case "H":
  333. r[1] = +pa[1] + x;
  334. break;
  335. case "M":
  336. mx = +pa[1] + x;
  337. my = +pa[2] + y;
  338. default:
  339. for (var j = 1, jj = pa.length; j < jj; j++) {
  340. r[j] = +pa[j] + (j % 2 ? x : y);
  341. }
  342. }
  343. } else {
  344. for (var k = 0, kk = pa.length; k < kk; k++) {
  345. r[k] = pa[k];
  346. }
  347. }
  348. pa0 = pa0.toUpperCase();
  349. switch (r[0]) {
  350. case "Z":
  351. x = +mx;
  352. y = +my;
  353. break;
  354. case "H":
  355. x = r[1];
  356. break;
  357. case "V":
  358. y = r[1];
  359. break;
  360. case "M":
  361. mx = r[r.length - 2];
  362. my = r[r.length - 1];
  363. default:
  364. x = r[r.length - 2];
  365. y = r[r.length - 1];
  366. }
  367. }
  368. res.toString = pathToString;
  369. pth.abs = pathClone(res);
  370. return res;
  371. }
  372. function isLine(bez) {
  373. return bez[0] === bez[2] && bez[1] === bez[3] && bez[4] === bez[6] && bez[5] === bez[7];
  374. }
  375. function lineToCurve(x1, y1, x2, y2) {
  376. return [
  377. x1,
  378. y1,
  379. x2,
  380. y2,
  381. x2,
  382. y2
  383. ];
  384. }
  385. function qubicToCurve(x1, y1, ax, ay, x2, y2) {
  386. var _13 = 1 / 3, _23 = 2 / 3;
  387. return [
  388. _13 * x1 + _23 * ax,
  389. _13 * y1 + _23 * ay,
  390. _13 * x2 + _23 * ax,
  391. _13 * y2 + _23 * ay,
  392. x2,
  393. y2
  394. ];
  395. }
  396. function arcToCurve(x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
  397. var _120 = PI * 120 / 180, rad = PI / 180 * (+angle || 0), res = [], xy, rotate = cacher(function(x3, y3, rad2) {
  398. var X = x3 * math.cos(rad2) - y3 * math.sin(rad2), Y = x3 * math.sin(rad2) + y3 * math.cos(rad2);
  399. return { x: X, y: Y };
  400. });
  401. if (!recursive) {
  402. xy = rotate(x1, y1, -rad);
  403. x1 = xy.x;
  404. y1 = xy.y;
  405. xy = rotate(x2, y2, -rad);
  406. x2 = xy.x;
  407. y2 = xy.y;
  408. var x = (x1 - x2) / 2, y = (y1 - y2) / 2;
  409. var h = x * x / (rx * rx) + y * y / (ry * ry);
  410. if (h > 1) {
  411. h = math.sqrt(h);
  412. rx = h * rx;
  413. ry = h * ry;
  414. }
  415. var rx2 = rx * rx, ry2 = ry * ry, k = (large_arc_flag == sweep_flag ? -1 : 1) * math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))), cx = k * rx * y / ry + (x1 + x2) / 2, cy = k * -ry * x / rx + (y1 + y2) / 2, f1 = math.asin(((y1 - cy) / ry).toFixed(9)), f2 = math.asin(((y2 - cy) / ry).toFixed(9));
  416. f1 = x1 < cx ? PI - f1 : f1;
  417. f2 = x2 < cx ? PI - f2 : f2;
  418. f1 < 0 && (f1 = PI * 2 + f1);
  419. f2 < 0 && (f2 = PI * 2 + f2);
  420. if (sweep_flag && f1 > f2) {
  421. f1 = f1 - PI * 2;
  422. }
  423. if (!sweep_flag && f2 > f1) {
  424. f2 = f2 - PI * 2;
  425. }
  426. } else {
  427. f1 = recursive[0];
  428. f2 = recursive[1];
  429. cx = recursive[2];
  430. cy = recursive[3];
  431. }
  432. var df = f2 - f1;
  433. if (abs(df) > _120) {
  434. var f2old = f2, x2old = x2, y2old = y2;
  435. f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
  436. x2 = cx + rx * math.cos(f2);
  437. y2 = cy + ry * math.sin(f2);
  438. res = arcToCurve(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
  439. }
  440. df = f2 - f1;
  441. var c1 = math.cos(f1), s1 = math.sin(f1), c2 = math.cos(f2), s2 = math.sin(f2), t = math.tan(df / 4), hx = 4 / 3 * rx * t, hy = 4 / 3 * ry * t, m1 = [x1, y1], m2 = [x1 + hx * s1, y1 - hy * c1], m3 = [x2 + hx * s2, y2 - hy * c2], m4 = [x2, y2];
  442. m2[0] = 2 * m1[0] - m2[0];
  443. m2[1] = 2 * m1[1] - m2[1];
  444. if (recursive) {
  445. return [m2, m3, m4].concat(res);
  446. } else {
  447. res = [m2, m3, m4].concat(res).join().split(",");
  448. var newres = [];
  449. for (var i = 0, ii = res.length; i < ii; i++) {
  450. newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
  451. }
  452. return newres;
  453. }
  454. }
  455. function curveBBox(x0, y0, x1, y1, x2, y2, x3, y3) {
  456. var tvalues = [], bounds = [[], []], a, b, c, t, t1, t2, b2ac, sqrtb2ac;
  457. for (var i = 0; i < 2; ++i) {
  458. if (i == 0) {
  459. b = 6 * x0 - 12 * x1 + 6 * x2;
  460. a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
  461. c = 3 * x1 - 3 * x0;
  462. } else {
  463. b = 6 * y0 - 12 * y1 + 6 * y2;
  464. a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
  465. c = 3 * y1 - 3 * y0;
  466. }
  467. if (abs(a) < 1e-12) {
  468. if (abs(b) < 1e-12) {
  469. continue;
  470. }
  471. t = -c / b;
  472. if (0 < t && t < 1) {
  473. tvalues.push(t);
  474. }
  475. continue;
  476. }
  477. b2ac = b * b - 4 * c * a;
  478. sqrtb2ac = math.sqrt(b2ac);
  479. if (b2ac < 0) {
  480. continue;
  481. }
  482. t1 = (-b + sqrtb2ac) / (2 * a);
  483. if (0 < t1 && t1 < 1) {
  484. tvalues.push(t1);
  485. }
  486. t2 = (-b - sqrtb2ac) / (2 * a);
  487. if (0 < t2 && t2 < 1) {
  488. tvalues.push(t2);
  489. }
  490. }
  491. var j = tvalues.length, jlen = j, mt;
  492. while (j--) {
  493. t = tvalues[j];
  494. mt = 1 - t;
  495. bounds[0][j] = mt * mt * mt * x0 + 3 * mt * mt * t * x1 + 3 * mt * t * t * x2 + t * t * t * x3;
  496. bounds[1][j] = mt * mt * mt * y0 + 3 * mt * mt * t * y1 + 3 * mt * t * t * y2 + t * t * t * y3;
  497. }
  498. bounds[0][jlen] = x0;
  499. bounds[1][jlen] = y0;
  500. bounds[0][jlen + 1] = x3;
  501. bounds[1][jlen + 1] = y3;
  502. bounds[0].length = bounds[1].length = jlen + 2;
  503. return {
  504. x0: mmin.apply(0, bounds[0]),
  505. y0: mmin.apply(0, bounds[1]),
  506. x1: mmax.apply(0, bounds[0]),
  507. y1: mmax.apply(0, bounds[1])
  508. };
  509. }
  510. function pathToCurve(path) {
  511. var pth = paths(path);
  512. if (pth.curve) {
  513. return pathClone(pth.curve);
  514. }
  515. var curvedPath = pathToAbsolute(path), attrs = { x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null }, processPath = function(path2, d, pathCommand3) {
  516. var nx, ny;
  517. if (!path2) {
  518. return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
  519. }
  520. !(path2[0] in { T: 1, Q: 1 }) && (d.qx = d.qy = null);
  521. switch (path2[0]) {
  522. case "M":
  523. d.X = path2[1];
  524. d.Y = path2[2];
  525. break;
  526. case "A":
  527. path2 = ["C"].concat(arcToCurve.apply(0, [d.x, d.y].concat(path2.slice(1))));
  528. break;
  529. case "S":
  530. if (pathCommand3 == "C" || pathCommand3 == "S") {
  531. nx = d.x * 2 - d.bx;
  532. ny = d.y * 2 - d.by;
  533. } else {
  534. nx = d.x;
  535. ny = d.y;
  536. }
  537. path2 = ["C", nx, ny].concat(path2.slice(1));
  538. break;
  539. case "T":
  540. if (pathCommand3 == "Q" || pathCommand3 == "T") {
  541. d.qx = d.x * 2 - d.qx;
  542. d.qy = d.y * 2 - d.qy;
  543. } else {
  544. d.qx = d.x;
  545. d.qy = d.y;
  546. }
  547. path2 = ["C"].concat(qubicToCurve(d.x, d.y, d.qx, d.qy, path2[1], path2[2]));
  548. break;
  549. case "Q":
  550. d.qx = path2[1];
  551. d.qy = path2[2];
  552. path2 = ["C"].concat(qubicToCurve(d.x, d.y, path2[1], path2[2], path2[3], path2[4]));
  553. break;
  554. case "L":
  555. path2 = ["C"].concat(lineToCurve(d.x, d.y, path2[1], path2[2]));
  556. break;
  557. case "H":
  558. path2 = ["C"].concat(lineToCurve(d.x, d.y, path2[1], d.y));
  559. break;
  560. case "V":
  561. path2 = ["C"].concat(lineToCurve(d.x, d.y, d.x, path2[1]));
  562. break;
  563. case "Z":
  564. path2 = ["C"].concat(lineToCurve(d.x, d.y, d.X, d.Y));
  565. break;
  566. }
  567. return path2;
  568. }, fixArc = function(pp, i2) {
  569. if (pp[i2].length > 7) {
  570. pp[i2].shift();
  571. var pi = pp[i2];
  572. while (pi.length) {
  573. pathCommands[i2] = "A";
  574. pp.splice(i2++, 0, ["C"].concat(pi.splice(0, 6)));
  575. }
  576. pp.splice(i2, 1);
  577. ii = curvedPath.length;
  578. }
  579. }, pathCommands = [], pfirst = "", pathCommand2 = "";
  580. for (var i = 0, ii = curvedPath.length; i < ii; i++) {
  581. curvedPath[i] && (pfirst = curvedPath[i][0]);
  582. if (pfirst != "C") {
  583. pathCommands[i] = pfirst;
  584. i && (pathCommand2 = pathCommands[i - 1]);
  585. }
  586. curvedPath[i] = processPath(curvedPath[i], attrs, pathCommand2);
  587. if (pathCommands[i] != "A" && pfirst == "C")
  588. pathCommands[i] = "C";
  589. fixArc(curvedPath, i);
  590. var seg = curvedPath[i], seglen = seg.length;
  591. attrs.x = seg[seglen - 2];
  592. attrs.y = seg[seglen - 1];
  593. attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
  594. attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
  595. }
  596. pth.curve = pathClone(curvedPath);
  597. return curvedPath;
  598. }
  599. module.exports = findPathIntersections;
  600. }
  601. });
  602. // node_modules/.pnpm/diagram-js@11.9.1/node_modules/diagram-js/lib/layout/LayoutUtil.js
  603. var import_path_intersection = __toESM(require_intersect());
  604. function roundBounds(bounds) {
  605. return {
  606. x: Math.round(bounds.x),
  607. y: Math.round(bounds.y),
  608. width: Math.round(bounds.width),
  609. height: Math.round(bounds.height)
  610. };
  611. }
  612. function roundPoint(point) {
  613. return {
  614. x: Math.round(point.x),
  615. y: Math.round(point.y)
  616. };
  617. }
  618. function asTRBL(bounds) {
  619. return {
  620. top: bounds.y,
  621. right: bounds.x + (bounds.width || 0),
  622. bottom: bounds.y + (bounds.height || 0),
  623. left: bounds.x
  624. };
  625. }
  626. function asBounds(trbl) {
  627. return {
  628. x: trbl.left,
  629. y: trbl.top,
  630. width: trbl.right - trbl.left,
  631. height: trbl.bottom - trbl.top
  632. };
  633. }
  634. function getBoundsMid(bounds) {
  635. return roundPoint({
  636. x: bounds.x + (bounds.width || 0) / 2,
  637. y: bounds.y + (bounds.height || 0) / 2
  638. });
  639. }
  640. function getConnectionMid(connection) {
  641. var waypoints = connection.waypoints;
  642. var parts = waypoints.reduce(function(parts2, point, index) {
  643. var lastPoint = waypoints[index - 1];
  644. if (lastPoint) {
  645. var lastPart = parts2[parts2.length - 1];
  646. var startLength = lastPart && lastPart.endLength || 0;
  647. var length = distance(lastPoint, point);
  648. parts2.push({
  649. start: lastPoint,
  650. end: point,
  651. startLength,
  652. endLength: startLength + length,
  653. length
  654. });
  655. }
  656. return parts2;
  657. }, []);
  658. var totalLength = parts.reduce(function(length, part) {
  659. return length + part.length;
  660. }, 0);
  661. var midLength = totalLength / 2;
  662. var i = 0;
  663. var midSegment = parts[i];
  664. while (midSegment.endLength < midLength) {
  665. midSegment = parts[++i];
  666. }
  667. var segmentProgress = (midLength - midSegment.startLength) / midSegment.length;
  668. var midPoint = {
  669. x: midSegment.start.x + (midSegment.end.x - midSegment.start.x) * segmentProgress,
  670. y: midSegment.start.y + (midSegment.end.y - midSegment.start.y) * segmentProgress
  671. };
  672. return midPoint;
  673. }
  674. function getMid(element) {
  675. if (isConnection(element)) {
  676. return getConnectionMid(element);
  677. }
  678. return getBoundsMid(element);
  679. }
  680. function getOrientation(rect, reference, padding) {
  681. padding = padding || 0;
  682. if (!isObject(padding)) {
  683. padding = { x: padding, y: padding };
  684. }
  685. var rectOrientation = asTRBL(rect), referenceOrientation = asTRBL(reference);
  686. var top = rectOrientation.bottom + padding.y <= referenceOrientation.top, right = rectOrientation.left - padding.x >= referenceOrientation.right, bottom = rectOrientation.top - padding.y >= referenceOrientation.bottom, left = rectOrientation.right + padding.x <= referenceOrientation.left;
  687. var vertical = top ? "top" : bottom ? "bottom" : null, horizontal = left ? "left" : right ? "right" : null;
  688. if (horizontal && vertical) {
  689. return vertical + "-" + horizontal;
  690. } else {
  691. return horizontal || vertical || "intersect";
  692. }
  693. }
  694. function getElementLineIntersection(elementPath, linePath, cropStart) {
  695. var intersections = getIntersections(elementPath, linePath);
  696. if (intersections.length === 1) {
  697. return roundPoint(intersections[0]);
  698. } else if (intersections.length === 2 && pointDistance(intersections[0], intersections[1]) < 1) {
  699. return roundPoint(intersections[0]);
  700. } else if (intersections.length > 1) {
  701. intersections = sortBy(intersections, function(i) {
  702. var distance2 = Math.floor(i.t2 * 100) || 1;
  703. distance2 = 100 - distance2;
  704. distance2 = (distance2 < 10 ? "0" : "") + distance2;
  705. return i.segment2 + "#" + distance2;
  706. });
  707. return roundPoint(intersections[cropStart ? 0 : intersections.length - 1]);
  708. }
  709. return null;
  710. }
  711. function getIntersections(a, b) {
  712. return (0, import_path_intersection.default)(a, b);
  713. }
  714. function filterRedundantWaypoints(waypoints) {
  715. waypoints = waypoints.slice();
  716. var idx = 0, point, previousPoint, nextPoint;
  717. while (waypoints[idx]) {
  718. point = waypoints[idx];
  719. previousPoint = waypoints[idx - 1];
  720. nextPoint = waypoints[idx + 1];
  721. if (pointDistance(point, nextPoint) === 0 || pointsOnLine(previousPoint, nextPoint, point)) {
  722. waypoints.splice(idx, 1);
  723. } else {
  724. idx++;
  725. }
  726. }
  727. return waypoints;
  728. }
  729. function distance(a, b) {
  730. return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
  731. }
  732. function isConnection(element) {
  733. return !!element.waypoints;
  734. }
  735. export {
  736. asBounds,
  737. asTRBL,
  738. filterRedundantWaypoints,
  739. getBoundsMid,
  740. getConnectionMid,
  741. getElementLineIntersection,
  742. getIntersections,
  743. getMid,
  744. getOrientation,
  745. roundBounds,
  746. roundPoint
  747. };
  748. //# sourceMappingURL=diagram-js_lib_layout_LayoutUtil.js.map