vector-traits 0.6.2

Rust traits for 2D and 3D vector types.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2023, 2025 lacklustr@protonmail.com https://github.com/eadf

// This file is part of vector-traits.

use crate::prelude::*;
use std::{fmt::Debug, ops::Mul};

/// A simple trait wrapper of a Matrix3
pub trait Affine2D: Sync + Send + Clone + Debug + Sized + Mul<Output = Self> {
    type Vector2: GenericVector2;

    /// Transform a 2D vector as a point (applies translation)
    #[must_use]
    fn transform_point2(&self, point: Self::Vector2) -> Self::Vector2;

    /// Transform a 2D vector as a vector (ignores translation)
    #[must_use]
    fn transform_vector2(&self, vec: Self::Vector2) -> Self::Vector2;

    /// Create a matrix from a column major array
    #[must_use]
    fn from_cols_array(array: &[<Self::Vector2 as HasXY>::Scalar; 9]) -> Self;

    /// Create an identity matrix
    #[must_use]
    fn identity() -> Self;

    /// Safely get inverse or None if not invertible
    #[must_use]
    fn try_inverse(&self) -> Option<Self>;
}

/// A simple trait wrapper of a Matrix4
pub trait Affine3D: Sync + Send + Clone + Debug + Sized + Mul<Output = Self> {
    type Vector3: GenericVector3;

    /// Transform a 3D vector as a point (applies translation)
    #[must_use]
    fn transform_point3(&self, point: Self::Vector3) -> Self::Vector3;

    /// Transform a 3D vector as a vector (ignores translation)
    #[must_use]
    fn transform_vector3(&self, vec: Self::Vector3) -> Self::Vector3;

    /// Create a matrix from a column major array
    #[must_use]
    fn from_cols_array(array: &[<Self::Vector3 as HasXY>::Scalar; 16]) -> Self;

    /// Create an identity matrix
    #[must_use]
    fn identity() -> Self;

    /// Creates a transform that transforms coordinates from the specified source plane
    /// to the XY plane (where Z = 0).
    ///
    /// This is useful when you have 2D data stored in an arbitrary plane and want to
    /// work with it in standard XY coordinates.
    ///
    /// # Arguments
    ///
    /// * `source_plane` - The plane in which the original 2D coordinates are defined.
    ///   - XY: Leaves coordinates unchanged (identity matrix)
    ///   - XZ: Swaps Y and Z axes (maps XZ plane to XY plane)
    ///   - YZ: Swaps X and Z axes (maps YZ plane to XY plane)
    ///
    /// # Returns
    ///
    /// The transform that will convert points from the source plane to XY.
    ///
    fn from_plane_to_xy(source_plane: Plane) -> Self;

    fn from_scale(scale: Self::Vector3) -> Self;

    fn from_translation(translation: Self::Vector3) -> Self;

    /// Safely get inverse or None if not invertible
    #[must_use]
    fn try_inverse(&self) -> Option<Self>;
}