summaryrefslogtreecommitdiff
path: root/client/splash/face/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/splash/face/util.js')
-rw-r--r--client/splash/face/util.js83
1 files changed, 48 insertions, 35 deletions
diff --git a/client/splash/face/util.js b/client/splash/face/util.js
index e6237316..38b1e376 100644
--- a/client/splash/face/util.js
+++ b/client/splash/face/util.js
@@ -1,6 +1,6 @@
-import { Vector3, Geometry } from 'three'
-import { FACE_SCALE, LINE_THICKNESS } from '../constants'
+import { Geometry } from 'three'
+import { LINE_THICKNESS } from '../constants'
export function getLineGeometry(points) {
return [
@@ -22,6 +22,11 @@ export function getLineGeometry(points) {
})
}
+export function updateFace(buf, cubes, meshes) {
+ updateCubeGeometry(buf, cubes)
+ updateLineGeometry(buf, meshes)
+}
+
export function updateLineGeometry(points, meshes) {
getLineGeometry(points).forEach((geometry, i) => {
const [line, mesh] = meshes[i]
@@ -31,6 +36,13 @@ export function updateLineGeometry(points, meshes) {
})
}
+export function updateCubeGeometry(points, cubes) {
+ cubes.forEach((cube, i) => {
+ const p = points[i]
+ cube.position.set(p.x, p.y, p.z)
+ })
+}
+
export function lerp(n, a, b) {
return (b - a) * n + a
}
@@ -47,40 +59,41 @@ export function lerpPoints(n, A, B, C) {
}
}
-export function updateCubeGeometry(points, cubes) {
- cubes.forEach((cube, i) => {
- const p = points[i]
- cube.position.set(p.x, p.y, p.z)
+export function getBboxForPoints(points) {
+ return points.reduce((a, p) => {
+ a.min.x = Math.min(a.min.x, p[0])
+ a.max.x = Math.max(a.max.x, p[0])
+ a.min.y = Math.min(a.min.y, p[1])
+ a.max.y = Math.max(a.max.y, p[1])
+ a.min.z = Math.min(a.min.z, p[2])
+ a.max.z = Math.max(a.max.z, p[2])
+ return a
+ }, {
+ min: { x: Infinity, y: Infinity, z: Infinity },
+ max: { x: -Infinity, y: -Infinity, z: -Infinity }
})
}
-export function updateFace(buf, cubes, meshes) {
- updateCubeGeometry(buf, cubes)
- updateLineGeometry(buf, meshes)
-}
-
-export function getBounds(obj) {
- return obj.reduce((a, p) => {
- return [
- Math.min(a[0], p[0]),
- Math.max(a[1], p[0]),
- Math.min(a[2], p[1]),
- Math.max(a[3], p[1]),
- Math.min(a[4], p[2]),
- Math.max(a[5], p[2]),
- ]
- }, [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity])
-}
-
-export function recenter(obj) {
- const bounds = getBounds(obj)
- const xWidth = (bounds[1] - bounds[0]) / 2
- const yWidth = (bounds[3] - bounds[2]) / -3
- const zWidth = (bounds[5] - bounds[4]) / 2
- return obj.map(p => {
- p[0] = p[0] - bounds[0] - xWidth
- p[1] = -p[1] + bounds[1] + yWidth
- p[2] = p[2] - bounds[2] + zWidth
- return new Vector3(p[0] * FACE_SCALE, p[1] * FACE_SCALE, p[2] * FACE_SCALE)
- })
+export function getBboxScaleAndCentroid(bbox, position) {
+ if (position && position.isQuantized) {
+ // If the geometry is quantized, transform the bounding box to the dequantized
+ // coordinates.
+ const normConstant = position.maxRange / (1 << position.numQuantizationBits)
+ const minPos = position.minValues
+ bbox.max.x = minPos[0] + bbox.max.x * normConstant
+ bbox.max.y = minPos[1] + bbox.max.y * normConstant
+ bbox.max.z = minPos[2] + bbox.max.z * normConstant
+ bbox.min.x = minPos[0] + bbox.min.x * normConstant
+ bbox.min.y = minPos[1] + bbox.min.y * normConstant
+ bbox.min.z = minPos[2] + bbox.min.z * normConstant
+ }
+ const sizeX = bbox.max.x - bbox.min.x
+ const sizeY = bbox.max.y - bbox.min.y
+ const sizeZ = bbox.max.z - bbox.min.z
+ const diagonalSize = Math.sqrt(sizeX * sizeX + sizeY * sizeY + sizeZ * sizeZ)
+ const scale = 1.0 / diagonalSize
+ const midX = (bbox.min.x + bbox.max.x) / 2
+ const midY = (bbox.min.y + bbox.max.y) / 2
+ const midZ = (bbox.min.z + bbox.max.z) / 2
+ return { scale, midX, midY, midZ }
}