Obj: prepare unittest and add missing tests for mesh comparison in
authorKim Kulling <kim.kulling@googlemail.com>
Tue, 20 Sep 2016 09:27:58 +0000 (11:27 +0200)
committerKim Kulling <kim.kulling@googlemail.com>
Tue, 20 Sep 2016 09:27:58 +0000 (11:27 +0200)
Modeldiffer.

test/CMakeLists.txt
test/unit/ModelDiffer.cpp
test/unit/ModelDiffer.h
test/unit/utObjImportExport.cpp [new file with mode: 0644]

index 10ff864..3227577 100644 (file)
@@ -76,6 +76,9 @@ SET( TEST_SRCS
   unit/utMaterialSystem.cpp
   unit/utMatrix3x3.cpp
   unit/utMatrix4x4.cpp
+  unit/ModelDiffer.h
+  unit/ModelDiffer.cpp
+  unit/utObjImportExport.cpp
   unit/utPretransformVertices.cpp
   unit/utRemoveComments.cpp
   unit/utRemoveComponent.cpp
index 8234643..9f12b38 100644 (file)
@@ -40,6 +40,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 #include "ModelDiffer.h"
 #include <assimp/scene.h>
+#include <assimp/mesh.h>
+#include <assimp/material.h>
 #include <sstream>
 
 using namespace Assimp;
@@ -65,17 +67,42 @@ bool ModelDiffer::isEqual( aiScene *expected, aiScene *toCompare ) {
         return false;
     }
 
+    // meshes
     if ( expected->mNumMeshes != toCompare->mNumMeshes ) {
         std::stringstream stream;
         stream << "Number of meshes not equal ( expected: " << expected->mNumMeshes << ", found : " << toCompare->mNumMeshes << " )\n";
         addDiff( stream.str() );
+        return false;
     }
 
     for ( unsigned int i = 0; i < expected->mNumMeshes; i++ ) {
         aiMesh *expMesh( expected->mMeshes[ i ] );
         aiMesh *toCompMesh( toCompare->mMeshes[ i ] );
-        compareMesh( expMesh, toCompMesh );
+        if ( !compareMesh( expMesh, toCompMesh ) ) {
+            std::stringstream stream;
+            stream << "Meshes are not equal, index : " << i << "\n";
+            addDiff( stream.str() );
+        }
+    }
+
+    // materials
+    if ( expected->mNumMaterials != toCompare->mNumMaterials ) {
+        std::stringstream stream;
+        stream << "Number of materials not equal ( expected: " << expected->mNumMaterials << ", found : " << toCompare->mNumMaterials << " )\n";
+        addDiff( stream.str() );
+        return false;
     }
+    for ( unsigned int i = 0; i < expected->mNumMaterials; i++ ) {
+        aiMaterial *expectedMat( expected->mMaterials[ i ] );
+        aiMaterial *toCompareMat( expected->mMaterials[ i ] );
+        if ( !compareMaterial( expectedMat, toCompareMat ) ) {
+            std::stringstream stream;
+            stream << "Materials are not equal, index : " << i << "\n";
+            addDiff( stream.str() );
+        }
+    }
+
+    return true;
 }
 
 void ModelDiffer::showReport() {
@@ -241,7 +268,7 @@ bool ModelDiffer::compareMesh( aiMesh *expected, aiMesh *toCompare ) {
         aiVector3D &toCompBiTangents( toCompare->mBitangents[ i ] );
         if ( expBiTangents != toCompBiTangents ) {
             std::stringstream stream;
-            stream << "Tangents not equal ( expected: " << dumpVector3( expBiTangents ) << ", found: " << dumpVector3( toCompBiTangents ) << "\n";
+            stream << "Tangents not equal ( expected: " << dumpVector3( expBiTangents ) << ", found: " << dumpVector3( toCompBiTangents ) << " )\n";
             addDiff( stream.str() );
             tangentsEqual = false;
         }
@@ -250,5 +277,64 @@ bool ModelDiffer::compareMesh( aiMesh *expected, aiMesh *toCompare ) {
         return false;
     }
 
+    // faces
+    if ( expected->mNumFaces != toCompare->mNumFaces ) {
+        std::stringstream stream;
+        stream << "Number of faces are not equal, ( expected: " << expected->mNumFaces << ", found: " << toCompare->mNumFaces << ")\n";
+        addDiff( stream.str() );
+        return false;
+    }
+    bool facesEqual( true );
+    for ( unsigned int i = 0; i < expected->mNumFaces; i++ ) {
+        aiFace &expFace( expected->mFaces[ i ] );
+        aiFace &toCompareFace( expected->mFaces[ i ] );
+        if ( !compareFace( &expFace, &toCompareFace ) ) {
+            facesEqual = false;
+        }
+    }
+    if ( !facesEqual ) {
+        return false;
+    }
+
+    return true;
+}
+
+bool ModelDiffer::compareFace( aiFace *expected, aiFace *toCompare ) {
+    if ( nullptr == expected ) {
+        return false;
+    }
+    if ( nullptr == toCompare ) {
+        return false;
+    }
+
+    // same instance
+    if ( expected == toCompare ) {
+        return true;
+    }
+
+    // using compare operator
+    if ( *expected == *toCompare ) {
+        return true;
+    }
+
+    return false;
+}
+
+bool ModelDiffer::compareMaterial( aiMaterial *expected, aiMaterial *toCompare ) {
+    if ( nullptr == expected ) {
+        return false;
+    }
+    if ( nullptr == toCompare ) {
+        return false;
+    }
+
+    // same instance
+    if ( expected == toCompare ) {
+        return true;
+    }
+
+    // todo!
+
     return true;
+
 }
index 03c4462..ed2e14e 100644 (file)
@@ -47,6 +47,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 struct aiScene;
 struct aiMesh;
+struct aiMaterial;
+struct aiFace;
 
 class ModelDiffer {
 public:
@@ -56,9 +58,11 @@ public:
     void showReport();
     void reset();
 
-private:
+protected:
     void addDiff( const std::string &diff );
     bool compareMesh( aiMesh *expected, aiMesh *toCompare );
+    bool compareFace( aiFace *expected, aiFace *toCompare );
+    bool compareMaterial( aiMaterial *expected, aiMaterial *toCompare );
 
 private:
     std::vector<std::string> m_diffs;
diff --git a/test/unit/utObjImportExport.cpp b/test/unit/utObjImportExport.cpp
new file mode 100644 (file)
index 0000000..1c5f89b
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+---------------------------------------------------------------------------
+Open Asset Import Library (assimp)
+---------------------------------------------------------------------------
+
+Copyright (c) 2006-2016, assimp team
+
+All rights reserved.
+
+Redistribution and use of this software in source and binary forms,
+with or without modification, are permitted provided that the following
+conditions are met:
+
+* Redistributions of source code must retain the above
+copyright notice, this list of conditions and the
+following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the
+following disclaimer in the documentation and/or other
+materials provided with the distribution.
+
+* Neither the name of the assimp team, nor the names of its
+contributors may be used to endorse or promote products
+derived from this software without specific prior
+written permission of the assimp team.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+---------------------------------------------------------------------------
+*/
+
+#include "UnitTestPCH.h"
+#include "ModelDiffer.h"
+#include <assimp/Importer.hpp>
+using namespace Assimp;
+
+class utObjImportExport : public ::testing::Test {
+    // empty
+};
+
+static const std::string ObjModel =
+    "o 1\n"
+    "\n"
+    "# Vertex list\n"
+    "\n"
+    "v -0.5 -0.5 0.5\n"
+    "v -0.5 -0.5 -0.5\n"
+    "v -0.5  0.5 -0.5\n"
+    "v -0.5  0.5  0.5\n"
+    "v  0.5 -0.5  0.5\n"
+    "v  0.5 -0.5 -0.5\n"
+    "v  0.5  0.5 -0.5\n"
+    "v  0.5  0.5  0.5\n"
+    "\n"
+    "# Point / Line / Face list\n"
+    "\n"
+    "usemtl Default\n"
+    "f 4 3 2 1\n"
+    "f 2 6 5 1\n"
+    "f 3 7 6 2\n"
+    "f 8 7 3 4\n"
+    "f 5 8 4 1\n"
+    "f 6 7 8 5\n"
+    "\n"
+    "# End of file\n";
+
+TEST_F( utObjImportExport, obj_import_test ) {
+    Assimp::Importer im;
+    const aiScene *scene = im.ReadFileFromMemory( (void*) ObjModel.c_str(), ObjModel.size(), 0 );
+    EXPECT_NE( nullptr, scene );
+}