From 5c81830d2d80c7a4383b2b641e5f13b6c2c2224b Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Wed, 20 Dec 2023 16:20:25 +0100 Subject: [PATCH] feat(utils): create pointToBox() --- src/helpers.ts | 16 +++++++++++++++- src/utils.ts | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/helpers.ts b/src/helpers.ts index 81495e62..8838e34f 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,7 +1,7 @@ import fs from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' -import { BufferGeometry, Euler, Mesh, Object3D, Vector3 as ThreeJsVector3 } from 'three' +import { Box3, BufferGeometry, Euler, Mesh, Object3D, Vector3 as ThreeJsVector3 } from 'three' import { Vector3 } from '@src/Vector3.js' import { mean } from '@src/faux-ramda.js' @@ -157,3 +157,17 @@ export const normalizeDegree = (degree: number) => { export const numberOfVertices = (geometry: BufferGeometry) => { return geometry.getAttribute('position').array.length / 3 } + +/** + * Expands a 3D point into a Box3 object with the given size + * + * @param point - the center of the box + * @param size - sidelength/diameter of the box + * @returns the generated Box3 object + */ +export const pointToBox = (point: Vector3, size: number | Vector3) => { + size = typeof size === 'number' ? new Vector3(size, size, size) : size + const min = point.clone().sub(size) + const max = point.clone().add(size) + return new Box3(min, max) +} diff --git a/src/utils.ts b/src/utils.ts index f958822d..6b87356e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,4 +8,5 @@ export { isBetween, circleOfVectors, numberOfVertices, + pointToBox, } from '@src/helpers.js'