Skip to content

pex-gl/pex-geom

pex-geom

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

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

pex-geom
aabb
plane
ray
rect

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.

vec2 : module:pex-math~vec2
vec3 : module:pex-math~vec3
TypedArray : module:pex-math~TypedArray

pex-geom

pex-geom.aabb : aabb

Kind: static property of pex-geom

pex-geom.plane : plane

Kind: static property of pex-geom

pex-geom.ray : ray

Kind: static property of pex-geom

pex-geom.rect : rect

Kind: static property of pex-geom

aabb

aabb.create() ⇒ aabb

Creates a new bounding box.

Kind: static method of aabb

aabb.empty(a) ⇒ rect

Reset a bounding box.

Kind: static method of aabb

Param Type
a aabb

aabb.copy(a) ⇒ aabb

Copies a bounding box.

Kind: static method of aabb

Param Type
a aabb

aabb.set(a, b) ⇒ aabb

Sets a bounding box to another.

Kind: static method of aabb

Param Type
a aabb
b aabb

aabb.isEmpty(a) ⇒ boolean

Checks if a bounding box is empty.

Kind: static method of aabb

Param Type
a aabb

aabb.fromPoints(a, points) ⇒ aabb

Updates a bounding box from a list of points.

Kind: static method of aabb

Param Type
a aabb
points Array.<vec3> | TypedArray

aabb.getCorners(a, [points]) ⇒ Array.<vec3>

Returns a list of 8 points from a bounding box.

Kind: static method of aabb

Param Type
a aabb
[points] Array.<vec3>

aabb.center(a, out) ⇒ vec3

Returns the center of a bounding box.

Kind: static method of aabb

Param Type
a aabb
out vec3

aabb.size(a, out) ⇒ vec3

Returns the size of a bounding box.

Kind: static method of aabb

Param Type
a aabb
out vec3

aabb.containsPoint(a, p) ⇒ boolean

Checks if a point is inside a bounding box.

Kind: static method of aabb

Param Type
a aabb
p vec3

aabb.includeAABB(a, b) ⇒ aabb

Includes a bounding box in another.

Kind: static method of aabb

Param Type
a aabb
b aabb

aabb.includePoint(a, p, [i]) ⇒ vec3

Includes a point in a bounding box.

Kind: static method of aabb

Param Type Default Description
a aabb
p vec3
[i] number 0 offset in the point array

aabb.toString(a, [precision]) ⇒ string

Prints a bounding box to a string.

Kind: static method of aabb

Param Type Default
a aabb
[precision] number 4

plane

plane.Side : enum

Enum for different side values

Kind: static enum of plane Read only: true Properties

Name Type Description
OnPlane number Point lies on the plane
Same number Point is on the same side as the plane normal
Opposite number Point is on the opposite side of the plane normal

plane.create() ⇒ plane

Creates a new plane

Kind: static method of plane

plane.side(plane, point) ⇒ number

Returns on which side a point is.

Kind: static method of plane

Param Type
plane plane
point vec3

plane.toString(a, [precision]) ⇒ string

Prints a plane to a string.

Kind: static method of plane

Param Type Default
a plane
[precision] number 4

ray

ray.Intersections : enum

Enum for different intersections values

Kind: static enum of ray Read only: true Properties

Name Type Description
Intersect number Ray intersects the target
NoIntersect number Ray does not intersect the target
SamePlane number Ray lies on the same plane as the target
Parallel number Ray is parallel to the target
TriangleDegenerate number Triangle has zero area and cannot be tested

ray.create() ⇒ ray

Creates a new ray

Kind: static method of ray

ray.hitTestPlane(ray, plane, out) ⇒ number

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 vec3

ray.hitTestTriangle(ray, triangle, out) ⇒ number

Determines if a ray intersect a triangle and set intersection point

Kind: static method of ray See: https://web.archive.org/web/20200701173519/http://geomalgorithms.com/a06-_intersect-2.html#intersect3D_RayTriangle()

Param Type
ray ray
triangle triangle
out vec3

ray.hitTestAABB(ray, aabb) ⇒ boolean

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

