Crate cga2d

Crate cga2d 

Source
Expand description

Conformal Geometric Algebra in 2D.

use cga2d::prelude::*;

let p1 = cga2d::point(1.0, 3.0);
let p2 = cga2d::point(-3.0, 5.0);
let line = p1 ^ p2 ^ NI;

assert!(line.is_flat());

assert_eq!(!(line ^ cga2d::point(-1.0, 4.0)), 0.0);

let circ = cga2d::circle(cga2d::point(3.0, 1.5), 3.0);
assert_eq!(circ.sandwich(NI).unpack().unwrap().finite(), Some([3.0, 1.5]));

let rot90_ccw: Rotor = cga2d::line(1.0, 1.0, 0.0) * cga2d::line(1.0, 0.0, 0.0);
assert_eq!(rot90_ccw.sandwich(cga2d::point(3.0, 4.0)).unpack().unwrap().finite(), Some([-4.0, 3.0]));

§Multivector types

There is no unified multivector type. Instead, there is a Multivector trait, implemented by several blades (which also implement the Blade trait) and several rotoflectors.

§Blades

Blade typeGradeUsed to represent
Scalar = f640Scalar quantities
Blade11Points, vectors, round points
Blade22Point pairs, tangent points, flat points
Blade33Circles (real & imaginary), lines
Pseudoscalar4Pseudoscalar quantities

§Rotoflectors

Rotoflector typeSubalgebraUsed to represent
Rotorevenorientation-preserving conformal transformations
Flectoroddnon-orientation-preserving conformal transformations
Rotoflectoreven or oddconformal transformations

Note that Rotoflector contains either even terms or odd terms. It is not a general-purpose multivector.

There is no general-purpose multivector type.

§Construction

The constants NI and NO contain 1-blades representing the point at infinity and the origin respectively.

§From components

All multivectors can be constructed directly via components.

let my_vector = Blade1 {
    m: 0.0,
    p: 0.0,
    x: 3.0,
    y: 4.0,
};

§Helper functions

There are also several convenience functions built-in for constructing common multivectors, and these can be composed with operations.

// Type annotations are not required
let v: Blade1 = cga2d::vector(3.0, 4.0);
let center: Blade1 = cga2d::point(3.0, 4.0);
let real_circle: Blade3 = cga2d::circle(center, 7.0);
let imag_circle: Blade3 = cga2d::circle(center, -7.0);
let line: Blade3 = cga2d::line(3.0, 4.0, 2.0);

let point_pair: Blade2 = cga2d::point(3.0, 4.0) ^ NO;
let flat_point: Blade2 = cga2d::point(3.0, 4.0) ^ NI;

Additionally, all multivectors can be constructed by summing terms. Terms that cannot be represented by the multivector are discarded. I.e., the terms are grade-projected.

§From terms

let vector: Blade1 = [
    cga2d::Term::new(cga2d::Axes::X, 3.0),
    cga2d::Term::new(cga2d::Axes::X, 4.0)
]
.into_iter()
.sum();

§From blades

Rotoflectors can be constructed from Blades of the appropriate grade.

let center = cga2d::point(3.0, 4.0);
let circle = cga2d::circle(center, 7.0);
let circle_inversion = Flector::from(circle);

let central_inversion = Rotor::from(NI ^ NO);
let inverted_circle = central_inversion.sandwich(circle);
assert_eq!(inverted_circle.unpack(), cga2d::Circle::Circle {
    cx: -3.0,
    cy: -4.0,
    r: 7.0,
    ori: Orientation::Pos, // central inversion preserves orientation
});

§Operations

§Wedge product

§Antiwedge product

§Left contraction

§Negation

§Dual

§Scaling

§Geometric product

§Geometric product by inverse

§Addition & subtraction

§Indexing

Indexing panics if the multivector type doesn’t support the term. For a non-panicking alternative, see Multivector::get() and Multivector::get_mut().

Modules§

prelude
Traits and basic types (blades, NI, NO, rotor/flector/rotoflector).
traits
Traits.

Structs§

Axes
Subset of 2D CGA basis vectors.
Blade1
1-blade, used to represent points, vectors, and round points.
Blade2
2-blade, used to represent point pairs (real and imaginary), tangent vectors, and flat points.
Blade3
3-blade, used to represent circles (real and imaginary).
Flector
Multivector in the odd subalgebra of 2D CGA, used to represent non-orientation-preserving (i.e., reflecting) conformal transformations.
Precision
Almost-total order on floats using relative comparison, sorting floats into buckets.
Pseudoscalar
4-blade, used to represent pseudoscalar quantities.
Rotor
Multivector in the even subalgebra of 2D CGA, used to represent orientation-preserving (i.e., non-reflecting) conformal transformations.
Term
Term in 2D CGA.

Enums§

Circle
Euclidean line or circle.
Dipole
Real or imaginary point pair in Euclidean space.
Orientation
Orientation of an object.
Point
Euclidean point.
Rotoflector
Multivector in either the even or odd subalgebra of 2D CGA, used to represent an arbitrary conformal transformation.

Constants§

NI
∞, also called n ͚, representing the point at infinity.
NO
nₒ, representing the origin.

Traits§

ApproxCmpZero
ApproxEq
Trait for types that can be approximately compared for equality with each other.
ApproxEqZero
Trait for types that can be approximately compared to some zero value.
ApproxHash
Trait for types that can be stored in a crate::ApproxHashMap.
ApproxOrd
Trait for types that can be approximately ordered with each other.
Blade
Multivector of a compile-time-known grade.
Multivector
Multivector supporting an arbitrary subset of terms.
Wedge
Wedge operation between two blades.

Functions§

circle
Constructs a counterclockwise circle given a center point and a radius.
dipole
Constructs a dipole given a point, a vector, and a radius.
line
Constructs a line from the equation ax+by+c=0.
point
Lifts a Euclidean point into 2D conformal space.
rotate
Constructs a rotor that rotates by angle, fixing the origin.
scale
Constructs a rotor that scales by factor, fixing the origin.
slerp
Interpolates between two multivectors using an angle.
tangent_point
Constructs a tangent point given a point and a vector.
vector
Lifts a Euclidean vector into 2D CGA.

Type Aliases§

Scalar
0-blade, used to represent scalar quantities.