Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Mar 6, 2025
1 parent f867c3f commit 6880d25
Show file tree
Hide file tree
Showing 41 changed files with 1,126 additions and 342 deletions.
153 changes: 153 additions & 0 deletions frontend/src/lib/utils/bbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { BoundingBox3D_api } from "@api";

import * as vec3 from "./vec3";

/**
* A bounding box.
*/
export type BBox = {
min: vec3.Vec3;
max: vec3.Vec3;
};

/**
* Creates a new bounding box.
* @param min The minimum point of the bounding box.
* @param max The maximum point of the bounding box.
* @returns A new bounding box.
*/
export function create(min: vec3.Vec3, max: vec3.Vec3): BBox {
return { min, max };
}

/**
* Creates a new bounding box from a given bounding box from the API.
* @param boundingBox A bounding box from the API.
* @returns A respective new bounding box.
*/
export function fromBoundingBox3DApi(boundingBox: BoundingBox3D_api): BBox {
return create(
vec3.create(boundingBox.xmin, boundingBox.ymin, boundingBox.zmin),
vec3.create(boundingBox.xmax, boundingBox.ymax, boundingBox.zmax)
);
}

/**
* Creates a new 2D bounding box from a given bounding box from the API.
* @param boundingBox A bounding box from the API.
* @returns A respective new bounding box.
*/
export function fromBoundingBox2DApi(boundingBox: BoundingBox3D_api): BBox {
return create(
vec3.create(boundingBox.xmin, boundingBox.ymin, 0),
vec3.create(boundingBox.xmax, boundingBox.ymax, 0)
);
}

/**
* Returns true if the bounding box contains the given point.
* @param box The bounding box.
* @param point The point.
* @returns True if the bounding box contains the point.
*/
export function containsPoint(box: BBox, point: vec3.Vec3): boolean {
return (
point.x >= box.min.x &&
point.x <= box.max.x &&
point.y >= box.min.y &&
point.y <= box.max.y &&
point.z >= box.min.z &&
point.z <= box.max.z
);
}

/**
* Returns true if the two bounding boxes intersect.
* @param box1 The first bounding box.
* @param box2 The second bounding box.
* @returns True if the two bounding boxes intersect.
*/
export function intersects(box1: BBox, box2: BBox): boolean {
return (
box1.min.x <= box2.max.x &&
box1.max.x >= box2.min.x &&
box1.min.y <= box2.max.y &&
box1.max.y >= box2.min.y &&
box1.min.z <= box2.max.z &&
box1.max.z >= box2.min.z
);
}

/**
* Returns true if outerBox contains innerBox.
* @param outerBox The outer bounding box.
* @param innerBox The inner bounding box.
* @returns True if outerBox contains innerBox.
*/
export function outerBoxcontainsInnerBox(outerBox: BBox, innerBox: BBox): boolean {
return (
outerBox.min.x <= innerBox.min.x &&
outerBox.min.y <= innerBox.min.y &&
outerBox.min.z <= innerBox.min.z &&
outerBox.max.x >= innerBox.max.x &&
outerBox.max.y >= innerBox.max.y &&
outerBox.max.z >= innerBox.max.z
);
}

/**
* Converts a bounding box to an array of numbers.
* The array contains the following numbers in the following order:
* [min.x, min.y, min.z, max.x, max.y, max.z]
*
* @param box The bounding box.
* @returns An array of numbers.
*/
export function toNumArray(box: BBox): [number, number, number, number, number, number] {
return [box.min.x, box.min.y, box.min.z, box.max.x, box.max.y, box.max.z];
}

/**
* Converts an array of numbers to a bounding box.
* The array should contain the following numbers in the following order:
* [min.x, min.y, min.z, max.x, max.y, max.z]
*
* @param array An array of numbers.
* @returns A new bounding box.
*/
export function fromNumArray(array: [number, number, number, number, number, number]): BBox {
return create(vec3.fromArray(array.slice(0, 3)), vec3.fromArray(array.slice(3, 6)));
}

/**
* Clones the given bounding box.
*
* @param box The bounding box to clone.
* @returns A new bounding box.
*/
export function clone(box: BBox): BBox {
return create(vec3.clone(box.min), vec3.clone(box.max));
}

/**
* Combines the two bounding boxes into a new bounding box that contains both.
*
* @param box1 The first bounding box.
* @param box2 The second bounding box.
*
* @returns A new bounding box that holds both bounding boxes.
*/
export function combine(box1: BBox, box2: BBox): BBox {
return create(
vec3.create(
Math.min(box1.min.x, box2.min.x),
Math.min(box1.min.y, box2.min.y),
Math.min(box1.min.z, box2.min.z)
),
vec3.create(
Math.max(box1.max.x, box2.max.x),
Math.max(box1.max.y, box2.max.y),
Math.max(box1.max.z, box2.max.z)
)
);
}
83 changes: 83 additions & 0 deletions frontend/src/lib/utils/mat3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* A 3x3 matrix.
*/
export type Mat3 = {
m00: number;
m01: number;
m02: number;
m10: number;
m11: number;
m12: number;
m20: number;
m21: number;
m22: number;
};

/**
* Creates a new 3x3 matrix with all elements set to 0.
*
* @returns A new 3x3 matrix.
*/
export function createEmpty(): Mat3 {
return {
m00: 0,
m01: 0,
m02: 0,
m10: 0,
m11: 0,
m12: 0,
m20: 0,
m21: 0,
m22: 0,
};
}

/**
* Creates a new 3x3 matrix with the given elements.
*
* @param m00 The element at row 0, column 0.
* @param m01 The element at row 0, column 1.
* @param m02 The element at row 0, column 2.
* @param m10 The element at row 1, column 0.
* @param m11 The element at row 1, column 1.
* @param m12 The element at row 1, column 2.
* @param m20 The element at row 2, column 0.
* @param m21 The element at row 2, column 1.
* @param m22 The element at row 2, column 2.
* @returns A new 3x3 matrix.
*/
export function create(
m00: number,
m01: number,
m02: number,
m10: number,
m11: number,
m12: number,
m20: number,
m21: number,
m22: number
): Mat3 {
return { m00, m01, m02, m10, m11, m12, m20, m21, m22 };
}

/**
* Creates a new 3x3 matrix from the given array of numbers.
*
* @param array An array of numbers in the following order: [m00, m01, m02, m10, m11, m12, m20, m21, m22].
* @returns A new 3x3 matrix.
*/
export function fromArray(
array: ArrayLike<number> | [number, number, number, number, number, number, number, number, number]
): Mat3 {
return {
m00: array[0],
m01: array[1],
m02: array[2],
m10: array[3],
m11: array[4],
m12: array[5],
m20: array[6],
m21: array[7],
m22: array[8],
};
}
Loading

0 comments on commit 6880d25

Please sign in to comment.