Foundation
Loading...
Searching...
No Matches
Decompose.hpp
Go to the documentation of this file.
1#pragma once
2#include "Math.hpp"
4{
5 inline bool decompose(mat4 const& m, float3& scale, quat& rotation, float3& transform)
6 {
15 mat3 basis = mat3(m);
16 transform = { m[3][0],m[3][1],m[3][2] };
17 // Negative determinant means handness flip
18 float det = determinant(basis);
19 float sgn = det > 0 ? 1 : -1;
20 scale = {
21 length(float3{ basis[0][0], basis[0][1], basis[0][2] }) * sgn,
22 length(float3{ basis[1][0], basis[1][1], basis[1][2] }) * sgn,
23 length(float3{ basis[2][0], basis[2][1], basis[2][2] }) * sgn
24 };
25 // Normalize to get pure rotation matrix
26 basis[0] /= scale.x == .0f ? 1.0f : scale.x;
27 basis[1] /= scale.y == .0f ? 1.0f : scale.y;
28 basis[2] /= scale.z == .0f ? 1.0f : scale.z;
29 rotation = quat_cast(basis);
30 return true;
31 }
32}
Definition Decompose.hpp:4
vec3 float3
Definition Math.hpp:26
bool decompose(mat4 const &m, float3 &scale, quat &rotation, float3 &transform)
Definition Decompose.hpp:5