use num::{One, Zero};
use crate::base::allocator::Allocator;
use crate::base::dimension::{DimName, DimNameDiff, DimNameSub, U1};
use crate::base::storage::{Storage, StorageMut};
use crate::base::{
DefaultAllocator, Matrix3, Matrix4, MatrixN, Scalar, SquareMatrix, Unit, Vector, Vector2,
Vector3, VectorN,
};
use crate::geometry::{
Isometry, IsometryMatrix3, Orthographic3, Perspective3, Point, Point2, Point3, Rotation2,
Rotation3,
};
use simba::scalar::{ClosedAdd, ClosedMul, RealField};
impl<N, D: DimName> MatrixN<N, D>
where
N: Scalar + Zero + One,
DefaultAllocator: Allocator<N, D, D>,
{
#[inline]
pub fn new_scaling(scaling: N) -> Self {
let mut res = Self::from_diagonal_element(scaling);
res[(D::dim() - 1, D::dim() - 1)] = N::one();
res
}
#[inline]
pub fn new_nonuniform_scaling<SB>(scaling: &Vector<N, DimNameDiff<D, U1>, SB>) -> Self
where
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
{
let mut res = Self::identity();
for i in 0..scaling.len() {
res[(i, i)] = scaling[i].inlined_clone();
}
res
}
#[inline]
pub fn new_translation<SB>(translation: &Vector<N, DimNameDiff<D, U1>, SB>) -> Self
where
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
{
let mut res = Self::identity();
res.fixed_slice_mut::<DimNameDiff<D, U1>, U1>(0, D::dim() - 1)
.copy_from(translation);
res
}
}
impl<N: RealField> Matrix3<N> {
#[inline]
pub fn new_rotation(angle: N) -> Self {
Rotation2::new(angle).to_homogeneous()
}
#[inline]
pub fn new_nonuniform_scaling_wrt_point(scaling: &Vector2<N>, pt: &Point2<N>) -> Self {
let zero = N::zero();
let one = N::one();
Matrix3::new(
scaling.x,
zero,
pt.x - pt.x * scaling.x,
zero,
scaling.y,
pt.y - pt.y * scaling.y,
zero,
zero,
one,
)
}
}
impl<N: RealField> Matrix4<N> {
#[inline]
pub fn new_rotation(axisangle: Vector3<N>) -> Self {
Rotation3::new(axisangle).to_homogeneous()
}
#[inline]
pub fn new_rotation_wrt_point(axisangle: Vector3<N>, pt: Point3<N>) -> Self {
let rot = Rotation3::from_scaled_axis(axisangle);
Isometry::rotation_wrt_point(rot, pt).to_homogeneous()
}
#[inline]
pub fn new_nonuniform_scaling_wrt_point(scaling: &Vector3<N>, pt: &Point3<N>) -> Self {
let zero = N::zero();
let one = N::one();
Matrix4::new(
scaling.x,
zero,
zero,
pt.x - pt.x * scaling.x,
zero,
scaling.y,
zero,
pt.y - pt.y * scaling.y,
zero,
zero,
scaling.z,
pt.z - pt.z * scaling.z,
zero,
zero,
zero,
one,
)
}
#[inline]
pub fn from_scaled_axis(axisangle: Vector3<N>) -> Self {
Rotation3::from_scaled_axis(axisangle).to_homogeneous()
}
pub fn from_euler_angles(roll: N, pitch: N, yaw: N) -> Self {
Rotation3::from_euler_angles(roll, pitch, yaw).to_homogeneous()
}
pub fn from_axis_angle(axis: &Unit<Vector3<N>>, angle: N) -> Self {
Rotation3::from_axis_angle(axis, angle).to_homogeneous()
}
#[inline]
pub fn new_orthographic(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Self {
Orthographic3::new(left, right, bottom, top, znear, zfar).into_inner()
}
#[inline]
pub fn new_perspective(aspect: N, fovy: N, znear: N, zfar: N) -> Self {
Perspective3::new(aspect, fovy, znear, zfar).into_inner()
}
#[inline]
pub fn face_towards(eye: &Point3<N>, target: &Point3<N>, up: &Vector3<N>) -> Self {
IsometryMatrix3::face_towards(eye, target, up).to_homogeneous()
}
#[deprecated(note = "renamed to `face_towards`")]
pub fn new_observer_frame(eye: &Point3<N>, target: &Point3<N>, up: &Vector3<N>) -> Self {
Matrix4::face_towards(eye, target, up)
}
#[inline]
pub fn look_at_rh(eye: &Point3<N>, target: &Point3<N>, up: &Vector3<N>) -> Self {
IsometryMatrix3::look_at_rh(eye, target, up).to_homogeneous()
}
#[inline]
pub fn look_at_lh(eye: &Point3<N>, target: &Point3<N>, up: &Vector3<N>) -> Self {
IsometryMatrix3::look_at_lh(eye, target, up).to_homogeneous()
}
}
impl<N: Scalar + Zero + One + ClosedMul + ClosedAdd, D: DimName, S: Storage<N, D, D>>
SquareMatrix<N, D, S>
{
#[inline]
#[must_use = "Did you mean to use append_scaling_mut()?"]
pub fn append_scaling(&self, scaling: N) -> MatrixN<N, D>
where
D: DimNameSub<U1>,
DefaultAllocator: Allocator<N, D, D>,
{
let mut res = self.clone_owned();
res.append_scaling_mut(scaling);
res
}
#[inline]
#[must_use = "Did you mean to use prepend_scaling_mut()?"]
pub fn prepend_scaling(&self, scaling: N) -> MatrixN<N, D>
where
D: DimNameSub<U1>,
DefaultAllocator: Allocator<N, D, D>,
{
let mut res = self.clone_owned();
res.prepend_scaling_mut(scaling);
res
}
#[inline]
#[must_use = "Did you mean to use append_nonuniform_scaling_mut()?"]
pub fn append_nonuniform_scaling<SB>(
&self,
scaling: &Vector<N, DimNameDiff<D, U1>, SB>,
) -> MatrixN<N, D>
where
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
DefaultAllocator: Allocator<N, D, D>,
{
let mut res = self.clone_owned();
res.append_nonuniform_scaling_mut(scaling);
res
}
#[inline]
#[must_use = "Did you mean to use prepend_nonuniform_scaling_mut()?"]
pub fn prepend_nonuniform_scaling<SB>(
&self,
scaling: &Vector<N, DimNameDiff<D, U1>, SB>,
) -> MatrixN<N, D>
where
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
DefaultAllocator: Allocator<N, D, D>,
{
let mut res = self.clone_owned();
res.prepend_nonuniform_scaling_mut(scaling);
res
}
#[inline]
#[must_use = "Did you mean to use append_translation_mut()?"]
pub fn append_translation<SB>(&self, shift: &Vector<N, DimNameDiff<D, U1>, SB>) -> MatrixN<N, D>
where
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
DefaultAllocator: Allocator<N, D, D>,
{
let mut res = self.clone_owned();
res.append_translation_mut(shift);
res
}
#[inline]
#[must_use = "Did you mean to use prepend_translation_mut()?"]
pub fn prepend_translation<SB>(
&self,
shift: &Vector<N, DimNameDiff<D, U1>, SB>,
) -> MatrixN<N, D>
where
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
DefaultAllocator: Allocator<N, D, D> + Allocator<N, DimNameDiff<D, U1>>,
{
let mut res = self.clone_owned();
res.prepend_translation_mut(shift);
res
}
#[inline]
pub fn append_scaling_mut(&mut self, scaling: N)
where
S: StorageMut<N, D, D>,
D: DimNameSub<U1>,
{
let mut to_scale = self.fixed_rows_mut::<DimNameDiff<D, U1>>(0);
to_scale *= scaling;
}
#[inline]
pub fn prepend_scaling_mut(&mut self, scaling: N)
where
S: StorageMut<N, D, D>,
D: DimNameSub<U1>,
{
let mut to_scale = self.fixed_columns_mut::<DimNameDiff<D, U1>>(0);
to_scale *= scaling;
}
#[inline]
pub fn append_nonuniform_scaling_mut<SB>(&mut self, scaling: &Vector<N, DimNameDiff<D, U1>, SB>)
where
S: StorageMut<N, D, D>,
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
{
for i in 0..scaling.len() {
let mut to_scale = self.fixed_rows_mut::<U1>(i);
to_scale *= scaling[i].inlined_clone();
}
}
#[inline]
pub fn prepend_nonuniform_scaling_mut<SB>(
&mut self,
scaling: &Vector<N, DimNameDiff<D, U1>, SB>,
) where
S: StorageMut<N, D, D>,
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
{
for i in 0..scaling.len() {
let mut to_scale = self.fixed_columns_mut::<U1>(i);
to_scale *= scaling[i].inlined_clone();
}
}
#[inline]
pub fn append_translation_mut<SB>(&mut self, shift: &Vector<N, DimNameDiff<D, U1>, SB>)
where
S: StorageMut<N, D, D>,
D: DimNameSub<U1>,
SB: Storage<N, DimNameDiff<D, U1>>,
{
for i in 0..D::dim() {
for j in 0..D::dim() - 1 {
let add = shift[j].inlined_clone() * self[(D::dim() - 1, i)].inlined_clone();
self[(j, i)] += add;
}
}
}
#[inline]
pub fn prepend_translation_mut<SB>(&mut self, shift: &Vector<N, DimNameDiff<D, U1>, SB>)
where
D: DimNameSub<U1>,
S: StorageMut<N, D, D>,
SB: Storage<N, DimNameDiff<D, U1>>,
DefaultAllocator: Allocator<N, DimNameDiff<D, U1>>,
{
let scale = self
.fixed_slice::<U1, DimNameDiff<D, U1>>(D::dim() - 1, 0)
.tr_dot(&shift);
let post_translation =
self.fixed_slice::<DimNameDiff<D, U1>, DimNameDiff<D, U1>>(0, 0) * shift;
self[(D::dim() - 1, D::dim() - 1)] += scale;
let mut translation = self.fixed_slice_mut::<DimNameDiff<D, U1>, U1>(0, D::dim() - 1);
translation += post_translation;
}
}
impl<N: RealField, D: DimNameSub<U1>, S: Storage<N, D, D>> SquareMatrix<N, D, S>
where
DefaultAllocator: Allocator<N, D, D>
+ Allocator<N, DimNameDiff<D, U1>>
+ Allocator<N, DimNameDiff<D, U1>, DimNameDiff<D, U1>>,
{
#[inline]
pub fn transform_vector(
&self,
v: &VectorN<N, DimNameDiff<D, U1>>,
) -> VectorN<N, DimNameDiff<D, U1>> {
let transform = self.fixed_slice::<DimNameDiff<D, U1>, DimNameDiff<D, U1>>(0, 0);
let normalizer = self.fixed_slice::<U1, DimNameDiff<D, U1>>(D::dim() - 1, 0);
let n = normalizer.tr_dot(&v);
if !n.is_zero() {
return transform * (v / n);
}
transform * v
}
#[inline]
pub fn transform_point(
&self,
pt: &Point<N, DimNameDiff<D, U1>>,
) -> Point<N, DimNameDiff<D, U1>> {
let transform = self.fixed_slice::<DimNameDiff<D, U1>, DimNameDiff<D, U1>>(0, 0);
let translation = self.fixed_slice::<DimNameDiff<D, U1>, U1>(0, D::dim() - 1);
let normalizer = self.fixed_slice::<U1, DimNameDiff<D, U1>>(D::dim() - 1, 0);
let n = normalizer.tr_dot(&pt.coords)
+ unsafe { *self.get_unchecked((D::dim() - 1, D::dim() - 1)) };
if !n.is_zero() {
(transform * pt + translation) / n
} else {
transform * pt + translation
}
}
}