@page-speed/venn-diagram 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1,26 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var require$$0 = require('react');
4
- var venn = require('@upsetjs/venn.js');
5
-
6
- function _interopNamespaceDefault(e) {
7
- var n = Object.create(null);
8
- if (e) {
9
- Object.keys(e).forEach(function (k) {
10
- if (k !== 'default') {
11
- var d = Object.getOwnPropertyDescriptor(e, k);
12
- Object.defineProperty(n, k, d.get ? d : {
13
- enumerable: true,
14
- get: function () { return e[k]; }
15
- });
16
- }
17
- });
18
- }
19
- n.default = e;
20
- return Object.freeze(n);
21
- }
22
-
23
- var venn__namespace = /*#__PURE__*/_interopNamespaceDefault(venn);
24
4
 
25
5
  var jsxRuntime = {exports: {}};
26
6
 
@@ -1395,6 +1375,1724 @@ if (process.env.NODE_ENV === 'production') {
1395
1375
 
1396
1376
  var jsxRuntimeExports = jsxRuntime.exports;
1397
1377
 
1378
+ const SMALL$1 = 1e-10;
1379
+
1380
+ /**
1381
+ * Returns the intersection area of a bunch of circles (where each circle
1382
+ * is an object having an x,y and radius property)
1383
+ * @param {ReadonlyArray<{x: number, y: number, radius: number}>} circles
1384
+ * @param {undefined | { area?: number, arcArea?: number, polygonArea?: number, arcs?: ReadonlyArray<{ circle: {x: number, y: number, radius: number}, width: number, p1: {x: number, y: number}, p2: {x: number, y: number} }>, innerPoints: ReadonlyArray<{
1385
+ x: number;
1386
+ y: number;
1387
+ parentIndex: [number, number];
1388
+ }>, intersectionPoints: ReadonlyArray<{
1389
+ x: number;
1390
+ y: number;
1391
+ parentIndex: [number, number];
1392
+ }> }} stats
1393
+ * @returns {number}
1394
+ */
1395
+ function intersectionArea(circles, stats) {
1396
+ // get all the intersection points of the circles
1397
+ const intersectionPoints = getIntersectionPoints(circles);
1398
+
1399
+ // filter out points that aren't included in all the circles
1400
+ const innerPoints = intersectionPoints.filter((p) => containedInCircles(p, circles));
1401
+
1402
+ let arcArea = 0;
1403
+ let polygonArea = 0;
1404
+ /** @type {{ circle: {x: number, y: number, radius: number}, width: number, p1: {x: number, y: number}, p2: {x: number, y: number} }[]} */
1405
+ const arcs = [];
1406
+
1407
+ // if we have intersection points that are within all the circles,
1408
+ // then figure out the area contained by them
1409
+ if (innerPoints.length > 1) {
1410
+ // sort the points by angle from the center of the polygon, which lets
1411
+ // us just iterate over points to get the edges
1412
+ const center = getCenter(innerPoints);
1413
+ for (let i = 0; i < innerPoints.length; ++i) {
1414
+ const p = innerPoints[i];
1415
+ p.angle = Math.atan2(p.x - center.x, p.y - center.y);
1416
+ }
1417
+ innerPoints.sort((a, b) => b.angle - a.angle);
1418
+
1419
+ // iterate over all points, get arc between the points
1420
+ // and update the areas
1421
+ let p2 = innerPoints[innerPoints.length - 1];
1422
+ for (let i = 0; i < innerPoints.length; ++i) {
1423
+ const p1 = innerPoints[i];
1424
+
1425
+ // polygon area updates easily ...
1426
+ polygonArea += (p2.x + p1.x) * (p1.y - p2.y);
1427
+
1428
+ // updating the arc area is a little more involved
1429
+ const midPoint = { x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2 };
1430
+ /** @types null | { circle: {x: number, y: number, radius: number}, width: number, p1: {x: number, y: number}, p2: {x: number, y: number} } */
1431
+ let arc = null;
1432
+
1433
+ for (let j = 0; j < p1.parentIndex.length; ++j) {
1434
+ if (p2.parentIndex.includes(p1.parentIndex[j])) {
1435
+ // figure out the angle halfway between the two points
1436
+ // on the current circle
1437
+ const circle = circles[p1.parentIndex[j]];
1438
+ const a1 = Math.atan2(p1.x - circle.x, p1.y - circle.y);
1439
+ const a2 = Math.atan2(p2.x - circle.x, p2.y - circle.y);
1440
+
1441
+ let angleDiff = a2 - a1;
1442
+ if (angleDiff < 0) {
1443
+ angleDiff += 2 * Math.PI;
1444
+ }
1445
+
1446
+ // and use that angle to figure out the width of the
1447
+ // arc
1448
+ const a = a2 - angleDiff / 2;
1449
+ let width = distance(midPoint, {
1450
+ x: circle.x + circle.radius * Math.sin(a),
1451
+ y: circle.y + circle.radius * Math.cos(a),
1452
+ });
1453
+
1454
+ // clamp the width to the largest is can actually be
1455
+ // (sometimes slightly overflows because of FP errors)
1456
+ if (width > circle.radius * 2) {
1457
+ width = circle.radius * 2;
1458
+ }
1459
+
1460
+ // pick the circle whose arc has the smallest width
1461
+ if (arc == null || arc.width > width) {
1462
+ arc = { circle, width, p1, p2, large: width > circle.radius, sweep: true };
1463
+ }
1464
+ }
1465
+ }
1466
+
1467
+ if (arc != null) {
1468
+ arcs.push(arc);
1469
+ arcArea += circleArea(arc.circle.radius, arc.width);
1470
+ p2 = p1;
1471
+ }
1472
+ }
1473
+ } else {
1474
+ // no intersection points, is either disjoint - or is completely
1475
+ // overlapped. figure out which by examining the smallest circle
1476
+ let smallest = circles[0];
1477
+ for (let i = 1; i < circles.length; ++i) {
1478
+ if (circles[i].radius < smallest.radius) {
1479
+ smallest = circles[i];
1480
+ }
1481
+ }
1482
+
1483
+ // make sure the smallest circle is completely contained in all
1484
+ // the other circles
1485
+ let disjoint = false;
1486
+ for (let i = 0; i < circles.length; ++i) {
1487
+ if (distance(circles[i], smallest) > Math.abs(smallest.radius - circles[i].radius)) {
1488
+ disjoint = true;
1489
+ break;
1490
+ }
1491
+ }
1492
+
1493
+ if (disjoint) {
1494
+ arcArea = polygonArea = 0;
1495
+ } else {
1496
+ arcArea = smallest.radius * smallest.radius * Math.PI;
1497
+ arcs.push({
1498
+ circle: smallest,
1499
+ p1: { x: smallest.x, y: smallest.y + smallest.radius },
1500
+ p2: { x: smallest.x - SMALL$1, y: smallest.y + smallest.radius },
1501
+ width: smallest.radius * 2,
1502
+ large: true,
1503
+ sweep: true,
1504
+ });
1505
+ }
1506
+ }
1507
+
1508
+ polygonArea /= 2;
1509
+
1510
+ if (stats) {
1511
+ stats.area = arcArea + polygonArea;
1512
+ stats.arcArea = arcArea;
1513
+ stats.polygonArea = polygonArea;
1514
+ stats.arcs = arcs;
1515
+ stats.innerPoints = innerPoints;
1516
+ stats.intersectionPoints = intersectionPoints;
1517
+ }
1518
+
1519
+ return arcArea + polygonArea;
1520
+ }
1521
+
1522
+ /**
1523
+ * returns whether a point is contained by all of a list of circles
1524
+ * @param {{x: number, y: number}} point
1525
+ * @param {ReadonlyArray<{x: number, y: number, radius: number}>} circles
1526
+ * @returns {boolean}
1527
+ */
1528
+ function containedInCircles(point, circles) {
1529
+ return circles.every((circle) => distance(point, circle) < circle.radius + SMALL$1);
1530
+ }
1531
+
1532
+ /**
1533
+ * Gets all intersection points between a bunch of circles
1534
+ * @param {ReadonlyArray<{x: number, y: number, radius: number}>} circles
1535
+ * @returns {ReadonlyArray<{x: number, y: number, parentIndex: [number, number]}>}
1536
+ */
1537
+ function getIntersectionPoints(circles) {
1538
+ /** @type {{x: number, y: number, parentIndex: [number, number]}[]} */
1539
+ const ret = [];
1540
+ for (let i = 0; i < circles.length; ++i) {
1541
+ for (let j = i + 1; j < circles.length; ++j) {
1542
+ const intersect = circleCircleIntersection(circles[i], circles[j]);
1543
+ for (const p of intersect) {
1544
+ p.parentIndex = [i, j];
1545
+ ret.push(p);
1546
+ }
1547
+ }
1548
+ }
1549
+ return ret;
1550
+ }
1551
+
1552
+ /**
1553
+ * Circular segment area calculation. See http://mathworld.wolfram.com/CircularSegment.html
1554
+ * @param {number} r
1555
+ * @param {number} width
1556
+ * @returns {number}
1557
+ **/
1558
+ function circleArea(r, width) {
1559
+ return r * r * Math.acos(1 - width / r) - (r - width) * Math.sqrt(width * (2 * r - width));
1560
+ }
1561
+
1562
+ /**
1563
+ * euclidean distance between two points
1564
+ * @param {{x: number, y: number}} p1
1565
+ * @param {{x: number, y: number}} p2
1566
+ * @returns {number}
1567
+ **/
1568
+ function distance(p1, p2) {
1569
+ return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
1570
+ }
1571
+
1572
+ /**
1573
+ * Returns the overlap area of two circles of radius r1 and r2 - that
1574
+ * have their centers separated by distance d. Simpler faster
1575
+ * circle intersection for only two circles
1576
+ * @param {number} r1
1577
+ * @param {number} r2
1578
+ * @param {number} d
1579
+ * @returns {number}
1580
+ */
1581
+ function circleOverlap(r1, r2, d) {
1582
+ // no overlap
1583
+ if (d >= r1 + r2) {
1584
+ return 0;
1585
+ }
1586
+
1587
+ // completely overlapped
1588
+ if (d <= Math.abs(r1 - r2)) {
1589
+ return Math.PI * Math.min(r1, r2) * Math.min(r1, r2);
1590
+ }
1591
+
1592
+ const w1 = r1 - (d * d - r2 * r2 + r1 * r1) / (2 * d);
1593
+ const w2 = r2 - (d * d - r1 * r1 + r2 * r2) / (2 * d);
1594
+ return circleArea(r1, w1) + circleArea(r2, w2);
1595
+ }
1596
+
1597
+ /**
1598
+ * Given two circles (containing a x/y/radius attributes),
1599
+ * returns the intersecting points if possible
1600
+ * note: doesn't handle cases where there are infinitely many
1601
+ * intersection points (circles are equivalent):, or only one intersection point
1602
+ * @param {{x: number, y: number, radius: number}} p1
1603
+ * @param {{x: number, y: number, radius: number}} p2
1604
+ * @returns {ReadonlyArray<{x: number, y: number}>}
1605
+ **/
1606
+ function circleCircleIntersection(p1, p2) {
1607
+ const d = distance(p1, p2);
1608
+ const r1 = p1.radius;
1609
+ const r2 = p2.radius;
1610
+
1611
+ // if to far away, or self contained - can't be done
1612
+ if (d >= r1 + r2 || d <= Math.abs(r1 - r2)) {
1613
+ return [];
1614
+ }
1615
+
1616
+ const a = (r1 * r1 - r2 * r2 + d * d) / (2 * d);
1617
+ const h = Math.sqrt(r1 * r1 - a * a);
1618
+ const x0 = p1.x + (a * (p2.x - p1.x)) / d;
1619
+ const y0 = p1.y + (a * (p2.y - p1.y)) / d;
1620
+ const rx = -(p2.y - p1.y) * (h / d);
1621
+ const ry = -(p2.x - p1.x) * (h / d);
1622
+
1623
+ return [
1624
+ { x: x0 + rx, y: y0 - ry },
1625
+ { x: x0 - rx, y: y0 + ry },
1626
+ ];
1627
+ }
1628
+
1629
+ /**
1630
+ * Returns the center of a bunch of points
1631
+ * @param {ReadonlyArray<{x: number, y: number}>} points
1632
+ * @returns {{x: number, y: number}}
1633
+ */
1634
+ function getCenter(points) {
1635
+ const center = { x: 0, y: 0 };
1636
+ for (const point of points) {
1637
+ center.x += point.x;
1638
+ center.y += point.y;
1639
+ }
1640
+ center.x /= points.length;
1641
+ center.y /= points.length;
1642
+ return center;
1643
+ }
1644
+
1645
+ /** finds the zeros of a function, given two starting points (which must
1646
+ * have opposite signs */
1647
+ function bisect(f, a, b, parameters) {
1648
+ parameters = parameters || {};
1649
+ const maxIterations = parameters.maxIterations || 100;
1650
+ const tolerance = parameters.tolerance || 1e-10;
1651
+ const fA = f(a);
1652
+ const fB = f(b);
1653
+ let delta = b - a;
1654
+
1655
+ if (fA * fB > 0) {
1656
+ throw 'Initial bisect points must have opposite signs';
1657
+ }
1658
+
1659
+ if (fA === 0) return a;
1660
+ if (fB === 0) return b;
1661
+
1662
+ for (let i = 0; i < maxIterations; ++i) {
1663
+ delta /= 2;
1664
+ const mid = a + delta;
1665
+ const fMid = f(mid);
1666
+
1667
+ if (fMid * fA >= 0) {
1668
+ a = mid;
1669
+ }
1670
+
1671
+ if (Math.abs(delta) < tolerance || fMid === 0) {
1672
+ return mid;
1673
+ }
1674
+ }
1675
+ return a + delta;
1676
+ }
1677
+
1678
+ // need some basic operations on vectors, rather than adding a dependency,
1679
+ // just define here
1680
+ function zeros(x) {
1681
+ const r = new Array(x);
1682
+ for (let i = 0; i < x; ++i) {
1683
+ r[i] = 0;
1684
+ }
1685
+ return r;
1686
+ }
1687
+ function zerosM(x, y) {
1688
+ return zeros(x).map(() => zeros(y));
1689
+ }
1690
+
1691
+ function dot(a, b) {
1692
+ let ret = 0;
1693
+ for (let i = 0; i < a.length; ++i) {
1694
+ ret += a[i] * b[i];
1695
+ }
1696
+ return ret;
1697
+ }
1698
+
1699
+ function norm2(a) {
1700
+ return Math.sqrt(dot(a, a));
1701
+ }
1702
+
1703
+ function scale(ret, value, c) {
1704
+ for (let i = 0; i < value.length; ++i) {
1705
+ ret[i] = value[i] * c;
1706
+ }
1707
+ }
1708
+
1709
+ function weightedSum(ret, w1, v1, w2, v2) {
1710
+ for (let j = 0; j < ret.length; ++j) {
1711
+ ret[j] = w1 * v1[j] + w2 * v2[j];
1712
+ }
1713
+ }
1714
+
1715
+ /** minimizes a function using the downhill simplex method */
1716
+ function nelderMead(f, x0, parameters) {
1717
+ parameters = parameters || {};
1718
+
1719
+ const maxIterations = parameters.maxIterations || x0.length * 200;
1720
+ const nonZeroDelta = parameters.nonZeroDelta || 1.05;
1721
+ const zeroDelta = parameters.zeroDelta || 0.001;
1722
+ const minErrorDelta = parameters.minErrorDelta || 1e-6;
1723
+ const minTolerance = parameters.minErrorDelta || 1e-5;
1724
+ const rho = parameters.rho !== undefined ? parameters.rho : 1;
1725
+ const chi = parameters.chi !== undefined ? parameters.chi : 2;
1726
+ const psi = parameters.psi !== undefined ? parameters.psi : -0.5;
1727
+ const sigma = parameters.sigma !== undefined ? parameters.sigma : 0.5;
1728
+ let maxDiff;
1729
+
1730
+ // initialize simplex.
1731
+ const N = x0.length;
1732
+ const simplex = new Array(N + 1);
1733
+ simplex[0] = x0;
1734
+ simplex[0].fx = f(x0);
1735
+ simplex[0].id = 0;
1736
+ for (let i = 0; i < N; ++i) {
1737
+ const point = x0.slice();
1738
+ point[i] = point[i] ? point[i] * nonZeroDelta : zeroDelta;
1739
+ simplex[i + 1] = point;
1740
+ simplex[i + 1].fx = f(point);
1741
+ simplex[i + 1].id = i + 1;
1742
+ }
1743
+
1744
+ function updateSimplex(value) {
1745
+ for (let i = 0; i < value.length; i++) {
1746
+ simplex[N][i] = value[i];
1747
+ }
1748
+ simplex[N].fx = value.fx;
1749
+ }
1750
+
1751
+ const sortOrder = (a, b) => a.fx - b.fx;
1752
+
1753
+ const centroid = x0.slice();
1754
+ const reflected = x0.slice();
1755
+ const contracted = x0.slice();
1756
+ const expanded = x0.slice();
1757
+
1758
+ for (let iteration = 0; iteration < maxIterations; ++iteration) {
1759
+ simplex.sort(sortOrder);
1760
+
1761
+ if (parameters.history) {
1762
+ // copy the simplex (since later iterations will mutate) and
1763
+ // sort it to have a consistent order between iterations
1764
+ const sortedSimplex = simplex.map((x) => {
1765
+ const state = x.slice();
1766
+ state.fx = x.fx;
1767
+ state.id = x.id;
1768
+ return state;
1769
+ });
1770
+ sortedSimplex.sort((a, b) => a.id - b.id);
1771
+
1772
+ parameters.history.push({
1773
+ x: simplex[0].slice(),
1774
+ fx: simplex[0].fx,
1775
+ simplex: sortedSimplex,
1776
+ });
1777
+ }
1778
+
1779
+ maxDiff = 0;
1780
+ for (let i = 0; i < N; ++i) {
1781
+ maxDiff = Math.max(maxDiff, Math.abs(simplex[0][i] - simplex[1][i]));
1782
+ }
1783
+
1784
+ if (Math.abs(simplex[0].fx - simplex[N].fx) < minErrorDelta && maxDiff < minTolerance) {
1785
+ break;
1786
+ }
1787
+
1788
+ // compute the centroid of all but the worst point in the simplex
1789
+ for (let i = 0; i < N; ++i) {
1790
+ centroid[i] = 0;
1791
+ for (let j = 0; j < N; ++j) {
1792
+ centroid[i] += simplex[j][i];
1793
+ }
1794
+ centroid[i] /= N;
1795
+ }
1796
+
1797
+ // reflect the worst point past the centroid and compute loss at reflected
1798
+ // point
1799
+ const worst = simplex[N];
1800
+ weightedSum(reflected, 1 + rho, centroid, -rho, worst);
1801
+ reflected.fx = f(reflected);
1802
+
1803
+ // if the reflected point is the best seen, then possibly expand
1804
+ if (reflected.fx < simplex[0].fx) {
1805
+ weightedSum(expanded, 1 + chi, centroid, -chi, worst);
1806
+ expanded.fx = f(expanded);
1807
+ if (expanded.fx < reflected.fx) {
1808
+ updateSimplex(expanded);
1809
+ } else {
1810
+ updateSimplex(reflected);
1811
+ }
1812
+ }
1813
+
1814
+ // if the reflected point is worse than the second worst, we need to
1815
+ // contract
1816
+ else if (reflected.fx >= simplex[N - 1].fx) {
1817
+ let shouldReduce = false;
1818
+
1819
+ if (reflected.fx > worst.fx) {
1820
+ // do an inside contraction
1821
+ weightedSum(contracted, 1 + psi, centroid, -psi, worst);
1822
+ contracted.fx = f(contracted);
1823
+ if (contracted.fx < worst.fx) {
1824
+ updateSimplex(contracted);
1825
+ } else {
1826
+ shouldReduce = true;
1827
+ }
1828
+ } else {
1829
+ // do an outside contraction
1830
+ weightedSum(contracted, 1 - psi * rho, centroid, psi * rho, worst);
1831
+ contracted.fx = f(contracted);
1832
+ if (contracted.fx < reflected.fx) {
1833
+ updateSimplex(contracted);
1834
+ } else {
1835
+ shouldReduce = true;
1836
+ }
1837
+ }
1838
+
1839
+ if (shouldReduce) {
1840
+ // if we don't contract here, we're done
1841
+ if (sigma >= 1) break;
1842
+
1843
+ // do a reduction
1844
+ for (let i = 1; i < simplex.length; ++i) {
1845
+ weightedSum(simplex[i], 1 - sigma, simplex[0], sigma, simplex[i]);
1846
+ simplex[i].fx = f(simplex[i]);
1847
+ }
1848
+ }
1849
+ } else {
1850
+ updateSimplex(reflected);
1851
+ }
1852
+ }
1853
+
1854
+ simplex.sort(sortOrder);
1855
+ return { fx: simplex[0].fx, x: simplex[0] };
1856
+ }
1857
+
1858
+ /// searches along line 'pk' for a point that satifies the wolfe conditions
1859
+ /// See 'Numerical Optimization' by Nocedal and Wright p59-60
1860
+ /// f : objective function
1861
+ /// pk : search direction
1862
+ /// current: object containing current gradient/loss
1863
+ /// next: output: contains next gradient/loss
1864
+ /// returns a: step size taken
1865
+ function wolfeLineSearch(f, pk, current, next, a, c1, c2) {
1866
+ const phi0 = current.fx;
1867
+ const phiPrime0 = dot(current.fxprime, pk);
1868
+ let phi = phi0;
1869
+ let phi_old = phi0;
1870
+ let phiPrime = phiPrime0;
1871
+ let a0 = 0;
1872
+
1873
+ a = a || 1;
1874
+ c1 = c1 || 1e-6;
1875
+ c2 = c2 || 0.1;
1876
+
1877
+ function zoom(a_lo, a_high, phi_lo) {
1878
+ for (let iteration = 0; iteration < 16; ++iteration) {
1879
+ a = (a_lo + a_high) / 2;
1880
+ weightedSum(next.x, 1.0, current.x, a, pk);
1881
+ phi = next.fx = f(next.x, next.fxprime);
1882
+ phiPrime = dot(next.fxprime, pk);
1883
+
1884
+ if (phi > phi0 + c1 * a * phiPrime0 || phi >= phi_lo) {
1885
+ a_high = a;
1886
+ } else {
1887
+ if (Math.abs(phiPrime) <= -c2 * phiPrime0) {
1888
+ return a;
1889
+ }
1890
+
1891
+ if (phiPrime * (a_high - a_lo) >= 0) {
1892
+ a_high = a_lo;
1893
+ }
1894
+
1895
+ a_lo = a;
1896
+ phi_lo = phi;
1897
+ }
1898
+ }
1899
+
1900
+ return 0;
1901
+ }
1902
+
1903
+ for (let iteration = 0; iteration < 10; ++iteration) {
1904
+ weightedSum(next.x, 1.0, current.x, a, pk);
1905
+ phi = next.fx = f(next.x, next.fxprime);
1906
+ phiPrime = dot(next.fxprime, pk);
1907
+ if (phi > phi0 + c1 * a * phiPrime0 || (iteration && phi >= phi_old)) {
1908
+ return zoom(a0, a, phi_old);
1909
+ }
1910
+
1911
+ if (Math.abs(phiPrime) <= -c2 * phiPrime0) {
1912
+ return a;
1913
+ }
1914
+
1915
+ if (phiPrime >= 0) {
1916
+ return zoom(a, a0, phi);
1917
+ }
1918
+
1919
+ phi_old = phi;
1920
+ a0 = a;
1921
+ a *= 2;
1922
+ }
1923
+
1924
+ return a;
1925
+ }
1926
+
1927
+ function conjugateGradient(f, initial, params) {
1928
+ // allocate all memory up front here, keep out of the loop for perfomance
1929
+ // reasons
1930
+ let current = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
1931
+ let next = { x: initial.slice(), fx: 0, fxprime: initial.slice() };
1932
+ const yk = initial.slice();
1933
+ let pk;
1934
+ let temp;
1935
+ let a = 1;
1936
+ let maxIterations;
1937
+
1938
+ params = params || {};
1939
+ maxIterations = params.maxIterations || initial.length * 20;
1940
+
1941
+ current.fx = f(current.x, current.fxprime);
1942
+ pk = current.fxprime.slice();
1943
+ scale(pk, current.fxprime, -1);
1944
+
1945
+ for (let i = 0; i < maxIterations; ++i) {
1946
+ a = wolfeLineSearch(f, pk, current, next, a);
1947
+
1948
+ // todo: history in wrong spot?
1949
+ if (params.history) {
1950
+ params.history.push({
1951
+ x: current.x.slice(),
1952
+ fx: current.fx,
1953
+ fxprime: current.fxprime.slice(),
1954
+ alpha: a,
1955
+ });
1956
+ }
1957
+
1958
+ if (!a) {
1959
+ // faiiled to find point that satifies wolfe conditions.
1960
+ // reset direction for next iteration
1961
+ scale(pk, current.fxprime, -1);
1962
+ } else {
1963
+ // update direction using Polak–Ribiere CG method
1964
+ weightedSum(yk, 1, next.fxprime, -1, current.fxprime);
1965
+
1966
+ const delta_k = dot(current.fxprime, current.fxprime);
1967
+ const beta_k = Math.max(0, dot(yk, next.fxprime) / delta_k);
1968
+
1969
+ weightedSum(pk, beta_k, pk, -1, next.fxprime);
1970
+
1971
+ temp = current;
1972
+ current = next;
1973
+ next = temp;
1974
+ }
1975
+
1976
+ if (norm2(current.fxprime) <= 1e-5) {
1977
+ break;
1978
+ }
1979
+ }
1980
+
1981
+ if (params.history) {
1982
+ params.history.push({
1983
+ x: current.x.slice(),
1984
+ fx: current.fx,
1985
+ fxprime: current.fxprime.slice(),
1986
+ alpha: a,
1987
+ });
1988
+ }
1989
+
1990
+ return current;
1991
+ }
1992
+
1993
+ /**
1994
+ * given a list of set objects, and their corresponding overlaps
1995
+ * updates the (x, y, radius) attribute on each set such that their positions
1996
+ * roughly correspond to the desired overlaps
1997
+ * @param {readonly {sets: readonly string[]; size: number; weight?: number}[]} sets
1998
+ * @returns {{[setid: string]: {x: number, y: number, radius: number}}}
1999
+ */
2000
+ function venn(sets, parameters = {}) {
2001
+ parameters.maxIterations = parameters.maxIterations || 500;
2002
+
2003
+ const initialLayout = parameters.initialLayout || bestInitialLayout;
2004
+ const loss = parameters.lossFunction || lossFunction;
2005
+
2006
+ // add in missing pairwise areas as having 0 size
2007
+ const areas = addMissingAreas(sets, parameters);
2008
+
2009
+ // initial layout is done greedily
2010
+ const circles = initialLayout(areas, parameters);
2011
+
2012
+ // transform x/y coordinates to a vector to optimize
2013
+ const setids = Object.keys(circles);
2014
+ /** @type {number[]} */
2015
+ const initial = [];
2016
+ for (const setid of setids) {
2017
+ initial.push(circles[setid].x);
2018
+ initial.push(circles[setid].y);
2019
+ }
2020
+
2021
+ // optimize initial layout from our loss function
2022
+ const solution = nelderMead(
2023
+ (values) => {
2024
+ const current = {};
2025
+ for (let i = 0; i < setids.length; ++i) {
2026
+ const setid = setids[i];
2027
+ current[setid] = {
2028
+ x: values[2 * i],
2029
+ y: values[2 * i + 1],
2030
+ radius: circles[setid].radius,
2031
+ // size : circles[setid].size
2032
+ };
2033
+ }
2034
+ return loss(current, areas);
2035
+ },
2036
+ initial,
2037
+ parameters
2038
+ );
2039
+
2040
+ // transform solution vector back to x/y points
2041
+ const positions = solution.x;
2042
+ for (let i = 0; i < setids.length; ++i) {
2043
+ const setid = setids[i];
2044
+ circles[setid].x = positions[2 * i];
2045
+ circles[setid].y = positions[2 * i + 1];
2046
+ }
2047
+
2048
+ return circles;
2049
+ }
2050
+
2051
+ const SMALL = 1e-10;
2052
+
2053
+ /**
2054
+ * Returns the distance necessary for two circles of radius r1 + r2 to
2055
+ * have the overlap area 'overlap'
2056
+ * @param {number} r1
2057
+ * @param {number} r2
2058
+ * @param {number} overlap
2059
+ * @returns {number}
2060
+ */
2061
+ function distanceFromIntersectArea(r1, r2, overlap) {
2062
+ // handle complete overlapped circles
2063
+ if (Math.min(r1, r2) * Math.min(r1, r2) * Math.PI <= overlap + SMALL) {
2064
+ return Math.abs(r1 - r2);
2065
+ }
2066
+
2067
+ return bisect((distance) => circleOverlap(r1, r2, distance) - overlap, 0, r1 + r2);
2068
+ }
2069
+
2070
+ /**
2071
+ * Missing pair-wise intersection area data can cause problems:
2072
+ * treating as an unknown means that sets will be laid out overlapping,
2073
+ * which isn't what people expect. To reflect that we want disjoint sets
2074
+ * here, set the overlap to 0 for all missing pairwise set intersections
2075
+ * @param {ReadonlyArray<{sets: ReadonlyArray<string>, size: number}>} areas
2076
+ * @returns {ReadonlyArray<{sets: ReadonlyArray<string>, size: number}>}
2077
+ */
2078
+ function addMissingAreas(areas, parameters = {}) {
2079
+ const distinct = parameters.distinct;
2080
+ const r = areas.map((s) => Object.assign({}, s));
2081
+
2082
+ function toKey(arr) {
2083
+ return arr.join(';');
2084
+ }
2085
+
2086
+ if (distinct) {
2087
+ // recreate the full ones by adding things up but just to level two since the rest doesn't matter
2088
+ /** @types Map<string, number> */
2089
+ const count = new Map();
2090
+ for (const area of r) {
2091
+ for (let i = 0; i < area.sets.length; i++) {
2092
+ const si = String(area.sets[i]);
2093
+ count.set(si, area.size + (count.get(si) || 0));
2094
+ for (let j = i + 1; j < area.sets.length; j++) {
2095
+ const sj = String(area.sets[j]);
2096
+ const k1 = `${si};${sj}`;
2097
+ const k2 = `${sj};${si}`;
2098
+ count.set(k1, area.size + (count.get(k1) || 0));
2099
+ count.set(k2, area.size + (count.get(k2) || 0));
2100
+ }
2101
+ }
2102
+ }
2103
+ for (const area of r) {
2104
+ if (area.sets.length < 3) {
2105
+ area.size = count.get(toKey(area.sets));
2106
+ }
2107
+ }
2108
+ }
2109
+
2110
+ // two circle intersections that aren't defined
2111
+ const ids = [];
2112
+
2113
+ /** @type {Set<string>} */
2114
+ const pairs = new Set();
2115
+ for (const area of r) {
2116
+ if (area.sets.length === 1) {
2117
+ ids.push(area.sets[0]);
2118
+ } else if (area.sets.length === 2) {
2119
+ const a = area.sets[0];
2120
+ const b = area.sets[1];
2121
+ pairs.add(toKey(area.sets));
2122
+ pairs.add(toKey([b, a]));
2123
+ }
2124
+ }
2125
+
2126
+ ids.sort((a, b) => (a === b ? 0 : a < b ? -1 : +1));
2127
+
2128
+ for (let i = 0; i < ids.length; ++i) {
2129
+ const a = ids[i];
2130
+ for (let j = i + 1; j < ids.length; ++j) {
2131
+ const b = ids[j];
2132
+ if (!pairs.has(toKey([a, b]))) {
2133
+ r.push({ sets: [a, b], size: 0 });
2134
+ }
2135
+ }
2136
+ }
2137
+ return r;
2138
+ }
2139
+
2140
+ /**
2141
+ * Returns two matrices, one of the euclidean distances between the sets
2142
+ * and the other indicating if there are subset or disjoint set relationships
2143
+ * @param {ReadonlyArray<{sets: ReadonlyArray<number>}>} areas
2144
+ * @param {ReadonlyArray<{size: number}>} sets
2145
+ * @param {ReadonlyArray<number>} setids
2146
+ */
2147
+ function getDistanceMatrices(areas, sets, setids) {
2148
+ // initialize an empty distance matrix between all the points
2149
+ /**
2150
+ * @type {number[][]}
2151
+ */
2152
+ const distances = zerosM(sets.length, sets.length);
2153
+ /**
2154
+ * @type {number[][]}
2155
+ */
2156
+ const constraints = zerosM(sets.length, sets.length);
2157
+
2158
+ // compute required distances between all the sets such that
2159
+ // the areas match
2160
+ areas
2161
+ .filter((x) => x.sets.length === 2)
2162
+ .forEach((current) => {
2163
+ const left = setids[current.sets[0]];
2164
+ const right = setids[current.sets[1]];
2165
+ const r1 = Math.sqrt(sets[left].size / Math.PI);
2166
+ const r2 = Math.sqrt(sets[right].size / Math.PI);
2167
+ const distance = distanceFromIntersectArea(r1, r2, current.size);
2168
+
2169
+ distances[left][right] = distances[right][left] = distance;
2170
+
2171
+ // also update constraints to indicate if its a subset or disjoint
2172
+ // relationship
2173
+ let c = 0;
2174
+ if (current.size + 1e-10 >= Math.min(sets[left].size, sets[right].size)) {
2175
+ c = 1;
2176
+ } else if (current.size <= 1e-10) {
2177
+ c = -1;
2178
+ }
2179
+ constraints[left][right] = constraints[right][left] = c;
2180
+ });
2181
+
2182
+ return { distances, constraints };
2183
+ }
2184
+
2185
+ /// computes the gradient and loss simultaneously for our constrained MDS optimizer
2186
+ function constrainedMDSGradient(x, fxprime, distances, constraints) {
2187
+ for (let i = 0; i < fxprime.length; ++i) {
2188
+ fxprime[i] = 0;
2189
+ }
2190
+
2191
+ let loss = 0;
2192
+ for (let i = 0; i < distances.length; ++i) {
2193
+ const xi = x[2 * i];
2194
+ const yi = x[2 * i + 1];
2195
+ for (let j = i + 1; j < distances.length; ++j) {
2196
+ const xj = x[2 * j];
2197
+ const yj = x[2 * j + 1];
2198
+ const dij = distances[i][j];
2199
+ const constraint = constraints[i][j];
2200
+
2201
+ const squaredDistance = (xj - xi) * (xj - xi) + (yj - yi) * (yj - yi);
2202
+ const distance = Math.sqrt(squaredDistance);
2203
+ const delta = squaredDistance - dij * dij;
2204
+
2205
+ if ((constraint > 0 && distance <= dij) || (constraint < 0 && distance >= dij)) {
2206
+ continue;
2207
+ }
2208
+
2209
+ loss += 2 * delta * delta;
2210
+
2211
+ fxprime[2 * i] += 4 * delta * (xi - xj);
2212
+ fxprime[2 * i + 1] += 4 * delta * (yi - yj);
2213
+
2214
+ fxprime[2 * j] += 4 * delta * (xj - xi);
2215
+ fxprime[2 * j + 1] += 4 * delta * (yj - yi);
2216
+ }
2217
+ }
2218
+ return loss;
2219
+ }
2220
+
2221
+ /**
2222
+ * takes the best working variant of either constrained MDS or greedy
2223
+ * @param {ReadonlyArray<{sets: ReadonlyArray<string>, size: number}>} areas
2224
+ */
2225
+ function bestInitialLayout(areas, params = {}) {
2226
+ let initial = greedyLayout(areas, params);
2227
+ const loss = params.lossFunction || lossFunction;
2228
+
2229
+ // greedylayout is sufficient for all 2/3 circle cases. try out
2230
+ // constrained MDS for higher order problems, take its output
2231
+ // if it outperforms. (greedy is aesthetically better on 2/3 circles
2232
+ // since it axis aligns)
2233
+ if (areas.length >= 8) {
2234
+ const constrained = constrainedMDSLayout(areas, params);
2235
+ const constrainedLoss = loss(constrained, areas);
2236
+ const greedyLoss = loss(initial, areas);
2237
+
2238
+ if (constrainedLoss + 1e-8 < greedyLoss) {
2239
+ initial = constrained;
2240
+ }
2241
+ }
2242
+ return initial;
2243
+ }
2244
+
2245
+ /**
2246
+ * use the constrained MDS variant to generate an initial layout
2247
+ * @param {ReadonlyArray<{sets: ReadonlyArray<string>, size: number}>} areas
2248
+ * @returns {{[key: string]: {x: number, y: number, radius: number}}}
2249
+ */
2250
+ function constrainedMDSLayout(areas, params = {}) {
2251
+ const restarts = params.restarts || 10;
2252
+
2253
+ // bidirectionally map sets to a rowid (so we can create a matrix)
2254
+ const sets = [];
2255
+ const setids = {};
2256
+ for (const area of areas) {
2257
+ if (area.sets.length === 1) {
2258
+ setids[area.sets[0]] = sets.length;
2259
+ sets.push(area);
2260
+ }
2261
+ }
2262
+
2263
+ let { distances, constraints } = getDistanceMatrices(areas, sets, setids);
2264
+
2265
+ // keep distances bounded, things get messed up otherwise.
2266
+ // TODO: proper preconditioner?
2267
+ const norm = norm2(distances.map(norm2)) / distances.length;
2268
+ distances = distances.map((row) => row.map((value) => value / norm));
2269
+
2270
+ const obj = (x, fxprime) => constrainedMDSGradient(x, fxprime, distances, constraints);
2271
+
2272
+ let best = null;
2273
+ for (let i = 0; i < restarts; ++i) {
2274
+ const initial = zeros(distances.length * 2).map(Math.random);
2275
+
2276
+ const current = conjugateGradient(obj, initial, params);
2277
+ if (!best || current.fx < best.fx) {
2278
+ best = current;
2279
+ }
2280
+ }
2281
+
2282
+ const positions = best.x;
2283
+
2284
+ // translate rows back to (x,y,radius) coordinates
2285
+ /** @type {{[key: string]: {x: number, y: number, radius: number}}} */
2286
+ const circles = {};
2287
+ for (let i = 0; i < sets.length; ++i) {
2288
+ const set = sets[i];
2289
+ circles[set.sets[0]] = {
2290
+ x: positions[2 * i] * norm,
2291
+ y: positions[2 * i + 1] * norm,
2292
+ radius: Math.sqrt(set.size / Math.PI),
2293
+ };
2294
+ }
2295
+
2296
+ if (params.history) {
2297
+ for (const h of params.history) {
2298
+ scale(h.x, norm);
2299
+ }
2300
+ }
2301
+ return circles;
2302
+ }
2303
+
2304
+ /**
2305
+ * Lays out a Venn diagram greedily, going from most overlapped sets to
2306
+ * least overlapped, attempting to position each new set such that the
2307
+ * overlapping areas to already positioned sets are basically right
2308
+ * @param {ReadonlyArray<{size: number, sets: ReadonlyArray<string>}>} areas
2309
+ * @return {{[key: string]: {x: number, y: number, radius: number}}}
2310
+ */
2311
+ function greedyLayout(areas, params) {
2312
+ const loss = params && params.lossFunction ? params.lossFunction : lossFunction;
2313
+
2314
+ // define a circle for each set
2315
+ /** @type {{[key: string]: {x: number, y: number, radius: number}}} */
2316
+ const circles = {};
2317
+ /** @type {{[key: string]: {set: string, size: number, weight: number}[]}} */
2318
+ const setOverlaps = {};
2319
+ for (const area of areas) {
2320
+ if (area.sets.length === 1) {
2321
+ const set = area.sets[0];
2322
+ circles[set] = {
2323
+ x: 1e10,
2324
+ y: 1e10,
2325
+ rowid: circles.length,
2326
+ size: area.size,
2327
+ radius: Math.sqrt(area.size / Math.PI),
2328
+ };
2329
+ setOverlaps[set] = [];
2330
+ }
2331
+ }
2332
+
2333
+ areas = areas.filter((a) => a.sets.length === 2);
2334
+
2335
+ // map each set to a list of all the other sets that overlap it
2336
+ for (const current of areas) {
2337
+ let weight = current.weight != null ? current.weight : 1.0;
2338
+ const left = current.sets[0];
2339
+ const right = current.sets[1];
2340
+
2341
+ // completely overlapped circles shouldn't be positioned early here
2342
+ if (current.size + SMALL >= Math.min(circles[left].size, circles[right].size)) {
2343
+ weight = 0;
2344
+ }
2345
+
2346
+ setOverlaps[left].push({ set: right, size: current.size, weight });
2347
+ setOverlaps[right].push({ set: left, size: current.size, weight });
2348
+ }
2349
+
2350
+ // get list of most overlapped sets
2351
+ const mostOverlapped = [];
2352
+ Object.keys(setOverlaps).forEach((set) => {
2353
+ let size = 0;
2354
+ for (let i = 0; i < setOverlaps[set].length; ++i) {
2355
+ size += setOverlaps[set][i].size * setOverlaps[set][i].weight;
2356
+ }
2357
+
2358
+ mostOverlapped.push({ set, size });
2359
+ });
2360
+
2361
+ // sort by size desc
2362
+ function sortOrder(a, b) {
2363
+ return b.size - a.size;
2364
+ }
2365
+ mostOverlapped.sort(sortOrder);
2366
+
2367
+ // keep track of what sets have been laid out
2368
+ const positioned = {};
2369
+ function isPositioned(element) {
2370
+ return element.set in positioned;
2371
+ }
2372
+
2373
+ /**
2374
+ * adds a point to the output
2375
+ * @param {{x: number, y: number}} point
2376
+ * @param {number} index
2377
+ */
2378
+ function positionSet(point, index) {
2379
+ circles[index].x = point.x;
2380
+ circles[index].y = point.y;
2381
+ positioned[index] = true;
2382
+ }
2383
+
2384
+ // add most overlapped set at (0,0)
2385
+ positionSet({ x: 0, y: 0 }, mostOverlapped[0].set);
2386
+
2387
+ // get distances between all points. TODO, necessary?
2388
+ // answer: probably not
2389
+ // var distances = venn.getDistanceMatrices(circles, areas).distances;
2390
+ for (let i = 1; i < mostOverlapped.length; ++i) {
2391
+ const setIndex = mostOverlapped[i].set;
2392
+ const overlap = setOverlaps[setIndex].filter(isPositioned);
2393
+ const set = circles[setIndex];
2394
+ overlap.sort(sortOrder);
2395
+
2396
+ if (overlap.length === 0) {
2397
+ // this shouldn't happen anymore with addMissingAreas
2398
+ throw 'ERROR: missing pairwise overlap information';
2399
+ }
2400
+
2401
+ /** @type {{x: number, y: number}[]} */
2402
+ const points = [];
2403
+ for (var j = 0; j < overlap.length; ++j) {
2404
+ // get appropriate distance from most overlapped already added set
2405
+ const p1 = circles[overlap[j].set];
2406
+ const d1 = distanceFromIntersectArea(set.radius, p1.radius, overlap[j].size);
2407
+
2408
+ // sample positions at 90 degrees for maximum aesthetics
2409
+ points.push({ x: p1.x + d1, y: p1.y });
2410
+ points.push({ x: p1.x - d1, y: p1.y });
2411
+ points.push({ y: p1.y + d1, x: p1.x });
2412
+ points.push({ y: p1.y - d1, x: p1.x });
2413
+
2414
+ // if we have at least 2 overlaps, then figure out where the
2415
+ // set should be positioned analytically and try those too
2416
+ for (let k = j + 1; k < overlap.length; ++k) {
2417
+ const p2 = circles[overlap[k].set];
2418
+ const d2 = distanceFromIntersectArea(set.radius, p2.radius, overlap[k].size);
2419
+
2420
+ const extraPoints = circleCircleIntersection(
2421
+ { x: p1.x, y: p1.y, radius: d1 },
2422
+ { x: p2.x, y: p2.y, radius: d2 }
2423
+ );
2424
+ points.push(...extraPoints);
2425
+ }
2426
+ }
2427
+
2428
+ // we have some candidate positions for the set, examine loss
2429
+ // at each position to figure out where to put it at
2430
+ let bestLoss = 1e50;
2431
+ let bestPoint = points[0];
2432
+ for (const point of points) {
2433
+ circles[setIndex].x = point.x;
2434
+ circles[setIndex].y = point.y;
2435
+ const localLoss = loss(circles, areas);
2436
+ if (localLoss < bestLoss) {
2437
+ bestLoss = localLoss;
2438
+ bestPoint = point;
2439
+ }
2440
+ }
2441
+
2442
+ positionSet(bestPoint, setIndex);
2443
+ }
2444
+
2445
+ return circles;
2446
+ }
2447
+
2448
+ /**
2449
+ * Given a bunch of sets, and the desired overlaps between these sets - computes
2450
+ * the distance from the actual overlaps to the desired overlaps. Note that
2451
+ * this method ignores overlaps of more than 2 circles
2452
+ * @param {{[key: string]: <{x: number, y: number, radius: number}>}} circles
2453
+ * @param {ReadonlyArray<{size: number, sets: ReadonlyArray<string>, weight?: number}>} overlaps
2454
+ * @returns {number}
2455
+ */
2456
+ function lossFunction(circles, overlaps) {
2457
+ let output = 0;
2458
+
2459
+ for (const area of overlaps) {
2460
+ if (area.sets.length === 1) {
2461
+ continue;
2462
+ }
2463
+ /** @type {number} */
2464
+ let overlap;
2465
+ if (area.sets.length === 2) {
2466
+ const left = circles[area.sets[0]];
2467
+ const right = circles[area.sets[1]];
2468
+ overlap = circleOverlap(left.radius, right.radius, distance(left, right));
2469
+ } else {
2470
+ overlap = intersectionArea(area.sets.map((d) => circles[d]));
2471
+ }
2472
+
2473
+ const weight = area.weight != null ? area.weight : 1.0;
2474
+ output += weight * (overlap - area.size) * (overlap - area.size);
2475
+ }
2476
+
2477
+ return output;
2478
+ }
2479
+
2480
+ function logRatioLossFunction(circles, overlaps) {
2481
+ let output = 0;
2482
+
2483
+ for (const area of overlaps) {
2484
+ if (area.sets.length === 1) {
2485
+ continue;
2486
+ }
2487
+ /** @type {number} */
2488
+ let overlap;
2489
+ if (area.sets.length === 2) {
2490
+ const left = circles[area.sets[0]];
2491
+ const right = circles[area.sets[1]];
2492
+ overlap = circleOverlap(left.radius, right.radius, distance(left, right));
2493
+ } else {
2494
+ overlap = intersectionArea(area.sets.map((d) => circles[d]));
2495
+ }
2496
+
2497
+ const weight = area.weight != null ? area.weight : 1.0;
2498
+ const differenceFromIdeal = Math.log((overlap + 1) / (area.size + 1));
2499
+ output += weight * differenceFromIdeal * differenceFromIdeal;
2500
+ }
2501
+
2502
+ return output;
2503
+ }
2504
+
2505
+ /**
2506
+ * orientates a bunch of circles to point in orientation
2507
+ * @param {{x :number, y: number, radius: number}[]} circles
2508
+ * @param {number | undefined} orientation
2509
+ * @param {((a: {x :number, y: number, radius: number}, b: {x :number, y: number, radius: number}) => number) | undefined} orientationOrder
2510
+ */
2511
+ function orientateCircles(circles, orientation, orientationOrder) {
2512
+ if (orientationOrder == null) {
2513
+ circles.sort((a, b) => b.radius - a.radius);
2514
+ } else {
2515
+ circles.sort(orientationOrder);
2516
+ }
2517
+
2518
+ // shift circles so largest circle is at (0, 0)
2519
+ if (circles.length > 0) {
2520
+ const largestX = circles[0].x;
2521
+ const largestY = circles[0].y;
2522
+
2523
+ for (const circle of circles) {
2524
+ circle.x -= largestX;
2525
+ circle.y -= largestY;
2526
+ }
2527
+ }
2528
+
2529
+ if (circles.length === 2) {
2530
+ // if the second circle is a subset of the first, arrange so that
2531
+ // it is off to one side. hack for https://github.com/benfred/venn.js/issues/120
2532
+ const dist = distance(circles[0], circles[1]);
2533
+ if (dist < Math.abs(circles[1].radius - circles[0].radius)) {
2534
+ circles[1].x = circles[0].x + circles[0].radius - circles[1].radius - 1e-10;
2535
+ circles[1].y = circles[0].y;
2536
+ }
2537
+ }
2538
+
2539
+ // rotate circles so that second largest is at an angle of 'orientation'
2540
+ // from largest
2541
+ if (circles.length > 1) {
2542
+ const rotation = Math.atan2(circles[1].x, circles[1].y) - orientation;
2543
+ const c = Math.cos(rotation);
2544
+ const s = Math.sin(rotation);
2545
+
2546
+ for (const circle of circles) {
2547
+ const x = circle.x;
2548
+ const y = circle.y;
2549
+ circle.x = c * x - s * y;
2550
+ circle.y = s * x + c * y;
2551
+ }
2552
+ }
2553
+
2554
+ // mirror solution if third solution is above plane specified by
2555
+ // first two circles
2556
+ if (circles.length > 2) {
2557
+ let angle = Math.atan2(circles[2].x, circles[2].y) - orientation;
2558
+ while (angle < 0) {
2559
+ angle += 2 * Math.PI;
2560
+ }
2561
+ while (angle > 2 * Math.PI) {
2562
+ angle -= 2 * Math.PI;
2563
+ }
2564
+ if (angle > Math.PI) {
2565
+ const slope = circles[1].y / (1e-10 + circles[1].x);
2566
+ for (const circle of circles) {
2567
+ var d = (circle.x + slope * circle.y) / (1 + slope * slope);
2568
+ circle.x = 2 * d - circle.x;
2569
+ circle.y = 2 * d * slope - circle.y;
2570
+ }
2571
+ }
2572
+ }
2573
+ }
2574
+
2575
+ /**
2576
+ *
2577
+ * @param {ReadonlyArray<{x: number, y: number, radius: number}>} circles
2578
+ * @returns {{x: number, y: number, radius: number}[][]}
2579
+ */
2580
+ function disjointCluster(circles) {
2581
+ // union-find clustering to get disjoint sets
2582
+ circles.forEach((circle) => {
2583
+ circle.parent = circle;
2584
+ });
2585
+
2586
+ // path compression step in union find
2587
+ function find(circle) {
2588
+ if (circle.parent !== circle) {
2589
+ circle.parent = find(circle.parent);
2590
+ }
2591
+ return circle.parent;
2592
+ }
2593
+
2594
+ function union(x, y) {
2595
+ const xRoot = find(x);
2596
+ const yRoot = find(y);
2597
+ xRoot.parent = yRoot;
2598
+ }
2599
+
2600
+ // get the union of all overlapping sets
2601
+ for (let i = 0; i < circles.length; ++i) {
2602
+ for (let j = i + 1; j < circles.length; ++j) {
2603
+ const maxDistance = circles[i].radius + circles[j].radius;
2604
+ if (distance(circles[i], circles[j]) + 1e-10 < maxDistance) {
2605
+ union(circles[j], circles[i]);
2606
+ }
2607
+ }
2608
+ }
2609
+
2610
+ // find all the disjoint clusters and group them together
2611
+ /** @type {Map<string, {x: number, y: number, radius: number}[]>} */
2612
+ const disjointClusters = new Map();
2613
+ for (let i = 0; i < circles.length; ++i) {
2614
+ const setid = find(circles[i]).parent.setid;
2615
+ if (!disjointClusters.has(setid)) {
2616
+ disjointClusters.set(setid, []);
2617
+ }
2618
+ disjointClusters.get(setid).push(circles[i]);
2619
+ }
2620
+
2621
+ // cleanup bookkeeping
2622
+ circles.forEach((circle) => {
2623
+ delete circle.parent;
2624
+ });
2625
+
2626
+ // return in more usable form
2627
+ return Array.from(disjointClusters.values());
2628
+ }
2629
+
2630
+ /**
2631
+ * @param {ReadonlyArray<{x :number, y: number, radius: number}>} circles
2632
+ * @returns {{xRange: [number, number], yRange: [number, number]}}
2633
+ */
2634
+ function getBoundingBox(circles) {
2635
+ const minMax = (d) => {
2636
+ const hi = circles.reduce((acc, c) => Math.max(acc, c[d] + c.radius), Number.NEGATIVE_INFINITY);
2637
+ const lo = circles.reduce((acc, c) => Math.min(acc, c[d] - c.radius), Number.POSITIVE_INFINITY);
2638
+ return { max: hi, min: lo };
2639
+ };
2640
+ return { xRange: minMax('x'), yRange: minMax('y') };
2641
+ }
2642
+
2643
+ /**
2644
+ *
2645
+ * @param {{[setid: string]: {x: number, y: number, radius: number}}} solution
2646
+ * @param {undefined | number} orientation
2647
+ * @param {((a: {x :number, y: number, radius: number}, b: {x :number, y: number, radius: number}) => number) | undefined} orientationOrder
2648
+ * @returns {{[setid: string]: {x: number, y: number, radius: number}}}
2649
+ */
2650
+ function normalizeSolution(solution, orientation, orientationOrder) {
2651
+ if (orientation == null) {
2652
+ orientation = Math.PI / 2;
2653
+ }
2654
+
2655
+ // work with a list instead of a dictionary, and take a copy so we
2656
+ // don't mutate input
2657
+ let circles = fromObjectNotation(solution).map((d) => Object.assign({}, d));
2658
+
2659
+ // get all the disjoint clusters
2660
+ const clusters = disjointCluster(circles);
2661
+
2662
+ // orientate all disjoint sets, get sizes
2663
+ for (const cluster of clusters) {
2664
+ orientateCircles(cluster, orientation, orientationOrder);
2665
+ const bounds = getBoundingBox(cluster);
2666
+ cluster.size = (bounds.xRange.max - bounds.xRange.min) * (bounds.yRange.max - bounds.yRange.min);
2667
+ cluster.bounds = bounds;
2668
+ }
2669
+ clusters.sort((a, b) => b.size - a.size);
2670
+
2671
+ // orientate the largest at 0,0, and get the bounds
2672
+ circles = clusters[0];
2673
+ let returnBounds = circles.bounds;
2674
+ const spacing = (returnBounds.xRange.max - returnBounds.xRange.min) / 50;
2675
+
2676
+ /**
2677
+ * @param {ReadonlyArray<{x: number, y: number, radius: number, setid: string}>} cluster
2678
+ * @param {boolean} right
2679
+ * @param {boolean} bottom
2680
+ */
2681
+ function addCluster(cluster, right, bottom) {
2682
+ if (!cluster) {
2683
+ return;
2684
+ }
2685
+
2686
+ const bounds = cluster.bounds;
2687
+ /** @type {number} */
2688
+ let xOffset;
2689
+ /** @type {number} */
2690
+ let yOffset;
2691
+
2692
+ if (right) {
2693
+ xOffset = returnBounds.xRange.max - bounds.xRange.min + spacing;
2694
+ } else {
2695
+ xOffset = returnBounds.xRange.max - bounds.xRange.max;
2696
+ const centreing =
2697
+ (bounds.xRange.max - bounds.xRange.min) / 2 - (returnBounds.xRange.max - returnBounds.xRange.min) / 2;
2698
+ if (centreing < 0) {
2699
+ xOffset += centreing;
2700
+ }
2701
+ }
2702
+
2703
+ if (bottom) {
2704
+ yOffset = returnBounds.yRange.max - bounds.yRange.min + spacing;
2705
+ } else {
2706
+ yOffset = returnBounds.yRange.max - bounds.yRange.max;
2707
+ const centreing =
2708
+ (bounds.yRange.max - bounds.yRange.min) / 2 - (returnBounds.yRange.max - returnBounds.yRange.min) / 2;
2709
+ if (centreing < 0) {
2710
+ yOffset += centreing;
2711
+ }
2712
+ }
2713
+
2714
+ for (const c of cluster) {
2715
+ c.x += xOffset;
2716
+ c.y += yOffset;
2717
+ circles.push(c);
2718
+ }
2719
+ }
2720
+
2721
+ let index = 1;
2722
+ while (index < clusters.length) {
2723
+ addCluster(clusters[index], true, false);
2724
+ addCluster(clusters[index + 1], false, true);
2725
+ addCluster(clusters[index + 2], true, true);
2726
+ index += 3;
2727
+
2728
+ // have one cluster (in top left). lay out next three relative
2729
+ // to it in a grid
2730
+ returnBounds = getBoundingBox(circles);
2731
+ }
2732
+
2733
+ // convert back to solution form
2734
+ return toObjectNotation(circles);
2735
+ }
2736
+
2737
+ /**
2738
+ * Scales a solution from venn.venn or venn.greedyLayout such that it fits in
2739
+ * a rectangle of width/height - with padding around the borders. also
2740
+ * centers the diagram in the available space at the same time.
2741
+ * If the scale parameter is not null, this automatic scaling is ignored in favor of this custom one
2742
+ * @param {{[setid: string]: {x: number, y: number, radius: number}}} solution
2743
+ * @param {number} width
2744
+ * @param {number} height
2745
+ * @param {number} padding
2746
+ * @param {boolean} scaleToFit
2747
+ * @returns {{[setid: string]: {x: number, y: number, radius: number}}}
2748
+ */
2749
+ function scaleSolution(solution, width, height, padding, scaleToFit) {
2750
+ const circles = fromObjectNotation(solution);
2751
+
2752
+ width -= 2 * padding;
2753
+ height -= 2 * padding;
2754
+
2755
+ const { xRange, yRange } = getBoundingBox(circles);
2756
+
2757
+ if (xRange.max === xRange.min || yRange.max === yRange.min) {
2758
+ console.log('not scaling solution: zero size detected');
2759
+ return solution;
2760
+ }
2761
+
2762
+ /** @type {number} */
2763
+ let xScaling;
2764
+ /** @type {number} */
2765
+ let yScaling;
2766
+ if (scaleToFit) {
2767
+ const toScaleDiameter = Math.sqrt(scaleToFit / Math.PI) * 2;
2768
+ xScaling = width / toScaleDiameter;
2769
+ yScaling = height / toScaleDiameter;
2770
+ } else {
2771
+ xScaling = width / (xRange.max - xRange.min);
2772
+ yScaling = height / (yRange.max - yRange.min);
2773
+ }
2774
+
2775
+ const scaling = Math.min(yScaling, xScaling);
2776
+ // while we're at it, center the diagram too
2777
+ const xOffset = (width - (xRange.max - xRange.min) * scaling) / 2;
2778
+ const yOffset = (height - (yRange.max - yRange.min) * scaling) / 2;
2779
+
2780
+ return toObjectNotation(
2781
+ circles.map((circle) => ({
2782
+ radius: scaling * circle.radius,
2783
+ x: padding + xOffset + (circle.x - xRange.min) * scaling,
2784
+ y: padding + yOffset + (circle.y - yRange.min) * scaling,
2785
+ setid: circle.setid,
2786
+ }))
2787
+ );
2788
+ }
2789
+
2790
+ /**
2791
+ * @param {readonly {x: number, y: number, radius: number, setid: string}[]} circles
2792
+ * @returns {{[setid: string]: {x: number, y: number, radius: number, setid: string}}}
2793
+ */
2794
+ function toObjectNotation(circles) {
2795
+ /** @type {{[setid: string]: {x: number, y: number, radius: number, setid: string}}} */
2796
+ const r = {};
2797
+ for (const circle of circles) {
2798
+ r[circle.setid] = circle;
2799
+ }
2800
+ return r;
2801
+ }
2802
+ /**
2803
+ * @param {{[setid: string]: {x: number, y: number, radius: number}}} solution
2804
+ * @returns {{x: number, y: number, radius: number, setid: string}[]}}
2805
+ */
2806
+ function fromObjectNotation(solution) {
2807
+ const setids = Object.keys(solution);
2808
+ return setids.map((id) => Object.assign(solution[id], { setid: id }));
2809
+ }
2810
+
2811
+ /**
2812
+ *
2813
+ * @param {{x: number, y: number}} current
2814
+ * @param {ReadonlyArray<{x: number, y: number}>} interior
2815
+ * @param {ReadonlyArray<{x: number, y: number}>} exterior
2816
+ * @returns {number}
2817
+ */
2818
+ function circleMargin(current, interior, exterior) {
2819
+ let margin = interior[0].radius - distance(interior[0], current);
2820
+
2821
+ for (let i = 1; i < interior.length; ++i) {
2822
+ const m = interior[i].radius - distance(interior[i], current);
2823
+ if (m <= margin) {
2824
+ margin = m;
2825
+ }
2826
+ }
2827
+
2828
+ for (let i = 0; i < exterior.length; ++i) {
2829
+ const m = distance(exterior[i], current) - exterior[i].radius;
2830
+ if (m <= margin) {
2831
+ margin = m;
2832
+ }
2833
+ }
2834
+ return margin;
2835
+ }
2836
+
2837
+ /**
2838
+ * compute the center of some circles by maximizing the margin of
2839
+ * the center point relative to the circles (interior) after subtracting
2840
+ * nearby circles (exterior)
2841
+ * @param {readonly {x: number, y: number, radius: number}[]} interior
2842
+ * @param {readonly {x: number, y: number, radius: number}[]} exterior
2843
+ * @param {boolean} symmetricalTextCentre
2844
+ * @returns {{x:number, y: number}}
2845
+ */
2846
+ function computeTextCentre(interior, exterior, symmetricalTextCentre) {
2847
+ // get an initial estimate by sampling around the interior circles
2848
+ // and taking the point with the biggest margin
2849
+ /** @type {{x: number, y: number}[]} */
2850
+ const points = [];
2851
+ for (const c of interior) {
2852
+ points.push({ x: c.x, y: c.y });
2853
+ points.push({ x: c.x + c.radius / 2, y: c.y });
2854
+ points.push({ x: c.x - c.radius / 2, y: c.y });
2855
+ points.push({ x: c.x, y: c.y + c.radius / 2 });
2856
+ points.push({ x: c.x, y: c.y - c.radius / 2 });
2857
+ }
2858
+
2859
+ let initial = points[0];
2860
+ let margin = circleMargin(points[0], interior, exterior);
2861
+
2862
+ for (let i = 1; i < points.length; ++i) {
2863
+ const m = circleMargin(points[i], interior, exterior);
2864
+ if (m >= margin) {
2865
+ initial = points[i];
2866
+ margin = m;
2867
+ }
2868
+ }
2869
+
2870
+ // maximize the margin numerically
2871
+ const solution = nelderMead(
2872
+ (p) => -1 * circleMargin({ x: p[0], y: p[1] }, interior, exterior),
2873
+ [initial.x, initial.y],
2874
+ { maxIterations: 500, minErrorDelta: 1e-10 }
2875
+ ).x;
2876
+
2877
+ const ret = { x: symmetricalTextCentre ? 0 : solution[0], y: solution[1] };
2878
+
2879
+ // check solution, fallback as needed (happens if fully overlapped
2880
+ // etc)
2881
+ let valid = true;
2882
+ for (const i of interior) {
2883
+ if (distance(ret, i) > i.radius) {
2884
+ valid = false;
2885
+ break;
2886
+ }
2887
+ }
2888
+
2889
+ for (const e of exterior) {
2890
+ if (distance(ret, e) < e.radius) {
2891
+ valid = false;
2892
+ break;
2893
+ }
2894
+ }
2895
+ if (valid) {
2896
+ return ret;
2897
+ }
2898
+
2899
+ if (interior.length == 1) {
2900
+ return { x: interior[0].x, y: interior[0].y };
2901
+ }
2902
+ const areaStats = {};
2903
+ intersectionArea(interior, areaStats);
2904
+
2905
+ if (areaStats.arcs.length === 0) {
2906
+ return { x: 0, y: -1000, disjoint: true };
2907
+ }
2908
+ if (areaStats.arcs.length == 1) {
2909
+ return { x: areaStats.arcs[0].circle.x, y: areaStats.arcs[0].circle.y };
2910
+ }
2911
+ if (exterior.length) {
2912
+ // try again without other circles
2913
+ return computeTextCentre(interior, []);
2914
+ }
2915
+ // take average of all the points in the intersection
2916
+ // polygon. this should basically never happen
2917
+ // and has some issues:
2918
+ // https://github.com/benfred/venn.js/issues/48#issuecomment-146069777
2919
+ return getCenter(areaStats.arcs.map((a) => a.p1));
2920
+ }
2921
+
2922
+ // given a dictionary of {setid : circle}, returns
2923
+ // a dictionary of setid to list of circles that completely overlap it
2924
+ function getOverlappingCircles(circles) {
2925
+ const ret = {};
2926
+ const circleids = Object.keys(circles);
2927
+ for (const circleid of circleids) {
2928
+ ret[circleid] = [];
2929
+ }
2930
+ for (let i = 0; i < circleids.length; i++) {
2931
+ const ci = circleids[i];
2932
+ const a = circles[ci];
2933
+ for (let j = i + 1; j < circleids.length; ++j) {
2934
+ const cj = circleids[j];
2935
+ const b = circles[cj];
2936
+ const d = distance(a, b);
2937
+
2938
+ if (d + b.radius <= a.radius + 1e-10) {
2939
+ ret[cj].push(ci);
2940
+ } else if (d + a.radius <= b.radius + 1e-10) {
2941
+ ret[ci].push(cj);
2942
+ }
2943
+ }
2944
+ }
2945
+ return ret;
2946
+ }
2947
+
2948
+ function computeTextCentres(circles, areas, symmetricalTextCentre) {
2949
+ const ret = {};
2950
+ const overlapped = getOverlappingCircles(circles);
2951
+ for (let i = 0; i < areas.length; ++i) {
2952
+ const area = areas[i].sets;
2953
+ const areaids = {};
2954
+ const exclude = {};
2955
+
2956
+ for (let j = 0; j < area.length; ++j) {
2957
+ areaids[area[j]] = true;
2958
+ const overlaps = overlapped[area[j]];
2959
+ // keep track of any circles that overlap this area,
2960
+ // and don't consider for purposes of computing the text
2961
+ // centre
2962
+ for (let k = 0; k < overlaps.length; ++k) {
2963
+ exclude[overlaps[k]] = true;
2964
+ }
2965
+ }
2966
+
2967
+ const interior = [];
2968
+ const exterior = [];
2969
+ for (let setid in circles) {
2970
+ if (setid in areaids) {
2971
+ interior.push(circles[setid]);
2972
+ } else if (!(setid in exclude)) {
2973
+ exterior.push(circles[setid]);
2974
+ }
2975
+ }
2976
+ const centre = computeTextCentre(interior, exterior, symmetricalTextCentre);
2977
+ ret[area] = centre;
2978
+ if (centre.disjoint && areas[i].size > 0) {
2979
+ console.log('WARNING: area ' + area + ' not represented on screen');
2980
+ }
2981
+ }
2982
+ return ret;
2983
+ }
2984
+
2985
+ /**
2986
+ * @param {number} x
2987
+ * @param {number} y
2988
+ * @param {number} r
2989
+ * @returns {string}
2990
+ */
2991
+ function circlePath(x, y, r) {
2992
+ const ret = [];
2993
+ ret.push('\nM', x, y);
2994
+ ret.push('\nm', -r, 0);
2995
+ ret.push('\na', r, r, 0, 1, 0, r * 2, 0);
2996
+ ret.push('\na', r, r, 0, 1, 0, -r * 2, 0);
2997
+ return ret.join(' ');
2998
+ }
2999
+
3000
+ function intersectionAreaArcs(circles) {
3001
+ if (circles.length === 0) {
3002
+ return [];
3003
+ }
3004
+ const stats = {};
3005
+ intersectionArea(circles, stats);
3006
+ return stats.arcs;
3007
+ }
3008
+
3009
+ function arcsToPath(arcs, round) {
3010
+ if (arcs.length === 0) {
3011
+ return 'M 0 0';
3012
+ }
3013
+ const rFactor = Math.pow(10, round || 0);
3014
+ const r = round != null ? (v) => Math.round(v * rFactor) / rFactor : (v) => v;
3015
+ if (arcs.length == 1) {
3016
+ const circle = arcs[0].circle;
3017
+ return circlePath(r(circle.x), r(circle.y), r(circle.radius));
3018
+ }
3019
+ // draw path around arcs
3020
+ const ret = ['\nM', r(arcs[0].p2.x), r(arcs[0].p2.y)];
3021
+ for (const arc of arcs) {
3022
+ const radius = r(arc.circle.radius);
3023
+ ret.push('\nA', radius, radius, 0, arc.large ? 1 : 0, arc.sweep ? 1 : 0, r(arc.p1.x), r(arc.p1.y));
3024
+ }
3025
+ return ret.join(' ');
3026
+ }
3027
+
3028
+ function layout(data, options = {}) {
3029
+ const {
3030
+ lossFunction: loss,
3031
+ layoutFunction: layout = venn,
3032
+ normalize = true,
3033
+ orientation = Math.PI / 2,
3034
+ orientationOrder,
3035
+ width = 600,
3036
+ height = 350,
3037
+ padding = 15,
3038
+ scaleToFit = false,
3039
+ symmetricalTextCentre = false,
3040
+ distinct,
3041
+ round = 2,
3042
+ } = options;
3043
+
3044
+ let solution = layout(data, {
3045
+ lossFunction: loss === 'default' || !loss ? lossFunction : loss === 'logRatio' ? logRatioLossFunction : loss,
3046
+ distinct,
3047
+ });
3048
+
3049
+ if (normalize) {
3050
+ solution = normalizeSolution(solution, orientation, orientationOrder);
3051
+ }
3052
+
3053
+ const circles = scaleSolution(solution, width, height, padding, scaleToFit);
3054
+ const textCentres = computeTextCentres(circles, data, symmetricalTextCentre);
3055
+
3056
+ const circleLookup = new Map(
3057
+ Object.keys(circles).map((set) => [
3058
+ set,
3059
+ {
3060
+ set,
3061
+ x: circles[set].x,
3062
+ y: circles[set].y,
3063
+ radius: circles[set].radius,
3064
+ },
3065
+ ])
3066
+ );
3067
+ const helpers = data.map((area) => {
3068
+ const circles = area.sets.map((s) => circleLookup.get(s));
3069
+ const arcs = intersectionAreaArcs(circles);
3070
+ const path = arcsToPath(arcs, round);
3071
+ return { circles, arcs, path, area, has: new Set(area.sets) };
3072
+ });
3073
+
3074
+ function genDistinctPath(sets) {
3075
+ let r = '';
3076
+ for (const e of helpers) {
3077
+ if (e.has.size > sets.length && sets.every((s) => e.has.has(s))) {
3078
+ r += ' ' + e.path;
3079
+ }
3080
+ }
3081
+ return r;
3082
+ }
3083
+
3084
+ return helpers.map(({ circles, arcs, path, area }) => {
3085
+ return {
3086
+ data: area,
3087
+ text: textCentres[area.sets],
3088
+ circles,
3089
+ arcs,
3090
+ path,
3091
+ distinctPath: path + genDistinctPath(area.sets),
3092
+ };
3093
+ });
3094
+ }
3095
+
1398
3096
  const useVennLayout = (data, options = {}) => {
1399
3097
  const {
1400
3098
  width = 600,
@@ -1414,11 +3112,11 @@ const useVennLayout = (data, options = {}) => {
1414
3112
  }));
1415
3113
  return [...sets, ...intersections];
1416
3114
  }, [data]);
