@maplat/tin 0.12.0 → 0.13.1

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,102 +1,102 @@
1
- import type { Tins, Tri } from "@maplat/transform";
2
- import type { PropertyTriKey } from "@maplat/transform";
3
-
4
- type BiDirectionKey = "forw" | "bakw";
5
- type TinsBD = { [key in BiDirectionKey]?: Tins };
6
- export type SearchTris = { [key in BiDirectionKey]: Tri };
7
- export type SearchIndex = { [key: string]: SearchTris[] };
8
-
9
- /**
10
- * 三角形のインデックスキーを計算する
11
- * 三つの頂点のインデックスから、ソートされたキー配列を生成
12
- *
13
- * @param tri インデックスキーを計算する三角形
14
- * @returns ソートされたキー配列
15
- */
16
- function calcSearchKeys(tri: Tri): string[] {
17
- const vtx = (["a", "b", "c"] as PropertyTriKey[]).map(
18
- (key) => tri.properties![key].index,
19
- );
20
- return [
21
- [0, 1],
22
- [0, 2],
23
- [1, 2],
24
- [0, 1, 2],
25
- ]
26
- .map((set) =>
27
- set
28
- .map((i) => vtx[i])
29
- .sort()
30
- .join("-")
31
- )
32
- .sort();
33
- }
34
-
35
- /**
36
- * 検索インデックスに三角形を追加する
37
- *
38
- * @param searchIndex 検索インデックス
39
- * @param tris 追加する三角形ペア
40
- * @param tins (オプション)追加先の三角形群
41
- * @throws キーの不一致エラー
42
- */
43
- function insertSearchIndex(
44
- searchIndex: SearchIndex,
45
- tris: SearchTris,
46
- tins?: TinsBD,
47
- ) {
48
- const keys = calcSearchKeys(tris.forw);
49
- const bakKeys = calcSearchKeys(tris.bakw);
50
- if (JSON.stringify(keys) != JSON.stringify(bakKeys)) {
51
- throw `${JSON.stringify(tris, null, 2)}\n${
52
- JSON.stringify(
53
- keys,
54
- )
55
- }\n${JSON.stringify(bakKeys)}`;
56
- }
57
- for (let i = 0; i < keys.length; i++) {
58
- const key = keys[i];
59
- if (!searchIndex[key]) searchIndex[key] = [];
60
- searchIndex[key].push(tris);
61
- }
62
- if (tins) {
63
- tins.forw!.features.push(tris.forw);
64
- tins.bakw!.features.push(tris.bakw);
65
- }
66
- }
67
-
68
- function removeSearchIndex(
69
- searchIndex: SearchIndex,
70
- tris: SearchTris,
71
- tins?: TinsBD,
72
- ) {
73
- const keys = calcSearchKeys(tris.forw);
74
- const bakKeys = calcSearchKeys(tris.bakw);
75
- if (JSON.stringify(keys) != JSON.stringify(bakKeys)) {
76
- throw `${JSON.stringify(tris, null, 2)}\n${JSON.stringify(keys)}\n${
77
- JSON.stringify(bakKeys)
78
- }`;
79
- }
80
-
81
- keys.forEach((key) => {
82
- const current = searchIndex[key];
83
- if (!current) return;
84
- const filtered = current.filter((item) => item !== tris);
85
- if (filtered.length === 0) {
86
- delete searchIndex[key];
87
- } else {
88
- searchIndex[key] = filtered;
89
- }
90
- });
91
-
92
- if (tins) {
93
- const removeFeature = (collection?: Tins, target?: Tri) => {
94
- if (!collection || !target) return;
95
- collection.features = collection.features.filter((tri) => tri !== target);
96
- };
97
- removeFeature(tins.forw, tris.forw);
98
- removeFeature(tins.bakw, tris.bakw);
99
- }
100
- }
101
-
102
- export { calcSearchKeys, insertSearchIndex, removeSearchIndex };
1
+ import type { Tins, Tri } from "@maplat/transform";
2
+ import type { PropertyTriKey } from "@maplat/transform";
3
+
4
+ type BiDirectionKey = "forw" | "bakw";
5
+ type TinsBD = { [key in BiDirectionKey]?: Tins };
6
+ export type SearchTris = { [key in BiDirectionKey]: Tri };
7
+ export type SearchIndex = { [key: string]: SearchTris[] };
8
+
9
+ /**
10
+ * 三角形のインデックスキーを計算する
11
+ * 三つの頂点のインデックスから、ソートされたキー配列を生成
12
+ *
13
+ * @param tri インデックスキーを計算する三角形
14
+ * @returns ソートされたキー配列
15
+ */
16
+ function calcSearchKeys(tri: Tri): string[] {
17
+ const vtx = (["a", "b", "c"] as PropertyTriKey[]).map(
18
+ (key) => tri.properties![key].index,
19
+ );
20
+ return [
21
+ [0, 1],
22
+ [0, 2],
23
+ [1, 2],
24
+ [0, 1, 2],
25
+ ]
26
+ .map((set) =>
27
+ set
28
+ .map((i) => vtx[i])
29
+ .sort()
30
+ .join("-")
31
+ )
32
+ .sort();
33
+ }
34
+
35
+ /**
36
+ * 検索インデックスに三角形を追加する
37
+ *
38
+ * @param searchIndex 検索インデックス
39
+ * @param tris 追加する三角形ペア
40
+ * @param tins (オプション)追加先の三角形群
41
+ * @throws キーの不一致エラー
42
+ */
43
+ function insertSearchIndex(
44
+ searchIndex: SearchIndex,
45
+ tris: SearchTris,
46
+ tins?: TinsBD,
47
+ ) {
48
+ const keys = calcSearchKeys(tris.forw);
49
+ const bakKeys = calcSearchKeys(tris.bakw);
50
+ if (JSON.stringify(keys) != JSON.stringify(bakKeys)) {
51
+ throw `${JSON.stringify(tris, null, 2)}\n${
52
+ JSON.stringify(
53
+ keys,
54
+ )
55
+ }\n${JSON.stringify(bakKeys)}`;
56
+ }
57
+ for (let i = 0; i < keys.length; i++) {
58
+ const key = keys[i];
59
+ if (!searchIndex[key]) searchIndex[key] = [];
60
+ searchIndex[key].push(tris);
61
+ }
62
+ if (tins) {
63
+ tins.forw!.features.push(tris.forw);
64
+ tins.bakw!.features.push(tris.bakw);
65
+ }
66
+ }
67
+
68
+ function removeSearchIndex(
69
+ searchIndex: SearchIndex,
70
+ tris: SearchTris,
71
+ tins?: TinsBD,
72
+ ) {
73
+ const keys = calcSearchKeys(tris.forw);
74
+ const bakKeys = calcSearchKeys(tris.bakw);
75
+ if (JSON.stringify(keys) != JSON.stringify(bakKeys)) {
76
+ throw `${JSON.stringify(tris, null, 2)}\n${JSON.stringify(keys)}\n${
77
+ JSON.stringify(bakKeys)
78
+ }`;
79
+ }
80
+
81
+ keys.forEach((key) => {
82
+ const current = searchIndex[key];
83
+ if (!current) return;
84
+ const filtered = current.filter((item) => item !== tris);
85
+ if (filtered.length === 0) {
86
+ delete searchIndex[key];
87
+ } else {
88
+ searchIndex[key] = filtered;
89
+ }
90
+ });
91
+
92
+ if (tins) {
93
+ const removeFeature = (collection?: Tins, target?: Tri) => {
94
+ if (!collection || !target) return;
95
+ collection.features = collection.features.filter((tri) => tri !== target);
96
+ };
97
+ removeFeature(tins.forw, tris.forw);
98
+ removeFeature(tins.bakw, tris.bakw);
99
+ }
100
+ }
101
+
102
+ export { calcSearchKeys, insertSearchIndex, removeSearchIndex };