plotboilerplate 1.25.0 → 1.27.0
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/README.md +3 -0
- package/dist/index.esm.js +171 -43
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/plotboilerplate.browser.min.js +1 -1
- package/dist/plotboilerplate.browser.min.js.map +1 -1
- package/dist/plotboilerplate.js +171 -43
- package/dist/plotboilerplate.min.js +1 -1
- package/package.json +6 -2
- package/src/cjs/Bounds.d.ts +79 -25
- package/src/cjs/Bounds.js +102 -29
- package/src/cjs/Bounds.js.map +1 -1
- package/src/cjs/CircleSector.d.ts +3 -0
- package/src/cjs/CircleSector.js +28 -1
- package/src/cjs/CircleSector.js.map +1 -1
- package/src/cjs/PlotBoilerplate.d.ts +4 -3
- package/src/cjs/PlotBoilerplate.js +2 -1
- package/src/cjs/PlotBoilerplate.js.map +1 -1
- package/src/cjs/draw.d.ts +4 -2
- package/src/cjs/draw.js +9 -4
- package/src/cjs/draw.js.map +1 -1
- package/src/cjs/drawgl.d.ts +3 -2
- package/src/cjs/drawgl.js +2 -1
- package/src/cjs/drawgl.js.map +1 -1
- package/src/cjs/drawutilssvg.d.ts +10 -4
- package/src/cjs/drawutilssvg.js +28 -7
- package/src/cjs/drawutilssvg.js.map +1 -1
- package/src/cjs/interfaces/DrawLib.d.ts +2 -0
- package/src/cjs/interfaces/DrawLib.js +1 -0
- package/src/cjs/interfaces/DrawLib.js.map +1 -1
- package/src/cjs/utils/LissajousFigure.d.ts +34 -0
- package/src/cjs/utils/LissajousFigure.js +100 -0
- package/src/cjs/utils/LissajousFigure.js.map +1 -0
- package/src/cjs/utils/PerlinNoise.d.ts +19 -0
- package/src/cjs/utils/PerlinNoise.js +352 -0
- package/src/cjs/utils/PerlinNoise.js.map +1 -0
- package/src/cjs/utils/SVGPathUtils.d.ts +22 -0
- package/src/cjs/utils/SVGPathUtils.js +45 -0
- package/src/cjs/utils/SVGPathUtils.js.map +1 -0
- package/src/cjs/utils/WebColors.d.ts +2 -1
- package/src/cjs/utils/WebColors.js +14 -25
- package/src/cjs/utils/WebColors.js.map +1 -1
- package/src/cjs/utils/algorithms/PolygonTesselationOutlines.d.ts +145 -0
- package/src/cjs/utils/algorithms/PolygonTesselationOutlines.js +269 -0
- package/src/cjs/utils/algorithms/PolygonTesselationOutlines.js.map +1 -0
- package/src/cjs/utils/algorithms/pixelCornersToRoundPaths.d.ts +23 -0
- package/src/cjs/utils/algorithms/pixelCornersToRoundPaths.js +270 -0
- package/src/cjs/utils/algorithms/pixelCornersToRoundPaths.js.map +1 -0
- package/src/cjs/utils/datastructures/Color.js +0 -19
- package/src/cjs/utils/datastructures/Color.js.map +1 -1
- package/src/cjs/utils/datastructures/ColorGradient.d.ts +2 -0
- package/src/cjs/utils/datastructures/ColorGradient.js +5 -0
- package/src/cjs/utils/datastructures/ColorGradient.js.map +1 -1
- package/src/cjs/utils/datastructures/DataGrid2d.d.ts +6 -0
- package/src/cjs/utils/datastructures/DataGrid2dArrayMatrix.d.ts +88 -0
- package/src/cjs/utils/datastructures/DataGrid2dArrayMatrix.js +155 -0
- package/src/cjs/utils/datastructures/DataGrid2dArrayMatrix.js.map +1 -0
- package/src/cjs/utils/datastructures/DataGrid2dListAdapter.d.ts +1 -7
- package/src/cjs/utils/datastructures/DataGrid2dListAdapter.js +3 -3
- package/src/cjs/utils/datastructures/DataGrid2dListAdapter.js.map +1 -1
- package/src/cjs/utils/datastructures/GeometryMesh.d.ts +49 -0
- package/src/cjs/utils/datastructures/GeometryMesh.js +58 -0
- package/src/cjs/utils/datastructures/GeometryMesh.js.map +1 -0
- package/src/cjs/utils/datastructures/Girih.d.ts +38 -1
- package/src/cjs/utils/datastructures/Girih.js +71 -1
- package/src/cjs/utils/datastructures/Girih.js.map +1 -1
- package/src/cjs/utils/detectDarkMode.js +16 -7
- package/src/cjs/utils/detectDarkMode.js.map +1 -1
- package/src/cjs/utils/dom/components/ColorGradientPicker.d.ts +4 -3
- package/src/cjs/utils/dom/components/ColorGradientPicker.js +8 -8
- package/src/cjs/utils/dom/components/ColorGradientPicker.js.map +1 -1
- package/src/cjs/utils/dom/components/ColorGradientSelector.d.ts +1 -0
- package/src/cjs/utils/dom/components/ColorGradientSelector.js +4 -2
- package/src/cjs/utils/dom/components/ColorGradientSelector.js.map +1 -1
- package/src/cjs/utils/io/FileDrop.d.ts +2 -0
- package/src/cjs/utils/io/FileDrop.js +4 -0
- package/src/cjs/utils/io/FileDrop.js.map +1 -1
- package/src/cjs/utils/parsers/svg/types.d.ts +8 -8
- package/src/esm/Bounds.d.ts +79 -25
- package/src/esm/Bounds.js +102 -29
- package/src/esm/Bounds.js.map +1 -1
- package/src/esm/CircleSector.d.ts +3 -0
- package/src/esm/CircleSector.js +28 -1
- package/src/esm/CircleSector.js.map +1 -1
- package/src/esm/PlotBoilerplate.d.ts +4 -3
- package/src/esm/PlotBoilerplate.js +2 -1
- package/src/esm/PlotBoilerplate.js.map +1 -1
- package/src/esm/draw.d.ts +4 -2
- package/src/esm/draw.js +9 -4
- package/src/esm/draw.js.map +1 -1
- package/src/esm/drawgl.d.ts +3 -2
- package/src/esm/drawgl.js +2 -1
- package/src/esm/drawgl.js.map +1 -1
- package/src/esm/drawutilssvg.d.ts +10 -4
- package/src/esm/drawutilssvg.js +28 -7
- package/src/esm/drawutilssvg.js.map +1 -1
- package/src/esm/interfaces/DrawLib.d.ts +2 -0
- package/src/esm/interfaces/DrawLib.js +1 -0
- package/src/esm/interfaces/DrawLib.js.map +1 -1
- package/src/esm/utils/LissajousFigure.d.ts +34 -0
- package/src/esm/utils/LissajousFigure.js +95 -0
- package/src/esm/utils/LissajousFigure.js.map +1 -0
- package/src/esm/utils/PerlinNoise.d.ts +19 -0
- package/src/esm/utils/PerlinNoise.js +346 -0
- package/src/esm/utils/PerlinNoise.js.map +1 -0
- package/src/esm/utils/SVGPathUtils.d.ts +22 -0
- package/src/esm/utils/SVGPathUtils.js +42 -0
- package/src/esm/utils/SVGPathUtils.js.map +1 -0
- package/src/esm/utils/WebColors.d.ts +2 -1
- package/src/esm/utils/WebColors.js +14 -25
- package/src/esm/utils/WebColors.js.map +1 -1
- package/src/esm/utils/algorithms/PolygonTesselationOutlines.d.ts +145 -0
- package/src/esm/utils/algorithms/PolygonTesselationOutlines.js +262 -0
- package/src/esm/utils/algorithms/PolygonTesselationOutlines.js.map +1 -0
- package/src/esm/utils/algorithms/pixelCornersToRoundPaths.d.ts +23 -0
- package/src/esm/utils/algorithms/pixelCornersToRoundPaths.js +266 -0
- package/src/esm/utils/algorithms/pixelCornersToRoundPaths.js.map +1 -0
- package/src/esm/utils/datastructures/Color.js +0 -19
- package/src/esm/utils/datastructures/Color.js.map +1 -1
- package/src/esm/utils/datastructures/ColorGradient.d.ts +2 -0
- package/src/esm/utils/datastructures/ColorGradient.js +5 -0
- package/src/esm/utils/datastructures/ColorGradient.js.map +1 -1
- package/src/esm/utils/datastructures/DataGrid2d.d.ts +6 -0
- package/src/esm/utils/datastructures/DataGrid2dArrayMatrix.d.ts +88 -0
- package/src/esm/utils/datastructures/DataGrid2dArrayMatrix.js +150 -0
- package/src/esm/utils/datastructures/DataGrid2dArrayMatrix.js.map +1 -0
- package/src/esm/utils/datastructures/DataGrid2dListAdapter.d.ts +1 -7
- package/src/esm/utils/datastructures/DataGrid2dListAdapter.js +3 -3
- package/src/esm/utils/datastructures/DataGrid2dListAdapter.js.map +1 -1
- package/src/esm/utils/datastructures/GeometryMesh.d.ts +49 -0
- package/src/esm/utils/datastructures/GeometryMesh.js +53 -0
- package/src/esm/utils/datastructures/GeometryMesh.js.map +1 -0
- package/src/esm/utils/datastructures/Girih.d.ts +38 -1
- package/src/esm/utils/datastructures/Girih.js +71 -1
- package/src/esm/utils/datastructures/Girih.js.map +1 -1
- package/src/esm/utils/detectDarkMode.js +16 -7
- package/src/esm/utils/detectDarkMode.js.map +1 -1
- package/src/esm/utils/dom/components/ColorGradientPicker.d.ts +4 -3
- package/src/esm/utils/dom/components/ColorGradientPicker.js +18 -7
- package/src/esm/utils/dom/components/ColorGradientPicker.js.map +1 -1
- package/src/esm/utils/dom/components/ColorGradientSelector.d.ts +1 -0
- package/src/esm/utils/dom/components/ColorGradientSelector.js +9 -2
- package/src/esm/utils/dom/components/ColorGradientSelector.js.map +1 -1
- package/src/esm/utils/io/FileDrop.d.ts +2 -0
- package/src/esm/utils/io/FileDrop.js +4 -0
- package/src/esm/utils/io/FileDrop.js.map +1 -1
- package/src/esm/utils/parsers/svg/types.d.ts +8 -8
- package/src/ts/Bounds.ts +107 -22
- package/src/ts/CircleSector.ts +39 -6
- package/src/ts/PlotBoilerplate.ts +4 -3
- package/src/ts/draw.ts +8 -3
- package/src/ts/drawgl.ts +3 -2
- package/src/ts/drawutilssvg.ts +34 -7
- package/src/ts/interfaces/DrawLib.ts +3 -0
- package/src/ts/utils/LissajousFigure.ts +110 -0
- package/src/ts/utils/PerlinNoise.ts +368 -0
- package/src/ts/utils/SVGPathUtils.ts +55 -0
- package/src/ts/utils/WebColors.ts +20 -33
- package/src/ts/utils/algorithms/PolygonTesselationOutlines.ts +296 -0
- package/src/ts/utils/algorithms/pixelCornersToRoundPaths.ts +379 -0
- package/src/ts/utils/datastructures/Color.ts +0 -19
- package/src/ts/utils/datastructures/ColorGradient.ts +6 -0
- package/src/ts/utils/datastructures/DataGrid2d.ts +7 -0
- package/src/ts/utils/datastructures/DataGrid2dArrayMatrix.ts +182 -0
- package/src/ts/utils/datastructures/DataGrid2dListAdapter.ts +4 -11
- package/src/ts/utils/datastructures/GeometryMesh.ts +79 -0
- package/src/ts/utils/datastructures/Girih.ts +78 -1
- package/src/ts/utils/datastructures/interfaces.ts +4 -4
- package/src/ts/utils/detectDarkMode.ts +17 -8
- package/src/ts/utils/dom/components/ColorGradientPicker.tsx +18 -6
- package/src/ts/utils/dom/components/ColorGradientSelector.tsx +9 -2
- package/src/ts/utils/io/FileDrop.ts +5 -0
- package/src/ts/utils/parsers/svg/types.ts +8 -8
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A collection of SVG path generation tools.
|
|
3
|
+
*
|
|
4
|
+
* @author Ikaros Kappler
|
|
5
|
+
* @date 2025-04-04
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Circle } from "../Circle";
|
|
10
|
+
import { Vertex } from "../Vertex";
|
|
11
|
+
import { geomutils } from "../geomutils";
|
|
12
|
+
import { SVGPathParams } from "../interfaces";
|
|
13
|
+
|
|
14
|
+
export const SVGPathUtils = {
|
|
15
|
+
/**
|
|
16
|
+
* Creates an SVG shape resembling a circlular ring sector.
|
|
17
|
+
*
|
|
18
|
+
* @param {Vertex} center - The center position of the ring.
|
|
19
|
+
* @param {number} innerRadius - The inner radius of the ring.
|
|
20
|
+
* @param {number} outerRadius - The outer radius of the ring.
|
|
21
|
+
* @param {number} startAngle - The start angle of the ring.
|
|
22
|
+
* @param {number} endAngle - The end angle of the ring.
|
|
23
|
+
* @returns {SVGPathParams} SVG path parmams for a closed shape representing the circular ring sector.
|
|
24
|
+
*/
|
|
25
|
+
mkCircularRingSector: (
|
|
26
|
+
center: Vertex,
|
|
27
|
+
innerRadius: number,
|
|
28
|
+
outerRadius: number,
|
|
29
|
+
startAngle: number,
|
|
30
|
+
endAngle: number
|
|
31
|
+
): SVGPathParams => {
|
|
32
|
+
const innerStart: Vertex = Circle.circleUtils.vertAt(startAngle, innerRadius).add(center);
|
|
33
|
+
const innerEnd: Vertex = Circle.circleUtils.vertAt(endAngle, innerRadius).add(center);
|
|
34
|
+
const outerStart: Vertex = Circle.circleUtils.vertAt(startAngle, outerRadius).add(center);
|
|
35
|
+
const outerEnd: Vertex = Circle.circleUtils.vertAt(endAngle, outerRadius).add(center);
|
|
36
|
+
|
|
37
|
+
const buffer: SVGPathParams = ["M", innerStart.x, innerStart.y];
|
|
38
|
+
// var isLargeArcRequired = geomutils.mapAngleTo2PI(Math.abs(startAngle - endAngle)) >= Math.PI;
|
|
39
|
+
// var isLargeArcRequired = Math.abs(geomutils.mapAngleTo2PI(startAngle) - geomutils.mapAngleTo2PI(endAngle)) >= Math.PI;
|
|
40
|
+
const isLargeArcRequired =
|
|
41
|
+
geomutils.mapAngleTo2PI(geomutils.mapAngleTo2PI(startAngle) - geomutils.mapAngleTo2PI(endAngle)) >= Math.PI;
|
|
42
|
+
|
|
43
|
+
const rotation: number = 0.0;
|
|
44
|
+
const innerLargeArcFlag: number = isLargeArcRequired ? 0 : 1;
|
|
45
|
+
const innerSweepFlag: number = 1; // isLargeArcRequired ? 1 : 0; // 1;
|
|
46
|
+
const outerLargeArcFlag: number = isLargeArcRequired ? 0 : 1;
|
|
47
|
+
const outerSweepFlag: number = 0; // isLargeArcRequired ? 0 : 1; // 0;
|
|
48
|
+
buffer.push("A", innerRadius, innerRadius, rotation, innerLargeArcFlag, innerSweepFlag, innerEnd.x, innerEnd.y);
|
|
49
|
+
buffer.push("L", outerEnd.x, outerEnd.y);
|
|
50
|
+
buffer.push("A", outerRadius, outerRadius, rotation, outerLargeArcFlag, outerSweepFlag, outerStart.x, outerStart.y);
|
|
51
|
+
buffer.push("Z");
|
|
52
|
+
|
|
53
|
+
return buffer;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author Ikaros Kappler
|
|
3
|
-
* @version 1.0.1
|
|
4
3
|
* @date 2018-11-10
|
|
5
4
|
* @modified 2020-10-23 Ported to Typescript.
|
|
6
5
|
* @modified 2020-10-30 Exporting each color under its name globally.
|
|
6
|
+
* @modified 2025-12-08 Replaying self defined colors by official web colors.
|
|
7
|
+
* @version 1.0.2
|
|
7
8
|
**/
|
|
8
9
|
|
|
9
10
|
import { Color } from "./datastructures/Color";
|
|
10
11
|
|
|
11
|
-
export const Red
|
|
12
|
-
export const Pink
|
|
13
|
-
export const Purple
|
|
14
|
-
export const DeepPurple
|
|
15
|
-
export const Indigo
|
|
16
|
-
export const Blue
|
|
17
|
-
export const LightBlue
|
|
18
|
-
export const Cyan
|
|
19
|
-
export const Teal
|
|
20
|
-
export const Green
|
|
21
|
-
export const LightGreen
|
|
22
|
-
|
|
12
|
+
export const Red: Color = Color.Red; // Color.makeRGB(255,67,55);
|
|
13
|
+
export const Pink: Color = Color.Pink; // Color.makeRGB(232,31,100);
|
|
14
|
+
export const Purple: Color = Color.Purple; // Color.makeRGB(156,39,175);
|
|
15
|
+
export const DeepPurple: Color = Color.DeepPink; // Color.makeRGB(103,59,184);
|
|
16
|
+
export const Indigo: Color = Color.Indigo; // Color.makeRGB(64,81,181);
|
|
17
|
+
export const Blue: Color = Color.Blue; // Color.makeRGB(35,151,245);
|
|
18
|
+
export const LightBlue: Color = Color.LightBlue; // Color.makeRGB(6,170,245);
|
|
19
|
+
export const Cyan: Color = Color.Cyan; // Color.makeRGB(3,189,214);
|
|
20
|
+
export const Teal: Color = Color.Teal; // Color.makeRGB(1,150,137);
|
|
21
|
+
export const Green: Color = Color.Green; // Color.makeRGB(77,175,82);
|
|
22
|
+
export const LightGreen: Color = Color.LightGreen; // Color.makeRGB(141,195,67);
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* A set of beautiful web colors (I know, beauty is in the eye of the beholder).
|
|
@@ -30,28 +30,15 @@ export const LightGreen : Color = Color.makeRGB(141,195,67);
|
|
|
30
30
|
* @requires Color
|
|
31
31
|
*
|
|
32
32
|
*/
|
|
33
|
-
export const WebColors
|
|
34
|
-
Red,
|
|
35
|
-
Pink,
|
|
36
|
-
Purple,
|
|
37
|
-
DeepPurple,
|
|
38
|
-
Indigo,
|
|
39
|
-
Blue,
|
|
40
|
-
LightBlue,
|
|
41
|
-
Cyan,
|
|
42
|
-
Teal,
|
|
43
|
-
Green,
|
|
44
|
-
LightGreen
|
|
45
|
-
];
|
|
46
|
-
|
|
33
|
+
export const WebColors: Array<Color> = [Red, Pink, Purple, DeepPurple, Indigo, Blue, LightBlue, Cyan, Teal, Green, LightGreen];
|
|
47
34
|
|
|
48
35
|
/**
|
|
49
36
|
* A helper function to shuffle the colors into a new order.
|
|
50
37
|
*/
|
|
51
|
-
export const shuffleWebColors = (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
38
|
+
export const shuffleWebColors = (order: Array<number>) => {
|
|
39
|
+
const result: Array<Color> = Array(order.length);
|
|
40
|
+
for (var i = 0; i < order.length; i++) {
|
|
41
|
+
result[i] = WebColors[order[i] % WebColors.length];
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
57
44
|
};
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find the outer hull outlines of a (not necessarily full connected) polygon tesellation.
|
|
3
|
+
*
|
|
4
|
+
* The method `findAllOutlinesGraph` will return a simple graph containing all vertices and edges
|
|
5
|
+
* that belong to the outer hull.
|
|
6
|
+
*
|
|
7
|
+
* The method `findAllOutlinesPolygons` will detect connected paths inside the graph and return
|
|
8
|
+
* an array of Polygons.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* @require detectPaths
|
|
12
|
+
*
|
|
13
|
+
* @author Ikaros Kappler
|
|
14
|
+
* @date 2026-01-20
|
|
15
|
+
* @version 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { Line } from "../../Line";
|
|
19
|
+
import { Polygon } from "../../Polygon";
|
|
20
|
+
import { Vertex } from "../../Vertex";
|
|
21
|
+
import { PathSegment, XYCoords } from "../../interfaces";
|
|
22
|
+
import { GenericPath } from "../datastructures/GenericPath";
|
|
23
|
+
import { IndexPair } from "../datastructures/interfaces";
|
|
24
|
+
import { arrayFill } from "./arrayFill";
|
|
25
|
+
import { detectPaths } from "./detectPaths";
|
|
26
|
+
|
|
27
|
+
export interface PolygonTesselationOutlinesOptions {
|
|
28
|
+
tolerance?: number;
|
|
29
|
+
removeUnusedVertices?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface TesselationGraph {
|
|
33
|
+
vertices: Array<Vertex>;
|
|
34
|
+
edges: Array<IndexPair>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Convert any non-intersecting tesselation to a graph.
|
|
39
|
+
* Adjadent tiles (polygons) will result in duplicate edges.
|
|
40
|
+
*
|
|
41
|
+
* @param {Polygon[]} polygons - The polygon tesselation to use.
|
|
42
|
+
* @return {TesselationGraph} A tesselation graph.
|
|
43
|
+
*/
|
|
44
|
+
export const polygonTesselationToGraph = (polygons: Polygon[]): TesselationGraph => {
|
|
45
|
+
const vertices: Array<Vertex> = [];
|
|
46
|
+
const edges: Array<IndexPair> = [];
|
|
47
|
+
// const vertexPolygonMapping: Array<number>[] = []; // Map polygon+vertex to graph vertices
|
|
48
|
+
for (var i = 0; i < polygons.length; i++) {
|
|
49
|
+
const polygon = polygons[i];
|
|
50
|
+
const polygonMapping: Array<number> = [];
|
|
51
|
+
// vertexPolygonMapping.push(polygonMapping);
|
|
52
|
+
// Add vertices
|
|
53
|
+
for (var j = 0; j < polygon.vertices.length; j++) {
|
|
54
|
+
const graphVertIndex = vertices.length;
|
|
55
|
+
vertices.push(polygon.vertices[j]);
|
|
56
|
+
polygonMapping.push(graphVertIndex);
|
|
57
|
+
}
|
|
58
|
+
// Now add edges
|
|
59
|
+
for (var j = 0; j < polygon.vertices.length; j++) {
|
|
60
|
+
const polyEdgeIndexA = j;
|
|
61
|
+
const polyEdgeIndexB = (j + 1) % polygon.vertices.length;
|
|
62
|
+
const grapgVertIndexA = polygonMapping[polyEdgeIndexA];
|
|
63
|
+
const grapgVertIndexB = polygonMapping[polyEdgeIndexB];
|
|
64
|
+
edges.push({ i: grapgVertIndexA, j: grapgVertIndexB });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return { vertices, edges };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A class to calculate the tesselation's outer hull.
|
|
72
|
+
*/
|
|
73
|
+
export class PolygonTesselationOutlines {
|
|
74
|
+
public static readonly DEFAULT_TOLERANCE = 0.01;
|
|
75
|
+
|
|
76
|
+
private readonly polygons: Polygon[];
|
|
77
|
+
// private readonly tesselationGraph: TesselationGraph;
|
|
78
|
+
private readonly tolerance: number;
|
|
79
|
+
private readonly removeUnusedVertices: boolean;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @constructor
|
|
83
|
+
* @param {Polygon[]} polygons - The polygon tesselation to use.
|
|
84
|
+
* @param {PolygonTesselationOutlinesOptions} options - (optional)
|
|
85
|
+
*/
|
|
86
|
+
constructor(polygons: Polygon[], options?: PolygonTesselationOutlinesOptions) {
|
|
87
|
+
this.polygons = polygons;
|
|
88
|
+
// this.tesselationGraph = tesselationToGraph(polygons);
|
|
89
|
+
this.tolerance = options?.tolerance ?? PolygonTesselationOutlines.DEFAULT_TOLERANCE;
|
|
90
|
+
this.removeUnusedVertices = options?.removeUnusedVertices ?? true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Clears duplicate edges.
|
|
95
|
+
*
|
|
96
|
+
* Note: this works only for duplicates. Triplets will not be removed.
|
|
97
|
+
*
|
|
98
|
+
* @name __eliminateDuplicateEdges
|
|
99
|
+
* @private
|
|
100
|
+
* @memberof TesselationGraph
|
|
101
|
+
* @instance
|
|
102
|
+
* @param {TesselationGraph} tesselationGraph - The tesselation graph to remove duplicate edges from.
|
|
103
|
+
* @return {TesselationGraph} A new tesselation graph with cleaned up edges.
|
|
104
|
+
*/
|
|
105
|
+
private __eliminateDuplicateEdges(tesselationGraph: TesselationGraph): TesselationGraph {
|
|
106
|
+
const newEdges: Array<IndexPair> = [];
|
|
107
|
+
for (var e = 0; e < tesselationGraph.edges.length; e++) {
|
|
108
|
+
const curEdge: IndexPair = tesselationGraph.edges[e];
|
|
109
|
+
if (!curEdge) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const curA: Vertex = tesselationGraph.vertices[curEdge.i];
|
|
113
|
+
const curB: Vertex = tesselationGraph.vertices[curEdge.j];
|
|
114
|
+
for (var f = 0; f < tesselationGraph.edges.length; f++) {
|
|
115
|
+
if (e === f) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const otherEdge: IndexPair = tesselationGraph.edges[f];
|
|
119
|
+
if (!otherEdge) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const otherA: Vertex = tesselationGraph.vertices[otherEdge.i];
|
|
123
|
+
const otherB: Vertex = tesselationGraph.vertices[otherEdge.j];
|
|
124
|
+
if (this.__isEqualEdge(curA, curB, otherA, otherB)) {
|
|
125
|
+
// Both edges are equal -> remove both
|
|
126
|
+
tesselationGraph.edges[e] = null;
|
|
127
|
+
tesselationGraph.edges[f] = null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// return tesselationGraph; // TODO: create clean clone with new mapping
|
|
132
|
+
// Clear null-records.
|
|
133
|
+
return {
|
|
134
|
+
vertices: tesselationGraph.vertices,
|
|
135
|
+
edges: tesselationGraph.edges.filter((edge: IndexPair) => edge != null)
|
|
136
|
+
}; // TODO: create clean clone with new mapping
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Locate some edge that adjacent to the given vertex index
|
|
141
|
+
*
|
|
142
|
+
* @name __locateEdgeWithVertIndex
|
|
143
|
+
* @private
|
|
144
|
+
* @memberof TesselationGraph
|
|
145
|
+
* @instance
|
|
146
|
+
* @param {number} vertIndex - The vertex index to find an edge for.
|
|
147
|
+
* @param {TesselationGraph} tesselationGraph - The tesselation graph to remove unused vertices from.
|
|
148
|
+
* @return {number} The edge index that's adjacent to the given vertex index.
|
|
149
|
+
* @return -1 if no such edge is found.
|
|
150
|
+
*/
|
|
151
|
+
private __locateEdgeWithVertIndex(vertIndex: number, tesselationGraph: TesselationGraph): number {
|
|
152
|
+
return tesselationGraph.edges.findIndex((edge: IndexPair) => edge.i == vertIndex || edge.j == vertIndex);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Removes all unused vertices from the graph and re-matps the edges.
|
|
157
|
+
*
|
|
158
|
+
* @name __removeUnusedVertices
|
|
159
|
+
* @private
|
|
160
|
+
* @memberof TesselationGraph
|
|
161
|
+
* @instance
|
|
162
|
+
* @param {TesselationGraph} tesselationGraph - The tesselation graph to remove unused vertices from.
|
|
163
|
+
* @return {TesselationGraph} A new tesselation graph.
|
|
164
|
+
*/
|
|
165
|
+
private __removeUnusedVertices(tesselationGraph: TesselationGraph): TesselationGraph {
|
|
166
|
+
// First remove un-used vertices and re-map indices.
|
|
167
|
+
const newVertices: Array<Vertex> = [];
|
|
168
|
+
const indexMapping: Array<number> = arrayFill(tesselationGraph.vertices.length, -1);
|
|
169
|
+
for (var j = 0; j < tesselationGraph.vertices.length; j++) {
|
|
170
|
+
const edgeIndex: number = this.__locateEdgeWithVertIndex(j, tesselationGraph);
|
|
171
|
+
if (edgeIndex === -1) {
|
|
172
|
+
// No adjacent edge found -> vertex is not in use
|
|
173
|
+
// Ignore; index mapping remains to -1
|
|
174
|
+
} else {
|
|
175
|
+
// Keep vertex
|
|
176
|
+
const newIndex: number = newVertices.length;
|
|
177
|
+
newVertices.push(tesselationGraph.vertices[j]);
|
|
178
|
+
indexMapping[j] = newIndex;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Now re-map the egdes
|
|
183
|
+
const newEdges: Array<IndexPair> = [];
|
|
184
|
+
for (var j = 0; j < tesselationGraph.edges.length; j++) {
|
|
185
|
+
const edge = tesselationGraph.edges[j];
|
|
186
|
+
newEdges.push({ i: indexMapping[edge.i], j: indexMapping[edge.j] });
|
|
187
|
+
}
|
|
188
|
+
return { vertices: newVertices, edges: newEdges };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Creates a tesselation graph and removes all inner edges.
|
|
193
|
+
*
|
|
194
|
+
* @name findAllOutlinesGraph
|
|
195
|
+
* @private
|
|
196
|
+
* @memberof PolygonTesselationOutlines
|
|
197
|
+
* @instance
|
|
198
|
+
* @return {TesselationGraph} A tesselation graph containing the outer hull edges only.
|
|
199
|
+
*/
|
|
200
|
+
findAllOutlinesGraph(): TesselationGraph {
|
|
201
|
+
// First build tesselation graph from polygons, containing all edges and vertices.
|
|
202
|
+
const tesselationGraph: TesselationGraph = polygonTesselationToGraph(this.polygons);
|
|
203
|
+
|
|
204
|
+
// Now eliminate all duplicate edges. By construction only inner edges are duplicates.
|
|
205
|
+
const outerHull = this.__eliminateDuplicateEdges(tesselationGraph);
|
|
206
|
+
// Only of requested remove unused vertices.
|
|
207
|
+
if (this.removeUnusedVertices) {
|
|
208
|
+
const cleanOuterHull = this.__removeUnusedVertices(outerHull);
|
|
209
|
+
return cleanOuterHull;
|
|
210
|
+
} else {
|
|
211
|
+
return outerHull;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Convert any generic path to a polygon. Each path segment (start point and end point)
|
|
217
|
+
* are interpreted as a linear segment here.
|
|
218
|
+
*
|
|
219
|
+
* @name __convertGenericPathToPolygon
|
|
220
|
+
* @private
|
|
221
|
+
* @memberof PolygonTesselationOutlines
|
|
222
|
+
* @instance
|
|
223
|
+
* @param {GenericPath} path - The path to convert.
|
|
224
|
+
*/
|
|
225
|
+
private __convertGenericPathToPolygon(path: GenericPath): Polygon {
|
|
226
|
+
const vertices: Array<Vertex> = [];
|
|
227
|
+
const n: number = path.getSegmentCount();
|
|
228
|
+
let isOpen: boolean = false;
|
|
229
|
+
for (var i = 0; i < n; i++) {
|
|
230
|
+
const segment: PathSegment = path.getSegmentAt(i);
|
|
231
|
+
vertices.push(segment.getStartPoint());
|
|
232
|
+
if (i > 0 && i + 1 >= n && !this.__isEqualPoint(segment.getEndPoint(), vertices[0])) {
|
|
233
|
+
isOpen = true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return new Polygon(vertices, isOpen);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Creates a tesselation graph and removes all inner edges.
|
|
241
|
+
*
|
|
242
|
+
* @name findAllOutlinesPolygons
|
|
243
|
+
* @private
|
|
244
|
+
* @memberof PolygonTesselationOutlines
|
|
245
|
+
* @instance
|
|
246
|
+
* @param {TesselationGraph} tesselationGraph - Find all outlines on the tesselation graph.
|
|
247
|
+
*/
|
|
248
|
+
findAllOutlinesPolygons(tesselationGraph: TesselationGraph): Array<Polygon> {
|
|
249
|
+
// Convert to real line segments:
|
|
250
|
+
const lineSegments: Array<Line> = tesselationGraph.edges.map(
|
|
251
|
+
(edge: IndexPair) => new Line(tesselationGraph.vertices[edge.i], tesselationGraph.vertices[edge.j])
|
|
252
|
+
);
|
|
253
|
+
// Now run the path detection.
|
|
254
|
+
const paths: Array<GenericPath> = detectPaths(lineSegments, this.tolerance);
|
|
255
|
+
|
|
256
|
+
// And now convert the generic paths back to polygons
|
|
257
|
+
// (we have put in line segments, so line segments must have returned)
|
|
258
|
+
return paths.map((path: GenericPath) => this.__convertGenericPathToPolygon(path));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Checks if two vertices are equal – regarding to the current tolerance setting.
|
|
263
|
+
*
|
|
264
|
+
* @name __isEqualPoint
|
|
265
|
+
* @private
|
|
266
|
+
* @memberof PolygonTesselationOutlines
|
|
267
|
+
* @instance
|
|
268
|
+
* @param {boolean} vertA - The first point.
|
|
269
|
+
* @param {boolean} vertB - The second point.
|
|
270
|
+
* @return True if the vertices are equal.
|
|
271
|
+
*/
|
|
272
|
+
private __isEqualPoint(vertA: Vertex, vertB: Vertex): boolean {
|
|
273
|
+
return vertA.distance(vertB) < this.tolerance;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Checks if two edges (identified as two pairs of vertices) are equal – regarding to the
|
|
278
|
+
* current tolerance setting.
|
|
279
|
+
*
|
|
280
|
+
* @name __isEqualEdge
|
|
281
|
+
* @private
|
|
282
|
+
* @memberof PolygonTesselationOutlines
|
|
283
|
+
* @instance
|
|
284
|
+
* @param {boolean} edgeAvertA - The first edge's point A.
|
|
285
|
+
* @param {boolean} edgeAvertB - The first edge's point B.
|
|
286
|
+
* @param {boolean} edgeBvertA - The second edge's point A.
|
|
287
|
+
* @param {boolean} edgeBvertB - The second edge's point B.
|
|
288
|
+
* @return True if the edges are equal.
|
|
289
|
+
*/
|
|
290
|
+
private __isEqualEdge(edgeAvertA: Vertex, edgeAvertB: Vertex, edgeBvertA: Vertex, edgeBvertB: Vertex): boolean {
|
|
291
|
+
return (
|
|
292
|
+
(this.__isEqualPoint(edgeAvertA, edgeBvertA) && this.__isEqualPoint(edgeAvertB, edgeBvertB)) ||
|
|
293
|
+
(this.__isEqualPoint(edgeAvertA, edgeBvertB) && this.__isEqualPoint(edgeAvertB, edgeBvertA))
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
}
|