@protomaze/geometry 0.0.0-stage → 0.1.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.
Files changed (118) hide show
  1. package/GeometryDemo.d.ts +1 -0
  2. package/GeometryDemo.js +1 -0
  3. package/README.md +301 -2
  4. package/common/Colors.d.ts +47 -0
  5. package/common/Colors.js +1 -0
  6. package/common/Disposables.d.ts +7 -0
  7. package/common/Disposables.js +1 -0
  8. package/data/Geometry.d.ts +62 -0
  9. package/data/Geometry.js +1 -0
  10. package/demo/DemoControls.d.ts +68 -0
  11. package/demo/DemoControls.js +1 -0
  12. package/demo/Town.d.ts +115 -0
  13. package/demo/Town.js +1 -0
  14. package/edit/CreateController.d.ts +101 -0
  15. package/edit/CreateController.js +1 -0
  16. package/edit/EditController.d.ts +121 -0
  17. package/edit/EditController.js +1 -0
  18. package/edit/EditHistory.d.ts +46 -0
  19. package/edit/EditHistory.js +1 -0
  20. package/edit/EditShape.d.ts +45 -0
  21. package/edit/EditShape.js +1 -0
  22. package/edit/GeometryController.d.ts +83 -0
  23. package/edit/GeometryController.js +1 -0
  24. package/edit/MeasureController.d.ts +102 -0
  25. package/edit/MeasureController.js +1 -0
  26. package/edit/Sketch.d.ts +73 -0
  27. package/edit/Sketch.js +1 -0
  28. package/edit/Snapper.d.ts +67 -0
  29. package/edit/Snapper.js +1 -0
  30. package/engine/GeometryEngine.d.ts +267 -0
  31. package/engine/GeometryEngine.js +1 -0
  32. package/engine/GeometryFrame.d.ts +91 -0
  33. package/engine/GeometryFrame.js +1 -0
  34. package/engine/GeometryOptions.d.ts +79 -0
  35. package/engine/GeometryOptions.js +1 -0
  36. package/engine/GeometryView.d.ts +192 -0
  37. package/engine/GeometryView.js +1 -0
  38. package/engine/Labels.d.ts +42 -0
  39. package/engine/Labels.js +1 -0
  40. package/engine/MapCamera.d.ts +78 -0
  41. package/engine/MapCamera.js +1 -0
  42. package/event/TypedEventEmitter.d.ts +13 -0
  43. package/event/TypedEventEmitter.js +1 -0
  44. package/index.d.ts +53 -0
  45. package/index.js +1 -0
  46. package/layers/AreaLayer.d.ts +36 -0
  47. package/layers/AreaLayer.js +1 -0
  48. package/layers/GeometryLayer.d.ts +312 -0
  49. package/layers/GeometryLayer.js +1 -0
  50. package/layers/LineLayer.d.ts +48 -0
  51. package/layers/LineLayer.js +1 -0
  52. package/layers/PointLayer.d.ts +42 -0
  53. package/layers/PointLayer.js +1 -0
  54. package/layers/SphereLayer.d.ts +25 -0
  55. package/layers/SphereLayer.js +1 -0
  56. package/layers/TubeLayer.d.ts +52 -0
  57. package/layers/TubeLayer.js +1 -0
  58. package/math/Boolean.d.ts +21 -0
  59. package/math/Boolean.js +1 -0
  60. package/math/FeatureIndex.d.ts +70 -0
  61. package/math/FeatureIndex.js +1 -0
  62. package/math/Geodesy.d.ts +29 -0
  63. package/math/Geodesy.js +1 -0
  64. package/math/Lines.d.ts +115 -0
  65. package/math/Lines.js +1 -0
  66. package/math/Measure.d.ts +71 -0
  67. package/math/Measure.js +1 -0
  68. package/math/Polygons.d.ts +59 -0
  69. package/math/Polygons.js +1 -0
  70. package/math/Projection.d.ts +25 -0
  71. package/math/Projection.js +1 -0
  72. package/math/Relations.d.ts +27 -0
  73. package/math/Relations.js +1 -0
  74. package/math/SpatialIndex.d.ts +36 -0
  75. package/math/SpatialIndex.js +1 -0
  76. package/math/Transform.d.ts +36 -0
  77. package/math/Transform.js +1 -0
  78. package/math/Triangulate.d.ts +19 -0
  79. package/math/Triangulate.js +1 -0
  80. package/math/Vector.d.ts +24 -0
  81. package/math/Vector.js +1 -0
  82. package/package.json +76 -4
  83. package/react/ReactGeometry.d.ts +35 -0
  84. package/react/ReactGeometry.js +1 -0
  85. package/three/CanvasGeometryEngine.d.ts +79 -0
  86. package/three/CanvasGeometryEngine.js +1 -0
  87. package/three/DensityMap.d.ts +31 -0
  88. package/three/DensityMap.js +1 -0
  89. package/three/FeatureLook.d.ts +191 -0
  90. package/three/FeatureLook.js +1 -0
  91. package/three/Ground.d.ts +54 -0
  92. package/three/Ground.js +1 -0
  93. package/three/Halo.d.ts +26 -0
  94. package/three/Halo.js +1 -0
  95. package/three/HostedGeometryEngine.d.ts +57 -0
  96. package/three/HostedGeometryEngine.js +1 -0
  97. package/three/RiaGeometryEngine.d.ts +48 -0
  98. package/three/RiaGeometryEngine.js +1 -0
  99. package/three/Shading.d.ts +42 -0
  100. package/three/Shading.js +1 -0
  101. package/three/Sky.d.ts +27 -0
  102. package/three/Sky.js +1 -0
  103. package/three/Tsl.d.ts +47 -0
  104. package/three/Tsl.js +1 -0
  105. package/three/primitives/Body.d.ts +82 -0
  106. package/three/primitives/Body.js +1 -0
  107. package/three/primitives/GpuAreas.d.ts +72 -0
  108. package/three/primitives/GpuAreas.js +1 -0
  109. package/three/primitives/GpuIcons.d.ts +112 -0
  110. package/three/primitives/GpuIcons.js +1 -0
  111. package/three/primitives/GpuLines.d.ts +198 -0
  112. package/three/primitives/GpuLines.js +1 -0
  113. package/three/primitives/GpuSpheres.d.ts +55 -0
  114. package/three/primitives/GpuSpheres.js +1 -0
  115. package/three/primitives/GpuTubes.d.ts +125 -0
  116. package/three/primitives/GpuTubes.js +1 -0
  117. package/three/primitives/IconAtlas.d.ts +12 -0
  118. package/three/primitives/IconAtlas.js +1 -0
