Skip to content

Commit

Permalink
[BREAKING] BoundingBox and OrientedBox copy parameters in the constru…
Browse files Browse the repository at this point in the history
…ctor (#6691)

* BoundingBox and Oriented box copy parameters in the constructor

* readonly tags

---------

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed Jun 26, 2024
1 parent 03920de commit bf0ddeb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 69 deletions.
25 changes: 14 additions & 11 deletions src/core/shape/bounding-box.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Debug } from '../debug.js';
import { Vec3 } from '../math/vec3.js';

const tmpVecA = new Vec3();
Expand All @@ -17,15 +16,17 @@ class BoundingBox {
* Center of box.
*
* @type {Vec3}
* @readonly
*/
center;
center = new Vec3();

/**
* Half the distance across the box in each axis.
*
* @type {Vec3}
* @readonly
*/
halfExtents;
halfExtents = new Vec3(0.5, 0.5, 0.5);

/**
* @type {Vec3}
Expand All @@ -43,15 +44,17 @@ class BoundingBox {
* Create a new BoundingBox instance. The bounding box is axis-aligned.
*
* @param {Vec3} [center] - Center of box. The constructor takes a reference of this parameter.
* Defaults to (0, 0, 0).
* @param {Vec3} [halfExtents] - Half the distance across the box in each axis. The constructor
* takes a reference of this parameter. Defaults to 0.5 on each axis.
* takes a reference of this parameter. Defaults to (0.5, 0.5, 0.5).
*/
constructor(center = new Vec3(), halfExtents = new Vec3(0.5, 0.5, 0.5)) {
Debug.assert(!Object.isFrozen(center), 'The constructor of \'BoundingBox\' does not accept a constant (frozen) object as a \'center\' parameter');
Debug.assert(!Object.isFrozen(halfExtents), 'The constructor of \'BoundingBox\' does not accept a constant (frozen) object as a \'halfExtents\' parameter');

this.center = center;
this.halfExtents = halfExtents;
constructor(center, halfExtents) {
if (center) {
this.center.copy(center);
}
if (halfExtents) {
this.halfExtents.copy(halfExtents);
}
}

/**
Expand Down Expand Up @@ -121,7 +124,7 @@ class BoundingBox {
* @returns {BoundingBox} A duplicate AABB.
*/
clone() {
return new BoundingBox(this.center.clone(), this.halfExtents.clone());
return new BoundingBox(this.center, this.halfExtents);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/core/shape/bounding-sphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BoundingSphere {
* Center of sphere.
*
* @type {Vec3}
* @readonly
*/
center;

Expand Down
72 changes: 23 additions & 49 deletions src/core/shape/frustum.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,88 +34,66 @@ class Frustum {
* frustum.setFromMat4(projMat);
*/
setFromMat4(matrix) {
const vpm = matrix.data;

let plane;
const normalize = (plane) => {
const t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
const invT = 1 / t;
plane[0] *= invT;
plane[1] *= invT;
plane[2] *= invT;
plane[3] *= invT;
};

const vpm = matrix.data;
const planes = this.planes;

// Extract the numbers for the RIGHT plane
plane = planes[0];
let plane = planes[0];
plane[0] = vpm[3] - vpm[0];
plane[1] = vpm[7] - vpm[4];
plane[2] = vpm[11] - vpm[8];
plane[3] = vpm[15] - vpm[12];
// Normalize the result
let t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
plane[0] /= t;
plane[1] /= t;
plane[2] /= t;
plane[3] /= t;
normalize(plane);

// Extract the numbers for the LEFT plane
plane = planes[1];
plane[0] = vpm[3] + vpm[0];
plane[1] = vpm[7] + vpm[4];
plane[2] = vpm[11] + vpm[8];
plane[3] = vpm[15] + vpm[12];
// Normalize the result
t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
plane[0] /= t;
plane[1] /= t;
plane[2] /= t;
plane[3] /= t;
normalize(plane);

// Extract the BOTTOM plane
plane = planes[2];
plane[0] = vpm[3] + vpm[1];
plane[1] = vpm[7] + vpm[5];
plane[2] = vpm[11] + vpm[9];
plane[3] = vpm[15] + vpm[13];
// Normalize the result
t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
plane[0] /= t;
plane[1] /= t;
plane[2] /= t;
plane[3] /= t;
normalize(plane);

// Extract the TOP plane
plane = planes[3];
plane[0] = vpm[3] - vpm[1];
plane[1] = vpm[7] - vpm[5];
plane[2] = vpm[11] - vpm[9];
plane[3] = vpm[15] - vpm[13];
// Normalize the result
t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
plane[0] /= t;
plane[1] /= t;
plane[2] /= t;
plane[3] /= t;
normalize(plane);

// Extract the FAR plane
plane = planes[4];
plane[0] = vpm[3] - vpm[2];
plane[1] = vpm[7] - vpm[6];
plane[2] = vpm[11] - vpm[10];
plane[3] = vpm[15] - vpm[14];
// Normalize the result
t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
plane[0] /= t;
plane[1] /= t;
plane[2] /= t;
plane[3] /= t;
normalize(plane);

// Extract the NEAR plane
plane = planes[5];
plane[0] = vpm[3] + vpm[2];
plane[1] = vpm[7] + vpm[6];
plane[2] = vpm[11] + vpm[10];
plane[3] = vpm[15] + vpm[14];
// Normalize the result
t = Math.sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
plane[0] /= t;
plane[1] /= t;
plane[2] /= t;
plane[3] /= t;
normalize(plane);
}

/**
Expand All @@ -126,9 +104,8 @@ class Frustum {
* @returns {boolean} True if the point is inside the frustum, false otherwise.
*/
containsPoint(point) {
let p, plane;
for (p = 0; p < 6; p++) {
plane = this.planes[p];
for (let p = 0; p < 6; p++) {
const plane = this.planes[p];
if (plane[0] * point.x + plane[1] * point.y + plane[2] * point.z + plane[3] <= 0) {
return false;
}
Expand All @@ -147,21 +124,18 @@ class Frustum {
* frustum and 2 if it is contained by the frustum.
*/
containsSphere(sphere) {
let c = 0;
let d;
let p;

const sr = sphere.radius;
const sc = sphere.center;
const scx = sc.x;
const scy = sc.y;
const scz = sc.z;
const planes = this.planes;
let plane;

for (p = 0; p < 6; p++) {
plane = planes[p];
d = plane[0] * scx + plane[1] * scy + plane[2] * scz + plane[3];
let c = 0;
for (let p = 0; p < 6; p++) {
const plane = planes[p];
const d = plane[0] * scx + plane[1] * scy + plane[2] * scz + plane[3];
if (d <= -sr)
return 0;
if (d > sr)
Expand Down
21 changes: 12 additions & 9 deletions src/core/shape/oriented-box.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Debug } from '../debug.js';
import { Mat4 } from '../math/mat4.js';
import { Vec3 } from '../math/vec3.js';

Expand All @@ -17,7 +16,11 @@ const tmpMat4 = new Mat4();
* @category Math
*/
class OrientedBox {
halfExtents;
/**
* @type {Vec3}
* @private
*/
halfExtents = new Vec3(0.5, 0.5, 0.5);

/**
* @type {Mat4}
Expand All @@ -41,15 +44,15 @@ class OrientedBox {
* Create a new OrientedBox instance.
*
* @param {Mat4} [worldTransform] - Transform that has the orientation and position of the box.
* Scale is assumed to be one.
* @param {Vec3} [halfExtents] - Half the distance across the box in each local axis. The
* constructor takes a reference of this parameter.
* Scale is assumed to be one. Defaults to identity matrix.
* @param {Vec3} [halfExtents] - Half the distance across the box in each local axis. Defaults
* to (0.5, 0.5, 0.5).
*/
constructor(worldTransform = new Mat4(), halfExtents = new Vec3(0.5, 0.5, 0.5)) {
Debug.assert(!Object.isFrozen(worldTransform), 'The constructor of \'OrientedBox\' does not accept a constant (frozen) object as a \'worldTransform\' parameter');
Debug.assert(!Object.isFrozen(halfExtents), 'The constructor of \'OrientedBox\' does not accept a constant (frozen) object as a \'halfExtents\' parameter');
constructor(worldTransform = new Mat4(), halfExtents) {

this.halfExtents = halfExtents;
if (halfExtents) {
this.halfExtents.copy(halfExtents);
}

this._modelTransform = worldTransform.clone().invert();
this._worldTransform = worldTransform.clone();
Expand Down
1 change: 1 addition & 0 deletions src/core/shape/plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Plane {
* The normal of the plane.
*
* @type {Vec3}
* @readonly
*/
normal = new Vec3();

Expand Down
3 changes: 3 additions & 0 deletions src/core/shape/tri.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Tri {
/**
* The first 3-dimensional vector of the triangle.
*
* @readonly
* @type {Vec3}
*/
v0 = new Vec3();
Expand All @@ -26,13 +27,15 @@ class Tri {
* The second 3-dimensional vector of the triangle.
*
* @type {Vec3}
* @readonly
*/
v1 = new Vec3();

/**
* The third 3-dimensional vector of the triangle.
*
* @type {Vec3}
* @readonly
*/
v2 = new Vec3();

Expand Down

0 comments on commit bf0ddeb

Please sign in to comment.