@xort/cga 0.0.93 → 0.1.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/THIRD_PARTY_NOTICES.md +47 -0
- package/build/@xort_cga.js +14187 -12097
- package/build/cga.d.ts +2284 -1983
- package/build/cga.esm.js +14179 -0
- package/dist/THIRD_PARTY_NOTICES.md +47 -0
- package/dist/index.cjs +14320 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2358 -0
- package/dist/index.d.ts +2358 -38
- package/dist/index.js +14181 -56
- package/dist/index.js.map +1 -0
- package/package.json +59 -47
- package/readme.md +162 -247
- package/.github/FUNDING.yml +0 -12
- package/cga/1./347/202/271/344/270/216/347/272/277/346/200/247/345/205/203/347/264/240/347/232/204/350/267/235/347/246/273.html +0 -601
- package/cga/1./347/202/271/344/270/216/347/272/277/346/200/247/345/205/203/347/264/240/347/232/204/350/267/235/347/246/273.md +0 -111
- package/cga/2./347/202/271/344/270/216/351/235/242/346/200/247/345/205/203/347/264/240.md +0 -4
- package/cga/img//347/202/271/344/270/216/345/260/204/347/272/277.png +0 -0
- package/cga/img//347/202/271/344/270/216/347/233/264/347/272/277.png +0 -0
- package/cga/img//347/202/271/344/270/216/347/272/277/346/256/265.png +0 -0
- package/cga/img//347/233/264/347/272/277/347/232/204/350/241/250/350/276/276/345/274/217.png +0 -0
- package/deploy.sh +0 -39
- package/dist/IKanimation/Bone.d.ts +0 -7
- package/dist/IKanimation/Bone.js +0 -20
- package/dist/IKanimation/CCDSolver.d.ts +0 -37
- package/dist/IKanimation/CCDSolver.js +0 -93
- package/dist/IKanimation/Joint.d.ts +0 -3
- package/dist/IKanimation/Joint.js +0 -17
- package/dist/IKanimation/constaint/constraint.d.ts +0 -10
- package/dist/IKanimation/constaint/constraint.js +0 -25
- package/dist/IKanimation/constaint/hinge.d.ts +0 -12
- package/dist/IKanimation/constaint/hinge.js +0 -46
- package/dist/IKanimation/constaint/slider.d.ts +0 -12
- package/dist/IKanimation/constaint/slider.js +0 -44
- package/dist/IKanimation/index.d.ts +0 -0
- package/dist/IKanimation/index.js +0 -9
- package/dist/IKanimation/isolver.d.ts +0 -4
- package/dist/IKanimation/isolver.js +0 -10
- package/dist/IKanimation/skeleton.d.ts +0 -13
- package/dist/IKanimation/skeleton.js +0 -31
- package/dist/alg/common.d.ts +0 -114
- package/dist/alg/common.js +0 -302
- package/dist/alg/curve.d.ts +0 -4
- package/dist/alg/curve.js +0 -11
- package/dist/alg/delaunator.d.ts +0 -29
- package/dist/alg/delaunator.js +0 -442
- package/dist/alg/delaunay.d.ts +0 -20
- package/dist/alg/delaunay.js +0 -199
- package/dist/alg/delaunayslow.d.ts +0 -18
- package/dist/alg/delaunayslow.js +0 -140
- package/dist/alg/extrude/extrude.d.ts +0 -3
- package/dist/alg/extrude/extrude.js +0 -17
- package/dist/alg/extrude.d.ts +0 -199
- package/dist/alg/extrude.js +0 -728
- package/dist/alg/offset/index.d.ts +0 -0
- package/dist/alg/offset/index.js +0 -9
- package/dist/alg/offset.d.ts +0 -0
- package/dist/alg/offset.js +0 -472
- package/dist/alg/pointset.d.ts +0 -94
- package/dist/alg/pointset.js +0 -223
- package/dist/alg/recognition.d.ts +0 -14
- package/dist/alg/recognition.js +0 -37
- package/dist/alg/result.d.ts +0 -22
- package/dist/alg/result.js +0 -2
- package/dist/alg/segmentset.d.ts +0 -11
- package/dist/alg/segmentset.js +0 -45
- package/dist/alg/sort.d.ts +0 -2
- package/dist/alg/sort.js +0 -14
- package/dist/alg/split.d.ts +0 -8
- package/dist/alg/split.js +0 -10
- package/dist/alg/trianglution.d.ts +0 -15
- package/dist/alg/trianglution.js +0 -70
- package/dist/alg/vol.d.ts +0 -6
- package/dist/alg/vol.js +0 -18
- package/dist/alg/voronoi.d.ts +0 -31
- package/dist/alg/voronoi.js +0 -418
- package/dist/app.d.ts +0 -1
- package/dist/app.js +0 -248
- package/dist/basic/distance.d.ts +0 -12
- package/dist/basic/distance.js +0 -39
- package/dist/csg/bsp.d.ts +0 -9
- package/dist/csg/bsp.js +0 -22
- package/dist/csg/node.d.ts +0 -12
- package/dist/csg/node.js +0 -75
- package/dist/csg/polygon.d.ts +0 -43
- package/dist/csg/polygon.js +0 -161
- package/dist/diamondMaterialShader.d.ts +0 -92
- package/dist/diamondMaterialShader.js +0 -118
- package/dist/effect/breath-light.d.ts +0 -4
- package/dist/effect/breath-light.js +0 -39
- package/dist/extends/geometryaid.d.ts +0 -28
- package/dist/extends/geometryaid.js +0 -71
- package/dist/extends/split.d.ts +0 -3
- package/dist/extends/split.js +0 -13
- package/dist/glview.d.ts +0 -112
- package/dist/glview.js +0 -564
- package/dist/math/Color.d.ts +0 -33
- package/dist/math/Color.js +0 -235
- package/dist/math/Euler.d.ts +0 -46
- package/dist/math/Euler.js +0 -225
- package/dist/math/HVec3.d.ts +0 -43
- package/dist/math/HVec3.js +0 -176
- package/dist/math/Mat3.d.ts +0 -29
- package/dist/math/Mat3.js +0 -244
- package/dist/math/Mat4.d.ts +0 -85
- package/dist/math/Mat4.js +0 -791
- package/dist/math/Math.d.ts +0 -130
- package/dist/math/Math.js +0 -198
- package/dist/math/Quat.d.ts +0 -82
- package/dist/math/Quat.js +0 -418
- package/dist/math/Vec2.d.ts +0 -68
- package/dist/math/Vec2.js +0 -328
- package/dist/math/Vec3.d.ts +0 -240
- package/dist/math/Vec3.js +0 -1082
- package/dist/math/Vec4.d.ts +0 -81
- package/dist/math/Vec4.js +0 -475
- package/dist/math/vecs.d.ts +0 -134
- package/dist/math/vecs.js +0 -464
- package/dist/render/bufferAttribute.d.ts +0 -104
- package/dist/render/bufferAttribute.js +0 -344
- package/dist/render/geometry.d.ts +0 -87
- package/dist/render/geometry.js +0 -769
- package/dist/render/mesh.d.ts +0 -30
- package/dist/render/mesh.js +0 -87
- package/dist/render/types.d.ts +0 -3
- package/dist/render/types.js +0 -8
- package/dist/robust/plane.d.ts +0 -7
- package/dist/robust/plane.js +0 -17
- package/dist/robust/vec.d.ts +0 -15
- package/dist/robust/vec.js +0 -71
- package/dist/struct/3d/Box.d.ts +0 -23
- package/dist/struct/3d/Box.js +0 -109
- package/dist/struct/3d/Capsule.d.ts +0 -12
- package/dist/struct/3d/Capsule.js +0 -36
- package/dist/struct/3d/Circle.d.ts +0 -52
- package/dist/struct/3d/Circle.js +0 -138
- package/dist/struct/3d/Disk.d.ts +0 -10
- package/dist/struct/3d/Disk.js +0 -22
- package/dist/struct/3d/Frustum.d.ts +0 -35
- package/dist/struct/3d/Frustum.js +0 -159
- package/dist/struct/3d/Line.d.ts +0 -40
- package/dist/struct/3d/Line.js +0 -340
- package/dist/struct/3d/Path.d.ts +0 -75
- package/dist/struct/3d/Path.js +0 -308
- package/dist/struct/3d/Plane.d.ts +0 -74
- package/dist/struct/3d/Plane.js +0 -298
- package/dist/struct/3d/Point.d.ts +0 -4
- package/dist/struct/3d/Point.js +0 -30
- package/dist/struct/3d/Polygon.d.ts +0 -8
- package/dist/struct/3d/Polygon.js +0 -55
- package/dist/struct/3d/Polyline.d.ts +0 -34
- package/dist/struct/3d/Polyline.js +0 -130
- package/dist/struct/3d/Ray.d.ts +0 -21
- package/dist/struct/3d/Ray.js +0 -262
- package/dist/struct/3d/Rectangle.d.ts +0 -7
- package/dist/struct/3d/Rectangle.js +0 -35
- package/dist/struct/3d/Segment.d.ts +0 -32
- package/dist/struct/3d/Segment.js +0 -334
- package/dist/struct/3d/Shape.d.ts +0 -4
- package/dist/struct/3d/Shape.js +0 -27
- package/dist/struct/3d/Sphere.d.ts +0 -11
- package/dist/struct/3d/Sphere.js +0 -30
- package/dist/struct/3d/Triangle.d.ts +0 -8
- package/dist/struct/3d/Triangle.js +0 -53
- package/dist/struct/bsp.d.ts +0 -9
- package/dist/struct/bsp.js +0 -44
- package/dist/struct/btree.d.ts +0 -0
- package/dist/struct/btree.js +0 -1
- package/dist/struct/data/ArrayList.d.ts +0 -42
- package/dist/struct/data/ArrayList.js +0 -172
- package/dist/struct/data/ArrayObject.d.ts +0 -30
- package/dist/struct/data/ArrayObject.js +0 -131
- package/dist/struct/data/type.d.ts +0 -13
- package/dist/struct/data/type.js +0 -18
- package/dist/struct/kdtree.d.ts +0 -0
- package/dist/struct/kdtree.js +0 -1
- package/dist/struct/polyTree.d.ts +0 -4
- package/dist/struct/polyTree.js +0 -27
- package/dist/struct/quatTree.d.ts +0 -0
- package/dist/struct/quatTree.js +0 -1
- package/dist/struct/tree.d.ts +0 -11
- package/dist/struct/tree.js +0 -26
- package/dist/utils/array.d.ts +0 -24
- package/dist/utils/array.js +0 -89
- package/dist/utils/types.d.ts +0 -24
- package/dist/utils/types.js +0 -20
- package/docs/.vuepress/components/distance.vue +0 -125
- package/docs/.vuepress/components/intersect.vue +0 -0
- package/docs/.vuepress/components/trianglution.vue +0 -121
- package/docs/.vuepress/components/utils.js +0 -314
- package/docs/.vuepress/config.js +0 -99
- package/docs/.vuepress/enhanceApp.js +0 -13
- package/docs/.vuepress/public/delaunay.png +0 -0
- package/docs/.vuepress/public/logo.png +0 -0
- package/docs/.vuepress/public/plane-set-convex.png +0 -0
- package/docs/.vuepress/public/sponsor/alipay.jpg +0 -0
- package/docs/.vuepress/public/sponsor/wechat.png +0 -0
- package/docs/README.md +0 -14
- package/docs/demo/README.md +0 -9
- package/docs/guide/README.md +0 -94
- package/docs/guide/array.md +0 -10
- package/docs/guide/cy/trianglution.html +0 -2
- package/docs/guide/distLineAll.md +0 -85
- package/docs/guide/distPointAll.md +0 -137
- package/docs/guide/distRayAll.md +0 -69
- package/docs/guide/distSegmentAll.md +0 -19
- package/docs/guide/point.vue +0 -12
- package/docs/guide/vector.md +0 -10
- package/docs/guide//345/270/270/347/224/250.md +0 -156
- package/docs/sponsor/README.md +0 -15
- package/docs//345/274/200/345/217/221/346/227/245/345/277/227/2020/345/271/2643/346/234/21013/346/227/245.md +0 -6
- package/effect/common.glsl +0 -34
- package/effect/drawing.glsl +0 -36
- package/effect/shape.arc.glsl +0 -13
- package/effect/shape.circle.glsl +0 -13
- package/effect/shape.grid.glsl +0 -42
- package/effect/shape.line.glsl +0 -18
- package/effect/shape.pie.glsl +0 -18
- package/effect/shape.plot.glsl +0 -10
- package/effect/shape.poly.glsl +0 -15
- package/effect/shape.rect.glsl +0 -14
- package/effect/shape.rectround.glsl +0 -15
- package/effect/shape.segment.glsl +0 -10
- package/effect/shape.spiral.glsl +0 -12
- package/effect/shape.star.glsl +0 -16
- package/examples/index.html +0 -31
- package/git.sh +0 -12
- package/gitproxy.sh +0 -6
- package/image/readme/1640758500266.png +0 -0
- package/image/readme/1640758513361.png +0 -0
- package/index.html +0 -14
- package/linear algorithm//346/225/260/345/255/246/350/241/250/350/276/276/345/274/217.md" +0 -122
- package/linear algorithm//347/237/251/351/230/265.md" +0 -91
- package/rollup.config.js +0 -82
- package/scripttest.html +0 -23
- package/src/IKanimation/Bone.ts +0 -20
- package/src/IKanimation/CCDSolver.ts +0 -114
- package/src/IKanimation/Joint.ts +0 -13
- package/src/IKanimation/constaint/constraint.ts +0 -23
- package/src/IKanimation/constaint/hinge.ts +0 -29
- package/src/IKanimation/constaint/slider.ts +0 -25
- package/src/IKanimation/index.ts +0 -8
- package/src/IKanimation/isolver.ts +0 -13
- package/src/IKanimation/skeleton.ts +0 -30
- package/src/alg/common.ts +0 -304
- package/src/alg/curve.ts +0 -9
- package/src/alg/delaunator.ts +0 -519
- package/src/alg/delaunay.ts +0 -163
- package/src/alg/delaunayslow.ts +0 -183
- package/src/alg/extrude/extrude.ts +0 -19
- package/src/alg/extrude.ts +0 -941
- package/src/alg/offset/index.ts +0 -8
- package/src/alg/offset.ts +0 -501
- package/src/alg/pointset.ts +0 -219
- package/src/alg/recognition.ts +0 -43
- package/src/alg/result.ts +0 -24
- package/src/alg/segmentset.ts +0 -44
- package/src/alg/sort.ts +0 -11
- package/src/alg/split.ts +0 -15
- package/src/alg/trianglution.ts +0 -53
- package/src/alg/vol.ts +0 -18
- package/src/alg/voronoi.ts +0 -303
- package/src/app.css +0 -12
- package/src/app.ts +0 -285
- package/src/assets/color.jpg +0 -0
- package/src/basic/distance.ts +0 -40
- package/src/clipper/clipper.d.ts +0 -323
- package/src/clipper/clipper.js +0 -7626
- package/src/clipper/clipper_float.js +0 -185
- package/src/clipper/clipper_float_unminified.js +0 -5639
- package/src/clipper/clipperint.js +0 -7626
- package/src/csg/bsp.ts +0 -24
- package/src/csg/csg.js +0 -583
- package/src/csg/node.ts +0 -91
- package/src/csg/polygon.ts +0 -177
- package/src/diamondMaterialShader.ts +0 -93
- package/src/effect/breath-light.ts +0 -24
- package/src/extends/geometryaid.ts +0 -86
- package/src/extends/split.ts +0 -15
- package/src/glview.ts +0 -570
- package/src/index.ts +0 -47
- package/src/math/Color.ts +0 -225
- package/src/math/Euler.ts +0 -238
- package/src/math/HVec3.ts +0 -1144
- package/src/math/Mat3.ts +0 -365
- package/src/math/Mat4.ts +0 -1134
- package/src/math/Math.ts +0 -213
- package/src/math/Quat.ts +0 -536
- package/src/math/Vec2.ts +0 -408
- package/src/math/Vec3.ts +0 -1307
- package/src/math/Vec4.ts +0 -587
- package/src/math/vecs.ts +0 -486
- package/src/render/bufferAttribute.ts +0 -491
- package/src/render/geometry.ts +0 -1329
- package/src/render/mesh.ts +0 -82
- package/src/render/types.ts +0 -17
- package/src/robust/plane.ts +0 -12
- package/src/robust/readme.md +0 -1
- package/src/robust/vec.ts +0 -61
- package/src/shader/cga.glsl +0 -202
- package/src/shader/common.glsl +0 -87
- package/src/shader/noise/GLSL-Noise.md +0 -1108
- package/src/shader/noise/cellular2D.glsl +0 -69
- package/src/shader/noise/cellular2x2.glsl +0 -64
- package/src/shader/noise/cellular2x2x2.glsl +0 -88
- package/src/shader/noise/cellular3D.glsl +0 -187
- package/src/shader/noise/classicnoise2D.glsl +0 -113
- package/src/shader/noise/classicnoise3D.glsl +0 -177
- package/src/shader/noise/classicnoise4D.glsl +0 -302
- package/src/shader/noise/noise2D.glsl +0 -71
- package/src/shader/noise/noise3D.glsl +0 -103
- package/src/shader/noise/noise3Dgrad.glsl +0 -111
- package/src/shader/noise/noise4D.glsl +0 -129
- package/src/shader/noise/psrdnoise2D.glsl +0 -462
- package/src/struct/3d/Box.ts +0 -124
- package/src/struct/3d/Capsule.ts +0 -18
- package/src/struct/3d/Circle.ts +0 -149
- package/src/struct/3d/Disk.ts +0 -22
- package/src/struct/3d/Frustum.ts +0 -189
- package/src/struct/3d/Line.ts +0 -378
- package/src/struct/3d/Path.ts +0 -314
- package/src/struct/3d/Plane.ts +0 -341
- package/src/struct/3d/Point.ts +0 -7
- package/src/struct/3d/PolyLine.ts +0 -115
- package/src/struct/3d/Polygon.ts +0 -46
- package/src/struct/3d/Ray.ts +0 -285
- package/src/struct/3d/Rectangle.ts +0 -18
- package/src/struct/3d/Segment.ts +0 -330
- package/src/struct/3d/Shape.ts +0 -7
- package/src/struct/3d/Sphere.ts +0 -31
- package/src/struct/3d/Triangle.ts +0 -28
- package/src/struct/bsp.ts +0 -18
- package/src/struct/btree.ts +0 -0
- package/src/struct/data/ArrayList.ts +0 -144
- package/src/struct/data/ArrayObject.ts +0 -105
- package/src/struct/data/type.ts +0 -14
- package/src/struct/kdtree.ts +0 -0
- package/src/struct/polyTree.ts +0 -8
- package/src/struct/quatTree.ts +0 -0
- package/src/struct/tree.ts +0 -30
- package/src/utils/array.ts +0 -106
- package/src/utils/types.ts +0 -42
- package/src-gis/app.ts +0 -7
- package/src-gis/core/Cartographic.ts +0 -48
- package/src-gis/core/EllipsoidGeodesic.ts +0 -56
- package/src-gis/core/EllipsoidTerrainProvider.ts +0 -35
- package/src-gis/core/HeightmapTerrainData.ts +0 -5
- package/src-gis/core/Rectangle.ts +0 -112
- package/src-gis/core/TaskProcessor.ts +0 -222
- package/src-gis/gis.ts +0 -25
- package/src-gis/globe.ts +0 -54
- package/src-gis/math/Cartographic.ts +0 -24
- package/src-gis/math/GVec3.ts +0 -55
- package/src-gis/math/ellipsoid.ts +0 -13
- package/src-gis/provider/DcTerrainProvider.ts +0 -36
- package/src-gis/provider/TerrainProvider.ts +0 -12
- package/src-gis/scene/DcImageryProvider.ts +0 -36
- package/src-gis/scene/GeographicTilingScheme.ts +0 -65
- package/src-gis/scene/Globe.ts +0 -76
- package/src-gis/scene/GlobeSurfaceShaderSet.ts +0 -9
- package/src-gis/scene/GlobeSurfaceTileProvider.ts +0 -25
- package/src-gis/scene/ImageryLayerCollection.ts +0 -75
- package/src-gis/scene/ImageryProvider.ts +0 -28
- package/src-gis/scene/QuadtreeTile.ts +0 -162
- package/src-gis/scene/UrlTemplateImageryProvider.ts +0 -32
- package/src-gis/scene/quadtree_primitive.ts +0 -58
- package/tsconfig.json +0 -77
- package/webpack.config.js +0 -112
- package//350/256/241/347/256/227/345/207/240/344/275/225/351/227/256/351/242/230.md +0 -26
package/src/alg/delaunator.ts
DELETED
|
@@ -1,519 +0,0 @@
|
|
|
1
|
-
import { Vec2 } from '../math/Vec2';
|
|
2
|
-
import { Vec3 } from '../math/Vec3';
|
|
3
|
-
const EPSILON = Math.pow(2, -52);
|
|
4
|
-
const EDGE_STACK = new Uint32Array(512);
|
|
5
|
-
|
|
6
|
-
export class Delaunator {
|
|
7
|
-
coords: Float64Array;
|
|
8
|
-
_triangles: Uint32Array;
|
|
9
|
-
_halfedges: Int32Array;
|
|
10
|
-
_hashSize: number;
|
|
11
|
-
_hullPrev: Uint32Array;
|
|
12
|
-
_hullNext: Uint32Array;
|
|
13
|
-
_hullTri: Uint32Array;
|
|
14
|
-
_hullHash: Int32Array;
|
|
15
|
-
_ids: Uint32Array;
|
|
16
|
-
_dists: Float64Array;
|
|
17
|
-
hull!: Uint32Array;
|
|
18
|
-
triangles!: Uint32Array;
|
|
19
|
-
halfedges!: any;
|
|
20
|
-
_cx: any;
|
|
21
|
-
_cy: any;
|
|
22
|
-
_hullStart!: number;
|
|
23
|
-
trianglesLen!: number;
|
|
24
|
-
|
|
25
|
-
static from(points: number[]) {
|
|
26
|
-
const n = points.length;
|
|
27
|
-
const coords = new Float64Array(n);
|
|
28
|
-
|
|
29
|
-
for (let i = 0; i < n; i++) {
|
|
30
|
-
const p = points[i];
|
|
31
|
-
coords[i] = p;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return new Delaunator(coords);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
static fromVecs(points: Vec2[] | Vec3[]) {
|
|
38
|
-
var ps: number[] = [];
|
|
39
|
-
for (let i = 0; i < points.length; i++) {
|
|
40
|
-
ps.push(points[i].x, points[i].y)
|
|
41
|
-
}
|
|
42
|
-
return Delaunator.from(ps)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
constructor(coords: Float64Array) {
|
|
47
|
-
const n = coords.length >> 1;
|
|
48
|
-
if (n > 0 && typeof coords[0] !== 'number') throw new Error('Expected coords to contain numbers.');
|
|
49
|
-
|
|
50
|
-
this.coords = coords;
|
|
51
|
-
|
|
52
|
-
// arrays that will store the triangulation graph
|
|
53
|
-
const maxTriangles = Math.max(2 * n - 5, 0);
|
|
54
|
-
this._triangles = new Uint32Array(maxTriangles * 3);
|
|
55
|
-
this._halfedges = new Int32Array(maxTriangles * 3);
|
|
56
|
-
|
|
57
|
-
// temporary arrays for tracking the edges of the advancing convex hull
|
|
58
|
-
this._hashSize = Math.ceil(Math.sqrt(n));
|
|
59
|
-
this._hullPrev = new Uint32Array(n); // edge to prev edge
|
|
60
|
-
this._hullNext = new Uint32Array(n); // edge to next edge
|
|
61
|
-
this._hullTri = new Uint32Array(n); // edge to adjacent triangle
|
|
62
|
-
this._hullHash = new Int32Array(this._hashSize).fill(-1); // angular edge hash
|
|
63
|
-
|
|
64
|
-
// temporary arrays for sorting points
|
|
65
|
-
this._ids = new Uint32Array(n);
|
|
66
|
-
this._dists = new Float64Array(n);
|
|
67
|
-
|
|
68
|
-
this.update();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
update() {
|
|
72
|
-
const { coords, _hullPrev: hullPrev, _hullNext: hullNext, _hullTri: hullTri, _hullHash: hullHash } = this;
|
|
73
|
-
const n = coords.length >> 1;
|
|
74
|
-
|
|
75
|
-
// populate an array of point indices; calculate input data bbox
|
|
76
|
-
let minX = Infinity;
|
|
77
|
-
let minY = Infinity;
|
|
78
|
-
let maxX = -Infinity;
|
|
79
|
-
let maxY = -Infinity;
|
|
80
|
-
|
|
81
|
-
for (let i = 0; i < n; i++) {
|
|
82
|
-
const x = coords[2 * i];
|
|
83
|
-
const y = coords[2 * i + 1];
|
|
84
|
-
if (x < minX) minX = x;
|
|
85
|
-
if (y < minY) minY = y;
|
|
86
|
-
if (x > maxX) maxX = x;
|
|
87
|
-
if (y > maxY) maxY = y;
|
|
88
|
-
this._ids[i] = i;
|
|
89
|
-
}
|
|
90
|
-
const cx = (minX + maxX) / 2;
|
|
91
|
-
const cy = (minY + maxY) / 2;
|
|
92
|
-
|
|
93
|
-
let minDist = Infinity;
|
|
94
|
-
let i0 = 0, i1 = 0, i2 = 0;
|
|
95
|
-
|
|
96
|
-
// pick a seed point close to the center
|
|
97
|
-
for (let i = 0; i < n; i++) {
|
|
98
|
-
const d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]);
|
|
99
|
-
if (d < minDist) {
|
|
100
|
-
i0 = i;
|
|
101
|
-
minDist = d;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const i0x = coords[2 * i0];
|
|
105
|
-
const i0y = coords[2 * i0 + 1];
|
|
106
|
-
|
|
107
|
-
minDist = Infinity;
|
|
108
|
-
|
|
109
|
-
// find the point closest to the seed
|
|
110
|
-
for (let i = 0; i < n; i++) {
|
|
111
|
-
if (i === i0) continue;
|
|
112
|
-
const d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]);
|
|
113
|
-
if (d < minDist && d > 0) {
|
|
114
|
-
i1 = i;
|
|
115
|
-
minDist = d;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
let i1x = coords[2 * i1];
|
|
119
|
-
let i1y = coords[2 * i1 + 1];
|
|
120
|
-
|
|
121
|
-
let minRadius = Infinity;
|
|
122
|
-
|
|
123
|
-
// find the third point which forms the smallest circumcircle with the first two
|
|
124
|
-
for (let i = 0; i < n; i++) {
|
|
125
|
-
if (i === i0 || i === i1) continue;
|
|
126
|
-
const r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]);
|
|
127
|
-
if (r < minRadius) {
|
|
128
|
-
i2 = i;
|
|
129
|
-
minRadius = r;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
let i2x = coords[2 * i2];
|
|
133
|
-
let i2y = coords[2 * i2 + 1];
|
|
134
|
-
|
|
135
|
-
if (minRadius === Infinity) {
|
|
136
|
-
// order collinear points by dx (or dy if all x are identical)
|
|
137
|
-
// and return the list as a hull
|
|
138
|
-
for (let i = 0; i < n; i++) {
|
|
139
|
-
this._dists[i] = (coords[2 * i] - coords[0]) || (coords[2 * i + 1] - coords[1]);
|
|
140
|
-
}
|
|
141
|
-
quicksort(this._ids, this._dists, 0, n - 1);
|
|
142
|
-
const hull = new Uint32Array(n);
|
|
143
|
-
let j = 0;
|
|
144
|
-
for (let i = 0, d0 = -Infinity; i < n; i++) {
|
|
145
|
-
const id = this._ids[i];
|
|
146
|
-
if (this._dists[id] > d0) {
|
|
147
|
-
hull[j++] = id;
|
|
148
|
-
d0 = this._dists[id];
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
this.hull = hull.subarray(0, j);
|
|
152
|
-
this.triangles = new Uint32Array(0);
|
|
153
|
-
this.halfedges = new Uint32Array(0);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// swap the order of the seed points for counter-clockwise orientation
|
|
158
|
-
if (orient(i0x, i0y, i1x, i1y, i2x, i2y)) {
|
|
159
|
-
const i = i1;
|
|
160
|
-
const x = i1x;
|
|
161
|
-
const y = i1y;
|
|
162
|
-
i1 = i2;
|
|
163
|
-
i1x = i2x;
|
|
164
|
-
i1y = i2y;
|
|
165
|
-
i2 = i;
|
|
166
|
-
i2x = x;
|
|
167
|
-
i2y = y;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
|
|
171
|
-
this._cx = center.x;
|
|
172
|
-
this._cy = center.y;
|
|
173
|
-
|
|
174
|
-
for (let i = 0; i < n; i++) {
|
|
175
|
-
this._dists[i] = dist(coords[2 * i], coords[2 * i + 1], center.x, center.y);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// sort the points by distance from the seed triangle circumcenter
|
|
179
|
-
quicksort(this._ids, this._dists, 0, n - 1);
|
|
180
|
-
|
|
181
|
-
// set up the seed triangle as the starting hull
|
|
182
|
-
this._hullStart = i0;
|
|
183
|
-
let hullSize = 3;
|
|
184
|
-
|
|
185
|
-
hullNext[i0] = hullPrev[i2] = i1;
|
|
186
|
-
hullNext[i1] = hullPrev[i0] = i2;
|
|
187
|
-
hullNext[i2] = hullPrev[i1] = i0;
|
|
188
|
-
|
|
189
|
-
hullTri[i0] = 0;
|
|
190
|
-
hullTri[i1] = 1;
|
|
191
|
-
hullTri[i2] = 2;
|
|
192
|
-
|
|
193
|
-
hullHash.fill(-1);
|
|
194
|
-
hullHash[this._hashKey(i0x, i0y)] = i0;
|
|
195
|
-
hullHash[this._hashKey(i1x, i1y)] = i1;
|
|
196
|
-
hullHash[this._hashKey(i2x, i2y)] = i2;
|
|
197
|
-
|
|
198
|
-
this.trianglesLen = 0;
|
|
199
|
-
this._addTriangle(i0, i1, i2, -1, -1, -1);
|
|
200
|
-
|
|
201
|
-
for (let k = 0, xp, yp; k < this._ids.length; k++) {
|
|
202
|
-
const i = this._ids[k];
|
|
203
|
-
const x = coords[2 * i];
|
|
204
|
-
const y = coords[2 * i + 1];
|
|
205
|
-
|
|
206
|
-
// skip near-duplicate points
|
|
207
|
-
if (k > 0)
|
|
208
|
-
if (xp !== undefined && yp !== undefined) {
|
|
209
|
-
if (Math.abs(x - xp) <= EPSILON && Math.abs(y - yp) <= EPSILON) continue;
|
|
210
|
-
}
|
|
211
|
-
else
|
|
212
|
-
continue;
|
|
213
|
-
xp = x;
|
|
214
|
-
yp = y;
|
|
215
|
-
|
|
216
|
-
// skip seed triangle points
|
|
217
|
-
if (i === i0 || i === i1 || i === i2) continue;
|
|
218
|
-
|
|
219
|
-
// find a visible edge on the convex hull using edge hash
|
|
220
|
-
let start = 0;
|
|
221
|
-
for (let j = 0, key = this._hashKey(x, y); j < this._hashSize; j++) {
|
|
222
|
-
start = hullHash[(key + j) % this._hashSize];
|
|
223
|
-
if (start !== -1 && start !== hullNext[start]) break;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
start = hullPrev[start];
|
|
227
|
-
let e = start, q;
|
|
228
|
-
while (q = hullNext[e], !orient(x, y, coords[2 * e], coords[2 * e + 1], coords[2 * q], coords[2 * q + 1])) {
|
|
229
|
-
e = q;
|
|
230
|
-
if (e === start) {
|
|
231
|
-
e = -1;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
if (e === -1) continue; // likely a near-duplicate point; skip it
|
|
236
|
-
|
|
237
|
-
// add the first triangle from the point
|
|
238
|
-
let t = this._addTriangle(e, i, hullNext[e], -1, -1, hullTri[e]);
|
|
239
|
-
|
|
240
|
-
// recursively flip triangles from the point until they satisfy the Delaunay condition
|
|
241
|
-
hullTri[i] = this._legalize(t + 2);
|
|
242
|
-
hullTri[e] = t; // keep track of boundary triangles on the hull
|
|
243
|
-
hullSize++;
|
|
244
|
-
|
|
245
|
-
// walk forward through the hull, adding more triangles and flipping recursively
|
|
246
|
-
let n = hullNext[e];
|
|
247
|
-
while (q = hullNext[n], orient(x, y, coords[2 * n], coords[2 * n + 1], coords[2 * q], coords[2 * q + 1])) {
|
|
248
|
-
t = this._addTriangle(n, i, q, hullTri[i], -1, hullTri[n]);
|
|
249
|
-
hullTri[i] = this._legalize(t + 2);
|
|
250
|
-
hullNext[n] = n; // mark as removed
|
|
251
|
-
hullSize--;
|
|
252
|
-
n = q;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// walk backward from the other side, adding more triangles and flipping
|
|
256
|
-
if (e === start) {
|
|
257
|
-
while (q = hullPrev[e], orient(x, y, coords[2 * q], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1])) {
|
|
258
|
-
t = this._addTriangle(q, i, e, -1, hullTri[e], hullTri[q]);
|
|
259
|
-
this._legalize(t + 2);
|
|
260
|
-
hullTri[q] = t;
|
|
261
|
-
hullNext[e] = e; // mark as removed
|
|
262
|
-
hullSize--;
|
|
263
|
-
e = q;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// update the hull indices
|
|
268
|
-
this._hullStart = hullPrev[i] = e;
|
|
269
|
-
hullNext[e] = hullPrev[n] = i;
|
|
270
|
-
hullNext[i] = n;
|
|
271
|
-
|
|
272
|
-
// save the two new edges in the hash table
|
|
273
|
-
hullHash[this._hashKey(x, y)] = i;
|
|
274
|
-
hullHash[this._hashKey(coords[2 * e], coords[2 * e + 1])] = e;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
this.hull = new Uint32Array(hullSize);
|
|
278
|
-
for (let i = 0, e = this._hullStart; i < hullSize; i++) {
|
|
279
|
-
this.hull[i] = e;
|
|
280
|
-
e = hullNext[e];
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// trim typed triangle mesh arrays
|
|
284
|
-
this.triangles = this._triangles.subarray(0, this.trianglesLen);
|
|
285
|
-
this.halfedges = this._halfedges.subarray(0, this.trianglesLen);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
_hashKey(x: number, y: number) {
|
|
289
|
-
return Math.floor(pseudoAngle(x - this._cx, y - this._cy) * this._hashSize) % this._hashSize;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
_legalize(a: number) {
|
|
293
|
-
const { _triangles: triangles, _halfedges: halfedges, coords } = this;
|
|
294
|
-
|
|
295
|
-
let i = 0;
|
|
296
|
-
let ar = 0;
|
|
297
|
-
|
|
298
|
-
// recursion eliminated with a fixed-size stack
|
|
299
|
-
while (true) {
|
|
300
|
-
const b = halfedges[a];
|
|
301
|
-
|
|
302
|
-
/* if the pair of triangles doesn't satisfy the Delaunay condition
|
|
303
|
-
* (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
|
|
304
|
-
* then do the same check/flip recursively for the new pair of triangles
|
|
305
|
-
*
|
|
306
|
-
* pl pl
|
|
307
|
-
* /||\ / \
|
|
308
|
-
* al/ || \bl al/ \a
|
|
309
|
-
* / || \ / \
|
|
310
|
-
* / a||b \ flip /___ar___\
|
|
311
|
-
* p0\ || /p1 => p0\---bl---/p1
|
|
312
|
-
* \ || / \ /
|
|
313
|
-
* ar\ || /br b\ /br
|
|
314
|
-
* \||/ \ /
|
|
315
|
-
* pr pr
|
|
316
|
-
*/
|
|
317
|
-
const a0 = a - a % 3;
|
|
318
|
-
ar = a0 + (a + 2) % 3;
|
|
319
|
-
|
|
320
|
-
if (b === -1) { // convex hull edge
|
|
321
|
-
if (i === 0) break;
|
|
322
|
-
a = EDGE_STACK[--i];
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
const b0 = b - b % 3;
|
|
327
|
-
const al = a0 + (a + 1) % 3;
|
|
328
|
-
const bl = b0 + (b + 2) % 3;
|
|
329
|
-
|
|
330
|
-
const p0 = triangles[ar];
|
|
331
|
-
const pr = triangles[a];
|
|
332
|
-
const pl = triangles[al];
|
|
333
|
-
const p1 = triangles[bl];
|
|
334
|
-
|
|
335
|
-
const illegal = inCircle(
|
|
336
|
-
coords[2 * p0], coords[2 * p0 + 1],
|
|
337
|
-
coords[2 * pr], coords[2 * pr + 1],
|
|
338
|
-
coords[2 * pl], coords[2 * pl + 1],
|
|
339
|
-
coords[2 * p1], coords[2 * p1 + 1]);
|
|
340
|
-
|
|
341
|
-
if (illegal) {
|
|
342
|
-
triangles[a] = p1;
|
|
343
|
-
triangles[b] = p0;
|
|
344
|
-
|
|
345
|
-
const hbl = halfedges[bl];
|
|
346
|
-
|
|
347
|
-
// edge swapped on the other side of the hull (rare); fix the halfedge reference
|
|
348
|
-
if (hbl === -1) {
|
|
349
|
-
let e = this._hullStart;
|
|
350
|
-
do {
|
|
351
|
-
if (this._hullTri[e] === bl) {
|
|
352
|
-
this._hullTri[e] = a;
|
|
353
|
-
break;
|
|
354
|
-
}
|
|
355
|
-
e = this._hullPrev[e];
|
|
356
|
-
} while (e !== this._hullStart);
|
|
357
|
-
}
|
|
358
|
-
this._link(a, hbl);
|
|
359
|
-
this._link(b, halfedges[ar]);
|
|
360
|
-
this._link(ar, bl);
|
|
361
|
-
|
|
362
|
-
const br = b0 + (b + 1) % 3;
|
|
363
|
-
|
|
364
|
-
// don't worry about hitting the cap: it can only happen on extremely degenerate input
|
|
365
|
-
if (i < EDGE_STACK.length) {
|
|
366
|
-
EDGE_STACK[i++] = br;
|
|
367
|
-
}
|
|
368
|
-
} else {
|
|
369
|
-
if (i === 0) break;
|
|
370
|
-
a = EDGE_STACK[--i];
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return ar;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
_link(a: number, b: number) {
|
|
378
|
-
this._halfedges[a] = b;
|
|
379
|
-
if (b !== -1) this._halfedges[b] = a;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
// add a new triangle given vertex indices and adjacent half-edge ids
|
|
383
|
-
_addTriangle(i0: number, i1: number, i2: number, a: number, b: number, c: number) {
|
|
384
|
-
const t = this.trianglesLen;
|
|
385
|
-
|
|
386
|
-
this._triangles[t] = i0;
|
|
387
|
-
this._triangles[t + 1] = i1;
|
|
388
|
-
this._triangles[t + 2] = i2;
|
|
389
|
-
|
|
390
|
-
this._link(t, a);
|
|
391
|
-
this._link(t + 1, b);
|
|
392
|
-
this._link(t + 2, c);
|
|
393
|
-
|
|
394
|
-
this.trianglesLen += 3;
|
|
395
|
-
|
|
396
|
-
return t;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// monotonically increases with real angle, but doesn't need expensive trigonometry
|
|
401
|
-
function pseudoAngle(dx: number, dy: number) {
|
|
402
|
-
const p = dx / (Math.abs(dx) + Math.abs(dy));
|
|
403
|
-
return (dy > 0 ? 3 - p : 1 + p) / 4; // [0..1]
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function dist(ax: number, ay: number, bx: number, by: number) {
|
|
407
|
-
const dx = ax - bx;
|
|
408
|
-
const dy = ay - by;
|
|
409
|
-
return dx * dx + dy * dy;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// return 2d orientation sign if we're confident in it through J. Shewchuk's error bound check
|
|
413
|
-
function orientIfSure(px: number, py: number, rx: number, ry: number, qx: number, qy: number) {
|
|
414
|
-
const l = (ry - py) * (qx - px);
|
|
415
|
-
const r = (rx - px) * (qy - py);
|
|
416
|
-
return Math.abs(l - r) >= 3.3306690738754716e-16 * Math.abs(l + r) ? l - r : 0;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// a more robust orientation test that's stable in a given triangle (to fix robustness issues)
|
|
420
|
-
function orient(rx: number, ry: number, qx: number, qy: number, px: number, py: number) {
|
|
421
|
-
return (orientIfSure(px, py, rx, ry, qx, qy) ||
|
|
422
|
-
orientIfSure(rx, ry, qx, qy, px, py) ||
|
|
423
|
-
orientIfSure(qx, qy, px, py, rx, ry)) < 0;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
function inCircle(ax: number, ay: number, bx: number, by: number, cx: number, cy: number, px: number, py: number) {
|
|
427
|
-
const dx = ax - px;
|
|
428
|
-
const dy = ay - py;
|
|
429
|
-
const ex = bx - px;
|
|
430
|
-
const ey = by - py;
|
|
431
|
-
const fx = cx - px;
|
|
432
|
-
const fy = cy - py;
|
|
433
|
-
|
|
434
|
-
const ap = dx * dx + dy * dy;
|
|
435
|
-
const bp = ex * ex + ey * ey;
|
|
436
|
-
const cp = fx * fx + fy * fy;
|
|
437
|
-
|
|
438
|
-
return dx * (ey * cp - bp * fy) -
|
|
439
|
-
dy * (ex * cp - bp * fx) +
|
|
440
|
-
ap * (ex * fy - ey * fx) < 0;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
function circumradius(ax: number, ay: number, bx: number, by: number, cx: number, cy: number) {
|
|
444
|
-
const dx = bx - ax;
|
|
445
|
-
const dy = by - ay;
|
|
446
|
-
const ex = cx - ax;
|
|
447
|
-
const ey = cy - ay;
|
|
448
|
-
|
|
449
|
-
const bl = dx * dx + dy * dy;
|
|
450
|
-
const cl = ex * ex + ey * ey;
|
|
451
|
-
const d = 0.5 / (dx * ey - dy * ex);
|
|
452
|
-
|
|
453
|
-
const x = (ey * bl - dy * cl) * d;
|
|
454
|
-
const y = (dx * cl - ex * bl) * d;
|
|
455
|
-
|
|
456
|
-
return x * x + y * y;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
function circumcenter(ax: number, ay: number, bx: number, by: number, cx: number, cy: number) {
|
|
460
|
-
const dx = bx - ax;
|
|
461
|
-
const dy = by - ay;
|
|
462
|
-
const ex = cx - ax;
|
|
463
|
-
const ey = cy - ay;
|
|
464
|
-
|
|
465
|
-
const bl = dx * dx + dy * dy;
|
|
466
|
-
const cl = ex * ex + ey * ey;
|
|
467
|
-
const d = 0.5 / (dx * ey - dy * ex);
|
|
468
|
-
|
|
469
|
-
const x = ax + (ey * bl - dy * cl) * d;
|
|
470
|
-
const y = ay + (dx * cl - ex * bl) * d;
|
|
471
|
-
|
|
472
|
-
return { x, y };
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
function quicksort(ids: any[] | Uint32Array, dists: Float64Array, left: number, right: number) {
|
|
476
|
-
if (right - left <= 20) {
|
|
477
|
-
for (let i = left + 1; i <= right; i++) {
|
|
478
|
-
const temp = ids[i];
|
|
479
|
-
const tempDist = dists[temp];
|
|
480
|
-
let j = i - 1;
|
|
481
|
-
while (j >= left && dists[ids[j]] > tempDist) ids[j + 1] = ids[j--];
|
|
482
|
-
ids[j + 1] = temp;
|
|
483
|
-
}
|
|
484
|
-
} else {
|
|
485
|
-
const median = (left + right) >> 1;
|
|
486
|
-
let i = left + 1;
|
|
487
|
-
let j = right;
|
|
488
|
-
swap(ids, median, i);
|
|
489
|
-
if (dists[ids[left]] > dists[ids[right]]) swap(ids, left, right);
|
|
490
|
-
if (dists[ids[i]] > dists[ids[right]]) swap(ids, i, right);
|
|
491
|
-
if (dists[ids[left]] > dists[ids[i]]) swap(ids, left, i);
|
|
492
|
-
|
|
493
|
-
const temp = ids[i];
|
|
494
|
-
const tempDist = dists[temp];
|
|
495
|
-
while (true) {
|
|
496
|
-
do i++; while (dists[ids[i]] < tempDist);
|
|
497
|
-
do j--; while (dists[ids[j]] > tempDist);
|
|
498
|
-
if (j < i) break;
|
|
499
|
-
swap(ids, i, j);
|
|
500
|
-
}
|
|
501
|
-
ids[left + 1] = ids[j];
|
|
502
|
-
ids[j] = temp;
|
|
503
|
-
|
|
504
|
-
if (right - i + 1 >= j - left) {
|
|
505
|
-
quicksort(ids, dists, i, right);
|
|
506
|
-
quicksort(ids, dists, left, j - 1);
|
|
507
|
-
} else {
|
|
508
|
-
quicksort(ids, dists, left, j - 1);
|
|
509
|
-
quicksort(ids, dists, i, right);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
function swap(arr: { [x: string]: any; }, i: number, j: string | number) {
|
|
515
|
-
const tmp = arr[i];
|
|
516
|
-
arr[i] = arr[j];
|
|
517
|
-
arr[j] = tmp;
|
|
518
|
-
}
|
|
519
|
-
|
package/src/alg/delaunay.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { Delaunator } from "./delaunator";
|
|
2
|
-
import { Voronoi } from './voronoi';
|
|
3
|
-
|
|
4
|
-
const tau = 2 * Math.PI, pow = Math.pow;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// 如果三角剖分的所有三角形都具有非空区域,则三角剖分是共线的
|
|
9
|
-
function collinear(d: any) {
|
|
10
|
-
const { triangles, coords } = d;
|
|
11
|
-
for (let i = 0; i < triangles.length; i += 3) {
|
|
12
|
-
const a = 2 * triangles[i],
|
|
13
|
-
b = 2 * triangles[i + 1],
|
|
14
|
-
c = 2 * triangles[i + 2],
|
|
15
|
-
cross = (coords[c] - coords[a]) * (coords[b + 1] - coords[a + 1])
|
|
16
|
-
- (coords[b] - coords[a]) * (coords[c + 1] - coords[a + 1]);
|
|
17
|
-
if (cross > 1e-10) return false;
|
|
18
|
-
}
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function jitter(x: number, y: number, r: number) {
|
|
23
|
-
return [x + Math.sin(x + y) * r, y + Math.cos(x - y) * r];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export default class Delaunay {
|
|
27
|
-
_delaunator: Delaunator;
|
|
28
|
-
inedges: Int32Array;
|
|
29
|
-
_hullIndex: Int32Array;
|
|
30
|
-
points: Float64Array;
|
|
31
|
-
collinear?: Int32Array;
|
|
32
|
-
halfedges: any;
|
|
33
|
-
hull!: Uint32Array;
|
|
34
|
-
triangles!: Uint32Array;
|
|
35
|
-
static from(points: number[]) {
|
|
36
|
-
return new Delaunay(new Float64Array(points));
|
|
37
|
-
}
|
|
38
|
-
constructor(points: Float64Array) {
|
|
39
|
-
this._delaunator = new Delaunator(points);
|
|
40
|
-
this.inedges = new Int32Array(points.length / 2);
|
|
41
|
-
this._hullIndex = new Int32Array(points.length / 2);
|
|
42
|
-
this.points = this._delaunator.coords;
|
|
43
|
-
this._init();
|
|
44
|
-
}
|
|
45
|
-
update() {
|
|
46
|
-
this._delaunator.update();
|
|
47
|
-
this._init();
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
_init() {
|
|
51
|
-
const d = this._delaunator, points = this.points;
|
|
52
|
-
|
|
53
|
-
// check for collinear
|
|
54
|
-
if (d.hull && d.hull.length > 2 && collinear(d)) {
|
|
55
|
-
this.collinear = Int32Array.from({ length: points.length / 2 }, (_, i) => i)
|
|
56
|
-
.sort((i, j) => points[2 * i] - points[2 * j] || points[2 * i + 1] - points[2 * j + 1]); // for exact neighbors
|
|
57
|
-
const e = this.collinear[0], f = this.collinear[this.collinear.length - 1],
|
|
58
|
-
bounds = [points[2 * e], points[2 * e + 1], points[2 * f], points[2 * f + 1]],
|
|
59
|
-
r = 1e-8 * Math.hypot(bounds[3] - bounds[1], bounds[2] - bounds[0]);
|
|
60
|
-
for (let i = 0, n = points.length / 2; i < n; ++i) {
|
|
61
|
-
const p = jitter(points[2 * i], points[2 * i + 1], r);
|
|
62
|
-
points[2 * i] = p[0];
|
|
63
|
-
points[2 * i + 1] = p[1];
|
|
64
|
-
}
|
|
65
|
-
this._delaunator = new Delaunator(points);
|
|
66
|
-
} else {
|
|
67
|
-
delete this.collinear;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const halfedges = this.halfedges = this._delaunator.halfedges;
|
|
71
|
-
const hull = this.hull = this._delaunator.hull;
|
|
72
|
-
const triangles = this.triangles = this._delaunator.triangles;
|
|
73
|
-
const inedges = this.inedges.fill(-1);
|
|
74
|
-
const hullIndex = this._hullIndex.fill(-1);
|
|
75
|
-
|
|
76
|
-
// Compute an index from each point to an (arbitrary) incoming halfedge
|
|
77
|
-
// Used to give the first neighbor of each point; for this reason,
|
|
78
|
-
// on the hull we give priority to exterior halfedges
|
|
79
|
-
for (let e = 0, n = halfedges.length; e < n; ++e) {
|
|
80
|
-
const p = triangles[e % 3 === 2 ? e - 2 : e + 1];
|
|
81
|
-
if (halfedges[e] === -1 || inedges[p] === -1) inedges[p] = e;
|
|
82
|
-
}
|
|
83
|
-
for (let i = 0, n = hull.length; i < n; ++i) {
|
|
84
|
-
hullIndex[hull[i]] = i;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// degenerate case: 1 or 2 (distinct) points
|
|
88
|
-
if (hull.length <= 2 && hull.length > 0) {
|
|
89
|
-
this.triangles = new Uint32Array(3).fill(-1);
|
|
90
|
-
this.halfedges = new Uint32Array(3).fill(-1);
|
|
91
|
-
this.triangles[0] = hull[0];
|
|
92
|
-
this.triangles[1] = hull[1];
|
|
93
|
-
this.triangles[2] = hull[1];
|
|
94
|
-
inedges[hull[0]] = 1;
|
|
95
|
-
if (hull.length === 2) inedges[hull[1]] = 0;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
voronoi(bounds: [number, number, number, number] | undefined) {
|
|
99
|
-
return new Voronoi(this, bounds);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
*neighbors(i: number) {
|
|
103
|
-
const { inedges, hull, _hullIndex, halfedges, triangles, collinear } = this;
|
|
104
|
-
|
|
105
|
-
// degenerate case with several collinear points
|
|
106
|
-
if (collinear) {
|
|
107
|
-
const l = collinear.indexOf(i);
|
|
108
|
-
if (l > 0) yield collinear[l - 1];
|
|
109
|
-
if (l < collinear.length - 1) yield collinear[l + 1];
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const e0 = inedges[i];
|
|
114
|
-
if (e0 === -1) return; // coincident point
|
|
115
|
-
let e = e0, p0 = -1;
|
|
116
|
-
do {
|
|
117
|
-
yield p0 = triangles[e];
|
|
118
|
-
e = e % 3 === 2 ? e - 2 : e + 1;
|
|
119
|
-
if (triangles[e] !== i) return; // bad triangulation
|
|
120
|
-
e = halfedges[e];
|
|
121
|
-
if (e === -1) {
|
|
122
|
-
const p = hull[(_hullIndex[i] + 1) % hull.length];
|
|
123
|
-
if (p !== p0) yield p;
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
} while (e !== e0);
|
|
127
|
-
}
|
|
128
|
-
find(x: number, y: number, i = 0) {
|
|
129
|
-
if ((x = +x, x !== x) || (y = +y, y !== y)) return -1;
|
|
130
|
-
const i0 = i;
|
|
131
|
-
let c;
|
|
132
|
-
while ((c = this._step(i, x, y)) >= 0 && c !== i && c !== i0) i = c;
|
|
133
|
-
return c;
|
|
134
|
-
}
|
|
135
|
-
_step(i: number, x: number, y: number) {
|
|
136
|
-
const { inedges, hull, _hullIndex, halfedges, triangles, points } = this;
|
|
137
|
-
if (inedges[i] === -1 || !points.length) return (i + 1) % (points.length >> 1);
|
|
138
|
-
let c = i;
|
|
139
|
-
let dc = pow(x - points[i * 2], 2) + pow(y - points[i * 2 + 1], 2);
|
|
140
|
-
const e0 = inedges[i];
|
|
141
|
-
let e = e0;
|
|
142
|
-
do {
|
|
143
|
-
let t = triangles[e];
|
|
144
|
-
const dt = pow(x - points[t * 2], 2) + pow(y - points[t * 2 + 1], 2);
|
|
145
|
-
if (dt < dc) dc = dt, c = t;
|
|
146
|
-
e = e % 3 === 2 ? e - 2 : e + 1;
|
|
147
|
-
if (triangles[e] !== i) break; // bad triangulation
|
|
148
|
-
e = halfedges[e];
|
|
149
|
-
if (e === -1) {
|
|
150
|
-
e = hull[(_hullIndex[i] + 1) % hull.length];
|
|
151
|
-
if (e !== t) {
|
|
152
|
-
if (pow(x - points[e * 2], 2) + pow(y - points[e * 2 + 1], 2) < dc) return e;
|
|
153
|
-
}
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
} while (e !== e0);
|
|
157
|
-
return c;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|