Apollonius 🌌
Apollonius is a lightweight, high-performance N-dimensional geometry library for Rust. It provides the mathematical and structural foundations for physics engines, collision detection systems, and spatial simulations using const generics.
📐 Library architecture (Mermaid)
Logical flow and relationships between modules. Algebra is the base; primitives build on it and share the SpatialRelation / Bounded / IntersectionResult API; space and utils support transforms and float handling.
flowchart TB
subgraph ROOT["apollonius"]
direction TB
subgraph ALGEBRA["algebra (foundation)"]
direction TB
POINTS["points.rs<br/>Point, Point2D, Point3D<br/>MetricSquared, EuclideanMetric"]
VECTORS["vectors.rs<br/>Vector, Vector2D, Vector3D<br/>VectorMetricSquared, EuclideanVector"]
MATRIX["matrix.rs<br/>Matrix<T,N,Tag> (General, Isometry, Affine)<br/>MatrixTag, IsAffine, IsIsometry"]
ANGLE["angle.rs<br/>Angle<br/>radians, degrees, sin, cos, tan"]
POINTS --> VECTORS
VECTORS --> MATRIX
end
subgraph PRIMITIVES["primitives (shapes + API)"]
direction TB
TRAITS["mod.rs<br/>SpatialRelation (closest_point, distance, contains)<br/>Bounded (aabb)<br/>IntersectionResult enum"]
LINE["line.rs<br/>Line (origin + direction)"]
SEG["segment.rs<br/>Segment (start + end)"]
AABB["aabb.rs<br/>AABB (min, max)"]
SPHERE["hypersphere.rs<br/>Hypersphere / Circle / Sphere"]
PLANE["hyperplane.rs<br/>Hyperplane (origin + normal)"]
TRI["triangle.rs<br/>Triangle (a, b, c)"]
TRAITS --> LINE
TRAITS --> SEG
TRAITS --> SPHERE
TRAITS --> PLANE
TRAITS --> TRI
TRAITS --> AABB
end
subgraph SPACE["space"]
LINEAR["Matrix × Point/Vector<br/>linear_ops: × Line, Segment, Hypersphere, Hyperplane, Triangle"]
AFFINE["AffineTransform (linear + translation)"]
LINEAR --> AFFINE
end
UTILS["utils.rs<br/>FloatSign, classify_to_zero"]
end
ALGEBRA -->|"Point, Vector"| PRIMITIVES
ALGEBRA -->|"Matrix, Vector"| SPACE
UTILS -->|"tolerance"| PRIMITIVES
UTILS -->|"tolerance"| ALGEBRA
subgraph FLOW["Typical usage flow"]
direction LR
F1["1. Build geometry<br/>(Point, Vector, Line, …)"]
F2["2. SpatialRelation / Bounded<br/>(closest_point, aabb, …)"]
F3["3. Intersection methods<br/>(intersect_*, IntersectionResult)"]
F1 --> F2 --> F3
end
- Algebra: All types use
T: Float(num_traits). Points and vectors are the coordinate types; Matrix (with type tagsGeneral,Isometry,Affineand traitsMatrixTag,IsAffine,IsIsometry) and Angle extend the toolbox. - Primitives: Each shape implements
SpatialRelation(and oftenBounded). Intersections between them returnIntersectionResult<T, N>(None, Tangent, Secant, Collinear, Single, HalfSpacePenetration). - Space: AffineTransform (linear part + translation); linear_ops:
Matrix × Point/VectorandMatrix/AffineTransform× Line, Segment, Hypersphere, Hyperplane, Triangle. Isometry tag required for hypersphere (radius preserved). - Utils:
classify_to_zeroandFloatSignfor robust float comparisons in primitives and algebra.
✨ Key Features
- N-Dimensional Support: Type-safe coordinates and vectors for 2D, 3D, and higher-dimensional spaces using Rust's
const generics. - Efficient Primitives:
- Hyperspheres: (Circles, Spheres, N-Spheres) with plane intersection and submerged volume ratio.
- Lines & Segments: Infinite lines and finite segments with parametric evaluation, projection, and full intersection APIs.
- Hyperplanes: Half-space queries, signed distance, and intersection with lines, segments, and hyperspheres.
- Triangles: N-dimensional triangles with centroid, area (Lagrange identity), and AABB.
- Broad-Phase Foundations: Native support for AABB (Axis-Aligned Bounding Boxes) with optimized overlap theorems.
- Unified Intersection Engine: A single
IntersectionResulttype covering:- None, Tangent(point), Secant(p1, p2), Collinear, Single(point) for point-like contacts.
- HalfSpacePenetration(depth) for hypersphere–hyperplane penetration.
- Point-to-Point Intersections: Line∩Line, Line∩Segment, Line∩Hypersphere, Line∩Hyperplane; Segment∩Segment, Segment∩Hypersphere, Segment∩Hyperplane, Segment∩Line; Hyperplane∩Line, Hyperplane∩Segment, Hyperplane∩Hypersphere; Hypersphere∩Line, Hypersphere∩Segment, Hypersphere∩Hyperplane.
- Numerical Stability: Robust floating-point classification via
classify_to_zeroandFloatSignto handle accumulation errors. - Type-safe matrices: Tags
General,Isometry,Affineand traitsMatrixTag,IsAffine,IsIsometryso you can restrict functions to specific matrix kinds (e.g. only isometries acting on hyperspheres). - Prelude and re-exports: All main types and traits are re-exported at the crate root. Use
use apollonius::prelude::*for one-shot imports (points, vectors, matrices, primitives, metric traits).
🛠 Technical Stack
- Language: Rust (Stable)
- Math Traits:
num-traitsfor generic support overf32andf64. - Core Philosophy: Minimal dependencies; core logic is independent of rendering or external physics frameworks.
📦 Installation
Add this to your Cargo.toml:
[]
= "0.1"
Optional serde for serialization:
= { = "0.1", = ["serde"] }
Importing: Use the crate root or the prelude. All core types and traits are re-exported:
// Explicit imports from the crate root
use ;
// Or bring in the most used items in one go (includes metric traits for distances/magnitudes)
use *;
📖 Quick Example: Line–Hypersphere Intersection
use ;
let line = new;
let sphere = new;
match line.intersect_hypersphere
📖 Matrices and affine transforms
Matrices are type-tagged: General (any N×N), Isometry (rotations; preserve distances), Affine (used in affine context). You can multiply them by points, vectors, and by primitives (Line, Segment, Hypersphere, Hyperplane, Triangle). AffineTransform = linear part + translation.
Matrix × Point and Matrix × Vector
use ;
// Identity leaves point and vector unchanged
let id = identity;
let p = new;
let v = new;
assert_eq!;
assert_eq!;
// General 2×2 matrix: row-major construction
let m = new;
let out = m * new;
assert_eq!;
2D rotation (Isometry)
use ;
use FRAC_PI_2;
// Rotate 90° counterclockwise in the plane
let rot = rotation_2d;
let v = new;
let rotated = rot * v;
// (1, 0) → (0, 1)
assert!;
assert!;
AffineTransform: translate and rotate
use ;
let linear = rotation_2d;
let translation = new;
let tr = new;
let line = new;
let transformed = tr * line;
// Origin and direction are transformed; direction is re-normalized
println!;
Matrix × Hypersphere (isometries preserve radius)
Only isometric matrices (and affine transforms with isometric linear part) can act on hyperspheres, so the radius is preserved:
use ;
let rot = rotation_2d;
let circle = new;
let rotated_circle = rot * circle;
assert_eq!; // unchanged
// Center is rotated: rot * circle.center()
Constraining functions by matrix kind
Use the traits MatrixTag, IsAffine, or IsIsometry to restrict generic functions. The type system ensures only isometries act on hyperspheres (radius is preserved):
use ;
/// Only matrices with tag implementing IsIsometry (e.g. Isometry) are accepted.
📖 Example: Hypersphere–Hyperplane (Tangent vs Penetration)
use ;
let sphere = new;
let plane = new;
match sphere.intersect_hyperplane
📖 Example: Segment, AABB, and closest point
use *;
let seg = new;
let aabb = seg.aabb;
assert_eq!;
assert_eq!;
let query = new;
let closest = seg.closest_point;
assert_eq!; // foot on segment
let dist_sq = seg.distance_to_point_squared;
assert!;
🛰 Roadmap
- N-dimensional Point & Vector algebra.
- Core primitives (Hypersphere, Line, Segment, Hyperplane, AABB, Triangle).
- AABB broad-phase overlap.
- Point-result intersections: Line/Segment with Line, Segment, Hypersphere, Hyperplane; Hyperplane with Line, Segment, Hypersphere; Hypersphere with Line, Segment, Hyperplane.
- Hypersphere–Hyperplane: tangent contact, half-space penetration,
submerged_ratio. - Documentation and doc tests.
- v0.1.0: Type-tagged matrices (General, Isometry, Affine), AffineTransform, linear_ops (Matrix/AffineTransform × Point, Vector, Line, Segment, Hypersphere, Hyperplane, Triangle). Prelude and crate-root re-exports.
- GJK (Gilbert–Johnson–Keerthi) for narrow-phase.
- Oriented Bounding Boxes (OBB).
- Spatial partitioning (BVH / quadtree).
📝 License
This project is licensed under the MIT License.
Developed with 🦀 by Mauricio Klainbard