include/vecmat/geom3.h file

Closest-point, gap, ray, and AABB predicates.

No broadphase.

Degenerate input returns ok = false. A well-posed miss keeps ok true and hit false. A point on the positive normal side of a triangle has a positive gap. An interior query recovers the barycentric combination. Rays are origin + t * direction with t >= 0. A plane is dot(normal, x) = offset; the normal need not be unit.

Classes

struct vm_geom3_hit
Closest point on a triangle and its clamped barycentric coordinates.
struct vm_geom3_gap
Signed point-triangle gap and the two closest points.
struct vm_geom3_edge
Closest points and distance between two edges.
struct vm_aabb
Axis-aligned box.
struct vm_geom3_ray
Ray hit.
struct vm_geom3_plane
Signed point-plane distance.
struct vm_geom3_segment
Closest point on a segment a + t (b - a), t in [0, 1].
struct vm_geom3_spheres
Solid-ball overlap of two spheres.
struct vm_geom3_planes
Intersection line of two planes.

Functions

auto vm_geom3_barycentric(vector3* bary, const vector3* p, const vector3* a, const vector3* b, const vector3* c) -> bool
Unclamped barycentric coordinates of a point in the triangle plane.
auto vm_geom3_closest_triangle(vm_geom3_hit* hit, const vector3* p, const vector3* a, const vector3* b, const vector3* c) -> bool
Closest point on a triangle, with clamped barycentric coordinates.
auto vm_geom3_point_triangle_gap(vm_geom3_gap* out, const vector3* p, const vector3* a, const vector3* b, const vector3* c) -> bool
Signed gap from a point to a triangle, and the closest points.
auto vm_geom3_edge_edge(vm_geom3_edge* out, const vector3* p0, const vector3* p1, const vector3* q0, const vector3* q1) -> bool
Closest points and distance between two line segments.
auto vm_geom3_aabb_triangle(const vector3* a, const vector3* b, const vector3* c) -> vm_aabb
AABB of a triangle.
auto vm_geom3_aabb_overlap(const vm_aabb* a, const vm_aabb* b) -> bool
Closed AABB overlap test.
auto vm_geom3_cell_index(int* i, int* j, int* k, const vector3* p, const vm_grid3* g) -> bool
Cell index of a point in an existing grid extent.
auto vm_geom3_point_plane(vm_geom3_plane* out, const vector3* p, const vector3* normal, vm_float_t offset) -> bool
Signed distance from a point to the plane dot(normal, x) = offset.
auto vm_geom3_closest_segment(vm_geom3_segment* out, const vector3* p, const vector3* a, const vector3* b) -> bool
Closest point on the segment from a to b.
auto vm_geom3_ray_triangle(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vector3* a, const vector3* b, const vector3* c) -> bool
Two-sided ray-triangle hit (Möller–Trumbore).
auto vm_geom3_ray_aabb(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vm_aabb* box) -> bool
Ray against a closed AABB (slab method).
auto vm_geom3_ray_sphere(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vector3* center, vm_float_t radius) -> bool
Nearest sphere-surface hit with t >= 0.
auto vm_geom3_ray_plane(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vector3* normal, vm_float_t offset) -> bool
Ray against the plane dot(normal, x) = offset.
auto vm_geom3_sphere_sphere(vm_geom3_spheres* out, const vector3* c0, vm_float_t r0, const vector3* c1, vm_float_t r1) -> bool
Solid overlap of two spheres.
auto vm_geom3_plane_plane(vm_geom3_planes* out, const vector3* n0, vm_float_t offset0, const vector3* n1, vm_float_t offset1) -> bool
Intersection line of two planes dot(n, x) = offset.
auto vm_geom3_aabb_contains(const vm_aabb* box, const vector3* p) -> bool
Closed AABB contains a point.
auto vm_geom3_aabb_union(const vm_aabb* a, const vm_aabb* b) -> vm_aabb
Union of two closed AABBs.
auto vm_geom3_aabb_expand(const vm_aabb* box, const vector3* p) -> vm_aabb
Grow a closed AABB so it contains p.

Function documentation

