FindInvalidDataProcess.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. {
  117. int result;
  118. if ((result = ProcessAnimation( pScene->mAnimations[a])))
  119. {
  120. out = true;
  121. if (2 == result)
  122. {
  123. // remove this animation
  124. delete pScene->mAnimations[a];
  125. AI_DEBUG_INVALIDATE_PTR(pScene->mAnimations[a]);
  126. continue;
  127. }
  128. }
  129. pScene->mAnimations[realAnimations++] = pScene->mAnimations[a];
  130. }
  131. if (out)
  132. {
  133. if(!(pScene->mNumAnimations = realAnimations))
  134. {
  135. delete[] pScene->mAnimations;
  136. pScene->mAnimations = NULL;
  137. }
  138. if ( real != pScene->mNumMeshes)
  139. {
  140. if (!real)
  141. throw new ImportErrorException("No meshes remaining");
  142. // we need to remove some meshes.
  143. // therefore we'll also need to remove all references
  144. // to them from the scenegraph
  145. UpdateMeshReferences(pScene->mRootNode,meshMapping);
  146. pScene->mNumMeshes = real;
  147. }
  148. DefaultLogger::get()->info("FindInvalidDataProcess finished. Found issues ...");
  149. }
  150. else DefaultLogger::get()->debug("FindInvalidDataProcess finished. Everything seems to be OK.");
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. template <typename T>
  154. inline const char* ValidateArrayContents(const T* arr, unsigned int size,
  155. const std::vector<bool>& dirtyMask)
  156. {
  157. return NULL;
  158. }
  159. // ------------------------------------------------------------------------------------------------
  160. template <>
  161. inline const char* ValidateArrayContents<aiVector3D>(const aiVector3D* arr, unsigned int size,
  162. const std::vector<bool>& dirtyMask)
  163. {
  164. bool b = false;
  165. for (unsigned int i = 0; i < size;++i)
  166. {
  167. if (dirtyMask.size() && dirtyMask[i])continue;
  168. const aiVector3D& v = arr[i];
  169. if (is_special_float(v.x) || is_special_float(v.y) || is_special_float(v.z))
  170. {
  171. return "INF/NAN was found in a vector component";
  172. }
  173. if (i && v != arr[i-1])b = true;
  174. }
  175. if (!b)
  176. return "All vectors are identical";
  177. return NULL;
  178. }
  179. // ------------------------------------------------------------------------------------------------
  180. template <typename T>
  181. inline bool ProcessArray(T*& in, unsigned int num,const char* name,
  182. const std::vector<bool>& dirtyMask)
  183. {
  184. const char* err = ValidateArrayContents(in,num,dirtyMask);
  185. if (err)
  186. {
  187. DefaultLogger::get()->error(std::string("FindInvalidDataProcess fails on mesh ") + name + ": " + err);
  188. delete[] in;
  189. in = NULL;
  190. return true;
  191. }
  192. return false;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. template <typename T>
  196. inline bool AllIdentical(T* in, unsigned int num)
  197. {
  198. if (!num)return true;
  199. for (unsigned int i = 0; i < num-1;++i)
  200. {
  201. if (in[i] != in[i+1])return false;
  202. }
  203. return true;
  204. }
  205. // ------------------------------------------------------------------------------------------------
  206. // Search an animation for invalid content
  207. int FindInvalidDataProcess::ProcessAnimation (aiAnimation* anim)
  208. {
  209. bool out = false;
  210. unsigned int real = 0;
  211. // Process all animation channels
  212. for (unsigned int a = 0; a < anim->mNumChannels;++a)
  213. {
  214. int result;
  215. if ((result = ProcessAnimationChannel( anim->mChannels[a])))
  216. {
  217. out = true;
  218. // remove this animation channel
  219. delete anim->mChannels[a];
  220. AI_DEBUG_INVALIDATE_PTR(anim->mChannels[a]);
  221. continue;
  222. }
  223. anim->mChannels[real++] = anim->mChannels[a];
  224. }
  225. if (out)
  226. {
  227. anim->mNumChannels = real;
  228. if (!real)
  229. {
  230. DefaultLogger::get()->error("Deleting anim: it consists of dummy tracks");
  231. return 2;
  232. }
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. int FindInvalidDataProcess::ProcessAnimationChannel (aiNodeAnim* anim)
  239. {
  240. // TODO: (thom) For some reason, even proper channels are deleted as well. Therefore deactivated it for the moment.
  241. //return 0;
  242. int i = 0;
  243. // Check whether all values are identical or whether there is just one keyframe
  244. if ((anim->mNumPositionKeys < 1 || AllIdentical(anim->mPositionKeys,anim->mNumPositionKeys)) &&
  245. (anim->mNumScalingKeys < 1 || AllIdentical(anim->mRotationKeys,anim->mNumRotationKeys)) &&
  246. (anim->mNumRotationKeys < 1 || AllIdentical(anim->mScalingKeys,anim->mNumScalingKeys)))
  247. {
  248. DefaultLogger::get()->error("Deleting dummy position animation channel");
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. // ------------------------------------------------------------------------------------------------
  254. // Search a mesh for invalid contents
  255. int FindInvalidDataProcess::ProcessMesh (aiMesh* pMesh)
  256. {
  257. bool ret = false;
  258. std::vector<bool> dirtyMask;
  259. // process vertex positions
  260. if(pMesh->mVertices && ProcessArray(pMesh->mVertices,pMesh->mNumVertices,"positions",dirtyMask))
  261. {
  262. DefaultLogger::get()->error("Deleting mesh: Unable to continue without vertex positions");
  263. return 2;
  264. }
  265. // process texture coordinates
  266. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  267. {
  268. if (!pMesh->mTextureCoords[i])break;
  269. if (ProcessArray(pMesh->mTextureCoords[i],pMesh->mNumVertices,"uvcoords",dirtyMask))
  270. {
  271. // delete all subsequent texture coordinate sets.
  272. for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a)
  273. {
  274. delete[] pMesh->mTextureCoords[a]; pMesh->mTextureCoords[a] = NULL;
  275. }
  276. ret = true;
  277. }
  278. }
  279. // -- we don't validate vertex colors, it's difficult to say whether
  280. // they are invalid or not.
  281. // normals and tangents are undefined for point and line faces.
  282. // we generate a small lookup table in which we mark all
  283. // indices into the normals/tangents array that MAY be invalid
  284. if (pMesh->mNormals || pMesh->mTangents)
  285. {
  286. if (aiPrimitiveType_POINT & pMesh->mPrimitiveTypes ||
  287. aiPrimitiveType_LINE & pMesh->mPrimitiveTypes)
  288. {
  289. if (aiPrimitiveType_TRIANGLE & pMesh->mPrimitiveTypes ||
  290. aiPrimitiveType_POLYGON & pMesh->mPrimitiveTypes)
  291. {
  292. // we need the lookup table
  293. dirtyMask.resize(pMesh->mNumVertices,false);
  294. for (unsigned int m = 0; m < pMesh->mNumFaces;++m)
  295. {
  296. const aiFace& f = pMesh->mFaces[m];
  297. if (2 == f.mNumIndices)
  298. {
  299. dirtyMask[f.mIndices[0]] = dirtyMask[f.mIndices[1]] = true;
  300. }
  301. else if (1 == f.mNumIndices)dirtyMask[f.mIndices[0]] = true;
  302. }
  303. }
  304. else return ret;
  305. }
  306. // process mesh normals
  307. if (pMesh->mNormals && ProcessArray(pMesh->mNormals,pMesh->mNumVertices,
  308. "normals",dirtyMask))
  309. ret = true;
  310. // process mesh tangents
  311. if (pMesh->mTangents && ProcessArray(pMesh->mTangents,pMesh->mNumVertices,
  312. "tangents",dirtyMask))
  313. {
  314. delete[] pMesh->mBitangents; pMesh->mBitangents = NULL;
  315. ret = true;
  316. }
  317. // process mesh bitangents
  318. if (pMesh->mBitangents && ProcessArray(pMesh->mBitangents,pMesh->mNumVertices,
  319. "bitangents",dirtyMask))
  320. {
  321. delete[] pMesh->mTangents; pMesh->mTangents = NULL;
  322. ret = true;
  323. }
  324. }
  325. return ret ? 1 : 0;
  326. }
  327. #endif // !! AI_BUILD_NO_FINDINVALIDDATA_PROCESS