@@ -0,0 +1,70 @@
1
+ import { GeometryFeature, Position } from '../data/Geometry';
2
+ import { Vec3 } from './Vector';
3
+ /** What part of a feature: a point, a segment of a line, or a segment of a polygon's ring. */
4
+ export type FeaturePartKind = 'point' | 'line' | 'ring';
5
+ /** A place on a feature, found by the index: the feature, the point, and where on it. */
6
+ export interface FeaturePlace<F> {
7
+ feature: F;
8
+ /** The feature's index in the features indexed. */
9
+ featureIndex: number;
10
+ /** The point found, its height interpolated along a segment. */
11
+ point: Vec3;
12
+ /** How far from the query point, across the map. */
13
+ distance: number;
14
+ kind: FeaturePartKind;
15
+ /**
16
+ * Which line or ring: lines numbered first in the feature's own order (a multi-line's parts),
17
+ * then its polygons' rings in order (outer ring, holes, next polygon); for points, which point.
18
+ */
19
+ part: number;
20
+ /** The vertex: the segment's first point's index in its line or ring; for points, 0. */
21
+ vertex: number;
22
+ /** On a segment, how far along it, 0 at `vertex` to 1 at the next. */
23
+ t: number;
24
+ }
25
+ /**
26
+ * Features indexed for finding by place, across the map: the nearest vertex or edge to a point -
27
+ * snapping, the pipe nearest a house - what lies in a box, what is at a point. Every point and
28
+ * segment goes into one `SpatialIndex`; build it again when the features change.
29
+ */
30
+ export declare class FeatureIndex<F extends GeometryFeature<any> = GeometryFeature> {
31
+ readonly features: readonly F[];
32
+ private readonly index;
33
+ private readonly boxes;
34
+ /** Per item: [ax, ay, az, bx, by, bz]. */
35
+ private readonly segments;
36
+ private readonly featureOf;
37
+ private readonly partOf;
38
+ private readonly vertexOf;
39
+ private readonly kindOf;
40
+ constructor(features: readonly F[]);
41
+ /**
42
+ * The vertex nearest `p` across the map - a point, a line's or a ring's corner - within
43
+ * `maxDistance`. `filter` may pass over features.
44
+ */
45
+ nearestVertex(p: Position, maxDistance?: number, filter?: (feature: F, index: number) => boolean): FeaturePlace<F> | null;
46
+ /** The nearest point on any feature's edge, line or point to `p` across the map, within `maxDistance`. */
47
+ nearestEdge(p: Position, maxDistance?: number, filter?: (feature: F, index: number) => boolean): FeaturePlace<F> | null;
48
+ /**
49
+ * The feature at `p`: a polygon it is inside (distance 0), or the nearest line or point within
50
+ * `tolerance`. Polygons win over what is merely near.
51
+ */
52
+ featureAt(p: Position, tolerance?: number, filter?: (feature: F, index: number) => boolean): FeaturePlace<F> | null;
53
+ /** Every feature with a part in the box, each once, in index order. */
54
+ inBox(minX: number, minY: number, maxX: number, maxY: number): F[];
55
+ /** The features nearest `p`, each once, nearest first: up to `k`, no further than `maxDistance`. */
56
+ nearestFeatures(p: Position, k?: number, maxDistance?: number): FeaturePlace<F>[];
57
+ /** The items - points and segments - whose boxes touch a box across the map, by index: for `item`. */
58
+ itemsIn(minX: number, minY: number, maxX: number, maxY: number): number[];
59
+ /** An item's two ends (the same for a point), its feature and where on it. */
60
+ item(i: number): {
61
+ a: Vec3;
62
+ b: Vec3;
63
+ feature: F;
64
+ featureIndex: number;
65
+ kind: FeaturePartKind;
66
+ part: number;
67
+ vertex: number;
68
+ };
69
+ private place;
70
+ }
@@ -0,0 +1 @@
1
+ import{linesOf as t,pointsOf as e,polygonsOf as s}from"../data/Geometry.js";import{closestOnSegment as r}from"./Lines.js";import{pointInPolygon as n}from"./Polygons.js";import{SpatialIndex as i}from"./SpatialIndex.js";import{zOf as a}from"./Vector.js";export class FeatureIndex{constructor(r){this.features=r,this.index=new i,this.boxes=new i;const n=[],h=[];r.forEach((r,i)=>{const o=r.geometry;let f=1/0,c=1/0,u=-1/0,d=-1/0;const m=(t,e,s,r,o)=>{n.push(t[0],t[1],a(t),e[0],e[1],a(e)),h.push(i,r,o,s),f=Math.min(f,t[0],e[0]),c=Math.min(c,t[1],e[1]),u=Math.max(u,t[0],e[0]),d=Math.max(d,t[1],e[1])};e(o).forEach((t,e)=>m(t,t,0,e,0));let x=0;for(const e of t(o)){1===e.length&&m(e[0],e[0],1,x,0);for(let t=1;t<e.length;t++)m(e[t-1],e[t],1,x,t-1);x++}for(const t of s(o))for(const e of t){for(let t=1;t<e.length;t++)m(e[t-1],e[t],2,x,t-1);x++}f<=u?this.boxes.add(f,c,u,d):this.boxes.add(1/0,1/0,-1/0,-1/0)}),this.segments=Float64Array.from(n);const o=h.length/4;this.featureOf=new Int32Array(o),this.partOf=new Int32Array(o),this.vertexOf=new Int32Array(o),this.kindOf=new Uint8Array(o);for(let t=0;t<o;t++){this.featureOf[t]=h[4*t],this.partOf[t]=h[4*t+1],this.vertexOf[t]=h[4*t+2],this.kindOf[t]=h[4*t+3];const e=6*t,s=this.segments;this.index.add(Math.min(s[e],s[e+3]),Math.min(s[e+1],s[e+4]),Math.max(s[e],s[e+3]),Math.max(s[e+1],s[e+4]))}this.index.finish(),this.boxes.finish()}nearestVertex(t,e=1/0,s){let r=null;return this.index.neighbors(t[0],t[1],(n,i)=>{if(i>e||r&&i>r.distance)return!1;const a=this.featureOf[n];if(s&&!s(this.features[a],a))return!0;const h=this.segments,o=6*n;for(const s of[0,1]){const i=h[o+3*s],a=h[o+3*s+1],f=Math.hypot(i-t[0],a-t[1]);f<=e&&(!r||f<r.distance)&&(r=this.place(n,[i,a,h[o+3*s+2]],f,s),r.vertex+=s,r.t=0)}return!0}),r}nearestEdge(t,e=1/0,s){let n=null;return this.index.neighbors(t[0],t[1],(i,a)=>{if(a>e||n&&a>n.distance)return!1;const h=this.featureOf[i];if(s&&!s(this.features[h],h))return!0;const o=this.segments,f=6*i,c=r(t,[o[f],o[f+1],o[f+2]],[o[f+3],o[f+4],o[f+5]],2);return c.distance<=e&&(!n||c.distance<n.distance)&&(n=this.place(i,c.point,c.distance,c.t)),!0}),n}featureAt(t,e=0,r){const i=this.boxes.search(t[0],t[1],t[0],t[1],e=>(!r||r(this.features[e],e))&&s(this.features[e].geometry).some(e=>n(t,e)));if(i.length>0){const e=i[i.length-1];return{feature:this.features[e],featureIndex:e,point:[t[0],t[1],a(t)],distance:0,kind:"ring",part:0,vertex:-1,t:0}}return this.nearestEdge(t,e,r)}inBox(t,e,r,i){const a=new Set;return this.index.search(t,e,r,i).forEach(t=>a.add(this.featureOf[t])),this.boxes.search(t,e,r,i).forEach(r=>{!a.has(r)&&s(this.features[r].geometry).some(s=>n([t,e],s))&&a.add(r)}),[...a].sort((t,e)=>t-e).map(t=>this.features[t])}nearestFeatures(t,e=1,s=1/0){const n=new Map;return this.index.neighbors(t[0],t[1],(i,a)=>{if(a>s)return!1;if(n.size>=e){if(a>Math.max(...[...n.values()].map(t=>t.distance)))return!1}const h=this.segments,o=6*i,f=r(t,[h[o],h[o+1],h[o+2]],[h[o+3],h[o+4],h[o+5]],2);if(f.distance>s)return!0;const c=this.featureOf[i],u=n.get(c);if((!u||f.distance<u.distance)&&(n.set(c,this.place(i,f.point,f.distance,f.t)),n.size>e)){const[t]=[...n.entries()].sort((t,e)=>e[1].distance-t[1].distance)[0];n.delete(t)}return!0}),[...n.values()].sort((t,e)=>t.distance-e.distance)}itemsIn(t,e,s,r){return this.index.search(t,e,s,r)}item(t){const e=this.segments,s=6*t,r=this.kindOf[t],n=this.featureOf[t];return{a:[e[s],e[s+1],e[s+2]],b:[e[s+3],e[s+4],e[s+5]],feature:this.features[n],featureIndex:n,kind:0===r?"point":1===r?"line":"ring",part:this.partOf[t],vertex:this.vertexOf[t]}}place(t,e,s,r){const n=this.featureOf[t],i=this.kindOf[t];return{feature:this.features[n],featureIndex:n,point:e,distance:s,kind:0===i?"point":1===i?"line":"ring",part:this.partOf[t],vertex:this.vertexOf[t],t:r}}}
@@ -0,0 +1,29 @@
1
+ import type { Geometry, Position } from '../data/Geometry';
2
+ /** The earth's mean radius (IUGG), for the spherical formulas. */
3
+ export declare const MEAN_EARTH_RADIUS = 6371008.8;
4
+ /** The great-circle distance on a sphere of the mean radius: within half a percent of the ellipsoid's. */
5
+ export declare function haversineDistance(a: Position, b: Position, radius?: number): number;
6
+ /**
7
+ * The distance on the WGS84 ellipsoid (Vincenty's inverse formula, to a millimetre), with the
8
+ * bearings at either end. Nearly antipodal points, where it does not settle, fall back on the
9
+ * great circle.
10
+ */
11
+ export declare function geodesicInverse(a: Position, b: Position): {
12
+ distance: number;
13
+ initialBearing: number;
14
+ finalBearing: number;
15
+ };
16
+ /** The distance on the WGS84 ellipsoid, in metres. */
17
+ export declare function geodesicDistance(a: Position, b: Position): number;
18
+ /** The bearing to set out on from `a` to reach `b` along the great circle, degrees clockwise from north. */
19
+ export declare function initialBearing(a: Position, b: Position): number;
20
+ /** Where `distance` metres on a bearing from `origin` leads, along the great circle. */
21
+ export declare function destination(origin: Position, bearingDegrees: number, distance: number, radius?: number): [number, number];
22
+ /** The point a fraction of the way from `a` to `b` along the great circle. */
23
+ export declare function interpolateGreatCircle(a: Position, b: Position, fraction: number): [number, number];
24
+ /** A line's length on the ellipsoid, every part of a multi-line and every ring of a polygon added up. */
25
+ export declare function geodesicLength(geometry: Geometry | null | undefined): number;
26
+ /** A ring's area on the sphere (Chamberlain and Duquette), in square metres. */
27
+ export declare function geodesicRingArea(ring: readonly Position[], radius?: number): number;
28
+ /** A polygon's area on the earth, holes taken off, in square metres. Multi-polygons add up. */
29
+ export declare function geodesicArea(geometry: Geometry | null | undefined): number;
@@ -0,0 +1 @@
1
+ import{linesOf as t,polygonsOf as a}from"../data/Geometry.js";import{openLength as n}from"./Triangulate.js";const i=1/298.257223563,s=6356752.314245179;export const MEAN_EARTH_RADIUS=6371008.8;const e=t=>t*Math.PI/180,o=t=>180*t/Math.PI,h=t=>(t%360+360)%360;export function haversineDistance(t,a,n=6371008.8){const i=e(t[1]),s=e(a[1]),o=s-i,h=e(a[0]-t[0]),r=Math.sin(o/2)**2+Math.cos(i)*Math.cos(s)*Math.sin(h/2)**2;return 2*n*Math.asin(Math.min(1,Math.sqrt(r)))}export function geodesicInverse(t,a){const n=e(a[0]-t[0]),r=Math.atan((1-i)*Math.tan(e(t[1]))),c=Math.atan((1-i)*Math.tan(e(a[1]))),M=Math.sin(r),f=Math.cos(r),u=Math.sin(c),g=Math.cos(c);let l=n,p=0,d=0,x=0,B=0,m=0,A=0,D=0,v=!1;for(let t=0;t<200;t++){if(p=Math.sin(l),d=Math.cos(l),x=Math.hypot(g*p,f*u-M*g*d),0===x)return{distance:0,initialBearing:0,finalBearing:0};B=M*u+f*g*d,m=Math.atan2(x,B);const t=f*g*p/x;A=1-t*t,D=0!==A?B-2*M*u/A:0;const a=i/16*A*(4+i*(4-3*A)),s=l;if(l=n+(1-a)*i*t*(m+a*x*(D+a*B*(2*D*D-1))),Math.abs(l-s)<1e-12){v=!0;break}}if(!v)return{distance:haversineDistance(t,a),initialBearing:initialBearing(t,a),finalBearing:h(initialBearing(a,t)+180)};const I=272331606107.5547*A/(s*s),R=I/1024*(256+I*(I*(74-47*I)-128)),b=s*(1+I/16384*(4096+I*(I*(320-175*I)-768)))*(m-R*x*(D+R/4*(B*(2*D*D-1)-R/6*D*(4*x*x-3)*(4*D*D-3)))),y=Math.atan2(g*p,f*u-M*g*d),E=Math.atan2(f*p,-M*g+f*u*d);return{distance:b,initialBearing:h(o(y)),finalBearing:h(o(E))}}export function geodesicDistance(t,a){return geodesicInverse(t,a).distance}export function initialBearing(t,a){const n=e(t[1]),i=e(a[1]),s=e(a[0]-t[0]),r=Math.sin(s)*Math.cos(i),c=Math.cos(n)*Math.sin(i)-Math.sin(n)*Math.cos(i)*Math.cos(s);return h(o(Math.atan2(r,c)))}export function destination(t,a,n,i=6371008.8){const s=n/i,h=e(a),r=e(t[1]),c=e(t[0]),M=Math.asin(Math.sin(r)*Math.cos(s)+Math.cos(r)*Math.sin(s)*Math.cos(h)),f=c+Math.atan2(Math.sin(h)*Math.sin(s)*Math.cos(r),Math.cos(s)-Math.sin(r)*Math.sin(M));return[(o(f)+540)%360-180,o(M)]}export function interpolateGreatCircle(t,a,n){const i=e(t[1]),s=e(t[0]),h=e(a[1]),r=e(a[0]),c=haversineDistance(t,a,1);if(0===c)return[t[0],t[1]];const M=Math.sin((1-n)*c)/Math.sin(c),f=Math.sin(n*c)/Math.sin(c),u=M*Math.cos(i)*Math.cos(s)+f*Math.cos(h)*Math.cos(r),g=M*Math.cos(i)*Math.sin(s)+f*Math.cos(h)*Math.sin(r),l=M*Math.sin(i)+f*Math.sin(h);return[o(Math.atan2(g,u)),o(Math.atan2(l,Math.hypot(u,g)))]}export function geodesicLength(n){let i=0;const s=[...t(n)];for(const t of a(n))for(const a of t)s.push([...a,a[0]]);for(const t of s)for(let a=1;a<t.length;a++)i+=geodesicDistance(t[a-1],t[a]);return i}export function geodesicRingArea(t,a=6378137){const i=n(t);if(i<3)return 0;let s=0;for(let a=0;a<i;a++){const n=t[(a+i-1)%i],o=t[a],h=t[(a+1)%i];s+=(e(h[0])-e(n[0]))*Math.sin(e(o[1]))}return Math.abs(s*a*a/2)}export function geodesicArea(t){let n=0;for(const i of a(t))i.forEach((t,a)=>{const i=geodesicRingArea(t);n+=0===a?i:-i});return Math.max(0,n)}
@@ -0,0 +1,115 @@
1
+ import type { Position } from '../data/Geometry';
2
+ import { Dims, Vec3 } from './Vector';
3
+ /**
4
+ * Working along lines - pipes, routes, trajectories, a ring's edges: the nearest point on one,
5
+ * the point some distance along, cutting, splitting, thinning, smoothing and offsetting them,
6
+ * where two cross and how close two come. Distances along count heights (`dims` 3) unless asked
7
+ * not to; the cuts and offsets across the map keep each point's height.
8
+ */
9
+ /** The nearest point of segment `a`-`b` to `p`: the point, how far along (0 at `a`, 1 at `b`), how far off. */
10
+ export interface SegmentPoint {
11
+ point: Vec3;
12
+ t: number;
13
+ distance: number;
14
+ }
15
+ /** The nearest point of a line to a position: and on which segment, and how far along the line. */
16
+ export interface LinePoint extends SegmentPoint {
17
+ /** The segment's first point's index. */
18
+ index: number;
19
+ /** How far along the whole line, in the same `dims` it was measured in. */
20
+ along: number;
21
+ }
22
+ /** Where two segments cross: the point, how far along each (0 to 1), and the second's height there. */
23
+ export interface Crossing {
24
+ point: Vec3;
25
+ /** The segment of the first line, and of the second: their first points' indices. */
26
+ indexA: number;
27
+ indexB: number;
28
+ t: number;
29
+ u: number;
30
+ /** The second segment's height where they cross: the first's is `point[2]`. Their difference is the clearance. */
31
+ zB: number;
32
+ }
33
+ /** The nearest points of two segments or lines, and how far apart they are. */
34
+ export interface Approach {
35
+ distance: number;
36
+ pointA: Vec3;
37
+ pointB: Vec3;
38
+ /** How far along each segment, 0 to 1; on lines, the segments' first points' indices too. */
39
+ s: number;
40
+ t: number;
41
+ indexA: number;
42
+ indexB: number;
43
+ }
44
+ /** The nearest point of the segment `a`-`b` to `p`, in 3D or across the map (`dims` 2; its height then interpolated). */
45
+ export declare function closestOnSegment(p: Position, a: Position, b: Position, dims?: Dims): SegmentPoint;
46
+ /** The nearest point of a line to `p`. Null for a line of no points. */
47
+ export declare function nearestPointOnLine(line: readonly Position[], p: Position, dims?: Dims): LinePoint | null;
48
+ /** The point `distance` along a line from its start, clamped to its ends. */
49
+ export declare function pointAlong(line: readonly Position[], distance: number, dims?: Dims): Vec3 | null;
50
+ /** The way a line runs at `distance` along it: a unit vector, the segment's there. */
51
+ export declare function directionAlong(line: readonly Position[], distance: number, dims?: Dims): Vec3 | null;
52
+ /**
53
+ * The stretch of a line from `from` to `to` along it, both clamped to its length, the cut ends
54
+ * interpolated. Reversed where `from` is past `to`.
55
+ */
56
+ export declare function lineSlice(line: readonly Position[], from: number, to: number, dims?: Dims): Position[];
57
+ /** A line cut at distances along it: one part more than cuts that fall within it. */
58
+ export declare function splitLine(line: readonly Position[], at: readonly number[], dims?: Dims): Position[][];
59
+ /** A line cut in two at the point of it nearest `p`, which both halves share. */
60
+ export declare function splitLineAt(line: readonly Position[], p: Position, dims?: Dims): [Position[], Position[]];
61
+ /** A line with points added so that no segment is longer than `maxLength`. */
62
+ export declare function densify(line: readonly Position[], maxLength: number, dims?: Dims): Position[];
63
+ /** `count` points evenly spaced along a line, its two ends among them. */
64
+ export declare function resample(line: readonly Position[], count: number, dims?: Dims): Position[];
65
+ /**
66
+ * A line thinned (Douglas-Peucker): the fewest of its points that keep it within `tolerance` of
67
+ * where it was. Its ends always stay.
68
+ */
69
+ export declare function simplify(line: readonly Position[], tolerance: number, dims?: Dims): Position[];
70
+ /** A ring thinned as `simplify` thins a line, kept closed and at least a triangle. */
71
+ export declare function simplifyRing(ring: readonly Position[], tolerance: number, dims?: Dims): Position[];
72
+ /**
73
+ * A line rounded off (Chaikin): every corner cut, `iterations` times. An open line keeps its
74
+ * ends; a `closed` one - a ring - is rounded all round and stays closed.
75
+ */
76
+ export declare function smooth(line: readonly Position[], iterations?: number, closed?: boolean): Position[];
77
+ /**
78
+ * A line moved sideways by `distance` across the map: to its left where positive, its right
79
+ * where negative - a kerb beside a road's axis, a duct beside a main. Joints are mitred, cut
80
+ * flat where the mitre would reach further than `miterLimit` times the distance. Each point
81
+ * keeps its height.
82
+ */
83
+ export declare function offsetLine(line: readonly Position[], distance: number, miterLimit?: number): Position[];
84
+ /** Where segments `a`-`b` and `c`-`d` cross across the map, ends included; null if they do not, or run parallel. */
85
+ export declare function segmentIntersection(a: Position, b: Position, c: Position, d: Position): {
86
+ point: Vec3;
87
+ t: number;
88
+ u: number;
89
+ zB: number;
90
+ } | null;
91
+ /**
92
+ * Every point where two lines cross across the map, in order along the first. Where a pipe
93
+ * crosses another, `point[2] - zB` is how far above it it runs there.
94
+ */
95
+ export declare function lineIntersections(lineA: readonly Position[], lineB: readonly Position[]): Crossing[];
96
+ /**
97
+ * Where a line crosses itself across the map, its neighbouring segments - which meet at their
98
+ * shared point - left out. A `closed` line's last and first segment are neighbours too.
99
+ */
100
+ export declare function selfIntersections(line: readonly Position[], closed?: boolean): Crossing[];
101
+ /** The nearest points of segments `a`-`b` and `c`-`d`, in 3D or across the map (`dims` 2). */
102
+ export declare function segmentApproach(a: Position, b: Position, c: Position, d: Position, dims?: Dims): Approach;
103
+ /**
104
+ * How close two lines come, and where: in 3D, the clearance between two pipes; across the map
105
+ * (`dims` 2), 0 where they cross. Null if either has no segment.
106
+ */
107
+ export declare function lineApproach(lineA: readonly Position[], lineB: readonly Position[], dims?: Dims): Approach | null;
108
+ /** Whether a line ends where it starts. */
109
+ export declare function isClosedLine(line: readonly Position[]): boolean;
110
+ /** A line with its first point repeated at its end, if it is not already. */
111
+ export declare function closeLine(line: readonly Position[]): Position[];
112
+ /** The same points, the other way. */
113
+ export declare function reverseLine(line: readonly Position[]): Position[];
114
+ /** Whether `c` is left of the line through `a` and `b` (positive), right (negative), or on it (0). */
115
+ export declare function sideOf(a: Position, b: Position, c: Position): number;
package/math/Lines.js ADDED
@@ -0,0 +1 @@
1
+ import{distanceOf as t,lerp3 as n,normalize3 as e,orient2D as o,sub3 as i,zOf as r}from"./Vector.js";const s=t=>t<0?0:t>1?1:t;export function closestOnSegment(e,o,i,l=3){const c=i[0]-o[0],h=i[1]-o[1],u=3===l?r(i)-r(o):0,f=e[0]-o[0],p=e[1]-o[1],a=3===l?r(e)-r(o):0,g=c*c+h*h+u*u,x=g>0?s((f*c+p*h+a*u)/g):0,m=n(o,i,x);return{point:m,t:x,distance:t(e,m,l)}}export function nearestPointOnLine(n,e,o=3){if(0===n.length)return null;if(1===n.length){const i=[n[0][0],n[0][1],r(n[0])];return{point:i,t:0,distance:t(e,i,o),index:0,along:0}}let i=null,s=0;for(let r=1;r<n.length;r++){const l=n[r-1],c=n[r],h=closestOnSegment(e,l,c,o),u=t(l,c,o);(!i||h.distance<i.distance)&&(i={...h,index:r-1,along:s+h.t*u}),s+=u}return i}export function pointAlong(e,o,i=3){if(0===e.length)return null;if(o<=0||1===e.length)return[e[0][0],e[0][1],r(e[0])];let s=0;for(let r=1;r<e.length;r++){const l=t(e[r-1],e[r],i);if(s+l>=o&&l>0)return n(e[r-1],e[r],(o-s)/l);s+=l}const l=e[e.length-1];return[l[0],l[1],r(l)]}export function directionAlong(n,o,r=3){if(n.length<2)return null;let s=0;for(let l=1;l<n.length;l++){const c=t(n[l-1],n[l],r);if((s+c>=o||l===n.length-1)&&c>0)return e(i(n[l],n[l-1]));s+=c}return null}export function lineSlice(n,e,o,i=3){if(e>o)return lineSlice(n,o,e,i).reverse();if(n.length<2)return n.map(t=>[...t]);const r=[];let s=0;const l=pointAlong(n,e,i);r.push(h(l,n[0]));for(let l=1;l<n.length&&(s+=t(n[l-1],n[l],i),s>e&&s<o&&r.push([...n[l]]),!(s>=o));l++);return r.push(h(pointAlong(n,o,i),n[0])),r}export function splitLine(t,n,e=3){const o=l(t,e),i=[...n].filter(t=>t>0&&t<o).sort((t,n)=>t-n),r=[];let s=0;for(const n of[...i,o])(n-s>0||0===r.length)&&r.push(lineSlice(t,s,n,e)),s=n;return r}export function splitLineAt(t,n,e=3){const o=nearestPointOnLine(t,n,e);if(!o)return[[],[]];const i=h(o.point,t[0]),r=t.slice(0,o.index+1).map(t=>[...t]),s=t.slice(o.index+1).map(t=>[...t]);return o.t>0||0===r.length?r.push(i):r[r.length-1]=i,(o.t<1||0===s.length)&&s.unshift(i),[r,s]}export function densify(e,o,i=3){if(e.length<2||!(o>0))return e.map(t=>[...t]);const r=[[...e[0]]];for(let s=1;s<e.length;s++){const l=e[s-1],c=e[s],u=Math.ceil(t(l,c,i)/o);for(let t=1;t<u;t++)r.push(h(n(l,c,t/u),l));r.push([...c])}return r}export function resample(t,n,e=3){if(0===t.length||n<1)return[];if(1===n)return[[...t[0]]];const o=l(t,e),i=[];for(let r=0;r<n;r++)i.push(h(pointAlong(t,o*r/(n-1),e),t[0]));return i}export function simplify(t,n,e=3){if(t.length<=2||!(n>0))return t.map(t=>[...t]);const o=new Uint8Array(t.length);o[0]=1,o[t.length-1]=1;const i=[[0,t.length-1]];for(;i.length>0;){const[r,s]=i.pop();let l=-1,c=n;for(let n=r+1;n<s;n++){const o=closestOnSegment(t[n],t[r],t[s],e).distance;o>c&&(c=o,l=n)}l>=0&&(o[l]=1,i.push([r,l],[l,s]))}return t.filter((t,n)=>o[n]).map(t=>[...t])}export function simplifyRing(n,e,o=3){const i=isClosedLine(n)?n.slice(0,-1):n;if(i.length<=3)return closeLine(i);let r=1,s=-1;i.forEach((n,e)=>{const l=t(i[0],n,o);l>s&&(s=l,r=e)});let l=[...simplify(i.slice(0,r+1),e,o),...simplify([...i.slice(r),i[0]],e,o).slice(1,-1)];return l.length<3&&(l=[i[0],i[r],i[Math.max(1,Math.floor((r+i.length)/2))%i.length]].map(t=>[...t])),closeLine(l)}export function smooth(t,e=2,o=!1){let i=(o&&isClosedLine(t)?t.slice(0,-1):t).map(t=>[...t]);if(i.length<3)return o?closeLine(i):i;for(let t=0;t<e;t++){const t=[],e=i.length;o||t.push(i[0]);const r=o?e:e-1;for(let s=0;s<r;s++){const l=i[s],c=i[(s+1)%e],u=h(n(l,c,.25),l),f=h(n(l,c,.75),l);(o||s>0)&&t.push(u),(o||s<r-1)&&t.push(f)}o||t.push(i[e-1]),i=t}return o?closeLine(i):i}export function offsetLine(t,n,e=4){const o=t.filter((n,e)=>0===e||n[0]!==t[e-1][0]||n[1]!==t[e-1][1]);if(o.length<2||0===n)return o.map(t=>[...t]);const i=[];for(let t=1;t<o.length;t++){const n=o[t][0]-o[t-1][0],e=o[t][1]-o[t-1][1],r=Math.hypot(n,e);i.push([-e/r,n/r])}const s=[],l=(t,e,o)=>h([t[0]+e*n,t[1]+o*n,r(t)],t);s.push(l(o[0],i[0][0],i[0][1]));for(let t=1;t<o.length-1;t++){const[n,r]=i[t-1],[c,h]=i[t];let u=n+c,f=r+h;const p=Math.hypot(u,f);if(p<1e-9){s.push(l(o[t],n,r),l(o[t],c,h));continue}u/=p,f/=p;const a=u*n+f*r,g=1/Math.max(a,1e-9);g>e?s.push(l(o[t],n,r),l(o[t],c,h)):s.push(l(o[t],u*g,f*g))}const c=i[i.length-1];return s.push(l(o[o.length-1],c[0],c[1])),s}export function segmentIntersection(t,e,o,i){const l=e[0]-t[0],c=e[1]-t[1],h=i[0]-o[0],u=i[1]-o[1],f=l*u-c*h,p=Math.max(Math.abs(l),Math.abs(c),1)*Math.max(Math.abs(h),Math.abs(u),1);if(Math.abs(f)<=1e-12*p)return null;const a=o[0]-t[0],g=o[1]-t[1],x=(a*u-g*h)/f,m=(a*c-g*l)/f,M=1e-12;if(x<-M||x>1+M||m<-M||m>1+M)return null;const d=s(x),A=s(m);return{point:n(t,e,d),t:d,u:A,zB:r(o)+(r(i)-r(o))*A}}export function lineIntersections(t,n){const e=[],o=c(n);for(let i=1;i<t.length;i++){const r=t[i-1],s=t[i],l=Math.min(r[0],s[0]),c=Math.max(r[0],s[0]),h=Math.min(r[1],s[1]),u=Math.max(r[1],s[1]);for(let t=1;t<n.length;t++){const f=4*(t-1);if(o[f]>c||o[f+2]<l||o[f+1]>u||o[f+3]<h)continue;const p=segmentIntersection(r,s,n[t-1],n[t]);if(!p)continue;const a=e[e.length-1];a&&Math.abs(a.point[0]-p.point[0])<1e-9&&Math.abs(a.point[1]-p.point[1])<1e-9||e.push({point:p.point,indexA:i-1,indexB:t-1,t:p.t,u:p.u,zB:p.zB})}}return e}export function selfIntersections(t,n=isClosedLine(t)){const e=[],o=t.length-1,i=c(t);for(let r=0;r<o;r++)for(let s=r+2;s<o;s++){if(n&&0===r&&s===o-1)continue;const l=4*r,c=4*s;if(i[c]>i[l+2]||i[c+2]<i[l]||i[c+1]>i[l+3]||i[c+3]<i[l+1])continue;const h=segmentIntersection(t[r],t[r+1],t[s],t[s+1]);h&&e.push({point:h.point,indexA:r,indexB:s,t:h.t,u:h.u,zB:h.zB})}return e}export function segmentApproach(e,o,i,l,c=3){const h=t=>3===c?r(t):0,u=[o[0]-e[0],o[1]-e[1],h(o)-h(e)],f=[l[0]-i[0],l[1]-i[1],h(l)-h(i)],p=[e[0]-i[0],e[1]-i[1],h(e)-h(i)],a=(t,n)=>t[0]*n[0]+t[1]*n[1]+t[2]*n[2],g=a(u,u),x=a(f,f),m=a(f,p),M=1e-18;let d,A;if(g<=M&&x<=M)d=0,A=0;else if(g<=M)d=0,A=s(m/x);else{const t=a(u,p);if(x<=M)A=0,d=s(-t/g);else{const n=a(u,f),e=g*x-n*n;d=e>M?s((n*m-t*x)/e):0,A=(n*d+m)/x,A<0?(A=0,d=s(-t/g)):A>1&&(A=1,d=s((n-t)/g))}}const L=n(e,o,d),y=n(i,l,A);return{distance:t(L,y,c),pointA:L,pointB:y,s:d,t:A,indexA:0,indexB:0}}export function lineApproach(t,n,e=3){const o=1===t.length?[t[0],t[0]]:t,i=1===n.length?[n[0],n[0]]:n;if(o.length<2||i.length<2)return null;let r=null;const s=c(i);for(let t=1;t<o.length;t++){const n=Math.min(o[t-1][0],o[t][0]),l=Math.max(o[t-1][0],o[t][0]),c=Math.min(o[t-1][1],o[t][1]),h=Math.max(o[t-1][1],o[t][1]);for(let u=1;u<i.length;u++){if(r){const t=4*(u-1),e=Math.max(0,s[t]-l,n-s[t+2]),o=Math.max(0,s[t+1]-h,c-s[t+3]);if(Math.hypot(e,o)>r.distance)continue}const f=segmentApproach(o[t-1],o[t],i[u-1],i[u],e);(!r||f.distance<r.distance)&&(r={...f,indexA:t-1,indexB:u-1})}}return r}export function isClosedLine(t){if(t.length<3)return!1;const n=t[0],e=t[t.length-1];return n[0]===e[0]&&n[1]===e[1]&&r(n)===r(e)}export function closeLine(t){const n=t.map(t=>[...t]);return n.length>0&&!isClosedLine(n)&&n.push([...n[0]]),n}export function reverseLine(t){return t.map(t=>[...t]).reverse()}export function sideOf(t,n,e){return Math.sign(o(t,n,e))}function l(n,e){let o=0;for(let i=1;i<n.length;i++)o+=t(n[i-1],n[i],e);return o}function c(t){const n=new Float64Array(4*Math.max(0,t.length-1));for(let e=1;e<t.length;e++){const o=t[e-1],i=t[e],r=4*(e-1);n[r]=Math.min(o[0],i[0]),n[r+1]=Math.min(o[1],i[1]),n[r+2]=Math.max(o[0],i[0]),n[r+3]=Math.max(o[1],i[1])}return n}function h(t,n){return n.length>2?t:[t[0],t[1]]}
@@ -0,0 +1,71 @@
1
+ import { Bounds3, Geometry, Position } from '../data/Geometry';
2
+ import { Dims, Vec3 } from './Vector';
3
+ /**
4
+ * Measuring geometry in its own world units: metres in a projected or local reference. Lengths
5
+ * and distances count heights unless asked not to (`dims` 2); areas are in x and y, as a map
6
+ * shows them, except `area3D`, which is in the polygon's own plane - a roof's slope, a wall.
7
+ *
8
+ * For longitude and latitude, `Geodesy.ts` measures on the ellipsoid.
9
+ */
10
+ /** The distance between two positions, heights counted (`dims` 2: in x and y alone). */
11
+ export declare function distance(a: Position, b: Position, dims?: Dims): number;
12
+ /** The distance in x and y alone: across a map. */
13
+ export declare function distance2D(a: Position, b: Position): number;
14
+ /** The way from `a` to `b`, in degrees clockwise from north (+y): 0 north, 90 east. */
15
+ export declare function bearing(a: Position, b: Position): number;
16
+ /** How much `b` is above `a` per unit across the ground: 0.005 is 5 ‰. Infinite straight up. */
17
+ export declare function slope(a: Position, b: Position): number;
18
+ /** The angle at `vertex` between the ways to `a` and to `c`, in degrees, 0 to 180, in 3D. */
19
+ export declare function angleAt(a: Position, vertex: Position, c: Position): number;
20
+ /**
21
+ * How much a path turns at `b`, going from `a` through `b` to `c`, in degrees across the map:
22
+ * positive to the left (counter-clockwise), negative to the right, 0 straight on.
23
+ */
24
+ export declare function turnAngle(a: Position, b: Position, c: Position): number;
25
+ /** A line's length along all its segments, heights counted (`dims` 2: across the map). */
26
+ export declare function pathLength(line: readonly Position[], dims?: Dims): number;
27
+ /**
28
+ * The length of a geometry: a line's along it, a multi-line's all parts, a polygon's every ring
29
+ * round (its perimeter); 0 for points.
30
+ */
31
+ export declare function length(geometry: Geometry | null | undefined, dims?: Dims): number;
32
+ /** The length round every ring of a polygon or multi-polygon; 0 for another kind. */
33
+ export declare function perimeter(geometry: Geometry | null | undefined, dims?: Dims): number;
34
+ /** A ring's area across the map, signed: positive counter-clockwise, negative clockwise. */
35
+ export declare function ringArea(ring: readonly Position[]): number;
36
+ /** A polygon's area across the map: its outer ring's less its holes'. Multi-polygons add up. */
37
+ export declare function area(geometry: Geometry | null | undefined): number;
38
+ /** The vector area of a ring in 3D (Newell): its direction the ring's normal, its length twice its area. */
39
+ export declare function ringNormal(ring: readonly Position[]): Vec3;
40
+ /**
41
+ * A polygon's area in its own plane, holes taken off: a sloped roof's or a wall's true area,
42
+ * where `area` gives what it covers on the map.
43
+ */
44
+ export declare function area3D(geometry: Geometry | null | undefined): number;
45
+ /** The volume of a polygon stood up `height` high: its area times its height. */
46
+ export declare function extrudedVolume(geometry: Geometry | null | undefined, height: number): number;
47
+ /** Where a geometry is: its box in world units, heights included. */
48
+ export declare function geometryBounds(geometry: Geometry | null | undefined): Bounds3;
49
+ /**
50
+ * A geometry's centre of mass: a polygon's by area (holes left out), a line's by length, points'
51
+ * their mean. Where a geometry mixes them, its largest dimension decides. Its height is its
52
+ * positions' mean height, weighted as its x and y are.
53
+ */
54
+ export declare function centroid(geometry: Geometry | null | undefined): Vec3 | null;
55
+ /**
56
+ * A point surely inside a polygon - for a label, a marker - where its centroid may not be (a
57
+ * U-shape, a ring): the middle of the widest stretch inside it along the line across its
58
+ * middle. The largest polygon of a multi-polygon. The centroid for other kinds.
59
+ */
60
+ export declare function pointOnSurface(geometry: Geometry | null | undefined): Vec3 | null;
61
+ /**
62
+ * A line's long section: at each point, how far along it is (across the map) and how high; and
63
+ * each segment's slope, rise over run - a sewer's fall, a road's grade.
64
+ */
65
+ export declare function lineProfile(line: readonly Position[]): {
66
+ along: number[];
67
+ z: number[];
68
+ slopes: number[];
69
+ };
70
+ /** A ring with its first point repeated at its end, as GeoJSON keeps them. */
71
+ export declare function closed(ring: readonly Position[]): readonly Position[];
@@ -0,0 +1 @@
1
+ import{emptyBounds as t,extendBounds as o,forEachPosition as n,linesOf as r,polygonsOf as e}from"../data/Geometry.js";import{openLength as c}from"./Triangulate.js";import{cross3 as f,distanceOf as s,length3 as u,sub3 as a,zOf as i}from"./Vector.js";export function distance(t,o,n=3){return s(t,o,n)}export function distance2D(t,o){return s(t,o,2)}export function bearing(t,o){return(180*Math.atan2(o[0]-t[0],o[1]-t[1])/Math.PI+360)%360}export function slope(t,o){const n=s(t,o,2),r=i(o)-i(t);return n>0?r/n:0===r?0:r>0?1/0:-1/0}export function angleAt(t,o,n){const r=a(t,o),e=a(n,o),c=u(r),f=u(e);if(0===c||0===f)return 0;const s=(r[0]*e[0]+r[1]*e[1]+r[2]*e[2])/(c*f);return 180*Math.acos(Math.max(-1,Math.min(1,s)))/Math.PI}export function turnAngle(t,o,n){const r=Math.atan2(o[1]-t[1],o[0]-t[0]);let e=180*(Math.atan2(n[1]-o[1],n[0]-o[0])-r)/Math.PI;for(;e>180;)e-=360;for(;e<=-180;)e+=360;return e}export function pathLength(t,o=3){let n=0;for(let r=1;r<t.length;r++)n+=s(t[r-1],t[r],o);return n}export function length(t,o=3){if(!t)return 0;let n=0;for(const e of r(t))n+=pathLength(e,o);return n+=perimeter(t,o),n}export function perimeter(t,o=3){let n=0;for(const r of e(t))for(const t of r)n+=pathLength(closed(t),o);return n}export function ringArea(t){const o=c(t);let n=0;for(let r=0,e=o-1;r<o;e=r++)n+=(t[e][0]-t[r][0])*(t[e][1]+t[r][1]);return n/2}export function area(t){let o=0;for(const n of e(t))n.forEach((t,n)=>{const r=Math.abs(ringArea(t));o+=0===n?r:-r});return Math.max(0,o)}export function ringNormal(t){const o=c(t),n=[0,0,0];for(let r=0;r<o;r++){const e=t[r],c=t[(r+1)%o],s=f(e,c);n[0]+=s[0],n[1]+=s[1],n[2]+=s[2]}return n}export function area3D(t){let o=0;for(const n of e(t))n.forEach((t,n)=>{const r=u(ringNormal(t))/2;o+=0===n?r:-r});return Math.max(0,o)}export function extrudedVolume(t,o){return area(t)*Math.abs(o)}export function geometryBounds(r){const e=t();return n(r,t=>o(e,t)),e}export function centroid(t){if(!t)return null;let o=0,f=0,u=0,a=0;for(const n of e(t))n.forEach((t,n)=>{const r=c(t);if(r<3)return;const e=0===n?1:-1,[s,l]=t[0];let p=0,h=0,x=0,m=0;for(let o=0;o<r;o++){const n=t[o],e=t[(o+1)%r],c=n[0]-s,f=n[1]-l,u=e[0]-s,a=e[1]-l,g=c*a-u*f;p+=(c+u)*g,h+=(f+a)*g,x+=g,m+=i(n)}if(0===x)return;const g=Math.abs(x/2)*e;o+=(s+p/(3*x))*g,f+=(l+h/(3*x))*g,u+=m/r*g,a+=g});if(Math.abs(a)>1e-12)return[o/a,f/a,u/a];let l=0,p=0,h=0,x=0;const m=[...r(t)];for(const o of e(t))for(const t of o)m.push(closed(t));for(const t of m)for(let o=1;o<t.length;o++){const n=t[o-1],r=t[o],e=s(n,r);l+=(n[0]+r[0])/2*e,p+=(n[1]+r[1])/2*e,h+=(i(n)+i(r))/2*e,x+=e}if(x>0)return[l/x,p/x,h/x];let g=0,d=0,M=0,y=0;return n(t,t=>{g+=t[0],d+=t[1],M+=i(t),y++}),y>0?[g/y,d/y,M/y]:null}export function pointOnSurface(t){const o=e(t);if(0===o.length)return centroid(t);let n=null,r=-1;for(const t of o){const o=geometryBounds({type:"Polygon",coordinates:t}),e=centroid({type:"Polygon",coordinates:t}),f=[e?e[1]:(o.min[1]+o.max[1])/2];for(let t=1;t<=7;t++)f.push(o.min[1]+(o.max[1]-o.min[1])*t/8);for(const s of f){const f=[];for(const o of t){const t=c(o);for(let n=0,r=t-1;n<t;r=n++){const t=o[r],e=o[n];t[1]>s!=e[1]>s&&f.push(t[0]+(s-t[1])/(e[1]-t[1])*(e[0]-t[0]))}}f.sort((t,o)=>t-o);for(let t=0;t+1<f.length;t+=2){const o=f[t+1]-f[t];o>r&&(r=o,n=[(f[t]+f[t+1])/2,s,e?e[2]:0])}if(r>.5*(o.max[0]-o.min[0]))break}}return n??centroid(t)}export function lineProfile(t){const o=[],n=[],r=[];let e=0;return t.forEach((c,f)=>{f>0&&(e+=s(t[f-1],c,2),r.push(slope(t[f-1],c))),o.push(e),n.push(i(c))}),{along:o,z:n,slopes:r}}export function closed(t){if(t.length<2)return t;const o=t[0],n=t[t.length-1];return o[0]===n[0]&&o[1]===n[1]&&i(o)===i(n)?t:[...t,o]}
@@ -0,0 +1,59 @@
1
+ import type { Geometry, PolygonGeometry, Position } from '../data/Geometry';
2
+ import { Vec3 } from './Vector';
3
+ /**
4
+ * Polygons across the map: what is inside one, rings put the right way round, shapes made to
5
+ * size - circles, rectangles, sectors - and whether a polygon is sound. Angles here are bearings,
6
+ * like `bearing`'s: degrees clockwise from north (+y).
7
+ */
8
+ /** A box across the map. */
9
+ export interface Box2 {
10
+ minX: number;
11
+ minY: number;
12
+ maxX: number;
13
+ maxY: number;
14
+ }
15
+ /** Whether `p` is inside a ring, across the map (crossing number). A point on its edge may go either way. */
16
+ export declare function pointInRing(p: Position, ring: readonly Position[]): boolean;
17
+ /** Whether `p` is inside a polygon: in its outer ring and in none of its holes. */
18
+ export declare function pointInPolygon(p: Position, polygon: readonly (readonly Position[])[]): boolean;
19
+ /** Whether `p` is inside a polygon or a multi-polygon's parts; false for other kinds. */
20
+ export declare function pointInGeometry(p: Position, geometry: Geometry | null | undefined): boolean;
21
+ /** Whether a ring runs clockwise across the map. */
22
+ export declare function isClockwise(ring: readonly Position[]): boolean;
23
+ /**
24
+ * A polygon's rings the GeoJSON way round (right-hand rule): its outer ring counter-clockwise,
25
+ * its holes clockwise, every ring closed.
26
+ */
27
+ export declare function orientPolygon(polygon: readonly (readonly Position[])[]): Position[][];
28
+ /** The smallest convex polygon round some points (monotone chain): a closed, counter-clockwise ring. */
29
+ export declare function convexHull(points: readonly Position[]): Position[];
30
+ /** A circle as a polygon of `segments` sides, counter-clockwise, at the centre's height. */
31
+ export declare function circle(center: Position, radius: number, segments?: number): PolygonGeometry;
32
+ /** An ellipse as a polygon: its radii along and across `rotation` (a bearing), counter-clockwise. */
33
+ export declare function ellipse(center: Position, radiusX: number, radiusY: number, rotation?: number, segments?: number): PolygonGeometry;
34
+ /** The rectangle with corners `a` and `b`, along the axes, counter-clockwise, at `a`'s height. */
35
+ export declare function rectangle(a: Position, b: Position): PolygonGeometry;
36
+ /** A rectangle round a centre, `width` across and `depth` along a bearing: a building's footprint, a trench. */
37
+ export declare function orientedRectangle(center: Position, width: number, depth: number, bearingDegrees?: number): PolygonGeometry;
38
+ /** A regular polygon of `sides` round a centre, its first corner on the bearing `rotation`. */
39
+ export declare function regularPolygon(center: Position, radius: number, sides: number, rotation?: number): PolygonGeometry;
40
+ /** An arc round a centre, from one bearing clockwise to another: a line. */
41
+ export declare function arc(center: Position, radius: number, from: number, to: number, segments?: number): Position[];
42
+ /** A pie slice: the arc from one bearing clockwise to another, and back to the centre. */
43
+ export declare function sector(center: Position, radius: number, from: number, to: number, segments?: number): PolygonGeometry;
44
+ /** Where a polygon's rings cross themselves or each other: none for a sound polygon. */
45
+ export declare function polygonKinks(polygon: readonly (readonly Position[])[]): Vec3[];
46
+ /** What is wrong with a polygon, if anything: too few points, crossing edges, holes outside it. Empty when sound. */
47
+ export declare function polygonProblems(polygon: readonly (readonly Position[])[]): string[];
48
+ /** Whether a polygon is sound: enough points, an area, no crossing edges, its holes inside it. */
49
+ export declare function isValidPolygon(polygon: readonly (readonly Position[])[]): boolean;
50
+ /**
51
+ * A line cut to a box (Liang-Barsky): the parts of it inside, each a line of its own. Heights
52
+ * are interpolated at the cuts.
53
+ */
54
+ export declare function clipLine(line: readonly Position[], box: Box2): Position[][];
55
+ /**
56
+ * A ring cut to a box (Sutherland-Hodgman): what of it is inside, closed. A concave ring cut in
57
+ * two keeps a seam along the box's edge between the parts.
58
+ */
59
+ export declare function clipRing(ring: readonly Position[], box: Box2): Position[];
@@ -0,0 +1 @@
1
+ import{polygonsOf as t}from"../data/Geometry.js";import{ringArea as n}from"./Measure.js";import{closeLine as o,isClosedLine as e,segmentIntersection as r,selfIntersections as s}from"./Lines.js";import{openLength as i}from"./Triangulate.js";import{orient2D as l,zOf as c}from"./Vector.js";export function pointInRing(t,n){const o=i(n),e=t[0],r=t[1];let s=!1;for(let t=0,i=o-1;t<o;i=t++){const o=n[t],l=n[i];o[1]>r!=l[1]>r&&e<(l[0]-o[0])*(r-o[1])/(l[1]-o[1])+o[0]&&(s=!s)}return s}export function pointInPolygon(t,n){if(0===n.length||!pointInRing(t,n[0]))return!1;for(let o=1;o<n.length;o++)if(pointInRing(t,n[o]))return!1;return!0}export function pointInGeometry(n,o){return t(o).some(t=>pointInPolygon(n,t))}export function isClockwise(t){return n(t)<0}export function orientPolygon(t){return t.map((t,n)=>{const e=o(t),r=isClockwise(e);return(0===n?r:!r)?e.reverse():e})}export function convexHull(t){const n=[...t].sort((t,n)=>t[0]-n[0]||t[1]-n[1]),e=n.filter((t,o)=>0===o||t[0]!==n[o-1][0]||t[1]!==n[o-1][1]);if(e.length<3)return e.map(t=>[...t]);const r=[];for(const t of e){for(;r.length>=2&&l(r[r.length-2],r[r.length-1],t)<=0;)r.pop();r.push(t)}const s=[];for(let t=e.length-1;t>=0;t--){const n=e[t];for(;s.length>=2&&l(s[s.length-2],s[s.length-1],n)<=0;)s.pop();s.push(n)}const i=[...r.slice(0,-1),...s.slice(0,-1)].map(t=>[...t]);return o(i)}function h(t,n,o){const e=n*Math.PI/180;return[t[0]+Math.sin(e)*o,t[1]+Math.cos(e)*o,c(t)]}function u(t,n){const e=n.length>2?t:t.map(t=>[t[0],t[1]]);return o(e)}export function circle(t,n,o=64){return ellipse(t,n,n,0,o)}export function ellipse(t,n,o,e=0,r=64){const s=Math.max(3,Math.round(r)),i=-e*Math.PI/180,l=Math.cos(i),h=Math.sin(i),p=[];for(let e=0;e<s;e++){const r=e/s*Math.PI*2,i=Math.cos(r)*n,u=Math.sin(r)*o;p.push([t[0]+i*l-u*h,t[1]+i*h+u*l,c(t)])}return{type:"Polygon",coordinates:[u(p,t)]}}export function rectangle(t,n){const o=c(t),[e,r]=[Math.min(t[0],n[0]),Math.max(t[0],n[0])],[s,i]=[Math.min(t[1],n[1]),Math.max(t[1],n[1])];return{type:"Polygon",coordinates:[u([[e,s,o],[r,s,o],[r,i,o],[e,i,o]],t)]}}export function orientedRectangle(t,o,e,r=0){const s=h([0,0],r,1),i=h([0,0],r+90,1),l=c(t),p=(n,r)=>[t[0]+i[0]*n*o/2+s[0]*r*e/2,t[1]+i[1]*n*o/2+s[1]*r*e/2,l],a=[p(-1,-1),p(1,-1),p(1,1),p(-1,1)];return n(a)<0&&a.reverse(),{type:"Polygon",coordinates:[u(a,t)]}}export function regularPolygon(t,n,o,e=0){const r=Math.max(3,Math.round(o)),s=[];for(let o=0;o<r;o++)s.push(h(t,e-o/r*360,n));return{type:"Polygon",coordinates:[u(s,t)]}}export function arc(t,n,o,e,r=64){let s=e-o;for(;s<=0;)s+=360;const i=Math.max(1,Math.ceil(r*s/360)),l=[];for(let e=0;e<=i;e++){const r=h(t,o+s*e/i,n);l.push(t.length>2?r:[r[0],r[1]])}return l}export function sector(t,e,r,s,i=64){const l=[[...t],...arc(t,e,r,s,i)],c=o(l);return n(c)<0&&c.reverse(),{type:"Polygon",coordinates:[c]}}export function polygonKinks(t){const n=[];t.forEach(t=>s(o(t),!0).forEach(t=>n.push(t.point)));for(let e=0;e<t.length;e++)for(let s=e+1;s<t.length;s++){const i=o(t[e]),l=o(t[s]);for(let t=1;t<i.length;t++)for(let o=1;o<l.length;o++){const e=r(i[t-1],i[t],l[o-1],l[o]);e&&n.push(e.point)}}return n}export function polygonProblems(t){const o=[];if(0===t.length)return["no rings"];t.forEach((t,e)=>{const r=0===e?"the outer ring":`hole ${e}`;new Set(t.map(t=>`${t[0]},${t[1]}`)).size<3?o.push(`${r} has fewer than three points`):0===Math.abs(n(t))&&o.push(`${r} has no area`)}),polygonKinks(t).length>0&&o.push("edges cross");for(let n=1;n<t.length;n++)t[n].every(n=>pointInRing(n,t[0]))||o.push(`hole ${n} is not inside the outer ring`);return o}export function isValidPolygon(t){return 0===polygonProblems(t).length}export function clipLine(t,n){const o=[];let e=null;for(let r=1;r<t.length;r++){const s=t[r-1],i=t[r],l=i[0]-s[0],h=i[1]-s[1];let u=0,p=1,a=!1;const f=[[-l,s[0]-n.minX],[l,n.maxX-s[0]],[-h,s[1]-n.minY],[h,n.maxY-s[1]]];for(const[t,n]of f)if(0===t)n<0&&(a=!0);else{const o=n/t;t<0?u=Math.max(u,o):p=Math.min(p,o)}if(a||u>p){e=null;continue}const g=n=>{const o=c(s)+(c(i)-c(s))*n,e=[s[0]+l*n,s[1]+h*n];return t[0].length>2?[...e,o]:e};(!e||u>0)&&(e=[g(u)],o.push(e)),e.push(g(p)),p<1&&(e=null)}return o}export function clipRing(t,n){let r=(e(t)?t.slice(0,-1):t).map(t=>[...t]);const s=[[t=>t[0]>=n.minX,(t,o)=>p(t,o,(n.minX-t[0])/(o[0]-t[0]))],[t=>t[0]<=n.maxX,(t,o)=>p(t,o,(n.maxX-t[0])/(o[0]-t[0]))],[t=>t[1]>=n.minY,(t,o)=>p(t,o,(n.minY-t[1])/(o[1]-t[1]))],[t=>t[1]<=n.maxY,(t,o)=>p(t,o,(n.maxY-t[1])/(o[1]-t[1]))]];for(const[t,n]of s){if(0===r.length)break;const o=r;r=[];for(let e=0;e<o.length;e++){const s=o[e],i=o[(e+o.length-1)%o.length];t(s)?(t(i)||r.push(n(i,s)),r.push(s)):t(i)&&r.push(n(i,s))}}return r.length>=3?o(r):[]}function p(t,n,o){const e=[t[0]+(n[0]-t[0])*o,t[1]+(n[1]-t[1])*o];return t.length>2?[...e,c(t)+(c(n)-c(t))*o]:e}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Getting longitude and latitude into world units. The package draws in whatever reference its
3
+ * positions are in; these are the common ones.
4
+ */
5
+ export declare const EARTH_RADIUS = 6378137;
6
+ /** Web Mercator (EPSG:3857): metres at the equator, larger than a metre on the ground elsewhere. */
7
+ export declare function toWebMercator(lon: number, lat: number): [number, number];
8
+ export declare function fromWebMercator(x: number, y: number): [number, number];
9
+ /**
10
+ * World units of Web Mercator per metre on the ground at a Mercator y: 1 / cos(latitude). A real
11
+ * width in metres is this many world units there (`GpuLines.worldPerMetre`).
12
+ */
13
+ export declare function mercatorWorldPerMetre(y: number): number;
14
+ /** Geocentric (EPSG:4978, ECEF) metres for a WGS84 longitude, latitude and height. */
15
+ export declare function toGeocentric(lon: number, lat: number, height?: number): [number, number, number];
16
+ /** The ellipsoid's up (WGS84) at a geocentric point: its normal there. */
17
+ export declare function ellipsoidUp([x, y, z]: readonly number[]): [number, number, number];
18
+ /**
19
+ * A local east-north-up frame in metres round a longitude and latitude: good for a town, where
20
+ * the earth's curve is a few centimetres. Returns the forward and inverse projection.
21
+ */
22
+ export declare function localProjection(originLon: number, originLat: number): {
23
+ forward: (lon: number, lat: number, z?: number) => [number, number, number];
24
+ inverse: (x: number, y: number) => [number, number];
25
+ };
@@ -0,0 +1 @@
1
+ export const EARTH_RADIUS=6378137;const t=6378137,a=.00669437999014;export function toWebMercator(t,a){const o=Math.max(-85.0511287798,Math.min(85.0511287798,a));return[t*Math.PI/180*6378137,6378137*Math.log(Math.tan(Math.PI/4+o*Math.PI/180/2))]}export function fromWebMercator(t,a){return[t/6378137*180/Math.PI,180*(2*Math.atan(Math.exp(a/6378137))-Math.PI/2)/Math.PI]}export function mercatorWorldPerMetre(t){return 1/Math.cos(2*Math.atan(Math.exp(t/6378137))-Math.PI/2)}export function toGeocentric(o,r,n=0){const M=o*Math.PI/180,h=r*Math.PI/180,e=Math.sin(h),c=t/Math.sqrt(1-a*e*e);return[(c+n)*Math.cos(h)*Math.cos(M),(c+n)*Math.cos(h)*Math.sin(M),(c*(1-a)+n)*e]}export function ellipsoidUp([a,o,r]){const n=t**2,M=[a/n,o/n,r/40408299984659.16],h=Math.hypot(M[0],M[1],M[2])||1;return[M[0]/h,M[1]/h,M[2]/h]}export function localProjection(t,a){const o=111132.92-559.82*Math.cos(2*a*Math.PI/180),r=111412.84*Math.cos(a*Math.PI/180);return{forward:(n,M,h=0)=>[(n-t)*r,(M-a)*o,h],inverse:(n,M)=>[t+n/r,a+M/o]}}
@@ -0,0 +1,27 @@
1
+ import { Geometry, Position } from '../data/Geometry';
2
+ import { Dims, Vec3 } from './Vector';
3
+ /** The point of a geometry nearest `p`, and how far: inside a polygon (across the map), `p` itself at 0. */
4
+ export declare function nearestPointOnGeometry(geometry: Geometry | null | undefined, p: Position, dims?: Dims): {
5
+ point: Vec3;
6
+ distance: number;
7
+ } | null;
8
+ /** How far `p` is from a geometry: 0 on it, or inside a polygon across the map. Infinity from nothing. */
9
+ export declare function distanceToGeometry(p: Position, geometry: Geometry | null | undefined, dims?: Dims): number;
10
+ /**
11
+ * How close two geometries come, and their nearest points: 0 where they touch or overlap
12
+ * across the map (`dims` 2). Infinity if either is empty.
13
+ */
14
+ export declare function geometryDistance(a: Geometry | null | undefined, b: Geometry | null | undefined, dims?: Dims): {
15
+ distance: number;
16
+ pointA: Vec3;
17
+ pointB: Vec3;
18
+ };
19
+ /** Whether two geometries touch or overlap across the map. */
20
+ export declare function intersects(a: Geometry | null | undefined, b: Geometry | null | undefined): boolean;
21
+ /**
22
+ * Whether `container`, a polygon or multi-polygon, holds all of `inner` across the map: every
23
+ * point of it inside, and no edge of it crossing out.
24
+ */
25
+ export declare function contains(container: Geometry | null | undefined, inner: Geometry | null | undefined): boolean;
26
+ /** Whether `inner` lies wholly within `container`: `contains` the other way round. */
27
+ export declare function within(inner: Geometry | null | undefined, container: Geometry | null | undefined): boolean;
@@ -0,0 +1 @@
1
+ import{linesOf as t,pointsOf as n,polygonsOf as o}from"../data/Geometry.js";import{closeLine as e,closestOnSegment as r,lineApproach as i,segmentIntersection as s}from"./Lines.js";import{geometryBounds as c}from"./Measure.js";import{pointInPolygon as f}from"./Polygons.js";import{distanceOf as a,zOf as u}from"./Vector.js";function m(n){const r=[...t(n)];for(const t of o(n))for(const n of t)r.push(e(n));return r}export function nearestPointOnGeometry(t,e,i=2){if(!t)return null;if(2===i&&o(t).some(t=>f(e,t)))return{point:[e[0],e[1],u(e)],distance:0};let s=null;for(const o of n(t)){const t=a(e,o,i);(!s||t<s.distance)&&(s={point:[o[0],o[1],u(o)],distance:t})}for(const n of m(t)){if(1===n.length){const t=a(e,n[0],i);(!s||t<s.distance)&&(s={point:[n[0][0],n[0][1],u(n[0])],distance:t})}for(let t=1;t<n.length;t++){const o=r(e,n[t-1],n[t],i);(!s||o.distance<s.distance)&&(s={point:o.point,distance:o.distance})}}return s}export function distanceToGeometry(t,n,o=2){return nearestPointOnGeometry(n,t,o)?.distance??1/0}export function geometryDistance(t,e,r=2){const s={distance:1/0,pointA:[NaN,NaN,NaN],pointB:[NaN,NaN,NaN]};if(!t||!e)return s;if(2===r)for(const[n,r]of[[t,e],[e,t]]){const t=p(n);if(t&&o(r).some(n=>f(t,n))){const n=[t[0],t[1],u(t)];return{distance:0,pointA:n,pointB:n}}}let c=s;const a=[...m(t),...n(t).map(t=>[t])],l=[...m(e),...n(e).map(t=>[t])];for(const t of a)for(const n of l){const o=i(t,n,r);o&&o.distance<c.distance&&(c={distance:o.distance,pointA:o.pointA,pointB:o.pointB})}return c}export function intersects(t,n){if(!t||!n)return!1;const o=c(t),e=c(n);return!(o.max[0]<e.min[0]||e.max[0]<o.min[0]||o.max[1]<e.min[1]||e.max[1]<o.min[1])&&0===geometryDistance(t,n,2).distance}export function contains(t,e){const r=o(t);if(0===r.length||!e)return!1;const i=[...n(e)];if(m(e).forEach(t=>i.push(...t)),0===i.length)return!1;if(!i.every(t=>r.some(n=>f(t,n))))return!1;const c=m(t);for(const t of m(e))for(let n=1;n<t.length;n++)for(const o of c)for(let e=1;e<o.length;e++){const r=s(t[n-1],t[n],o[e-1],o[e]);if(r&&r.t>1e-9&&r.t<1-1e-9&&r.u>1e-9&&r.u<1-1e-9)return!1}return!0}export function within(t,n){return contains(n,t)}function p(t){const o=n(t);if(o.length>0)return o[0];for(const n of m(t))if(n.length>0)return n[0];return null}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A static R-tree over boxes across the map, packed sort-tile-recursive: add every box, `finish`,
3
+ * then ask what lies in a box or what is nearest a point, in increasing distance. A million
4
+ * boxes build in well under a second; a query reads a few dozen nodes.
5
+ *
6
+ * ```ts
7
+ * const index = new SpatialIndex();
8
+ * segments.forEach(s => index.add(s.minX, s.minY, s.maxX, s.maxY));
9
+ * index.finish();
10
+ * index.search(x - r, y - r, x + r, y + r); // the indices, in the order added
11
+ * ```
12
+ */
13
+ export declare class SpatialIndex {
14
+ readonly nodeSize: number;
15
+ private boxes;
16
+ private items;
17
+ private root;
18
+ private finished;
19
+ constructor(nodeSize?: number);
20
+ /** Adds a box; its index is the number of boxes added before it. */
21
+ add(minX: number, minY: number, maxX: number, maxY: number): number;
22
+ get size(): number;
23
+ /** Builds the tree; nothing can be added after. */
24
+ finish(): this;
25
+ /** Every box that touches the query box, by index. `filter` may leave some out. */
26
+ search(minX: number, minY: number, maxX: number, maxY: number, filter?: (index: number) => boolean): number[];
27
+ /**
28
+ * Visits boxes in order of their distance from a point, nearest first, until `visit` returns
29
+ * false or the boxes run out. A box the point is in is 0 away.
30
+ */
31
+ neighbors(x: number, y: number, visit: (index: number, distance: number) => boolean | void): void;
32
+ /** The `k` boxes nearest a point, no further than `maxDistance`, nearest first. */
33
+ nearest(x: number, y: number, k?: number, maxDistance?: number, filter?: (index: number) => boolean): number[];
34
+ /** A box's corners, as added. */
35
+ boxOf(index: number): [number, number, number, number];
36
+ }
@@ -0,0 +1 @@
1
+ export class SpatialIndex{constructor(t=16){this.nodeSize=t,this.boxes=[],this.items=new Float64Array(0),this.root=null,this.finished=!1}add(t,s,i,n){if(this.finished)throw new Error("SpatialIndex: add after finish");return this.boxes.push(t,s,i,n),this.boxes.length/4-1}get size(){return this.finished?this.items.length/4:this.boxes.length/4}finish(){if(this.finished)return this;this.finished=!0,this.items=Float64Array.from(this.boxes),this.boxes=[];const i=this.items.length/4;if(0===i)return this;let n=t(Array.from({length:i},(t,s)=>s),t=>this.items[4*t]+this.items[4*t+2],t=>this.items[4*t+1]+this.items[4*t+3],this.nodeSize).map(t=>function(t,s){const i={minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0,children:null,items:Int32Array.from(t)};for(const n of t){const t=4*n;s[t]<i.minX&&(i.minX=s[t]),s[t+1]<i.minY&&(i.minY=s[t+1]),s[t+2]>i.maxX&&(i.maxX=s[t+2]),s[t+3]>i.maxY&&(i.maxY=s[t+3])}return i}(t,this.items));for(;n.length>1;)n=t(n,t=>t.minX+t.maxX,t=>t.minY+t.maxY,this.nodeSize).map(s);return this.root=n[0],this}search(t,s,i,n,e){const o=[];if(!this.root)return o;const r=[this.root],h=this.items;for(;r.length>0;){const m=r.pop();if(!(m.maxX<t||m.maxY<s||m.minX>i||m.minY>n))if(m.items)for(const r of m.items){const m=4*r;h[m+2]<t||h[m+3]<s||h[m]>i||h[m+1]>n||(e&&!e(r)||o.push(r))}else for(const t of m.children)r.push(t)}return o}neighbors(t,s,e){if(!this.root)return;const o=new n,r=this.items;for(o.push(this.root,i(t,s,this.root.minX,this.root.minY,this.root.maxX,this.root.maxY));o.size>0;){const n=o.peekDistance(),h=o.pop();if("number"!=typeof h)if(h.items)for(const n of h.items){const e=4*n;o.push(n,i(t,s,r[e],r[e+1],r[e+2],r[e+3]))}else for(const n of h.children)o.push(n,i(t,s,n.minX,n.minY,n.maxX,n.maxY));else if(!1===e(h,n))return}}nearest(t,s,i=1,n=1/0,e){const o=[];return this.neighbors(t,s,(t,s)=>!(s>n||o.length>=i)&&(e&&!e(t)||o.push(t),o.length<i)),o}boxOf(t){const s=this.finished?this.items:this.boxes,i=4*t;return[s[i],s[i+1],s[i+2],s[i+3]]}}function t(t,s,i,n){const e=[],o=Math.ceil(t.length/n),r=Math.ceil(Math.sqrt(o))*n,h=t.map(t=>({e:t,x:s(t),y:i(t)})).sort((t,s)=>t.x-s.x);for(let t=0;t<h.length;t+=r){const s=h.slice(t,t+r).sort((t,s)=>t.y-s.y);for(let t=0;t<s.length;t+=n)e.push(s.slice(t,t+n).map(t=>t.e))}return e}function s(t){const s={minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0,children:t,items:null};for(const i of t)i.minX<s.minX&&(s.minX=i.minX),i.minY<s.minY&&(s.minY=i.minY),i.maxX>s.maxX&&(s.maxX=i.maxX),i.maxY>s.maxY&&(s.maxY=i.maxY);return s}function i(t,s,i,n,e,o){const r=t<i?i-t:t>e?t-e:0,h=s<n?n-s:s>o?s-o:0;return Math.hypot(r,h)}class n{constructor(){this.entries=[],this.distances=[]}get size(){return this.entries.length}peekDistance(){return this.distances[0]}push(t,s){const i=this.entries,n=this.distances;let e=i.length;for(i.push(t),n.push(s);e>0;){const t=e-1>>1;if(n[t]<=s)break;i[e]=i[t],n[e]=n[t],e=t}i[e]=t,n[e]=s}pop(){const t=this.entries,s=this.distances,i=t[0],n=t.pop(),e=s.pop(),o=t.length;if(o>0){let i=0;for(;;){const n=2*i+1;if(n>=o)break;const r=n+1,h=r<o&&s[r]<s[n]?r:n;if(s[h]>=e)break;t[i]=t[h],s[i]=s[h],i=h}t[i]=n,s[i]=e}return i}}
@@ -0,0 +1,36 @@
1
+ import type { Geometry, GeometryFeature, Position } from '../data/Geometry';
2
+ /**
3
+ * Geometry moved, turned, sized and re-heighted: every function returns a new geometry of the
4
+ * same kind and leaves the one given as it was. Rotations are counter-clockwise seen from above,
5
+ * as angles in x and y go; each defaults its pivot to the geometry's centroid.
6
+ */
7
+ /** A geometry with every position replaced by what `map` makes of it. */
8
+ export declare function mapPositions<G extends Geometry>(geometry: G, map: (p: Position) => Position): G;
9
+ /** A copy that shares nothing with the original. */
10
+ export declare function cloneGeometry<G extends Geometry>(geometry: G): G;
11
+ /** A feature with its geometry replaced, its id and properties kept. */
12
+ export declare function withGeometry<F extends GeometryFeature<any>>(feature: F, geometry: Geometry | null): F;
13
+ /** Moved by `dx`, `dy` and `dz`. A position without a height gets one only if `dz` is not 0. */
14
+ export declare function translate<G extends Geometry>(geometry: G, dx: number, dy: number, dz?: number): G;
15
+ /** Turned by `degrees` counter-clockwise round `pivot` (its centroid by default), across the map. */
16
+ export declare function rotate<G extends Geometry>(geometry: G, degrees: number, pivot?: Position): G;
17
+ /**
18
+ * Sized by `factor` - one for all, or one each for x, y and z - away from `pivot` (its centroid
19
+ * by default). Heights are sized only with a z factor given.
20
+ */
21
+ export declare function scale<G extends Geometry>(geometry: G, factor: number | readonly [number, number] | readonly [number, number, number], pivot?: Position): G;
22
+ /**
23
+ * Through a 2D affine transform `[a, b, c, d, e, f]`: x' = a x + b y + c, y' = d x + e y + f.
24
+ * Heights kept.
25
+ */
26
+ export declare function affine<G extends Geometry>(geometry: G, m: readonly [number, number, number, number, number, number]): G;
27
+ /** Through a 4 by 4 matrix, row by row (as `FrameInput.viewProjection`), divided by w. */
28
+ export declare function applyMatrix<G extends Geometry>(geometry: G, m: ArrayLike<number>): G;
29
+ /** Every position at height `z`, or at the height `z` gives it - a terrain, a pipe's invert. */
30
+ export declare function withZ<G extends Geometry>(geometry: G, z: number | ((p: Position) => number)): G;
31
+ /** Every position without its height: flat on the map. */
32
+ export declare function dropZ<G extends Geometry>(geometry: G): G;
33
+ /** A multi-geometry's parts as geometries of their own; a single one as it is. */
34
+ export declare function explode(geometry: Geometry | null | undefined): Geometry[];
35
+ /** Geometries of one kind as one multi-geometry: points, lines or polygons. Null for none, or for mixed kinds. */
36
+ export declare function combine(geometries: readonly Geometry[]): Geometry | null;
@@ -0,0 +1 @@
1
+ import{centroid as t}from"./Measure.js";import{zOf as n}from"./Vector.js";export function mapPositions(t,n){switch(t.type){case"Point":return{...t,coordinates:n(t.coordinates)};case"MultiPoint":case"LineString":return{...t,coordinates:t.coordinates.map(n)};case"MultiLineString":case"Polygon":return{...t,coordinates:t.coordinates.map(t=>t.map(n))};case"MultiPolygon":return{...t,coordinates:t.coordinates.map(t=>t.map(t=>t.map(n)))}}return t}export function cloneGeometry(t){return mapPositions(t,t=>[...t])}export function withGeometry(t,n){return{...t,geometry:n}}export function translate(t,o,e,r=0){return mapPositions(t,t=>t.length>2||0!==r?[t[0]+o,t[1]+e,n(t)+r]:[t[0]+o,t[1]+e])}export function rotate(n,o,e){const r=e??t(n)??[0,0,0],i=o*Math.PI/180,a=Math.cos(i),s=Math.sin(i);return mapPositions(n,t=>{const n=t[0]-r[0],o=t[1]-r[1],e=[r[0]+n*a-o*s,r[1]+n*s+o*a];return t.length>2?[...e,t[2]]:e})}export function scale(o,e,r){const i=r??t(o)??[0,0,0],[a,s,c]="number"==typeof e?[e,e,1]:[e[0],e[1],e[2]??1];return mapPositions(o,t=>{const o=[i[0]+(t[0]-i[0])*a,i[1]+(t[1]-i[1])*s];return t.length>2?[...o,(i[2]??0)+(n(t)-(i[2]??0))*c]:o})}export function affine(t,n){return mapPositions(t,t=>{const o=[n[0]*t[0]+n[1]*t[1]+n[2],n[3]*t[0]+n[4]*t[1]+n[5]];return t.length>2?[...o,t[2]]:o})}export function applyMatrix(t,o){return mapPositions(t,t=>{const e=t[0],r=t[1],i=n(t),a=o[12]*e+o[13]*r+o[14]*i+o[15]||1;return[(o[0]*e+o[1]*r+o[2]*i+o[3])/a,(o[4]*e+o[5]*r+o[6]*i+o[7])/a,(o[8]*e+o[9]*r+o[10]*i+o[11])/a]})}export function withZ(t,n){return mapPositions(t,t=>[t[0],t[1],"number"==typeof n?n:n(t)])}export function dropZ(t){return mapPositions(t,t=>[t[0],t[1]])}export function explode(t){if(!t)return[];switch(t.type){case"MultiPoint":return t.coordinates.map(t=>({type:"Point",coordinates:t}));case"MultiLineString":return t.coordinates.map(t=>({type:"LineString",coordinates:t}));case"MultiPolygon":return t.coordinates.map(t=>({type:"Polygon",coordinates:t}));default:return[t]}}export function combine(t){if(0===t.length)return null;const n=t=>t.type.replace("Multi",""),o=n(t[0]);if(!t.every(t=>n(t)===o))return null;const e=t.flatMap(t=>explode(t));return"Point"===o?{type:"MultiPoint",coordinates:e.map(t=>t.coordinates)}:"LineString"===o?{type:"MultiLineString",coordinates:e.map(t=>t.coordinates)}:{type:"MultiPolygon",coordinates:e.map(t=>t.coordinates)}}
@@ -0,0 +1,19 @@
1
+ import type { Position } from '../data/Geometry';
2
+ /**
3
+ * A polygon's triangles: its rings - the outer first, holes after - cut into triangles by earcut,
4
+ * in the plane the polygon lies in. A flat polygon is cut in x and y as it is; one that stands
5
+ * or leans (a wall, a roof's slope) is first laid into its own plane, found by Newell's method,
6
+ * so a vertical polygon cuts as well as a level one.
7
+ *
8
+ * Returns the polygon's points flattened (x, y, z each, rings in order, closing points dropped)
9
+ * and the triangles as indices into them.
10
+ */
11
+ export declare function triangulate(rings: readonly (readonly Position[])[]): {
12
+ positions: number[];
13
+ triangles: number[];
14
+ ringStarts: number[];
15
+ };
16
+ /** The number of points of a ring, without the closing point that repeats the first. */
17
+ export declare function openLength(ring: readonly Position[]): number;
18
+ /** Twice the signed area of a ring in x and y: positive counter-clockwise. */
19
+ export declare function signedArea(ring: readonly Position[]): number;
@@ -0,0 +1 @@
1
+ import t from"earcut";const n=t.default??t;export function triangulate(t){const e=[],o=[];for(const n of t){const t=openLength(n);if(t<3){if(0===o.length)return{positions:[],triangles:[],ringStarts:[]}}else{o.push(e.length/3);for(let o=0;o<t;o++){const t=n[o];e.push(t[0],t[1],t[2]??0)}}}if(0===o.length)return{positions:e,triangles:[],ringStarts:o};const r=function(t){const n=t.length/3;let e=0,o=0,r=0;for(let s=0;s<n;s++){const i=(s+1)%n,[l,c,f]=[t[3*s],t[3*s+1],t[3*s+2]],[g,a,h]=[t[3*i],t[3*i+1],t[3*i+2]];e+=(c-a)*(f+h),o+=(f-h)*(l+g),r+=(l-g)*(c+a)}const s=Math.hypot(e,o,r),i=new Array(2*n);if(s<1e-12||Math.abs(r)/s>.5){for(let e=0;e<n;e++)i[2*e]=t[3*e],i[2*e+1]=t[3*e+1];return i}e/=s,o/=s,r/=s;let l=-o,c=e,f=0;const g=Math.hypot(l,c,f)||1;l/=g,c/=g;const a=o*f-r*c,h=r*l-e*f,u=e*c-o*l;for(let e=0;e<n;e++){const[n,o,r]=[t[3*e],t[3*e+1],t[3*e+2]];i[2*e]=n*l+o*c+r*f,i[2*e+1]=n*a+o*h+r*u}return i}(e),s=o.slice(1);return{positions:e,triangles:n(r,s.length>0?s:void 0,2),ringStarts:o}}export function openLength(t){let n=t.length;if(n>1){const e=t[0],o=t[n-1];e[0]===o[0]&&e[1]===o[1]&&(e[2]??0)===(o[2]??0)&&n--}return n}export function signedArea(t){const n=openLength(t);let e=0;for(let o=0,r=n-1;o<n;r=o++)e+=(t[r][0]-t[o][0])*(t[r][1]+t[o][1]);return e}