2
0

ValidateDataStructure.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, 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 ValidateDataStructure.cpp
  35. * @brief Implementation of the post processing step to validate
  36. * the data structure returned by Assimp.
  37. */
  38. // internal headers
  39. #include "ValidateDataStructure.h"
  40. #include "ProcessHelper.h"
  41. #include <assimp/BaseImporter.h>
  42. #include <assimp/fast_atof.h>
  43. #include <memory>
  44. // CRT headers
  45. #include <stdarg.h>
  46. using namespace Assimp;
  47. // ------------------------------------------------------------------------------------------------
  48. // Constructor to be privately used by Importer
  49. ValidateDSProcess::ValidateDSProcess() :
  50. mScene() {}
  51. // ------------------------------------------------------------------------------------------------
  52. // Destructor, private as well
  53. ValidateDSProcess::~ValidateDSProcess() {}
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the processing step is present in the given flag field.
  56. bool ValidateDSProcess::IsActive(unsigned int pFlags) const {
  57. return (pFlags & aiProcess_ValidateDataStructure) != 0;
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. AI_WONT_RETURN void ValidateDSProcess::ReportError(const char *msg, ...) {
  61. ai_assert(nullptr != msg);
  62. va_list args;
  63. va_start(args, msg);
  64. char szBuffer[3000];
  65. const int iLen = vsprintf(szBuffer, msg, args);
  66. ai_assert(iLen > 0);
  67. va_end(args);
  68. throw DeadlyImportError("Validation failed: ", std::string(szBuffer, iLen));
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. void ValidateDSProcess::ReportWarning(const char *msg, ...) {
  72. ai_assert(nullptr != msg);
  73. va_list args;
  74. va_start(args, msg);
  75. char szBuffer[3000];
  76. const int iLen = vsprintf(szBuffer, msg, args);
  77. ai_assert(iLen > 0);
  78. va_end(args);
  79. ASSIMP_LOG_WARN("Validation warning: ", std::string(szBuffer, iLen));
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. inline int HasNameMatch(const aiString &in, aiNode *node) {
  83. int result = (node->mName == in ? 1 : 0);
  84. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  85. result += HasNameMatch(in, node->mChildren[i]);
  86. }
  87. return result;
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. template <typename T>
  91. inline void ValidateDSProcess::DoValidation(T **parray, unsigned int size, const char *firstName, const char *secondName) {
  92. // validate all entries
  93. if (size) {
  94. if (!parray) {
  95. ReportError("aiScene::%s is nullptr (aiScene::%s is %i)",
  96. firstName, secondName, size);
  97. }
  98. for (unsigned int i = 0; i < size; ++i) {
  99. if (!parray[i]) {
  100. ReportError("aiScene::%s[%i] is nullptr (aiScene::%s is %i)",
  101. firstName, i, secondName, size);
  102. }
  103. Validate(parray[i]);
  104. }
  105. }
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. template <typename T>
  109. inline void ValidateDSProcess::DoValidationEx(T **parray, unsigned int size,
  110. const char *firstName, const char *secondName) {
  111. // validate all entries
  112. if (size) {
  113. if (!parray) {
  114. ReportError("aiScene::%s is nullptr (aiScene::%s is %i)",
  115. firstName, secondName, size);
  116. }
  117. for (unsigned int i = 0; i < size; ++i) {
  118. if (!parray[i]) {
  119. ReportError("aiScene::%s[%u] is nullptr (aiScene::%s is %u)",
  120. firstName, i, secondName, size);
  121. }
  122. Validate(parray[i]);
  123. // check whether there are duplicate names
  124. for (unsigned int a = i + 1; a < size; ++a) {
  125. if (parray[i]->mName == parray[a]->mName) {
  126. ReportError("aiScene::%s[%u] has the same name as "
  127. "aiScene::%s[%u]",
  128. firstName, i, secondName, a);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. // ------------------------------------------------------------------------------------------------
  135. template <typename T>
  136. inline void ValidateDSProcess::DoValidationWithNameCheck(T **array, unsigned int size, const char *firstName,
  137. const char *secondName) {
  138. // validate all entries
  139. DoValidationEx(array, size, firstName, secondName);
  140. for (unsigned int i = 0; i < size; ++i) {
  141. int res = HasNameMatch(array[i]->mName, mScene->mRootNode);
  142. if (0 == res) {
  143. const std::string name = static_cast<char *>(array[i]->mName.data);
  144. ReportError("aiScene::%s[%i] has no corresponding node in the scene graph (%s)",
  145. firstName, i, name.c_str());
  146. } else if (1 != res) {
  147. const std::string name = static_cast<char *>(array[i]->mName.data);
  148. ReportError("aiScene::%s[%i]: there are more than one nodes with %s as name",
  149. firstName, i, name.c_str());
  150. }
  151. }
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. // Executes the post processing step on the given imported data.
  155. void ValidateDSProcess::Execute(aiScene *pScene) {
  156. mScene = pScene;
  157. ASSIMP_LOG_DEBUG("ValidateDataStructureProcess begin");
  158. // validate the node graph of the scene
  159. Validate(pScene->mRootNode);
  160. // validate all meshes
  161. if (pScene->mNumMeshes) {
  162. DoValidation(pScene->mMeshes, pScene->mNumMeshes, "mMeshes", "mNumMeshes");
  163. } else if (!(mScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)) {
  164. ReportError("aiScene::mNumMeshes is 0. At least one mesh must be there");
  165. } else if (pScene->mMeshes) {
  166. ReportError("aiScene::mMeshes is non-null although there are no meshes");
  167. }
  168. // validate all animations
  169. if (pScene->mNumAnimations) {
  170. DoValidation(pScene->mAnimations, pScene->mNumAnimations,
  171. "mAnimations", "mNumAnimations");
  172. } else if (pScene->mAnimations) {
  173. ReportError("aiScene::mAnimations is non-null although there are no animations");
  174. }
  175. // validate all cameras
  176. if (pScene->mNumCameras) {
  177. DoValidationWithNameCheck(pScene->mCameras, pScene->mNumCameras,
  178. "mCameras", "mNumCameras");
  179. } else if (pScene->mCameras) {
  180. ReportError("aiScene::mCameras is non-null although there are no cameras");
  181. }
  182. // validate all lights
  183. if (pScene->mNumLights) {
  184. DoValidationWithNameCheck(pScene->mLights, pScene->mNumLights,
  185. "mLights", "mNumLights");
  186. } else if (pScene->mLights) {
  187. ReportError("aiScene::mLights is non-null although there are no lights");
  188. }
  189. // validate all textures
  190. if (pScene->mNumTextures) {
  191. DoValidation(pScene->mTextures, pScene->mNumTextures,
  192. "mTextures", "mNumTextures");
  193. } else if (pScene->mTextures) {
  194. ReportError("aiScene::mTextures is non-null although there are no textures");
  195. }
  196. // validate all materials
  197. if (pScene->mNumMaterials) {
  198. DoValidation(pScene->mMaterials, pScene->mNumMaterials, "mMaterials", "mNumMaterials");
  199. }
  200. #if 0
  201. // NOTE: ScenePreprocessor generates a default material if none is there
  202. else if (!(mScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)) {
  203. ReportError("aiScene::mNumMaterials is 0. At least one material must be there");
  204. }
  205. #endif
  206. else if (pScene->mMaterials) {
  207. ReportError("aiScene::mMaterials is non-null although there are no materials");
  208. }
  209. // if (!has)ReportError("The aiScene data structure is empty");
  210. ASSIMP_LOG_DEBUG("ValidateDataStructureProcess end");
  211. }
  212. // ------------------------------------------------------------------------------------------------
  213. void ValidateDSProcess::Validate(const aiLight *pLight) {
  214. if (pLight->mType == aiLightSource_UNDEFINED)
  215. ReportWarning("aiLight::mType is aiLightSource_UNDEFINED");
  216. if (!pLight->mAttenuationConstant &&
  217. !pLight->mAttenuationLinear &&
  218. !pLight->mAttenuationQuadratic) {
  219. ReportWarning("aiLight::mAttenuationXXX - all are zero");
  220. }
  221. if (pLight->mAngleInnerCone > pLight->mAngleOuterCone)
  222. ReportError("aiLight::mAngleInnerCone is larger than aiLight::mAngleOuterCone");
  223. if (pLight->mColorDiffuse.IsBlack() && pLight->mColorAmbient.IsBlack() && pLight->mColorSpecular.IsBlack()) {
  224. ReportWarning("aiLight::mColorXXX - all are black and won't have any influence");
  225. }
  226. }
  227. // ------------------------------------------------------------------------------------------------
  228. void ValidateDSProcess::Validate(const aiCamera *pCamera) {
  229. if (pCamera->mClipPlaneFar <= pCamera->mClipPlaneNear)
  230. ReportError("aiCamera::mClipPlaneFar must be >= aiCamera::mClipPlaneNear");
  231. // FIX: there are many 3ds files with invalid FOVs. No reason to
  232. // reject them at all ... a warning is appropriate.
  233. if (!pCamera->mHorizontalFOV || pCamera->mHorizontalFOV >= (float)AI_MATH_PI)
  234. ReportWarning("%f is not a valid value for aiCamera::mHorizontalFOV", pCamera->mHorizontalFOV);
  235. }
  236. // ------------------------------------------------------------------------------------------------
  237. void ValidateDSProcess::Validate(const aiMesh *pMesh) {
  238. // validate the material index of the mesh
  239. if (mScene->mNumMaterials && pMesh->mMaterialIndex >= mScene->mNumMaterials) {
  240. ReportError("aiMesh::mMaterialIndex is invalid (value: %i maximum: %i)",
  241. pMesh->mMaterialIndex, mScene->mNumMaterials - 1);
  242. }
  243. Validate(&pMesh->mName);
  244. for (unsigned int i = 0; i < pMesh->mNumFaces; ++i) {
  245. aiFace &face = pMesh->mFaces[i];
  246. if (pMesh->mPrimitiveTypes) {
  247. switch (face.mNumIndices) {
  248. case 0:
  249. ReportError("aiMesh::mFaces[%i].mNumIndices is 0", i);
  250. break;
  251. case 1:
  252. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_POINT)) {
  253. ReportError("aiMesh::mFaces[%i] is a POINT but aiMesh::mPrimitiveTypes "
  254. "does not report the POINT flag",
  255. i);
  256. }
  257. break;
  258. case 2:
  259. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_LINE)) {
  260. ReportError("aiMesh::mFaces[%i] is a LINE but aiMesh::mPrimitiveTypes "
  261. "does not report the LINE flag",
  262. i);
  263. }
  264. break;
  265. case 3:
  266. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE)) {
  267. ReportError("aiMesh::mFaces[%i] is a TRIANGLE but aiMesh::mPrimitiveTypes "
  268. "does not report the TRIANGLE flag",
  269. i);
  270. }
  271. break;
  272. default:
  273. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON)) {
  274. this->ReportError("aiMesh::mFaces[%i] is a POLYGON but aiMesh::mPrimitiveTypes "
  275. "does not report the POLYGON flag",
  276. i);
  277. }
  278. break;
  279. };
  280. }
  281. if (!face.mIndices)
  282. ReportError("aiMesh::mFaces[%i].mIndices is nullptr", i);
  283. }
  284. // positions must always be there ...
  285. if (!pMesh->mNumVertices || (!pMesh->mVertices && !mScene->mFlags)) {
  286. ReportError("The mesh %s contains no vertices", pMesh->mName.C_Str());
  287. }
  288. if (pMesh->mNumVertices > AI_MAX_VERTICES) {
  289. ReportError("Mesh has too many vertices: %u, but the limit is %u", pMesh->mNumVertices, AI_MAX_VERTICES);
  290. }
  291. if (pMesh->mNumFaces > AI_MAX_FACES) {
  292. ReportError("Mesh has too many faces: %u, but the limit is %u", pMesh->mNumFaces, AI_MAX_FACES);
  293. }
  294. // if tangents are there there must also be bitangent vectors ...
  295. if ((pMesh->mTangents != nullptr) != (pMesh->mBitangents != nullptr)) {
  296. ReportError("If there are tangents, bitangent vectors must be present as well");
  297. }
  298. // faces, too
  299. if (!pMesh->mNumFaces || (!pMesh->mFaces && !mScene->mFlags)) {
  300. ReportError("Mesh %s contains no faces", pMesh->mName.C_Str());
  301. }
  302. // now check whether the face indexing layout is correct:
  303. // unique vertices, pseudo-indexed.
  304. std::vector<bool> abRefList;
  305. abRefList.resize(pMesh->mNumVertices, false);
  306. for (unsigned int i = 0; i < pMesh->mNumFaces; ++i) {
  307. aiFace &face = pMesh->mFaces[i];
  308. if (face.mNumIndices > AI_MAX_FACE_INDICES) {
  309. ReportError("Face %u has too many faces: %u, but the limit is %u", i, face.mNumIndices, AI_MAX_FACE_INDICES);
  310. }
  311. for (unsigned int a = 0; a < face.mNumIndices; ++a) {
  312. if (face.mIndices[a] >= pMesh->mNumVertices) {
  313. ReportError("aiMesh::mFaces[%i]::mIndices[%i] is out of range", i, a);
  314. }
  315. // the MSB flag is temporarily used by the extra verbose
  316. // mode to tell us that the JoinVerticesProcess might have
  317. // been executed already.
  318. /*if ( !(this->mScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT ) && !(this->mScene->mFlags & AI_SCENE_FLAGS_ALLOW_SHARED) &&
  319. abRefList[face.mIndices[a]])
  320. {
  321. ReportError("aiMesh::mVertices[%i] is referenced twice - second "
  322. "time by aiMesh::mFaces[%i]::mIndices[%i]",face.mIndices[a],i,a);
  323. }*/
  324. abRefList[face.mIndices[a]] = true;
  325. }
  326. }
  327. // check whether there are vertices that aren't referenced by a face
  328. bool b = false;
  329. for (unsigned int i = 0; i < pMesh->mNumVertices; ++i) {
  330. if (!abRefList[i]) b = true;
  331. }
  332. abRefList.clear();
  333. if (b) {
  334. ReportWarning("There are unreferenced vertices");
  335. }
  336. // texture channel 2 may not be set if channel 1 is zero ...
  337. {
  338. unsigned int i = 0;
  339. for (; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  340. if (!pMesh->HasTextureCoords(i)) break;
  341. }
  342. for (; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i)
  343. if (pMesh->HasTextureCoords(i)) {
  344. ReportError("Texture coordinate channel %i exists "
  345. "although the previous channel was nullptr.",
  346. i);
  347. }
  348. }
  349. // the same for the vertex colors
  350. {
  351. unsigned int i = 0;
  352. for (; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
  353. if (!pMesh->HasVertexColors(i)) break;
  354. }
  355. for (; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i)
  356. if (pMesh->HasVertexColors(i)) {
  357. ReportError("Vertex color channel %i is exists "
  358. "although the previous channel was nullptr.",
  359. i);
  360. }
  361. }
  362. // now validate all bones
  363. if (pMesh->mNumBones) {
  364. if (!pMesh->mBones) {
  365. ReportError("aiMesh::mBones is nullptr (aiMesh::mNumBones is %i)",
  366. pMesh->mNumBones);
  367. }
  368. std::unique_ptr<float[]> afSum(nullptr);
  369. if (pMesh->mNumVertices) {
  370. afSum.reset(new float[pMesh->mNumVertices]);
  371. for (unsigned int i = 0; i < pMesh->mNumVertices; ++i)
  372. afSum[i] = 0.0f;
  373. }
  374. // check whether there are duplicate bone names
  375. for (unsigned int i = 0; i < pMesh->mNumBones; ++i) {
  376. const aiBone *bone = pMesh->mBones[i];
  377. if (bone->mNumWeights > AI_MAX_BONE_WEIGHTS) {
  378. ReportError("Bone %u has too many weights: %u, but the limit is %u", i, bone->mNumWeights, AI_MAX_BONE_WEIGHTS);
  379. }
  380. if (!pMesh->mBones[i]) {
  381. ReportError("aiMesh::mBones[%i] is nullptr (aiMesh::mNumBones is %i)",
  382. i, pMesh->mNumBones);
  383. }
  384. Validate(pMesh, pMesh->mBones[i], afSum.get());
  385. for (unsigned int a = i + 1; a < pMesh->mNumBones; ++a) {
  386. if (pMesh->mBones[i]->mName == pMesh->mBones[a]->mName) {
  387. const char *name = "unknown";
  388. if (nullptr != pMesh->mBones[i]->mName.C_Str()) {
  389. name = pMesh->mBones[i]->mName.C_Str();
  390. }
  391. ReportError("aiMesh::mBones[%i], name = \"%s\" has the same name as "
  392. "aiMesh::mBones[%i]",
  393. i, name, a);
  394. }
  395. }
  396. }
  397. // check whether all bone weights for a vertex sum to 1.0 ...
  398. for (unsigned int i = 0; i < pMesh->mNumVertices; ++i) {
  399. if (afSum[i] && (afSum[i] <= 0.94 || afSum[i] >= 1.05)) {
  400. ReportWarning("aiMesh::mVertices[%i]: bone weight sum != 1.0 (sum is %f)", i, afSum[i]);
  401. }
  402. }
  403. } else if (pMesh->mBones) {
  404. ReportError("aiMesh::mBones is non-null although there are no bones");
  405. }
  406. }
  407. // ------------------------------------------------------------------------------------------------
  408. void ValidateDSProcess::Validate(const aiMesh *pMesh, const aiBone *pBone, float *afSum) {
  409. this->Validate(&pBone->mName);
  410. if (!pBone->mNumWeights) {
  411. //ReportError("aiBone::mNumWeights is zero");
  412. }
  413. // check whether all vertices affected by this bone are valid
  414. for (unsigned int i = 0; i < pBone->mNumWeights; ++i) {
  415. if (pBone->mWeights[i].mVertexId >= pMesh->mNumVertices) {
  416. ReportError("aiBone::mWeights[%i].mVertexId is out of range", i);
  417. } else if (!pBone->mWeights[i].mWeight || pBone->mWeights[i].mWeight > 1.0f) {
  418. ReportWarning("aiBone::mWeights[%i].mWeight has an invalid value", i);
  419. }
  420. afSum[pBone->mWeights[i].mVertexId] += pBone->mWeights[i].mWeight;
  421. }
  422. }
  423. // ------------------------------------------------------------------------------------------------
  424. void ValidateDSProcess::Validate(const aiAnimation *pAnimation) {
  425. Validate(&pAnimation->mName);
  426. // validate all animations
  427. if (pAnimation->mNumChannels || pAnimation->mNumMorphMeshChannels) {
  428. if (!pAnimation->mChannels && pAnimation->mNumChannels) {
  429. ReportError("aiAnimation::mChannels is nullptr (aiAnimation::mNumChannels is %i)",
  430. pAnimation->mNumChannels);
  431. }
  432. if (!pAnimation->mMorphMeshChannels && pAnimation->mNumMorphMeshChannels) {
  433. ReportError("aiAnimation::mMorphMeshChannels is nullptr (aiAnimation::mNumMorphMeshChannels is %i)",
  434. pAnimation->mNumMorphMeshChannels);
  435. }
  436. for (unsigned int i = 0; i < pAnimation->mNumChannels; ++i) {
  437. if (!pAnimation->mChannels[i]) {
  438. ReportError("aiAnimation::mChannels[%i] is nullptr (aiAnimation::mNumChannels is %i)",
  439. i, pAnimation->mNumChannels);
  440. }
  441. Validate(pAnimation, pAnimation->mChannels[i]);
  442. }
  443. for (unsigned int i = 0; i < pAnimation->mNumMorphMeshChannels; ++i) {
  444. if (!pAnimation->mMorphMeshChannels[i]) {
  445. ReportError("aiAnimation::mMorphMeshChannels[%i] is nullptr (aiAnimation::mNumMorphMeshChannels is %i)",
  446. i, pAnimation->mNumMorphMeshChannels);
  447. }
  448. Validate(pAnimation, pAnimation->mMorphMeshChannels[i]);
  449. }
  450. } else {
  451. ReportError("aiAnimation::mNumChannels is 0. At least one node animation channel must be there.");
  452. }
  453. }
  454. // ------------------------------------------------------------------------------------------------
  455. void ValidateDSProcess::SearchForInvalidTextures(const aiMaterial *pMaterial,
  456. aiTextureType type) {
  457. const char *szType = TextureTypeToString(type);
  458. // ****************************************************************************
  459. // Search all keys of the material ...
  460. // textures must be specified with ascending indices
  461. // (e.g. diffuse #2 may not be specified if diffuse #1 is not there ...)
  462. // ****************************************************************************
  463. int iNumIndices = 0;
  464. int iIndex = -1;
  465. for (unsigned int i = 0; i < pMaterial->mNumProperties; ++i) {
  466. aiMaterialProperty *prop = pMaterial->mProperties[i];
  467. ai_assert(nullptr != prop);
  468. if (!::strcmp(prop->mKey.data, "$tex.file") && prop->mSemantic == static_cast<unsigned int>(type)) {
  469. iIndex = std::max(iIndex, (int)prop->mIndex);
  470. ++iNumIndices;
  471. if (aiPTI_String != prop->mType) {
  472. ReportError("Material property %s is expected to be a string", prop->mKey.data);
  473. }
  474. }
  475. }
  476. if (iIndex + 1 != iNumIndices) {
  477. ReportError("%s #%i is set, but there are only %i %s textures",
  478. szType, iIndex, iNumIndices, szType);
  479. }
  480. if (!iNumIndices) {
  481. return;
  482. }
  483. std::vector<aiTextureMapping> mappings(iNumIndices);
  484. // Now check whether all UV indices are valid ...
  485. bool bNoSpecified = true;
  486. for (unsigned int i = 0; i < pMaterial->mNumProperties; ++i) {
  487. aiMaterialProperty *prop = pMaterial->mProperties[i];
  488. if (static_cast<aiTextureType>(prop->mSemantic) != type) {
  489. continue;
  490. }
  491. if ((int)prop->mIndex >= iNumIndices) {
  492. ReportError("Found texture property with index %i, although there "
  493. "are only %i textures of type %s",
  494. prop->mIndex, iNumIndices, szType);
  495. }
  496. if (!::strcmp(prop->mKey.data, "$tex.mapping")) {
  497. if (aiPTI_Integer != prop->mType || prop->mDataLength < sizeof(aiTextureMapping)) {
  498. ReportError("Material property %s%i is expected to be an integer (size is %i)",
  499. prop->mKey.data, prop->mIndex, prop->mDataLength);
  500. }
  501. mappings[prop->mIndex] = *((aiTextureMapping *)prop->mData);
  502. } else if (!::strcmp(prop->mKey.data, "$tex.uvtrafo")) {
  503. if (aiPTI_Float != prop->mType || prop->mDataLength < sizeof(aiUVTransform)) {
  504. ReportError("Material property %s%i is expected to be 5 floats large (size is %i)",
  505. prop->mKey.data, prop->mIndex, prop->mDataLength);
  506. }
  507. //mappings[prop->mIndex] = ((aiUVTransform*)prop->mData);
  508. } else if (!::strcmp(prop->mKey.data, "$tex.uvwsrc")) {
  509. if (aiPTI_Integer != prop->mType || sizeof(int) > prop->mDataLength) {
  510. ReportError("Material property %s%i is expected to be an integer (size is %i)",
  511. prop->mKey.data, prop->mIndex, prop->mDataLength);
  512. }
  513. bNoSpecified = false;
  514. // Ignore UV indices for texture channels that are not there ...
  515. // Get the value
  516. iIndex = *((unsigned int *)prop->mData);
  517. // Check whether there is a mesh using this material
  518. // which has not enough UV channels ...
  519. for (unsigned int a = 0; a < mScene->mNumMeshes; ++a) {
  520. aiMesh *mesh = this->mScene->mMeshes[a];
  521. if (mesh->mMaterialIndex == (unsigned int)i) {
  522. int iChannels = 0;
  523. while (mesh->HasTextureCoords(iChannels))
  524. ++iChannels;
  525. if (iIndex >= iChannels) {
  526. ReportWarning("Invalid UV index: %i (key %s). Mesh %i has only %i UV channels",
  527. iIndex, prop->mKey.data, a, iChannels);
  528. }
  529. }
  530. }
  531. }
  532. }
  533. if (bNoSpecified) {
  534. // Assume that all textures are using the first UV channel
  535. for (unsigned int a = 0; a < mScene->mNumMeshes; ++a) {
  536. aiMesh *mesh = mScene->mMeshes[a];
  537. if (mesh->mMaterialIndex == (unsigned int)iIndex && mappings[0] == aiTextureMapping_UV) {
  538. if (!mesh->mTextureCoords[0]) {
  539. // This is a special case ... it could be that the
  540. // original mesh format intended the use of a special
  541. // mapping here.
  542. ReportWarning("UV-mapped texture, but there are no UV coords");
  543. }
  544. }
  545. }
  546. }
  547. }
  548. // ------------------------------------------------------------------------------------------------
  549. void ValidateDSProcess::Validate(const aiMaterial *pMaterial) {
  550. // check whether there are material keys that are obviously not legal
  551. for (unsigned int i = 0; i < pMaterial->mNumProperties; ++i) {
  552. const aiMaterialProperty *prop = pMaterial->mProperties[i];
  553. if (!prop) {
  554. ReportError("aiMaterial::mProperties[%i] is nullptr (aiMaterial::mNumProperties is %i)",
  555. i, pMaterial->mNumProperties);
  556. }
  557. if (!prop->mDataLength || !prop->mData) {
  558. ReportError("aiMaterial::mProperties[%i].mDataLength or "
  559. "aiMaterial::mProperties[%i].mData is 0",
  560. i, i);
  561. }
  562. // check all predefined types
  563. if (aiPTI_String == prop->mType) {
  564. // FIX: strings are now stored in a less expensive way, but we can't use the
  565. // validation routine for 'normal' aiStrings
  566. if (prop->mDataLength < 5 || prop->mDataLength < 4 + (*reinterpret_cast<uint32_t *>(prop->mData)) + 1) {
  567. ReportError("aiMaterial::mProperties[%i].mDataLength is "
  568. "too small to contain a string (%i, needed: %i)",
  569. i, prop->mDataLength, static_cast<int>(sizeof(aiString)));
  570. }
  571. if (prop->mData[prop->mDataLength - 1]) {
  572. ReportError("Missing null-terminator in string material property");
  573. }
  574. // Validate((const aiString*)prop->mData);
  575. } else if (aiPTI_Float == prop->mType) {
  576. if (prop->mDataLength < sizeof(float)) {
  577. ReportError("aiMaterial::mProperties[%i].mDataLength is "
  578. "too small to contain a float (%i, needed: %i)",
  579. i, prop->mDataLength, static_cast<int>(sizeof(float)));
  580. }
  581. } else if (aiPTI_Integer == prop->mType) {
  582. if (prop->mDataLength < sizeof(int)) {
  583. ReportError("aiMaterial::mProperties[%i].mDataLength is "
  584. "too small to contain an integer (%i, needed: %i)",
  585. i, prop->mDataLength, static_cast<int>(sizeof(int)));
  586. }
  587. }
  588. // TODO: check whether there is a key with an unknown name ...
  589. }
  590. // make some more specific tests
  591. ai_real fTemp;
  592. int iShading;
  593. if (AI_SUCCESS == aiGetMaterialInteger(pMaterial, AI_MATKEY_SHADING_MODEL, &iShading)) {
  594. switch ((aiShadingMode)iShading) {
  595. case aiShadingMode_Blinn:
  596. case aiShadingMode_CookTorrance:
  597. case aiShadingMode_Phong:
  598. if (AI_SUCCESS != aiGetMaterialFloat(pMaterial, AI_MATKEY_SHININESS, &fTemp)) {
  599. ReportWarning("A specular shading model is specified but there is no "
  600. "AI_MATKEY_SHININESS key");
  601. }
  602. if (AI_SUCCESS == aiGetMaterialFloat(pMaterial, AI_MATKEY_SHININESS_STRENGTH, &fTemp) && !fTemp) {
  603. ReportWarning("A specular shading model is specified but the value of the "
  604. "AI_MATKEY_SHININESS_STRENGTH key is 0.0");
  605. }
  606. break;
  607. default:
  608. break;
  609. }
  610. }
  611. if (AI_SUCCESS == aiGetMaterialFloat(pMaterial, AI_MATKEY_OPACITY, &fTemp) && (!fTemp || fTemp > 1.01)) {
  612. ReportWarning("Invalid opacity value (must be 0 < opacity < 1.0)");
  613. }
  614. // Check whether there are invalid texture keys
  615. // TODO: that's a relict of the past, where texture type and index were baked
  616. // into the material string ... we could do that in one single pass.
  617. SearchForInvalidTextures(pMaterial, aiTextureType_DIFFUSE);
  618. SearchForInvalidTextures(pMaterial, aiTextureType_SPECULAR);
  619. SearchForInvalidTextures(pMaterial, aiTextureType_AMBIENT);
  620. SearchForInvalidTextures(pMaterial, aiTextureType_EMISSIVE);
  621. SearchForInvalidTextures(pMaterial, aiTextureType_OPACITY);
  622. SearchForInvalidTextures(pMaterial, aiTextureType_SHININESS);
  623. SearchForInvalidTextures(pMaterial, aiTextureType_HEIGHT);
  624. SearchForInvalidTextures(pMaterial, aiTextureType_NORMALS);
  625. SearchForInvalidTextures(pMaterial, aiTextureType_DISPLACEMENT);
  626. SearchForInvalidTextures(pMaterial, aiTextureType_LIGHTMAP);
  627. SearchForInvalidTextures(pMaterial, aiTextureType_REFLECTION);
  628. SearchForInvalidTextures(pMaterial, aiTextureType_BASE_COLOR);
  629. SearchForInvalidTextures(pMaterial, aiTextureType_NORMAL_CAMERA);
  630. SearchForInvalidTextures(pMaterial, aiTextureType_EMISSION_COLOR);
  631. SearchForInvalidTextures(pMaterial, aiTextureType_METALNESS);
  632. SearchForInvalidTextures(pMaterial, aiTextureType_DIFFUSE_ROUGHNESS);
  633. SearchForInvalidTextures(pMaterial, aiTextureType_AMBIENT_OCCLUSION);
  634. }
  635. // ------------------------------------------------------------------------------------------------
  636. void ValidateDSProcess::Validate(const aiTexture *pTexture) {
  637. // the data section may NEVER be nullptr
  638. if (nullptr == pTexture->pcData) {
  639. ReportError("aiTexture::pcData is nullptr");
  640. }
  641. if (pTexture->mHeight) {
  642. if (!pTexture->mWidth) {
  643. ReportError("aiTexture::mWidth is zero (aiTexture::mHeight is %i, uncompressed texture)",
  644. pTexture->mHeight);
  645. }
  646. } else {
  647. if (!pTexture->mWidth) {
  648. ReportError("aiTexture::mWidth is zero (compressed texture)");
  649. }
  650. if ('\0' != pTexture->achFormatHint[HINTMAXTEXTURELEN - 1]) {
  651. ReportWarning("aiTexture::achFormatHint must be zero-terminated");
  652. } else if ('.' == pTexture->achFormatHint[0]) {
  653. ReportWarning("aiTexture::achFormatHint should contain a file extension "
  654. "without a leading dot (format hint: %s).",
  655. pTexture->achFormatHint);
  656. }
  657. }
  658. const char *sz = pTexture->achFormatHint;
  659. if ((sz[0] >= 'A' && sz[0] <= 'Z') ||
  660. (sz[1] >= 'A' && sz[1] <= 'Z') ||
  661. (sz[2] >= 'A' && sz[2] <= 'Z') ||
  662. (sz[3] >= 'A' && sz[3] <= 'Z')) {
  663. ReportError("aiTexture::achFormatHint contains non-lowercase letters");
  664. }
  665. }
  666. // ------------------------------------------------------------------------------------------------
  667. void ValidateDSProcess::Validate(const aiAnimation *pAnimation,
  668. const aiNodeAnim *pNodeAnim) {
  669. Validate(&pNodeAnim->mNodeName);
  670. if (!pNodeAnim->mNumPositionKeys && !pNodeAnim->mScalingKeys && !pNodeAnim->mNumRotationKeys) {
  671. ReportError("Empty node animation channel");
  672. }
  673. // otherwise check whether one of the keys exceeds the total duration of the animation
  674. if (pNodeAnim->mNumPositionKeys) {
  675. if (!pNodeAnim->mPositionKeys) {
  676. ReportError("aiNodeAnim::mPositionKeys is nullptr (aiNodeAnim::mNumPositionKeys is %i)",
  677. pNodeAnim->mNumPositionKeys);
  678. }
  679. double dLast = -10e10;
  680. for (unsigned int i = 0; i < pNodeAnim->mNumPositionKeys; ++i) {
  681. // ScenePreprocessor will compute the duration if still the default value
  682. // (Aramis) Add small epsilon, comparison tended to fail if max_time == duration,
  683. // seems to be due the compilers register usage/width.
  684. if (pAnimation->mDuration > 0. && pNodeAnim->mPositionKeys[i].mTime > pAnimation->mDuration + 0.001) {
  685. ReportError("aiNodeAnim::mPositionKeys[%i].mTime (%.5f) is larger "
  686. "than aiAnimation::mDuration (which is %.5f)",
  687. i,
  688. (float)pNodeAnim->mPositionKeys[i].mTime,
  689. (float)pAnimation->mDuration);
  690. }
  691. if (i && pNodeAnim->mPositionKeys[i].mTime <= dLast) {
  692. ReportWarning("aiNodeAnim::mPositionKeys[%i].mTime (%.5f) is smaller "
  693. "than aiAnimation::mPositionKeys[%i] (which is %.5f)",
  694. i,
  695. (float)pNodeAnim->mPositionKeys[i].mTime,
  696. i - 1, (float)dLast);
  697. }
  698. dLast = pNodeAnim->mPositionKeys[i].mTime;
  699. }
  700. }
  701. // rotation keys
  702. if (pNodeAnim->mNumRotationKeys) {
  703. if (!pNodeAnim->mRotationKeys) {
  704. ReportError("aiNodeAnim::mRotationKeys is nullptr (aiNodeAnim::mNumRotationKeys is %i)",
  705. pNodeAnim->mNumRotationKeys);
  706. }
  707. double dLast = -10e10;
  708. for (unsigned int i = 0; i < pNodeAnim->mNumRotationKeys; ++i) {
  709. if (pAnimation->mDuration > 0. && pNodeAnim->mRotationKeys[i].mTime > pAnimation->mDuration + 0.001) {
  710. ReportError("aiNodeAnim::mRotationKeys[%i].mTime (%.5f) is larger "
  711. "than aiAnimation::mDuration (which is %.5f)",
  712. i,
  713. (float)pNodeAnim->mRotationKeys[i].mTime,
  714. (float)pAnimation->mDuration);
  715. }
  716. if (i && pNodeAnim->mRotationKeys[i].mTime <= dLast) {
  717. ReportWarning("aiNodeAnim::mRotationKeys[%i].mTime (%.5f) is smaller "
  718. "than aiAnimation::mRotationKeys[%i] (which is %.5f)",
  719. i,
  720. (float)pNodeAnim->mRotationKeys[i].mTime,
  721. i - 1, (float)dLast);
  722. }
  723. dLast = pNodeAnim->mRotationKeys[i].mTime;
  724. }
  725. }
  726. // scaling keys
  727. if (pNodeAnim->mNumScalingKeys) {
  728. if (!pNodeAnim->mScalingKeys) {
  729. ReportError("aiNodeAnim::mScalingKeys is nullptr (aiNodeAnim::mNumScalingKeys is %i)",
  730. pNodeAnim->mNumScalingKeys);
  731. }
  732. double dLast = -10e10;
  733. for (unsigned int i = 0; i < pNodeAnim->mNumScalingKeys; ++i) {
  734. if (pAnimation->mDuration > 0. && pNodeAnim->mScalingKeys[i].mTime > pAnimation->mDuration + 0.001) {
  735. ReportError("aiNodeAnim::mScalingKeys[%i].mTime (%.5f) is larger "
  736. "than aiAnimation::mDuration (which is %.5f)",
  737. i,
  738. (float)pNodeAnim->mScalingKeys[i].mTime,
  739. (float)pAnimation->mDuration);
  740. }
  741. if (i && pNodeAnim->mScalingKeys[i].mTime <= dLast) {
  742. ReportWarning("aiNodeAnim::mScalingKeys[%i].mTime (%.5f) is smaller "
  743. "than aiAnimation::mScalingKeys[%i] (which is %.5f)",
  744. i,
  745. (float)pNodeAnim->mScalingKeys[i].mTime,
  746. i - 1, (float)dLast);
  747. }
  748. dLast = pNodeAnim->mScalingKeys[i].mTime;
  749. }
  750. }
  751. if (!pNodeAnim->mNumScalingKeys && !pNodeAnim->mNumRotationKeys &&
  752. !pNodeAnim->mNumPositionKeys) {
  753. ReportError("A node animation channel must have at least one subtrack");
  754. }
  755. }
  756. void ValidateDSProcess::Validate(const aiAnimation *pAnimation,
  757. const aiMeshMorphAnim *pMeshMorphAnim) {
  758. Validate(&pMeshMorphAnim->mName);
  759. if (!pMeshMorphAnim->mNumKeys) {
  760. ReportWarning("Empty mesh morph animation channel");
  761. return;
  762. }
  763. // otherwise check whether one of the keys exceeds the total duration of the animation
  764. if (pMeshMorphAnim->mNumKeys) {
  765. if (!pMeshMorphAnim->mKeys) {
  766. ReportError("aiMeshMorphAnim::mKeys is nullptr (aiMeshMorphAnim::mNumKeys is %i)",
  767. pMeshMorphAnim->mNumKeys);
  768. }
  769. double dLast = -10e10;
  770. for (unsigned int i = 0; i < pMeshMorphAnim->mNumKeys; ++i) {
  771. // ScenePreprocessor will compute the duration if still the default value
  772. // (Aramis) Add small epsilon, comparison tended to fail if max_time == duration,
  773. // seems to be due the compilers register usage/width.
  774. if (pAnimation->mDuration > 0. && pMeshMorphAnim->mKeys[i].mTime > pAnimation->mDuration + 0.001) {
  775. ReportError("aiMeshMorphAnim::mKeys[%i].mTime (%.5f) is larger "
  776. "than aiAnimation::mDuration (which is %.5f)",
  777. i,
  778. (float)pMeshMorphAnim->mKeys[i].mTime,
  779. (float)pAnimation->mDuration);
  780. }
  781. if (i && pMeshMorphAnim->mKeys[i].mTime <= dLast) {
  782. ReportWarning("aiMeshMorphAnim::mKeys[%i].mTime (%.5f) is smaller "
  783. "than aiMeshMorphAnim::mKeys[%i] (which is %.5f)",
  784. i,
  785. (float)pMeshMorphAnim->mKeys[i].mTime,
  786. i - 1, (float)dLast);
  787. }
  788. dLast = pMeshMorphAnim->mKeys[i].mTime;
  789. }
  790. }
  791. }
  792. // ------------------------------------------------------------------------------------------------
  793. void ValidateDSProcess::Validate(const aiNode *pNode) {
  794. if (!pNode) {
  795. ReportError("A node of the scene-graph is nullptr");
  796. }
  797. // Validate node name string first so that it's safe to use in below expressions
  798. this->Validate(&pNode->mName);
  799. const char *nodeName = (&pNode->mName)->C_Str();
  800. if (pNode != mScene->mRootNode && !pNode->mParent) {
  801. ReportError("Non-root node %s lacks a valid parent (aiNode::mParent is nullptr) ", nodeName);
  802. }
  803. // validate all meshes
  804. if (pNode->mNumMeshes) {
  805. if (!pNode->mMeshes) {
  806. ReportError("aiNode::mMeshes is nullptr for node %s (aiNode::mNumMeshes is %i)",
  807. nodeName, pNode->mNumMeshes);
  808. }
  809. std::vector<bool> abHadMesh;
  810. abHadMesh.resize(mScene->mNumMeshes, false);
  811. for (unsigned int i = 0; i < pNode->mNumMeshes; ++i) {
  812. if (pNode->mMeshes[i] >= mScene->mNumMeshes) {
  813. ReportError("aiNode::mMeshes[%i] is out of range for node %s (maximum is %i)",
  814. pNode->mMeshes[i], nodeName, mScene->mNumMeshes - 1);
  815. }
  816. if (abHadMesh[pNode->mMeshes[i]]) {
  817. ReportError("aiNode::mMeshes[%i] is already referenced by this node %s (value: %i)",
  818. i, nodeName, pNode->mMeshes[i]);
  819. }
  820. abHadMesh[pNode->mMeshes[i]] = true;
  821. }
  822. }
  823. if (pNode->mNumChildren) {
  824. if (!pNode->mChildren) {
  825. ReportError("aiNode::mChildren is nullptr for node %s (aiNode::mNumChildren is %i)",
  826. nodeName, pNode->mNumChildren);
  827. }
  828. for (unsigned int i = 0; i < pNode->mNumChildren; ++i) {
  829. Validate(pNode->mChildren[i]);
  830. }
  831. }
  832. }
  833. // ------------------------------------------------------------------------------------------------
  834. void ValidateDSProcess::Validate(const aiString *pString) {
  835. if (pString->length > MAXLEN) {
  836. ReportError("aiString::length is too large (%u, maximum is %lu)",
  837. pString->length, MAXLEN);
  838. }
  839. const char *sz = pString->data;
  840. while (true) {
  841. if ('\0' == *sz) {
  842. if (pString->length != (unsigned int)(sz - pString->data)) {
  843. ReportError("aiString::data is invalid: the terminal zero is at a wrong offset");
  844. }
  845. break;
  846. } else if (sz >= &pString->data[MAXLEN]) {
  847. ReportError("aiString::data is invalid. There is no terminal character");
  848. }
  849. ++sz;
  850. }
  851. }