@maplat/tin 0.12.0 → 0.13.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.
@@ -1,84 +1,84 @@
1
- import { featureCollection, polygon } from "@turf/helpers";
2
- import type { FeatureCollection, Point, Polygon } from "geojson";
3
- import Delaunator from "delaunator";
4
- import { Constrain as EdgeBound } from "./edgebound/index.ts";
5
- import type { Edge } from "@maplat/transform";
6
- import type { TriangleProperties } from "./types/tin.d.ts";
7
-
8
- /**
9
- * 制約付きTIN(Triangulated Irregular Network)を生成します
10
- *
11
- * 点群から三角形分割を行い、指定されたエッジ(制約線)を
12
- * 必ず三角形の辺として含むように調整します。
13
- *
14
- * @param points - 点群を含むFeatureCollection
15
- * 各Featureは座標を持つPointである必要があります
16
- * @param edges - 制約として扱うエッジの配列
17
- * 各エッジは2つの頂点インデックスの組として指定
18
- * @param z - 生成される三角形のプロパティに含める項目の名前
19
- * (例: "target")
20
- *
21
- * @returns 三角形のFeatureCollectionを返します
22
- * 各三角形は以下の情報を持ちます:
23
- * - geometry: 三角形の座標
24
- * - properties: 頂点に関連付けられた情報(a, b, cプロパティ)
25
- *
26
- * @throws {Error} pointsがFeatureCollectionでない場合
27
- * @throws {Error} edgesが配列でない場合
28
- *
29
- * 処理の流れ:
30
- * 1. Delaunay三角分割を実行
31
- * 2. 制約エッジに基づいて三角分割を調整
32
- * 3. 結果をGeoJSON形式に変換
33
- */
34
- export default function (
35
- points: FeatureCollection<Point>,
36
- edges: Edge[],
37
- z: string,
38
- ): FeatureCollection<Polygon> {
39
- if (!edges) edges = [];
40
- if (typeof points !== "object" || points.type !== "FeatureCollection") {
41
- throw "Argument points must be FeatureCollection";
42
- }
43
- if (!Array.isArray(edges)) throw "Argument points must be Array of Array";
44
-
45
- // Delaunay三角分割の実行
46
- const del_points = points.features.map(
47
- (point) => point.geometry!.coordinates as number[],
48
- );
49
- const del = Delaunator.from(del_points);
50
-
51
- // 制約エッジの適用
52
- let con;
53
- const tris = [];
54
- if (del.triangles.length !== 0 && edges.length !== 0) {
55
- con = new EdgeBound(del);
56
- con.constrainAll(edges);
57
- }
58
-
59
- // 三角形リストの構築
60
- for (let i = 0; i < del.triangles.length; i += 3) {
61
- tris.push([del.triangles[i], del.triangles[i + 1], del.triangles[i + 2]]);
62
- }
63
-
64
- // GeoJSON形式での出力
65
- const keys = ["a", "b", "c"] as const;
66
- return featureCollection(
67
- tris.map((indices) => {
68
- const properties: TriangleProperties = {};
69
- const coords = indices.map((index, i) => {
70
- const point = points.features[index];
71
- const xyz = point.geometry!.coordinates as number[];
72
- const coord: number[] = [xyz[0], xyz[1]];
73
- if (xyz.length === 3) {
74
- coord[2] = xyz[2];
75
- } else {
76
- properties[keys[i]] = point.properties![z];
77
- }
78
- return coord;
79
- });
80
- coords[3] = coords[0];
81
- return polygon([coords], properties);
82
- }),
83
- );
84
- }
1
+ import { featureCollection, polygon } from "@turf/turf";
2
+ import type { FeatureCollection, Point, Polygon } from "geojson";
3
+ import Delaunator from "delaunator";
4
+ import { Constrain as EdgeBound } from "./edgebound/index.ts";
5
+ import type { Edge } from "@maplat/transform";
6
+ import type { TriangleProperties } from "./types/tin.d.ts";
7
+
8
+ /**
9
+ * 制約付きTIN(Triangulated Irregular Network)を生成します
10
+ *
11
+ * 点群から三角形分割を行い、指定されたエッジ(制約線)を
12
+ * 必ず三角形の辺として含むように調整します。
13
+ *
14
+ * @param points - 点群を含むFeatureCollection
15
+ * 各Featureは座標を持つPointである必要があります
16
+ * @param edges - 制約として扱うエッジの配列
17
+ * 各エッジは2つの頂点インデックスの組として指定
18
+ * @param z - 生成される三角形のプロパティに含める項目の名前
19
+ * (例: "target")
20
+ *
21
+ * @returns 三角形のFeatureCollectionを返します
22
+ * 各三角形は以下の情報を持ちます:
23
+ * - geometry: 三角形の座標
24
+ * - properties: 頂点に関連付けられた情報(a, b, cプロパティ)
25
+ *
26
+ * @throws {Error} pointsがFeatureCollectionでない場合
27
+ * @throws {Error} edgesが配列でない場合
28
+ *
29
+ * 処理の流れ:
30
+ * 1. Delaunay三角分割を実行
31
+ * 2. 制約エッジに基づいて三角分割を調整
32
+ * 3. 結果をGeoJSON形式に変換
33
+ */
34
+ export default function (
35
+ points: FeatureCollection<Point>,
36
+ edges: Edge[],
37
+ z: string,
38
+ ): FeatureCollection<Polygon> {
39
+ if (!edges) edges = [];
40
+ if (typeof points !== "object" || points.type !== "FeatureCollection") {
41
+ throw "Argument points must be FeatureCollection";
42
+ }
43
+ if (!Array.isArray(edges)) throw "Argument points must be Array of Array";
44
+
45
+ // Delaunay三角分割の実行
46
+ const del_points = points.features.map(
47
+ (point) => point.geometry!.coordinates as number[],
48
+ );
49
+ const del = Delaunator.from(del_points);
50
+
51
+ // 制約エッジの適用
52
+ let con;
53
+ const tris = [];
54
+ if (del.triangles.length !== 0 && edges.length !== 0) {
55
+ con = new EdgeBound(del);
56
+ con.constrainAll(edges);
57
+ }
58
+
59
+ // 三角形リストの構築
60
+ for (let i = 0; i < del.triangles.length; i += 3) {
61
+ tris.push([del.triangles[i], del.triangles[i + 1], del.triangles[i + 2]]);
62
+ }
63
+
64
+ // GeoJSON形式での出力
65
+ const keys = ["a", "b", "c"] as const;
66
+ return featureCollection(
67
+ tris.map((indices) => {
68
+ const properties: TriangleProperties = {};
69
+ const coords = indices.map((index, i) => {
70
+ const point = points.features[index];
71
+ const xyz = point.geometry!.coordinates as number[];
72
+ const coord: number[] = [xyz[0], xyz[1]];
73
+ if (xyz.length === 3) {
74
+ coord[2] = xyz[2];
75
+ } else {
76
+ properties[keys[i]] = point.properties![z];
77
+ }
78
+ return coord;
79
+ });
80
+ coords[3] = coords[0];
81
+ return polygon([coords], properties);
82
+ }),
83
+ );
84
+ }
@@ -1,65 +1,65 @@
1
- import { orient2d } from "robust-predicates";
2
-
3
- export interface DelaunatorLike {
4
- coords: { readonly length: number; readonly [index: number]: number };
5
- triangles: { readonly length: number; [index: number]: number };
6
- halfedges: { readonly length: number; [index: number]: number };
7
- hull: { readonly length: number; readonly [index: number]: number };
8
- }
9
-
10
- export function nextEdge(edge: number): number {
11
- return edge % 3 === 2 ? edge - 2 : edge + 1;
12
- }
13
-
14
- export function prevEdge(edge: number): number {
15
- return edge % 3 === 0 ? edge + 2 : edge - 1;
16
- }
17
-
18
- /**
19
- * Compute if two line segments [p1, p2] and [p3, p4] intersect.
20
- */
21
- export function intersectSegments(
22
- p1x: number,
23
- p1y: number,
24
- p2x: number,
25
- p2y: number,
26
- p3x: number,
27
- p3y: number,
28
- p4x: number,
29
- p4y: number,
30
- ): boolean {
31
- const x0 = orient2d(p1x, p1y, p3x, p3y, p4x, p4y);
32
- const y0 = orient2d(p2x, p2y, p3x, p3y, p4x, p4y);
33
- if ((x0 > 0 && y0 > 0) || (x0 < 0 && y0 < 0)) {
34
- return false;
35
- }
36
-
37
- const x1 = orient2d(p3x, p3y, p1x, p1y, p2x, p2y);
38
- const y1 = orient2d(p4x, p4y, p1x, p1y, p2x, p2y);
39
- if ((x1 > 0 && y1 > 0) || (x1 < 0 && y1 < 0)) {
40
- return false;
41
- }
42
-
43
- // Handle the degenerate collinear case
44
- if (x0 === 0 && y0 === 0 && x1 === 0 && y1 === 0) {
45
- return !(
46
- Math.max(p3x, p4x) < Math.min(p1x, p2x) ||
47
- Math.max(p1x, p2x) < Math.min(p3x, p4x) ||
48
- Math.max(p3y, p4y) < Math.min(p1y, p2y) ||
49
- Math.max(p1y, p2y) < Math.min(p3y, p4y)
50
- );
51
- }
52
-
53
- return true;
54
- }
55
-
56
- export class Base {
57
- /**
58
- * The triangulation object from Delaunator.
59
- */
60
- public del: DelaunatorLike;
61
-
62
- constructor(del: DelaunatorLike) {
63
- this.del = del;
64
- }
65
- }
1
+ import { orient2d } from "robust-predicates";
2
+
3
+ export interface DelaunatorLike {
4
+ coords: { readonly length: number; readonly [index: number]: number };
5
+ triangles: { readonly length: number; [index: number]: number };
6
+ halfedges: { readonly length: number; [index: number]: number };
7
+ hull: { readonly length: number; readonly [index: number]: number };
8
+ }
9
+
10
+ export function nextEdge(edge: number): number {
11
+ return edge % 3 === 2 ? edge - 2 : edge + 1;
12
+ }
13
+
14
+ export function prevEdge(edge: number): number {
15
+ return edge % 3 === 0 ? edge + 2 : edge - 1;
16
+ }
17
+
18
+ /**
19
+ * Compute if two line segments [p1, p2] and [p3, p4] intersect.
20
+ */
21
+ export function intersectSegments(
22
+ p1x: number,
23
+ p1y: number,
24
+ p2x: number,
25
+ p2y: number,
26
+ p3x: number,
27
+ p3y: number,
28
+ p4x: number,
29
+ p4y: number,
30
+ ): boolean {
31
+ const x0 = orient2d(p1x, p1y, p3x, p3y, p4x, p4y);
32
+ const y0 = orient2d(p2x, p2y, p3x, p3y, p4x, p4y);
33
+ if ((x0 > 0 && y0 > 0) || (x0 < 0 && y0 < 0)) {
34
+ return false;
35
+ }
36
+
37
+ const x1 = orient2d(p3x, p3y, p1x, p1y, p2x, p2y);
38
+ const y1 = orient2d(p4x, p4y, p1x, p1y, p2x, p2y);
39
+ if ((x1 > 0 && y1 > 0) || (x1 < 0 && y1 < 0)) {
40
+ return false;
41
+ }
42
+
43
+ // Handle the degenerate collinear case
44
+ if (x0 === 0 && y0 === 0 && x1 === 0 && y1 === 0) {
45
+ return !(
46
+ Math.max(p3x, p4x) < Math.min(p1x, p2x) ||
47
+ Math.max(p1x, p2x) < Math.min(p3x, p4x) ||
48
+ Math.max(p3y, p4y) < Math.min(p1y, p2y) ||
49
+ Math.max(p1y, p2y) < Math.min(p3y, p4y)
50
+ );
51
+ }
52
+
53
+ return true;
54
+ }
55
+
56
+ export class Base {
57
+ /**
58
+ * The triangulation object from Delaunator.
59
+ */
60
+ public del: DelaunatorLike;
61
+
62
+ constructor(del: DelaunatorLike) {
63
+ this.del = del;
64
+ }
65
+ }
@@ -1,109 +1,109 @@
1
- /**
2
- * A set of numbers, stored as bits in a typed array.
3
- * The amount of numbers that can be stored is limited by the length,
4
- * which is fixed at construction time.
5
- */
6
- abstract class BitSet {
7
- protected readonly bs: Uint8Array | Uint16Array | Uint32Array;
8
- protected readonly width: 8 | 16 | 32;
9
-
10
- protected constructor(width: BitSet["width"], bs: BitSet["bs"]) {
11
- this.width = width;
12
- this.bs = bs;
13
- }
14
-
15
- /**
16
- * Add a number to the set.
17
- *
18
- * @param idx The number to add. Must be 0 <= idx < len.
19
- */
20
- add(idx: number): this {
21
- const byte = Math.floor(idx / this.width);
22
- const bit = idx % this.width;
23
- this.bs[byte] |= 1 << bit;
24
- return this;
25
- }
26
-
27
- /**
28
- * Delete a number from the set.
29
- *
30
- * @param idx The number to delete. Must be 0 <= idx < len.
31
- */
32
- delete(idx: number): this {
33
- const byte = Math.floor(idx / this.width);
34
- const bit = idx % this.width;
35
- this.bs[byte] &= ~(1 << bit);
36
- return this;
37
- }
38
-
39
- /**
40
- * Add or delete a number in the set, depending on the second argument.
41
- *
42
- * @param idx The number to add or delete. Must be 0 <= idx < len.
43
- * @param val If true, add the number, otherwise delete.
44
- */
45
- set(idx: number, val: boolean): boolean {
46
- const byte = Math.floor(idx / this.width);
47
- const bit = idx % this.width;
48
- const mask = 1 << bit;
49
- this.bs[byte] ^= (-Number(val) ^ this.bs[byte]) & mask;
50
- return val;
51
- }
52
-
53
- /**
54
- * Whether the number is in the set.
55
- *
56
- * @param idx The number to test. Must be 0 <= idx < len.
57
- */
58
- has(idx: number): boolean {
59
- const byte = Math.floor(idx / this.width);
60
- const bit = idx % this.width;
61
- return (this.bs[byte] & (1 << bit)) !== 0;
62
- }
63
-
64
- /**
65
- * Iterate over the numbers that are in the set.
66
- */
67
- forEach(fn: (idx: number) => void): this {
68
- const len = this.bs.length;
69
- for (let byte = 0; byte < len; byte++) {
70
- let bit = 0;
71
- while (this.bs[byte] && bit < this.width) {
72
- if (this.bs[byte] & (1 << bit)) {
73
- fn(byte * this.width + bit);
74
- }
75
- bit++;
76
- }
77
- }
78
- return this;
79
- }
80
- }
81
-
82
- export type { BitSet };
83
-
84
- /**
85
- * A bit set using 8 bits per cell.
86
- */
87
- export class BitSet8 extends BitSet {
88
- constructor(len: number) {
89
- super(8, new Uint8Array(Math.ceil(len / 8)).fill(0));
90
- }
91
- }
92
-
93
- /**
94
- * A bit set using 16 bits per cell.
95
- */
96
- export class BitSet16 extends BitSet {
97
- constructor(len: number) {
98
- super(16, new Uint16Array(Math.ceil(len / 16)).fill(0));
99
- }
100
- }
101
-
102
- /**
103
- * A bit set using 32 bits per cell.
104
- */
105
- export class BitSet32 extends BitSet {
106
- constructor(len: number) {
107
- super(32, new Uint32Array(Math.ceil(len / 32)).fill(0));
108
- }
109
- }
1
+ /**
2
+ * A set of numbers, stored as bits in a typed array.
3
+ * The amount of numbers that can be stored is limited by the length,
4
+ * which is fixed at construction time.
5
+ */
6
+ abstract class BitSet {
7
+ protected readonly bs: Uint8Array | Uint16Array | Uint32Array;
8
+ protected readonly width: 8 | 16 | 32;
9
+
10
+ protected constructor(width: BitSet["width"], bs: BitSet["bs"]) {
11
+ this.width = width;
12
+ this.bs = bs;
13
+ }
14
+
15
+ /**
16
+ * Add a number to the set.
17
+ *
18
+ * @param idx The number to add. Must be 0 <= idx < len.
19
+ */
20
+ add(idx: number): this {
21
+ const byte = Math.floor(idx / this.width);
22
+ const bit = idx % this.width;
23
+ this.bs[byte] |= 1 << bit;
24
+ return this;
25
+ }
26
+
27
+ /**
28
+ * Delete a number from the set.
29
+ *
30
+ * @param idx The number to delete. Must be 0 <= idx < len.
31
+ */
32
+ delete(idx: number): this {
33
+ const byte = Math.floor(idx / this.width);
34
+ const bit = idx % this.width;
35
+ this.bs[byte] &= ~(1 << bit);
36
+ return this;
37
+ }
38
+
39
+ /**
40
+ * Add or delete a number in the set, depending on the second argument.
41
+ *
42
+ * @param idx The number to add or delete. Must be 0 <= idx < len.
43
+ * @param val If true, add the number, otherwise delete.
44
+ */
45
+ set(idx: number, val: boolean): boolean {
46
+ const byte = Math.floor(idx / this.width);
47
+ const bit = idx % this.width;
48
+ const mask = 1 << bit;
49
+ this.bs[byte] ^= (-Number(val) ^ this.bs[byte]) & mask;
50
+ return val;
51
+ }
52
+
53
+ /**
54
+ * Whether the number is in the set.
55
+ *
56
+ * @param idx The number to test. Must be 0 <= idx < len.
57
+ */
58
+ has(idx: number): boolean {
59
+ const byte = Math.floor(idx / this.width);
60
+ const bit = idx % this.width;
61
+ return (this.bs[byte] & (1 << bit)) !== 0;
62
+ }
63
+
64
+ /**
65
+ * Iterate over the numbers that are in the set.
66
+ */
67
+ forEach(fn: (idx: number) => void): this {
68
+ const len = this.bs.length;
69
+ for (let byte = 0; byte < len; byte++) {
70
+ let bit = 0;
71
+ while (this.bs[byte] && bit < this.width) {
72
+ if (this.bs[byte] & (1 << bit)) {
73
+ fn(byte * this.width + bit);
74
+ }
75
+ bit++;
76
+ }
77
+ }
78
+ return this;
79
+ }
80
+ }
81
+
82
+ export type { BitSet };
83
+
84
+ /**
85
+ * A bit set using 8 bits per cell.
86
+ */
87
+ export class BitSet8 extends BitSet {
88
+ constructor(len: number) {
89
+ super(8, new Uint8Array(Math.ceil(len / 8)).fill(0));
90
+ }
91
+ }
92
+
93
+ /**
94
+ * A bit set using 16 bits per cell.
95
+ */
96
+ export class BitSet16 extends BitSet {
97
+ constructor(len: number) {
98
+ super(16, new Uint16Array(Math.ceil(len / 16)).fill(0));
99
+ }
100
+ }
101
+
102
+ /**
103
+ * A bit set using 32 bits per cell.
104
+ */
105
+ export class BitSet32 extends BitSet {
106
+ constructor(len: number) {
107
+ super(32, new Uint32Array(Math.ceil(len / 32)).fill(0));
108
+ }
109
+ }
@@ -1,15 +1,15 @@
1
- ISC License
2
-
3
- Copyright 2020, Marco Gunnink
4
-
5
- Permission to use, copy, modify, and/or distribute this software for any purpose
6
- with or without fee is hereby granted, provided that the above copyright notice
7
- and this permission notice appear in all copies.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
- FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
- THIS SOFTWARE.
1
+ ISC License
2
+
3
+ Copyright 2020, Marco Gunnink
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose
6
+ with or without fee is hereby granted, provided that the above copyright notice
7
+ and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
+ THIS SOFTWARE.