bool vm_geom3_barycentric(vector3* bary, const vector3* p, const vector3* a, const vector3* b, const vector3* c)

Unclamped barycentric coordinates of a point in the triangle plane.

Returns false if the triangle is degenerate.

The point is projected onto the plane of (a, b, c). Weights need not lie in [0, 1].

bool vm_geom3_closest_triangle(vm_geom3_hit* hit, const vector3* p, const vector3* a, const vector3* b, const vector3* c)

Closest point on a triangle, with clamped barycentric coordinates.

Returns false if the triangle is degenerate (hit->ok is also false).

bool vm_geom3_point_triangle_gap(vm_geom3_gap* out, const vector3* p, const vector3* a, const vector3* b, const vector3* c)

Signed gap from a point to a triangle, and the closest points.

Returns false if the triangle is degenerate.

bool vm_geom3_edge_edge(vm_geom3_edge* out, const vector3* p0, const vector3* p1, const vector3* q0, const vector3* q1)

Closest points and distance between two line segments.

Returns false if either segment has zero length.

Includes the parallel-edge case.

bool vm_geom3_cell_index(int* i, int* j, int* k, const vector3* p, const vm_grid3* g)

Cell index of a point in an existing grid extent.

Returns false if the grid is invalid or the point is outside the extent.

Grid origin is the origin. Cell (i, j, k) covers [i dx, (i+1) dx) × [j dy, (j+1) dy) × [k dz, (k+1) dz). A point on the far boundary is assigned to the last cell. No broadphase object is created. Use vm_grid_cell for the linear index.

bool vm_geom3_point_plane(vm_geom3_plane* out, const vector3* p, const vector3* normal, vm_float_t offset)

Signed distance from a point to the plane dot(normal, x) = offset.

Returns false if normal is zero (out->ok is also false).

bool vm_geom3_closest_segment(vm_geom3_segment* out, const vector3* p, const vector3* a, const vector3* b)

Closest point on the segment from a to b.

Returns false if the segment has zero length.

bool vm_geom3_ray_triangle(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vector3* a, const vector3* b, const vector3* c)

Two-sided ray-triangle hit (Möller–Trumbore).

Returns false if the query is degenerate.

A degenerate triangle or a zero direction returns false. A miss of a real triangle returns true with hit false.

bool vm_geom3_ray_aabb(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vm_aabb* box)

Ray against a closed AABB (slab method).

Returns false if the query is degenerate.

An inverted box (min greater than max on any axis) or a zero direction returns false. A ray that starts inside hits at t = 0.

bool vm_geom3_ray_sphere(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vector3* center, vm_float_t radius)

Nearest sphere-surface hit with t >= 0.

Returns false if the query is degenerate.

A negative radius or a zero direction returns false. A ray that starts inside the ball hits the exit point.

bool vm_geom3_ray_plane(vm_geom3_ray* out, const vector3* origin, const vector3* direction, const vector3* normal, vm_float_t offset)

Ray against the plane dot(normal, x) = offset.

Returns false if the query is degenerate.

A zero normal or a zero direction returns false. A ray lying in the plane hits at t = 0.

bool vm_geom3_sphere_sphere(vm_geom3_spheres* out, const vector3* c0, vm_float_t r0, const vector3* c1, vm_float_t r1)

Solid overlap of two spheres.

Returns false if either radius is negative.

bool vm_geom3_plane_plane(vm_geom3_planes* out, const vector3* n0, vm_float_t offset0, const vector3* n1, vm_float_t offset1)

Intersection line of two planes dot(n, x) = offset.

Returns false if either normal is zero.

bool vm_geom3_aabb_contains(const vm_aabb* box, const vector3* p)

Closed AABB contains a point.

An inverted box contains nothing.

vm_aabb vm_geom3_aabb_union(const vm_aabb* a, const vm_aabb* b)

Union of two closed AABBs.

An inverted box is empty and is dropped. Two empty inputs return an inverted box.

vm_aabb vm_geom3_aabb_expand(const vm_aabb* box, const vector3* p)

Grow a closed AABB so it contains p.

An inverted or null box becomes the point box.