pex-geom
Geometry intersection and bounding volume helpers for PEX.
Installation
npm install pex-geom
Usage
import { ray, aabb } from "pex-geom";
const box = aabb.fromPoints([
[-1, -1, -1],
[1, 1, 1],
]);
const intersect = ray.hitTestAABB(
[
[0, 0, 0],
[0, 1, 0],
],
box
);
console.log(intersect);
// => true
API
Modules
Typedefs
-
aabb :
Array.<Array.<number>>
-
An axis-aligned bounding box defined by two min and max 3D points (eg. [[minX, minY, minZ], [maxX, maxY, maxZ]]).
-
plane :
Array.<Array.<number>>
-
A plane defined by a 3D point and a normal vector perpendicular to the plane’s surface (eg. [[pointX, pointY, pointZ], [normalX, normalY, normalZ]]).
-
ray :
Array.<Array.<number>>
-
A ray defined by a starting 3D point origin and a 3D direction vector (eg. [[originX, originY, originZ], [directionX, directionY, directionZ]).
-
rect :
Array.<Array.<number>>
-
A rectangle defined by two diagonally opposite 2D points (eg. [[minX, minY], [maxX, maxY]]).
-
triangle :
Array.<Array.<number>>
-
A triangle defined by three 3D points.
aabb
-
aabb
-
.create() ⇒
aabb
-
.empty(a) ⇒
rect
-
.copy(a) ⇒
aabb
-
.set(a, b) ⇒
aabb
-
.isEmpty(a) ⇒
boolean
-
.fromPoints(a, points) ⇒
aabb
-
.getCorners(a, [points]) ⇒
Array.<module:pex-math/types/types~vec3>
-
.center(a, out) ⇒
module:pex-math/types/types~vec3
-
.size(a, out) ⇒
module:pex-math/types/types~vec3
-
.containsPoint(a, p) ⇒
boolean
-
.includeAABB(a, b) ⇒
aabb
-
.includePoint(a, p, [i]) ⇒
module:pex-math/types/types~vec3
-
.toString(a, [precision]) ⇒
string
-
.create() ⇒
aabb
aabb.create() ⇒ Creates a new bounding box.
Kind: static method of aabb
rect
aabb.empty(a) ⇒ Reset a bounding box.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
aabb
aabb.copy(a) ⇒ Copies a bounding box.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
aabb
aabb.set(a, b) ⇒ Sets a bounding box to another.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
b | aabb |
boolean
aabb.isEmpty(a) ⇒ Checks if a bounding box is empty.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
aabb
aabb.fromPoints(a, points) ⇒ Updates a bounding box from a list of points.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
points |
Array.<module:pex-math/types/types | module:pex-math/types/typesTypedArray
|
Array.<module:pex-math/types/types~vec3>
aabb.getCorners(a, [points]) ⇒ Returns a list of 8 points from a bounding box.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
[points] | Array.<module:pex-math/types/types~vec3> |
module:pex-math/types/types~vec3
aabb.center(a, out) ⇒ Returns the center of a bounding box.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
out | module:pex-math/types/types~vec3 |
module:pex-math/types/types~vec3
aabb.size(a, out) ⇒ Returns the size of a bounding box.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
out | module:pex-math/types/types~vec3 |
boolean
aabb.containsPoint(a, p) ⇒ Checks if a point is inside a bounding box.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
p | module:pex-math/types/types~vec3 |
aabb
aabb.includeAABB(a, b) ⇒ Includes a bounding box in another.
Kind: static method of aabb
Param | Type |
---|---|
a | aabb |
b | aabb |
module:pex-math/types/types~vec3
aabb.includePoint(a, p, [i]) ⇒ Includes a point in a bounding box.
Kind: static method of aabb
Param | Type | Default | Description |
---|---|---|---|
a | aabb |
||
p | module:pex-math/types/types~vec3 |
||
[i] | number |
0 |
offset in the point array |
string
aabb.toString(a, [precision]) ⇒ Prints a bounding box to a string.
Kind: static method of aabb
Param | Type | Default |
---|---|---|
a | aabb |
|
[precision] | number |
4 |
index
Re-export aabb, plane, ray and rect
plane
-
plane
-
.Side :
enum
-
.create() ⇒
plane
-
.side(plane, point) ⇒
number
-
.toString(a, [precision]) ⇒
string
-
.Side :
enum
plane.Side : Enum for different side values
Kind: static enum of plane
Read only: true
plane
plane.create() ⇒ Creates a new plane
Kind: static method of plane
number
plane.side(plane, point) ⇒ Returns on which side a point is.
Kind: static method of plane
Param | Type |
---|---|
plane | plane |
point | module:pex-math/types/types~vec3 |
string
plane.toString(a, [precision]) ⇒ Prints a plane to a string.
Kind: static method of plane
Param | Type | Default |
---|---|---|
a | plane |
|
[precision] | number |
4 |
ray
-
ray
-
.Intersections :
enum
-
.create() ⇒
ray
-
.hitTestPlane(ray, plane, out) ⇒
number
-
.hitTestTriangle(ray, triangle, out) ⇒
number
-
.hitTestAABB(ray, aabb) ⇒
boolean
- .intersectsAABB()
-
.toString(a, [precision]) ⇒
string
-
.Intersections :
enum
ray.Intersections : Enum for different intersections values
Kind: static enum of ray
Read only: true
ray
ray.create() ⇒ Creates a new ray
Kind: static method of ray
number
ray.hitTestPlane(ray, plane, out) ⇒ Determines if a ray intersect a plane and set intersection point
Kind: static method of ray
See: https://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld017.htm
Param | Type |
---|---|
ray | ray |
plane | plane |
out | module:pex-math/types/types~vec3 |
number
ray.hitTestTriangle(ray, triangle, out) ⇒ Determines if a ray intersect a triangle and set intersection point
Kind: static method of ray
See: http://geomalgorithms.com/a06-_intersect-2.html#intersect3D_RayTriangle()
Param | Type |
---|---|
ray | ray |
triangle | triangle |
out | module:pex-math/types/types~vec3 |
boolean
ray.hitTestAABB(ray, aabb) ⇒ Determines if a ray intersect an AABB bounding box
Kind: static method of ray
See: http://gamedev.stackexchange.com/questions/18436/most-efficient-aabb-vs-ray-collision-algorithms
Param | Type |
---|---|
ray | ray |
aabb | aabb |
ray.intersectsAABB()
Alias for hitTestAABB
Kind: static method of ray
string
ray.toString(a, [precision]) ⇒ Prints a plane to a string.
Kind: static method of ray
Param | Type | Default |
---|---|---|
a | ray |
|
[precision] | number |
4 |
rect
-
rect
-
.create() ⇒
rect
-
.empty(a) ⇒
rect
-
.copy(a) ⇒
rect
-
.set(a, b) ⇒
rect
-
.isEmpty(a) ⇒
boolean
-
.fromPoints(a, points) ⇒
rect
-
.getCorners(a, points) ⇒
Array.<module:pex-math/types/types~vec2>
-
.scale(a, n) ⇒
rect
-
.setSize(a, size) ⇒
rect
-
.size(a, out) ⇒
module:pex-math/types/types~vec2
-
.width(a) ⇒
number
-
.height(a) ⇒
number
-
.aspectRatio(a) ⇒
number
-
.setPosition(a, p) ⇒
rect
-
.center(a, out) ⇒
rect
-
.containsPoint(a, p) ⇒
boolean
-
.containsRect(a, b) ⇒
boolean
-
.includePoint(a, p) ⇒
rect
-
.includeRect(a, b) ⇒
rect
-
.mapPoint(a, p) ⇒
module:pex-math/types/types~vec2
-
.clampPoint(a, p) ⇒
module:pex-math/types/types~vec2
-
.toString(a, [precision]) ⇒
string
-
.create() ⇒
rect
rect.create() ⇒ Creates a new rectangle.
Kind: static method of rect
rect
rect.empty(a) ⇒ Reset a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
rect
rect.copy(a) ⇒ Copies a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
rect
rect.set(a, b) ⇒ Sets a rectangle to another.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
b | rect |
boolean
rect.isEmpty(a) ⇒ Checks if a rectangle is empty.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
rect
rect.fromPoints(a, points) ⇒ Updates a rectangle from a list of points.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
points |
Array.<module:pex-math/types/types | module:pex-math/types/typesTypedArray
|
Array.<module:pex-math/types/types~vec2>
rect.getCorners(a, points) ⇒ Returns a list of 4 points from a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
points | Array.<module:pex-math/types/types~vec2> |
rect
rect.scale(a, n) ⇒ Scales a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
n | number |
rect
rect.setSize(a, size) ⇒ Sets the size of a rectangle using width and height.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
size | module:pex-math/types/types~vec2 |
module:pex-math/types/types~vec2
rect.size(a, out) ⇒ Returns the size of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
out | module:pex-math/types/types~vec2 |
number
rect.width(a) ⇒ Returns the width of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
number
rect.height(a) ⇒ Returns the height of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
number
rect.aspectRatio(a) ⇒ Returns the aspect ratio of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
rect
rect.setPosition(a, p) ⇒ Sets the position of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
p | module:pex-math/types/types~vec2 |
rect
rect.center(a, out) ⇒ Returns the center of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
out | module:pex-math/types/types~vec2 |
boolean
rect.containsPoint(a, p) ⇒ Checks if a point is inside a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
p | module:pex-math/types/types~vec2 |
boolean
rect.containsRect(a, b) ⇒ Checks if a rectangle is inside another rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
b | rect |
rect
rect.includePoint(a, p) ⇒ Includes a point in a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
p | module:pex-math/types/types~vec2 |
rect
rect.includeRect(a, b) ⇒ Includes a rectangle in another rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
b | rect |
module:pex-math/types/types~vec2
rect.mapPoint(a, p) ⇒ Maps a point into the dimensions of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
p | module:pex-math/types/types~vec2 |
module:pex-math/types/types~vec2
rect.clampPoint(a, p) ⇒ Clamps a point into the dimensions of a rectangle.
Kind: static method of rect
Param | Type |
---|---|
a | rect |
p | module:pex-math/types/types~vec2 |
string
rect.toString(a, [precision]) ⇒ Prints a rect to a string.
Kind: static method of rect
Param | Type | Default |
---|---|---|
a | rect |
|
[precision] | number |
4 |
Array.<Array.<number>>
aabb : An axis-aligned bounding box defined by two min and max 3D points (eg. [[minX, minY, minZ], [maxX, maxY, maxZ]]).
Array.<Array.<number>>
plane : A plane defined by a 3D point and a normal vector perpendicular to the plane’s surface (eg. [[pointX, pointY, pointZ], [normalX, normalY, normalZ]]).
Array.<Array.<number>>
ray : A ray defined by a starting 3D point origin and a 3D direction vector (eg. [[originX, originY, originZ], [directionX, directionY, directionZ]).
Array.<Array.<number>>
rect : A rectangle defined by two diagonally opposite 2D points (eg. [[minX, minY], [maxX, maxY]]).
Array.<Array.<number>>
triangle : A triangle defined by three 3D points.
Kind: global typedef
License
MIT. See license file.