Skip to content

Commit

Permalink
Add missing from_mat3a methods (#352)
Browse files Browse the repository at this point in the history
* Add a lot of missing `from_mat3a` methods.
  • Loading branch information
bitshifter committed Oct 10, 2022
1 parent 34c57c6 commit 43cf6e8
Show file tree
Hide file tree
Showing 40 changed files with 248 additions and 37 deletions.
12 changes: 12 additions & 0 deletions codegen/templates/affine.rs.tera
Expand Up @@ -270,6 +270,18 @@ impl {{ self_t }} {
}
}

{% if scalar_t == "f32" %}
/// The given `Mat3A` must be an affine transform,
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
use crate::swizzles::Vec3Swizzles;
Self {
matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
translation: m.z_axis.xy(),
}
}
{% endif %}

/// Transforms the given 2D point, applying shear, scale, rotation and translation.
#[inline]
pub fn transform_point2(&self, rhs: {{ vec2_t }}) -> {{ vec2_t }} {
Expand Down
36 changes: 32 additions & 4 deletions codegen/templates/mat.rs.tera
Expand Up @@ -18,11 +18,9 @@
{% set affine3_t = "Affine3A" %}
{% set vec2_t = "Vec2" %}
{% set vec3_t = "Vec3" %}
{% set vec3a_t = "Vec3A" %}
{% set vec4_t = "Vec4" %}
{% set mat2_t = "Mat2" %}
{% set mat3_t = "Mat3" %}
{% set mat3a_t = "Mat3A" %}
{% set mat4_t = "Mat4" %}
{% elif scalar_t == "f64" %}
{% set vecn_t = "DVec" ~ dim %}
Expand Down Expand Up @@ -66,19 +64,22 @@ use crate::{
{% endif %}
{% if dim == 2 %}
{{ mat3_t }}, {{ vec2_t }},
{% if scalar_t == "f32" %}
Mat3A,
{% endif %}
{% elif dim == 3 %}
EulerRot,
{{ mat2_t }}, {{ mat4_t }}, {{ quat_t }}, {{ vec2_t }}, {{ col_t }},
{% if is_align %}
{{ mat3_t }}, {{ vec3_t }},
{% elif scalar_t == "f32" %}
{{ mat3a_t }}, {{ vec3a_t }},
Mat3A, Vec3A,
{% endif %}
{% elif dim == 4 %}
EulerRot,
{{ mat3_t }}, {{ quat_t }}, {{ vec3_t }}, {{ col_t }},
{% if scalar_t == "f32" %}
{{ vec3a_t }},
Mat3A, Vec3A,
{% endif %}
{% endif %}
{% if self_t == "Mat4" %}
Expand Down Expand Up @@ -407,6 +408,16 @@ impl {{ self_t }} {
pub fn from_mat3(m: {{ mat3_t }}) -> Self {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

{% if scalar_t == "f32" %}
/// Creates a {{ nxn }} matrix from a 3x3 matrix, discarding the 2nd row and column.
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
{# TODO: SIMD optimise #}
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}
{% endif %}

{% elif dim == 3 %}
/// Creates a 3x3 matrix from a 4x4 matrix, discarding the 4th row and column.
pub fn from_mat4(m: {{ mat4_t }}) -> Self {
Expand Down Expand Up @@ -717,6 +728,23 @@ impl {{ self_t }} {
)
}

{% if scalar_t == "f32" %}
/// Creates an affine transformation matrix from the given 3x3 linear transformation
/// matrix.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(
Vec4::from((m.x_axis, 0.0)),
Vec4::from((m.y_axis, 0.0)),
Vec4::from((m.z_axis, 0.0)),
Vec4::W,
)
}
{% endif %}

/// Creates an affine transformation matrix from the given 3D `translation`.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
Expand Down
14 changes: 13 additions & 1 deletion codegen/templates/quat.rs.tera
Expand Up @@ -33,7 +33,7 @@ use crate::{
FloatEx,
euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion},
{% if scalar_t == "f32" %}
DQuat, Mat3, Mat4, Vec2, Vec3, Vec3A, Vec4,
DQuat, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4,
{% elif scalar_t == "f64" %}
DMat3, DMat4, DVec2, DVec3, DVec4, Quat,
{% endif %}
Expand Down Expand Up @@ -340,6 +340,18 @@ impl {{ self_t }} {
)
}

{% if scalar_t == "f32" %}
/// Creates a quaternion from a 3x3 SIMD aligned rotation matrix.
#[inline]
pub fn from_mat3a(mat: &Mat3A) -> Self {
Self::from_rotation_axes(
mat.x_axis.into(),
mat.y_axis.into(),
mat.z_axis.into(),
)
}
{% endif %}

/// Creates a quaternion from a 3x3 rotation matrix inside a homogeneous 4x4 matrix.
#[inline]
pub fn from_mat4(mat: &{{ mat4_t }}) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/vec.rs.tera
Expand Up @@ -430,7 +430,7 @@ impl {{ self_t }} {
{% endif %}
}

/// Returns a vector where every component is the dot product of `self` and `rhs`
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
{% if is_sse2 %}
Expand Down
10 changes: 10 additions & 0 deletions src/f32/affine2.rs
Expand Up @@ -196,6 +196,16 @@ impl Affine2 {
}
}

