ModelDiffer.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #include "ModelDiffer.h"
  35. #include <assimp/scene.h>
  36. #include <sstream>
  37. using namespace Assimp;
  38. ModelDiffer::ModelDiffer() {
  39. // empty
  40. }
  41. ModelDiffer::~ModelDiffer() {
  42. // empty
  43. }
  44. bool ModelDiffer::isEqual( aiScene *expected, aiScene *toCompare ) {
  45. if ( expected == toCompare ) {
  46. return true;
  47. }
  48. if ( nullptr == expected ) {
  49. return false;
  50. }
  51. if ( nullptr == toCompare ) {
  52. return false;
  53. }
  54. if ( expected->mNumMeshes != toCompare->mNumMeshes ) {
  55. std::stringstream stream;
  56. stream << "Number of meshes not equal ( expected: " << expected->mNumMeshes << ", found : " << toCompare->mNumMeshes << " )\n";
  57. addDiff( stream.str() );
  58. }
  59. for ( unsigned int i = 0; i < expected->mNumMeshes; i++ ) {
  60. aiMesh *expMesh( expected->mMeshes[ i ] );
  61. aiMesh *toCompMesh( toCompare->mMeshes[ i ] );
  62. compareMesh( expMesh, toCompMesh );
  63. }
  64. }
  65. void ModelDiffer::showReport() {
  66. if ( m_diffs.empty() ) {
  67. return;
  68. }
  69. for ( std::vector<std::string>::iterator it = m_diffs.begin(); it != m_diffs.end(); it++ ) {
  70. std::cout << *it << "\n";
  71. }
  72. std::cout << std::endl;
  73. }
  74. void ModelDiffer::reset() {
  75. m_diffs.resize( 0 );
  76. }
  77. void ModelDiffer::addDiff( const std::string &diff ) {
  78. if ( diff.empty() ) {
  79. return;
  80. }
  81. m_diffs.push_back( diff );
  82. }
  83. static std::string dumpVector3( const aiVector3D &toDump ) {
  84. std::stringstream stream;
  85. stream << "( " << toDump.x << ", " << toDump.y << ", " << toDump.z << ")";
  86. return stream.str();
  87. }
  88. static std::string dumpColor4D( const aiColor4D &toDump ) {
  89. std::stringstream stream;
  90. stream << "( " << toDump.r << ", " << toDump.g << ", " << toDump.b << ", " << toDump.a << ")";
  91. return stream.str();
  92. }
  93. bool ModelDiffer::compareMesh( aiMesh *expected, aiMesh *toCompare ) {
  94. if ( expected == toCompare ) {
  95. return true;
  96. }
  97. if ( nullptr == expected || nullptr == toCompare ) {
  98. return false;
  99. }
  100. if ( expected->mName != toCompare->mName ) {
  101. std::stringstream stream;
  102. stream << "Mesh name not equal ( expected: " << expected->mName.C_Str() << ", found : " << toCompare->mName.C_Str() << " )\n";
  103. addDiff( stream.str() );
  104. }
  105. if ( expected->mNumVertices != toCompare->mNumVertices ) {
  106. std::stringstream stream;
  107. stream << "Number of vertices not equal ( expected: " << expected->mNumVertices << ", found : " << toCompare->mNumVertices << " )\n";
  108. addDiff( stream.str() );
  109. return false;
  110. }
  111. // positions
  112. if ( expected->HasPositions() != toCompare->HasPositions() ) {
  113. addDiff( "Expected are vertices, toCompare does not have any." );
  114. return false;
  115. }
  116. bool vertEqual( true );
  117. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  118. aiVector3D &expVert( expected->mVertices[ i ] );
  119. aiVector3D &toCompVert( toCompare->mVertices[ i ] );
  120. if ( expVert != toCompVert ) {
  121. std::stringstream stream;
  122. stream << "Vertex not equal ( expected: " << dumpVector3( expVert ) << ", found: " << dumpVector3( toCompVert ) << "\n";
  123. addDiff( stream.str() );
  124. vertEqual = false;
  125. }
  126. }
  127. if ( !vertEqual ) {
  128. return false;
  129. }
  130. // normals
  131. if ( expected->HasNormals() != toCompare->HasNormals() ) {
  132. addDiff( "Expected are normals, toCompare does not have any." );
  133. return false;
  134. }
  135. bool normalEqual( true );
  136. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  137. aiVector3D &expNormal( expected->mNormals[ i ] );
  138. aiVector3D &toCompNormal( toCompare->mNormals[ i ] );
  139. if ( expNormal != toCompNormal ) {
  140. std::stringstream stream;
  141. stream << "Normal not equal ( expected: " << dumpVector3( expNormal ) << ", found: " << dumpVector3( toCompNormal ) << "\n";
  142. addDiff( stream.str() );
  143. normalEqual = false;
  144. }
  145. }
  146. if ( !normalEqual ) {
  147. return false;
  148. }
  149. // vertex colors
  150. bool vertColEqual( true );
  151. for ( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++ ) {
  152. if ( expected->HasVertexColors(a) != toCompare->HasVertexColors(a) ) {
  153. addDiff( "Expected are normals, toCompare does not have any." );
  154. return false;
  155. }
  156. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  157. aiColor4D &expColor4D( expected->mColors[ a ][ i ] );
  158. aiColor4D &toCompColor4D( toCompare->mColors[ a ][ i ] );
  159. if ( expColor4D != toCompColor4D ) {
  160. std::stringstream stream;
  161. stream << "Color4D not equal ( expected: " << dumpColor4D( expColor4D ) << ", found: " << dumpColor4D( toCompColor4D ) << "\n";
  162. addDiff( stream.str() );
  163. vertColEqual = false;
  164. }
  165. }
  166. if ( !vertColEqual ) {
  167. return false;
  168. }
  169. }
  170. // texture coords
  171. bool texCoordsEqual( true );
  172. for ( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++ ) {
  173. if ( expected->HasTextureCoords( a ) != toCompare->HasTextureCoords( a ) ) {
  174. addDiff( "Expected are texture coords, toCompare does not have any." );
  175. return false;
  176. }
  177. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  178. aiVector3D &expTexCoord( expected->mTextureCoords[ a ][ i ] );
  179. aiVector3D &toCompTexCoord( toCompare->mTextureCoords[ a ][ i ] );
  180. if ( expTexCoord != toCompTexCoord ) {
  181. std::stringstream stream;
  182. stream << "Texture coords not equal ( expected: " << dumpVector3( expTexCoord ) << ", found: " << dumpVector3( toCompTexCoord ) << "\n";
  183. addDiff( stream.str() );
  184. vertColEqual = false;
  185. }
  186. }
  187. if ( !vertColEqual ) {
  188. return false;
  189. }
  190. }
  191. // tangents and bi-tangents
  192. if ( expected->HasTangentsAndBitangents() != toCompare->HasTangentsAndBitangents() ) {
  193. addDiff( "Expected are tangents and bi-tangents, toCompare does not have any." );
  194. return false;
  195. }
  196. bool tangentsEqual( true );
  197. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  198. aiVector3D &expTangents( expected->mTangents[ i ] );
  199. aiVector3D &toCompTangents( toCompare->mTangents[ i ] );
  200. if ( expTangents != toCompTangents ) {
  201. std::stringstream stream;
  202. stream << "Tangents not equal ( expected: " << dumpVector3( expTangents ) << ", found: " << dumpVector3( toCompTangents ) << "\n";
  203. addDiff( stream.str() );
  204. tangentsEqual = false;
  205. }
  206. aiVector3D &expBiTangents( expected->mBitangents[ i ] );
  207. aiVector3D &toCompBiTangents( toCompare->mBitangents[ i ] );
  208. if ( expBiTangents != toCompBiTangents ) {
  209. std::stringstream stream;
  210. stream << "Tangents not equal ( expected: " << dumpVector3( expBiTangents ) << ", found: " << dumpVector3( toCompBiTangents ) << "\n";
  211. addDiff( stream.str() );
  212. tangentsEqual = false;
  213. }
  214. }
  215. if ( !tangentsEqual ) {
  216. return false;
  217. }
  218. return true;
  219. }