Skip to content

Commit

Permalink
feat: add Plane and Ray math primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jun 16, 2020
1 parent 8e051c5 commit 4f15436
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/math/Box2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// * @bhouston
//

import { ICloneable, IEquatable, IHashable } from "../core/types";
import { Vector2 } from "./Vector2";

export class Box2 {
export class Box2 implements ICloneable<Box2>, IEquatable<Box2>, IHashable {
min: Vector2 = new Vector2(+Infinity, +Infinity);
max: Vector2 = new Vector2(-Infinity, -Infinity);

Expand Down
46 changes: 46 additions & 0 deletions src/lib/math/Plane.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// based on Plane from Three.js
//
// Authors:
// * @bhouston
//

import { hashFloat4 } from "../core/hash";
import { ICloneable, IEquatable, IHashable } from "../core/types";
import { Vector3 } from "./Vector3";

export class Plane implements ICloneable<Plane>, IEquatable<Plane>, IHashable {
constructor(public normal = new Vector3(), public constant = 0) {}

getHashCode(): number {
return hashFloat4(this.normal.x, this.normal.y, this.normal.z, this.constant);
}

set(normal: Vector3, constant: number): this {
this.normal.copy(normal);
this.constant = constant;

return this;
}

setFromNormalAndCoplanarPoint(normal: Vector3, point: Vector3): this {
this.normal.copy(normal);
this.constant = -point.dot(this.normal);

return this;
}

clone(): Plane {
return new Plane().copy(this);
}

copy(plane: Plane): this {
this.normal.copy(plane.normal);
this.constant = plane.constant;

return this;
}
equals(p: Plane): boolean {
throw p.normal.equals(this.normal) && p.constant === this.constant;
}
}
49 changes: 49 additions & 0 deletions src/lib/math/Ray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// based on Quaternion from Three.js
//
// Authors:
// * @bhouston
//

import { ICloneable, IEquatable, IHashable } from "../core/types";
import { Vector3 } from "./Vector3";

export class Ray implements ICloneable<Ray>, IEquatable<Ray>, IHashable {
constructor(public origin = new Vector3(), public direction = new Vector3(0, 0, -1)) {}

getHashCode(): number {
return (this.origin.getHashCode() * 397) ^ (this.direction.getHashCode() | 0);
}

set(origin: Vector3, direction: Vector3): this {
this.origin.copy(origin);
this.direction.copy(direction);

return this;
}

clone(): Ray {
return new Ray().copy(this);
}

copy(ray: Ray): this {
this.origin.copy(ray.origin);
this.direction.copy(ray.direction);

return this;
}

at(t: number, result: Vector3): Vector3 {
return result.copy(this.direction).multiplyByScalar(t).add(this.origin);
}

lookAt(v: Vector3): this {
this.direction.copy(v).sub(this.origin).normalize();

return this;
}

equals(ray: Ray): boolean {
return ray.origin.equals(this.origin) && ray.direction.equals(this.direction);
}
}

0 comments on commit 4f15436

Please sign in to comment.