1417
- const layout = require$$0.useMemo(() => {
3115
+ const layout$1 = require$$0.useMemo(() => {
1418
3116
  try {
1419
3117
  setIsLoading(true);
1420
3118
  setError(null);
1421
- const computed = venn__namespace.layout(vennData, {
3119
+ const computed = layout(vennData, {
1422
3120
  width: width - padding * 2,
1423
3121
  height: height - padding * 2
1424
3122
  });
@@ -1439,7 +3137,7 @@ const useVennLayout = (data, options = {}) => {
1439
3137
  }
1440
3138
  }, [vennData, width, height, padding]);
1441
3139
  const paths = require$$0.useMemo(() => {
1442
- return layout.map(circle => {
3140
+ return layout$1.map(circle => {
1443
3141
  const {
1444
3142
  x,
1445
3143
  y,
@@ -1447,10 +3145,10 @@ const useVennLayout = (data, options = {}) => {
1447
3145
  } = circle;
1448
3146
  return `M ${x - radius} ${y} A ${radius} ${radius} 0 1 0 ${x + radius} ${y} A ${radius} ${radius} 0 1 0 ${x - radius} ${y}`;
1449
3147
  });
1450
- }, [layout]);
3148
+ }, [layout$1]);
1451
3149
  const textPositions = require$$0.useMemo(() => {
1452
3150
  try {
1453
- const computed = venn__namespace.layout(vennData, {
3151
+ const computed = layout(vennData, {
1454
3152
  width: width - padding * 2,
1455
3153
  height: height - padding * 2
1456
3154
  });
@@ -1464,7 +3162,7 @@ const useVennLayout = (data, options = {}) => {
1464
3162
  }
1465
3163
  }, [vennData, width, height, padding]);
1466
3164
  return {
1467
- layout,
3165
+ layout: layout$1,
1468
3166
  paths,
1469
3167
  textPositions,
1470
3168
  error,