2
0

FindInvalidDataProcess.cpp 15 KB

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