BB.Collision.Mesh (bb v0.15.2)

Copy Markdown View Source

Mesh loading and bounding geometry computation for collision detection.

This module provides basic mesh support for collision detection by computing bounding primitives (spheres or AABBs) from mesh vertices. Triangle-level collision detection is not supported - meshes are approximated by their bounding geometry.

Currently supports:

  • Binary STL files
  • ASCII STL files

Usage

# Load mesh and compute bounds
{:ok, bounds} = BB.Collision.Mesh.load_bounds("/path/to/model.stl")

# bounds contains:
%{
  aabb: {min_vec3, max_vec3},
  bounding_sphere: {centre_vec3, radius}
}

Caching

Mesh bounds are cached in an ETS table to avoid repeated file parsing. The cache key includes the file path and modification time.

Summary

Functions

Clear the mesh bounds cache.

Compute bounding geometry from a list of vertices.

Load mesh bounds from a file.

Load mesh bounds, raising on error.

Types

bounds()

@type bounds() :: %{
  aabb: {BB.Math.Vec3.t(), BB.Math.Vec3.t()},
  bounding_sphere: {BB.Math.Vec3.t(), float()}
}

Functions

clear_cache()

@spec clear_cache() :: :ok

Clear the mesh bounds cache.

compute_bounds(vertices)

@spec compute_bounds([{float(), float(), float()}]) ::
  {:ok, bounds()} | {:error, :empty_mesh}

Compute bounding geometry from a list of vertices.

Each vertex should be a {x, y, z} tuple of floats.

load_bounds(path)

@spec load_bounds(String.t()) :: {:ok, bounds()} | {:error, term()}

Load mesh bounds from a file.

Parses the mesh file, computes bounding geometry, and caches the result. Subsequent calls with the same file (unchanged) return cached bounds.

Returns {:ok, bounds} or {:error, reason}.

load_bounds!(path)

@spec load_bounds!(String.t()) :: bounds()

Load mesh bounds, raising on error.