@protomaze/geometry 0.0.0-stage → 0.1.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.
Files changed (118) hide show
  1. package/GeometryDemo.d.ts +1 -0
  2. package/GeometryDemo.js +1 -0
  3. package/README.md +294 -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 +257 -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
package/demo/Town.d.ts ADDED
@@ -0,0 +1,115 @@
1
+ import { GeometryFeature } from '../data/Geometry';
2
+ /**
3
+ * A made-up town for the demo, the same every time for a seed: an organic street grid, a gravity
4
+ * sewer flowing to an outfall by the river - deeper and wider downstream, its manholes' shafts,
5
+ * every building's lateral - water, gas and power under the pavements, buildings, flood zones,
6
+ * sensors, trees, a sewer robot's run and a drone's survey flight. Positions in UTM-like metres,
7
+ * far from zero, as real data is: the engine's origin keeps them exact on the GPU.
8
+ */
9
+ export declare const ORIGIN_X = 356000;
10
+ export declare const ORIGIN_Y = 5645000;
11
+ export declare const MATERIALS: readonly ["Steinzeug", "Beton", "Stahlbeton", "Mauerwerk", "PVC", "PP", "PE-HD", "GFK", "Guss"];
12
+ export type Material = typeof MATERIALS[number];
13
+ export declare const MATERIAL_COLORS: Record<Material, string>;
14
+ export declare const USES: readonly ["Residential", "Commercial", "Industrial", "Public"];
15
+ export type Use = typeof USES[number];
16
+ export interface PipeProps {
17
+ kind: 'sewer' | 'lateral' | 'water' | 'gas' | 'power';
18
+ name: string;
19
+ material: Material;
20
+ year: number;
21
+ /** Nominal diameter, mm; for a box its width. */
22
+ dn: number;
23
+ /** Height, mm, for a profile that is not round. */
24
+ height?: number;
25
+ /** 0 good to 5 critical. */
26
+ condition: number;
27
+ /** Invert depth below the street at the two ends, m. */
28
+ depthFrom: number;
29
+ depthTo: number;
30
+ length: number;
31
+ slope: number;
32
+ upstream?: string;
33
+ downstream?: string;
34
+ }
35
+ export interface ManholeProps {
36
+ kind: 'manhole';
37
+ name: string;
38
+ depth: number;
39
+ diameter: number;
40
+ year: number;
41
+ material: Material;
42
+ condition: number;
43
+ }
44
+ export interface BuildingProps {
45
+ kind: 'building';
46
+ name: string;
47
+ use: Use;
48
+ height: number;
49
+ floors: number;
50
+ year: number;
51
+ }
52
+ export interface ZoneProps {
53
+ kind: 'zone';
54
+ name: string;
55
+ risk: 'High' | 'Medium' | 'Low' | 'Works';
56
+ }
57
+ export interface SensorProps {
58
+ kind: 'sensor';
59
+ name: string;
60
+ type: 'Level' | 'Flow' | 'Quality';
61
+ /** Fill or load, 0 to 100 %. */
62
+ value: number;
63
+ }
64
+ export interface Town {
65
+ bounds: [number, number, number, number];
66
+ sewer: GeometryFeature<PipeProps>[];
67
+ manholes: GeometryFeature<ManholeProps>[];
68
+ laterals: GeometryFeature<PipeProps>[];
69
+ connections: GeometryFeature<{
70
+ kind: 'connection';
71
+ name: string;
72
+ pipe: string;
73
+ }>[];
74
+ water: GeometryFeature<PipeProps>[];
75
+ gas: GeometryFeature<PipeProps>[];
76
+ power: GeometryFeature<PipeProps>[];
77
+ buildings: GeometryFeature<BuildingProps>[];
78
+ zones: GeometryFeature<ZoneProps>[];
79
+ sensors: GeometryFeature<SensorProps>[];
80
+ trees: GeometryFeature<{
81
+ kind: 'tree';
82
+ name: string;
83
+ crown: number;
84
+ height: number;
85
+ }>[];
86
+ defects: GeometryFeature<{
87
+ kind: 'defect';
88
+ name: string;
89
+ severity: number;
90
+ }>[];
91
+ trajectories: GeometryFeature<{
92
+ kind: 'trajectory';
93
+ name: string;
94
+ vehicle: 'Robot' | 'Drone';
95
+ }>[];
96
+ streets: {
97
+ a: [number, number];
98
+ b: [number, number];
99
+ width: number;
100
+ }[];
101
+ parks: [number, number][][];
102
+ river: [number, number][];
103
+ /** Manhole radius by its point, for the tubes' joints. */
104
+ stats: {
105
+ pipes: number;
106
+ manholes: number;
107
+ laterals: number;
108
+ buildings: number;
109
+ metres: number;
110
+ };
111
+ }
112
+ /** A town `scale` times as wide and high as the smallest. */
113
+ export declare function createTown(scale?: number, seed?: number): Town;
114
+ /** The town's ground as a map: blocks, pavements, streets with their lanes, parks and the river. */
115
+ export declare function paintGround(town: Town, dark: boolean, resolution?: number): HTMLCanvasElement;
package/demo/Town.js ADDED
@@ -0,0 +1 @@
1
+ export const ORIGIN_X=356e3;export const ORIGIN_Y=5645e3;export const MATERIALS=["Steinzeug","Beton","Stahlbeton","Mauerwerk","PVC","PP","PE-HD","GFK","Guss"];export const MATERIAL_COLORS={Steinzeug:"#e07a3f",Beton:"#9aa5b1",Stahlbeton:"#6c7a89",Mauerwerk:"#c0504d",PVC:"#4cc9f0",PP:"#80ed99","PE-HD":"#b388eb",GFK:"#f9c74f",Guss:"#577590"};export const USES=["Residential","Commercial","Industrial","Public"];const e=[200,250,300,400,500,600,700,800,1e3,1200,1400,1600];export function createTown(t=1,o=11){const n=function(e){let t=e>>>0||1;return()=>(t=Math.imul(t,1664525)+1013904223>>>0,t/4294967296)}(o),r=e=>e[Math.floor(n()*e.length)],i=Math.round(11*t)+1,a=Math.round(8*t)+1,s=132*(i-1),h=118*(a-1),l=356e3-s/2,c=5645e3-h/2,d=[356e3+.06*s,5645e3+.08*h],p=(e,o)=>Math.exp(-((e-d[0])**2+(o-d[1])**2)/(2*(380*Math.sqrt(t))**2)),u=[],g=(e,t)=>t*i+e;for(let e=0;e<a;e++)for(let t=0;t<i;t++){const o=132*t,r=118*e,s=26*Math.sin(r/410+.7)+6*Math.sin(r/170),h=22*Math.sin(o/520+1.3)+5*Math.cos(o/210),d=0===t||0===e||t===i-1||e===a-1;u.push({x:l+o+s+(d?0:18*(n()-.5)),y:c+r+h+(d?0:18*(n()-.5)),dist:1/0,catchment:0,id:u.length,edges:[],invert:0,big:!1})}const m=[],f=(e,t,o)=>{const n=Math.hypot(u[t].x-u[e].x,u[t].y-u[e].y),r=m.push({a:e,b:t,length:n,avenue:o,removed:!1})-1;u[e].edges.push(r),u[t].edges.push(r)};for(let e=0;e<a;e++)for(let t=0;t<i-1;t++)f(g(t,e),g(t+1,e),e%4==2);for(let e=0;e<i;e++)for(let t=0;t<a-1;t++)f(g(e,t),g(e,t+1),e%4==1);m.forEach(e=>{const t=[u[e.a],u[e.b]].some(e=>e.x<=l+40||e.y<=c+40||e.x>=l+s-40||e.y>=c+h-40);!e.avenue&&!t&&n()<.07&&(e.removed=!0)});const y=m.filter(e=>!e.removed),M=u.map((e,t)=>({x:e.x,y:e.y,dist:1/0,edges:[],invert:0,catchment:0,big:!1,name:`S${t}`})),b=[];for(const e of y){const t=u[e.a],o=u[e.b],n=Math.max(1,Math.ceil(e.length/55));let r=e.a;for(let i=1;i<=n;i++){let a;if(i===n)a=e.b;else{const e=i/n;a=M.push({x:t.x+(o.x-t.x)*e,y:t.y+(o.y-t.y)*e,dist:1/0,edges:[],invert:0,catchment:0,big:!1,name:`S${M.length}`})-1}const s=Math.hypot(M[a].x-M[r].x,M[a].y-M[r].y),h=b.push({a:r,b:a,length:s,avenue:e.avenue})-1;M[r].edges.push(h),M[a].edges.push(h),r=a}}let x=0;M.forEach((e,t)=>{e.x-l+1.6*(e.y-c)<M[x].x-l+1.6*(M[x].y-c)&&(x=t)}),M[x].dist=0;const v=new Map,k=[x],P=(e,t)=>M[k[e]].dist<M[k[t]].dist,w=(e,t)=>{const o=k[e];k[e]=k[t],k[t]=o},S=e=>{k.push(e);let t=k.length-1;for(;t>0&&P(t,t-1>>1);)w(t,t-1>>1),t=t-1>>1},$=()=>{const e=k[0],t=k.pop();if(k.length>0){k[0]=t;let e=0;for(;;){const t=2*e+1,o=t+1;let n=e;if(t<k.length&&P(t,n)&&(n=t),o<k.length&&P(o,n)&&(n=o),n===e)break;w(e,n),e=n}}return e},E=new Set;for(;k.length>0;){const e=$();if(!E.has(e)){E.add(e);for(const t of M[e].edges){const o=b[t],n=o.a===e?o.b:o.a,r=M[e].dist+o.length;r<M[n].dist&&(M[n].dist=r,v.set(n,e),S(n))}}}const C=Math.max(...M.map(e=>Number.isFinite(e.dist)?e.dist:0)),T=M.map((e,t)=>t).filter(e=>Number.isFinite(M[e].dist)).sort((e,t)=>M[t].dist-M[e].dist);for(const e of T){const t=v.get(e);if(void 0===t)continue;const o=M[e].edges.reduce((e,t)=>e+b[t].length/2,0);M[e].catchment+=o,M[t].catchment+=M[e].catchment}M.forEach(e=>{e.invert=1.7+.0042*(C-(Number.isFinite(e.dist)?e.dist:C))+.25*n()});const z=(e,t)=>{const o=p(e,t),r=2024-Math.round(105*o+35*n()+(1-o)*n()*40);return Math.max(1895,Math.min(2024,r))},F=e=>Math.max(0,Math.min(5,Math.round((2024-e)/28+2.2*(n()-.4)))),L=[],I=[];let G=0;b.forEach((t,o)=>{let n=M[t.a],i=M[t.b];n.dist<i.dist&&([n,i]=[i,n]);const a=Math.max(n.catchment,40),s=180+46*Math.sqrt(a/60),h=e.find(e=>e>=s)??1600,l=h>=1e3?Math.round(1.5*h):void 0;h>=800&&(n.big=!0,i.big=!0);const c=z((n.x+i.x)/2,(n.y+i.y)/2),d=((e,t)=>e<1930?t>=800?"Mauerwerk":"Steinzeug":e<1970?t>=600?"Beton":r(["Steinzeug","Steinzeug","Beton"]):e<1995?t>=800?"Stahlbeton":r(["Beton","Steinzeug","PVC"]):r(t>=800?["GFK","Stahlbeton"]:["PVC","PP","PE-HD","PVC"]))(c,h),p=(l??h)/2e3,u=[n.x,n.y,-n.invert+p],g=[i.x,i.y,-Math.max(i.invert,n.invert+.02)+p],m=`K${1e4+o}`;G+=t.length,L.push({id:m,geometry:{type:"LineString",coordinates:[u,g]},properties:{kind:"sewer",name:`Sewer ${m}`,material:d,year:c,dn:h,height:l,condition:F(c),depthFrom:n.invert,depthTo:Math.max(i.invert,n.invert+.02),length:t.length,slope:(Math.max(i.invert,n.invert+.02)-n.invert)/t.length*1e3,upstream:n.name,downstream:i.name}}),I.push({a:u,b:g,dn:h,id:m})});const D=M.map((e,t)=>{const o=z(e.x,e.y);return{id:e.name,geometry:{type:"Point",coordinates:[e.x,e.y,0]},properties:{kind:"manhole",name:`Manhole ${e.name}`,depth:e.invert+.3,diameter:e.big?1.5:t===x?2:1,year:o,material:o<1960?"Mauerwerk":"Beton",condition:F(o)}}}),R=[],H=[],V=[],B=[],W=[],N=(e,t,o)=>{const[n,r,i,a]=e;return[(1-t)*(1-o)*n[0]+t*(1-o)*r[0]+t*o*i[0]+(1-t)*o*a[0],(1-t)*(1-o)*n[1]+t*(1-o)*r[1]+t*o*i[1]+(1-t)*o*a[1]]},A=new Map;I.forEach((e,t)=>{const o=Math.ceil(Math.hypot(e.b[0]-e.a[0],e.b[1]-e.a[1])/30);for(let n=0;n<=o;n++){const r=n/o,i=`${Math.floor((e.a[0]+(e.b[0]-e.a[0])*r)/60)},${Math.floor((e.a[1]+(e.b[1]-e.a[1])*r)/60)}`,a=A.get(i)??[];a.includes(t)||a.push(t),A.set(i,a)}});const K=(e,t)=>{let o=null;const n=Math.floor(e/60),r=Math.floor(t/60);for(let i=-2;i<=2;i++)for(let a=-2;a<=2;a++)for(const s of A.get(`${n+i},${r+a}`)??[]){const n=I[s],r=n.b[0]-n.a[0],i=n.b[1]-n.a[1],a=Math.max(.08,Math.min(.92,((e-n.a[0])*r+(t-n.a[1])*i)/(r*r+i*i))),h=Math.hypot(n.a[0]+r*a-e,n.a[1]+i*a-t);(!o||h<o.d)&&(o={i:s,t:a,d:h})}return o};let O=0;for(let e=0;e<a-1;e++)for(let t=0;t<i-1;t++){const o=[u[g(t,e)],u[g(t+1,e)],u[g(t+1,e+1)],u[g(t,e+1)]],r=13,i=o.reduce((e,t)=>e+t.x,0)/4,a=o.reduce((e,t)=>e+t.y,0)/4,d=o.map(e=>{const t=i-e.x,o=a-e.y,n=Math.hypot(t,o);return[e.x+t/n*r*1.41,e.y+o/n*r*1.41]}),m=p(i,a);if(n()<.07&&m<.6){H.push(d);for(let e=0;e<22;e++){const[e,t]=N(d,.08+.84*n(),.08+.84*n());V.push(Q(e,t))}continue}const f=i<l+.3*s&&a>c+.62*h,y=f?2:4+Math.floor(3*n()),M=f?.42:.24+.06*n(),b=(e,t,o,r,i)=>{const a=.012,s=[N(d,e+a,t+a),N(d,o-a,t+a),N(d,o-a,r-a),N(d,e+a,r-a)],h=Math.max(1,Math.round(("Industrial"===i?2:3+3*n())*(1+m*(2.4+3*n())))),l=h*("Industrial"===i?5.5:3.2)+2*n(),c=z((s[0][0]+s[2][0])/2,(s[0][1]+s[2][1])/2),p="B"+ ++O;R.push({id:p,geometry:{type:"Polygon",coordinates:[[...s,s[0]]]},properties:{kind:"building",name:`${i} building ${p}`,use:i,height:l,floors:h,year:c}});const u=(s[0][0]+s[2][0])/2,g=(s[0][1]+s[2][1])/2,f=K(u,g);if(!f||f.d>70)return;const y=I[f.i],M=y.a[0]+(y.b[0]-y.a[0])*f.t,b=y.a[1]+(y.b[1]-y.a[1])*f.t,x=y.a[2]+(y.b[2]-y.a[2])*f.t+y.dn/2e3*.6,v=Math.hypot(M-u,b-g)||1,k=Math.min(.42*v,9),P=[u+(M-u)/v*k,g+(b-g)/v*k,-1.1-.3*n()],w=[M-(M-u)/v*2.2,b-(b-g)/v*2.2,Math.max(x+.35,-1.6-.02*v)],S=Math.max(c,1900+Math.floor(20*n())),$=`L${B.length+1}`;B.push({id:$,geometry:{type:"LineString",coordinates:[P,w,[M,b,x]]},properties:{kind:"lateral",name:`Lateral ${$} of ${p}`,material:S<1970?"Steinzeug":"PVC",year:S,dn:150,condition:F(S),depthFrom:-P[2],depthTo:-x,length:v,slope:20,downstream:y.id}}),W.push({id:`C${W.length+1}`,geometry:{type:"Point",coordinates:[M,b,x]},properties:{kind:"connection",name:`Connection of ${$}`,pipe:y.id}})},x=()=>f?"Industrial":m>.5&&n()<.55?"Commercial":n()<.06?"Public":"Residential";for(let e=0;e<y;e++){const t=e/y,o=(e+1)/y;b(t,0,o,M,x()),b(t,1-M,o,1,x())}if(!f)if(b(0,M,.9*M,1-M,x()),b(1-.9*M,M,1,1-M,x()),m>.72&&n()<.6)b(.36,.38,.64,.62,"Commercial");else for(let e=0;e<3;e++){const[e,t]=N(d,.35+.3*n(),.35+.3*n());V.push(Q(e,t))}}function Q(e,t){const o=2.2+2.2*n(),r=5+6*n();return{id:`T${V.length+1}`,geometry:{type:"Point",coordinates:[e,t,r]},properties:{kind:"tree",name:`Tree ${V.length+1}`,crown:o,height:r}}}y.filter(e=>e.avenue).forEach(e=>{const t=u[e.a],o=u[e.b],n=e.length,r=-(o.y-t.y)/n,i=(o.x-t.x)/n;for(let e=14;e<n-14;e+=14)for(const a of[-1,1])V.push(Q(t.x+(o.x-t.x)*e/n+8.5*r*a,t.y+(o.y-t.y)*e/n+8.5*i*a))});const Z=(e,t,o,i)=>y.filter(i).map((i,a)=>{const s=u[i.a],h=u[i.b],l=i.length,c=(h.x-s.x)/l,d=(h.y-s.y)/l,p=Math.abs(t),g=(e,r)=>[e.x-d*t+c*p*r,e.y+c*t+d*p*r,-o-.15*(n()-.5)],m=z((s.x+h.x)/2,(s.y+h.y)/2),f="power"===e?400:"water"===e?i.avenue?300:r([100,150,150,200]):i.avenue?200:r([80,100,125]),y="water"===e?m<1975?"Guss":"PE-HD":"gas"===e?m<1980?"Guss":"PE-HD":"PVC";return{id:`${e[0].toUpperCase()}${a+1}`,geometry:{type:"LineString",coordinates:[g(s,-1),g(h,1)]},properties:{kind:e,name:`${"power"===e?"Power duct":"water"===e?"Water main":"Gas main"} ${a+1}`,material:y,year:m,dn:f,height:"power"===e?250:void 0,condition:F(m),depthFrom:o,depthTo:o,length:l,slope:0}}}),j=Z("water",4.2,1.35,()=>!0),_=Z("gas",-4.2,.95,()=>!0),q=Z("power",-6.4,.75,e=>e.avenue||n()<.35),U=(e,t,o,r,i)=>{const a=[],s=6*n();for(let n=0;n<48;n++){const h=n/48*Math.PI*2,l=1+Math.sin(3*h+s)*i+Math.sin(5*h+2*s)*i*.5;a.push([e+Math.cos(h)*o*l,t+Math.sin(h)*r*l,0])}return a.push(a[0]),a},J=[{id:"Z1",geometry:{type:"Polygon",coordinates:[U(l+.22*s,c+.16*h,.2*s,.13*h,.12)]},properties:{kind:"zone",name:"Flood zone HQ10",risk:"High"}},{id:"Z2",geometry:{type:"Polygon",coordinates:[U(l+.3*s,c+.2*h,.3*s,.2*h,.1)]},properties:{kind:"zone",name:"Flood zone HQ100",risk:"Medium"}},{id:"Z3",geometry:{type:"Polygon",coordinates:[U(l+.38*s,c+.25*h,.42*s,.28*h,.08)]},properties:{kind:"zone",name:"Flood zone HQextreme",risk:"Low"}},{id:"Z4",geometry:{type:"Polygon",coordinates:[U(d[0]+260,d[1]-120,70,50,.05)]},properties:{kind:"zone",name:"Works: main relining",risk:"Works"}}],X=[];M.forEach((e,t)=>{if(n()>.05&&t!==x)return;const o=r(["Level","Flow","Quality"]);X.push({id:`M${X.length+1}`,geometry:{type:"Point",coordinates:[e.x,e.y,0]},properties:{kind:"sensor",name:`${o} sensor ${X.length+1}`,type:o,value:Math.round(100*Math.pow(n(),.8))}})});const Y=[];L.forEach(e=>{const t=Math.floor(e.properties.condition*e.properties.length/35*n()),[o,r]=e.geometry.coordinates;for(let e=0;e<t;e++){const e=n();Y.push({id:`D${Y.length+1}`,geometry:{type:"Point",coordinates:[o[0]+(r[0]-o[0])*e,o[1]+(r[1]-o[1])*e,(o[2]??0)+((r[2]??0)-(o[2]??0))*e]},properties:{kind:"defect",name:`Defect ${Y.length+1}`,severity:1+Math.floor(5*n())}})}});const ee=[];let te=T.find(e=>M[e].dist>.7*C&&p(M[e].x,M[e].y)>.2)??T[0];const oe=[];for(let e=0;e<40&&void 0!==te;e++){const e=M[te];oe.push([e.x,e.y,.12-e.invert]);const t=v.get(te);if(void 0===t)break;te=t}oe.length>1&&ee.push({id:"R1",geometry:{type:"LineString",coordinates:oe},properties:{kind:"trajectory",name:"CCTV robot run",vehicle:"Robot"}});const ne=[],re=d[0]-420,ie=d[1]-300;for(let e=0;e<7;e++){const t=ie+95*e,o=e%2==0;for(let n=0;n<=24;n++){const r=o?n/24:1-n/24;ne.push([re+840*r,t,72+4*Math.sin(n/3+e)])}if(e<6){const e=o?re+840:re;for(let n=1;n<8;n++){const r=n/8*Math.PI;ne.push([e+(o?1:-1)*Math.sin(r)*47,t+47.5-47.5*Math.cos(r),72])}}}ee.push({id:"F1",geometry:{type:"LineString",coordinates:ne},properties:{kind:"trajectory",name:"Survey flight",vehicle:"Drone"}});const ae=[];for(let e=0;e<=40;e++){const t=l-300+e/40*(s+600);ae.push([t,c-70+22*Math.sin(e/5)])}return{bounds:[l-200,c-200,l+s+200,c+h+200],sewer:L,manholes:D,laterals:B,connections:W,water:j,gas:_,power:q,buildings:R,zones:J,sensors:X,trees:V,defects:Y,trajectories:ee,streets:y.map(e=>({a:[u[e.a].x,u[e.a].y],b:[u[e.b].x,u[e.b].y],width:e.avenue?22:14})),parks:H,river:ae,stats:{pipes:L.length,manholes:D.length,laterals:B.length,buildings:R.length,metres:G}}}export function paintGround(e,t,o=.6){const[n,r,i,a]=e.bounds,s=Math.min(1/o,4096/(i-n),4096/(a-r)),h=document.createElement("canvas");h.width=Math.ceil((i-n)*s),h.height=Math.ceil((a-r)*s);const l=h.getContext("2d"),c=e=>(e-n)*s,d=e=>(a-e)*s,p=t?{ground:"#151a20",block:"#181e25",pavement:"#232a33",street:"#2b333d",lane:"#3b4552",park:"#15261c",river:"#0d2233",riverEdge:"#163650"}:{ground:"#eef0ea",block:"#f6f6f2",pavement:"#e1e3df",street:"#d3d6d9",lane:"#ffffff",park:"#d6ead2",river:"#bfdcf0",riverEdge:"#9cc7e6"};l.fillStyle=p.ground,l.fillRect(0,0,h.width,h.height),l.lineCap="round",l.lineJoin="round",l.beginPath(),e.river.forEach(([e,t],o)=>0===o?l.moveTo(c(e),d(t)):l.lineTo(c(e),d(t))),l.strokeStyle=p.riverEdge,l.lineWidth=64*s,l.stroke(),l.strokeStyle=p.river,l.lineWidth=56*s,l.stroke(),l.fillStyle=p.park,e.parks.forEach(e=>{l.beginPath(),e.forEach(([e,t],o)=>0===o?l.moveTo(c(e),d(t)):l.lineTo(c(e),d(t))),l.closePath(),l.fill()});const u=(t,o,n)=>{l.strokeStyle=t,l.setLineDash(n??[]),e.streets.forEach(e=>{l.lineWidth=Math.max(1,o(e)*s),l.beginPath(),l.moveTo(c(e.a[0]),d(e.a[1])),l.lineTo(c(e.b[0]),d(e.b[1])),l.stroke()})};return u(p.pavement,e=>e.width+7),u(p.street,e=>e.width),u(p.lane,()=>.3,[3*s,6*s]),l.setLineDash([]),l.fillStyle=t?"#1e252e":"#e4e4de",e.buildings.forEach(e=>{const t=e.geometry.coordinates[0];l.beginPath(),t.forEach(([e,t],o)=>0===o?l.moveTo(c(e),d(t)):l.lineTo(c(e),d(t))),l.fill()}),h}
@@ -0,0 +1,101 @@
1
+ import type { Geometry, GeometryFeature } from '../data/Geometry';
2
+ import type { GeometryLayer } from '../layers/GeometryLayer';
3
+ import type { Vec3 } from '../math/Vector';
4
+ import type { EditHistory } from './EditHistory';
5
+ import { ControllerHost, ControllerPointer, GeometryController } from './GeometryController';
6
+ import { SketchPoint, SketchTheme } from './Sketch';
7
+ import { SnapOptions, SnapResult } from './Snapper';
8
+ /** What is drawn: a point, a line, a polygon, a rectangle along the axes, a circle. */
9
+ export type CreateShape = 'point' | 'line' | 'polygon' | 'rectangle' | 'circle';
10
+ export interface CreateControllerOptions<F extends GeometryFeature<any> = GeometryFeature> {
11
+ shape: CreateShape;
12
+ /** Where finished shapes go, as features; none to only hear of them (`finish`). */
13
+ layer?: GeometryLayer<F, any>;
14
+ /** The feature for a finished shape: its id and properties. A new id and no properties by default. */
15
+ feature?: (geometry: Geometry) => F;
16
+ /** Snapping to other features, the grid, angles; false for none. */
17
+ snapping?: SnapOptions | false;
18
+ /** The height drawn at where nothing is snapped to: the camera target's by default. */
19
+ z?: number;
20
+ /** A circle's sides. 64 by default. */
21
+ segments?: number;
22
+ /** Lengths and areas written beside the shape as it is drawn. On by default. */
23
+ measure?: boolean;
24
+ /** After a shape, ready for the next; on by default. */
25
+ continuous?: boolean;
26
+ /** Records each shape added to the layer, to undo. */
27
+ history?: EditHistory;
28
+ /** Why a shape cannot be finished, or null: a polygon whose edges cross, by default. */
29
+ validate?: (geometry: Geometry) => string | null;
30
+ theme?: Partial<SketchTheme>;
31
+ }
32
+ /** The shape as it is being drawn, the pointer's position included. */
33
+ export interface CreateChange {
34
+ shape: CreateShape;
35
+ geometry: Geometry | null;
36
+ /** The points placed so far. */
37
+ points: Vec3[];
38
+ length?: number;
39
+ area?: number;
40
+ /** Why it cannot be finished as it is. */
41
+ problem?: string | null;
42
+ }
43
+ export interface CreateControllerEvents<F> {
44
+ change: CreateChange;
45
+ finish: {
46
+ geometry: Geometry;
47
+ feature: F | null;
48
+ };
49
+ cancel: void;
50
+ }
51
+ /**
52
+ * Draws new geometry on the view: click to place each point, the line to the pointer drawn as
53
+ * it moves; double-click, Enter, or a click on the first point (a polygon) or the last (a line)
54
+ * finishes it. Backspace takes the last point back, Escape lets the shape go. Shift turns the
55
+ * way from the last point to whole steps of 15°. Dragging still pans.
56
+ *
57
+ * Points snap to what is near on screen - other features' vertices and edges at whatever depth -
58
+ * and the shape's lengths, area and radius are written beside it as it grows. A finished shape
59
+ * goes into `layer` as a feature, undoable through `history`.
60
+ *
61
+ * ```ts
62
+ * const draw = new CreateController({ shape: 'polygon', layer: parcels });
63
+ * view.setController(draw);
64
+ * draw.on('finish', ({ feature }) => save(feature));
65
+ * ```
66
+ */
67
+ export declare class CreateController<F extends GeometryFeature<any> = GeometryFeature> extends GeometryController<CreateControllerEvents<F>> {
68
+ private options;
69
+ private readonly sketch;
70
+ private snapper;
71
+ private points;
72
+ private hover;
73
+ private problem;
74
+ constructor(options: CreateControllerOptions<F>);
75
+ get shape(): CreateShape;
76
+ /** Draws another kind of shape from now on; the one begun is let go. */
77
+ setShape(shape: CreateShape): void;
78
+ setOptions(options: Partial<CreateControllerOptions<F>>): void;
79
+ /** The points placed so far. */
80
+ getPoints(): readonly Vec3[];
81
+ protected onAttach(host: ControllerHost): void;
82
+ protected onDetach(host: ControllerHost): void;
83
+ onPointerMove(event: ControllerPointer): boolean;
84
+ onPointerLeave(): void;
85
+ onClick(event: ControllerPointer): boolean;
86
+ onDoubleClick(event: ControllerPointer): boolean;
87
+ onKeyDown(event: KeyboardEvent): boolean;
88
+ /** Lets the shape begun go. */
89
+ cancel(): void;
90
+ /** Finishes the shape as it is, if it can be: a feature in the layer, and `finish`. False if it cannot. */
91
+ finish(): boolean;
92
+ private reset;
93
+ private locate;
94
+ /** The shape the points make, the pointer's point as the last where `withHover`. */
95
+ private geometryOf;
96
+ private check;
97
+ /** The sketch and its labels as the shape stands, with the pointer's point. */
98
+ private render;
99
+ }
100
+ /** How the pointer's point shows: what it snapped to. */
101
+ export declare function snapRole(snap: SnapResult): SketchPoint['role'];
@@ -0,0 +1 @@
1
+ import{formatArea as t,formatLength as s}from"../engine/Labels.js";import{distance as e,pathLength as i,pointOnSurface as n,area as o}from"../math/Measure.js";import{selfIntersections as h}from"../math/Lines.js";import{circle as r,orientPolygon as p,rectangle as l}from"../math/Polygons.js";import{GeometryController as a}from"./GeometryController.js";import{Sketch as c,SKETCH_THEME as u}from"./Sketch.js";import{Snapper as g}from"./Snapper.js";let d=0;export class CreateController extends a{constructor(t){super(),this.options=t,this.snapper=null,this.points=[],this.hover=null,this.problem=null,this.sketch=new c({...u,...t.theme}),this.cursor="crosshair"}get shape(){return this.options.shape}setShape(t){this.options={...this.options,shape:t},this.reset()}setOptions(t){this.options={...this.options,...t},this.snapper&&(this.snapper.options=!1===t.snapping?{}:t.snapping??this.snapper.options),this.render()}getPoints(){return this.points}onAttach(t){this.sketch.attach(t),!1!==this.options.snapping&&(this.snapper=new g(t,this.options.snapping??{}),this.snapper.warm()),this.render()}onDetach(t){this.sketch.detach(),this.snapper?.dispose(),this.snapper=null,t.labels?.clear("create"),this.points=[],this.hover=null}onPointerMove(t){return this.hover=this.locate(t),this.render(),!0}onPointerLeave(){this.hover=null,this.render()}onClick(t){if(0!==t.button)return!1;const s=this.locate(t);if(!s)return!0;this.hover=s;const e=this.options.shape;if("point"===e)return this.points=[s.point],this.finish(),!0;if("rectangle"===e||"circle"===e)return this.points.push(s.point),this.points.length>=2?this.finish():this.render(),!0;if("self"===s.kind){if(("polygon"===e?this.points.length>=3:this.points.length>=2)&&("polygon"===e&&0===s.own||s.own===this.points.length-1))return this.finish(),!0}const i=this.points[this.points.length-1],n=i?this.host?.toScreen(i):null,o=this.host?.toScreen(s.point);return n&&o&&Math.hypot(n[0]-o[0],n[1]-o[1])<3||(this.points.push(s.point),this.render()),!0}onDoubleClick(t){const s=this.options.shape;if("line"!==s&&"polygon"!==s)return this.points.length>0;const e=this.points.length;if(e>=2){const t=this.host?.toScreen(this.points[e-1]),s=this.host?.toScreen(this.points[e-2]);t&&s&&Math.hypot(t[0]-s[0],t[1]-s[1])<6&&this.points.pop()}return this.finish(),!0}onKeyDown(t){return"Escape"===t.key?0!==this.points.length&&(this.cancel(),!0):"Enter"===t.key?(this.finish(),!0):!("Backspace"!==t.key&&"Delete"!==t.key&&(!t.ctrlKey&&!t.metaKey||"z"!==t.key.toLowerCase()))&&(0!==this.points.length&&(this.points.pop(),this.render(),!0))}cancel(){const t=this.points.length>0;this.reset(),t&&this.emit("cancel")}finish(){const t=this.geometryOf(this.points);if(!t)return!1;const s=this.check(t);if(s)return this.problem=s,this.render(),!1;let e=null;const i=this.options.layer;return i&&(e=this.options.feature?.(t)??{id:`drawn-${Date.now().toString(36)}-${++d}`,geometry:t,properties:{}},i.addFeatures([e]),this.options.history?.push(`Draw ${this.options.shape}`,[{layer:i,before:null,after:e}])),this.emit("finish",{geometry:t,feature:e}),this.reset(),!0}reset(){this.points=[],this.problem=null,this.render()}locate(t){const s=this.host;if(!s)return null;const e=this.options.z??s.camera.state.target[2],i=this.options.shape,n="polygon"===i?this.points.length>=3?[this.points[0],this.points[this.points.length-1]]:[]:"line"===i&&this.points.length>=2?[this.points[this.points.length-1]]:[],o=this.points[this.points.length-1]??null;if(this.snapper){const s=this.snapper.snap(t.x,t.y,t.shift,{z:e,own:n,from:o});return"self"===s?.kind&&void 0!==s.own&&(s.own=n[s.own]===this.points[0]?0:this.points.length-1),s||null}const h=s.toGround(t.x,t.y,e);return h?{point:h,kind:"none"}:null}geometryOf(t){const s=this.options.shape,e=t.length;if("point"===s)return e>=1?{type:"Point",coordinates:t[0]}:null;if("line"===s)return e>=2?{type:"LineString",coordinates:t.map(t=>[...t])}:null;if("polygon"===s)return e>=3?{type:"Polygon",coordinates:p([t.map(t=>[...t])])}:null;if("rectangle"===s)return e<2||t[0][0]===t[1][0]||t[0][1]===t[1][1]?null:l(t[0],t[1]);if(e<2)return null;const i=Math.hypot(t[1][0]-t[0][0],t[1][1]-t[0][1]);return i>0?r(t[0],i,this.options.segments??64):null}check(t){return"Polygon"!==t.type||o(t)>0?"LineString"!==t.type||i(t.coordinates)>0?this.options.validate?this.options.validate(t):"Polygon"===t.type&&h(t.coordinates[0],!0).length>0?"Edges cross":null:"A line needs length":"An area needs room inside"}render(){const r=this.host;if(!r)return;const p=this.options.shape,l=this.hover,a=l?[...this.points,l.point]:[...this.points],c={fills:[],lines:[],points:[]},u=[],g=!1!==this.options.measure;let d,m;const y=this.geometryOf(a),k="polygon"===p&&a.length>=4?h([...a,a[0]],!0):[],v=this.problem??(k.length>0?"Edges cross":null);if("line"===p||"polygon"===p){if("polygon"===p&&a.length>=3&&c.fills.push({rings:[[...a,a[0]]],role:v?"invalid":"fill"}),this.points.length>=2&&c.lines.push({coordinates:this.points,role:v?"invalid":"edge"}),l&&this.points.length>=1&&(c.lines.push({coordinates:[this.points[this.points.length-1],l.point],role:"rubber"}),"polygon"===p&&this.points.length>=2&&c.lines.push({coordinates:[l.point,this.points[0]],role:"guide"})),this.points.forEach(t=>c.points.push({position:t,role:"vertex"})),g){for(let t=1;t<a.length;t++){const i=a[t-1],n=a[t],o=e(i,n);o>0&&u.push({id:`s${t}`,position:f(i,n),text:s(o),kind:"segment"})}if(d=i(a),"polygon"===p&&y){m=o(y);const s=n(y);s&&u.push({id:"area",position:s,text:t(m),kind:"measure"})}else l&&this.points.length>=2&&u.push({id:"total",position:l.point,text:s(d),kind:"measure",offset:[0,-22]})}}else if("rectangle"===p&&a.length>=1){const[e,i]=[a[0],a[1]??a[0]],n=[[e[0],e[1],e[2]],[i[0],e[1],e[2]],[i[0],i[1],e[2]],[e[0],i[1],e[2]]];if(a.length>=2&&(c.fills.push({rings:[[...n,n[0]]],role:"fill"}),c.lines.push({coordinates:[...n,n[0]],role:"edge"}),g)){const o=Math.abs(i[0]-e[0]),h=Math.abs(i[1]-e[1]);m=o*h,u.push({id:"w",position:f(n[0],n[1]),text:s(o),kind:"segment"}),u.push({id:"h",position:f(n[1],n[2]),text:s(h),kind:"segment"}),u.push({id:"area",position:f(e,i),text:t(m),kind:"measure"})}c.points.push({position:e,role:"vertex"})}else if("circle"===p&&a.length>=1){const e=a[0];if(c.points.push({position:e,role:"center"}),a.length>=2&&y){const i=y.coordinates[0];if(c.fills.push({rings:[i],role:"fill"}),c.lines.push({coordinates:i,role:"edge"}),c.lines.push({coordinates:[e,a[1]],role:"guide"}),g){const i=Math.hypot(a[1][0]-e[0],a[1][1]-e[1]);m=Math.PI*i*i,u.push({id:"r",position:f(e,a[1]),text:`r ${s(i)}`,kind:"segment"}),u.push({id:"area",position:e,text:t(m),kind:"measure",offset:[0,20]})}}}if(l&&c.points.push({position:l.point,role:snapRole(l)}),v){const t=k[0]?.point??a[a.length-1];t&&u.push({id:"problem",position:t,text:v,kind:"warning",offset:[0,22]})}this.sketch.set(c),r.labels?.set("create",u),r.requestRepaint(),this.emit("change",{shape:p,geometry:y,points:[...this.points],length:d,area:m,problem:v})}}function f(t,s){return[(t[0]+s[0])/2,(t[1]+s[1])/2,((t[2]??0)+(s[2]??0))/2]}export function snapRole(t){switch(t.kind){case"vertex":case"self":return"snap-vertex";case"edge":return"snap-edge";case"grid":case"angle":return"snap-other";default:return"active"}}
@@ -0,0 +1,121 @@
1
+ import type { Geometry, GeometryFeature, GeometryId } from '../data/Geometry';
2
+ import type { GeometryLayer } from '../layers/GeometryLayer';
3
+ import { EditHistory } from './EditHistory';
4
+ import { ControllerHost, ControllerPointer, GeometryController } from './GeometryController';
5
+ import { SketchTheme } from './Sketch';
6
+ import { SnapOptions } from './Snapper';
7
+ export interface EditControllerOptions {
8
+ /** The layers whose features may be edited: a click on one starts editing it. */
9
+ layers: readonly GeometryLayer<any, any>[];
10
+ /** Snapping of moved vertices to other features, the grid, angles; false for none. */
11
+ snapping?: SnapOptions | false;
12
+ /** Undo and redo; one of its own by default. Share one with a `CreateController` to undo both in order. */
13
+ history?: EditHistory;
14
+ /** Lengths, areas, angles written beside the shape. On by default. */
15
+ measure?: boolean;
16
+ /** In 3D, Alt-dragging a vertex moves it up and down. On by default. */
17
+ heights?: boolean;
18
+ /** Why a geometry cannot be, or null: a ring whose edges cross, by default. An edit that makes one is undone. */
19
+ validate?: (geometry: Geometry, feature: GeometryFeature) => string | null;
20
+ /** Whether a click picks the feature to edit. On by default; `edit()` starts one either way. */
21
+ clickToEdit?: boolean;
22
+ theme?: Partial<SketchTheme>;
23
+ }
24
+ /** What is being edited: the layer, the feature as it stands, its geometry as it is being changed. */
25
+ export interface EditTarget {
26
+ layer: GeometryLayer<any, any>;
27
+ feature: GeometryFeature;
28
+ geometry: Geometry;
29
+ }
30
+ export interface EditControllerEvents {
31
+ /** The feature edited now, or null for none. */
32
+ select: EditTarget | null;
33
+ /** The geometry while it is dragged, before it is committed. */
34
+ change: EditTarget;
35
+ /** An edit put into the layer: the feature before and after. */
36
+ commit: {
37
+ layer: GeometryLayer<any, any>;
38
+ before: GeometryFeature;
39
+ after: GeometryFeature;
40
+ label: string;
41
+ };
42
+ /** A feature deleted from its layer. */
43
+ delete: {
44
+ layer: GeometryLayer<any, any>;
45
+ feature: GeometryFeature;
46
+ };
47
+ /** An edit refused: why. */
48
+ problem: {
49
+ message: string;
50
+ };
51
+ mode: 'vertices' | 'transform';
52
+ }
53
+ /**
54
+ * Edits features in their layers: click one to edit it - its shape is drawn over it with a handle
55
+ * on every vertex; while it is dragged, it fades in its layer under the shape it is becoming.
56
+ *
57
+ * - **Vertices**: drag one to move it, snapping to other features; Alt-drag (3D) to raise or
58
+ * lower it; drag an edge's middle to pull out a new one; double-click a vertex - or select it
59
+ * and press Delete - to take it away; double-click an edge to add one there.
60
+ * - **The whole**: drag the shape itself to move it; press T for the transform handles, to
61
+ * scale it from its corners or turn it round its middle (Shift: 15° steps).
62
+ * - Delete with no vertex selected deletes the feature; Ctrl+Z and Ctrl+Y undo and redo;
63
+ * Escape stops editing.
64
+ *
65
+ * Every drag changes the shape on screen only; letting go commits it to the layer
66
+ * (`updateFeatures`: the geometry in place, the shaders kept) and to the history.
67
+ */
68
+ export declare class EditController extends GeometryController<EditControllerEvents> {
69
+ private options;
70
+ readonly history: EditHistory;
71
+ private readonly sketch;
72
+ private snapper;
73
+ private target;
74
+ private mode;
75
+ private hovered;
76
+ private overBody;
77
+ private selectedVertex;
78
+ private drag;
79
+ private problem;
80
+ private offHistory;
81
+ constructor(options: EditControllerOptions);
82
+ setOptions(options: Partial<EditControllerOptions>): void;
83
+ /** The feature edited now. */
84
+ getTarget(): EditTarget | null;
85
+ getMode(): "transform" | "vertices";
86
+ /** Vertex handles, or the transform's: scale from the corners, turn round the middle. */
87
+ setMode(mode: 'vertices' | 'transform'): void;
88
+ /** Starts editing a feature of a layer; the one edited before is let go. */
89
+ edit(layer: GeometryLayer<any, any>, id: GeometryId): boolean;
90
+ /** Stops editing: the feature is drawn as its layer draws it again. */
91
+ stop(): void;
92
+ /** Deletes the feature edited from its layer, to undo like any edit. */
93
+ deleteFeature(): boolean;
94
+ undo(): boolean;
95
+ redo(): boolean;
96
+ protected onAttach(host: ControllerHost): void;
97
+ protected onDetach(host: ControllerHost): void;
98
+ onPointerMove(event: ControllerPointer): boolean;
99
+ onPointerDown(event: ControllerPointer): boolean;
100
+ onPointerDrag(event: ControllerPointer): void;
101
+ onPointerUp(): void;
102
+ onClick(event: ControllerPointer): boolean;
103
+ onDoubleClick(event: ControllerPointer): boolean;
104
+ onKeyDown(event: KeyboardEvent): boolean;
105
+ onPointerLeave(): void;
106
+ private commit;
107
+ private refuse;
108
+ private check;
109
+ private handles;
110
+ private handleAt;
111
+ /** Whether a view point is on the shape itself: inside an area, on a line, on a point. */
112
+ private bodyAt;
113
+ /** The nearest point on the shape's edges to a view point, on screen, and the index a vertex there would get. */
114
+ private edgeAt;
115
+ /** The height under a view point on the upright plane through `point` that faces the camera. */
116
+ private heightAt;
117
+ private centerOf;
118
+ /** The shape's box across the map, at its mean height: corners, middle, and where the rotate handle goes. */
119
+ private boxOf;
120
+ private render;
121
+ }
@@ -0,0 +1 @@
1
+ import{formatAngle as t,formatArea as e,formatLength as s}from"../engine/Labels.js";import{closestOnSegment as i,selfIntersections as n}from"../math/Lines.js";import{area as r,distance as o,pathLength as h,pointOnSurface as a}from"../math/Measure.js";import{pointInPolygon as d}from"../math/Polygons.js";import{snapRole as l}from"./CreateController.js";import{EditHistory as p}from"./EditHistory.js";import{cloneEditShape as c,fillsOf as u,fromEditShape as f,midpoints as m,orientRings as g,outlines as y,removeVertex as x,toEditShape as k,vertices as v}from"./EditShape.js";import{GeometryController as b}from"./GeometryController.js";import{Sketch as M,SKETCH_THEME as P}from"./Sketch.js";import{Snapper as A}from"./Snapper.js";export class EditController extends b{constructor(t){super(),this.options=t,this.snapper=null,this.target=null,this.mode="vertices",this.hovered=null,this.overBody=!1,this.selectedVertex=null,this.drag=null,this.problem=null,this.offHistory=null,this.history=t.history??new p,this.sketch=new M({...P,...t.theme})}setOptions(t){this.options={...this.options,...t},this.snapper&&void 0!==t.snapping&&(this.snapper.options=!1===t.snapping?{vertices:!1,edges:!1}:t.snapping),this.render()}getTarget(){const t=this.target;return t?{layer:t.layer,feature:t.feature,geometry:f(t.shape)}:null}getMode(){return this.mode}setMode(t){t!==this.mode&&(this.mode=t,this.selectedVertex=null,this.render(),this.emit("mode",t))}edit(t,e){const s=t.getFeature(e);return!!s?.geometry&&(this.target?.layer===t&&this.target.feature.id===e||(this.stop(),this.target={layer:t,feature:s,shape:k(s.geometry)},this.render(),this.emit("select",this.getTarget())),!0)}stop(){const t=this.target;t&&(t.layer.setOverride([t.feature.id],null),this.target=null,this.drag=null,this.selectedVertex=null,this.hovered=null,this.problem=null,this.render(),this.emit("select",null))}deleteFeature(){const t=this.target;return!!t&&(this.stop(),t.layer.removeFeatures([t.feature.id]),this.history.push("Delete feature",[{layer:t.layer,before:t.feature,after:null}]),this.emit("delete",{layer:t.layer,feature:t.feature}),!0)}undo(){return null!==this.history.undo()}redo(){return null!==this.history.redo()}onAttach(t){this.sketch.attach(t),!1!==this.options.snapping&&(this.snapper=new A(t,this.options.snapping??{}),this.snapper.warm()),this.offHistory=this.history.on("apply",({step:t})=>{const e=this.target;if(!e)return;if(!t.records.find(t=>t.layer===e.layer&&(t.before?.id??t.after?.id)===e.feature.id))return;const s=e.layer.getFeature(e.feature.id);s?.geometry?(e.feature=s,e.shape=k(s.geometry),this.selectedVertex=null,this.render()):this.stop()}),this.render()}onDetach(t){this.stop(),this.offHistory?.(),this.offHistory=null,this.sketch.detach(),this.snapper?.dispose(),this.snapper=null,t.labels?.clear("edit")}onPointerMove(t){if(!this.target)return this.cursor=null,!1;const e=this.handleAt(t.x,t.y),s=!e&&this.bodyAt(t.x,t.y);return e?.kind===this.hovered?.kind&&e?.path===this.hovered?.path&&e?.index===this.hovered?.index&&s===this.overBody||(this.hovered=e,this.overBody=s,this.render()),this.cursor=e?"rotate"===e.kind?"grab":"pointer":s?"move":null,!!e||s}onPointerDown(t){const e=this.target,s=this.host;if(!e||!s||0!==t.button)return!1;const i=this.handleAt(t.x,t.y),n=c(e.shape),r=this.centerOf(e.shape);if(i){let{path:o,index:h}=i,a="vertex",d="Move vertex";return"mid"===i.kind?(e.shape.paths[o].points.splice(h,0,[...i.point]),d="Add vertex"):"vertex"===i.kind&&t.alt&&!1!==this.options.heights&&"3d"===s.mode?(a="height",d="Change height"):"scale"===i.kind?(a="scale",d="Scale"):"rotate"===i.kind&&(a="rotate",d="Rotate"),"scale"!==i.kind&&"rotate"!==i.kind||(o=-1,h=-1),this.drag={kind:a,path:o,index:h,start:"mid"===i.kind?c(e.shape):n,before:n,startPoint:[...i.point],center:r,moved:"mid"===i.kind,snap:null,label:d},"vertex"!==i.kind&&"mid"!==i.kind||(this.selectedVertex={path:o,index:h}),this.cursor="rotate"===a?"grabbing":"move",this.render(),!0}if(this.bodyAt(t.x,t.y)){const e=s.toGround(t.x,t.y,r[2]);return!!e&&(this.drag={kind:"move",path:-1,index:-1,start:n,before:n,startPoint:e,center:r,moved:!1,snap:null,label:"Move"},this.cursor="move",!0)}return!1}onPointerDrag(e){const s=this.drag,i=this.target,n=this.host;if(!s||!i||!n)return;s.moved&&"vertex"!==s.kind&&"height"!==s.kind||i.layer.setOverride([i.feature.id],"faded"),s.moved=!0;const r=c(s.start);if("vertex"===s.kind){const t=r.paths[s.path],o=t.points[s.index],h=t.points.length>1?[t.points[(s.index+t.points.length-1)%t.points.length]]:[],a=this.snapper?.snap(e.x,e.y,e.shift,{z:o[2],from:h[0]??null,exclude:{layer:i.layer,id:i.feature.id}})??(()=>{const t=n.toGround(e.x,e.y,o[2]);return t?{point:t,kind:"none"}:null})();a&&(t.points[s.index]=[a.point[0],a.point[1],"vertex"===a.kind||"edge"===a.kind?a.point[2]:o[2]],"vertex"!==a.kind&&"edge"!==a.kind||(r.has3D=r.has3D||0!==a.point[2])),s.snap=a}else if("height"===s.kind){const t=r.paths[s.path].points[s.index],i=this.heightAt(e.x,e.y,t);null!==i&&(t[2]=i,r.has3D=!0)}else{const i=n.toGround(e.x,e.y,s.center[2]);if(!i)return;const o=s.center;let h;if("move"===s.kind){const t=i[0]-s.startPoint[0],e=i[1]-s.startPoint[1];h=s=>[s[0]+t,s[1]+e,s[2]]}else if("rotate"===s.kind){let n=Math.atan2(i[1]-o[1],i[0]-o[0])-Math.atan2(s.startPoint[1]-o[1],s.startPoint[0]-o[0]);e.shift&&(n=Math.round(n/(Math.PI/12))*(Math.PI/12));const r=Math.cos(n),a=Math.sin(n);h=t=>[o[0]+(t[0]-o[0])*r-(t[1]-o[1])*a,o[1]+(t[0]-o[0])*a+(t[1]-o[1])*r,t[2]],s.label=`Rotate ${t(180*n/Math.PI)}`}else{const t=Math.hypot(s.startPoint[0]-o[0],s.startPoint[1]-o[1]),e=t>0?Math.hypot(i[0]-o[0],i[1]-o[1])/t:1;h=t=>[o[0]+(t[0]-o[0])*e,o[1]+(t[1]-o[1])*e,t[2]],s.label=`Scale × ${e.toFixed(2)}`}r.paths.forEach(t=>{t.points=t.points.map(h)})}i.shape=r,this.problem=null,this.render(),this.emit("change",this.getTarget())}onPointerUp(){const t=this.drag;this.drag=null,t&&this.target&&(this.target.layer.setOverride([this.target.feature.id],null),t.moved?this.commit("move"===t.kind?"Move":"rotate"===t.kind?"Rotate":"scale"===t.kind?"Scale":t.label,t.before):this.render())}onClick(t){const e=this.host;return e&&0===t.button&&!1!==this.options.clickToEdit?(e.pickAt(t.x,t.y).then(t=>{t&&this.options.layers.includes(t.layer)?this.edit(t.layer,t.id):this.target&&this.stop()}),!0):!!this.target}onDoubleClick(t){const e=this.target;if(!e||"vertices"!==this.mode)return!1;const s=this.handleAt(t.x,t.y),i=c(e.shape);if("vertex"===s?.kind)return x(e.shape,s.path,s.index)&&(this.selectedVertex=null,this.commit("Delete vertex",i)),!0;const n=this.edgeAt(t.x,t.y);return!!n&&(e.shape.paths[n.path].points.splice(n.index,0,n.point),this.selectedVertex={path:n.path,index:n.index},this.commit("Add vertex",i),!0)}onKeyDown(t){const e=t.ctrlKey||t.metaKey,s=t.key.toLowerCase();if(e&&"z"===s)return t.shiftKey?this.redo():this.undo();if(e&&"y"===s)return this.redo();const i=this.target;if(!i)return!1;if("Escape"===t.key)return this.drag?(i.shape=this.drag.before,this.drag=null,i.layer.setOverride([i.feature.id],null),this.render()):this.stop(),!0;if("Delete"===t.key||"Backspace"===t.key){if(this.selectedVertex&&"vertices"===this.mode){const t=c(i.shape);return x(i.shape,this.selectedVertex.path,this.selectedVertex.index)?(this.selectedVertex=null,this.commit("Delete vertex",t)):this.refuse("A shape needs more vertices than that",i.shape.paths[this.selectedVertex.path].points[0]),!0}return this.deleteFeature()}return"t"===s&&!e&&(this.setMode("vertices"===this.mode?"transform":"vertices"),!0)}onPointerLeave(){(this.hovered||this.overBody)&&(this.hovered=null,this.overBody=!1,this.render())}commit(t,e){const s=this.target;if(!s)return;g(s.shape);const i=f(s.shape),n=this.check(i,s.feature);if(n)return s.shape=e,void this.refuse(n.message,n.at);const r=s.feature,o={...r,geometry:i};s.layer.updateFeatures([o]),s.feature=o,s.shape=k(i),this.history.push(t,[{layer:s.layer,before:r,after:o}]),this.render(),this.emit("commit",{layer:s.layer,before:r,after:o,label:t})}refuse(t,e){this.problem={message:t,at:e},this.render(),this.emit("problem",{message:t}),window.setTimeout(()=>{this.problem?.message===t&&(this.problem=null,this.render())},2200)}check(t,e){const s=function(t){let e=[0,0];switch(t.type){case"Point":e=t.coordinates;break;case"MultiPoint":case"LineString":e=t.coordinates[0];break;case"MultiLineString":case"Polygon":e=t.coordinates[0][0];break;case"MultiPolygon":e=t.coordinates[0][0][0]}return[e[0],e[1],e[2]??0]}(t);if(this.options.validate){const i=this.options.validate(t,e);return i?{message:i,at:s}:null}if("Polygon"===t.type||"MultiPolygon"===t.type){const e="Polygon"===t.type?[t.coordinates]:t.coordinates;for(const t of e)for(const e of t){const t=n(e,!0)[0];if(t)return{message:"Edges cross",at:t.point}}}return null}handles(){const t=this.target,e=this.host;if(!t||!e)return[];if("transform"===this.mode){const e=this.boxOf(t.shape),s=e.corners.map((t,e)=>({kind:"scale",path:-1,index:e,point:t}));return s.push({kind:"rotate",path:-1,index:0,point:e.rotate}),s}const s=v(t.shape).map(t=>({kind:"vertex",...t}));for(const i of m(t.shape)){const n=t.shape.paths[i.path],r=e.toScreen(n.points[i.index-1]),o=e.toScreen(n.points[i.index%n.points.length]);r&&o&&Math.hypot(r[0]-o[0],r[1]-o[1])>36&&s.push({kind:"mid",...i})}return s}handleAt(t,e){const s=this.host;if(!s)return null;let i=null,n=1/0;for(const r of this.handles()){const o=s.toScreen(r.point);if(!o)continue;const h=Math.hypot(o[0]-t,o[1]-e),a="mid"===r.kind?7:9,d=h+("mid"===r.kind?4:0);h<=a&&d<n&&(n=d,i=r)}return i}bodyAt(t,e){const s=this.target,i=this.host;if(!s||!i)return!1;if(this.edgeAt(t,e,7))return!0;const n=this.centerOf(s.shape),r=i.toGround(t,e,n[2]);if(r&&u(s.shape).some(t=>d(r,t)))return!0;for(const n of s.shape.paths){if("point"!==n.kind)continue;const s=i.toScreen(n.points[0]);if(s&&Math.hypot(s[0]-t,s[1]-e)<=11)return!0}return!1}edgeAt(t,e,s=7){const n=this.target,r=this.host;if(!n||!r)return null;let o=null,h=s;return n.shape.paths.forEach((s,n)=>{if("point"===s.kind)return;const a=s.points.length,d="ring"===s.kind?a:a-1;for(let l=0;l<d;l++){const d=s.points[l],p=s.points[(l+1)%a],c=r.toScreen(d),u=r.toScreen(p);if(!c||!u)continue;const f=i([t,e],c,u,2);if(f.distance<h){h=f.distance;const t=f.t;o={path:n,index:l+1,point:[d[0]+(p[0]-d[0])*t,d[1]+(p[1]-d[1])*t,d[2]+(p[2]-d[2])*t]}}}}),o}heightAt(t,e,s){const i=this.host;if(!i)return null;const{origin:n,direction:r}=i.rayAt(t,e),{f:o}=i.camera.basis();let h=o[0],a=o[1];const d=Math.hypot(h,a);if(d<1e-6)return null;h/=d,a/=d;const l=h*r[0]+a*r[1];if(Math.abs(l)<1e-6)return null;const p=(h*(s[0]-n[0])+a*(s[1]-n[1]))/l;return p>0?n[2]+r[2]*p:null}centerOf(t){return this.boxOf(t).center}boxOf(t){let e=1/0,s=1/0,i=-1/0,n=-1/0,r=0,o=0;t.paths.forEach(t=>t.points.forEach(t=>{e=Math.min(e,t[0]),s=Math.min(s,t[1]),i=Math.max(i,t[0]),n=Math.max(n,t[1]),r+=t[2],o++})),r=o>0?r/o:0;const h=[(e+i)/2,(s+n)/2,r],a=[[e,s,r],[i,s,r],[i,n,r],[e,n,r]],d=this.host?.pixelSizeAt(h)??1;return{center:h,corners:a,rotate:[h[0],n+32*d,r],top:[h[0],n,r]}}render(){const t=this.host;if(!t)return;const i=this.target,n=[];if(!i)return this.sketch.clear(),t.labels?.set("edit",n),void t.requestRepaint();const d=i.shape,p=this.drag,c=!!this.problem,m={fills:[],lines:[],points:[]};if(u(d).forEach(t=>m.fills.push({rings:t,role:c?"invalid":"fill"})),y(d).forEach(t=>m.lines.push({coordinates:t,role:c?"invalid":"edge"})),"transform"===this.mode){const t=this.boxOf(d);m.lines.push({coordinates:[...t.corners,t.corners[0]],role:"guide"}),m.lines.push({coordinates:[t.top,t.rotate],role:"guide"}),m.points.push({position:t.center,role:"center"})}const g=t=>this.selectedVertex&&"vertex"===t.kind&&t.path===this.selectedVertex.path&&t.index===this.selectedVertex.index||this.hovered&&t.kind===this.hovered.kind&&t.path===this.hovered.path&&t.index===this.hovered.index;for(const t of this.handles()){const e=g(t)?"active":"mid"===t.kind?"mid":"scale"===t.kind?"scale":"rotate"===t.kind?"rotate":d.type.includes("Point")?"point":"vertex";m.points.push({position:t.point,role:e})}if(p?.snap&&"none"!==p.snap.kind&&m.points.push({position:p.snap.point,role:l(p.snap)}),!1!==this.options.measure){const t=f(d),i=p&&p.path>=0?{path:p.path,index:p.index}:this.selectedVertex;if(i){const t=d.paths[i.path],e=t?.points.length??0;if(t&&"point"!==t.kind)for(const r of[i.index-1,i.index]){if(!("ring"===t.kind)&&(r<0||r+1>=e))continue;const i=t.points[(r+e)%e],h=t.points[(r+1)%e];n.push({id:`e${r}`,position:S(i,h),text:s(o(i,h)),kind:"segment"})}if("height"===p?.kind&&t){const e=t.points[i.index],s=p.start.paths[i.path].points[i.index][2],r=e[2]-s;n.push({id:"z",position:e,text:`z ${e[2].toFixed(2)} m (${r>=0?"+":""}${r.toFixed(2)})`,kind:"measure",offset:[0,-24]})}}if("Polygon"===t.type||"MultiPolygon"===t.type){const s=a(t);s&&n.push({id:"area",position:s,text:e(r(t)),kind:"measure"})}else if("LineString"===t.type||"MultiLineString"===t.type){const e="LineString"===t.type?[t.coordinates]:t.coordinates,i=e.reduce((t,e)=>t+h(e),0),r=e.reduce((t,e)=>h(e)>h(t)?e:t,e[0]);n.push({id:"length",position:V(r),text:s(i),kind:"measure",offset:[0,-18]})}if(p&&p.moved&&("move"===p.kind||"rotate"===p.kind||"scale"===p.kind)){const t=this.centerOf(d),e="move"===p.kind?`Δ ${s(Math.hypot(t[0]-p.center[0],t[1]-p.center[1]))}`:p.label.replace(/^(Rotate|Scale) /,"");n.push({id:"transform",position:t,text:e,kind:"info",offset:[0,24]})}}this.problem&&n.push({id:"problem",position:this.problem.at,text:this.problem.message,kind:"warning",offset:[0,22]}),this.sketch.set(m),t.labels?.set("edit",n),t.requestRepaint()}}function S(t,e){return[(t[0]+e[0])/2,(t[1]+e[1])/2,((t[2]??0)+(e[2]??0))/2]}function V(t){const e=h(t)/2;let s=0;for(let i=1;i<t.length;i++){const n=o(t[i-1],t[i]);if(s+n>=e&&n>0){const r=(e-s)/n,o=t[i-1],h=t[i];return[o[0]+(h[0]-o[0])*r,o[1]+(h[1]-o[1])*r,(o[2]??0)+((h[2]??0)-(o[2]??0))*r]}s+=n}const i=t[0];return[i[0],i[1],i[2]??0]}
@@ -0,0 +1,46 @@
1
+ import type { GeometryFeature } from '../data/Geometry';
2
+ import { TypedEventEmitter } from '../event/TypedEventEmitter';
3
+ import type { GeometryLayer } from '../layers/GeometryLayer';
4
+ /** One feature's change in one layer: added (no `before`), removed (no `after`), or changed. */
5
+ export interface EditRecord<F extends GeometryFeature<any> = GeometryFeature> {
6
+ layer: GeometryLayer<F, any>;
7
+ before: F | null;
8
+ after: F | null;
9
+ }
10
+ /** An edit as undone and redone: its changes together, and what to call it. */
11
+ export interface EditStep {
12
+ label: string;
13
+ records: EditRecord<any>[];
14
+ }
15
+ export interface EditHistoryEvents {
16
+ change: {
17
+ canUndo: boolean;
18
+ canRedo: boolean;
19
+ undoLabel: string | null;
20
+ redoLabel: string | null;
21
+ };
22
+ /** A step undone or redone: the features as they are again. */
23
+ apply: {
24
+ step: EditStep;
25
+ direction: 'undo' | 'redo';
26
+ };
27
+ }
28
+ /**
29
+ * Undo and redo for edits to layers' features: each step the features before and after, put
30
+ * back into their layers as they were (`updateFeatures`, `addFeatures`, `removeFeatures`).
31
+ * Controllers share one if given it, so drawing and editing undo in one order.
32
+ */
33
+ export declare class EditHistory extends TypedEventEmitter<EditHistoryEvents> {
34
+ readonly limit: number;
35
+ private done;
36
+ private undone;
37
+ constructor(limit?: number);
38
+ get canUndo(): boolean;
39
+ get canRedo(): boolean;
40
+ /** Records an edit already made to the layers. */
41
+ push(label: string, records: EditRecord<any>[]): void;
42
+ undo(): EditStep | null;
43
+ redo(): EditStep | null;
44
+ clear(): void;
45
+ private changed;
46
+ }
@@ -0,0 +1 @@
1
+ import{TypedEventEmitter as e}from"../event/TypedEventEmitter.js";export class EditHistory extends e{constructor(e=200){super(),this.limit=e,this.done=[],this.undone=[]}get canUndo(){return this.done.length>0}get canRedo(){return this.undone.length>0}push(e,t){0!==t.length&&(this.done.push({label:e,records:t}),this.done.length>this.limit&&this.done.shift(),this.undone=[],this.changed())}undo(){const e=this.done.pop();return e?([...e.records].reverse().forEach(e=>t(e.layer,e.after,e.before)),this.undone.push(e),this.changed(),this.emit("apply",{step:e,direction:"undo"}),e):null}redo(){const e=this.undone.pop();return e?(e.records.forEach(e=>t(e.layer,e.before,e.after)),this.done.push(e),this.changed(),this.emit("apply",{step:e,direction:"redo"}),e):null}clear(){this.done=[],this.undone=[],this.changed()}changed(){this.emit("change",{canUndo:this.canUndo,canRedo:this.canRedo,undoLabel:this.done[this.done.length-1]?.label??null,redoLabel:this.undone[this.undone.length-1]?.label??null})}}function t(e,t,n){n?e.updateFeatures([n]):t&&e.removeFeatures([t.id])}
@@ -0,0 +1,45 @@
1
+ import type { Geometry } from '../data/Geometry';
2
+ import type { Vec3 } from '../math/Vector';
3
+ /** One run of vertices of a shape being edited: a point, a line, or a ring (kept open, closed again on the way out). */
4
+ export interface EditPath {
5
+ kind: 'point' | 'line' | 'ring';
6
+ points: Vec3[];
7
+ /** The fewest vertices it may have: 1 for a point, 2 for a line, 3 for a ring. */
8
+ min: number;
9
+ /** For rings: which polygon, and whether it is that polygon's outer ring. */
10
+ polygon: number;
11
+ outer: boolean;
12
+ }
13
+ /** A geometry as runs of vertices to move, add and take away, and back. */
14
+ export interface EditShape {
15
+ type: Geometry['type'];
16
+ paths: EditPath[];
17
+ /** Whether its positions have heights: kept as they came, or given them by an edit in height. */
18
+ has3D: boolean;
19
+ }
20
+ /** A geometry taken apart for editing: every vertex a three-number copy. */
21
+ export declare function toEditShape(geometry: Geometry): EditShape;
22
+ /** The geometry again: rings closed, heights only where it had them. */
23
+ export declare function fromEditShape(shape: EditShape): Geometry;
24
+ /** A deep copy: edits to it leave the original alone. */
25
+ export declare function cloneEditShape(shape: EditShape): EditShape;
26
+ /** Every vertex, with its path and index: what handles are put on. */
27
+ export declare function vertices(shape: EditShape): {
28
+ path: number;
29
+ index: number;
30
+ point: Vec3;
31
+ }[];
32
+ /** Every edge's middle - where a vertex can be pulled out of it - with its path and the index the new vertex gets. */
33
+ export declare function midpoints(shape: EditShape): {
34
+ path: number;
35
+ index: number;
36
+ point: Vec3;
37
+ }[];
38
+ /** The lines a shape shows as: its lines, its rings closed. */
39
+ export declare function outlines(shape: EditShape): Vec3[][];
40
+ /** The polygons a shape fills: each outer ring with its holes. */
41
+ export declare function fillsOf(shape: EditShape): Vec3[][][];
42
+ /** Takes a vertex away where its path keeps enough; a point of a multi-point goes with its path. True if it went. */
43
+ export declare function removeVertex(shape: EditShape, path: number, index: number): boolean;
44
+ /** Rings turned the GeoJSON way: outer rings counter-clockwise, holes clockwise. */
45
+ export declare function orientRings(shape: EditShape): void;
@@ -0,0 +1 @@
1
+ import{ringArea as t}from"../math/Measure.js";import{isClosedLine as n}from"../math/Lines.js";export function toEditShape(t){let o=!1;const i=t=>(t.length>2&&(o=!0),[t[0],t[1],t[2]??0]),e=(t,o,e)=>({kind:"ring",points:(n(t)?t.slice(0,-1):t).map(i),min:3,polygon:o,outer:e}),s=[];switch(t.type){case"Point":s.push({kind:"point",points:[i(t.coordinates)],min:1,polygon:-1,outer:!1});break;case"MultiPoint":t.coordinates.forEach(t=>s.push({kind:"point",points:[i(t)],min:1,polygon:-1,outer:!1}));break;case"LineString":s.push({kind:"line",points:t.coordinates.map(i),min:2,polygon:-1,outer:!1});break;case"MultiLineString":t.coordinates.forEach(t=>s.push({kind:"line",points:t.map(i),min:2,polygon:-1,outer:!1}));break;case"Polygon":t.coordinates.forEach((t,n)=>s.push(e(t,0,0===n)));break;case"MultiPolygon":t.coordinates.forEach((t,n)=>t.forEach((t,o)=>s.push(e(t,n,0===o))))}return{type:t.type,paths:s,has3D:o}}export function fromEditShape(t){const n=n=>t.has3D?[n[0],n[1],n[2]]:[n[0],n[1]],o=t=>{const o=t.points.map(n);return o.length>0&&o.push(n(t.points[0])),o},i=t.paths;switch(t.type){case"Point":return{type:"Point",coordinates:n(i[0].points[0])};case"MultiPoint":return{type:"MultiPoint",coordinates:i.map(t=>n(t.points[0]))};case"LineString":return{type:"LineString",coordinates:i[0].points.map(n)};case"MultiLineString":return{type:"MultiLineString",coordinates:i.map(t=>t.points.map(n))};case"Polygon":return{type:"Polygon",coordinates:i.map(o)};case"MultiPolygon":{const t=[];for(const n of i)(n.outer||0===t.length)&&t.push([]),t[t.length-1].push(o(n));return{type:"MultiPolygon",coordinates:t}}}}export function cloneEditShape(t){return{...t,paths:t.paths.map(t=>({...t,points:t.points.map(t=>[...t])}))}}export function vertices(t){const n=[];return t.paths.forEach((t,o)=>t.points.forEach((t,i)=>n.push({path:o,index:i,point:t}))),n}export function midpoints(t){const n=[];return t.paths.forEach((t,o)=>{if("point"===t.kind)return;const i=t.points.length,e="ring"===t.kind?i:i-1;for(let s=0;s<e;s++){const e=t.points[s],r=t.points[(s+1)%i];n.push({path:o,index:s+1,point:[(e[0]+r[0])/2,(e[1]+r[1])/2,(e[2]+r[2])/2]})}}),n}export function outlines(t){return t.paths.filter(t=>"point"!==t.kind).map(t=>"ring"===t.kind&&t.points.length>0?[...t.points,t.points[0]]:t.points)}export function fillsOf(t){const n=[];for(const o of t.paths)"ring"===o.kind&&((o.outer||0===n.length)&&n.push([]),n[n.length-1].push([...o.points,o.points[0]]));return n}export function removeVertex(t,n,o){const i=t.paths[n];if(!i)return!1;if(i.points.length>i.min)return i.points.splice(o,1),!0;const e=t.paths.filter(t=>t.kind===i.kind).length;return("point"===i.kind&&e>1||"ring"===i.kind&&!i.outer)&&(t.paths.splice(n,1),!0)}export function orientRings(n){for(const o of n.paths){if("ring"!==o.kind||o.points.length<3)continue;const n=t(o.points);(o.outer&&n<0||!o.outer&&n>0)&&o.points.reverse()}}
@@ -0,0 +1,83 @@
1
+ import type { Position } from '../data/Geometry';
2
+ import type { GeometryHit } from '../engine/GeometryView';
3
+ import type { LabelOverlay } from '../engine/Labels';
4
+ import type { MapCamera } from '../engine/MapCamera';
5
+ import { TypedEventEmitter } from '../event/TypedEventEmitter';
6
+ import type { GeometryLayer } from '../layers/GeometryLayer';
7
+ import type { Vec3 } from '../math/Vector';
8
+ /** A pointer event as a controller gets it: where in the view, which button, which keys held. */
9
+ export interface ControllerPointer {
10
+ /** View pixels from the view's top-left. */
11
+ x: number;
12
+ y: number;
13
+ button: number;
14
+ shift: boolean;
15
+ ctrl: boolean;
16
+ alt: boolean;
17
+ pointerType: string;
18
+ native: PointerEvent | MouseEvent;
19
+ }
20
+ /**
21
+ * What a controller needs of the view it works in: its camera and layers, screen and world
22
+ * positions both ways, picking, overlay layers to draw in and labels to write. `GeometryView`
23
+ * is one; a map that hosts the engine can be another.
24
+ */
25
+ export interface ControllerHost {
26
+ readonly mode: '2d' | '3d';
27
+ readonly camera: MapCamera;
28
+ readonly labels: LabelOverlay | null;
29
+ readonly element: HTMLElement | null;
30
+ readonly viewSize: {
31
+ width: number;
32
+ height: number;
33
+ };
34
+ getLayers(): readonly GeometryLayer<any, any>[];
35
+ /** A layer drawn over everything else, not picked, not in the view's layers or bounds. */
36
+ addOverlay(layer: GeometryLayer<any, any>): void;
37
+ removeOverlay(layer: GeometryLayer<any, any>): void;
38
+ /** Where a world position is in the view, in view pixels; null behind the camera. */
39
+ toScreen(p: Position): [number, number] | null;
40
+ /** Where the ray through a view point meets the level `z`; null where it does not. */
41
+ toGround(x: number, y: number, z?: number): Vec3 | null;
42
+ rayAt(x: number, y: number): {
43
+ origin: Vec3;
44
+ direction: Vec3;
45
+ };
46
+ /** World units per view pixel at a position: a pixel tolerance in world units there. */
47
+ pixelSizeAt(p: Position): number;
48
+ pickAt(x: number, y: number): Promise<GeometryHit | null>;
49
+ requestRepaint(): void;
50
+ }
51
+ /**
52
+ * Something that takes over the pointer and keys of a view for a while - drawing new geometry,
53
+ * editing it, measuring - as LuciadRIA's controllers do on its map. The view offers it every
54
+ * event first; what it handles (returns true for) the view leaves alone, the rest - panning,
55
+ * zooming, hovering, selecting - goes on as usual.
56
+ *
57
+ * Install one with `view.setController(controller)`; `null` gives the view back its own ways.
58
+ */
59
+ export declare abstract class GeometryController<Events extends Record<string, any> = {}> extends TypedEventEmitter<Events> {
60
+ protected host: ControllerHost | null;
61
+ /** The cursor while this controller is in charge; null for the view's own. */
62
+ cursor: string | null;
63
+ get active(): boolean;
64
+ /** Called by the view as the controller is installed. */
65
+ attach(host: ControllerHost): void;
66
+ /** Called by the view as the controller is taken away. */
67
+ detach(): void;
68
+ protected onAttach(_host: ControllerHost): void;
69
+ protected onDetach(_host: ControllerHost): void;
70
+ /** A button pressed: true takes the drag that may follow (`onPointerDrag`, `onPointerUp`) from the view. */
71
+ onPointerDown(_event: ControllerPointer): boolean;
72
+ /** The pointer moving with no drag: true keeps the view from picking what is under it. */
73
+ onPointerMove(_event: ControllerPointer): boolean;
74
+ /** The pointer moving in a drag this controller took. */
75
+ onPointerDrag(_event: ControllerPointer): void;
76
+ /** The end of a drag this controller took. */
77
+ onPointerUp(_event: ControllerPointer): void;
78
+ /** A click - pressed and let go without moving - the controller did not take at `onPointerDown`: true keeps the view from selecting. */
79
+ onClick(_event: ControllerPointer): boolean;
80
+ onDoubleClick(_event: ControllerPointer): boolean;
81
+ onKeyDown(_event: KeyboardEvent): boolean;
82
+ onPointerLeave(): void;
83
+ }
@@ -0,0 +1 @@
1
+ import{TypedEventEmitter as t}from"../event/TypedEventEmitter.js";export class GeometryController extends t{constructor(){super(...arguments),this.host=null,this.cursor=null}get active(){return null!==this.host}attach(t){this.host=t,this.onAttach(t)}detach(){const t=this.host;t&&(this.onDetach(t),this.host=null)}onAttach(t){}onDetach(t){}onPointerDown(t){return!1}onPointerMove(t){return!1}onPointerDrag(t){}onPointerUp(t){}onClick(t){return!1}onDoubleClick(t){return!1}onKeyDown(t){return!1}onPointerLeave(){}}