@maplat/tin 0.9.4 → 0.11.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.
package/README.ja.md ADDED
@@ -0,0 +1,176 @@
1
+ # Maplat Tin
2
+
3
+ 2つの平面座標系間で制御点に基づく同相変換を定義・実行するJavaScriptライブラリです。
4
+ [Maplat](https://github.com/code4history/Maplat/)プロジェクトの一部として開発されています。
5
+
6
+ English README is [here](./README.md).
7
+
8
+ ## 主な機能
9
+
10
+ - **座標変換定義の生成:** 制御点とエッジ制約に基づいて座標変換定義を生成
11
+ - **双方向座標変換:** 2つの平面間で双方向の座標変換が可能
12
+ - **位相保存:** 変換時の同相性(トポロジー)を維持
13
+ - **柔軟な設定:**
14
+ - 通常/鳥瞰図モードの頂点処理
15
+ - 厳密/自動/緩和モードのトポロジーチェック
16
+ - Y軸の向きの制御
17
+ - **エッジ制約:** より正確な変換のための制約付きエッジの指定が可能
18
+ - **状態管理:** 変換定義の保存と復元をサポート
19
+
20
+ ## インストール方法
21
+
22
+ ### npm
23
+
24
+ ```sh
25
+ # メインパッケージのインストール
26
+ npm install @maplat/tin
27
+
28
+ # 必要な依存パッケージのインストール
29
+ npm install delaunator
30
+ ```
31
+
32
+ ### JSR (JavaScript Registry)
33
+
34
+ ```sh
35
+ # Deno向け
36
+ deno add @maplat/tin
37
+
38
+ # npm/Node.js向け
39
+ npx jsr add @maplat/tin
40
+ ```
41
+
42
+ ### Deno
43
+
44
+ ```typescript
45
+ // JSR経由(推奨)
46
+ import Tin from "jsr:@maplat/tin";
47
+
48
+ // deno.jsonのimport mapを使用
49
+ import Tin from "@maplat/tin";
50
+
51
+ // またはnpmから直接
52
+ import Tin from "npm:@maplat/tin@0.11.1";
53
+
54
+ // ライブラリを使用
55
+ const tin = new Tin({
56
+ wh: [500, 500],
57
+ yaxisMode: Tin.YAXIS_FOLLOW
58
+ });
59
+ ```
60
+
61
+ ### ブラウザ
62
+
63
+ ブラウザで使用する場合、ピア依存関係を含める必要があります:
64
+
65
+ ```html
66
+ <!-- 必要な依存関係 -->
67
+ <script src="https://unpkg.com/delaunator@5.0.0/delaunator.min.js"></script>
68
+ <script src="https://unpkg.com/@turf/helpers@7.2.0/dist/turf-helpers.min.js"></script>
69
+ <script src="https://unpkg.com/@turf/boolean-point-in-polygon@7.2.0/dist/turf-boolean-point-in-polygon.min.js"></script>
70
+ <script src="https://unpkg.com/@turf/centroid@7.2.0/dist/turf-centroid.min.js"></script>
71
+ <script src="https://unpkg.com/@turf/convex@7.2.0/dist/turf-convex.min.js"></script>
72
+ <script src="https://unpkg.com/@turf/line-intersect@7.2.0/dist/turf-line-intersect.min.js"></script>
73
+
74
+ <!-- Maplat Tin (UMD) -->
75
+ <script src="https://unpkg.com/@maplat/tin/dist/tin.umd.js"></script>
76
+ <script>
77
+ // ライブラリを使用
78
+ const tin = new tin.default({
79
+ wh: [500, 500]
80
+ });
81
+ </script>
82
+ ```
83
+
84
+ またはESモジュールとして使用する場合:
85
+
86
+ ```html
87
+ <!-- Maplat Tin (ESモジュール) -->
88
+ <script type="module">
89
+ import Tin from "https://unpkg.com/@maplat/tin/dist/index.js";
90
+ // ライブラリを使用
91
+ const tin = new Tin({
92
+ wh: [500, 500]
93
+ });
94
+ </script>
95
+ ```
96
+
97
+ ## 基本的な使用方法
98
+
99
+ ```javascript
100
+ // 基本設定でインスタンスを作成
101
+ const tin = new Tin({
102
+ wh: [500, 500], // ソース座標系の幅と高さ
103
+ yaxisMode: Tin.YAXIS_FOLLOW // Y軸の向きの設定
104
+ });
105
+
106
+ // 制御点の設定: [ソース座標, ターゲット座標] の配列
107
+ tin.setPoints([
108
+ [[100,100], [200, 200]], // 点1
109
+ [[200,200], [400, 400]], // 点2
110
+ [[150,150], [320, 350]], // 点3
111
+ [[200,100], [420, 220]] // 点4
112
+ ]);
113
+
114
+ // TINネットワークの初期化
115
+ tin.updateTin();
116
+
117
+ // トポロジーの状態確認
118
+ if (tin.strict_status === Tin.STATUS_STRICT) {
119
+ console.log('トポロジーOK: 往復変換が保証されます');
120
+ } else if (tin.strict_status === Tin.STATUS_LOOSE) {
121
+ console.log('トポロジー警告: 往復変換が保証されません');
122
+ }
123
+
124
+ // 順方向の変換(ソース → ターゲット)
125
+ const transformed = tin.transform([160, 160], false);
126
+
127
+ // 逆方向の変換(ターゲット → ソース)
128
+ const restored = tin.transform(transformed, true);
129
+ ```
130
+
131
+ ## 設定オプション
132
+
133
+ ### コンストラクタオプション
134
+ | **オプション** | **型** | **説明** | **デフォルト** |
135
+ | ------------- | --------------------------- | ------------------------- | ------------- |
136
+ | `bounds` | `Position[]` | **境界多角形の頂点** | `-` |
137
+ | `wh` | `number[]` | **幅と高さ [w, h]** | `-` |
138
+ | `vertexMode` | `"plain"|"birdeye"` | **頂点処理モード** | `"plain"` |
139
+ | `strictMode` | `"strict"|"auto"|"loose"` | **トポロジーチェックモード** | `"auto"` |
140
+ | `yaxisMode` | `"follow"|"invert"` | **Y軸の向き** | `"invert"` |
141
+ | `importance` | `number` | **地図の重要度** | `0` |
142
+ | `priority` | `number` | **地図の優先度** | `0` |
143
+
144
+ `bounds`または`wh`のいずれかを指定する必要があります。
145
+
146
+ ### メソッド
147
+
148
+ | **メソッド** | **説明** |
149
+ | ---------------------------- | --------------------------------- |
150
+ | `setPoints(points)` | **制御点の設定** |
151
+ | `setEdges(edges)` | **制約付きエッジの設定** |
152
+ | `updateTin()` | **TINネットワークの初期化/更新** |
153
+ | `transform(coords, inverse)` | **座標変換の実行** |
154
+ | `getCompiled()` | **シリアライズ可能な状態の取得** |
155
+ | `setCompiled(state)` | **シリアライズされた状態からの復元** |
156
+
157
+ ### エラーハンドリング
158
+
159
+ このライブラリは以下の場合にエラーをスローします:
160
+
161
+ - `"TOO LINEAR1"`,`"TOO LINEAR2"`: 制御点が直線状に並びすぎている
162
+ - `"SOME POINTS OUTSIDE"`: 点が境界外にある
163
+ - 逆変換が許可されていない状態での逆変換実行時
164
+
165
+ ## ライセンス
166
+
167
+ Maplat Limited License 1.1
168
+
169
+ Copyright (c) 2024 Code for History
170
+
171
+ ## 開発者
172
+
173
+ - Kohei Otsuka
174
+ - Code for History
175
+
176
+ あなたの貢献をお待ちしています![イシューやプルリクエスト](https://github.com/code4history/MaplatTin/issues)は大歓迎です。
package/README.md CHANGED
@@ -1,111 +1,177 @@
1
- # Maplat Tin library
1
+ # Maplat Tin
2
2
 
3
- JavaScript library which performs homeomorphic conversion mutually between the coordinate systems of two planes based on the control points.
4
- This is part of [Maplat](https://github.com/code4nara/Maplat/wiki) project.
3
+ A JavaScript library that defines and executes homeomorphic conversion between two plane coordinate systems based on control points.
4
+ This is part of the [Maplat](https://github.com/code4history/Maplat/) project.
5
5
 
6
- ## Installation
6
+ 日本語のREADMEは[こちら](./README.ja.md)
7
+
8
+ ## Key Features
7
9
 
8
- ### node.js
10
+ - **Generate Transformation Definitions:** Create coordinate transformation definitions based on control points and edge constraints
11
+ - **Bidirectional Coordinate Transformation:** Convert coordinates between two planes in both directions
12
+ - **Topology Preservation:** Maintains homeomorphic properties during transformation
13
+ - **Flexible Configuration:**
14
+ - Vertex handling modes (normal/bird's-eye view)
15
+ - Topology check modes (strict/auto/loose)
16
+ - Y-axis direction control
17
+ - **Edge Constraints:** Ability to specify constrained edges for more accurate transformation
18
+ - **State Management:** Save and restore transformation definitions
9
19
 
10
- The easiest way to install maplat_tin is with [`npm`][npm].
20
+ ## Installation
11
21
 
12
- [npm]: https://www.npmjs.com/
22
+ ### npm
13
23
 
14
24
  ```sh
25
+ # Install the main package
15
26
  npm install @maplat/tin
27
+
28
+ # Install required dependency
29
+ npm install delaunator
16
30
  ```
17
31
 
18
- ### Browser
32
+ ### JSR (JavaScript Registry)
19
33
 
20
- Use [maplat_tin.js](https://code4history.github.io/MaplatTin/www/maplat_tin.js)
34
+ ```sh
35
+ # For Deno
36
+ deno add @maplat/tin
21
37
 
22
- ```html
23
- <script type="text/javascript" src="maplat_tin.js"></script>
38
+ # For npm/Node.js
39
+ npx jsr add @maplat/tin
24
40
  ```
25
41
 
42
+ ### Deno
26
43
 
27
- ## How to use (node.js case)
44
+ ```typescript
45
+ // Using JSR (recommended)
46
+ import Tin from "jsr:@maplat/tin";
28
47
 
29
- ```javascript
30
- var Tin = require('maplat_tin');
48
+ // Using deno.json import map
49
+ import Tin from "@maplat/tin";
50
+
51
+ // Or directly from npm
52
+ import Tin from "npm:@maplat/tin@0.11.1";
31
53
 
32
- var tin = new Tin({
54
+ // Use the library
55
+ const tin = new Tin({
33
56
  wh: [500, 500],
34
57
  yaxisMode: Tin.YAXIS_FOLLOW
35
58
  });
59
+ ```
60
+
61
+ ### Browser
62
+
63
+ When using in the browser, you need to include the peer dependencies:
64
+
65
+ ```html
66
+ <!-- Required dependencies -->
67
+ <script src="https://unpkg.com/delaunator@5.0.0/delaunator.min.js"></script>
68
+ <script src="https://unpkg.com/@turf/helpers@7.2.0/dist/turf-helpers.min.js"></script>
69
+ <script src="https://unpkg.com/@turf/boolean-point-in-polygon@7.2.0/dist/turf-boolean-point-in-polygon.min.js"></script>
70
+ <script src="https://unpkg.com/@turf/centroid@7.2.0/dist/turf-centroid.min.js"></script>
71
+ <script src="https://unpkg.com/@turf/convex@7.2.0/dist/turf-convex.min.js"></script>
72
+ <script src="https://unpkg.com/@turf/line-intersect@7.2.0/dist/turf-line-intersect.min.js"></script>
73
+
74
+ <!-- Maplat Tin (UMD) -->
75
+ <script src="https://unpkg.com/@maplat/tin/dist/tin.umd.js"></script>
76
+ <script>
77
+ // Use the library
78
+ const tin = new tin.default({
79
+ wh: [500, 500]
80
+ });
81
+ </script>
82
+ ```
83
+
84
+ Or using ES modules:
85
+
86
+ ```html
87
+ <!-- Maplat Tin (ESM) -->
88
+ <script type="module">
89
+ import Tin from "https://unpkg.com/@maplat/tin/dist/index.js";
90
+ // Use the library
91
+ const tin = new Tin({
92
+ wh: [500, 500]
93
+ });
94
+ </script>
95
+ ```
96
+
97
+ ## Basic Usage
36
98
 
99
+ ```javascript
100
+ // Create a new instance with basic configuration
101
+ const tin = new Tin({
102
+ wh: [500, 500], // Width and height of the source plane
103
+ yaxisMode: Tin.YAXIS_FOLLOW // Y-axis direction handling
104
+ });
105
+
106
+ // Set control points: Array of [source, target] coordinates
37
107
  tin.setPoints([
38
- [[100,100], [200, 200]],
39
- [[200,200], [400, 400]],
40
- [[150,150], [320, 350]],
41
- [[200,100], [420, 220]]
108
+ [[100,100], [200, 200]], // Point 1
109
+ [[200,200], [400, 400]], // Point 2
110
+ [[150,150], [320, 350]], // Point 3
111
+ [[200,100], [420, 220]] // Point 4
42
112
  ]);
43
- tin.updateTinAsync().then(function() {
44
- if (tin.strict_status == Tin.STATUS_STRICT) {
45
- console.log('Topology OK: In this case, roundtrip transform is guaranteed');
46
- } else if (tin.strict_status == Tin.STATUS_LOOSE) {
47
- console.log('Topology error: In this case, roundtrip transform is not guaranteed');
48
- } else {
49
- console.log('Something wrong');
50
- }
51
-
52
- //Forward transform
53
- var ord = tin.transform([160, 160], false);
54
- console.log(ord);
55
- //ord => [336.0769755832048, 360.048109739503]
56
-
57
- //Backward transform
58
- var rev = tin.transform(ord, true);
59
- console.log(rev);
60
- //rev => [160, 160]
61
-
62
- //Solving triangulated irregular network from many points by scrach takes too many time.
63
- //So, there are object-serialization method to store solved instance.
64
-
65
- var compiled = tin.getCompiled();
66
-
67
- var tin2 = new Tin();
68
- tin2.setCompiled(compiled);
69
- // Compiled triangulated irregular network was set. no "updateTinAsync" call is need.
70
-
71
- var ord2 = tin.transform([160, 160], false);
72
- console.log(ord2);
73
- var rev2 = tin.transform(ord2, true);
74
- console.log(rev2);
75
- //Same results with ord, rev
76
- }).catch(function(e) {
77
- if (e == 'TOO LINEAR1' || e == 'TOO LINEAR2') {
78
- console.log('Given GCP points are too linear, fail to create triangulated irregular network.');
79
- } else {
80
- console.log('Something wrong');
81
- }
82
- });
113
+
114
+ // Initialize the TIN network
115
+ tin.updateTin();
116
+
117
+ // Check topology status
118
+ if (tin.strict_status === Tin.STATUS_STRICT) {
119
+ console.log('Topology OK: Roundtrip transform is guaranteed');
120
+ } else if (tin.strict_status === Tin.STATUS_LOOSE) {
121
+ console.log('Topology warning: Roundtrip transform is not guaranteed');
122
+ }
123
+
124
+ // Forward transform: source to target
125
+ const transformed = tin.transform([160, 160], false);
126
+
127
+ // Backward transform: target to source
128
+ const restored = tin.transform(transformed, true);
83
129
  ```
84
130
 
85
- ## Options
131
+ ## Configuration
132
+
133
+ ### Constructor Options
134
+
135
+ | **Option** | **Type** | **Description** | **Default** |
136
+ | ------------- | --------------------------- | ------------------------- | ------------- |
137
+ | `bounds` | `Position[]` | **Boundary polygon vertices** | `-` |
138
+ | `wh` | `number[]` | **Width and height [w, h]** | `-` |
139
+ | `vertexMode` | `"plain"|"birdeye"` | **Vertex handling mode** | `"plain"` |
140
+ | `strictMode` | `"strict"|"auto"|"loose"` | **Topology check mode** | `"auto"` |
141
+ | `yaxisMode` | `"follow"|"invert"` | **Y-axis direction** | `"invert"` |
142
+ | `importance` | `number` | **Map importance** | `0` |
143
+ | `priority` | `number` | **Map priority** | `0` |
144
+
145
+ Either `bounds` or `wh` must be specified.
146
+
147
+ ### Methods
148
+
149
+ | **Method** | **Description** |
150
+ | ---------------------------- | --------------------------------- |
151
+ | `setPoints(points)` | **Set control points** |
152
+ | `setEdges(edges)` | **Set constrained edges** |
153
+ | `updateTin()` | **Initialize/update TIN network** |
154
+ | `transform(coords, inverse)` | **Execute coordinate transformation** |
155
+ | `getCompiled()` | **Get serializable state** |
156
+ | `setCompiled(state)` | **Restore from serialized state** |
86
157
 
87
- ### wh (method: setWh)
88
- Set width and height of the first coordinate system.
89
- *NOTE: If setWh was called, TIN network is reset. So need to call updateTinAsync() again.*
158
+ ### Error Handling
90
159
 
91
- ### yaxisMode
92
- Set *Tin.YAXIS_FOLLOW* if the y coordinate of the first coordinate system is the same as the direction of the y axis of the second coordinate system, and set *Tin.YAXIS_INVERT* if it is in the opposite direction.
93
- Default value is *Tin.YAXIS_INVERT*.
160
+ The library may throw errors in the following cases:
94
161
 
95
- ### points (method: setPoints)
96
- A set of corresponding points in the first coordinate system and the second coordinate system is specified as an array. Minimum 3 points required. Also, if the alignment of points is too linear, TIN can not be calculated at updateTinAsync and an error occurs.
97
- *NOTE: If setPoints was called, TIN network is reset. So need to call updateTinAsync() again.*
162
+ - `"TOO LINEAR1"`,`"TOO LINEAR2"`: Control points are too linear
163
+ - `"SOME POINTS OUTSIDE"`: Points outside boundary
164
+ - Custom error when attempting backward transformation in a disallowed state
98
165
 
99
- ## Methods
166
+ ## License
100
167
 
101
- ### updateTinAsync()
102
- Return value is Promise. Calcurate TIN asynchronously. Before calling *transform*, this method needs to be completed.
168
+ Maplat Limited License 1.1
103
169
 
104
- ### transform(xy, inverse)
105
- Convert the coordinates. If the value of *inverse* is false, the direction of conversion is from the first coordinate system to the second one, if it is true, the direction of conversion is reverse direction.
170
+ Copyright (c) 2024 Code for History
106
171
 
107
- ## Properties
172
+ ## Developers
108
173
 
109
- ### strict_status
174
+ - Kohei Otsuka
175
+ - Code for History
110
176
 
111
- Confirm after completion of *updateTinAsync()*. When the value is *Tin.STATUS_STRICT*, bijection conversion is guaranteed. In the case of *Tin.STATUS_LOOSE* it is not guaranteed.
177
+ We welcome your contributions! Feel free to submit [issues and pull requests](https://github.com/code4history/MaplatTin/issues).
@@ -0,0 +1,29 @@
1
+ import { FeatureCollection, Point, Polygon } from 'geojson';
2
+ import { Edge } from '@maplat/transform';
3
+ /**
4
+ * 制約付きTIN(Triangulated Irregular Network)を生成します
5
+ *
6
+ * 点群から三角形分割を行い、指定されたエッジ(制約線)を
7
+ * 必ず三角形の辺として含むように調整します。
8
+ *
9
+ * @param points - 点群を含むFeatureCollection
10
+ * 各Featureは座標を持つPointである必要があります
11
+ * @param edges - 制約として扱うエッジの配列
12
+ * 各エッジは2つの頂点インデックスの組として指定
13
+ * @param z - 生成される三角形のプロパティに含める項目の名前
14
+ * (例: "target")
15
+ *
16
+ * @returns 三角形のFeatureCollectionを返します
17
+ * 各三角形は以下の情報を持ちます:
18
+ * - geometry: 三角形の座標
19
+ * - properties: 頂点に関連付けられた情報(a, b, cプロパティ)
20
+ *
21
+ * @throws {Error} pointsがFeatureCollectionでない場合
22
+ * @throws {Error} edgesが配列でない場合
23
+ *
24
+ * 処理の流れ:
25
+ * 1. Delaunay三角分割を実行
26
+ * 2. 制約エッジに基づいて三角分割を調整
27
+ * 3. 結果をGeoJSON形式に変換
28
+ */
29
+ export default function (points: FeatureCollection<Point>, edges: Edge[], z: string): FeatureCollection<Polygon>;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @maplat/tin - TIN (Triangulated Irregular Network) generation library
3
+ *
4
+ * This is the main entry point for the library.
5
+ * Provides TIN generation with constrained edges for map transformation.
6
+ */
7
+ export { Tin } from "./tin";
8
+ export type { Options } from "./tin";
9
+ export { default as constrainedTin } from "./constrained-tin";
10
+ export { default as findIntersections } from "./kinks";
11
+ export { insertSearchIndex } from "./searchutils";
12
+ export { createPoint, counterPoint, vertexCalc } from "./vertexutils";
13
+ export type { SearchIndex } from "./searchutils";
14
+ export type { IntersectionPoint } from "./types/kinks";
15
+ export type { TriangleProperties } from "./types/tin";
16
+ export type { Compiled, Edge, EdgeSet, EdgeSetLegacy, PointSet, PropertyTriKey, StrictMode, StrictStatus, Tri, Tins, Transform, VertexMode, YaxisMode } from '@maplat/transform';
17
+ export { format_version } from '@maplat/transform';
@@ -0,0 +1,42 @@
1
+ <!DOCTYPE html>
2
+ <html lang="ja">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>MaplatTin Test</title>
6
+ <script src="https://unpkg.com/@turf/turf/turf.min.js"></script>
7
+ <script src="https://unpkg.com/delaunator/delaunator.min.js"></script>
8
+ <script src="https://unpkg.com/@kninnug/constrainautor/lib/Constrainautor.min.js"></script>
9
+ </head>
10
+ <body>
11
+ <h1>MaplatTin Test</h1>
12
+ <div id="output"></div>
13
+ <script type="module">
14
+ import Tin from '/src/index.ts';
15
+
16
+ const tin = new Tin({
17
+ bounds: [[100, 50], [150, 150], [150, 200], [60, 190], [50, 100]],
18
+ strictMode: Tin.MODE_STRICT
19
+ });
20
+
21
+ tin.setPoints([
22
+ [[80, 90], [160, -90]],
23
+ [[120, 120], [240, -120]],
24
+ [[100, 140], [200, -140]],
25
+ [[130, 180], [260, -180]],
26
+ [[70, 150], [140, -150]]
27
+ ]);
28
+
29
+ tin.updateTinAsync().then(() => {
30
+ const output = document.getElementById('output');
31
+ const result1 = tin.transform([140, 150]);
32
+ const result2 = tin.transform(result1, true);
33
+
34
+ output.innerHTML = `
35
+ <h2>Test Results:</h2>
36
+ <p>Forward transform [140, 150] => [${result1.join(', ')}]</p>
37
+ <p>Backward transform [${result1.join(', ')}] => [${result2.join(', ')}]</p>
38
+ `;
39
+ });
40
+ </script>
41
+ </body>
42
+ </html>
@@ -0,0 +1,8 @@
1
+ import { Position, Feature, Point } from 'geojson';
2
+ /**
3
+ * 線分の交差点を検出するメインの関数
4
+ *
5
+ * @param coords - 線分群の座標配列。各線分は始点と終点の座標で表現
6
+ * @returns 検出された交差点のFeature配列
7
+ */
8
+ export default function findIntersections(coords: Position[][]): Feature<Point>[];
@@ -0,0 +1,21 @@
1
+ import { Tri, Tins } from '@maplat/transform';
2
+ type BiDirectionKey = "forw" | "bakw";
3
+ type TinsBD = {
4
+ [key in BiDirectionKey]?: Tins;
5
+ };
6
+ type SearchTris = {
7
+ [key in BiDirectionKey]: Tri;
8
+ };
9
+ export type SearchIndex = {
10
+ [key: string]: SearchTris[];
11
+ };
12
+ /**
13
+ * 検索インデックスに三角形を追加する
14
+ *
15
+ * @param searchIndex 検索インデックス
16
+ * @param tris 追加する三角形ペア
17
+ * @param tins (オプション)追加先の三角形群
18
+ * @throws キーの不一致エラー
19
+ */
20
+ declare function insertSearchIndex(searchIndex: SearchIndex, tris: SearchTris, tins?: TinsBD): void;
21
+ export { insertSearchIndex };