@pirireis/webglobeplugins 0.17.0 → 1.0.2

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 (274) hide show
  1. package/Math/{angle-calculation.js → angle-calculation.ts} +18 -14
  2. package/Math/{arc-cdf-points.js → arc-cdf-points.ts} +329 -272
  3. package/Math/{arc-generate-points-exponantial.js → arc-generate-points-exponantial.ts} +299 -254
  4. package/Math/{arc.js → arc.ts} +421 -292
  5. package/Math/bounds/line-bbox.js +225 -186
  6. package/Math/{circle-cdf-points.js → circle-cdf-points.ts} +143 -78
  7. package/Math/{circle.js → circle.ts} +49 -33
  8. package/Math/{constants.js → constants.ts} +12 -4
  9. package/Math/contour/{quadtreecontours.js → quadtreecontours.ts} +371 -300
  10. package/Math/contour/quadtreecontours1.js +336 -298
  11. package/Math/{finite-line-2d.js → finite-line-2d.ts} +68 -58
  12. package/Math/haversine.ts +33 -0
  13. package/Math/index.js +0 -1
  14. package/Math/juction/{arc-plane.js → arc-plane.ts} +203 -143
  15. package/Math/juction/{line-sphere.js → line-sphere.ts} +32 -22
  16. package/Math/juction/{plane-plane.js → plane-plane.ts} +62 -53
  17. package/Math/{line.js → line.ts} +84 -52
  18. package/Math/matrix4.ts +0 -0
  19. package/Math/mesh/mapbox-delaunay.d.ts +74 -0
  20. package/Math/{methods.js → methods.ts} +182 -107
  21. package/Math/{plane.js → plane.ts} +92 -56
  22. package/Math/{quaternion.js → quaternion.ts} +128 -106
  23. package/Math/roadmap.md +10 -0
  24. package/Math/templete-shapes/{grid-visually-equal.js → grid-visually-equal.ts} +118 -65
  25. package/Math/tessellation/constants.ts +1 -0
  26. package/Math/tessellation/methods.ts +79 -0
  27. package/Math/tessellation/nearest-value-padding.ts +147 -0
  28. package/Math/tessellation/roadmap.md +48 -0
  29. package/Math/tessellation/spherical-triangle-area.ts +127 -0
  30. package/Math/tessellation/tile-merger.ts +578 -0
  31. package/Math/tessellation/triangle-tessellation.ts +533 -0
  32. package/Math/tessellation/types.ts +1 -0
  33. package/Math/types.ts +68 -0
  34. package/Math/utils.js +3 -2
  35. package/Math/{vec3.js → vec3.ts} +227 -151
  36. package/Math/xyz-tile.ts +26 -0
  37. package/algorithms/search-binary.js +14 -16
  38. package/altitude-locator/adaptors.js +0 -1
  39. package/altitude-locator/keymethod.js +0 -1
  40. package/altitude-locator/plugin.js +445 -345
  41. package/altitude-locator/types.js +26 -21
  42. package/compass-rose/compass-rose-padding-flat.js +274 -230
  43. package/compass-rose/{compass-text-writer.js → compass-text-writer.ts} +210 -155
  44. package/compass-rose/index.js +3 -3
  45. package/{constants.js → constants.ts} +8 -6
  46. package/heatwave/datamanager.js +168 -149
  47. package/heatwave/heatwave.js +261 -206
  48. package/heatwave/index.js +5 -5
  49. package/heatwave/isobar.js +340 -303
  50. package/heatwave/{texture-point-sampler.js → texture-point-sampler.ts} +220 -187
  51. package/investigation-tools/draw/tiles/adapters.ts +133 -0
  52. package/investigation-tools/draw/tiles/tiles.ts +162 -0
  53. package/jest.config.js +6 -7
  54. package/package.json +1 -1
  55. package/pin/pin-object-array1.js +381 -300
  56. package/pin/pin-point-totem1.js +77 -60
  57. package/programs/arrowfield/arrow-field.js +89 -60
  58. package/programs/arrowfield/logic.js +173 -141
  59. package/programs/data2legend/density-to-legend.js +86 -68
  60. package/programs/data2legend/point-to-density-texture.js +84 -67
  61. package/programs/float2legendwithratio/index.js +3 -2
  62. package/programs/float2legendwithratio/logic.js +144 -118
  63. package/programs/float2legendwithratio/object.js +141 -104
  64. package/programs/helpers/blender.js +73 -58
  65. package/programs/helpers/{fadeaway.js → fadeaway.ts} +73 -55
  66. package/programs/index.js +19 -20
  67. package/programs/line-on-globe/circle-accurate-3d.js +112 -85
  68. package/programs/line-on-globe/circle-accurate-flat.js +200 -148
  69. package/programs/line-on-globe/degree-padding-around-circle-3d.js +134 -102
  70. package/programs/line-on-globe/index.js +0 -1
  71. package/programs/line-on-globe/lines-color-instanced-flat.js +99 -80
  72. package/programs/line-on-globe/linestrip/data.ts +29 -0
  73. package/programs/line-on-globe/linestrip/{linestrip.js → linestrip.ts} +152 -93
  74. package/programs/line-on-globe/{naive-accurate-flexible.js → naive-accurate-flexible.ts} +175 -126
  75. package/programs/line-on-globe/util.js +8 -5
  76. package/programs/picking/pickable-polygon-renderer.js +129 -98
  77. package/programs/picking/pickable-renderer.js +130 -98
  78. package/programs/point-on-globe/element-globe-surface-glow.js +122 -93
  79. package/programs/point-on-globe/element-point-glow.js +114 -80
  80. package/programs/point-on-globe/square-pixel-point.js +139 -120
  81. package/programs/polygon-on-globe/roadmap.md +8 -0
  82. package/programs/polygon-on-globe/texture-dem-triangles.ts +290 -0
  83. package/programs/{programcache.js → programcache.ts} +134 -126
  84. package/programs/rings/index.js +1 -1
  85. package/programs/rings/partial-ring/{piece-of-pie.js → piece-of-pie.ts} +222 -152
  86. package/programs/totems/camera-totem-attactment-interface.ts +4 -0
  87. package/programs/totems/{camerauniformblock.js → camerauniformblock.ts} +326 -230
  88. package/programs/totems/{canvas-webglobe-info.js → canvas-webglobe-info.ts} +147 -132
  89. package/programs/totems/dem-textures-manager.ts +368 -0
  90. package/programs/totems/{globe-changes.js → globe-changes.ts} +79 -59
  91. package/programs/totems/gpu-selection-uniform-block.js +127 -99
  92. package/programs/totems/{index.js → index.ts} +2 -2
  93. package/programs/two-d/pixel-padding-for-compass.js +101 -87
  94. package/programs/util.js +19 -14
  95. package/programs/vectorfields/logics/{constants.js → constants.ts} +5 -4
  96. package/programs/vectorfields/logics/drawrectangleparticles.ts +182 -0
  97. package/programs/vectorfields/logics/index.js +4 -2
  98. package/programs/vectorfields/logics/particle-ubo.ts +23 -0
  99. package/programs/vectorfields/logics/{pixelbased.js → pixelbased.ts} +119 -84
  100. package/programs/vectorfields/logics/ubo.js +57 -51
  101. package/programs/vectorfields/{pingpongbuffermanager.js → pingpongbuffermanager.ts} +113 -73
  102. package/publish.bat +62 -0
  103. package/range-tools-on-terrain/bearing-line/{adapters.js → adapters.ts} +154 -114
  104. package/range-tools-on-terrain/bearing-line/{plugin.js → plugin.ts} +569 -457
  105. package/range-tools-on-terrain/bearing-line/types.ts +65 -0
  106. package/range-tools-on-terrain/circle-line-chain/{adapters.js → adapters.ts} +104 -85
  107. package/range-tools-on-terrain/circle-line-chain/{chain-list-map.js → chain-list-map.ts} +446 -382
  108. package/range-tools-on-terrain/circle-line-chain/{plugin.js → plugin.ts} +607 -464
  109. package/range-tools-on-terrain/circle-line-chain/types.ts +43 -0
  110. package/range-tools-on-terrain/range-ring/{adapters.js → adapters.ts} +114 -93
  111. package/range-tools-on-terrain/range-ring/{enum.js → enum.ts} +2 -2
  112. package/range-tools-on-terrain/range-ring/{plugin.js → plugin.ts} +444 -377
  113. package/range-tools-on-terrain/range-ring/rangeringangletext.ts +396 -0
  114. package/range-tools-on-terrain/range-ring/types.ts +30 -0
  115. package/semiplugins/interface.ts +14 -0
  116. package/semiplugins/lightweight/{line-plugin.js → line-plugin.ts} +342 -221
  117. package/semiplugins/lightweight/{piece-of-pie-plugin.js → piece-of-pie-plugin.ts} +275 -200
  118. package/semiplugins/shape-on-terrain/{arc-plugin.js → arc-plugin.ts} +616 -472
  119. package/semiplugins/shape-on-terrain/{circle-plugin.js → circle-plugin.ts} +588 -444
  120. package/semiplugins/shape-on-terrain/goal.md +12 -0
  121. package/semiplugins/shape-on-terrain/{padding-1-degree.js → padding-1-degree.ts} +713 -539
  122. package/semiplugins/shape-on-terrain/terrain-polygon/adapters.ts +69 -0
  123. package/semiplugins/shape-on-terrain/terrain-polygon/data/cache-shortcuts.md +20 -0
  124. package/semiplugins/shape-on-terrain/terrain-polygon/data/cache.ts +149 -0
  125. package/semiplugins/shape-on-terrain/terrain-polygon/data/index-polygon-map.ts +58 -0
  126. package/semiplugins/shape-on-terrain/terrain-polygon/data/manager.ts +4 -0
  127. package/semiplugins/shape-on-terrain/terrain-polygon/data/master-worker.js +196 -0
  128. package/semiplugins/shape-on-terrain/terrain-polygon/data/master-worker.ts +209 -0
  129. package/semiplugins/shape-on-terrain/terrain-polygon/data/polygon-to-triangles.ts +144 -0
  130. package/semiplugins/shape-on-terrain/terrain-polygon/data/random.ts +165 -0
  131. package/semiplugins/shape-on-terrain/terrain-polygon/data/readme.md +5 -0
  132. package/semiplugins/shape-on-terrain/terrain-polygon/data/types.ts +37 -0
  133. package/semiplugins/shape-on-terrain/terrain-polygon/data/worker-contact.ts +81 -0
  134. package/semiplugins/shape-on-terrain/terrain-polygon/data/worker.js +146 -0
  135. package/semiplugins/shape-on-terrain/terrain-polygon/notes.md +90 -0
  136. package/semiplugins/shape-on-terrain/terrain-polygon/terrain-polygon.ts +265 -0
  137. package/semiplugins/shape-on-terrain/terrain-polygon/types.ts +69 -0
  138. package/semiplugins/shell/bbox-renderer/index.ts +2 -0
  139. package/semiplugins/shell/bbox-renderer/{logic.js → logic.ts} +273 -249
  140. package/semiplugins/shell/bbox-renderer/object.ts +129 -0
  141. package/semiplugins/type.ts +8 -0
  142. package/semiplugins/utility/{container-plugin.js → container-plugin.ts} +126 -94
  143. package/semiplugins/utility/{object-pass-container-plugin.js → object-pass-container-plugin.ts} +101 -80
  144. package/shaders/fragment-toy/firework.js +1 -1
  145. package/shaders/fragment-toy/singularity.js +5 -2
  146. package/terrain-plugin.mmd +83 -0
  147. package/tests/Math/arc-sampling-test.js +367 -0
  148. package/tests/Math/arc-sampling-test.ts +429 -0
  149. package/tests/Math/arc.test.ts +77 -0
  150. package/tests/Math/junction/arc-limit.test.ts +7 -0
  151. package/tests/Math/junction/arc-plane-points.test.ts +196 -0
  152. package/tests/Math/junction/arc-plane.test.ts +172 -0
  153. package/tests/Math/junction/line-sphere.test.ts +127 -0
  154. package/tests/Math/junction/plane-plane.test.ts +91 -0
  155. package/tests/Math/plane-test.ts +17 -0
  156. package/tests/Math/plane.test.ts +43 -0
  157. package/tests/Math/vec3.test.ts +33 -0
  158. package/tracks/point-heat-map/adaptors/timetracksplugin-format-to-this.js +78 -63
  159. package/tracks/point-heat-map/index.js +0 -1
  160. package/tracks/point-heat-map/plugin-webworker.js +148 -121
  161. package/tracks/point-heat-map/point-to-heat-map-flow.js +150 -121
  162. package/tracks/point-heat-map/readme.md +15 -0
  163. package/tracks/point-tracks/key-methods.js +3 -2
  164. package/tracks/point-tracks/plugin.js +487 -393
  165. package/tracks/timetracks/adaptors-line-strip.js +79 -65
  166. package/tracks/timetracks/plugin-line-strip.js +295 -240
  167. package/tracks/timetracks/program-line-strip.js +495 -411
  168. package/tracks/timetracks/programpoint-line-strip.js +137 -109
  169. package/tracks/timetracks/readme.md +1 -0
  170. package/tsconfig.json +22 -0
  171. package/types/@pirireis/webglobe.d.ts +102 -0
  172. package/types/delaunator.d.ts +40 -0
  173. package/types/earcut.d.ts +11 -0
  174. package/types/rbush.d.ts +57 -0
  175. package/types.ts +319 -0
  176. package/util/account/bufferoffsetmanager.js +209 -176
  177. package/util/account/create-buffermap-orchastration.ts +85 -0
  178. package/util/account/index.js +6 -3
  179. package/util/account/single-attribute-buffer-management/{buffer-manager.js → buffer-manager.ts} +151 -117
  180. package/util/account/single-attribute-buffer-management/{buffer-orchestrator.js → buffer-orchestrator.ts} +238 -212
  181. package/util/account/single-attribute-buffer-management/{buffer-orchestrator1.js → buffer-orchestrator1.ts} +184 -159
  182. package/util/account/single-attribute-buffer-management/{index.js → index.ts} +11 -4
  183. package/util/account/single-attribute-buffer-management/{object-store.js → object-store.ts} +76 -55
  184. package/util/account/single-attribute-buffer-management/types.ts +43 -0
  185. package/util/account/util.js +22 -18
  186. package/util/algorithms/index.js +0 -1
  187. package/util/algorithms/search-binary.js +26 -25
  188. package/util/build-strategy/{static-dynamic.js → static-dynamic.ts} +50 -41
  189. package/util/check/index.js +0 -1
  190. package/util/check/typecheck.ts +74 -0
  191. package/util/{frame-counter-trigger.js → frame-counter-trigger.ts} +99 -84
  192. package/util/geometry/{index.js → index.ts} +155 -121
  193. package/util/gl-util/buffer/{attribute-loader.js → attribute-loader.ts} +84 -62
  194. package/util/gl-util/buffer/{index.js → index.ts} +6 -3
  195. package/util/gl-util/draw-options/{methods.js → methods.ts} +47 -32
  196. package/util/gl-util/uniform-block/{manager.js → manager.ts} +232 -190
  197. package/util/{webglobe/gldefaultstates.js → globe-default-gl-states.ts} +5 -4
  198. package/util/helper-methods.ts +9 -0
  199. package/util/index.js +9 -10
  200. package/util/interpolation/index.js +0 -1
  201. package/util/interpolation/timetrack/index.js +9 -2
  202. package/util/interpolation/timetrack/timetrack-interpolator.js +94 -79
  203. package/util/interpolation/timetrack/web-worker.js +51 -46
  204. package/util/picking/{fence.js → fence.ts} +47 -41
  205. package/util/picking/picker-displayer.ts +226 -0
  206. package/util/programs/draw-from-pixel-coords.js +201 -164
  207. package/util/programs/{draw-texture-on-canvas.js → draw-texture-on-canvas.ts} +92 -67
  208. package/util/programs/supersampletotextures.js +130 -97
  209. package/util/programs/texturetoglobe.js +153 -128
  210. package/util/shaderfunctions/{geometrytransformations.js → geometrytransformations.ts} +169 -41
  211. package/util/shaderfunctions/index.js +2 -2
  212. package/util/shaderfunctions/nodata.js +4 -2
  213. package/util/shaderfunctions/noisefunctions.js +10 -7
  214. package/util/{webglobjectbuilders.js → webglobjectbuilders.ts} +446 -358
  215. package/vectorfield/arrowfield/adaptor.js +11 -11
  216. package/vectorfield/arrowfield/index.js +3 -3
  217. package/vectorfield/arrowfield/plugin.js +128 -83
  218. package/vectorfield/waveparticles/adaptor.js +16 -15
  219. package/vectorfield/waveparticles/index.js +3 -3
  220. package/vectorfield/waveparticles/plugin.ts +506 -0
  221. package/vectorfield/wind/adapters/image-to-fields.ts +74 -0
  222. package/vectorfield/wind/adapters/types.ts +12 -0
  223. package/vectorfield/wind/{imagetovectorfieldandmagnitude.js → imagetovectorfieldandmagnitude.ts} +78 -56
  224. package/vectorfield/wind/index.js +5 -5
  225. package/vectorfield/wind/plugin-persistant copy.ts +461 -0
  226. package/vectorfield/wind/plugin-persistant.ts +483 -0
  227. package/vectorfield/wind/plugin.js +883 -671
  228. package/vectorfield/wind/vectorfieldimage.js +27 -23
  229. package/write-text/{context-text-bulk.js → context-text-bulk.ts} +285 -200
  230. package/write-text/context-text3.ts +252 -0
  231. package/write-text/{context-text4.js → context-text4.ts} +231 -145
  232. package/write-text/context-textDELETE.js +125 -94
  233. package/write-text/objectarraylabels/{index.js → index.ts} +2 -2
  234. package/write-text/objectarraylabels/objectarraylabels.d.ts +72 -0
  235. package/write-text/objectarraylabels/objectarraylabels.js +247 -200
  236. package/Math/matrix4.js +0 -1
  237. package/Math/tessellation/earcut/adapters.js +0 -37
  238. package/Math/tessellation/hybrid-triangle-tessellation-meta.js +0 -123
  239. package/Math/tessellation/methods.js +0 -46
  240. package/Math/tessellation/shred-input.js +0 -18
  241. package/Math/tessellation/tile-merger.js +0 -56
  242. package/Math/tessellation/tiler.js +0 -50
  243. package/Math/tessellation/triangle-tessellation-meta.js +0 -516
  244. package/Math/tessellation/triangle-tessellation.js +0 -14
  245. package/Math/tessellation/types.js +0 -1
  246. package/Math/tessellation/zoom-catch.js +0 -1
  247. package/Math/types.js +0 -1
  248. package/programs/line-on-globe/linestrip/data.js +0 -4
  249. package/programs/polygon-on-globe/partial-tesselation.js +0 -1
  250. package/programs/polygon-on-globe/texture-dem-triangle-test-plugin-triangle.js +0 -204
  251. package/programs/polygon-on-globe/texture-dem-triangle-test-plugin.js +0 -118
  252. package/programs/polygon-on-globe/texture-dem-triangles.js +0 -236
  253. package/programs/vectorfields/logics/drawrectangleparticles.js +0 -112
  254. package/programs/vectorfields/logics/ubo-new.js +0 -25
  255. package/range-tools-on-terrain/bearing-line/types.js +0 -1
  256. package/range-tools-on-terrain/circle-line-chain/types.js +0 -1
  257. package/range-tools-on-terrain/range-ring/rangeringangletext.js +0 -331
  258. package/range-tools-on-terrain/range-ring/types.js +0 -9
  259. package/semiplugins/interface.js +0 -1
  260. package/semiplugins/shape-on-terrain/terrain-cover/texture-dem-cover.js +0 -1
  261. package/semiplugins/shell/bbox-renderer/index.js +0 -2
  262. package/semiplugins/shell/bbox-renderer/object.js +0 -65
  263. package/semiplugins/type.js +0 -1
  264. package/types.js +0 -19
  265. package/util/account/create-buffermap-orchastration.js +0 -39
  266. package/util/account/single-attribute-buffer-management/types.js +0 -1
  267. package/util/check/typecheck.js +0 -66
  268. package/util/gl-util/uniform-block/types.js +0 -1
  269. package/util/picking/picker-displayer.js +0 -134
  270. package/util/webglobe/index.js +0 -2
  271. package/util/webglobe/rasteroverlay.js +0 -76
  272. package/vectorfield/waveparticles/plugin.js +0 -290
  273. package/write-text/attached-text-writer.js +0 -95
  274. package/write-text/context-text3.js +0 -167
