affn
Affine geometry primitives for strongly-typed coordinate systems.
affn is a small, domain-agnostic geometry kernel for scientific/engineering software. It provides:
- Reference centers (
C): the origin of a coordinate system (optionally parameterized viaC::Params) - Reference frames (
F): the orientation of axes - Typed coordinates: Cartesian and spherical positions and directions
- Units: distances/lengths are carried via
qttyunits at the type level
The goal is to make invalid operations (like adding two positions) fail at compile time.
Quick Start
Add the dependency:
[]
= "0.2"
= "0.2"
Define a center + frame and do basic affine algebra:
use ;
use ReferenceCenter;
use ReferenceFrame;
use *;
;
;
let a = new;
let b = new;
// Position - Position -> Displacement
let d: = b - a;
assert!;
// Position + Displacement -> Position
let c = a + d;
assert!;
Core Concepts
Position<C, F, U>: an affine point (depends on both center and frame)Direction<F>: a unit vector (frame-only, translation-invariant)Vector<F, U>/Displacement<F, U>/Velocity<F, U>: free vectors (frame-only)
The type system enforces the usual affine rules:
- ✅
Position - Position -> Displacement - ✅
Position + Displacement -> Position - ❌
Position + Position(does not compile)
Defining Custom Systems (Derive Macros)
For zero-sized marker types, use the derive macros re-exported by the crate:
use *;
;
;
Some centers need runtime parameters (e.g. “topocentric” depends on an observer):
use *;
;
Spherical ↔ Cartesian
Cartesian and spherical positions can be converted losslessly (up to floating point error):
use Position as CPos;
use Position as SPos;
use ReferenceCenter;
use ReferenceFrame;
use *;
;
;
let cart = new;
let sph: = cart.to_spherical;
let back: = from_spherical;
assert!;
Examples
Run the included examples:
cargo run --example basic_cartesiancargo run --example parameterized_centercargo run --example spherical_roundtrip
License
Licensed under AGPL-3.0-only. See LICENSE.