glamdet-na-conv 2.0.0

Type conversions between glam and nalgebra
Documentation
// Copyright (C) 2020-2025 glamdet-na-conv authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![deny(warnings)]

use glam_det as glam;

#[cfg(feature = "f64")]
pub type Real = f64;
#[cfg(not(feature = "f64"))]
pub type Real = f32;

mod na {
    use crate::Real;

    pub type Vec3 = nalgebra::Vector3<Real>;
    pub type Point3 = nalgebra::Point3<Real>;
    pub type UnitVec3 = nalgebra::UnitVector3<Real>;
    pub type UnitQuat = nalgebra::UnitQuaternion<Real>;
    pub type Isometry3 = nalgebra::Isometry3<Real>;
    pub type Quat = nalgebra::Quaternion<Real>;
    pub type Translation3 = nalgebra::Translation3<Real>;
    pub type Mat3 = nalgebra::Matrix3<Real>;
}

pub trait ConvTo<T> {
    fn conv_to(self) -> T;
}

impl ConvTo<glam::Point3> for na::Point3 {
    #[inline]
    fn conv_to(self) -> glam::Point3 {
        glam::Point3::new(self.x, self.y, self.z)
    }
}

impl ConvTo<na::Point3> for glam::Point3 {
    #[inline]
    fn conv_to(self) -> na::Point3 {
        na::Point3::new(self.x, self.y, self.z)
    }
}

impl ConvTo<glam::UnitVec3> for na::UnitVec3 {
    #[inline]
    fn conv_to(self) -> glam::UnitVec3 {
        glam::UnitVec3::new_unchecked(self.x, self.y, self.z)
    }
}

impl ConvTo<na::UnitVec3> for glam::UnitVec3 {
    #[inline]
    fn conv_to(self) -> na::UnitVec3 {
        na::UnitVec3::new_unchecked(na::Vec3::new(self.x, self.y, self.z))
    }
}

impl ConvTo<glam::Vec3> for na::Vec3 {
    #[inline]
    fn conv_to(self) -> glam::Vec3 {
        glam::Vec3::new(self.x, self.y, self.z)
    }
}

impl ConvTo<na::Vec3> for glam::Vec3 {
    #[inline]
    fn conv_to(self) -> na::Vec3 {
        na::Vec3::new(self.x, self.y, self.z)
    }
}

impl ConvTo<glam::UnitQuat> for na::UnitQuat {
    #[inline]
    fn conv_to(self) -> glam::UnitQuat {
        glam::UnitQuat::from_xyzw_unchecked(self.i, self.j, self.k, self.w)
    }
}

impl ConvTo<na::UnitQuat> for glam::UnitQuat {
    #[inline]
    fn conv_to(self) -> na::UnitQuat {
        let vec = na::Vec3::new(self.x, self.y, self.z);
        na::UnitQuat::new_normalize(na::Quat::from_parts(self.w, vec))
    }
}

impl ConvTo<glam::Isometry3> for na::Isometry3 {
    #[inline]
    fn conv_to(self) -> glam::Isometry3 {
        glam::Isometry3::from_rotation_translation(
            self.rotation.conv_to(),
            self.translation.vector.conv_to(),
        )
    }
}

impl ConvTo<na::Isometry3> for glam::Isometry3 {
    #[inline]
    fn conv_to(self) -> na::Isometry3 {
        na::Isometry3::from_parts(
            na::Translation3::from(self.translation.conv_to()),
            self.rotation.conv_to(),
        )
    }
}

impl ConvTo<glam::Mat3> for na::Mat3 {
    #[inline]
    fn conv_to(self) -> glam::Mat3 {
        glam::Mat3::from_cols_array_2d(&self.data.0)
    }
}