UNPKG

aabb-tree

Version:

Basic implementation of the AABB-Tree (Axis Aligned Box Bounding Tree)

84 lines (83 loc) 3.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Vector3_1 = __importDefault(require("./Vector3")); const Vector2_1 = __importDefault(require("./Vector2")); class AABB { constructor(minX, minY, minZ, maxX, maxY, maxZ) { const MinX = Math.min(minX, maxX); const MaxX = Math.max(minX, maxX); const MinY = Math.min(minY, maxY); const MaxY = Math.max(minY, maxY); if (minZ !== null && maxZ !== null) { const MinZ = Math.min(minZ, maxZ); const MaxZ = Math.max(minZ, maxZ); this.lowPoint = new Vector3_1.default(MinX, MinY, MinZ); this.highPoint = new Vector3_1.default(MaxX, MaxY, MaxZ); } else { this.lowPoint = new Vector2_1.default(MinX, MinY); this.highPoint = new Vector2_1.default(MaxX, MaxY); } } Merge(other) { return new AABB(Math.min(this.lowPoint.X, other.lowPoint.X), Math.min(this.lowPoint.Y, other.lowPoint.Y), this.is3D ? Math.min(this.lowPoint.Z, other.lowPoint.Z) : null, Math.max(this.highPoint.X, other.highPoint.X), Math.max(this.highPoint.Y, other.highPoint.Y), this.is3D ? Math.min(this.highPoint.Z, other.highPoint.Z) : null); } Overlaps(other) { if (this.is3D) { return (this.highPoint.X > other.lowPoint.X && this.lowPoint.X < other.highPoint.X && this.highPoint.Y > other.lowPoint.Y && this.lowPoint.Y < other.highPoint.Y && this.highPoint.Z > other.lowPoint.Z && this.lowPoint.Z < other.highPoint.Z); } else { return (this.highPoint.X > other.lowPoint.X && this.lowPoint.X < other.highPoint.X && this.highPoint.Y > other.lowPoint.Y && this.lowPoint.Y < other.highPoint.Y); } } ContainsPoint(point) { if (this.is3D) { return this.lowPoint.X < point.X && this.highPoint.X > point.X && this.lowPoint.Y < point.Y && this.highPoint.Y > point.Y && point instanceof Vector3_1.default ? this.lowPoint.Z < point.Z : true && point instanceof Vector3_1.default ? this.highPoint.Z > point.Z : true; } else { return (this.lowPoint.X < point.X && this.highPoint.X > point.X && this.lowPoint.Y < point.Y && this.highPoint.Y > point.Y); } } get is3D() { return this.lowPoint instanceof Vector3_1.default; } get Width() { return this.highPoint.X - this.lowPoint.X; } get Height() { return this.highPoint.Y - this.lowPoint.Y; } get Depth() { if (!this.is3D) { return 0; } return this.highPoint.Z - this.lowPoint.Z; } get Space() { const area = this.Width * this.Height; return this.is3D ? area * this.Depth : area; } } exports.default = AABB;