Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

420 рядки
13 KiB

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