| Crates.io | affn |
| lib.rs | affn |
| version | 0.2.0 |
| created_at | 2025-12-21 01:17:33.976437+00 |
| updated_at | 2025-12-21 18:34:03.535174+00 |
| description | Affine geometry primitives: strongly-typed coordinate systems, reference frames, and centers for scientific computing. |
| homepage | |
| repository | https://github.com/Siderust/affn |
| max_upload_size | |
| id | 1997193 |
| size | 211,722 |
Affine geometry primitives for strongly-typed coordinate systems.
affn is a small, domain-agnostic geometry kernel for scientific/engineering software. It provides:
C): the origin of a coordinate system (optionally parameterized via C::Params)F): the orientation of axesqtty units at the type levelThe goal is to make invalid operations (like adding two positions) fail at compile time.
Add the dependency:
[dependencies]
affn = "0.2"
qtty = "0.2"
Define a center + frame and do basic affine algebra:
use affn::cartesian::{Displacement, Position};
use affn::centers::ReferenceCenter;
use affn::frames::ReferenceFrame;
use qtty::*;
#[derive(Debug, Copy, Clone)]
struct World;
impl ReferenceFrame for World {
fn frame_name() -> &'static str { "World" }
}
#[derive(Debug, Copy, Clone)]
struct Origin;
impl ReferenceCenter for Origin {
type Params = ();
fn center_name() -> &'static str { "Origin" }
}
let a = Position::<Origin, World, Meter>::new(0.0, 0.0, 0.0);
let b = Position::<Origin, World, Meter>::new(3.0, 4.0, 0.0);
// Position - Position -> Displacement
let d: Displacement<World, Meter> = b - a;
assert!((d.magnitude().value() - 5.0).abs() < 1e-12);
// Position + Displacement -> Position
let c = a + d;
assert!((c.y().value() - 4.0).abs() < 1e-12);
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 -> DisplacementPosition + Displacement -> PositionPosition + Position (does not compile)For zero-sized marker types, use the derive macros re-exported by the crate:
use affn::prelude::*;
#[derive(Debug, Copy, Clone, ReferenceFrame)]
struct MyFrame;
#[derive(Debug, Copy, Clone, ReferenceCenter)]
struct MyCenter;
Some centers need runtime parameters (e.g. “topocentric” depends on an observer):
use affn::prelude::*;
#[derive(Clone, Debug, Default, PartialEq)]
struct Observer {
lat_deg: f64,
lon_deg: f64,
}
#[derive(Debug, Copy, Clone, ReferenceCenter)]
#[center(params = Observer)]
struct Topocentric;
Cartesian and spherical positions can be converted losslessly (up to floating point error):
use affn::cartesian::Position as CPos;
use affn::spherical::Position as SPos;
use affn::centers::ReferenceCenter;
use affn::frames::ReferenceFrame;
use qtty::*;
#[derive(Debug, Copy, Clone)]
struct Frame;
impl ReferenceFrame for Frame { fn frame_name() -> &'static str { "Frame" } }
#[derive(Debug, Copy, Clone)]
struct Center;
impl ReferenceCenter for Center { type Params = (); fn center_name() -> &'static str { "Center" } }
let cart = CPos::<Center, Frame, Meter>::new(1.0, 1.0, 1.0);
let sph: SPos<Center, Frame, Meter> = cart.to_spherical();
let back: CPos<Center, Frame, Meter> = CPos::from_spherical(&sph);
assert!((back.z().value() - 1.0).abs() < 1e-10);
Run the included examples:
cargo run --example basic_cartesiancargo run --example parameterized_centercargo run --example spherical_roundtripLicensed under AGPL-3.0-only. See LICENSE.