-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateCellPolygon.ts
More file actions
67 lines (56 loc) · 1.91 KB
/
Copy pathcreateCellPolygon.ts
File metadata and controls
67 lines (56 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {
type TransformNode,
type Scene,
Vector3,
MeshBuilder,
} from '@babylonjs/core'
import { cellToLatLng, cellToVertexes, vertexToLatLng } from 'h3-js'
import qh from 'quickhull3d'
import { RADIUS } from '../constants'
import { geoToVector3 } from '../geoCoords/geoToVector3'
// FIXME: positionNode should be in a scaled position (if we want to scale the cells)
export const worldToLocal = (
vector: Vector3,
positionNode: TransformNode
): Vector3 => {
const worldMatrix = positionNode.getWorldMatrix()
const localToWorldMatrix = worldMatrix.clone().invert()
return Vector3.TransformCoordinates(vector, localToWorldMatrix)
}
const createDipyramid = (basePoints: Vector3[], apexPoint: Vector3) => {
const vertices: Vector3[] = [...basePoints]
const largestValue = vertices.reduce((acc, v) => {
const value = Math.max(Math.abs(v.x), Math.abs(v.y), Math.abs(v.z))
return acc > value ? acc : value
}, 0)
vertices.push(apexPoint)
// FIXME: fiddle with this height
vertices.push(apexPoint.subtract(new Vector3(0, largestValue / 4, 0)))
const vertex = vertices.map((v) => v.asArray())
const face = qh(vertex)
return { vertex, face }
}
export const createCellPolygon = (
scene: Scene,
positionNode: TransformNode,
h: string,
radius: number = RADIUS
) => {
// NOTE: vertexes are far superior to boundaries
const cellVertexes: Vector3[] = []
for (const v of cellToVertexes(h)) {
const [lat, lng] = vertexToLatLng(v)
const localPosition = geoToVector3(lat, lng, radius)
const position = worldToLocal(localPosition, positionNode)
cellVertexes.push(position)
}
const [lat, lng] = cellToLatLng(h)
const cellCenter = worldToLocal(geoToVector3(lat, lng, radius), positionNode)
const cellNode = MeshBuilder.CreatePolyhedron(
'customPolyhedron',
{ custom: createDipyramid(cellVertexes, cellCenter) },
scene
)
cellNode.parent = positionNode
return cellNode
}