Trait cgmath::EuclideanSpace
source · pub trait EuclideanSpace: Copy + Clonewhere
Self: Array<Element = Self::Scalar> + Add<Self::Diff, Output = Self> + Sub<Self::Diff, Output = Self> + Sub<Self, Output = Self::Diff> + Mul<Self::Scalar, Output = Self> + Div<Self::Scalar, Output = Self> + Rem<Self::Scalar, Output = Self>,{
type Scalar: BaseNum;
type Diff: VectorSpace<Scalar = Self::Scalar>;
// Required methods
fn origin() -> Self;
fn from_vec(v: Self::Diff) -> Self;
fn to_vec(self) -> Self::Diff;
fn dot(self, v: Self::Diff) -> Self::Scalar;
// Provided methods
fn midpoint(self, other: Self) -> Self { ... }
fn centroid(points: &[Self]) -> Self { ... }
}
Expand description
Points in a Euclidean space with an associated space of displacement vectors.
Point-Vector distinction
cgmath
distinguishes between points and vectors in the following way:
- Points are locations relative to an origin
- Vectors are displacements between those points
For example, to find the midpoint between two points, you can write the following:
use cgmath::Point3;
let p0 = Point3::new(1.0, 2.0, 3.0);
let p1 = Point3::new(-3.0, 1.0, 2.0);
let midpoint: Point3<f32> = p0 + (p1 - p0) * 0.5;
Breaking the expression up, and adding explicit types makes it clearer to see what is going on:
let dv: Vector3<f32> = p1 - p0;
let half_dv: Vector3<f32> = dv * 0.5;
let midpoint: Point3<f32> = p0 + half_dv;
Converting between points and vectors
Points can be converted to and from displacement vectors using the
EuclideanSpace::{from_vec, to_vec}
methods. Note that under the hood these
are implemented as inlined a type conversion, so should not have any
performance implications.
References
Required Associated Types§
sourcetype Scalar: BaseNum
type Scalar: BaseNum
The associated scalar over which the space is defined.
Due to the equality constraints demanded by Self::Diff
, this is effectively just an
alias to Self::Diff::Scalar
.
sourcetype Diff: VectorSpace<Scalar = Self::Scalar>
type Diff: VectorSpace<Scalar = Self::Scalar>
The associated space of displacement vectors.
Required Methods§
sourcefn from_vec(v: Self::Diff) -> Self
fn from_vec(v: Self::Diff) -> Self
Convert a displacement vector to a point.
This can be considered equivalent to the addition of the displacement
vector v
to to Self::origin()
.