/// The given `Mat3A` must be an affine transform,
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
use crate::swizzles::Vec3Swizzles;
Self {
matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
translation: m.z_axis.xy(),
}
}

/// Transforms the given 2D point, applying shear, scale, rotation and translation.
#[inline]
pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
Expand Down
8 changes: 7 additions & 1 deletion src/f32/coresimd/mat2.rs
@@ -1,6 +1,6 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{swizzles::*, DMat2, Mat3, Vec2};
use crate::{swizzles::*, DMat2, Mat3, Mat3A, Vec2};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -103,6 +103,12 @@ impl Mat2 {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

/// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

/// Creates a 2x2 matrix from the first 4 values in `slice`.
///
/// # Panics
Expand Down
17 changes: 16 additions & 1 deletion src/f32/coresimd/mat4.rs
@@ -1,6 +1,6 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{coresimd::*, swizzles::*, DMat4, EulerRot, Mat3, Quat, Vec3, Vec3A, Vec4};
use crate::{coresimd::*, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -287,6 +287,21 @@ impl Mat4 {
)
}

/// Creates an affine transformation matrix from the given 3x3 linear transformation
/// matrix.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(
Vec4::from((m.x_axis, 0.0)),
Vec4::from((m.y_axis, 0.0)),
Vec4::from((m.z_axis, 0.0)),
Vec4::W,
)
}

/// Creates an affine transformation matrix from the given 3D `translation`.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
Expand Down
8 changes: 7 additions & 1 deletion src/f32/coresimd/quat.rs
Expand Up @@ -3,7 +3,7 @@
use crate::{
coresimd::*,
euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion},
DQuat, FloatEx, Mat3, Mat4, Vec2, Vec3, Vec3A, Vec4,
DQuat, FloatEx, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4,
};

#[cfg(feature = "libm")]
Expand Down Expand Up @@ -233,6 +233,12 @@ impl Quat {
Self::from_rotation_axes(mat.x_axis, mat.y_axis, mat.z_axis)
}

/// Creates a quaternion from a 3x3 SIMD aligned rotation matrix.
#[inline]
pub fn from_mat3a(mat: &Mat3A) -> Self {
Self::from_rotation_axes(mat.x_axis.into(), mat.y_axis.into(), mat.z_axis.into())
}

/// Creates a quaternion from a 3x3 rotation matrix inside a homogeneous 4x4 matrix.
#[inline]
pub fn from_mat4(mat: &Mat4) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/f32/coresimd/vec3a.rs
Expand Up @@ -148,7 +148,7 @@ impl Vec3A {
dot3(self.0, rhs.0)
}

/// Returns a vector where every component is the dot product of `self` and `rhs`
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
Self(unsafe { dot3_into_f32x4(self.0, rhs.0) })
Expand Down
2 changes: 1 addition & 1 deletion src/f32/coresimd/vec4.rs
Expand Up @@ -141,7 +141,7 @@ impl Vec4 {
dot4(self.0, rhs.0)
}

