Sfoglia il codice sorgente

Added equality operators for Matrix classes.

Lasse Öörni 12 anni fa
parent
commit
f226c4bc4a

+ 3 - 0
Docs/ScriptAPI.dox

@@ -434,6 +434,7 @@ Methods:<br>
 - void SetScale(float)
 - Matrix3 Transpose() const
 - Matrix3 Inverse() const
+- bool Equals(const Matrix3&) const
 
 Properties:<br>
 - float m00
@@ -462,6 +463,7 @@ Methods:<br>
 - Matrix4 Transpose() const
 - void Decompose(Vector3&, Quaternion&, Vector3&) const
 - Matrix4 Inverse() const
+- bool Equals(const Matrix4&) const
 
 Properties:<br>
 - float m00
@@ -496,6 +498,7 @@ Methods:<br>
 - Vector3 Translation() const
 - void Decompose(Vector3&, Quaternion&, Vector3&) const
 - Matrix3x4 Inverse() const
+- bool Equals(const Matrix3x4&) const
 
 Properties:<br>
 - float m00

+ 6 - 0
Engine/Engine/MathAPI.cpp

@@ -503,12 +503,14 @@ static void RegisterMatrix3(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Matrix3", "Matrix3 opAdd(const Matrix3&in) const", asMETHODPR(Matrix3, operator +, (const Matrix3 &) const, Matrix3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "Matrix3 opSub(const Matrix3&in) const", asMETHODPR(Matrix3, operator -, (const Matrix3 &) const, Matrix3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "Matrix3& opAssign(const Matrix3&in)", asMETHODPR(Matrix3, operator =, (const Matrix3 &), Matrix3&), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Matrix3", "bool opEquals(const Matrix3&in) const", asMETHOD(Matrix3, operator ==), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "Vector3 Scale() const", asMETHODPR(Matrix3, Scale, () const, Vector3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "Matrix3 Scaled(const Vector3&in) const", asMETHODPR(Matrix3, Scaled, (const Vector3 &) const, Matrix3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "void SetScale(const Vector3&in)", asMETHODPR(Matrix3,SetScale, (const Vector3 &), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "void SetScale(float)", asMETHODPR(Matrix3,SetScale, (float), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "Matrix3 Transpose() const", asMETHODPR(Matrix3, Transpose, () const, Matrix3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3", "Matrix3 Inverse() const", asMETHODPR(Matrix3, Inverse, () const, Matrix3), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Matrix3", "bool Equals(const Matrix3&in) const", asMETHOD(Matrix3, Equals), asCALL_THISCALL);
     engine->RegisterObjectProperty("Matrix3", "float m00", offsetof(Matrix3, m00_));
     engine->RegisterObjectProperty("Matrix3", "float m01", offsetof(Matrix3, m01_));
     engine->RegisterObjectProperty("Matrix3", "float m02", offsetof(Matrix3, m02_));
@@ -555,6 +557,7 @@ static void RegisterMatrix4(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Matrix4", "Matrix4 opSub(const Matrix4&) const", asMETHODPR(Matrix4, operator -, (const Matrix4&) const, Matrix4), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "Matrix4& opAssign(const Matrix3&)", asMETHODPR(Matrix4, operator =, (const Matrix3&), Matrix4&), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "Matrix4& opAssign(const Matrix4&)", asMETHODPR(Matrix4, operator =, (const Matrix4&), Matrix4&), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Matrix4", "bool opEquals(const Matrix4&in) const", asMETHOD(Matrix4, operator ==), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "Quaternion Rotation() const", asMETHODPR(Matrix4, Rotation, () const, Quaternion), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "Matrix3 RotationMatrix() const", asMETHODPR(Matrix4, RotationMatrix, () const, Matrix3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "Vector3 Scale() const", asMETHODPR(Matrix4, Scale, () const, Vector3), asCALL_THISCALL);
@@ -567,6 +570,7 @@ static void RegisterMatrix4(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Matrix4", "Matrix4 Transpose() const", asMETHODPR(Matrix4, Transpose, () const, Matrix4), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "void Decompose(Vector3&, Quaternion&, Vector3&) const", asMETHODPR(Matrix4,Decompose, (Vector3 &, Quaternion &, Vector3 &) const, void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix4", "Matrix4 Inverse() const", asMETHODPR(Matrix4, Inverse, () const, Matrix4), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Matrix4", "bool Equals(const Matrix4&in) const", asMETHOD(Matrix4, Equals), asCALL_THISCALL);
     engine->RegisterObjectProperty("Matrix4", "float m00", offsetof(Matrix4, m00_));
     engine->RegisterObjectProperty("Matrix4", "float m01", offsetof(Matrix4, m01_));
     engine->RegisterObjectProperty("Matrix4", "float m02", offsetof(Matrix4, m02_));
@@ -640,6 +644,7 @@ static void RegisterMatrix3x4(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Matrix3x4", "Matrix3x4& opAssign(const Matrix3&in)", asMETHODPR(Matrix3x4, operator =, (const Matrix3&), Matrix3x4&), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "Matrix3x4& opAssign(const Matrix3x4&in)", asMETHODPR(Matrix3x4, operator =, (const Matrix3x4&), Matrix3x4&), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "Matrix3x4& opAssign(const Matrix4&in)", asMETHODPR(Matrix3x4, operator =, (const Matrix4&), Matrix3x4&), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Matrix3x4", "bool opEquals(const Matrix3x4&in) const", asMETHOD(Matrix3x4, operator ==), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "Quaternion Rotation() const", asMETHODPR(Matrix3x4, Rotation, () const, Quaternion), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "Matrix3 RotationMatrix() const", asMETHODPR(Matrix3x4, RotationMatrix, () const, Matrix3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "Vector3 Scale() const", asMETHODPR(Matrix3x4, Scale, () const, Vector3), asCALL_THISCALL);
@@ -651,6 +656,7 @@ static void RegisterMatrix3x4(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Matrix3x4", "Vector3 Translation() const", asMETHODPR(Matrix3x4, Translation, () const, Vector3), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "void Decompose(Vector3&, Quaternion&, Vector3&) const", asMETHODPR(Matrix3x4, Decompose, (Vector3&, Quaternion&, Vector3&) const, void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Matrix3x4", "Matrix3x4 Inverse() const", asMETHODPR(Matrix3x4, Inverse, () const, Matrix3x4), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Matrix3x4", "bool Equals(const Matrix3x4&in) const", asMETHOD(Matrix3x4, Equals), asCALL_THISCALL);
     engine->RegisterObjectProperty("Matrix3x4", "float m00", offsetof(Matrix3x4, m00_));
     engine->RegisterObjectProperty("Matrix3x4", "float m01", offsetof(Matrix3x4, m01_));
     engine->RegisterObjectProperty("Matrix3x4", "float m02", offsetof(Matrix3x4, m02_));

+ 1 - 9
Engine/Graphics/DecalSet.cpp

@@ -906,15 +906,7 @@ bool DecalSet::GetBones(Drawable* target, unsigned batchIndex, const float* blen
                 {
                     // Check also that the offset matrix matches, in case we for example have a separate attachment AnimatedModel
                     // with a different bind pose
-                    const float* offsetA = bones_[index].offsetMatrix_.Data();
-                    const float* offsetB = bone->offsetMatrix_.Data();
-                    unsigned j;
-                    for (j = 0; j < 12; ++j)
-                    {
-                        if (!Equals(offsetA[j], offsetB[j]))
-                            break;
-                    }
-                    if (j == 12)
+                    if (bones_[index].offsetMatrix_.Equals(bone->offsetMatrix_))
                     {
                         found = true;
                         break;

+ 33 - 0
Engine/Math/Matrix3.h

@@ -95,6 +95,24 @@ public:
         return *this;
     }
     
+    /// Test for equality with another matrix without epsilon.
+    bool operator == (const Matrix3& rhs) const
+    {
+        const float* leftData = Data();
+        const float* rightData = rhs.Data();
+        
+        for (unsigned i = 0; i < 9; ++i)
+        {
+            if (leftData[i] != rightData[i])
+                return false;
+        }
+        
+        return true;
+    }
+    
+    /// Test for inequality with another matrix without epsilon.
+    bool operator != (const Matrix3& rhs) const { return !(*this == rhs); }
+    
     /// Multiply a Vector3.
     Vector3 operator * (const Vector3& rhs) const
     {
@@ -227,6 +245,21 @@ public:
         );
     }
     
+    /// Test for equality with another matrix with epsilon.
+    bool Equals(const Matrix3& rhs) const
+    {
+        const float* leftData = Data();
+        const float* rightData = rhs.Data();
+        
+        for (unsigned i = 0; i < 9; ++i)
+        {
+            if (!Urho3D::Equals(leftData[i], rightData[i]))
+                return false;
+        }
+        
+        return true;
+    }
+    
     /// Return inverse.
     Matrix3 Inverse() const;
     

+ 33 - 0
Engine/Math/Matrix3x4.h

@@ -182,6 +182,24 @@ public:
         return *this;
     }
     
+    /// Test for equality with another matrix without epsilon.
+    bool operator == (const Matrix3x4& rhs) const
+    {
+        const float* leftData = Data();
+        const float* rightData = rhs.Data();
+        
+        for (unsigned i = 0; i < 12; ++i)
+        {
+            if (leftData[i] != rightData[i])
+                return false;
+        }
+        
+        return true;
+    }
+    
+    /// Test for inequality with another matrix without epsilon.
+    bool operator != (const Matrix3x4& rhs) const { return !(*this == rhs); }
+    
     /// Multiply a Vector3 which is assumed to represent position.
     Vector3 operator * (const Vector3& rhs) const
     {
@@ -390,6 +408,21 @@ public:
         );
     }
     
+    /// Test for equality with another matrix with epsilon.
+    bool Equals(const Matrix3x4& rhs) const
+    {
+        const float* leftData = Data();
+        const float* rightData = rhs.Data();
+        
+        for (unsigned i = 0; i < 12; ++i)
+        {
+            if (!Urho3D::Equals(leftData[i], rightData[i]))
+                return false;
+        }
+        
+        return true;
+    }
+    
     /// Return decomposition to translation, rotation and scale.
     void Decompose(Vector3& translation, Quaternion& rotation, Vector3& scale) const;
     /// Return inverse.

+ 33 - 0
Engine/Math/Matrix4.h

@@ -168,6 +168,24 @@ public:
         return *this;
     }
     
+    /// Test for equality with another matrix without epsilon.
+    bool operator == (const Matrix4& rhs) const
+    {
+        const float* leftData = Data();
+        const float* rightData = rhs.Data();
+        
+        for (unsigned i = 0; i < 16; ++i)
+        {
+            if (leftData[i] != rightData[i])
+                return false;
+        }
+        
+        return true;
+    }
+    
+    /// Test for inequality with another matrix without epsilon.
+    bool operator != (const Matrix4& rhs) const { return !(*this == rhs); }
+    
     /// Multiply a Vector3 which is assumed to represent position.
     Vector3 operator * (const Vector3& rhs) const
     {
@@ -395,6 +413,21 @@ public:
         );
     }
     
+    /// Test for equality with another matrix with epsilon.
+    bool Equals(const Matrix4& rhs) const
+    {
+        const float* leftData = Data();
+        const float* rightData = rhs.Data();
+        
+        for (unsigned i = 0; i < 16; ++i)
+        {
+            if (!Urho3D::Equals(leftData[i], rightData[i]))
+                return false;
+        }
+        
+        return true;
+    }
+    
     /// Return decomposition to translation, rotation and scale
     void Decompose(Vector3& translation, Quaternion& rotation, Vector3& scale) const;
     /// Return inverse