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;

let epsilon = 0.0001; // comparison threshold
assert!(line.is_flat(epsilon));

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_point(), (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_point(), (-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);
let epsilon = 0.0001; // comparison threshold
assert_eq!(inverted_circle.unpack(epsilon), cga2d::LineOrCircle::Circle {
    cx: -3.0,
    cy: -4.0,
    r: 7.0
});

§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§

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

Structs§

  • Subset of 2D CGA basis vectors.
  • 1-blade, used to represent points, vectors, and round points.
  • 2-blade, used to represent point pairs (real and imaginary), tangent vectors, and flat points.
  • 3-blade, used to represent circles (real and imaginary).
  • Multivector in the odd subalgebra of 2D CGA, used to represent non-orientation-preserving (i.e., reflecting) conformal transformations.
  • 4-blade, used to represent pseudoscalar quantities.
  • Multivector in the even subalgebra of 2D CGA, used to represent orientation-preserving (i.e., non-reflecting) conformal transformations.
  • Term in 2D CGA.

Enums§

  • Euclidean line or circle.
  • Multivector in either the even or odd subalgebra of 2D CGA, used to represent an arbitrary conformal transformation.

Constants§

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

Traits§

  • Multivector of a compile-time-known grade.
  • Multivector supporting an arbitrary subset of terms.
  • Wedge operation between two blades.

Functions§

  • Constructs a circle given a center point and a radius.
  • Constructs a line from the equation ax+by+c=0.
  • Lifts a Euclidean point into 2D conformal space.
  • Interpolates between two multivectors using an angle.
  • Lifts a Euclidean vector into 2D CGA.

Type Aliases§

  • 0-blade, used to represent scalar quantities.