Improved math operation with epsilon support

pull/261/head
Léo Terziman 2013-10-11 16:54:07 +02:00
parent 6c0ebb679a
commit 365b3aa412
11 changed files with 85 additions and 24 deletions

View File

@ -115,21 +115,11 @@ void ColladaExporter::WriteFile()
mOutput << "</COLLADA>" << endstr;
}
// ------------------------------------------------------------------------------------------------
// Utility function to test equality of scalars and quaternions
inline bool equal(double a, double b, double epsilon) {
return std::abs(a - b) <= epsilon;
}
inline bool equal(aiQuaternion a, aiQuaternion b, double epsilon) {
return equal(a.x, b.x, epsilon) && equal(a.y, b.y, epsilon) && equal(a.z, b.z, epsilon) && equal(a.w, b.w, epsilon);
}
// ------------------------------------------------------------------------------------------------
// Writes the asset header
void ColladaExporter::WriteHeader()
{
static const double epsilon = 0.000001;
static const float epsilon = 0.000001f;
static const aiQuaternion x_rot(aiMatrix3x3(
0, -1, 0,
1, 0, 0,
@ -156,18 +146,18 @@ void ColladaExporter::WriteHeader()
mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position);
float scale = 1.0;
if(equal(scaling.x, scaling.y, epsilon) && equal(scaling.x, scaling.z, epsilon) && equal(scaling.y, scaling.z, epsilon)) {
if(std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) {
scale = scaling.x;
} else {
DefaultLogger::get()->warn("Collada: Unable to compute the global scale of the scene " + scene_name);
}
std::string up_axis = "Y_UP";
if(equal(rotation, x_rot, epsilon)) {
if(rotation.Equal(x_rot, epsilon)) {
up_axis = "X_UP";
} else if(equal(rotation, y_rot, epsilon)) {
} else if(rotation.Equal(y_rot, epsilon)) {
up_axis = "Y_UP";
} else if(equal(rotation, z_rot, epsilon)) {
} else if(rotation.Equal(z_rot, epsilon)) {
up_axis = "Z_UP";
} else {
DefaultLogger::get()->warn("Collada: Unable to compute the up axis of the scene " + scene_name);

View File

@ -90,8 +90,10 @@ public:
const TReal* operator[] (unsigned int p_iIndex) const;
// comparison operators
bool operator== (const aiMatrix4x4t<TReal> m) const;
bool operator!= (const aiMatrix4x4t<TReal> m) const;
bool operator== (const aiMatrix4x4t<TReal>& m) const;
bool operator!= (const aiMatrix4x4t<TReal>& m) const;
bool Equal(const aiMatrix4x4t<TReal>& m, float epsilon = 1e-6) const;
template <typename TOther>
operator aiMatrix3x3t<TOther> () const;

View File

@ -113,7 +113,7 @@ inline const TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) cons
// ------------------------------------------------------------------------------------------------
template <typename TReal>
inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const
inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const
{
return a1 == m.a1 && a2 == m.a2 && a3 == m.a3 &&
b1 == m.b1 && b2 == m.b2 && b3 == m.b3 &&
@ -122,11 +122,26 @@ inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const
// ------------------------------------------------------------------------------------------------
template <typename TReal>
inline bool aiMatrix3x3t<TReal>::operator!= (const aiMatrix4x4t<TReal> m) const
inline bool aiMatrix3x3t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const
{
return !(*this == m);
}
// ---------------------------------------------------------------------------
template<typename TReal>
inline bool aiMatrix3x3t<TReal>::Equal(const aiMatrix4x4t<TReal>& m, float epsilon) const {
return
std::abs(a1 - m.a1) <= epsilon &&
std::abs(a2 - m.a2) <= epsilon &&
std::abs(a3 - m.a3) <= epsilon &&
std::abs(b1 - m.b1) <= epsilon &&
std::abs(b2 - m.b2) <= epsilon &&
std::abs(b3 - m.b3) <= epsilon &&
std::abs(c1 - m.c1) <= epsilon &&
std::abs(c2 - m.c2) <= epsilon &&
std::abs(c3 - m.c3) <= epsilon;
}
// ------------------------------------------------------------------------------------------------
template <typename TReal>
inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Transpose()

View File

@ -94,8 +94,10 @@ public:
const TReal* operator[] (unsigned int p_iIndex) const;
// comparison operators
bool operator== (const aiMatrix4x4t m) const;
bool operator!= (const aiMatrix4x4t m) const;
bool operator== (const aiMatrix4x4t& m) const;
bool operator!= (const aiMatrix4x4t& m) const;
bool Equal(const aiMatrix4x4t& m, float epsilon = 1e-6) const;
// matrix multiplication.
aiMatrix4x4t& operator *= (const aiMatrix4x4t& m);

View File

@ -254,7 +254,7 @@ inline const TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) const
// ----------------------------------------------------------------------------------------
template <typename TReal>
inline bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const
inline bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const
{
return (a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && a4 == m.a4 &&
b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && b4 == m.b4 &&
@ -264,11 +264,33 @@ inline bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const
// ----------------------------------------------------------------------------------------
template <typename TReal>
inline bool aiMatrix4x4t<TReal>::operator!= (const aiMatrix4x4t<TReal> m) const
inline bool aiMatrix4x4t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const
{
return !(*this == m);
}
// ---------------------------------------------------------------------------
template<typename TReal>
inline bool aiMatrix4x4t<TReal>::Equal(const aiMatrix4x4t<TReal>& m, float epsilon) const {
return
std::abs(a1 - m.a1) <= epsilon &&
std::abs(a2 - m.a2) <= epsilon &&
std::abs(a3 - m.a3) <= epsilon &&
std::abs(a4 - m.a4) <= epsilon &&
std::abs(b1 - m.b1) <= epsilon &&
std::abs(b2 - m.b2) <= epsilon &&
std::abs(b3 - m.b3) <= epsilon &&
std::abs(b4 - m.b4) <= epsilon &&
std::abs(c1 - m.c1) <= epsilon &&
std::abs(c2 - m.c2) <= epsilon &&
std::abs(c3 - m.c3) <= epsilon &&
std::abs(c4 - m.c4) <= epsilon &&
std::abs(d1 - m.d1) <= epsilon &&
std::abs(d2 - m.d2) <= epsilon &&
std::abs(d3 - m.d3) <= epsilon &&
std::abs(d4 - m.d4) <= epsilon;
}
// ----------------------------------------------------------------------------------------
template <typename TReal>
inline void aiMatrix4x4t<TReal>::Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,

View File

@ -79,6 +79,8 @@ public:
bool operator== (const aiQuaterniont& o) const;
bool operator!= (const aiQuaterniont& o) const;
bool Equal(const aiQuaterniont& o, float epsilon = 1e-6) const;
public:
/** Normalize the quaternion */

View File

@ -62,7 +62,15 @@ bool aiQuaterniont<TReal>::operator!= (const aiQuaterniont& o) const
return !(*this == o);
}
// ---------------------------------------------------------------------------
template<typename TReal>
inline bool aiQuaterniont<TReal>::Equal(const aiQuaterniont& o, float epsilon) const {
return
std::abs(x - o.x) <= epsilon &&
std::abs(y - o.y) <= epsilon &&
std::abs(z - o.z) <= epsilon &&
std::abs(w - o.w) <= epsilon;
}
// ---------------------------------------------------------------------------
// Constructs a quaternion from a rotation matrix

View File

@ -83,6 +83,8 @@ public:
bool operator== (const aiVector2t& other) const;
bool operator!= (const aiVector2t& other) const;
bool Equal(const aiVector2t& other, float epsilon = 1e-6) const;
aiVector2t& operator= (TReal f);
const aiVector2t SymMul(const aiVector2t& o);

View File

@ -131,6 +131,14 @@ bool aiVector2t<TReal>::operator!= (const aiVector2t& other) const {
return x != other.x || y != other.y;
}
// ---------------------------------------------------------------------------
template<typename TReal>
bool aiVector2t<TReal>::Equal(const aiVector2t& other, float epsilon) const {
return
std::abs(x - other.x) <= epsilon &&
std::abs(y - other.y) <= epsilon;
}
// ------------------------------------------------------------------------------------------------
template <typename TReal>
aiVector2t<TReal>& aiVector2t<TReal>::operator= (TReal f) {

View File

@ -86,6 +86,8 @@ public:
bool operator== (const aiVector3t& other) const;
bool operator!= (const aiVector3t& other) const;
bool Equal(const aiVector3t& other, float epsilon = 1e-6) const;
template <typename TOther>
operator aiVector3t<TOther> () const;

View File

@ -147,6 +147,14 @@ template <typename TReal>
AI_FORCE_INLINE bool aiVector3t<TReal>::operator!= (const aiVector3t<TReal>& other) const {
return x != other.x || y != other.y || z != other.z;
}
// ---------------------------------------------------------------------------
template<typename TReal>
AI_FORCE_INLINE bool aiVector3t<TReal>::Equal(const aiVector3t<TReal>& other, float epsilon) const {
return
std::abs(x - other.x) <= epsilon &&
std::abs(y - other.y) <= epsilon &&
std::abs(z - other.z) <= epsilon;
}
// ------------------------------------------------------------------------------------------------
template <typename TReal>
AI_FORCE_INLINE const aiVector3t<TReal> aiVector3t<TReal>::SymMul(const aiVector3t<TReal>& o) {