/// Returns a vector where every component is the dot product of `self` and `rhs`
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
Self(unsafe { dot4_into_f32x4(self.0, rhs.0) })
Expand Down
8 changes: 7 additions & 1 deletion src/f32/scalar/mat2.rs
@@ -1,6 +1,6 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{swizzles::*, DMat2, Mat3, Vec2};
use crate::{swizzles::*, DMat2, Mat3, Mat3A, Vec2};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -112,6 +112,12 @@ impl Mat2 {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

/// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

/// Creates a 2x2 matrix from the first 4 values in `slice`.
///
/// # Panics
Expand Down
17 changes: 16 additions & 1 deletion src/f32/scalar/mat4.rs
@@ -1,6 +1,6 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{swizzles::*, DMat4, EulerRot, Mat3, Quat, Vec3, Vec3A, Vec4};
use crate::{swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -300,6 +300,21 @@ impl Mat4 {
)
}

/// Creates an affine transformation matrix from the given 3x3 linear transformation
/// matrix.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(
Vec4::from((m.x_axis, 0.0)),
Vec4::from((m.y_axis, 0.0)),
Vec4::from((m.z_axis, 0.0)),
Vec4::W,
)
}

/// Creates an affine transformation matrix from the given 3D `translation`.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
Expand Down
8 changes: 7 additions & 1 deletion src/f32/scalar/quat.rs
Expand Up @@ -2,7 +2,7 @@

use crate::{
euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion},
DQuat, FloatEx, Mat3, Mat4, Vec2, Vec3, Vec3A, Vec4,
DQuat, FloatEx, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4,
};

#[cfg(feature = "libm")]
Expand Down Expand Up @@ -243,6 +243,12 @@ impl Quat {
Self::from_rotation_axes(mat.x_axis, mat.y_axis, mat.z_axis)
}

/// Creates a quaternion from a 3x3 SIMD aligned rotation matrix.
#[inline]
pub fn from_mat3a(mat: &Mat3A) -> Self {
Self::from_rotation_axes(mat.x_axis.into(), mat.y_axis.into(), mat.z_axis.into())
}

/// Creates a quaternion from a 3x3 rotation matrix inside a homogeneous 4x4 matrix.
#[inline]
pub fn from_mat4(mat: &Mat4) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/f32/scalar/vec3a.rs
Expand Up @@ -159,7 +159,7 @@ impl Vec3A {
(self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
}

/// Returns a vector where every component is the dot product of `self` and `rhs`
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
Self::splat(self.dot(rhs))
Expand Down
2 changes: 1 addition & 1 deletion src/f32/scalar/vec4.rs
Expand Up @@ -162,7 +162,7 @@ impl Vec4 {
(self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
}

/// Returns a vector where every component is the dot product of `self` and `rhs`
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
Self::splat(self.dot(rhs))
Expand Down
8 changes: 7 additions & 1 deletion src/f32/sse2/mat2.rs
@@ -1,6 +1,6 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{swizzles::*, DMat2, Mat3, Vec2};
use crate::{swizzles::*, DMat2, Mat3, Mat3A, Vec2};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -121,6 +121,12 @@ impl Mat2 {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

/// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}

/// Creates a 2x2 matrix from the first 4 values in `slice`.
///
/// # Panics
Expand Down
17 changes: 16 additions & 1 deletion src/f32/sse2/mat4.rs
@@ -1,6 +1,6 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{sse2::*, swizzles::*, DMat4, EulerRot, Mat3, Quat, Vec3, Vec3A, Vec4};
use crate::{sse2::*, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -290,6 +290,21 @@ impl Mat4 {
)
}

/// Creates an affine transformation matrix from the given 3x3 linear transformation
/// matrix.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
#[inline]
pub fn from_mat3a(m: Mat3A) -> Self {
Self::from_cols(
Vec4::from((m.x_axis, 0.0)),
Vec4::from((m.y_axis, 0.0)),
Vec4::from((m.z_axis, 0.0)),
Vec4::W,
)
}

/// Creates an affine transformation matrix from the given 3D `translation`.
///
/// The resulting matrix can be used to transform 3D points and vectors. See
Expand Down

0 comments on commit 43cf6e8

Please sign in to comment.