FindInvalidDataProcess.cpp 15 KB

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