@vim3d/geojson 0.4.0 → 0.5.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 +14 -0
- package/esm/index.js +1 -1
- package/esm/scene.js +183 -30
- package/index.d.ts +2 -2
- package/index.js +4 -1
- package/package.json +3 -1
- package/scene.d.ts +44 -2
- package/scene.js +189 -30
package/README.md
CHANGED
|
@@ -48,4 +48,18 @@ comes out of `maxPositions: 5000` with all 41 polygons kept.
|
|
|
48
48
|
- `toSceneCurves(input, origin, { elevation })`: lines and polygon rings as
|
|
49
49
|
`curve` geometries in the Vim3D `world` frame around `origin` (x east, y up,
|
|
50
50
|
-z north), with each feature's `properties` and `id`.
|
|
51
|
+
- `toScenePoints(input, origin, { elevation })`: every `Point` and
|
|
52
|
+
`MultiPoint` position in the same frame, with its feature's `properties`
|
|
53
|
+
and `id`, for labels, trees and other markers.
|
|
54
|
+
- `toSceneFills(input, origin, { elevation })`: each polygon (each
|
|
55
|
+
`MultiPolygon` part on its own) as a flat, upward-facing `mesh` geometry
|
|
56
|
+
with its holes cut out, triangulated with
|
|
57
|
+
[earcut](https://www.npmjs.com/package/earcut). It has the same
|
|
58
|
+
`positions`/`indices` shape the `geo:mesh` node outputs.
|
|
59
|
+
- `toSceneRibbons(input, origin, { width, widthProperty, elevation })`: each
|
|
60
|
+
line as a flat, upward-facing strip around its centerline, for streets.
|
|
61
|
+
The width is in metres: `properties[widthProperty]` when it is a positive
|
|
62
|
+
number, else `width` (default 1). Corners are mitered up to twice the half
|
|
63
|
+
width and beveled past that, or when a short segment would make the strip
|
|
64
|
+
fold. A line that ends where it starts joins up.
|
|
51
65
|
- `simplifyLine`, `simplifyRing`, `countPositions`, `bbox`, `eachPosition`.
|
package/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const name = '@vim3d/geojson';
|
|
2
2
|
export { loadGeoJSON } from './fetch.js';
|
|
3
3
|
export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
|
|
4
|
-
export { toSceneCurves } from './scene.js';
|
|
4
|
+
export { toSceneCurves, toSceneFills, toScenePoints, toSceneRibbons } from './scene.js';
|
|
5
5
|
export { simplifyLine, simplifyRing } from './simplify.js';
|
|
6
6
|
export { borders } from './topology.js';
|
package/esm/scene.js
CHANGED
|
@@ -1,25 +1,45 @@
|
|
|
1
1
|
import { geodeticToScene } from '@vim3d/core';
|
|
2
|
-
|
|
2
|
+
import earcut from 'earcut';
|
|
3
|
+
function eachFeature(input, emit) {
|
|
4
|
+
if (input.type === 'FeatureCollection')
|
|
5
|
+
input.features.forEach(feature => emit(feature.geometry, feature.properties, feature.id));
|
|
6
|
+
else if (input.type === 'Feature')
|
|
7
|
+
emit(input.geometry, input.properties, input.id);
|
|
8
|
+
else
|
|
9
|
+
emit(input, null);
|
|
10
|
+
}
|
|
11
|
+
function eachGeometry(geometry, visit) {
|
|
3
12
|
if (!geometry)
|
|
4
13
|
return;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
if (geometry.type === 'GeometryCollection')
|
|
15
|
+
geometry.geometries.forEach(child => eachGeometry(child, visit));
|
|
16
|
+
else
|
|
17
|
+
visit(geometry);
|
|
18
|
+
}
|
|
19
|
+
const finite = ([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat);
|
|
20
|
+
function projector(origin, options) {
|
|
21
|
+
const elevation = options.elevation ?? origin.elevation;
|
|
22
|
+
return (positions) => positions.filter(finite)
|
|
23
|
+
.map(([lon, lat, height]) => geodeticToScene(origin, lat, lon, Number.isFinite(height) ? height : elevation));
|
|
24
|
+
}
|
|
25
|
+
function lines(geometry, emit) {
|
|
26
|
+
eachGeometry(geometry, child => {
|
|
27
|
+
switch (child.type) {
|
|
28
|
+
case 'LineString':
|
|
29
|
+
emit(child.coordinates, false);
|
|
30
|
+
break;
|
|
31
|
+
case 'MultiLineString':
|
|
32
|
+
child.coordinates.forEach(line => emit(line, false));
|
|
33
|
+
break;
|
|
34
|
+
case 'Polygon':
|
|
35
|
+
child.coordinates.forEach(ring => emit(ring, true));
|
|
36
|
+
break;
|
|
37
|
+
case 'MultiPolygon':
|
|
38
|
+
child.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
|
|
39
|
+
break;
|
|
40
|
+
default: break;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
23
43
|
}
|
|
24
44
|
/**
|
|
25
45
|
* Turns every line and polygon ring into a scene-space curve around `origin`,
|
|
@@ -28,22 +48,155 @@ function lines(geometry, emit) {
|
|
|
28
48
|
*/
|
|
29
49
|
export function toSceneCurves(input, origin, options = {}) {
|
|
30
50
|
const curves = [];
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
const vertices = line
|
|
34
|
-
.map(([lon, lat, height]) => geodeticToScene(origin, lat, lon, Number.isFinite(height) ? height : elevation));
|
|
51
|
+
const project = projector(origin, options);
|
|
52
|
+
eachFeature(input, (geometry, properties, id) => lines(geometry, (line, closed) => {
|
|
53
|
+
const vertices = project(line);
|
|
35
54
|
if (vertices.length < 2)
|
|
36
55
|
return;
|
|
37
56
|
const [first, last] = [vertices[0], vertices[vertices.length - 1]];
|
|
38
57
|
if (closed && first.some((v, i) => v !== last[i]))
|
|
39
58
|
vertices.push(first);
|
|
40
59
|
curves.push({ geometry: { primitive: 'curve', args: [], vertices }, properties, id, closed });
|
|
41
|
-
});
|
|
42
|
-
if (input.type === 'FeatureCollection')
|
|
43
|
-
input.features.forEach(feature => add(feature.geometry, feature.properties, feature.id));
|
|
44
|
-
else if (input.type === 'Feature')
|
|
45
|
-
add(input.geometry, input.properties, input.id);
|
|
46
|
-
else
|
|
47
|
-
add(input, null);
|
|
60
|
+
}));
|
|
48
61
|
return curves;
|
|
49
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Every `Point` and `MultiPoint` position as a scene-space point with its
|
|
65
|
+
* feature's `properties` and `id`, for labels, trees and other markers.
|
|
66
|
+
*/
|
|
67
|
+
export function toScenePoints(input, origin, options = {}) {
|
|
68
|
+
const points = [];
|
|
69
|
+
const project = projector(origin, options);
|
|
70
|
+
eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
|
|
71
|
+
const positions = child.type === 'Point' ? [child.coordinates] : child.type === 'MultiPoint' ? child.coordinates : [];
|
|
72
|
+
for (const position of project(positions))
|
|
73
|
+
points.push({ position, properties, id });
|
|
74
|
+
}));
|
|
75
|
+
return points;
|
|
76
|
+
}
|
|
77
|
+
function mesh(positions, indices) {
|
|
78
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
79
|
+
const [a, b, c] = [indices[i] * 3, indices[i + 1] * 3, indices[i + 2] * 3];
|
|
80
|
+
const up = (positions[b + 2] - positions[a + 2]) * (positions[c] - positions[a])
|
|
81
|
+
- (positions[b] - positions[a]) * (positions[c + 2] - positions[a + 2]);
|
|
82
|
+
if (up < 0)
|
|
83
|
+
[indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
|
|
84
|
+
}
|
|
85
|
+
return { primitive: 'mesh', args: [], positions, indices };
|
|
86
|
+
}
|
|
87
|
+
function openRing(ring) {
|
|
88
|
+
const [first, last] = [ring[0], ring[ring.length - 1]];
|
|
89
|
+
return ring.length > 1 && first[0] === last[0] && first[2] === last[2] ? ring.slice(0, -1) : ring;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Every polygon (each part of a `MultiPolygon` on its own) as a flat,
|
|
93
|
+
* upward-facing triangle mesh with its holes cut out.
|
|
94
|
+
*/
|
|
95
|
+
export function toSceneFills(input, origin, options = {}) {
|
|
96
|
+
const meshes = [];
|
|
97
|
+
const project = projector(origin, options);
|
|
98
|
+
const fill = (rings, properties, id) => {
|
|
99
|
+
const [outer, ...holes] = rings.map(ring => openRing(project(ring)));
|
|
100
|
+
if (!outer || outer.length < 3)
|
|
101
|
+
return;
|
|
102
|
+
const kept = [outer, ...holes.filter(hole => hole.length >= 3)];
|
|
103
|
+
const positions = [];
|
|
104
|
+
const flat = [];
|
|
105
|
+
const starts = [];
|
|
106
|
+
for (const ring of kept) {
|
|
107
|
+
if (ring !== outer)
|
|
108
|
+
starts.push(positions.length / 3);
|
|
109
|
+
for (const [x, y, z] of ring) {
|
|
110
|
+
positions.push(x, y, z);
|
|
111
|
+
flat.push(x, z);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const indices = earcut(flat, starts.length ? starts : undefined, 2);
|
|
115
|
+
if (indices.length)
|
|
116
|
+
meshes.push({ geometry: mesh(positions, indices), properties, id });
|
|
117
|
+
};
|
|
118
|
+
eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
|
|
119
|
+
if (child.type === 'Polygon')
|
|
120
|
+
fill(child.coordinates, properties, id);
|
|
121
|
+
else if (child.type === 'MultiPolygon')
|
|
122
|
+
child.coordinates.forEach(polygon => fill(polygon, properties, id));
|
|
123
|
+
}));
|
|
124
|
+
return meshes;
|
|
125
|
+
}
|
|
126
|
+
const MITER_LIMIT = 2;
|
|
127
|
+
function ribbon(line, width) {
|
|
128
|
+
let points = line.filter((p, i) => i === 0 || p[0] !== line[i - 1][0] || p[2] !== line[i - 1][2]);
|
|
129
|
+
const [first, last] = [points[0], points[points.length - 1]];
|
|
130
|
+
const closed = points.length > 3 && first[0] === last[0] && first[2] === last[2];
|
|
131
|
+
if (closed)
|
|
132
|
+
points = points.slice(0, -1);
|
|
133
|
+
if (points.length < 2)
|
|
134
|
+
return null;
|
|
135
|
+
const count = points.length;
|
|
136
|
+
const half = width / 2;
|
|
137
|
+
const segments = closed ? count : count - 1;
|
|
138
|
+
const normals = [];
|
|
139
|
+
const lengths = [];
|
|
140
|
+
for (let i = 0; i < segments; i++) {
|
|
141
|
+
const [p, q] = [points[i], points[(i + 1) % count]];
|
|
142
|
+
const [dx, dz] = [q[0] - p[0], q[2] - p[2]];
|
|
143
|
+
const length = Math.hypot(dx, dz);
|
|
144
|
+
normals.push([-dz / length, dx / length]);
|
|
145
|
+
lengths.push(length);
|
|
146
|
+
}
|
|
147
|
+
const sections = [];
|
|
148
|
+
const section = ([x, y, z], [nx, nz], scale) => sections.push([x, y, z, nx * scale, nz * scale]);
|
|
149
|
+
points.forEach((point, i) => {
|
|
150
|
+
const before = closed ? (i + count - 1) % count : i - 1;
|
|
151
|
+
const after = i < segments ? i : -1;
|
|
152
|
+
if (before < 0 || after < 0)
|
|
153
|
+
return section(point, normals[Math.max(before, after)], half);
|
|
154
|
+
const [a, b] = [normals[before], normals[after]];
|
|
155
|
+
const [mx, mz] = [a[0] + b[0], a[1] + b[1]];
|
|
156
|
+
const length = Math.hypot(mx, mz);
|
|
157
|
+
const cos = length / 2;
|
|
158
|
+
const reach = cos > 0 ? half * Math.sqrt(Math.max(0, 1 - cos * cos)) / cos : Infinity;
|
|
159
|
+
if (cos >= 1 / MITER_LIMIT && reach <= Math.min(lengths[before], lengths[after])) {
|
|
160
|
+
section(point, [mx / length, mz / length], half / cos);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
section(point, a, half);
|
|
164
|
+
section(point, b, half);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
if (closed)
|
|
168
|
+
sections.push(sections[0]);
|
|
169
|
+
const positions = [];
|
|
170
|
+
const indices = [];
|
|
171
|
+
sections.forEach(([x, y, z, ox, oz], i) => {
|
|
172
|
+
positions.push(x + ox, y, z + oz, x - ox, y, z - oz);
|
|
173
|
+
if (i > 0) {
|
|
174
|
+
const k = 2 * (i - 1);
|
|
175
|
+
indices.push(k, k + 1, k + 2, k + 1, k + 3, k + 2);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return mesh(positions, indices);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Every line (not polygon rings) as a flat, upward-facing strip of the given
|
|
182
|
+
* width around its centerline, for streets and paths. Corners are mitered;
|
|
183
|
+
* corners sharper than the miter limit, or whose miter would reach past the
|
|
184
|
+
* next vertex, are beveled. A line that ends where it starts joins up.
|
|
185
|
+
*/
|
|
186
|
+
export function toSceneRibbons(input, origin, options = {}) {
|
|
187
|
+
const meshes = [];
|
|
188
|
+
const project = projector(origin, options);
|
|
189
|
+
eachFeature(input, (geometry, properties, id) => {
|
|
190
|
+
const value = options.widthProperty ? properties?.[options.widthProperty] : undefined;
|
|
191
|
+
const width = typeof value === 'number' && value > 0 && Number.isFinite(value) ? value : options.width ?? 1;
|
|
192
|
+
eachGeometry(geometry, child => {
|
|
193
|
+
const parts = child.type === 'LineString' ? [child.coordinates] : child.type === 'MultiLineString' ? child.coordinates : [];
|
|
194
|
+
for (const part of parts) {
|
|
195
|
+
const strip = ribbon(project(part), width);
|
|
196
|
+
if (strip)
|
|
197
|
+
meshes.push({ geometry: strip, properties, id });
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
return meshes;
|
|
202
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ export type { LoadGeoJSONOptions } from './fetch.js';
|
|
|
3
3
|
export { loadGeoJSON } from './fetch.js';
|
|
4
4
|
export type { ResampleOptions } from './resample.js';
|
|
5
5
|
export { bbox, countPositions, eachPosition, resampleGeoJSON } from './resample.js';
|
|
6
|
-
export type { SceneCurve, SceneCurveOptions } from './scene.js';
|
|
7
|
-
export { toSceneCurves } from './scene.js';
|
|
6
|
+
export type { MeshGeometry, SceneCurve, SceneCurveOptions, SceneMesh, ScenePoint, SceneRibbonOptions } from './scene.js';
|
|
7
|
+
export { toSceneCurves, toSceneFills, toScenePoints, toSceneRibbons } from './scene.js';
|
|
8
8
|
export { simplifyLine, simplifyRing } from './simplify.js';
|
|
9
9
|
export { borders } from './topology.js';
|
|
10
10
|
export type * from './types.js';
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.borders = exports.simplifyRing = exports.simplifyLine = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
|
|
3
|
+
exports.borders = exports.simplifyRing = exports.simplifyLine = exports.toSceneRibbons = exports.toScenePoints = exports.toSceneFills = exports.toSceneCurves = exports.resampleGeoJSON = exports.eachPosition = exports.countPositions = exports.bbox = exports.loadGeoJSON = exports.name = void 0;
|
|
4
4
|
exports.name = '@vim3d/geojson';
|
|
5
5
|
var fetch_js_1 = require("./fetch.js");
|
|
6
6
|
Object.defineProperty(exports, "loadGeoJSON", { enumerable: true, get: function () { return fetch_js_1.loadGeoJSON; } });
|
|
@@ -11,6 +11,9 @@ Object.defineProperty(exports, "eachPosition", { enumerable: true, get: function
|
|
|
11
11
|
Object.defineProperty(exports, "resampleGeoJSON", { enumerable: true, get: function () { return resample_js_1.resampleGeoJSON; } });
|
|
12
12
|
var scene_js_1 = require("./scene.js");
|
|
13
13
|
Object.defineProperty(exports, "toSceneCurves", { enumerable: true, get: function () { return scene_js_1.toSceneCurves; } });
|
|
14
|
+
Object.defineProperty(exports, "toSceneFills", { enumerable: true, get: function () { return scene_js_1.toSceneFills; } });
|
|
15
|
+
Object.defineProperty(exports, "toScenePoints", { enumerable: true, get: function () { return scene_js_1.toScenePoints; } });
|
|
16
|
+
Object.defineProperty(exports, "toSceneRibbons", { enumerable: true, get: function () { return scene_js_1.toSceneRibbons; } });
|
|
14
17
|
var simplify_js_1 = require("./simplify.js");
|
|
15
18
|
Object.defineProperty(exports, "simplifyLine", { enumerable: true, get: function () { return simplify_js_1.simplifyLine; } });
|
|
16
19
|
Object.defineProperty(exports, "simplifyRing", { enumerable: true, get: function () { return simplify_js_1.simplifyRing; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vim3d/geojson",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Fetch GeoJSON at load time and resample it for Vim3D scenes.",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/geojson",
|
|
@@ -22,9 +22,11 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@constructive-io/fetch": "^1.2.2",
|
|
25
|
+
"earcut": "^2.2.4",
|
|
25
26
|
"@vim3d/core": "^0.6.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
29
|
+
"@types/earcut": "^2.1.4",
|
|
28
30
|
"makage": "^0.8.0"
|
|
29
31
|
},
|
|
30
32
|
"scripts": {
|
package/scene.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type GeometrySnapshot, type GeoOrigin } from '@vim3d/core';
|
|
1
|
+
import { type GeometrySnapshot, type GeoOrigin, type Vec3 } from '@vim3d/core';
|
|
2
2
|
import type { GeoJSON, Properties } from './types.js';
|
|
3
3
|
export interface SceneCurve {
|
|
4
|
-
/** A curve geometry @vim3d/threejs
|
|
4
|
+
/** A curve geometry @vim3d/threejs draws as a line. */
|
|
5
5
|
readonly geometry: GeometrySnapshot;
|
|
6
6
|
readonly properties: Properties;
|
|
7
7
|
readonly id?: string | number;
|
|
@@ -11,9 +11,51 @@ export interface SceneCurveOptions {
|
|
|
11
11
|
/** Elevation in metres for positions without one; defaults to the origin's. */
|
|
12
12
|
elevation?: number;
|
|
13
13
|
}
|
|
14
|
+
export interface ScenePoint {
|
|
15
|
+
readonly position: Vec3;
|
|
16
|
+
readonly properties: Properties;
|
|
17
|
+
readonly id?: string | number;
|
|
18
|
+
}
|
|
19
|
+
export interface MeshGeometry {
|
|
20
|
+
readonly primitive: 'mesh';
|
|
21
|
+
readonly args: readonly number[];
|
|
22
|
+
/** Flat x, y, z vertices. */
|
|
23
|
+
readonly positions: number[];
|
|
24
|
+
/** Vertex triples, wound counter-clockwise seen from above (+y). */
|
|
25
|
+
readonly indices: number[];
|
|
26
|
+
}
|
|
27
|
+
export interface SceneMesh {
|
|
28
|
+
/** A `mesh` geometry, the same shape the `geo:mesh` node outputs. */
|
|
29
|
+
readonly geometry: MeshGeometry;
|
|
30
|
+
readonly properties: Properties;
|
|
31
|
+
readonly id?: string | number;
|
|
32
|
+
}
|
|
33
|
+
export interface SceneRibbonOptions extends SceneCurveOptions {
|
|
34
|
+
/** Ribbon width in metres; defaults to 1. */
|
|
35
|
+
width?: number;
|
|
36
|
+
/** Feature property holding a width in metres, used when it is a positive number. */
|
|
37
|
+
widthProperty?: string;
|
|
38
|
+
}
|
|
14
39
|
/**
|
|
15
40
|
* Turns every line and polygon ring into a scene-space curve around `origin`,
|
|
16
41
|
* using the same local frame as Vim3D `world` (x east, y up, -z north).
|
|
17
42
|
* Positions without a finite longitude and latitude are skipped.
|
|
18
43
|
*/
|
|
19
44
|
export declare function toSceneCurves(input: GeoJSON, origin: GeoOrigin, options?: SceneCurveOptions): SceneCurve[];
|
|
45
|
+
/**
|
|
46
|
+
* Every `Point` and `MultiPoint` position as a scene-space point with its
|
|
47
|
+
* feature's `properties` and `id`, for labels, trees and other markers.
|
|
48
|
+
*/
|
|
49
|
+
export declare function toScenePoints(input: GeoJSON, origin: GeoOrigin, options?: SceneCurveOptions): ScenePoint[];
|
|
50
|
+
/**
|
|
51
|
+
* Every polygon (each part of a `MultiPolygon` on its own) as a flat,
|
|
52
|
+
* upward-facing triangle mesh with its holes cut out.
|
|
53
|
+
*/
|
|
54
|
+
export declare function toSceneFills(input: GeoJSON, origin: GeoOrigin, options?: SceneCurveOptions): SceneMesh[];
|
|
55
|
+
/**
|
|
56
|
+
* Every line (not polygon rings) as a flat, upward-facing strip of the given
|
|
57
|
+
* width around its centerline, for streets and paths. Corners are mitered;
|
|
58
|
+
* corners sharper than the miter limit, or whose miter would reach past the
|
|
59
|
+
* next vertex, are beveled. A line that ends where it starts joins up.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toSceneRibbons(input: GeoJSON, origin: GeoOrigin, options?: SceneRibbonOptions): SceneMesh[];
|
package/scene.js
CHANGED
|
@@ -1,28 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.toSceneCurves = toSceneCurves;
|
|
7
|
+
exports.toScenePoints = toScenePoints;
|
|
8
|
+
exports.toSceneFills = toSceneFills;
|
|
9
|
+
exports.toSceneRibbons = toSceneRibbons;
|
|
4
10
|
const core_1 = require("@vim3d/core");
|
|
5
|
-
|
|
11
|
+
const earcut_1 = __importDefault(require("earcut"));
|
|
12
|
+
function eachFeature(input, emit) {
|
|
13
|
+
if (input.type === 'FeatureCollection')
|
|
14
|
+
input.features.forEach(feature => emit(feature.geometry, feature.properties, feature.id));
|
|
15
|
+
else if (input.type === 'Feature')
|
|
16
|
+
emit(input.geometry, input.properties, input.id);
|
|
17
|
+
else
|
|
18
|
+
emit(input, null);
|
|
19
|
+
}
|
|
20
|
+
function eachGeometry(geometry, visit) {
|
|
6
21
|
if (!geometry)
|
|
7
22
|
return;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
if (geometry.type === 'GeometryCollection')
|
|
24
|
+
geometry.geometries.forEach(child => eachGeometry(child, visit));
|
|
25
|
+
else
|
|
26
|
+
visit(geometry);
|
|
27
|
+
}
|
|
28
|
+
const finite = ([lon, lat]) => Number.isFinite(lon) && Number.isFinite(lat);
|
|
29
|
+
function projector(origin, options) {
|
|
30
|
+
const elevation = options.elevation ?? origin.elevation;
|
|
31
|
+
return (positions) => positions.filter(finite)
|
|
32
|
+
.map(([lon, lat, height]) => (0, core_1.geodeticToScene)(origin, lat, lon, Number.isFinite(height) ? height : elevation));
|
|
33
|
+
}
|
|
34
|
+
function lines(geometry, emit) {
|
|
35
|
+
eachGeometry(geometry, child => {
|
|
36
|
+
switch (child.type) {
|
|
37
|
+
case 'LineString':
|
|
38
|
+
emit(child.coordinates, false);
|
|
39
|
+
break;
|
|
40
|
+
case 'MultiLineString':
|
|
41
|
+
child.coordinates.forEach(line => emit(line, false));
|
|
42
|
+
break;
|
|
43
|
+
case 'Polygon':
|
|
44
|
+
child.coordinates.forEach(ring => emit(ring, true));
|
|
45
|
+
break;
|
|
46
|
+
case 'MultiPolygon':
|
|
47
|
+
child.coordinates.forEach(polygon => polygon.forEach(ring => emit(ring, true)));
|
|
48
|
+
break;
|
|
49
|
+
default: break;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
26
52
|
}
|
|
27
53
|
/**
|
|
28
54
|
* Turns every line and polygon ring into a scene-space curve around `origin`,
|
|
@@ -31,22 +57,155 @@ function lines(geometry, emit) {
|
|
|
31
57
|
*/
|
|
32
58
|
function toSceneCurves(input, origin, options = {}) {
|
|
33
59
|
const curves = [];
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
const vertices = line
|
|
37
|
-
.map(([lon, lat, height]) => (0, core_1.geodeticToScene)(origin, lat, lon, Number.isFinite(height) ? height : elevation));
|
|
60
|
+
const project = projector(origin, options);
|
|
61
|
+
eachFeature(input, (geometry, properties, id) => lines(geometry, (line, closed) => {
|
|
62
|
+
const vertices = project(line);
|
|
38
63
|
if (vertices.length < 2)
|
|
39
64
|
return;
|
|
40
65
|
const [first, last] = [vertices[0], vertices[vertices.length - 1]];
|
|
41
66
|
if (closed && first.some((v, i) => v !== last[i]))
|
|
42
67
|
vertices.push(first);
|
|
43
68
|
curves.push({ geometry: { primitive: 'curve', args: [], vertices }, properties, id, closed });
|
|
44
|
-
});
|
|
45
|
-
if (input.type === 'FeatureCollection')
|
|
46
|
-
input.features.forEach(feature => add(feature.geometry, feature.properties, feature.id));
|
|
47
|
-
else if (input.type === 'Feature')
|
|
48
|
-
add(input.geometry, input.properties, input.id);
|
|
49
|
-
else
|
|
50
|
-
add(input, null);
|
|
69
|
+
}));
|
|
51
70
|
return curves;
|
|
52
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Every `Point` and `MultiPoint` position as a scene-space point with its
|
|
74
|
+
* feature's `properties` and `id`, for labels, trees and other markers.
|
|
75
|
+
*/
|
|
76
|
+
function toScenePoints(input, origin, options = {}) {
|
|
77
|
+
const points = [];
|
|
78
|
+
const project = projector(origin, options);
|
|
79
|
+
eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
|
|
80
|
+
const positions = child.type === 'Point' ? [child.coordinates] : child.type === 'MultiPoint' ? child.coordinates : [];
|
|
81
|
+
for (const position of project(positions))
|
|
82
|
+
points.push({ position, properties, id });
|
|
83
|
+
}));
|
|
84
|
+
return points;
|
|
85
|
+
}
|
|
86
|
+
function mesh(positions, indices) {
|
|
87
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
88
|
+
const [a, b, c] = [indices[i] * 3, indices[i + 1] * 3, indices[i + 2] * 3];
|
|
89
|
+
const up = (positions[b + 2] - positions[a + 2]) * (positions[c] - positions[a])
|
|
90
|
+
- (positions[b] - positions[a]) * (positions[c + 2] - positions[a + 2]);
|
|
91
|
+
if (up < 0)
|
|
92
|
+
[indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
|
|
93
|
+
}
|
|
94
|
+
return { primitive: 'mesh', args: [], positions, indices };
|
|
95
|
+
}
|
|
96
|
+
function openRing(ring) {
|
|
97
|
+
const [first, last] = [ring[0], ring[ring.length - 1]];
|
|
98
|
+
return ring.length > 1 && first[0] === last[0] && first[2] === last[2] ? ring.slice(0, -1) : ring;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Every polygon (each part of a `MultiPolygon` on its own) as a flat,
|
|
102
|
+
* upward-facing triangle mesh with its holes cut out.
|
|
103
|
+
*/
|
|
104
|
+
function toSceneFills(input, origin, options = {}) {
|
|
105
|
+
const meshes = [];
|
|
106
|
+
const project = projector(origin, options);
|
|
107
|
+
const fill = (rings, properties, id) => {
|
|
108
|
+
const [outer, ...holes] = rings.map(ring => openRing(project(ring)));
|
|
109
|
+
if (!outer || outer.length < 3)
|
|
110
|
+
return;
|
|
111
|
+
const kept = [outer, ...holes.filter(hole => hole.length >= 3)];
|
|
112
|
+
const positions = [];
|
|
113
|
+
const flat = [];
|
|
114
|
+
const starts = [];
|
|
115
|
+
for (const ring of kept) {
|
|
116
|
+
if (ring !== outer)
|
|
117
|
+
starts.push(positions.length / 3);
|
|
118
|
+
for (const [x, y, z] of ring) {
|
|
119
|
+
positions.push(x, y, z);
|
|
120
|
+
flat.push(x, z);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const indices = (0, earcut_1.default)(flat, starts.length ? starts : undefined, 2);
|
|
124
|
+
if (indices.length)
|
|
125
|
+
meshes.push({ geometry: mesh(positions, indices), properties, id });
|
|
126
|
+
};
|
|
127
|
+
eachFeature(input, (geometry, properties, id) => eachGeometry(geometry, child => {
|
|
128
|
+
if (child.type === 'Polygon')
|
|
129
|
+
fill(child.coordinates, properties, id);
|
|
130
|
+
else if (child.type === 'MultiPolygon')
|
|
131
|
+
child.coordinates.forEach(polygon => fill(polygon, properties, id));
|
|
132
|
+
}));
|
|
133
|
+
return meshes;
|
|
134
|
+
}
|
|
135
|
+
const MITER_LIMIT = 2;
|
|
136
|
+
function ribbon(line, width) {
|
|
137
|
+
let points = line.filter((p, i) => i === 0 || p[0] !== line[i - 1][0] || p[2] !== line[i - 1][2]);
|
|
138
|
+
const [first, last] = [points[0], points[points.length - 1]];
|
|
139
|
+
const closed = points.length > 3 && first[0] === last[0] && first[2] === last[2];
|
|
140
|
+
if (closed)
|
|
141
|
+
points = points.slice(0, -1);
|
|
142
|
+
if (points.length < 2)
|
|
143
|
+
return null;
|
|
144
|
+
const count = points.length;
|
|
145
|
+
const half = width / 2;
|
|
146
|
+
const segments = closed ? count : count - 1;
|
|
147
|
+
const normals = [];
|
|
148
|
+
const lengths = [];
|
|
149
|
+
for (let i = 0; i < segments; i++) {
|
|
150
|
+
const [p, q] = [points[i], points[(i + 1) % count]];
|
|
151
|
+
const [dx, dz] = [q[0] - p[0], q[2] - p[2]];
|
|
152
|
+
const length = Math.hypot(dx, dz);
|
|
153
|
+
normals.push([-dz / length, dx / length]);
|
|
154
|
+
lengths.push(length);
|
|
155
|
+
}
|
|
156
|
+
const sections = [];
|
|
157
|
+
const section = ([x, y, z], [nx, nz], scale) => sections.push([x, y, z, nx * scale, nz * scale]);
|
|
158
|
+
points.forEach((point, i) => {
|
|
159
|
+
const before = closed ? (i + count - 1) % count : i - 1;
|
|
160
|
+
const after = i < segments ? i : -1;
|
|
161
|
+
if (before < 0 || after < 0)
|
|
162
|
+
return section(point, normals[Math.max(before, after)], half);
|
|
163
|
+
const [a, b] = [normals[before], normals[after]];
|
|
164
|
+
const [mx, mz] = [a[0] + b[0], a[1] + b[1]];
|
|
165
|
+
const length = Math.hypot(mx, mz);
|
|
166
|
+
const cos = length / 2;
|
|
167
|
+
const reach = cos > 0 ? half * Math.sqrt(Math.max(0, 1 - cos * cos)) / cos : Infinity;
|
|
168
|
+
if (cos >= 1 / MITER_LIMIT && reach <= Math.min(lengths[before], lengths[after])) {
|
|
169
|
+
section(point, [mx / length, mz / length], half / cos);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
section(point, a, half);
|
|
173
|
+
section(point, b, half);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
if (closed)
|
|
177
|
+
sections.push(sections[0]);
|
|
178
|
+
const positions = [];
|
|
179
|
+
const indices = [];
|
|
180
|
+
sections.forEach(([x, y, z, ox, oz], i) => {
|
|
181
|
+
positions.push(x + ox, y, z + oz, x - ox, y, z - oz);
|
|
182
|
+
if (i > 0) {
|
|
183
|
+
const k = 2 * (i - 1);
|
|
184
|
+
indices.push(k, k + 1, k + 2, k + 1, k + 3, k + 2);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
return mesh(positions, indices);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Every line (not polygon rings) as a flat, upward-facing strip of the given
|
|
191
|
+
* width around its centerline, for streets and paths. Corners are mitered;
|
|
192
|
+
* corners sharper than the miter limit, or whose miter would reach past the
|
|
193
|
+
* next vertex, are beveled. A line that ends where it starts joins up.
|
|
194
|
+
*/
|
|
195
|
+
function toSceneRibbons(input, origin, options = {}) {
|
|
196
|
+
const meshes = [];
|
|
197
|
+
const project = projector(origin, options);
|
|
198
|
+
eachFeature(input, (geometry, properties, id) => {
|
|
199
|
+
const value = options.widthProperty ? properties?.[options.widthProperty] : undefined;
|
|
200
|
+
const width = typeof value === 'number' && value > 0 && Number.isFinite(value) ? value : options.width ?? 1;
|
|
201
|
+
eachGeometry(geometry, child => {
|
|
202
|
+
const parts = child.type === 'LineString' ? [child.coordinates] : child.type === 'MultiLineString' ? child.coordinates : [];
|
|
203
|
+
for (const part of parts) {
|
|
204
|
+
const strip = ribbon(project(part), width);
|
|
205
|
+
if (strip)
|
|
206
|
+
meshes.push({ geometry: strip, properties, id });
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
return meshes;
|
|
211
|
+
}
|