Math/Matrix4: Remove implicit conversion to pointer

This commit is contained in:
Jérôme Leclercq 2021-07-03 18:20:00 +02:00
parent ffb28a9044
commit f2bb1a839c
3 changed files with 57 additions and 70 deletions

View File

@ -12,6 +12,7 @@
#include <Nazara/Core/TypeTag.hpp> #include <Nazara/Core/TypeTag.hpp>
#include <Nazara/Math/Angle.hpp> #include <Nazara/Math/Angle.hpp>
#include <Nazara/Math/Config.hpp> #include <Nazara/Math/Config.hpp>
#include <cstddef>
#include <string> #include <string>
namespace Nz namespace Nz
@ -100,11 +101,11 @@ namespace Nz
Matrix4& Transpose(); Matrix4& Transpose();
operator T* (); T& operator()(std::size_t x, std::size_t y);
operator const T* () const; T operator()(std::size_t x, std::size_t y) const;
T& operator()(unsigned int x, unsigned int y); T& operator[](std::size_t i);
T operator()(unsigned int x, unsigned int y) const; T operator[](std::size_t i) const;
Matrix4& operator=(const Matrix4& matrix) = default; Matrix4& operator=(const Matrix4& matrix) = default;

View File

@ -1287,80 +1287,64 @@ namespace Nz
return *this; return *this;
} }
/*!
* \brief Converts matrix to pointer to its own data
* \return A pointer to the own data
*
* \remark Access to index greather than 15 is undefined behavior
*/
template<typename T>
Matrix4<T>::operator T* ()
{
return &m11;
}
/*!
* \brief Converts matrix to pointer to its own data
* \return A constant pointer to the own data
*
* \remark Access to index greather than 15 is undefined behavior
*/
template<typename T>
Matrix4<T>::operator const T* () const
{
return &m11;
}
/*! /*!
* \brief Gets the component (x, y) of the matrix * \brief Gets the component (x, y) of the matrix
* \return A reference to the component (x, y) * \return A reference to the component (x, y)
* *
* \remark Produce a NazaraError if you try to access index greater than 3 for x or y with NAZARA_MATH_SAFE defined * \remark x and y must both be comprised in range [0,4[
* \throw std::out_of_range if NAZARA_MATH_SAFE is defined and if you try to access index greater than 3 for x or y
*/ */
template<typename T> template<typename T>
T& Matrix4<T>::operator()(unsigned int x, unsigned int y) T& Matrix4<T>::operator()(std::size_t x, std::size_t y)
{ {
#if NAZARA_MATH_SAFE NazaraAssert(x <= 3, "index out of range");
if (x > 3 || y > 3) NazaraAssert(y <= 3, "index out of range");
{
std::string error("Index out of range: (" + NumberToString(x) + ", " + NumberToString(y) +") > (3, 3)");
NazaraError(error); return (&m11)[y*4 + x];
throw std::out_of_range(error);
}
#endif
return (&m11)[y*4+x];
} }
/*! /*!
* \brief Gets the component (x, y) of the matrix * \brief Gets the component (x, y) of the matrix
* \return The value of the component (x, y) * \return The value of the component (x, y)
* *
* \remark Produce a NazaraError if you try to access index greater than 3 for x or y with NAZARA_MATH_SAFE defined * \remark x and y must both be comprised in range [0,4[
* \throw std::out_of_range if NAZARA_MATH_SAFE is defined and if you try to access index greater than 3 for x or y
*/ */
template<typename T> template<typename T>
T Matrix4<T>::operator()(unsigned int x, unsigned int y) const T Matrix4<T>::operator()(std::size_t x, std::size_t y) const
{ {
#if NAZARA_MATH_SAFE NazaraAssert(x <= 3, "index out of range");
if (x > 3 || y > 3) NazaraAssert(y <= 3, "index out of range");
{
std::string error("Index out of range: (" + NumberToString(x) + ", " + NumberToString(y) +") > (3, 3)");
NazaraError(error);
throw std::out_of_range(error);
}
#endif
return (&m11)[y*4+x]; return (&m11)[y*4+x];
} }
/*!
* \brief Gets the i-th component of the matrix
* \return The value of the component (i)
*
* \remark i must be comprised in range [0,16[
*/
template<typename T>
T& Matrix4<T>::operator[](std::size_t i)
{
NazaraAssert(i <= 16, "index out of range");
return (&m11)[i];
}
/*!
* \brief Gets the i-th component of the matrix
* \return The value of the component (i)
*
* \remark i must be comprised in range [0,16[
*/
template<typename T>
T Matrix4<T>::operator[](std::size_t i) const
{
NazaraAssert(i <= 16, "index out of range");
return (&m11)[i];
}
/*! /*!
* \brief Multiplies the components of the matrix with other matrix * \brief Multiplies the components of the matrix with other matrix
* \return A matrix where components are the product of this matrix and the other one according to matrix product * \return A matrix where components are the product of this matrix and the other one according to matrix product
@ -1771,7 +1755,7 @@ namespace Nz
template<typename T> template<typename T>
bool Unserialize(SerializationContext& context, Matrix4<T>* matrix, TypeTag<Matrix4<T>>) bool Unserialize(SerializationContext& context, Matrix4<T>* matrix, TypeTag<Matrix4<T>>)
{ {
T* head = matrix->operator T*(); T* head = &matrix->m11;
for (unsigned int i = 0; i < 16; ++i) for (unsigned int i = 0; i < 16; ++i)
{ {
if (!Unserialize(context, head + i)) if (!Unserialize(context, head + i))
@ -1814,3 +1798,4 @@ Nz::Matrix4<T> operator*(T scale, const Nz::Matrix4<T>& matrix)
} }
#include <Nazara/Core/DebugOff.hpp> #include <Nazara/Core/DebugOff.hpp>
#include "Matrix4.hpp"

View File

@ -60,12 +60,12 @@ namespace Nz
NewtonCollision* collision = CreateHandle(&world); NewtonCollision* collision = CreateHandle(&world);
{ {
NewtonCollisionCalculateAABB(collision, offsetMatrix, &min.x, &max.x); NewtonCollisionCalculateAABB(collision, &offsetMatrix.m11, &min.x, &max.x);
} }
NewtonDestroyCollision(collision); NewtonDestroyCollision(collision);
} }
else else
NewtonCollisionCalculateAABB(m_handles.begin()->second, offsetMatrix, &min.x, &max.x); NewtonCollisionCalculateAABB(m_handles.begin()->second, &offsetMatrix.m11, &min.x, &max.x);
return Boxf(scale * min, scale * max); return Boxf(scale * min, scale * max);
} }
@ -128,18 +128,19 @@ namespace Nz
}; };
// Check for existing collision handles, and create a temporary one if none is available // Check for existing collision handles, and create a temporary one if none is available
Matrix4f identity = Matrix4f::Identity();
if (m_handles.empty()) if (m_handles.empty())
{ {
PhysWorld3D world; PhysWorld3D world;
NewtonCollision* collision = CreateHandle(&world); NewtonCollision* collision = CreateHandle(&world);
{ {
NewtonCollisionForEachPolygonDo(collision, Nz::Matrix4f::Identity(), newtCallback, const_cast<void*>(static_cast<const void*>(&callback))); //< This isn't that bad; pointer will not be used for writing NewtonCollisionForEachPolygonDo(collision, &identity.m11, newtCallback, const_cast<void*>(static_cast<const void*>(&callback))); //< This isn't that bad; pointer will not be used for writing
} }
NewtonDestroyCollision(collision); NewtonDestroyCollision(collision);
} }
else else
NewtonCollisionForEachPolygonDo(m_handles.begin()->second, Nz::Matrix4f::Identity(), newtCallback, const_cast<void*>(static_cast<const void*>(&callback))); //< This isn't that bad; pointer will not be used for writing NewtonCollisionForEachPolygonDo(m_handles.begin()->second, &identity.m11, newtCallback, const_cast<void*>(static_cast<const void*>(&callback))); //< This isn't that bad; pointer will not be used for writing
} }
std::shared_ptr<StaticMesh> Collider3D::GenerateMesh() const std::shared_ptr<StaticMesh> Collider3D::GenerateMesh() const
@ -248,7 +249,7 @@ namespace Nz
NewtonCollision* BoxCollider3D::CreateHandle(PhysWorld3D* world) const NewtonCollision* BoxCollider3D::CreateHandle(PhysWorld3D* world) const
{ {
return NewtonCreateBox(world->GetHandle(), m_lengths.x, m_lengths.y, m_lengths.z, 0, m_matrix); return NewtonCreateBox(world->GetHandle(), m_lengths.x, m_lengths.y, m_lengths.z, 0, &m_matrix.m11);
} }
/******************************** CapsuleCollider3D ********************************/ /******************************** CapsuleCollider3D ********************************/
@ -282,7 +283,7 @@ namespace Nz
NewtonCollision* CapsuleCollider3D::CreateHandle(PhysWorld3D* world) const NewtonCollision* CapsuleCollider3D::CreateHandle(PhysWorld3D* world) const
{ {
return NewtonCreateCapsule(world->GetHandle(), m_radius, m_radius, m_length, 0, m_matrix); return NewtonCreateCapsule(world->GetHandle(), m_radius, m_radius, m_length, 0, &m_matrix.m11);
} }
/******************************* CompoundCollider3D ********************************/ /******************************* CompoundCollider3D ********************************/
@ -354,7 +355,7 @@ namespace Nz
NewtonCollision* ConeCollider3D::CreateHandle(PhysWorld3D* world) const NewtonCollision* ConeCollider3D::CreateHandle(PhysWorld3D* world) const
{ {
return NewtonCreateCone(world->GetHandle(), m_radius, m_length, 0, m_matrix); return NewtonCreateCone(world->GetHandle(), m_radius, m_length, 0, &m_matrix.m11);
} }
/****************************** ConvexCollider3D *******************************/ /****************************** ConvexCollider3D *******************************/
@ -385,7 +386,7 @@ namespace Nz
NewtonCollision* ConvexCollider3D::CreateHandle(PhysWorld3D* world) const NewtonCollision* ConvexCollider3D::CreateHandle(PhysWorld3D* world) const
{ {
return NewtonCreateConvexHull(world->GetHandle(), static_cast<int>(m_vertices.size()), reinterpret_cast<const float*>(m_vertices.data()), sizeof(Vector3f), m_tolerance, 0, m_matrix); return NewtonCreateConvexHull(world->GetHandle(), static_cast<int>(m_vertices.size()), reinterpret_cast<const float*>(m_vertices.data()), sizeof(Vector3f), m_tolerance, 0, &m_matrix.m11);
} }
/******************************* CylinderCollider3D ********************************/ /******************************* CylinderCollider3D ********************************/
@ -419,7 +420,7 @@ namespace Nz
NewtonCollision* CylinderCollider3D::CreateHandle(PhysWorld3D* world) const NewtonCollision* CylinderCollider3D::CreateHandle(PhysWorld3D* world) const
{ {
return NewtonCreateCylinder(world->GetHandle(), m_radius, m_radius, m_length, 0, m_matrix); return NewtonCreateCylinder(world->GetHandle(), m_radius, m_radius, m_length, 0, &m_matrix.m11);
} }
/********************************* NullCollider3D **********************************/ /********************************* NullCollider3D **********************************/
@ -454,11 +455,10 @@ namespace Nz
{ {
} }
SphereCollider3D::SphereCollider3D(float radius, const Vector3f& translation, const Quaternionf& rotation) : SphereCollider3D::SphereCollider3D(float radius, const Vector3f& translation, const Quaternionf& /*rotation*/) :
m_position(translation), m_position(translation),
m_radius(radius) m_radius(radius)
{ {
NazaraUnused(rotation);
} }
Boxf SphereCollider3D::ComputeAABB(const Matrix4f& offsetMatrix, const Vector3f& scale) const Boxf SphereCollider3D::ComputeAABB(const Matrix4f& offsetMatrix, const Vector3f& scale) const
@ -486,6 +486,7 @@ namespace Nz
NewtonCollision* SphereCollider3D::CreateHandle(PhysWorld3D* world) const NewtonCollision* SphereCollider3D::CreateHandle(PhysWorld3D* world) const
{ {
return NewtonCreateSphere(world->GetHandle(), m_radius, 0, Matrix4f::Translate(m_position)); Matrix4f transformMatrix = Matrix4f::Translate(m_position);
return NewtonCreateSphere(world->GetHandle(), m_radius, 0, &transformMatrix.m11);
} }
} }