@maplat/tin 0.13.1 → 0.14.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 CHANGED
@@ -1,221 +1,221 @@
1
- # Maplat Tin
2
-
3
- [![CI](https://github.com/code4history/MaplatTin/actions/workflows/ci.yml/badge.svg)](https://github.com/code4history/MaplatTin/actions/workflows/ci.yml)
4
-
5
- A JavaScript library that defines and executes homeomorphic conversion between two plane coordinate systems based on control points.
6
- This is part of the [Maplat](https://github.com/code4history/Maplat/) project.
7
-
8
- 日本語のREADMEは[こちら](./README.ja.md)
9
-
10
- ## Key Features
11
-
12
- - **Generate Transformation Definitions:** Create coordinate transformation definitions based on control points and edge constraints
13
- - **Bidirectional Coordinate Transformation:** Convert coordinates between two planes in both directions
14
- - **Topology Preservation:** Maintains homeomorphic properties during transformation
15
- - **Flexible Configuration:**
16
- - Vertex handling modes (normal/bird's-eye view)
17
- - Topology check modes (strict/auto/loose)
18
- - Y-axis direction control
19
- - **Edge Constraints:** Ability to specify constrained edges for more accurate transformation
20
- - **State Management:** Save and restore transformation definitions
21
-
22
- ## Requirements
23
-
24
- - **Node.js**: 20.0.0 or higher
25
- - **Package Manager**: pnpm 9.0.0 or higher (recommended) or npm
26
-
27
- ## Installation
28
-
29
- ### pnpm (Recommended)
30
-
31
- ```sh
32
- pnpm add @maplat/tin
33
- ```
34
-
35
- ### npm
36
-
37
- ```sh
38
- npm install @maplat/tin
39
- ```
40
-
41
-
42
- ## Development Setup
43
-
44
- ### Testing
45
-
46
- The project includes comprehensive unit tests for both the TIN transformation logic and the internal EdgeBound constraint engine.
47
-
48
- **Run all tests:**
49
- ```sh
50
- pnpm test
51
- ```
52
-
53
- **Run specific test suite:**
54
- ```sh
55
- # EdgeBound constraint engine tests
56
- pnpm test tests/edgebound
57
-
58
- # TIN transformation tests
59
- pnpm test tests/tin
60
- ```
61
-
62
- **Test coverage:**
63
- - `tests/edgebound/` - Internal EdgeBound constraint engine tests (455 tests)
64
- - `constrain.test.ts` - Integration tests for constraint operations
65
- - `constrain.unit.test.ts` - Unit tests for constraint methods
66
- - `intersect.test.ts` - Segment intersection detection tests
67
- - `validators.ts` - Test utilities for validating triangulation integrity
68
- - `tests/tin.test.ts` - TIN transformation tests with real map data
69
- - `tests/transform.test.ts` - Coordinate transformation tests
70
-
71
- ### Building
72
-
73
- Build the library:
74
- ```sh
75
- pnpm run build
76
- ```
77
-
78
- Build the demo site:
79
- ```sh
80
- pnpm run build:demo
81
- ```
82
-
83
- ### Browser
84
-
85
- When using in the browser:
86
-
87
- ```html
88
- <!-- Maplat Tin (UMD) -->
89
- <script src="https://unpkg.com/@maplat/tin/dist/tin.umd.js"></script>
90
- <script>
91
- // Use the library
92
- const tin = new tin.default({
93
- wh: [500, 500]
94
- });
95
- </script>
96
- ```
97
-
98
- Or using ES modules:
99
-
100
- ```html
101
- <!-- Maplat Tin (ESM) -->
102
- <script type="module">
103
- import Tin from "https://unpkg.com/@maplat/tin/dist/index.js";
104
- // Use the library
105
- const tin = new Tin({
106
- wh: [500, 500]
107
- });
108
- </script>
109
- ```
110
-
111
- ## Basic Usage
112
-
113
- ```javascript
114
- // Create a new instance with basic configuration
115
- const tin = new Tin({
116
- wh: [500, 500], // Width and height of the source plane
117
- yaxisMode: Tin.YAXIS_FOLLOW // Y-axis direction handling
118
- });
119
-
120
- // Set control points: Array of [source, target] coordinates
121
- tin.setPoints([
122
- [[100,100], [200, 200]], // Point 1
123
- [[200,200], [400, 400]], // Point 2
124
- [[150,150], [320, 350]], // Point 3
125
- [[200,100], [420, 220]] // Point 4
126
- ]);
127
-
128
- // Initialize the TIN network
129
- tin.updateTin();
130
-
131
- // Check topology status
132
- if (tin.strict_status === Tin.STATUS_STRICT) {
133
- console.log('Topology OK: Roundtrip transform is guaranteed');
134
- } else if (tin.strict_status === Tin.STATUS_LOOSE) {
135
- console.log('Topology warning: Roundtrip transform is not guaranteed');
136
- }
137
-
138
- // Forward transform: source to target
139
- const transformed = tin.transform([160, 160], false);
140
-
141
- // Backward transform: target to source
142
- const restored = tin.transform(transformed, true);
143
- ```
144
-
145
- ## Configuration
146
-
147
- ### Constructor Options
148
-
149
- | **Option** | **Type** | **Description** | **Default** |
150
- | ------------ | --------------------------- | ----------------------------- | ----------- |
151
- | `bounds` | `Position[]` | **Boundary polygon vertices** | `-` |
152
- | `wh` | `number[]` | **Width and height [w, h]** | `-` |
153
- | `vertexMode` | `"plain"|"birdeye"` | **Vertex handling mode** | `"plain"` |
154
- | `strictMode` | `"strict"|"auto"|"loose"` | **Topology check mode** | `"auto"` |
155
- | `yaxisMode` | `"follow"|"invert"` | **Y-axis direction** | `"invert"` |
156
- | `importance` | `number` | **Map importance** | `0` |
157
- | `priority` | `number` | **Map priority** | `0` |
158
-
159
- Either `bounds` or `wh` must be specified.
160
-
161
- ### Methods
162
-
163
- | **Method** | **Description** |
164
- | ---------------------------- | ------------------------------------- |
165
- | `setPoints(points)` | **Set control points** |
166
- | `setEdges(edges)` | **Set constrained edges** |
167
- | `updateTin()` | **Initialize/update TIN network** |
168
- | `transform(coords, inverse)` | **Execute coordinate transformation** |
169
- | `getCompiled()` | **Get serializable state** |
170
- | `setCompiled(state)` | **Restore from serialized state** |
171
- | `getFormatVersion()` | **Get format version number** |
172
-
173
- ### Constants
174
-
175
- **Y-axis Mode:**
176
- - `Tin.YAXIS_FOLLOW` - Y-axis follows the coordinate system
177
- - `Tin.YAXIS_INVERT` - Y-axis is inverted
178
-
179
- **Status Values:**
180
- - `Tin.STATUS_STRICT` - Strict topology (roundtrip transform guaranteed)
181
- - `Tin.STATUS_LOOSE` - Loose topology (roundtrip transform not guaranteed)
182
-
183
- **Format Version:**
184
- - `format_version` - Current TIN format version
185
-
186
- ### Utility Functions
187
-
188
- | **Function** | **Description** |
189
- | --------------------------------------------------------------------- | ----------------------------------------------- |
190
- | `constrainedTin(points, edges, z)` | **Generate constrained Delaunay triangulation** |
191
- | `findIntersections(features)` | **Find intersection points in features** |
192
- | `insertSearchIndex(tins, centroid, strict)` | **Insert search index for TIN** |
193
- | `counterPoint(point)` | **Calculate counter point** |
194
- | `createPoint(xy, mercator, forw)` | **Create point feature** |
195
- | `vertexCalc(points, edgeNodes, centroid, originPoly, xy, vertexMode)` | **Calculate vertex coordinates** |
196
-
197
- ## Documentation
198
-
199
- For detailed information about TIN internals:
200
- - [TIN Internals](docs/tin-internals.md)
201
-
202
- ### Error Handling
203
-
204
- The library may throw errors in the following cases:
205
-
206
- - `"TOO LINEAR1"`,`"TOO LINEAR2"`: Control points are too linear
207
- - `"SOME POINTS OUTSIDE"`: Points outside boundary
208
- - Custom error when attempting backward transformation in a disallowed state
209
-
210
- ## License
211
-
212
- Maplat Limited License 1.1
213
-
214
- Copyright (c) 2024-2026 Code for History
215
-
216
- ## Developers
217
-
218
- - Kohei Otsuka
219
- - Code for History
220
-
221
- We welcome your contributions! Feel free to submit [issues and pull requests](https://github.com/code4history/MaplatTin/issues).
1
+ # Maplat Tin
2
+
3
+ [![CI](https://github.com/code4history/MaplatTin/actions/workflows/ci.yml/badge.svg)](https://github.com/code4history/MaplatTin/actions/workflows/ci.yml)
4
+
5
+ A JavaScript library that defines and executes homeomorphic conversion between two plane coordinate systems based on control points.
6
+ This is part of the [Maplat](https://github.com/code4history/Maplat/) project.
7
+
8
+ 日本語のREADMEは[こちら](./README.ja.md)
9
+
10
+ ## Key Features
11
+
12
+ - **Generate Transformation Definitions:** Create coordinate transformation definitions based on control points and edge constraints
13
+ - **Bidirectional Coordinate Transformation:** Convert coordinates between two planes in both directions
14
+ - **Topology Preservation:** Maintains homeomorphic properties during transformation
15
+ - **Flexible Configuration:**
16
+ - Vertex handling modes (normal/bird's-eye view)
17
+ - Topology check modes (strict/auto/loose)
18
+ - Y-axis direction control
19
+ - **Edge Constraints:** Ability to specify constrained edges for more accurate transformation
20
+ - **State Management:** Save and restore transformation definitions
21
+
22
+ ## Requirements
23
+
24
+ - **Node.js**: 20.0.0 or higher
25
+ - **Package Manager**: pnpm 9.0.0 or higher (recommended) or npm
26
+
27
+ ## Installation
28
+
29
+ ### pnpm (Recommended)
30
+
31
+ ```sh
32
+ pnpm add @maplat/tin
33
+ ```
34
+
35
+ ### npm
36
+
37
+ ```sh
38
+ npm install @maplat/tin
39
+ ```
40
+
41
+
42
+ ## Development Setup
43
+
44
+ ### Testing
45
+
46
+ The project includes comprehensive unit tests for both the TIN transformation logic and the internal EdgeBound constraint engine.
47
+
48
+ **Run all tests:**
49
+ ```sh
50
+ pnpm test
51
+ ```
52
+
53
+ **Run specific test suite:**
54
+ ```sh
55
+ # EdgeBound constraint engine tests
56
+ pnpm test tests/edgebound
57
+
58
+ # TIN transformation tests
59
+ pnpm test tests/tin
60
+ ```
61
+
62
+ **Test coverage:**
63
+ - `tests/edgebound/` - Internal EdgeBound constraint engine tests (455 tests)
64
+ - `constrain.test.ts` - Integration tests for constraint operations
65
+ - `constrain.unit.test.ts` - Unit tests for constraint methods
66
+ - `intersect.test.ts` - Segment intersection detection tests
67
+ - `validators.ts` - Test utilities for validating triangulation integrity
68
+ - `tests/tin.test.ts` - TIN transformation tests with real map data
69
+ - `tests/transform.test.ts` - Coordinate transformation tests
70
+
71
+ ### Building
72
+
73
+ Build the library:
74
+ ```sh
75
+ pnpm run build
76
+ ```
77
+
78
+ Build the demo site:
79
+ ```sh
80
+ pnpm run build:demo
81
+ ```
82
+
83
+ ### Browser
84
+
85
+ When using in the browser:
86
+
87
+ ```html
88
+ <!-- Maplat Tin (UMD) -->
89
+ <script src="https://unpkg.com/@maplat/tin/dist/tin.umd.js"></script>
90
+ <script>
91
+ // Use the library
92
+ const tin = new tin.default({
93
+ wh: [500, 500]
94
+ });
95
+ </script>
96
+ ```
97
+
98
+ Or using ES modules:
99
+
100
+ ```html
101
+ <!-- Maplat Tin (ESM) -->
102
+ <script type="module">
103
+ import Tin from "https://unpkg.com/@maplat/tin/dist/index.js";
104
+ // Use the library
105
+ const tin = new Tin({
106
+ wh: [500, 500]
107
+ });
108
+ </script>
109
+ ```
110
+
111
+ ## Basic Usage
112
+
113
+ ```javascript
114
+ // Create a new instance with basic configuration
115
+ const tin = new Tin({
116
+ wh: [500, 500], // Width and height of the source plane
117
+ yaxisMode: Tin.YAXIS_FOLLOW // Y-axis direction handling
118
+ });
119
+
120
+ // Set control points: Array of [source, target] coordinates
121
+ tin.setPoints([
122
+ [[100,100], [200, 200]], // Point 1
123
+ [[200,200], [400, 400]], // Point 2
124
+ [[150,150], [320, 350]], // Point 3
125
+ [[200,100], [420, 220]] // Point 4
126
+ ]);
127
+
128
+ // Initialize the TIN network
129
+ tin.updateTin();
130
+
131
+ // Check topology status
132
+ if (tin.strict_status === Tin.STATUS_STRICT) {
133
+ console.log('Topology OK: Roundtrip transform is guaranteed');
134
+ } else if (tin.strict_status === Tin.STATUS_LOOSE) {
135
+ console.log('Topology warning: Roundtrip transform is not guaranteed');
136
+ }
137
+
138
+ // Forward transform: source to target
139
+ const transformed = tin.transform([160, 160], false);
140
+
141
+ // Backward transform: target to source
142
+ const restored = tin.transform(transformed, true);
143
+ ```
144
+
145
+ ## Configuration
146
+
147
+ ### Constructor Options
148
+
149
+ | **Option** | **Type** | **Description** | **Default** |
150
+ | ------------ | --------------------------- | ----------------------------- | ----------- |
151
+ | `bounds` | `Position[]` | **Boundary polygon vertices** | `-` |
152
+ | `wh` | `number[]` | **Width and height [w, h]** | `-` |
153
+ | `vertexMode` | `"plain"|"birdeye"` | **Vertex handling mode** | `"plain"` |
154
+ | `strictMode` | `"strict"|"auto"|"loose"` | **Topology check mode** | `"auto"` |
155
+ | `yaxisMode` | `"follow"|"invert"` | **Y-axis direction** | `"invert"` |
156
+ | `importance` | `number` | **Map importance** | `0` |
157
+ | `priority` | `number` | **Map priority** | `0` |
158
+
159
+ Either `bounds` or `wh` must be specified.
160
+
161
+ ### Methods
162
+
163
+ | **Method** | **Description** |
164
+ | ---------------------------- | ------------------------------------- |
165
+ | `setPoints(points)` | **Set control points** |
166
+ | `setEdges(edges)` | **Set constrained edges** |
167
+ | `updateTin()` | **Initialize/update TIN network** |
168
+ | `transform(coords, inverse)` | **Execute coordinate transformation** |
169
+ | `getCompiled()` | **Get serializable state** |
170
+ | `setCompiled(state)` | **Restore from serialized state** |
171
+ | `getFormatVersion()` | **Get format version number** |
172
+
173
+ ### Constants
174
+
175
+ **Y-axis Mode:**
176
+ - `Tin.YAXIS_FOLLOW` - Y-axis follows the coordinate system
177
+ - `Tin.YAXIS_INVERT` - Y-axis is inverted
178
+
179
+ **Status Values:**
180
+ - `Tin.STATUS_STRICT` - Strict topology (roundtrip transform guaranteed)
181
+ - `Tin.STATUS_LOOSE` - Loose topology (roundtrip transform not guaranteed)
182
+
183
+ **Format Version:**
184
+ - `format_version` - Current TIN format version
185
+
186
+ ### Utility Functions
187
+
188
+ | **Function** | **Description** |
189
+ | --------------------------------------------------------------------- | ----------------------------------------------- |
190
+ | `constrainedTin(points, edges, z)` | **Generate constrained Delaunay triangulation** |
191
+ | `findIntersections(features)` | **Find intersection points in features** |
192
+ | `insertSearchIndex(tins, centroid, strict)` | **Insert search index for TIN** |
193
+ | `counterPoint(point)` | **Calculate counter point** |
194
+ | `createPoint(xy, mercator, forw)` | **Create point feature** |
195
+ | `vertexCalc(points, edgeNodes, centroid, originPoly, xy, vertexMode)` | **Calculate vertex coordinates** |
196
+
197
+ ## Documentation
198
+
199
+ For detailed information about TIN internals:
200
+ - [TIN Internals](docs/tin-internals.md)
201
+
202
+ ### Error Handling
203
+
204
+ The library may throw errors in the following cases:
205
+
206
+ - `"TOO LINEAR1"`,`"TOO LINEAR2"`: Control points are too linear
207
+ - `"SOME POINTS OUTSIDE"`: Points outside boundary
208
+ - Custom error when attempting backward transformation in a disallowed state
209
+
210
+ ## License
211
+
212
+ Maplat Limited License 1.1
213
+
214
+ Copyright (c) 2024-2026 Code for History
215
+
216
+ ## Developers
217
+
218
+ - Kohei Otsuka
219
+ - Code for History
220
+
221
+ We welcome your contributions! Feel free to submit [issues and pull requests](https://github.com/code4history/MaplatTin/issues).
@@ -4,24 +4,51 @@ interface ConvexEntry {
4
4
  forw: Position;
5
5
  bakw: Position;
6
6
  }
7
- interface BoundaryVerticesParams {
7
+ /**
8
+ * Unified parameters for all boundary vertex computations (V2 and V3).
9
+ *
10
+ * `allGcps` must include **all** interior points: GCPs from `this.points` **and**
11
+ * edge intermediate nodes from `this.edgeNodes`. This is necessary so that
12
+ * `checkAndAdjustVerticesN` can guarantee that every constrained-edge vertex in
13
+ * bakw space lies inside the boundary polygon.
14
+ */
15
+ export interface BoundaryVerticesParams {
8
16
  convexBuf: Record<string, ConvexEntry>;
9
17
  centroid: {
10
18
  forw: Position;
11
19
  bakw: Position;
12
20
  };
13
- bbox: Position[];
21
+ /** GCPs + edge intermediate nodes */
22
+ allGcps: Array<{
23
+ forw: Position;
24
+ bakw: Position;
25
+ }>;
14
26
  minx: number;
15
27
  maxx: number;
16
28
  miny: number;
17
29
  maxy: number;
18
30
  }
19
31
  /**
20
- * Calculate the standard four boundary vertices around the centroid.
32
+ * Calculate boundary vertices in plain mode.
33
+ *
34
+ * @param params - Input parameters including GCPs, centroid, and bounding box.
35
+ * @param v3 - When true (V3 format), runs the full 36-bin edge vertex pass
36
+ * (Phase 3) in addition to the 4 bbox corners, producing up to 36 vertices.
37
+ * When false (V2 format), returns only the 4 bbox corners.
38
+ *
39
+ * Plain mode uses a single aggregate [scale, rotation] ratio from all GCPs.
21
40
  */
22
- export declare function calculatePlainVertices(params: BoundaryVerticesParams): VertexPosition[];
41
+ export declare function calculatePlainVertices(params: BoundaryVerticesParams, v3?: boolean): VertexPosition[];
23
42
  /**
24
- * Calculate boundary vertices for bird's-eye mode using quadrant-aware ratios.
43
+ * Calculate boundary vertices in bird's-eye mode.
44
+ *
45
+ * @param params - Input parameters including GCPs, centroid, and bounding box.
46
+ * @param v3 - When true (V3 format), runs the full 36-bin edge vertex pass
47
+ * (Phase 3) in addition to the 4 bbox corners, producing up to 36 vertices.
48
+ * When false (V2 format), returns only the 4 bbox corners.
49
+ *
50
+ * Birdeye mode uses per-quadrant [scale, rotation] ratios to capture
51
+ * perspective distortion in the 4 corner positions.
25
52
  */
26
- export declare function calculateBirdeyeVertices(params: BoundaryVerticesParams): VertexPosition[];
53
+ export declare function calculateBirdeyeVertices(params: BoundaryVerticesParams, v3?: boolean): VertexPosition[];
27
54
  export {};