-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Plane and Ray math primitives
- Loading branch information
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |