2
0

ValidateDataStructure.cpp 40 KB

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