ray.toString(a, [precision]) ⇒ string

Prints a plane to a string.

Kind: static method of ray

Param Type Default
a ray
[precision] number 4

rect

rect.create() ⇒ rect

Creates a new rectangle.

Kind: static method of rect

rect.empty(a) ⇒ rect

Reset a rectangle.

Kind: static method of rect

Param Type
a rect

rect.copy(a) ⇒ rect

Copies a rectangle.

Kind: static method of rect

Param Type
a rect

rect.set(a, b) ⇒ rect

Sets a rectangle to another.

Kind: static method of rect

Param Type
a rect
b rect

rect.isEmpty(a) ⇒ boolean

Checks if a rectangle is empty.

Kind: static method of rect

Param Type
a rect

rect.fromPoints(a, points) ⇒ rect

Updates a rectangle from a list of points.

Kind: static method of rect

Param Type
a rect
points Array.<vec2> | TypedArray

rect.getCorners(a, points) ⇒ Array.<vec2>

Returns a list of 4 points from a rectangle.

Kind: static method of rect

Param Type
a rect
points Array.<vec2>

rect.scale(a, n) ⇒ rect

Scales a rectangle.

Kind: static method of rect

Param Type
a rect
n number

rect.setSize(a, size) ⇒ rect

Sets the size of a rectangle using width and height.

Kind: static method of rect

Param Type
a rect
size vec2

rect.size(a, out) ⇒ vec2

Returns the size of a rectangle.

Kind: static method of rect

Param Type
a rect
out vec2

rect.width(a) ⇒ number

Returns the width of a rectangle.

Kind: static method of rect

Param Type
a rect

rect.height(a) ⇒ number

Returns the height of a rectangle.

Kind: static method of rect

Param Type
a rect

rect.aspectRatio(a) ⇒ number

Returns the aspect ratio of a rectangle.

Kind: static method of rect

Param Type
a rect

rect.setPosition(a, p) ⇒ rect

Sets the position of a rectangle.

Kind: static method of rect

Param Type
a rect
p vec2

rect.center(a, out) ⇒ rect

Returns the center of a rectangle.

Kind: static method of rect

Param Type
a rect
out vec2

rect.containsPoint(a, p) ⇒ boolean

Checks if a point is inside a rectangle.

Kind: static method of rect

Param Type
a rect
p vec2

rect.containsRect(a, b) ⇒ boolean

Checks if a rectangle is inside another rectangle.

Kind: static method of rect

Param Type
a rect
b rect

rect.includePoint(a, p) ⇒ rect

Includes a point in a rectangle.

Kind: static method of rect

Param Type
a rect
p vec2

rect.includeRect(a, b) ⇒ rect

Includes a rectangle in another rectangle.

Kind: static method of rect

Param Type
a rect
b rect

rect.mapPoint(a, p) ⇒ vec2

Maps a point into the dimensions of a rectangle.

Kind: static method of rect

Param Type
a rect
p vec2

rect.clampPoint(a, p) ⇒ vec2

Clamps a point into the dimensions of a rectangle.

Kind: static method of rect

Param Type
a rect
p vec2

rect.toString(a, [precision]) ⇒ string

Prints a rect to a string.

Kind: static method of rect

Param Type Default
a rect
[precision] number 4

aabb : Array.<Array.<number>>

An axis-aligned bounding box defined by two min and max 3D points (eg. [[minX, minY, minZ], [maxX, maxY, maxZ]]).

Kind: global typedef

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]]).

Kind: global typedef

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]).

Kind: global typedef

rect : Array.<Array.<number>>

A rectangle defined by two diagonally opposite 2D points (eg. [[minX, minY], [maxX, maxY]]).

Kind: global typedef

triangle : Array.<Array.<number>>

A triangle defined by three 3D points.

Kind: global typedef

vec2 : module:pex-math~vec2

Kind: global typedef

vec3 : module:pex-math~vec3

Kind: global typedef

TypedArray : module:pex-math~TypedArray

Kind: global typedef

License

MIT. See license file.

About

Geometry intersection and bounding volume helpers for PEX.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors