-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core.trs] added trs library, note that rotations are not yet impleme…
…nted coz quaternions dont exist yet
- Loading branch information
Showing
5 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |