@maplat/tin 0.11.4 → 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.
package/README.md CHANGED
@@ -1,220 +1,215 @@
1
- # Maplat Tin
2
-
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
-
6
- 日本語のREADMEは[こちら](./README.ja.md)
7
-
8
- ## Key Features
9
-
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
19
-
20
- ## Installation
21
-
22
- ### npm
23
-
24
- ```sh
25
- # Install the main package
26
- npm install @maplat/tin
27
-
28
- # Install required dependency
29
- npm install delaunator
30
- ```
31
-
32
- ### JSR (JavaScript Registry)
33
-
34
- ```sh
35
- # For Deno
36
- deno add @maplat/tin
37
-
38
- # For npm/Node.js
39
- npx jsr add @maplat/tin
40
- ```
41
-
42
- ### Deno
43
-
44
- ```typescript
45
- // Using JSR (recommended)
46
- import Tin from "jsr:@maplat/tin";
47
-
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.4";
53
-
54
- // Use the library
55
- const tin = new Tin({
56
- wh: [500, 500],
57
- yaxisMode: Tin.YAXIS_FOLLOW
58
- });
59
- ```
60
-
61
- ## Development Setup
62
-
63
- This project supports using local dependencies for development. We recommend using `yalc` for managing local package dependencies.
64
-
65
- ### Using yalc for Local Development
66
-
67
- 1. **Install yalc globally:**
68
- ```sh
69
- npm install -g yalc
70
- ```
71
-
72
- 2. **Publish local dependencies:**
73
- ```sh
74
- # In the MaplatTransform directory
75
- cd ../MaplatTransform
76
- yalc publish
77
-
78
- # In the MaplatEdgeBound directory
79
- cd ../MaplatEdgeBound
80
- yalc publish
81
- ```
82
-
83
- 3. **Link local dependencies in this project:**
84
- ```sh
85
- # In the MaplatTin directory
86
- yalc add @maplat/transform @maplat/edgebound
87
- ```
88
-
89
- 4. **Update local dependencies when changed:**
90
- ```sh
91
- # In the dependency directory (e.g., MaplatTransform)
92
- yalc publish --push
93
- ```
94
-
95
- 5. **Remove local dependencies (to use npm packages):**
96
- ```sh
97
- # In the MaplatTin directory
98
- yalc remove @maplat/transform @maplat/edgebound
99
- npm install
100
- ```
101
-
102
- **Note:** The `.yalc` directory and `yalc.lock` file are ignored by git and will not be committed.
103
-
104
- ### Browser
105
-
106
- When using in the browser, you need to include the peer dependencies:
107
-
108
- ```html
109
- <!-- Required dependencies -->
110
- <script src="https://unpkg.com/delaunator@5.0.0/delaunator.min.js"></script>
111
- <script src="https://unpkg.com/@turf/helpers@7.2.0/dist/turf-helpers.min.js"></script>
112
- <script src="https://unpkg.com/@turf/boolean-point-in-polygon@7.2.0/dist/turf-boolean-point-in-polygon.min.js"></script>
113
- <script src="https://unpkg.com/@turf/centroid@7.2.0/dist/turf-centroid.min.js"></script>
114
- <script src="https://unpkg.com/@turf/convex@7.2.0/dist/turf-convex.min.js"></script>
115
- <script src="https://unpkg.com/@turf/line-intersect@7.2.0/dist/turf-line-intersect.min.js"></script>
116
-
117
- <!-- Maplat Tin (UMD) -->
118
- <script src="https://unpkg.com/@maplat/tin/dist/tin.umd.js"></script>
119
- <script>
120
- // Use the library
121
- const tin = new tin.default({
122
- wh: [500, 500]
123
- });
124
- </script>
125
- ```
126
-
127
- Or using ES modules:
128
-
129
- ```html
130
- <!-- Maplat Tin (ESM) -->
131
- <script type="module">
132
- import Tin from "https://unpkg.com/@maplat/tin/dist/index.js";
133
- // Use the library
134
- const tin = new Tin({
135
- wh: [500, 500]
136
- });
137
- </script>
138
- ```
139
-
140
- ## Basic Usage
141
-
142
- ```javascript
143
- // Create a new instance with basic configuration
144
- const tin = new Tin({
145
- wh: [500, 500], // Width and height of the source plane
146
- yaxisMode: Tin.YAXIS_FOLLOW // Y-axis direction handling
147
- });
148
-
149
- // Set control points: Array of [source, target] coordinates
150
- tin.setPoints([
151
- [[100,100], [200, 200]], // Point 1
152
- [[200,200], [400, 400]], // Point 2
153
- [[150,150], [320, 350]], // Point 3
154
- [[200,100], [420, 220]] // Point 4
155
- ]);
156
-
157
- // Initialize the TIN network
158
- tin.updateTin();
159
-
160
- // Check topology status
161
- if (tin.strict_status === Tin.STATUS_STRICT) {
162
- console.log('Topology OK: Roundtrip transform is guaranteed');
163
- } else if (tin.strict_status === Tin.STATUS_LOOSE) {
164
- console.log('Topology warning: Roundtrip transform is not guaranteed');
165
- }
166
-
167
- // Forward transform: source to target
168
- const transformed = tin.transform([160, 160], false);
169
-
170
- // Backward transform: target to source
171
- const restored = tin.transform(transformed, true);
172
- ```
173
-
174
- ## Configuration
175
-
176
- ### Constructor Options
177
-
178
- | **Option** | **Type** | **Description** | **Default** |
179
- | ------------- | --------------------------- | ------------------------- | ------------- |
180
- | `bounds` | `Position[]` | **Boundary polygon vertices** | `-` |
181
- | `wh` | `number[]` | **Width and height [w, h]** | `-` |
182
- | `vertexMode` | `"plain"|"birdeye"` | **Vertex handling mode** | `"plain"` |
183
- | `strictMode` | `"strict"|"auto"|"loose"` | **Topology check mode** | `"auto"` |
184
- | `yaxisMode` | `"follow"|"invert"` | **Y-axis direction** | `"invert"` |
185
- | `importance` | `number` | **Map importance** | `0` |
186
- | `priority` | `number` | **Map priority** | `0` |
187
-
188
- Either `bounds` or `wh` must be specified.
189
-
190
- ### Methods
191
-
192
- | **Method** | **Description** |
193
- | ---------------------------- | --------------------------------- |
194
- | `setPoints(points)` | **Set control points** |
195
- | `setEdges(edges)` | **Set constrained edges** |
196
- | `updateTin()` | **Initialize/update TIN network** |
197
- | `transform(coords, inverse)` | **Execute coordinate transformation** |
198
- | `getCompiled()` | **Get serializable state** |
199
- | `setCompiled(state)` | **Restore from serialized state** |
200
-
201
- ### Error Handling
202
-
203
- The library may throw errors in the following cases:
204
-
205
- - `"TOO LINEAR1"`,`"TOO LINEAR2"`: Control points are too linear
206
- - `"SOME POINTS OUTSIDE"`: Points outside boundary
207
- - Custom error when attempting backward transformation in a disallowed state
208
-
209
- ## License
210
-
211
- Maplat Limited License 1.1
212
-
213
- Copyright (c) 2024 Code for History
214
-
215
- ## Developers
216
-
217
- - Kohei Otsuka
218
- - Code for History
219
-
220
- We welcome your contributions! Feel free to submit [issues and pull requests](https://github.com/code4history/MaplatTin/issues).
1
+ # Maplat Tin
2
+
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
+
6
+ 日本語のREADMEは[こちら](./README.ja.md)
7
+
8
+ ## Key Features
9
+
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
19
+
20
+ ## Installation
21
+
22
+ ### npm
23
+
24
+ ```sh
25
+ # Install the main package
26
+ npm install @maplat/tin
27
+
28
+ # Install required dependency
29
+ npm install delaunator
30
+ ```
31
+
32
+
33
+ ## Development Setup
34
+
35
+ This project supports using local dependencies for development. We recommend using `yalc` for managing local package dependencies.
36
+
37
+ ### Using yalc for Local Development
38
+
39
+ 1. **Install yalc globally:**
40
+ ```sh
41
+ npm install -g yalc
42
+ ```
43
+
44
+ 2. **Publish local dependencies:**
45
+ ```sh
46
+ # In the MaplatTransform directory
47
+ cd ../MaplatTransform
48
+ yalc publish
49
+ ```
50
+
51
+ 3. **Link local dependencies in this project:**
52
+ ```sh
53
+ # In the MaplatTin directory
54
+ yalc add @maplat/transform
55
+ ```
56
+
57
+ 4. **Update local dependencies when changed:**
58
+ ```sh
59
+ # In the dependency directory (e.g., MaplatTransform)
60
+ yalc publish --push
61
+ ```
62
+
63
+ 5. **Remove local dependencies (to use npm packages):**
64
+ ```sh
65
+ # In the MaplatTin directory
66
+ yalc remove @maplat/transform
67
+ npm install
68
+ ```
69
+
70
+ **Note:** The `.yalc` directory and `yalc.lock` file are ignored by git and will not be committed.
71
+
72
+ ### Testing
73
+
74
+ The project includes comprehensive unit tests for both the TIN transformation logic and the internal EdgeBound constraint engine.
75
+
76
+ **Run all tests:**
77
+ ```sh
78
+ pnpm test
79
+ ```
80
+
81
+ **Run specific test suite:**
82
+ ```sh
83
+ # EdgeBound constraint engine tests
84
+ pnpm test tests/edgebound
85
+
86
+ # TIN transformation tests
87
+ pnpm test tests/tin
88
+ ```
89
+
90
+ **Test coverage:**
91
+ - `tests/edgebound/` - Internal EdgeBound constraint engine tests (455 tests)
92
+ - `constrain.test.ts` - Integration tests for constraint operations
93
+ - `constrain.unit.test.ts` - Unit tests for constraint methods
94
+ - `intersect.test.ts` - Segment intersection detection tests
95
+ - `validators.ts` - Test utilities for validating triangulation integrity
96
+ - `tests/tin.test.ts` - TIN transformation tests with real map data
97
+ - `tests/transform.test.ts` - Coordinate transformation tests
98
+
99
+ ### Browser
100
+
101
+ When using in the browser, you need to include the peer dependencies:
102
+
103
+ ```html
104
+ <!-- Required dependencies -->
105
+ <script src="https://unpkg.com/delaunator@5.0.0/delaunator.min.js"></script>
106
+ <script src="https://unpkg.com/@turf/helpers@7.2.0/dist/turf-helpers.min.js"></script>
107
+ <script src="https://unpkg.com/@turf/boolean-point-in-polygon@7.2.0/dist/turf-boolean-point-in-polygon.min.js"></script>
108
+ <script src="https://unpkg.com/@turf/centroid@7.2.0/dist/turf-centroid.min.js"></script>
109
+ <script src="https://unpkg.com/@turf/convex@7.2.0/dist/turf-convex.min.js"></script>
110
+ <script src="https://unpkg.com/@turf/line-intersect@7.2.0/dist/turf-line-intersect.min.js"></script>
111
+
112
+ <!-- Maplat Tin (UMD) -->
113
+ <script src="https://unpkg.com/@maplat/tin/dist/tin.umd.js"></script>
114
+ <script>
115
+ // Use the library
116
+ const tin = new tin.default({
117
+ wh: [500, 500]
118
+ });
119
+ </script>
120
+ ```
121
+
122
+ Or using ES modules:
123
+
124
+ ```html
125
+ <!-- Maplat Tin (ESM) -->
126
+ <script type="module">
127
+ import Tin from "https://unpkg.com/@maplat/tin/dist/index.js";
128
+ // Use the library
129
+ const tin = new Tin({
130
+ wh: [500, 500]
131
+ });
132
+ </script>
133
+ ```
134
+
135
+ ## Basic Usage
136
+
137
+ ```javascript
138
+ // Create a new instance with basic configuration
139
+ const tin = new Tin({
140
+ wh: [500, 500], // Width and height of the source plane
141
+ yaxisMode: Tin.YAXIS_FOLLOW // Y-axis direction handling
142
+ });
143
+
144
+ // Set control points: Array of [source, target] coordinates
145
+ tin.setPoints([
146
+ [[100,100], [200, 200]], // Point 1
147
+ [[200,200], [400, 400]], // Point 2
148
+ [[150,150], [320, 350]], // Point 3
149
+ [[200,100], [420, 220]] // Point 4
150
+ ]);
151
+
152
+ // Initialize the TIN network
153
+ tin.updateTin();
154
+
155
+ // Check topology status
156
+ if (tin.strict_status === Tin.STATUS_STRICT) {
157
+ console.log('Topology OK: Roundtrip transform is guaranteed');
158
+ } else if (tin.strict_status === Tin.STATUS_LOOSE) {
159
+ console.log('Topology warning: Roundtrip transform is not guaranteed');
160
+ }
161
+
162
+ // Forward transform: source to target
163
+ const transformed = tin.transform([160, 160], false);
164
+
165
+ // Backward transform: target to source
166
+ const restored = tin.transform(transformed, true);
167
+ ```
168
+
169
+ ## Configuration
170
+
171
+ ### Constructor Options
172
+
173
+ | **Option** | **Type** | **Description** | **Default** |
174
+ | ------------- | --------------------------- | ------------------------- | ------------- |
175
+ | `bounds` | `Position[]` | **Boundary polygon vertices** | `-` |
176
+ | `wh` | `number[]` | **Width and height [w, h]** | `-` |
177
+ | `vertexMode` | `"plain"|"birdeye"` | **Vertex handling mode** | `"plain"` |
178
+ | `strictMode` | `"strict"|"auto"|"loose"` | **Topology check mode** | `"auto"` |
179
+ | `yaxisMode` | `"follow"|"invert"` | **Y-axis direction** | `"invert"` |
180
+ | `importance` | `number` | **Map importance** | `0` |
181
+ | `priority` | `number` | **Map priority** | `0` |
182
+
183
+ Either `bounds` or `wh` must be specified.
184
+
185
+ ### Methods
186
+
187
+ | **Method** | **Description** |
188
+ | ---------------------------- | --------------------------------- |
189
+ | `setPoints(points)` | **Set control points** |
190
+ | `setEdges(edges)` | **Set constrained edges** |
191
+ | `updateTin()` | **Initialize/update TIN network** |
192
+ | `transform(coords, inverse)` | **Execute coordinate transformation** |
193
+ | `getCompiled()` | **Get serializable state** |
194
+ | `setCompiled(state)` | **Restore from serialized state** |
195
+
196
+ ### Error Handling
197
+
198
+ The library may throw errors in the following cases:
199
+
200
+ - `"TOO LINEAR1"`,`"TOO LINEAR2"`: Control points are too linear
201
+ - `"SOME POINTS OUTSIDE"`: Points outside boundary
202
+ - Custom error when attempting backward transformation in a disallowed state
203
+
204
+ ## License
205
+
206
+ Maplat Limited License 1.1
207
+
208
+ Copyright (c) 2024 Code for History
209
+
210
+ ## Developers
211
+
212
+ - Kohei Otsuka
213
+ - Code for History
214
+
215
+ We welcome your contributions! Feel free to submit [issues and pull requests](https://github.com/code4history/MaplatTin/issues).
@@ -0,0 +1,27 @@
1
+ import { Position } from 'geojson';
2
+ import { VertexPosition } from "./types/tin.d";
3
+ interface ConvexEntry {
4
+ forw: Position;
5
+ bakw: Position;
6
+ }
7
+ interface BoundaryVerticesParams {
8
+ convexBuf: Record<string, ConvexEntry>;
9
+ centroid: {
10
+ forw: Position;
11
+ bakw: Position;
12
+ };
13
+ bbox: Position[];
14
+ minx: number;
15
+ maxx: number;
16
+ miny: number;
17
+ maxy: number;
18
+ }
19
+ /**
20
+ * Calculate the standard four boundary vertices around the centroid.
21
+ */
22
+ export declare function calculatePlainVertices(params: BoundaryVerticesParams): VertexPosition[];
23
+ /**
24
+ * Calculate boundary vertices for bird's-eye mode using quadrant-aware ratios.
25
+ */
26
+ export declare function calculateBirdeyeVertices(params: BoundaryVerticesParams): VertexPosition[];
27
+ export {};
@@ -0,0 +1,31 @@
1
+ export interface DelaunatorLike {
2
+ coords: {
3
+ readonly length: number;
4
+ readonly [index: number]: number;
5
+ };
6
+ triangles: {
7
+ readonly length: number;
8
+ [index: number]: number;
9
+ };
10
+ halfedges: {
11
+ readonly length: number;
12
+ [index: number]: number;
13
+ };
14
+ hull: {
15
+ readonly length: number;
16
+ readonly [index: number]: number;
17
+ };
18
+ }
19
+ export declare function nextEdge(edge: number): number;
20
+ export declare function prevEdge(edge: number): number;
21
+ /**
22
+ * Compute if two line segments [p1, p2] and [p3, p4] intersect.
23
+ */
24
+ export declare function intersectSegments(p1x: number, p1y: number, p2x: number, p2y: number, p3x: number, p3y: number, p4x: number, p4y: number): boolean;
25
+ export declare class Base {
26
+ /**
27
+ * The triangulation object from Delaunator.
28
+ */
29
+ del: DelaunatorLike;
30
+ constructor(del: DelaunatorLike);
31
+ }
@@ -0,0 +1,58 @@
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
+ declare abstract class BitSet {
7
+ protected readonly bs: Uint8Array | Uint16Array | Uint32Array;
8
+ protected readonly width: 8 | 16 | 32;
9
+ protected constructor(width: BitSet["width"], bs: BitSet["bs"]);
10
+ /**
11
+ * Add a number to the set.
12
+ *
13
+ * @param idx The number to add. Must be 0 <= idx < len.
14
+ */
15
+ add(idx: number): this;
16
+ /**
17
+ * Delete a number from the set.
18
+ *
19
+ * @param idx The number to delete. Must be 0 <= idx < len.
20
+ */
21
+ delete(idx: number): this;
22
+ /**
23
+ * Add or delete a number in the set, depending on the second argument.
24
+ *
25
+ * @param idx The number to add or delete. Must be 0 <= idx < len.
26
+ * @param val If true, add the number, otherwise delete.
27
+ */
28
+ set(idx: number, val: boolean): boolean;
29
+ /**
30
+ * Whether the number is in the set.
31
+ *
32
+ * @param idx The number to test. Must be 0 <= idx < len.
33
+ */
34
+ has(idx: number): boolean;
35
+ /**
36
+ * Iterate over the numbers that are in the set.
37
+ */
38
+ forEach(fn: (idx: number) => void): this;
39
+ }
40
+ export type { BitSet };
41
+ /**
42
+ * A bit set using 8 bits per cell.
43
+ */
44
+ export declare class BitSet8 extends BitSet {
45
+ constructor(len: number);
46
+ }
47
+ /**
48
+ * A bit set using 16 bits per cell.
49
+ */
50
+ export declare class BitSet16 extends BitSet {
51
+ constructor(len: number);
52
+ }
53
+ /**
54
+ * A bit set using 32 bits per cell.
55
+ */
56
+ export declare class BitSet32 extends BitSet {
57
+ constructor(len: number);
58
+ }
@@ -0,0 +1,68 @@
1
+ import { Base, DelaunatorLike, intersectSegments as baseIntersectSegments } from "./base";
2
+ import { BitSet } from "./bitset";
3
+ export declare class Constrain extends Base {
4
+ vertMap: Uint32Array;
5
+ flips: BitSet;
6
+ consd: BitSet;
7
+ /**
8
+ * Create a Constrain instance.
9
+ *
10
+ * @param del The triangulation output from Delaunator.
11
+ * @param edges If provided, constrain these edges via constrainAll.
12
+ */
13
+ constructor(del: DelaunatorLike, edges?: readonly [number, number][]);
14
+ /**
15
+ * Constrain the triangulation such that there is an edge between p1 and p2.
16
+ */
17
+ constrainOne(segP1: number, segP2: number): number;
18
+ /**
19
+ * Fix the Delaunay condition.
20
+ */
21
+ delaunify(deep?: boolean): this;
22
+ /**
23
+ * Call constrainOne on each edge.
24
+ */
25
+ constrainAll(edges: readonly [number, number][]): this;
26
+ /**
27
+ * Whether an edge is constrained.
28
+ */
29
+ isConstrained(edg: number): boolean;
30
+ /**
31
+ * Find the edge that points from p1 -> p2. If there is only an edge from
32
+ * p2 -> p1 (i.e. it is on the hull), returns the negative id of it.
33
+ */
34
+ findEdge(p1: number, p2: number): number;
35
+ /**
36
+ * Mark an edge as constrained, i.e. should not be touched by `delaunify`.
37
+ */
38
+ private protect;
39
+ /**
40
+ * Mark an edge as flipped unless constrained.
41
+ */
42
+ private markFlip;
43
+ /**
44
+ * Flip the edge shared by two triangles.
45
+ */
46
+ private flipDiagonal;
47
+ /**
48
+ * Whether point p1, p2, and p are collinear.
49
+ */
50
+ private isCollinear;
51
+ /**
52
+ * Whether the triangle formed by p1, p2, p3 keeps px outside the circumcircle.
53
+ */
54
+ private inCircle;
55
+ /**
56
+ * Whether the triangles sharing edg conform to the Delaunay condition.
57
+ */
58
+ private isDelaunay;
59
+ /**
60
+ * Update the vertex -> incoming edge map.
61
+ */
62
+ private updateVert;
63
+ /**
64
+ * Whether the segments between vertices intersect.
65
+ */
66
+ protected intersectSegments(p1: number, p2: number, p3: number, p4: number): boolean;
67
+ static intersectSegments: typeof baseIntersectSegments;
68
+ }
@@ -0,0 +1,4 @@
1
+ export { Constrain } from "./constrain";
2
+ export { nextEdge, prevEdge, intersectSegments, type DelaunatorLike, Base, } from "./base";
3
+ export { BitSet8, BitSet16, BitSet32 } from "./bitset";
4
+ export type { BitSet } from "./bitset";
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  */
7
7
  export { Tin } from "./tin";
8
8
  export type { Options } from "./tin";
9
+ export { Tin as default } from "./tin";
9
10
  export { default as constrainedTin } from "./constrained-tin";
10
11
  export { default as findIntersections } from "./kinks";
11
12
  export { insertSearchIndex } from "./searchutils";