Skip to content

Commit

Permalink
[core.trs] added trs library, note that rotations are not yet impleme…
Browse files Browse the repository at this point in the history
…nted coz quaternions dont exist yet
  • Loading branch information
harrand committed Oct 20, 2024
1 parent 5151f12 commit 17699ce
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ topaz_add_library(
src/tz/core/job.cpp
src/tz/core/vector.cpp
src/tz/core/matrix.cpp
src/tz/core/trs.cpp
src/tz/gpu/rhi_vulkan.cpp
src/tz/os/impl_win32.cpp
)
Expand Down
6 changes: 5 additions & 1 deletion include/tz/core/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ namespace tz

// matrix-scalar
matrix<T, N>& operator+=(T scalar);
matrix<T, N> operator+(T scalar) const{auto cpy = *this; return cpy += scalar;}
matrix<T, N>& operator-=(T scalar);
matrix<T, N> operator-(T scalar) const{auto cpy = *this; return cpy -= scalar;}
matrix<T, N>& operator*=(T scalar);
matrix<T, N> operator*(T scalar) const{auto cpy = *this; return cpy *= scalar;}
matrix<T, N>& operator/=(T scalar);
matrix<T, N> operator/(T scalar) const{auto cpy = *this; return cpy /= scalar;}

// matrix-matrix
matrix<T, N>& operator*=(const matrix<T, N>& rhs);
matrix<T, N> operator*(const matrix<T, N>& rhs){auto cpy = *this; return cpy *= rhs;}
matrix<T, N> operator*(const matrix<T, N>& rhs) const{auto cpy = *this; return cpy *= rhs;}

bool operator==(const matrix<T, N>& rhs) const = default;

Expand Down
25 changes: 25 additions & 0 deletions include/tz/core/trs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef TOPAZ_CORE_TRS_HPP
#define TOPAZ_CORE_TRS_HPP
#include "tz/core/vector.hpp"
#include "tz/core/matrix.hpp"

namespace tz
{
struct trs
{
tz::v3f translate = tz::v3f::zero();
// tz::quat rotate = tz::quat::iden();
tz::v3f scale = tz::v3f::filled(1.0f);

trs lerp(const trs& rhs, float factor) const;
m4u matrix() const;
static trs from_matrix(m4f mat);

trs inverse() const;
trs combine(const trs& rhs);

bool operator==(const trs& rhs) const = default;
};
}

#endif // TOPAZ_CORE_TRS_HPP
8 changes: 4 additions & 4 deletions include/tz/core/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ namespace tz
vector<T, N>& operator*=(const vector<T, N>& rhs);
vector<T, N>& operator/=(const vector<T, N>& rhs);

vector<T, N> operator+(const vector<T, N>& rhs){auto cpy = *this; return cpy += rhs;}
vector<T, N> operator-(const vector<T, N>& rhs){auto cpy = *this; return cpy -= rhs;}
vector<T, N> operator*(const vector<T, N>& rhs){auto cpy = *this; return cpy *= rhs;}
vector<T, N> operator/(const vector<T, N>& rhs){auto cpy = *this; return cpy /= rhs;}
vector<T, N> operator+(const vector<T, N>& rhs) const{auto cpy = *this; return cpy += rhs;}
vector<T, N> operator-(const vector<T, N>& rhs) const{auto cpy = *this; return cpy -= rhs;}
vector<T, N> operator*(const vector<T, N>& rhs) const{auto cpy = *this; return cpy *= rhs;}
vector<T, N> operator/(const vector<T, N>& rhs) const{auto cpy = *this; return cpy /= rhs;}

bool operator==(const vector<T, N>& rhs) const = default;
private:
Expand Down
67 changes: 67 additions & 0 deletions src/tz/core/trs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "tz/core/trs.hpp"
#include "tz/topaz.hpp"

namespace tz
{
trs trs::lerp(const trs& rhs, float factor) const
{
trs ret;
ret.translate = this->translate + (rhs.translate - this->translate) * factor;
//ret.rotate = this->rotate.slerp(rhs.rotate, factor);
ret.scale = this->scale + ((rhs.scale - this->scale) * factor);
return ret;
}

tz::m4u trs::matrix() const
{
tz_error("trs -> matrix is NYI");
return {};
}

trs trs::from_matrix(tz::m4f mat)
{
// decompose matrix -> trs using:https://github.com/KhronosGroup/glTF-Validator/issues/33
//If M[3] != 0.0 || M[7] != 0.0 || M[11] != 0.0 || M[15] != 1.0,
// matrix is indecomposable, exit.

/*
0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15
*/
trs ret;
ret.translate = {mat(3, 0), mat(3, 1), mat(3, 2)};
ret.scale[0] = std::sqrt(mat(0, 0) * mat(0, 0) + mat(1, 0) * mat(1, 0) + mat(2, 0) * mat(2, 0));
ret.scale[1] = std::sqrt(mat(0, 1) * mat(0, 1) + mat(1, 1) * mat(1, 1) + mat(2, 1) * mat(2, 1));
ret.scale[2] = std::sqrt(mat(0, 2) * mat(0, 2) + mat(1, 2) * mat(1, 2) + mat(2, 2) * mat(2, 2));

float isx = 1.0f / ret.scale[0];
float isy = 1.0f / ret.scale[1];
float isz = 1.0f / ret.scale[2];

// remove scaling from matrix
mat(0, 0) *= isx; mat(1, 0) *= isx; mat(1, 0) *= isx;
mat(0, 1) *= isy; mat(1, 1) *= isy; mat(2, 1) *= isy;
mat(0, 2) *= isz; mat(1, 2) *= isz; mat(2, 2) *= isz;

// Construct the quaternion. This algo is copied from here:
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm.
/*
ret.rotate[3] = std::max(0.0f, 1.0f + mat(0, 0) + mat(1, 1) + mat(2, 2));
ret.rotate[0] = std::max(0.0f, 1.0f + mat(0, 0) - mat(1, 1) - mat(2, 2));
ret.rotate[1] = std::max(0.0f, 1.0f - mat(0, 0) + mat(1, 1) - mat(2, 2));
ret.rotate[2] = std::max(0.0f, 1.0f - mat(0, 0) - mat(1, 1) - mat(2, 2));
for(std::size_t i = 0; i < 4; i++)
{
ret.rotate[i] = std::sqrt(ret.rotate[i] * 0.5f);
}
ret.rotate[0] = std::copysignf(ret.rotate[0], mat(1, 2) - mat(2, 1));
ret.rotate[1] = std::copysignf(ret.rotate[1], mat(2, 0) - mat(0, 2));
ret.rotate[2] = std::copysignf(ret.rotate[2], mat(0, 1) - mat(1, 0));
*/
return ret;
}


}

0 comments on commit 17699ce

Please sign in to comment.