aabb-tree
Version:
Basic implementation of the AABB-Tree (Axis Aligned Box Bounding Tree)
74 lines (73 loc) • 2.39 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const AABB_1 = __importDefault(require("../AABB"));
class PolygonalPrism {
constructor(shape, depth) {
this.shape = shape;
this.depth = depth;
if (shape.length < 2) {
throw new Error("The polygon can't be a line or a point");
}
}
GetAABB() {
let maxX = -Infinity;
let minX = Infinity;
let maxY = -Infinity;
let minY = Infinity;
this.shape.forEach(point => {
if (point.X > maxX) {
maxX = point.X;
}
if (point.X < minX) {
minX = point.X;
}
if (point.Y > maxY) {
maxY = point.Y;
}
if (point.Y < minY) {
minY = point.Y;
}
});
return new AABB_1.default(minX, minY, this.shape[0].Z, maxX, maxY, this.shape[0].Z + this.depth);
}
ContainsPoint(rayStart) {
if (rayStart.Z > this.shape[0].Z || rayStart.Z < this.shape[0].Z - this.depth) {
return false;
}
let hitCount = 0;
for (let i = 0; i < this.shape.length; i++) {
const point1 = this.shape[i];
const point2 = this.shape[(i + 1) % this.shape.length];
if (this.checkForHit(point1, point2, rayStart)) {
hitCount++;
}
}
return hitCount > 0 && hitCount % 2 === 1;
}
checkForHit(point1, point2, rayStart) {
if (point1.Y <= point2.Y) {
if (rayStart.Y <= point1.Y || rayStart.Y > point2.Y || (rayStart.X >= point1.X && rayStart.X >= point2.X)) {
return false;
}
else if (rayStart.X < point1.X && rayStart.X < point2.X) {
return true;
}
else {
return (rayStart.Y - point1.Y) / (rayStart.X - point1.X) > (point2.Y - point1.Y) / (point2.X - point1.X);
}
}
else {
return this.checkForHit(point2, point1, rayStart);
}
}
get Shape() {
return this.shape;
}
get Depth() {
return this.depth;
}
}
exports.default = PolygonalPrism;