@@ -0,0 +1,578 @@
1
+ import { DrawnTileInfo, GlobeBBox, Globe } from "../../types";
2
+ import { xyzToBBOX } from "../xyz-tile";
3
+ import { calculateMergedMeshDimLength } from "./methods";
4
+ import { NearestValuePadding } from "./nearest-value-padding";
5
+ // Channel of float32array mesh is 1;
6
+
7
+
8
+ type TileLimit = {
9
+ minX: number;
10
+ maxX: number;
11
+ minY: number;
12
+ maxY: number;
13
+ antimeridian?: boolean;
14
+ centerX?: number;
15
+ centerY?: number;
16
+ }
17
+
18
+ type TileMetaData = {
19
+ tileMap: Map<string, DrawnTileInfo>;
20
+ maxLevel: number;
21
+ minLevel: number;
22
+ tileLimitsByZoom: Map<number, TileLimit>;
23
+
24
+
25
+ }
26
+
27
+
28
+
29
+
30
+
31
+ export type MergedTilesInfo = {
32
+ mesh: Float32Array, bbox: GlobeBBox, zoom: number, coverRatio: { x: number, y: number }
33
+ };
34
+
35
+
36
+
37
+
38
+ function keyMethod(x: number, y: number, level: number): string {
39
+ return `${level}_${x}_${y}`;
40
+ }
41
+
42
+ function calculateParentTile(x: number, y: number, level: number): { x: number, y: number, level: number } {
43
+ return {
44
+ x: Math.floor(x / 2),
45
+ y: Math.floor(y / 2),
46
+ level: level - 1
47
+ };
48
+ }
49
+
50
+ // helper for analyses
51
+ type ZoomLevelData = {
52
+ xValues: number[]; // All X coordinates at this level
53
+ yValues: number[]; // All Y Coordinates at this level
54
+ }
55
+
56
+ function createMapAndMetaData(tiles: DrawnTileInfo[], globe: Globe): TileMetaData {
57
+ const tileMap = new Map<string, DrawnTileInfo>();
58
+ let maxLevel = -Infinity;
59
+ let minLevel = Infinity;
60
+
61
+ // Intermediate storage to analyze distributions per zoom level
62
+ const zoomData = new Map<number, ZoomLevelData>();
63
+
64
+ // 1. First Pass: Populate Map and Collect Coordinates
65
+ for (const tile of tiles) {
66
+ const key = keyMethod(tile.x, tile.y, tile.level);
67
+ tileMap.set(key, tile);
68
+
69
+ // Update Level Range
70
+ if (tile.level > maxLevel) maxLevel = tile.level;
71
+ if (tile.level < minLevel) minLevel = tile.level;
72
+
73
+ // Collect Data
74
+ if (!zoomData.has(tile.level)) {
75
+ zoomData.set(tile.level, { xValues: [], yValues: [] });
76
+ }
77
+ const data = zoomData.get(tile.level)!;
78
+ data.xValues.push(tile.x);
79
+ data.yValues.push(tile.y);
80
+ }
81
+
82
+ const tileLimitsByZoom = new Map<number, TileLimit>();
83
+
84
+ // 2. Second Pass: Analyze Data per Zoom Level
85
+ zoomData.forEach((data, level) => {
86
+ // --- Calculate Gravity Centers (Mode) ---
87
+
88
+
89
+ // --- Calculate Min/Max and Antimeridian ---
90
+ // Sort X values to detect gaps
91
+ const sortedX = [...new Set(data.xValues)].sort((a, b) => a - b);
92
+ const minY = Math.min(...data.yValues);
93
+ const maxY = Math.max(...data.yValues);
94
+
95
+ let minX = sortedX[0];
96
+ let maxX = sortedX[sortedX.length - 1];
97
+ let isAntimeridian = false;
98
+
99
+
100
+ // We only check for wrapping if we have tiles near both edges (0 and Max)
101
+ // A simple heuristic: find the largest gap between sorted indices.
102
+ let maxGap = 0;
103
+ let gapIndex = -1;
104
+
105
+ for (let i = 0; i < sortedX.length - 1; i++) {
106
+ const gap = sortedX[i + 1] - sortedX[i];
107
+ if (gap > maxGap) {
108
+ maxGap = gap;
109
+ gapIndex = i;
110
+ }
111
+ }
112
+
113
+ // Also check the wrap-around gap (distance from last tile to world end + first tile)
114
+ // Though strictly for clustering split, we usually look for the internal gap.
115
+
116
+ // If the largest internal gap is significantly large (e.g., > worldWidth / 2),
117
+ // we treat the smaller numbers as the "East of view" (Right side)
118
+ // and large numbers as "West of view" (Left side) relative to the dateline.
119
+ // let centerX = getGravityCenter(data.xValues);
120
+ // const centerY = getGravityCenter(data.yValues);
121
+ const { x: centerX, y: centerY } = getCenterFromCamera(globe, level);
122
+ if (maxGap > 5 && maxX === Math.pow(2, level) - 1 && minX === 0) {
123
+ isAntimeridian = true;
124
+
125
+ // Cluster 1 (Low X): sortedX[0] ... sortedX[gapIndex] -> "West Cluster" (The Americas/Left side of map 0..x)
126
+ // Cluster 2 (High X): sortedX[gapIndex+1] ... end -> "East Cluster" (Asia/Right side of map x..max)
127
+
128
+ // Per requirements:
129
+ // "max value now represents max of west cluster" -> Max of the Low X numbers
130
+ maxX = sortedX[gapIndex];
131
+
132
+ // "min values represents min of east cluster" -> Min of the High X numbers
133
+ minX = sortedX[gapIndex + 1];
134
+
135
+ }
136
+
137
+ tileLimitsByZoom.set(level, {
138
+ minX,
139
+ maxX,
140
+ minY,
141
+ maxY,
142
+ antimeridian: isAntimeridian,
143
+ centerX,
144
+ centerY
145
+ });
146
+ });
147
+
148
+ return {
149
+ tileMap,
150
+ maxLevel,
151
+ minLevel,
152
+ tileLimitsByZoom
153
+ };
154
+ }
155
+
156
+ // Helper to find the most frequent value (Gravity Center)
157
+ function getMode(arr: number[]): number {
158
+ if (arr.length === 0) return 0;
159
+ const map = new Map<number, number>();
160
+ let maxCount = 0;
161
+ let mode = arr[0];
162
+
163
+ for (const num of arr) {
164
+ const count = (map.get(num) || 0) + 1;
165
+ map.set(num, count);
166
+ if (count > maxCount) {
167
+ maxCount = count;
168
+ mode = num;
169
+ }
170
+ }
171
+ return mode;
172
+ }
173
+
174
+
175
+ function getGravityCenter(arr: number[]): number {
176
+ if (arr.length === 0) return 0;
177
+ const sum = arr.reduce((acc, val) => acc + val, 0);
178
+ return Math.round(sum / arr.length);
179
+ }
180
+
181
+ function getCenterFromCamera(globe: any, zoom: number): { x: number, y: number } {
182
+ const camLongLat = globe.FCamera.FCamLongLat;
183
+ const n = Math.pow(2, zoom);
184
+ const x = Math.floor((camLongLat.x / (2 * Math.PI) + 0.5) * n);
185
+ const y = Math.floor((camLongLat.y / Math.PI + 0.5) * n);
186
+ return { x, y };
187
+
188
+ }
189
+
190
+
191
+ function populateTileIntoHigherZoomLevel(
192
+ tileMesh: Float32Array[], tileDimensionLength: number,
193
+ targetDimensionLength: number,
194
+ ): Float32Array[] {
195
+ const newMesh = Array.from({ length: targetDimensionLength }, () => new Float32Array(targetDimensionLength));
196
+ // use linear interpolation to populate
197
+ for (let row = 0; row < targetDimensionLength; row++) {
198
+ for (let col = 0; col < targetDimensionLength; col++) {
199
+ const sourceRow = row / (targetDimensionLength - 1) * (tileDimensionLength - 1);
200
+ const sourceCol = col / (targetDimensionLength - 1) * (tileDimensionLength - 1);
201
+ const rowLow = Math.floor(sourceRow);
202
+ const rowHigh = Math.min(Math.ceil(sourceRow), tileDimensionLength - 1);
203
+ const colLow = Math.floor(sourceCol);
204
+ const colHigh = Math.min(Math.ceil(sourceCol), tileDimensionLength - 1);
205
+ const topLeft = tileMesh[rowLow][colLow];
206
+ const topRight = tileMesh[rowLow][colHigh];
207
+ const bottomLeft = tileMesh[rowHigh][colLow];
208
+ const bottomRight = tileMesh[rowHigh][colHigh];
209
+ const top = topLeft + (topRight - topLeft) * (sourceCol - colLow);
210
+ const bottom = bottomLeft + (bottomRight - bottomLeft) * (sourceCol - colLow);
211
+ newMesh[row][col] = top + (bottom - top) * (sourceRow - rowLow);
212
+ }
213
+ }
214
+ return newMesh;
215
+ }
216
+
217
+
218
+
219
+
220
+ function fillFromChildren(
221
+ targetMesh: Float32Array, targetDimensionLength: number,
222
+ childrenMeshes: (Float32Array[] | undefined)[], sourceDimensionLength: number,
223
+ startX: number, startY: number): void {
224
+
225
+ const halfDim = (sourceDimensionLength - 1) / 2;
226
+
227
+ // Child 0: Top Left
228
+ if (childrenMeshes[0]) {
229
+ const child = childrenMeshes[0];
230
+ for (let r = 0; r <= halfDim; r++) {
231
+ // Read from top of child (source convention is bottom-up, so top is index Length-1)
232
+ // We skip every second row: (L-1), (L-3), ...
233
+ const childRowIndex = (sourceDimensionLength - 1) - (r * 2);
234
+ const rowData = child[childRowIndex];
235
+ for (let c = 0; c <= halfDim; c++) {
236
+ // Skip every second column
237
+ const val = rowData[c * 2];
238
+ const targetIdx = (startY + r) * targetDimensionLength + (startX + c);
239
+ targetMesh[targetIdx] = val;
240
+ }
241
+ }
242
+ }
243
+
244
+ // Child 1: Top Right
245
+ // In mergeTiles loop (dx outer, dy inner), index 2 corresponds to dx=1, dy=0 (Top Right)
246
+ if (childrenMeshes[2]) {
247
+ const child = childrenMeshes[2];
248
+ for (let r = 0; r <= halfDim; r++) {
249
+ const childRowIndex = (sourceDimensionLength - 1) - (r * 2);
250
+ const rowData = child[childRowIndex];
251
+ for (let c = 0; c <= halfDim; c++) {
252
+ const val = rowData[c * 2];
253
+ const targetIdx = (startY + r) * targetDimensionLength + (startX + halfDim + c);
254
+ if (isNaN(targetMesh[targetIdx])) {
255
+ targetMesh[targetIdx] = val;
256
+ }
257
+ }
258
+ }
259
+ }
260
+
261
+ // Child 2: Bottom Left
262
+ // In mergeTiles loop, index 1 corresponds to dx=0, dy=1 (Bottom Left)
263
+ if (childrenMeshes[1]) {
264
+ const child = childrenMeshes[1];
265
+ for (let r = 0; r <= halfDim; r++) {
266
+ const childRowIndex = (sourceDimensionLength - 1) - (r * 2);
267
+ const rowData = child[childRowIndex];
268
+ for (let c = 0; c <= halfDim; c++) {
269
+ const val = rowData[c * 2];
270
+ const targetIdx = (startY + halfDim + r) * targetDimensionLength + (startX + c);
271
+ if (isNaN(targetMesh[targetIdx])) {
272
+ targetMesh[targetIdx] = val;
273
+ }
274
+ }
275
+ }
276
+ }
277
+
278
+ // Child 3: Bottom Right
279
+ if (childrenMeshes[3]) {
280
+ const child = childrenMeshes[3];
281
+ for (let r = 0; r <= halfDim; r++) {
282
+ const childRowIndex = (sourceDimensionLength - 1) - (r * 2);
283
+ const rowData = child[childRowIndex];
284
+ for (let c = 0; c <= halfDim; c++) {
285
+ const val = rowData[c * 2];
286
+ const targetIdx = (startY + halfDim + r) * targetDimensionLength + (startX + halfDim + c);
287
+ if (isNaN(targetMesh[targetIdx])) {
288
+ targetMesh[targetIdx] = val;
289
+ }
290
+ }
291
+ }
292
+ }
293
+ }
294
+
295
+
296
+
297
+ // This function is correct, assuming its inputs are prepared properly.
298
+ // This function is correct, assuming its inputs are prepared properly.
299
+ function moveMeshToMergedMesh(
300
+ targetMesh: Float32Array, targetDimensionLength: number,
301
+ sourceMesh: Float32Array[], sourceDimensionLength: number,
302
+ startX: number, startY: number): void {
303
+ // sourceMesh[0][0] is south west corner
304
+ // targetMesh[0][0] is top left corner
305
+ let targetIndex: number = startY * targetDimensionLength + startX;
306
+ for (let y = 0; y < sourceDimensionLength; y++) {
307
+ // Read from bottom to top (source mesh convention)
308
+ const vertical = sourceMesh[sourceDimensionLength - y - 1];
309
+ for (let x = 0; x < sourceDimensionLength; x++) {
310
+ const value = vertical[x];
311
+ // const currentValue = targetMesh[targetIndex];
312
+ // TODO: investigate why overlapping values are not the same. cancel parent filling
313
+ // if (!isNaN(value) && value !== 0 && currentValue !== 0) {
314
+ // if (currentValue !== value) {
315
+ // console.warn(`Overlapping values are not the same: currentValue=${currentValue}, newValue=${value}`);
316
+ // }
317
+ // }
318
+ if (isNaN(targetMesh[targetIndex])) {
319
+ targetMesh[targetIndex] = value;
320
+ }
321
+ targetIndex++;
322
+ }
323
+ targetIndex += targetDimensionLength - sourceDimensionLength;
324
+ }
325
+ }
326
+
327
+
328
+
329
+
330
+
331
+ function limitCheck(limit: TileLimit, limitRange = 12): void {
332
+ // TODO: far tiles from camera should be pruned to fit in the limit range
333
+ const rowRange = limit.maxX - limit.minX + 1;
334
+ const colRange = limit.maxY - limit.minY + 1;
335
+
336
+ if (rowRange > limitRange || colRange > limitRange) {
337
+ console.warn(`Tile limit range exceeded: Row Range = ${rowRange}, Col Range = ${colRange}, Limit Range = ${limitRange}`);
338
+ throw new Error("Tile limit range exceeded");
339
+ }
340
+ }
341
+
342
+ function mergeTiles(tilesMetaData: TileMetaData, mergeCount = 8, tileDimensionLength = 5, processLimit = 6): MergedTilesInfo[] {
343
+ const mergedMeshDimLength = calculateMergedMeshDimLength(mergeCount, tileDimensionLength);
344
+ const nearestValuePadding = new NearestValuePadding(tileDimensionLength, mergeCount);
345
+
346
+ // Helper to check if a number is between range (inclusive)
347
+
348
+ const result: MergedTilesInfo[] = [];
349
+
350
+ for (let zoom = tilesMetaData.maxLevel; zoom >= tilesMetaData.minLevel; zoom--) {
351
+
352
+ if (tilesMetaData.maxLevel - zoom >= processLimit) {
353
+ break;
354
+ }
355
+
356
+ const limits = tilesMetaData.tileLimitsByZoom.get(zoom);
357
+ if (!limits) continue;
358
+
359
+ const mergedMesh = new Float32Array(mergedMeshDimLength * mergedMeshDimLength).fill(NaN);
360
+
361
+ // Initialize BBox.
362
+ // We track East (high X) and West (low X) clusters separately for Antimeridian cases.
363
+ const bboxLimit: GlobeBBox = {
364
+ ur: { x: -Infinity, y: -Infinity },
365
+ ll: { x: Infinity, y: Infinity }
366
+ };
367
+
368
+ // Specific BBox trackers for antimeridian split
369
+ let eastClusterMinX = Infinity; // For the left side of the view (e.g. 170)
370
+ let westClusterMaxX = -Infinity; // For the right side of the view (e.g. -170)
371
+
372
+ // limitCheck(limits, 20); // Assumed helper<
373
+
374
+ const worldWidth = Math.pow(2, zoom);
375
+
376
+ // --- Task 1: Calculate Grid Origin (Centering) ---
377
+ // We determine where the top-left of our mergeCount x mergeCount grid starts in tile coordinates.
378
+
379
+ let originX = limits.minX;
380
+ let originY = limits.minY;
381
+
382
+ const rangeX = limits.antimeridian
383
+ ? (worldWidth - limits.minX) + limits.maxX + 1
384
+ : limits.maxX - limits.minX + 1;
385
+ const rangeY = limits.maxY - limits.minY + 1;
386
+
387
+ // If the data is wider than the mesh, center it using centerX
388
+ if (rangeX > mergeCount && limits.centerX !== undefined) {
389
+ originX = Math.floor(limits.centerX - (mergeCount / 2));
390
+ }
391
+
392
+ // If the data is taller than the mesh, center it using centerY
393
+ if (rangeY > mergeCount && limits.centerY !== undefined) {
394
+ originY = Math.floor(limits.centerY - (mergeCount / 2));
395
+ }
396
+
397
+ // --- Task 2: Iterate Grid Slots ---
398
+ // Instead of iterating tile coordinates, we iterate the target grid slots (0..mergeCount)
399
+ // and calculate which tile belongs there.
400
+
401
+ for (let i = 0; i < mergeCount; i++) {
402
+ for (let j = 0; j < mergeCount; j++) {
403
+
404
+ // Calculate actual Tile Coordinates
405
+ // Handle X Wrapping for Antimeridian
406
+ let tileX = (originX + i);
407
+ // JS modulo of negative numbers behaves oddly, ensure positive wrap
408
+ tileX = ((tileX % worldWidth) + worldWidth) % worldWidth;
409
+
410
+ const tileY = originY + j;
411
+
412
+ // --- Validity Check ---
413
+ // Check if this specific tileX/tileY is actually part of the visible map data
414
+ let isValidX = false;
415
+ if (limits.antimeridian) {
416
+ isValidX = tileX >= limits.minX || tileX <= limits.maxX;
417
+ } else {
418
+ isValidX = tileX >= limits.minX && tileX <= limits.maxX;
419
+ }
420
+
421
+ const isValidY = tileY >= limits.minY && tileY <= limits.maxY;
422
+
423
+ if (!isValidX || !isValidY) continue;
424
+
425
+ const key = keyMethod(tileX, tileY, zoom);
426
+ const tile = tilesMetaData.tileMap.get(key);
427
+
428
+ // Calculate mesh offset (target position in pixels/floats)
429
+ const meshOffsetX = i * (tileDimensionLength - 1);
430
+ const meshOffsetY = j * (tileDimensionLength - 1);
431
+
432
+ if (tile) {
433
+ // 1. Direct Tile Found
434
+ nearestValuePadding.insert(tile.x, tile.y); // Using original coordinates for padding logic
435
+ moveMeshToMergedMesh(
436
+ mergedMesh, mergedMeshDimLength,
437
+ tile.mesh, tileDimensionLength,
438
+ meshOffsetX, meshOffsetY
439
+ );
440
+
441
+ // Update BBox Limits
442
+ // We simply expand the LL/UR.
443
+ // Post-processing will fix the inverted X for antimeridian.
444
+ if (tile.bbox.ll.x < bboxLimit.ll.x) bboxLimit.ll.x = tile.bbox.ll.x;
445
+ if (tile.bbox.ll.y < bboxLimit.ll.y) bboxLimit.ll.y = tile.bbox.ll.y;
446
+ if (tile.bbox.ur.x > bboxLimit.ur.x) bboxLimit.ur.x = tile.bbox.ur.x;
447
+ if (tile.bbox.ur.y > bboxLimit.ur.y) bboxLimit.ur.y = tile.bbox.ur.y;
448
+
449
+ // Track clusters for antimeridian split
450
+ // High X values (East Cluster) -> Left side of visual boundary
451
+ // Low X values (West Cluster) -> Right side of visual boundary
452
+ if (limits.antimeridian) {
453
+ if (tileX >= limits.minX) {
454
+ if (tile.bbox.ll.x < eastClusterMinX) eastClusterMinX = tile.bbox.ll.x;
455
+ } else {
456
+ if (tile.bbox.ur.x > westClusterMaxX) westClusterMaxX = tile.bbox.ur.x;
457
+ }
458
+ }
459
+
460
+ } else {
461
+ // 2. Parent Tile Fallback
462
+ const parentTileCoord = calculateParentTile(tileX, tileY, zoom);
463
+ const parentKey = keyMethod(parentTileCoord.x, parentTileCoord.y, parentTileCoord.level);
464
+ const parentTile = tilesMetaData.tileMap.get(parentKey);
465
+
466
+ if (parentTile) {
467
+ const populatedMesh = populateTileIntoHigherZoomLevel(
468
+ parentTile.mesh, tileDimensionLength,
469
+ tileDimensionLength * 2 - 1
470
+ );
471
+
472
+ // Adjust offset for parent logic
473
+ // If current tile is odd (right/bottom), the parent mesh drawing needs to shift back
474
+ // because 'populatedMesh' covers 2x2.
475
+ const parentDrawOffsetX = meshOffsetX - (tileX % 2) * (tileDimensionLength - 1);
476
+ const parentDrawOffsetY = meshOffsetY - (tileY % 2) * (tileDimensionLength - 1);
477
+
478
+ moveMeshToMergedMesh(
479
+ mergedMesh, mergedMeshDimLength,
480
+ populatedMesh, tileDimensionLength * 2 - 1,
481
+ parentDrawOffsetX,
482
+ parentDrawOffsetY
483
+ );
484
+
485
+ // Padding logic for parents
486
+ const xP = parentTile.x * 2;
487
+ const yP = parentTile.y * 2;
488
+ for (let dx = 0; dx < 2; dx++) {
489
+ for (let dy = 0; dy < 2; dy++) {
490
+ nearestValuePadding.insert(xP + dx, yP + dy);
491
+ }
492
+ }
493
+ } else {
494
+ // 3. Children Tile Fallback
495
+ const childrenTiles: (Float32Array[] | undefined)[] = [];
496
+ let anyChildren = 0;
497
+ for (let dx = 0; dx < 2; dx++) {
498
+ for (let dy = 0; dy < 2; dy++) {
499
+ const childX = tileX * 2 + dx;
500
+ const childY = tileY * 2 + dy;
501
+ const childKey = keyMethod(childX, childY, zoom + 1);
502
+ const childTile = tilesMetaData.tileMap.get(childKey);
503
+ if (childTile) {
504
+ anyChildren++;
505
+ childrenTiles.push(childTile.mesh);
506
+ } else {
507
+ childrenTiles.push(undefined);
508
+ }
509
+ }
510
+ }
511
+
512
+ if (anyChildren > 0) {
513
+ nearestValuePadding.insert(tileX, tileY);
514
+ fillFromChildren(
515
+ mergedMesh, mergedMeshDimLength,
516
+ childrenTiles, tileDimensionLength,
517
+ meshOffsetX, meshOffsetY
518
+ );
519
+ }
520
+ }
521
+ }
522
+ }
523
+ }
524
+
525
+ // Post-Processing BBox for Antimeridian
526
+ if (limits.antimeridian) {
527
+ // Task: bbox.ll should contain lower-left of East Cluster, bbox.ur should contain upper-right of West Cluster
528
+ // This creates the condition ur.x < ll.x (e.g. 170 to -170)
529
+
530
+ // Only update if we actually found tiles in those clusters
531
+ if (eastClusterMinX !== Infinity) bboxLimit.ll.x = eastClusterMinX;
532
+ if (westClusterMaxX !== -Infinity) bboxLimit.ur.x = westClusterMaxX;
533
+ // console.log("Antimeridian BBox adjusted:", bboxLimit);
534
+ }
535
+
536
+ // We pad the merged mesh based on the VALID range we iterated.
537
+ // Since we iterate 0..mergeCount, the indices are implicitly 0..mergeCount
538
+ // However, the clamp function usually expects absolute coordinates.
539
+ // Given the new "Grid" approach, the mesh is always locally 0 to mergeCount in valid data.
540
+ // We pass the grid-relative limits to clamp.
541
+ nearestValuePadding.clamp(mergedMesh, 0, 0, mergeCount, mergeCount);
542
+ nearestValuePadding.clear();
543
+
544
+ // Calculate Cover Ratio
545
+ // How much of the mesh grid (mergeCount) is covered by actual data range?
546
+ const currentWidth = limits.antimeridian ? rangeX : (limits.maxX - limits.minX + 1);
547
+ const currentHeight = limits.maxY - limits.minY + 1;
548
+
549
+ const usedWidth = Math.min(mergeCount, currentWidth);
550
+ const usedHeight = Math.min(mergeCount, currentHeight);
551
+
552
+ const xRatio = calculateMergedMeshDimLength(usedWidth, tileDimensionLength) / mergedMeshDimLength;
553
+ const yRatio = calculateMergedMeshDimLength(usedHeight, tileDimensionLength) / mergedMeshDimLength;
554
+
555
+ result.push({ mesh: mergedMesh, bbox: bboxLimit, zoom: zoom, coverRatio: { x: xRatio, y: yRatio } });
556
+ }
557
+
558
+ return result;
559
+ }
560
+
561
+
562
+ export function mergeMeshes(
563
+
564
+ tiles: DrawnTileInfo[],
565
+ mergeCount = 12,
566
+ tileDimensionLength = 5,
567
+ processLimit = 6,
568
+ globe: Globe
569
+ ): MergedTilesInfo[] {
570
+ // filter out tiles with NaN values
571
+ // return returnTop6tiles(tiles);
572
+
573
+ const tilesMetaData = createMapAndMetaData(tiles, globe);
574
+ const mergedTiles = mergeTiles(tilesMetaData, mergeCount, tileDimensionLength, processLimit);
575
+ return mergedTiles;
576
+ }
577
+
578
+