Utility: Refactor some algorithms

This commit is contained in:
SirLynix
2022-05-12 18:15:20 +02:00
parent 7c2b8e0576
commit 6469ab5fde
7 changed files with 80 additions and 58 deletions

View File

@@ -86,6 +86,7 @@ namespace Nz
static Quaternion LookAt(const Vector3<T>& forward, const Vector3<T>& up);
static Quaternion Normalize(const Quaternion& quat, T* length = nullptr);
static Quaternion RotationBetween(const Vector3<T>& from, const Vector3<T>& to);
static Quaternion Mirror(Quaternion quat, const Vector3<T>& axis);
static Quaternion Slerp(const Quaternion& from, const Quaternion& to, T interpolation);
static Quaternion Zero();

View File

@@ -787,6 +787,20 @@ namespace Nz
return quaternion;
}
template<typename T>
Quaternion<T> Quaternion<T>::Mirror(Quaternion quat, const Vector3<T>& axis)
{
float x = std::copysign(T(1.0), axis.x);
float y = std::copysign(T(1.0), axis.y);
float z = std::copysign(T(1.0), axis.z);
quat.x = y * z * quat.x;
quat.y = x * z * quat.y;
quat.z = x * y * quat.z;
return quat;
}
/*!
* \brief Interpolates spherically the quaternion to other one with a factor of interpolation
* \return A new quaternion which is the interpolation of two quaternions

View File

@@ -64,11 +64,17 @@ namespace Nz
NAZARA_UTILITY_API void SkinPositionNormal(const SkinningData& data, UInt64 startVertex, UInt64 vertexCount);
NAZARA_UTILITY_API void SkinPositionNormalTangent(const SkinningData& data, UInt64 startVertex, UInt64 vertexCount);
NAZARA_UTILITY_API void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix);
inline Vector3f TransformPositionTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& position);
inline Vector3f TransformNormalTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& normal);
inline Quaternionf TransformRotationTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Quaternionf& rotation);
inline Vector3f TransformScaleTRS(const Vector3f& transformScale, const Vector3f& scale);
inline void TransformTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, Vector3f& position, Quaternionf& rotation, Vector3f& scale);
inline void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix);
template<typename T> constexpr ComponentType ComponentTypeId();
template<typename T> constexpr ComponentType GetComponentTypeOf();
}
#include <Nazara/Utility/Algorithm.inl>
#endif // NAZARA_UTILITY_ALGORITHM_HPP

View File

@@ -8,6 +8,59 @@
namespace Nz
{
inline Vector3f TransformPositionTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& position)
{
return transformRotation * (transformScale * position) + transformTranslation;
}
Vector3f TransformNormalTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& normal)
{
return Quaternionf::Mirror(transformRotation, transformScale) * normal;
}
inline Quaternionf TransformRotationTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Quaternionf& rotation)
{
return Quaternionf::Mirror(transformRotation, transformScale) * rotation;
}
inline Vector3f TransformScaleTRS(const Vector3f& transformScale, const Vector3f& scale)
{
return transformScale * scale;
}
inline void TransformTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, Vector3f& position, Quaternionf& rotation, Vector3f& scale)
{
position = TransformPositionTRS(transformTranslation, transformRotation, transformScale, position);
rotation = TransformRotationTRS(transformRotation, transformScale, rotation);
scale = TransformScaleTRS(transformScale, scale);
}
inline void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix)
{
if (vertexPointers.positionPtr)
{
for (UInt64 i = 0; i < vertexCount; ++i)
*vertexPointers.positionPtr++ = matrix.Transform(*vertexPointers.positionPtr);
}
if (vertexPointers.normalPtr || vertexPointers.tangentPtr)
{
Vector3f scale = matrix.GetScale();
if (vertexPointers.normalPtr)
{
for (UInt64 i = 0; i < vertexCount; ++i)
*vertexPointers.normalPtr++ = matrix.Transform(*vertexPointers.normalPtr, 0.f) / scale;
}
if (vertexPointers.tangentPtr)
{
for (UInt64 i = 0; i < vertexCount; ++i)
*vertexPointers.tangentPtr++ = matrix.Transform(*vertexPointers.tangentPtr, 0.f) / scale;
}
}
}
template<typename T> constexpr ComponentType ComponentTypeId()
{
static_assert(AlwaysFalse<T>::value, "This type cannot be used as a component.");

View File

@@ -108,8 +108,6 @@ namespace Nz
virtual void UpdateDerived() const;
virtual void UpdateTransformMatrix() const;
static Quaternionf ScaleQuaternion(const Vector3f& scale, Quaternionf quaternion);
mutable std::vector<Node*> m_childs;
mutable Matrix4f m_transformMatrix;
mutable Quaternionf m_derivedRotation;