2
0

SceneDiffer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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 "SceneDiffer.h"
  35. #include <assimp/scene.h>
  36. #include <assimp/mesh.h>
  37. #include <assimp/material.h>
  38. #include <sstream>
  39. namespace Assimp {
  40. SceneDiffer::SceneDiffer()
  41. : m_diffs() {
  42. // empty
  43. }
  44. SceneDiffer::~SceneDiffer() = default;
  45. bool SceneDiffer::isEqual( const aiScene *expected, const aiScene *toCompare ) {
  46. if ( expected == toCompare ) {
  47. return true;
  48. }
  49. if ( nullptr == expected ) {
  50. return false;
  51. }
  52. if ( nullptr == toCompare ) {
  53. return false;
  54. }
  55. // meshes
  56. if ( expected->mNumMeshes != toCompare->mNumMeshes ) {
  57. std::stringstream stream;
  58. stream << "Number of meshes not equal ( expected: " << expected->mNumMeshes << ", found : " << toCompare->mNumMeshes << " )\n";
  59. addDiff( stream.str() );
  60. return false;
  61. }
  62. for ( unsigned int i = 0; i < expected->mNumMeshes; i++ ) {
  63. aiMesh *expMesh( expected->mMeshes[ i ] );
  64. aiMesh *toCompMesh( toCompare->mMeshes[ i ] );
  65. if ( !compareMesh( expMesh, toCompMesh ) ) {
  66. std::stringstream stream;
  67. stream << "Meshes are not equal, index : " << i << "\n";
  68. addDiff( stream.str() );
  69. }
  70. }
  71. // materials
  72. /*if ( expected->mNumMaterials != toCompare->mNumMaterials ) {
  73. std::stringstream stream;
  74. stream << "Number of materials not equal ( expected: " << expected->mNumMaterials << ", found : " << toCompare->mNumMaterials << " )\n";
  75. addDiff( stream.str() );
  76. return false;
  77. }
  78. if ( expected->mNumMaterials > 0 ) {
  79. if ( nullptr == expected->mMaterials || nullptr == toCompare->mMaterials ) {
  80. addDiff( "Number of materials > 0 and mat pointer is nullptr" );
  81. return false;
  82. }
  83. }
  84. for ( unsigned int i = 0; i < expected->mNumMaterials; i++ ) {
  85. aiMaterial *expectedMat( expected->mMaterials[ i ] );
  86. aiMaterial *toCompareMat( expected->mMaterials[ i ] );
  87. if ( !compareMaterial( expectedMat, toCompareMat ) ) {
  88. std::stringstream stream;
  89. stream << "Materials are not equal, index : " << i << "\n";
  90. addDiff( stream.str() );
  91. return false;
  92. }
  93. }*/
  94. return true;
  95. }
  96. void SceneDiffer::showReport() {
  97. if ( m_diffs.empty() ) {
  98. return;
  99. }
  100. for ( std::vector<std::string>::iterator it = m_diffs.begin(); it != m_diffs.end(); ++it ) {
  101. std::cout << *it << "\n";
  102. }
  103. std::cout << std::endl;
  104. }
  105. void SceneDiffer::reset() {
  106. m_diffs.resize( 0 );
  107. }
  108. void SceneDiffer::addDiff( const std::string &diff ) {
  109. if ( diff.empty() ) {
  110. return;
  111. }
  112. m_diffs.push_back( diff );
  113. }
  114. static std::string dumpVector3( const aiVector3D &toDump ) {
  115. std::stringstream stream;
  116. stream << "( " << toDump.x << ", " << toDump.y << ", " << toDump.z << ")";
  117. return stream.str();
  118. }
  119. /*static std::string dumpColor4D( const aiColor4D &toDump ) {
  120. std::stringstream stream;
  121. stream << "( " << toDump.r << ", " << toDump.g << ", " << toDump.b << ", " << toDump.a << ")";
  122. return stream.str();
  123. }*/
  124. static std::string dumpFace( const aiFace &face ) {
  125. std::stringstream stream;
  126. for ( unsigned int i = 0; i < face.mNumIndices; i++ ) {
  127. stream << face.mIndices[ i ];
  128. if ( i < face.mNumIndices - 1 ) {
  129. stream << ", ";
  130. }
  131. else {
  132. stream << "\n";
  133. }
  134. }
  135. return stream.str();
  136. }
  137. bool SceneDiffer::compareMesh( aiMesh *expected, aiMesh *toCompare ) {
  138. if ( expected == toCompare ) {
  139. return true;
  140. }
  141. if ( nullptr == expected || nullptr == toCompare ) {
  142. return false;
  143. }
  144. if ( expected->mName != toCompare->mName ) {
  145. std::stringstream stream;
  146. stream << "Mesh name not equal ( expected: " << expected->mName.C_Str() << ", found : " << toCompare->mName.C_Str() << " )\n";
  147. addDiff( stream.str() );
  148. }
  149. if ( expected->mNumVertices != toCompare->mNumVertices ) {
  150. std::stringstream stream;
  151. stream << "Number of vertices not equal ( expected: " << expected->mNumVertices << ", found : " << toCompare->mNumVertices << " )\n";
  152. addDiff( stream.str() );
  153. return false;
  154. }
  155. // positions
  156. if ( expected->HasPositions() != toCompare->HasPositions() ) {
  157. addDiff( "Expected are vertices, toCompare does not have any." );
  158. return false;
  159. }
  160. bool vertEqual( true );
  161. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  162. aiVector3D &expVert( expected->mVertices[ i ] );
  163. aiVector3D &toCompVert( toCompare->mVertices[ i ] );
  164. if ( !expVert.Equal( toCompVert ) ) {
  165. std::cout << "index = " << i << dumpVector3( toCompVert ) << "\n";
  166. std::stringstream stream;
  167. stream << "Vertex not equal ( expected: " << dumpVector3( toCompVert ) << ", found: " << dumpVector3( toCompVert ) << "\n";
  168. addDiff( stream.str() );
  169. vertEqual = false;
  170. }
  171. }
  172. if ( !vertEqual ) {
  173. return false;
  174. }
  175. // normals
  176. if ( expected->HasNormals() != toCompare->HasNormals() ) {
  177. addDiff( "Expected are normals, toCompare does not have any." );
  178. return false;
  179. }
  180. // return true;
  181. //ToDo!
  182. /*bool normalEqual( true );
  183. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  184. aiVector3D &expNormal( expected->mNormals[ i ] );
  185. aiVector3D &toCompNormal( toCompare->mNormals[ i ] );
  186. if ( expNormal.Equal( toCompNormal ) ) {
  187. std::stringstream stream;
  188. stream << "Normal not equal ( expected: " << dumpVector3( expNormal ) << ", found: " << dumpVector3( toCompNormal ) << "\n";
  189. addDiff( stream.str() );
  190. normalEqual = false;
  191. }
  192. }
  193. if ( !normalEqual ) {
  194. return false;
  195. }
  196. // vertex colors
  197. bool vertColEqual( true );
  198. for ( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++ ) {
  199. if ( expected->HasVertexColors(a) != toCompare->HasVertexColors(a) ) {
  200. addDiff( "Expected are normals, toCompare does not have any." );
  201. return false;
  202. }
  203. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  204. aiColor4D &expColor4D( expected->mColors[ a ][ i ] );
  205. aiColor4D &toCompColor4D( toCompare->mColors[ a ][ i ] );
  206. if ( expColor4D != toCompColor4D ) {
  207. std::stringstream stream;
  208. stream << "Color4D not equal ( expected: " << dumpColor4D( expColor4D ) << ", found: " << dumpColor4D( toCompColor4D ) << "\n";
  209. addDiff( stream.str() );
  210. vertColEqual = false;
  211. }
  212. }
  213. if ( !vertColEqual ) {
  214. return false;
  215. }
  216. }
  217. // texture coords
  218. bool texCoordsEqual( true );
  219. for ( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++ ) {
  220. if ( expected->HasTextureCoords( a ) != toCompare->HasTextureCoords( a ) ) {
  221. addDiff( "Expected are texture coords, toCompare does not have any." );
  222. return false;
  223. }
  224. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  225. aiVector3D &expTexCoord( expected->mTextureCoords[ a ][ i ] );
  226. aiVector3D &toCompTexCoord( toCompare->mTextureCoords[ a ][ i ] );
  227. if ( expTexCoord.Equal( toCompTexCoord ) ) {
  228. std::stringstream stream;
  229. stream << "Texture coords not equal ( expected: " << dumpVector3( expTexCoord ) << ", found: " << dumpVector3( toCompTexCoord ) << "\n";
  230. addDiff( stream.str() );
  231. vertColEqual = false;
  232. }
  233. }
  234. if ( !vertColEqual ) {
  235. return false;
  236. }
  237. }
  238. // tangents and bi-tangents
  239. if ( expected->HasTangentsAndBitangents() != toCompare->HasTangentsAndBitangents() ) {
  240. addDiff( "Expected are tangents and bi-tangents, toCompare does not have any." );
  241. return false;
  242. }
  243. bool tangentsEqual( true );
  244. for ( unsigned int i = 0; i < expected->mNumVertices; i++ ) {
  245. aiVector3D &expTangents( expected->mTangents[ i ] );
  246. aiVector3D &toCompTangents( toCompare->mTangents[ i ] );
  247. if ( expTangents.Equal( toCompTangents ) ) {
  248. std::stringstream stream;
  249. stream << "Tangents not equal ( expected: " << dumpVector3( expTangents ) << ", found: " << dumpVector3( toCompTangents ) << "\n";
  250. addDiff( stream.str() );
  251. tangentsEqual = false;
  252. }
  253. aiVector3D &expBiTangents( expected->mBitangents[ i ] );
  254. aiVector3D &toCompBiTangents( toCompare->mBitangents[ i ] );
  255. if ( expBiTangents.Equal( toCompBiTangents ) ) {
  256. std::stringstream stream;
  257. stream << "Tangents not equal ( expected: " << dumpVector3( expBiTangents ) << ", found: " << dumpVector3( toCompBiTangents ) << " )\n";
  258. addDiff( stream.str() );
  259. tangentsEqual = false;
  260. }
  261. }
  262. if ( !tangentsEqual ) {
  263. return false;
  264. }*/
  265. // faces
  266. if ( expected->mNumFaces != toCompare->mNumFaces ) {
  267. std::stringstream stream;
  268. stream << "Number of faces are not equal, ( expected: " << expected->mNumFaces << ", found: " << toCompare->mNumFaces << ")\n";
  269. addDiff( stream.str() );
  270. return false;
  271. }
  272. bool facesEqual( true );
  273. for ( unsigned int i = 0; i < expected->mNumFaces; i++ ) {
  274. aiFace &expFace( expected->mFaces[ i ] );
  275. aiFace &toCompareFace( toCompare->mFaces[ i ] );
  276. if ( !compareFace( &expFace, &toCompareFace ) ) {
  277. addDiff( "Faces are not equal\n" );
  278. addDiff( dumpFace( expFace ) );
  279. addDiff( dumpFace( toCompareFace ) );
  280. facesEqual = false;
  281. }
  282. }
  283. if ( !facesEqual ) {
  284. return false;
  285. }
  286. return true;
  287. }
  288. bool SceneDiffer::compareFace( aiFace *expected, aiFace *toCompare ) {
  289. if ( nullptr == expected ) {
  290. return false;
  291. }
  292. if ( nullptr == toCompare ) {
  293. return false;
  294. }
  295. // same instance
  296. if ( expected == toCompare ) {
  297. return true;
  298. }
  299. // using compare operator
  300. if ( *expected == *toCompare ) {
  301. return true;
  302. }
  303. return false;
  304. }
  305. bool SceneDiffer::compareMaterial( aiMaterial *expected, aiMaterial *toCompare ) {
  306. if ( nullptr == expected ) {
  307. return false;
  308. }
  309. if ( nullptr == toCompare ) {
  310. return false;
  311. }
  312. // same instance
  313. if ( expected == toCompare ) {
  314. return true;
  315. }
  316. // todo!
  317. return true;
  318. }
  319. }