FindInvalidDataProcess.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. /** @file Defines a post processing step to search an importer's output
  35. for data that is obviously invalid */
  36. #include "AssimpPCH.h"
  37. #ifndef AI_BUILD_NO_FINDINVALIDDATA_PROCESS
  38. // internal headers
  39. #include "FindInvalidDataProcess.h"
  40. #include "ProcessHelper.h"
  41. using namespace Assimp;
  42. // ------------------------------------------------------------------------------------------------
  43. // Constructor to be privately used by Importer
  44. FindInvalidDataProcess::FindInvalidDataProcess()
  45. {
  46. // nothing to do here
  47. }
  48. // ------------------------------------------------------------------------------------------------
  49. // Destructor, private as well
  50. FindInvalidDataProcess::~FindInvalidDataProcess()
  51. {
  52. // nothing to do here
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the processing step is present in the given flag field.
  56. bool FindInvalidDataProcess::IsActive( unsigned int pFlags) const
  57. {
  58. return 0 != (pFlags & aiProcess_FindInvalidData);
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Update mesh references in the node graph
  62. void UpdateMeshReferences(aiNode* node, const std::vector<unsigned int>& meshMapping)
  63. {
  64. if (node->mNumMeshes)
  65. {
  66. unsigned int out = 0;
  67. for (unsigned int a = 0; a < node->mNumMeshes;++a)
  68. {
  69. register unsigned int ref = node->mMeshes[a];
  70. if (0xffffffff != (ref = meshMapping[ref]))
  71. {
  72. node->mMeshes[out++] = ref;
  73. }
  74. }
  75. // just let the members that are unused, that's much cheaper
  76. // than a full array realloc'n'copy party ...
  77. if(!(node->mNumMeshes = out))
  78. {
  79. delete[] node->mMeshes;
  80. node->mMeshes = NULL;
  81. }
  82. }
  83. // recursively update all children
  84. for (unsigned int i = 0; i < node->mNumChildren;++i)
  85. UpdateMeshReferences(node->mChildren[i],meshMapping);
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // Executes the post processing step on the given imported data.
  89. void FindInvalidDataProcess::Execute( aiScene* pScene)
  90. {
  91. DefaultLogger::get()->debug("FindInvalidDataProcess begin");
  92. bool out = false;
  93. std::vector<unsigned int> meshMapping(pScene->mNumMeshes);
  94. unsigned int real = 0, realAnimations = 0;
  95. // Process meshes
  96. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  97. {
  98. int result;
  99. if ((result = ProcessMesh( pScene->mMeshes[a])))
  100. {
  101. out = true;
  102. if (2 == result)
  103. {
  104. // remove this mesh
  105. delete pScene->mMeshes[a];
  106. AI_DEBUG_INVALIDATE_PTR(pScene->mMeshes[a]);
  107. meshMapping[a] = 0xffffffff;
  108. continue;
  109. }
  110. }
  111. pScene->mMeshes[real] = pScene->mMeshes[a];
  112. meshMapping[a] = real++;
  113. }
  114. // Process animations
  115. for (unsigned int a = 0; a < pScene->mNumAnimations;++a)
  116. ProcessAnimation( pScene->mAnimations[a]);
  117. if (out)
  118. {
  119. if ( real != pScene->mNumMeshes)
  120. {
  121. if (!real)
  122. throw new ImportErrorException("No meshes remaining");
  123. // we need to remove some meshes.
  124. // therefore we'll also need to remove all references
  125. // to them from the scenegraph
  126. UpdateMeshReferences(pScene->mRootNode,meshMapping);
  127. pScene->mNumMeshes = real;
  128. }
  129. DefaultLogger::get()->info("FindInvalidDataProcess finished. Found issues ...");
  130. }
  131. else DefaultLogger::get()->debug("FindInvalidDataProcess finished. Everything seems to be OK.");
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. template <typename T>
  135. inline const char* ValidateArrayContents(const T* arr, unsigned int size,
  136. const std::vector<bool>& dirtyMask)
  137. {
  138. return NULL;
  139. }
  140. // ------------------------------------------------------------------------------------------------
  141. template <>
  142. inline const char* ValidateArrayContents<aiVector3D>(const aiVector3D* arr, unsigned int size,
  143. const std::vector<bool>& dirtyMask)
  144. {
  145. bool b = false;
  146. for (unsigned int i = 0; i < size;++i)
  147. {
  148. if (dirtyMask.size() && dirtyMask[i])continue;
  149. const aiVector3D& v = arr[i];
  150. if (is_special_float(v.x) || is_special_float(v.y) || is_special_float(v.z))
  151. {
  152. return "INF/NAN was found in a vector component";
  153. }
  154. if (i && v != arr[i-1])b = true;
  155. }
  156. if (!b)
  157. return "All vectors are identical";
  158. return NULL;
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. template <typename T>
  162. inline bool ProcessArray(T*& in, unsigned int num,const char* name,
  163. const std::vector<bool>& dirtyMask)
  164. {
  165. const char* err = ValidateArrayContents(in,num,dirtyMask);
  166. if (err)
  167. {
  168. DefaultLogger::get()->error(std::string("FindInvalidDataProcess fails on mesh ") + name + ": " + err);
  169. delete[] in;
  170. in = NULL;
  171. return true;
  172. }
  173. return false;
  174. }
  175. // ------------------------------------------------------------------------------------------------
  176. template <typename T>
  177. inline bool AllIdentical(T* in, unsigned int num)
  178. {
  179. if (!num)return true;
  180. for (unsigned int i = 0; i < num-1;++i)
  181. {
  182. if (in[i] != in[i+1])return false;
  183. }
  184. return true;
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. // Search an animation for invalid content
  188. void FindInvalidDataProcess::ProcessAnimation (aiAnimation* anim)
  189. {
  190. // Process all animation channels
  191. for (unsigned int a = 0; a < anim->mNumChannels;++a)
  192. ProcessAnimationChannel( anim->mChannels[a]);
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. void FindInvalidDataProcess::ProcessAnimationChannel (aiNodeAnim* anim)
  196. {
  197. int i = 0;
  198. // ScenePreprocessor's work ...
  199. ai_assert(0 != anim->mPositionKeys && 0 != anim->mRotationKeys && 0 != anim->mScalingKeys);
  200. // Check whether all values in a tracks are identical - in this case
  201. // we can remove al keys except one.
  202. // POSITIONS
  203. if (anim->mNumPositionKeys > 1 && AllIdentical(anim->mPositionKeys,anim->mNumPositionKeys))
  204. {
  205. aiVectorKey v = anim->mPositionKeys[0];
  206. // Reallocate ... we need just ONE element, it makes no sense to reuse the array
  207. delete[] anim->mPositionKeys;
  208. anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys = 1];
  209. anim->mPositionKeys[0] = v;
  210. i = 1;
  211. }
  212. // ROTATIONS
  213. if (anim->mNumRotationKeys > 1 && AllIdentical(anim->mRotationKeys,anim->mNumRotationKeys))
  214. {
  215. aiQuatKey v = anim->mRotationKeys[0];
  216. // Reallocate ... we need just ONE element, it makes no sense to reuse the array
  217. delete[] anim->mRotationKeys;
  218. anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys = 1];
  219. anim->mRotationKeys[0] = v;
  220. i = 1;
  221. }
  222. // SCALINGS
  223. if (anim->mNumScalingKeys > 1 && AllIdentical(anim->mScalingKeys,anim->mNumScalingKeys))
  224. {
  225. aiVectorKey v = anim->mScalingKeys[0];
  226. // Reallocate ... we need just ONE element, it makes no sense to reuse the array
  227. delete[] anim->mScalingKeys;
  228. anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys = 1];
  229. anim->mScalingKeys[0] = v;
  230. i = 1;
  231. }
  232. if (1 == i)
  233. DefaultLogger::get()->warn("Simplified dummy tracks with just one key");
  234. }
  235. // ------------------------------------------------------------------------------------------------
  236. // Search a mesh for invalid contents
  237. int FindInvalidDataProcess::ProcessMesh (aiMesh* pMesh)
  238. {
  239. bool ret = false;
  240. std::vector<bool> dirtyMask;
  241. // process vertex positions
  242. if(pMesh->mVertices && ProcessArray(pMesh->mVertices,pMesh->mNumVertices,"positions",dirtyMask))
  243. {
  244. DefaultLogger::get()->error("Deleting mesh: Unable to continue without vertex positions");
  245. return 2;
  246. }
  247. // process texture coordinates
  248. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  249. {
  250. if (!pMesh->mTextureCoords[i])break;
  251. if (ProcessArray(pMesh->mTextureCoords[i],pMesh->mNumVertices,"uvcoords",dirtyMask))
  252. {
  253. // delete all subsequent texture coordinate sets.
  254. for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a)
  255. {
  256. delete[] pMesh->mTextureCoords[a]; pMesh->mTextureCoords[a] = NULL;
  257. }
  258. ret = true;
  259. }
  260. }
  261. // -- we don't validate vertex colors, it's difficult to say whether
  262. // they are invalid or not.
  263. // normals and tangents are undefined for point and line faces.
  264. // we generate a small lookup table in which we mark all
  265. // indices into the normals/tangents array that MAY be invalid
  266. if (pMesh->mNormals || pMesh->mTangents)
  267. {
  268. if (aiPrimitiveType_POINT & pMesh->mPrimitiveTypes ||
  269. aiPrimitiveType_LINE & pMesh->mPrimitiveTypes)
  270. {
  271. if (aiPrimitiveType_TRIANGLE & pMesh->mPrimitiveTypes ||
  272. aiPrimitiveType_POLYGON & pMesh->mPrimitiveTypes)
  273. {
  274. // we need the lookup table
  275. dirtyMask.resize(pMesh->mNumVertices,false);
  276. for (unsigned int m = 0; m < pMesh->mNumFaces;++m)
  277. {
  278. const aiFace& f = pMesh->mFaces[m];
  279. if (2 == f.mNumIndices)
  280. {
  281. dirtyMask[f.mIndices[0]] = dirtyMask[f.mIndices[1]] = true;
  282. }
  283. else if (1 == f.mNumIndices)dirtyMask[f.mIndices[0]] = true;
  284. }
  285. }
  286. else return ret;
  287. }
  288. // process mesh normals
  289. if (pMesh->mNormals && ProcessArray(pMesh->mNormals,pMesh->mNumVertices,
  290. "normals",dirtyMask))
  291. ret = true;
  292. // process mesh tangents
  293. if (pMesh->mTangents && ProcessArray(pMesh->mTangents,pMesh->mNumVertices,
  294. "tangents",dirtyMask))
  295. {
  296. delete[] pMesh->mBitangents; pMesh->mBitangents = NULL;
  297. ret = true;
  298. }
  299. // process mesh bitangents
  300. if (pMesh->mBitangents && ProcessArray(pMesh->mBitangents,pMesh->mNumVertices,
  301. "bitangents",dirtyMask))
  302. {
  303. delete[] pMesh->mTangents; pMesh->mTangents = NULL;
  304. ret = true;
  305. }
  306. }
  307. return ret ? 1 : 0;
  308. }
  309. #endif // !! AI_BUILD_NO_FINDINVALIDDATA_PROCESS