Spaces:
Running
Running
File size: 5,700 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | import { Color } from '../../math/Color.js';
import { Path } from './Path.js';
import { Shape } from './Shape.js';
import { ShapeUtils } from '../ShapeUtils.js';
class ShapePath {
constructor() {
this.type = 'ShapePath';
this.color = new Color();
this.subPaths = [];
this.currentPath = null;
}
moveTo(x, y) {
this.currentPath = new Path();
this.subPaths.push(this.currentPath);
this.currentPath.moveTo(x, y);
return this;
}
lineTo(x, y) {
this.currentPath.lineTo(x, y);
return this;
}
quadraticCurveTo(aCPx, aCPy, aX, aY) {
this.currentPath.quadraticCurveTo(aCPx, aCPy, aX, aY);
return this;
}
bezierCurveTo(aCP1x, aCP1y, aCP2x, aCP2y, aX, aY) {
this.currentPath.bezierCurveTo(aCP1x, aCP1y, aCP2x, aCP2y, aX, aY);
return this;
}
splineThru(pts) {
this.currentPath.splineThru(pts);
return this;
}
toShapes(isCCW, noHoles) {
function toShapesNoHoles(inSubpaths) {
const shapes = [];
for (let i = 0, l = inSubpaths.length; i < l; i++) {
const tmpPath = inSubpaths[i];
const tmpShape = new Shape();
tmpShape.curves = tmpPath.curves;
shapes.push(tmpShape);
}
return shapes;
}
function isPointInsidePolygon(inPt, inPolygon) {
const polyLen = inPolygon.length;
// inPt on polygon contour => immediate success or
// toggling of inside/outside at every single! intersection point of an edge
// with the horizontal line through inPt, left of inPt
// not counting lowerY endpoints of edges and whole edges on that line
let inside = false;
for (let p = polyLen - 1, q = 0; q < polyLen; p = q++) {
let edgeLowPt = inPolygon[p];
let edgeHighPt = inPolygon[q];
let edgeDx = edgeHighPt.x - edgeLowPt.x;
let edgeDy = edgeHighPt.y - edgeLowPt.y;
if (Math.abs(edgeDy) > Number.EPSILON) {
// not parallel
if (edgeDy < 0) {
edgeLowPt = inPolygon[q];
edgeDx = -edgeDx;
edgeHighPt = inPolygon[p];
edgeDy = -edgeDy;
}
if (inPt.y < edgeLowPt.y || inPt.y > edgeHighPt.y) continue;
if (inPt.y === edgeLowPt.y) {
if (inPt.x === edgeLowPt.x) return true; // inPt is on contour ?
// continue; // no intersection or edgeLowPt => doesn't count !!!
} else {
const perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y);
if (perpEdge === 0) return true; // inPt is on contour ?
if (perpEdge < 0) continue;
inside = !inside; // true intersection left of inPt
}
} else {
// parallel or collinear
if (inPt.y !== edgeLowPt.y) continue; // parallel
// edge lies on the same horizontal line as inPt
if ((edgeHighPt.x <= inPt.x && inPt.x <= edgeLowPt.x) || (edgeLowPt.x <= inPt.x && inPt.x <= edgeHighPt.x)) return true; // inPt: Point on contour !
// continue;
}
}
return inside;
}
const isClockWise = ShapeUtils.isClockWise;
const subPaths = this.subPaths;
if (subPaths.length === 0) return [];
if (noHoles === true) return toShapesNoHoles(subPaths);
let solid, tmpPath, tmpShape;
const shapes = [];
if (subPaths.length === 1) {
tmpPath = subPaths[0];
tmpShape = new Shape();
tmpShape.curves = tmpPath.curves;
shapes.push(tmpShape);
return shapes;
}
let holesFirst = !isClockWise(subPaths[0].getPoints());
holesFirst = isCCW ? !holesFirst : holesFirst;
// console.log("Holes first", holesFirst);
const betterShapeHoles = [];
const newShapes = [];
let newShapeHoles = [];
let mainIdx = 0;
let tmpPoints;
newShapes[mainIdx] = undefined;
newShapeHoles[mainIdx] = [];
for (let i = 0, l = subPaths.length; i < l; i++) {
tmpPath = subPaths[i];
tmpPoints = tmpPath.getPoints();
solid = isClockWise(tmpPoints);
solid = isCCW ? !solid : solid;
if (solid) {
if (!holesFirst && newShapes[mainIdx]) mainIdx++;
newShapes[mainIdx] = { s: new Shape(), p: tmpPoints };
newShapes[mainIdx].s.curves = tmpPath.curves;
if (holesFirst) mainIdx++;
newShapeHoles[mainIdx] = [];
//console.log('cw', i);
} else {
newShapeHoles[mainIdx].push({ h: tmpPath, p: tmpPoints[0] });
//console.log('ccw', i);
}
}
// only Holes? -> probably all Shapes with wrong orientation
if (!newShapes[0]) return toShapesNoHoles(subPaths);
if (newShapes.length > 1) {
let ambiguous = false;
const toChange = [];
for (let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx++) {
betterShapeHoles[sIdx] = [];
}
for (let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx++) {
const sho = newShapeHoles[sIdx];
for (let hIdx = 0; hIdx < sho.length; hIdx++) {
const ho = sho[hIdx];
let hole_unassigned = true;
for (let s2Idx = 0; s2Idx < newShapes.length; s2Idx++) {
if (isPointInsidePolygon(ho.p, newShapes[s2Idx].p)) {
if (sIdx !== s2Idx) toChange.push({ froms: sIdx, tos: s2Idx, hole: hIdx });
if (hole_unassigned) {
hole_unassigned = false;
betterShapeHoles[s2Idx].push(ho);
} else {
ambiguous = true;
}
}
}
if (hole_unassigned) {
betterShapeHoles[sIdx].push(ho);
}
}
}
// console.log("ambiguous: ", ambiguous);
if (toChange.length > 0) {
// console.log("to change: ", toChange);
if (!ambiguous) newShapeHoles = betterShapeHoles;
}
}
let tmpHoles;
for (let i = 0, il = newShapes.length; i < il; i++) {
tmpShape = newShapes[i].s;
shapes.push(tmpShape);
tmpHoles = newShapeHoles[i];
for (let j = 0, jl = tmpHoles.length; j < jl; j++) {
tmpShape.holes.push(tmpHoles[j].h);
}
}
//console.log("shape", shapes);
return shapes;
}
}
export { ShapePath };
|