inner-space 0.1.0

Provides the dot product trait and auto implements the inner space trait, which contains a bunch of useful functions for working with vectors
Documentation

Inner Space

Crates.io Docs License

A Rust crate providing essential traits for vector operations, building on vector-space.

Features

  • DotProduct trait: Defines dot product with flexible output type
  • InnerSpace trait: Provides common vector operations like normalize, project, reflect...
  • Free functions: distance and distance2 for point operations
  • Zero-cost abstractions: All operations are inlined and allocation-free

Example

use std::ops::{Add, Div, Mul, Neg, Sub};

use inner_space::{DotProduct, VectorSpace, distance};
use num_traits::Zero;

#[derive(Clone, Copy, PartialEq, Debug)]
struct Vector {
    x: f32,
    y: f32,
}

impl Vector {
    fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }
}

impl Add for Vector {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

impl Sub for Vector {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

impl Mul<f32> for Vector {
    type Output = Self;

    fn mul(self, other: f32) -> Self {
        Self {
            x: self.x * other,
            y: self.y * other,
        }
    }
}

impl Div<f32> for Vector {
    type Output = Self;

    fn div(self, other: f32) -> Self {
        Self {
            x: self.x / other,
            y: self.y / other,
        }
    }
}

impl Neg for Vector {
    type Output = Self;

    fn neg(self) -> Self {
        Self {
            x: -self.x,
            y: -self.y,
        }
    }
}

impl Zero for Vector {
    fn zero() -> Self {
        Self { x: 0.0, y: 0.0 }
    }

    fn is_zero(&self) -> bool {
        self.x == 0.0 && self.y == 0.0
    }
}

impl VectorSpace for Vector {
    type Scalar = f32;
}

impl DotProduct for Vector {
    type Output = f32;

    fn dot(&self, other: &Self) -> f32 {
        self.x * other.x + self.y * other.y
    }
}

// distance

let a = Vector::new(-1.0, 2.0);
let b = Vector::new(2.0, 6.0);

assert_eq!(distance(a, b), 5.0);

// projection, rejection, reflection

let a = Vector::new(-1.0, 3.0);
let b = Vector::new(0.0, 6.0);

assert_eq!(a.project(b), Vector::new(0.0, 3.0));
assert_eq!(a.reject(b), Vector::new(-1.0, 0.0));
assert_eq!(a.reflect(b), Vector::new(1.0, 3.0));

Core Components

DotProduct Trait

pub trait DotProduct<T = Self>: VectorSpace {
    type Output;

    fn dot(&self, other: &T) -> Self::Output;
}

InnerSpace Trait

Automatically implemented for types that implement DotProduct<Output = Scalar>. Provides:

  • magnitude() and magnitude2()
  • normalize()
  • project() and reject()
  • reflect()
  • angle()
  • with_magnitude() and with_direction()

Free Functions

  • distance(a, b): Distance between two points
  • distance2(a, b): Squared distance between two points

Example: Vector Operations

let v = Vec2(3.0, 4.0);
let unit = v.normalize();
assert_eq!(unit, Vec2(0.6, 0.8));

let reflected = v.reflect(Vec2(1.0, 0.0));  // Reflect over x-axis
assert_eq!(reflected, Vec2(3.0, -4.0));

Implementation Guide

  1. Implement VectorSpace from vector-space crate (also reexported here)
  2. Implement DotProduct with Output = VectorSpace::Scalar
  3. Get all InnerSpace methods automatically

Compatibility

Works with any type that implements VectorSpace and the required operations. Perfect for:

  • Custom vector types
  • Game development
  • Physics simulations
  • Graphics programming