|
| 1 | +import * as vec3 from "./vec3"; |
| 2 | + |
| 3 | +/** |
| 4 | + * A bounding box. |
| 5 | + */ |
| 6 | +export type BBox = { |
| 7 | + min: vec3.Vec3; |
| 8 | + max: vec3.Vec3; |
| 9 | +}; |
| 10 | + |
| 11 | +/** |
| 12 | + * Creates a new bounding box. |
| 13 | + * @param min The minimum point of the bounding box. |
| 14 | + * @param max The maximum point of the bounding box. |
| 15 | + * @returns A new bounding box. |
| 16 | + */ |
| 17 | +export function create(min: vec3.Vec3, max: vec3.Vec3): BBox { |
| 18 | + return { min, max }; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Returns true if the bounding box contains the given point. |
| 23 | + * @param box The bounding box. |
| 24 | + * @param point The point. |
| 25 | + * @returns True if the bounding box contains the point. |
| 26 | + */ |
| 27 | +export function containsPoint(box: BBox, point: vec3.Vec3): boolean { |
| 28 | + return ( |
| 29 | + point.x >= box.min.x && |
| 30 | + point.x <= box.max.x && |
| 31 | + point.y >= box.min.y && |
| 32 | + point.y <= box.max.y && |
| 33 | + point.z >= box.min.z && |
| 34 | + point.z <= box.max.z |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Returns true if the two bounding boxes intersect. |
| 40 | + * @param box1 The first bounding box. |
| 41 | + * @param box2 The second bounding box. |
| 42 | + * @returns True if the two bounding boxes intersect. |
| 43 | + */ |
| 44 | +export function intersects(box1: BBox, box2: BBox): boolean { |
| 45 | + return ( |
| 46 | + box1.min.x <= box2.max.x && |
| 47 | + box1.max.x >= box2.min.x && |
| 48 | + box1.min.y <= box2.max.y && |
| 49 | + box1.max.y >= box2.min.y && |
| 50 | + box1.min.z <= box2.max.z && |
| 51 | + box1.max.z >= box2.min.z |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Returns true if outerBox contains innerBox. |
| 57 | + * @param outerBox The outer bounding box. |
| 58 | + * @param innerBox The inner bounding box. |
| 59 | + * @returns True if outerBox contains innerBox. |
| 60 | + */ |
| 61 | +export function outerBoxcontainsInnerBox(outerBox: BBox, innerBox: BBox): boolean { |
| 62 | + return ( |
| 63 | + outerBox.min.x <= innerBox.min.x && |
| 64 | + outerBox.min.y <= innerBox.min.y && |
| 65 | + outerBox.min.z <= innerBox.min.z && |
| 66 | + outerBox.max.x >= innerBox.max.x && |
| 67 | + outerBox.max.y >= innerBox.max.y && |
| 68 | + outerBox.max.z >= innerBox.max.z |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Converts a bounding box to an array of numbers. |
| 74 | + * The array contains the following numbers in the following order: |
| 75 | + * [min.x, min.y, min.z, max.x, max.y, max.z] |
| 76 | + * |
| 77 | + * @param box The bounding box. |
| 78 | + * @returns An array of numbers. |
| 79 | + */ |
| 80 | +export function toNumArray(box: BBox): [number, number, number, number, number, number] { |
| 81 | + return [box.min.x, box.min.y, box.min.z, box.max.x, box.max.y, box.max.z]; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Converts an array of numbers to a bounding box. |
| 86 | + * The array should contain the following numbers in the following order: |
| 87 | + * [min.x, min.y, min.z, max.x, max.y, max.z] |
| 88 | + * |
| 89 | + * @param array An array of numbers. |
| 90 | + * @returns A new bounding box. |
| 91 | + */ |
| 92 | +export function fromNumArray(array: [number, number, number, number, number, number]): BBox { |
| 93 | + return create(vec3.fromArray(array.slice(0, 3)), vec3.fromArray(array.slice(3, 6))); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Clones the given bounding box. |
| 98 | + * |
| 99 | + * @param box The bounding box to clone. |
| 100 | + * @returns A new bounding box. |
| 101 | + */ |
| 102 | +export function clone(box: BBox): BBox { |
| 103 | + return create(vec3.clone(box.min), vec3.clone(box.max)); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Combines the two bounding boxes into a new bounding box that contains both. |
| 108 | + * |
| 109 | + * @param box1 The first bounding box. |
| 110 | + * @param box2 The second bounding box. |
| 111 | + * |
| 112 | + * @returns A new bounding box that holds both bounding boxes. |
| 113 | + */ |
| 114 | +export function combine(box1: BBox, box2: BBox): BBox { |
| 115 | + return create( |
| 116 | + vec3.create( |
| 117 | + Math.min(box1.min.x, box2.min.x), |
| 118 | + Math.min(box1.min.y, box2.min.y), |
| 119 | + Math.min(box1.min.z, box2.min.z) |
| 120 | + ), |
| 121 | + vec3.create( |
| 122 | + Math.max(box1.max.x, box2.max.x), |
| 123 | + Math.max(box1.max.y, box2.max.y), |
| 124 | + Math.max(box1.max.z, box2.max.z) |
| 125 | + ) |
| 126 | + ); |
| 127 | +} |
0 commit comments