| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.clipEdges = clipEdges;
|
|---|
| 7 | exports.createBorderEdge = createBorderEdge;
|
|---|
| 8 | exports.createEdge = createEdge;
|
|---|
| 9 | exports.setEdgeEnd = setEdgeEnd;
|
|---|
| 10 | var _Diagram = require("./Diagram");
|
|---|
| 11 | function createEdge(left, right, v0, v1) {
|
|---|
| 12 | var edge = [null, null],
|
|---|
| 13 | index = _Diagram.edges.push(edge) - 1;
|
|---|
| 14 | edge.left = left;
|
|---|
| 15 | edge.right = right;
|
|---|
| 16 | if (v0) setEdgeEnd(edge, left, right, v0);
|
|---|
| 17 | if (v1) setEdgeEnd(edge, right, left, v1);
|
|---|
| 18 | _Diagram.cells[left.index].halfedges.push(index);
|
|---|
| 19 | _Diagram.cells[right.index].halfedges.push(index);
|
|---|
| 20 | return edge;
|
|---|
| 21 | }
|
|---|
| 22 | function createBorderEdge(left, v0, v1) {
|
|---|
| 23 | var edge = [v0, v1];
|
|---|
| 24 | edge.left = left;
|
|---|
| 25 | return edge;
|
|---|
| 26 | }
|
|---|
| 27 | function setEdgeEnd(edge, left, right, vertex) {
|
|---|
| 28 | if (!edge[0] && !edge[1]) {
|
|---|
| 29 | edge[0] = vertex;
|
|---|
| 30 | edge.left = left;
|
|---|
| 31 | edge.right = right;
|
|---|
| 32 | } else if (edge.left === right) {
|
|---|
| 33 | edge[1] = vertex;
|
|---|
| 34 | } else {
|
|---|
| 35 | edge[0] = vertex;
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // Liang–Barsky line clipping.
|
|---|
| 40 | function clipEdge(edge, x0, y0, x1, y1) {
|
|---|
| 41 | var a = edge[0],
|
|---|
| 42 | b = edge[1],
|
|---|
| 43 | ax = a[0],
|
|---|
| 44 | ay = a[1],
|
|---|
| 45 | bx = b[0],
|
|---|
| 46 | by = b[1],
|
|---|
| 47 | t0 = 0,
|
|---|
| 48 | t1 = 1,
|
|---|
| 49 | dx = bx - ax,
|
|---|
| 50 | dy = by - ay,
|
|---|
| 51 | r;
|
|---|
| 52 | r = x0 - ax;
|
|---|
| 53 | if (!dx && r > 0) return;
|
|---|
| 54 | r /= dx;
|
|---|
| 55 | if (dx < 0) {
|
|---|
| 56 | if (r < t0) return;
|
|---|
| 57 | if (r < t1) t1 = r;
|
|---|
| 58 | } else if (dx > 0) {
|
|---|
| 59 | if (r > t1) return;
|
|---|
| 60 | if (r > t0) t0 = r;
|
|---|
| 61 | }
|
|---|
| 62 | r = x1 - ax;
|
|---|
| 63 | if (!dx && r < 0) return;
|
|---|
| 64 | r /= dx;
|
|---|
| 65 | if (dx < 0) {
|
|---|
| 66 | if (r > t1) return;
|
|---|
| 67 | if (r > t0) t0 = r;
|
|---|
| 68 | } else if (dx > 0) {
|
|---|
| 69 | if (r < t0) return;
|
|---|
| 70 | if (r < t1) t1 = r;
|
|---|
| 71 | }
|
|---|
| 72 | r = y0 - ay;
|
|---|
| 73 | if (!dy && r > 0) return;
|
|---|
| 74 | r /= dy;
|
|---|
| 75 | if (dy < 0) {
|
|---|
| 76 | if (r < t0) return;
|
|---|
| 77 | if (r < t1) t1 = r;
|
|---|
| 78 | } else if (dy > 0) {
|
|---|
| 79 | if (r > t1) return;
|
|---|
| 80 | if (r > t0) t0 = r;
|
|---|
| 81 | }
|
|---|
| 82 | r = y1 - ay;
|
|---|
| 83 | if (!dy && r < 0) return;
|
|---|
| 84 | r /= dy;
|
|---|
| 85 | if (dy < 0) {
|
|---|
| 86 | if (r > t1) return;
|
|---|
| 87 | if (r > t0) t0 = r;
|
|---|
| 88 | } else if (dy > 0) {
|
|---|
| 89 | if (r < t0) return;
|
|---|
| 90 | if (r < t1) t1 = r;
|
|---|
| 91 | }
|
|---|
| 92 | if (!(t0 > 0) && !(t1 < 1)) return true; // TODO Better check?
|
|---|
| 93 |
|
|---|
| 94 | if (t0 > 0) edge[0] = [ax + t0 * dx, ay + t0 * dy];
|
|---|
| 95 | if (t1 < 1) edge[1] = [ax + t1 * dx, ay + t1 * dy];
|
|---|
| 96 | return true;
|
|---|
| 97 | }
|
|---|
| 98 | function connectEdge(edge, x0, y0, x1, y1) {
|
|---|
| 99 | var v1 = edge[1];
|
|---|
| 100 | if (v1) return true;
|
|---|
| 101 | var v0 = edge[0],
|
|---|
| 102 | left = edge.left,
|
|---|
| 103 | right = edge.right,
|
|---|
| 104 | lx = left[0],
|
|---|
| 105 | ly = left[1],
|
|---|
| 106 | rx = right[0],
|
|---|
| 107 | ry = right[1],
|
|---|
| 108 | fx = (lx + rx) / 2,
|
|---|
| 109 | fy = (ly + ry) / 2,
|
|---|
| 110 | fm,
|
|---|
| 111 | fb;
|
|---|
| 112 | if (ry === ly) {
|
|---|
| 113 | if (fx < x0 || fx >= x1) return;
|
|---|
| 114 | if (lx > rx) {
|
|---|
| 115 | if (!v0) v0 = [fx, y0];else if (v0[1] >= y1) return;
|
|---|
| 116 | v1 = [fx, y1];
|
|---|
| 117 | } else {
|
|---|
| 118 | if (!v0) v0 = [fx, y1];else if (v0[1] < y0) return;
|
|---|
| 119 | v1 = [fx, y0];
|
|---|
| 120 | }
|
|---|
| 121 | } else {
|
|---|
| 122 | fm = (lx - rx) / (ry - ly);
|
|---|
| 123 | fb = fy - fm * fx;
|
|---|
| 124 | if (fm < -1 || fm > 1) {
|
|---|
| 125 | if (lx > rx) {
|
|---|
| 126 | if (!v0) v0 = [(y0 - fb) / fm, y0];else if (v0[1] >= y1) return;
|
|---|
| 127 | v1 = [(y1 - fb) / fm, y1];
|
|---|
| 128 | } else {
|
|---|
| 129 | if (!v0) v0 = [(y1 - fb) / fm, y1];else if (v0[1] < y0) return;
|
|---|
| 130 | v1 = [(y0 - fb) / fm, y0];
|
|---|
| 131 | }
|
|---|
| 132 | } else {
|
|---|
| 133 | if (ly < ry) {
|
|---|
| 134 | if (!v0) v0 = [x0, fm * x0 + fb];else if (v0[0] >= x1) return;
|
|---|
| 135 | v1 = [x1, fm * x1 + fb];
|
|---|
| 136 | } else {
|
|---|
| 137 | if (!v0) v0 = [x1, fm * x1 + fb];else if (v0[0] < x0) return;
|
|---|
| 138 | v1 = [x0, fm * x0 + fb];
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | edge[0] = v0;
|
|---|
| 143 | edge[1] = v1;
|
|---|
| 144 | return true;
|
|---|
| 145 | }
|
|---|
| 146 | function clipEdges(x0, y0, x1, y1) {
|
|---|
| 147 | var i = _Diagram.edges.length,
|
|---|
| 148 | edge;
|
|---|
| 149 | while (i--) {
|
|---|
| 150 | if (!connectEdge(edge = _Diagram.edges[i], x0, y0, x1, y1) || !clipEdge(edge, x0, y0, x1, y1) || !(Math.abs(edge[0][0] - edge[1][0]) > _Diagram.epsilon || Math.abs(edge[0][1] - edge[1][1]) > _Diagram.epsilon)) {
|
|---|
| 151 | delete _Diagram.edges[i];
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | } |
|---|