ValidateDataStructure.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the post processing step to validate
  35. * the data structure returned by Assimp
  36. */
  37. // STL headers
  38. #include <vector>
  39. #include <assert.h>
  40. // internal headers
  41. #include "ValidateDataStructure.h"
  42. #include "BaseImporter.h"
  43. #include "StringComparison.h"
  44. #include "fast_atof.h"
  45. // public ASSIMP headers
  46. #include "../include/DefaultLogger.h"
  47. #include "../include/aiPostProcess.h"
  48. #include "../include/aiMesh.h"
  49. #include "../include/aiScene.h"
  50. #include "../include/aiAssert.h"
  51. // CRT headers
  52. #include <stdarg.h>
  53. using namespace Assimp;
  54. // ------------------------------------------------------------------------------------------------
  55. // Constructor to be privately used by Importer
  56. ValidateDSProcess::ValidateDSProcess()
  57. {
  58. // nothing to do here
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Destructor, private as well
  62. ValidateDSProcess::~ValidateDSProcess()
  63. {
  64. // nothing to do here
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Returns whether the processing step is present in the given flag field.
  68. bool ValidateDSProcess::IsActive( unsigned int pFlags) const
  69. {
  70. return (pFlags & aiProcess_ValidateDataStructure) != 0;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. void ValidateDSProcess::ReportError(const char* msg,...)
  74. {
  75. ai_assert(NULL != msg);
  76. va_list args;
  77. va_start(args,msg);
  78. char szBuffer[3000];
  79. int iLen;
  80. #if _MSC_VER >= 1400
  81. iLen = vsprintf_s(szBuffer,msg,args);
  82. #else
  83. iLen = vsprintf(szBuffer,msg,args);
  84. #endif
  85. if (0 >= iLen)
  86. {
  87. // :-) should not happen ...
  88. throw new ImportErrorException("Idiot ... learn coding!");
  89. }
  90. va_end(args);
  91. ai_assert(false);
  92. throw new ImportErrorException("Validation failed: " + std::string(szBuffer,iLen));
  93. }
  94. // ------------------------------------------------------------------------------------------------
  95. void ValidateDSProcess::ReportWarning(const char* msg,...)
  96. {
  97. ai_assert(NULL != msg);
  98. va_list args;
  99. va_start(args,msg);
  100. char szBuffer[3000];
  101. int iLen;
  102. #if _MSC_VER >= 1400
  103. iLen = vsprintf_s(szBuffer,msg,args);
  104. #else
  105. iLen = vsprintf(szBuffer,msg,args);
  106. #endif
  107. if (0 >= iLen)
  108. {
  109. // :-) should not happen ...
  110. throw new ImportErrorException("Idiot ... learn coding!");
  111. }
  112. va_end(args);
  113. DefaultLogger::get()->warn("Validation warning: " + std::string(szBuffer,iLen));
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. // Executes the post processing step on the given imported data.
  117. void ValidateDSProcess::Execute( aiScene* pScene)
  118. {
  119. this->mScene = pScene;
  120. DefaultLogger::get()->debug("ValidateDataStructureProcess begin");
  121. // validate all meshes
  122. if (pScene->mNumMeshes)
  123. {
  124. if (!pScene->mMeshes)
  125. {
  126. this->ReportError("aiScene::mMeshes is NULL (aiScene::mNumMeshes is %i)",
  127. pScene->mNumMeshes);
  128. }
  129. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  130. {
  131. if (!pScene->mMeshes[i])
  132. {
  133. this->ReportError("aiScene::mMeshes[%i] is NULL (aiScene::mNumMeshes is %i)",
  134. i,pScene->mNumMeshes);
  135. }
  136. this->Validate(pScene->mMeshes[i]);
  137. }
  138. }
  139. else if (!(this->mScene->mFlags & AI_SCENE_FLAGS_ANIM_SKELETON_ONLY))
  140. {
  141. this->ReportError("aiScene::mNumMeshes is 0. At least one mesh must be there");
  142. }
  143. // validate all animations
  144. if (pScene->mNumAnimations)
  145. {
  146. if (!pScene->mAnimations)
  147. {
  148. this->ReportError("aiScene::mAnimations is NULL (aiScene::mNumAnimations is %i)",
  149. pScene->mNumAnimations);
  150. }
  151. for (unsigned int i = 0; i < pScene->mNumAnimations;++i)
  152. {
  153. if (!pScene->mAnimations[i])
  154. {
  155. this->ReportError("aiScene::mAnimations[%i] is NULL (aiScene::mNumAnimations is %i)",
  156. i,pScene->mNumAnimations);
  157. }
  158. this->Validate(pScene->mAnimations[i]);
  159. // check whether there are duplicate animation names
  160. for (unsigned int a = i+1; a < pScene->mNumAnimations;++a)
  161. {
  162. if (pScene->mAnimations[i]->mName == pScene->mAnimations[a]->mName)
  163. {
  164. this->ReportError("aiScene::mAnimations[%i] has the same name as "
  165. "aiScene::mAnimations[%i]",i,a);
  166. }
  167. }
  168. }
  169. }
  170. else if (this->mScene->mFlags & AI_SCENE_FLAGS_ANIM_SKELETON_ONLY)
  171. {
  172. this->ReportError("aiScene::mNumAnimations is 0 and the "
  173. "AI_SCENE_FLAGS_ANIM_SKELETON_ONLY flag is set.");
  174. }
  175. // validate all textures
  176. if (pScene->mNumTextures)
  177. {
  178. if (!pScene->mTextures)
  179. {
  180. this->ReportError("aiScene::mTextures is NULL (aiScene::mNumTextures is %i)",
  181. pScene->mNumTextures);
  182. }
  183. for (unsigned int i = 0; i < pScene->mNumTextures;++i)
  184. {
  185. if (!pScene->mTextures[i])
  186. {
  187. this->ReportError("aiScene::mTextures[%i] is NULL (aiScene::mNumTextures is %i)",
  188. i,pScene->mNumTextures);
  189. }
  190. this->Validate(pScene->mTextures[i]);
  191. }
  192. }
  193. // validate all materials
  194. if (pScene->mNumMaterials)
  195. {
  196. if (!pScene->mMaterials)
  197. {
  198. this->ReportError("aiScene::mMaterials is NULL (aiScene::mNumMaterials is %i)",
  199. pScene->mNumMaterials);
  200. }
  201. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  202. {
  203. if (!pScene->mMaterials[i])
  204. {
  205. this->ReportError("aiScene::mMaterials[%i] is NULL (aiScene::mNumMaterials is %i)",
  206. i,pScene->mNumMaterials);
  207. }
  208. this->Validate(pScene->mMaterials[i]);
  209. }
  210. }
  211. else this->ReportError("aiScene::mNumMaterials is 0. At least one material must be there.");
  212. // validate the node graph of the scene
  213. this->Validate(pScene->mRootNode);
  214. DefaultLogger::get()->debug("ValidateDataStructureProcess end");
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. void ValidateDSProcess::Validate( const aiMesh* pMesh)
  218. {
  219. // validate the material index of the mesh
  220. if (pMesh->mMaterialIndex >= this->mScene->mNumMaterials)
  221. {
  222. this->ReportError("aiMesh::mMaterialIndex is invalid (value: %i maximum: %i)",
  223. pMesh->mMaterialIndex,this->mScene->mNumMaterials-1);
  224. }
  225. if (this->mScene->mFlags & AI_SCENE_FLAGS_ANIM_SKELETON_ONLY)
  226. {
  227. if (pMesh->mNumVertices || pMesh->mVertices ||
  228. pMesh->mNumFaces || pMesh->mFaces)
  229. {
  230. this->ReportWarning("The mesh contains vertices and faces although "
  231. "the AI_SCENE_FLAGS_ANIM_SKELETON_ONLY flag is set");
  232. }
  233. }
  234. else
  235. {
  236. // positions must always be there ...
  237. if (!pMesh->mNumVertices || !pMesh->mVertices)
  238. {
  239. this->ReportError("The mesh contains no vertices");
  240. }
  241. // faces, too
  242. if (!pMesh->mNumFaces || !pMesh->mFaces)
  243. {
  244. this->ReportError("The mesh contains no faces");
  245. }
  246. // now check whether the face indexing layout is correct:
  247. // unique vertices, pseudo-indexed.
  248. std::vector<bool> abRefList;
  249. abRefList.resize(pMesh->mNumVertices,false);
  250. for (unsigned int i = 0; i < pMesh->mNumFaces;++i)
  251. {
  252. aiFace& face = pMesh->mFaces[i];
  253. if (!face.mIndices)this->ReportError("aiMesh::mFaces[%i].mIndices is NULL",i);
  254. if (face.mNumIndices < 3)this->ReportError(
  255. "aiMesh::mFaces[%i].mIndices is not a triangle or polygon",i);
  256. for (unsigned int a = 0; a < face.mNumIndices;++a)
  257. {
  258. if (face.mIndices[a] >= pMesh->mNumVertices)
  259. {
  260. this->ReportError("aiMesh::mFaces[%i]::mIndices[%i] is out of range",i,a);
  261. }
  262. // the MSB flag is temporarily used by the extra verbose
  263. // mode to tell us that the JoinVerticesProcess might have
  264. // been executed already.
  265. if ( !(this->mScene->mFlags & 0x80000000 ) && abRefList[face.mIndices[a]])
  266. {
  267. this->ReportError("aiMesh::mVertices[%i] is referenced twice - second "
  268. "time by aiMesh::mFaces[%i]::mIndices[%i]",face.mIndices[a],i,a);
  269. }
  270. abRefList[face.mIndices[a]] = true;
  271. }
  272. }
  273. // check whether there are vertices that aren't referenced by a face
  274. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  275. {
  276. if (!abRefList[i])this->ReportError("aiMesh::mVertices[%i] is not referenced",i);
  277. }
  278. abRefList.clear();
  279. // texture channel 2 may not be set if channel 1 is zero ...
  280. {
  281. unsigned int i = 0;
  282. for (;i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  283. {
  284. if (!pMesh->HasTextureCoords(i))break;
  285. }
  286. for (;i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  287. if (pMesh->HasTextureCoords(i))
  288. {
  289. this->ReportError("Texture coordinate channel %i is existing, "
  290. "although the previous channel was NULL.",i);
  291. }
  292. }
  293. // the same for the vertex colors
  294. {
  295. unsigned int i = 0;
  296. for (;i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
  297. {
  298. if (!pMesh->HasVertexColors(i))break;
  299. }
  300. for (;i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
  301. if (pMesh->HasVertexColors(i))
  302. {
  303. this->ReportError("Vertex color channel %i is existing, "
  304. "although the previous channel was NULL.",i);
  305. }
  306. }
  307. }
  308. // now validate all bones
  309. if (pMesh->HasBones())
  310. {
  311. if (!pMesh->mBones)
  312. {
  313. this->ReportError("aiMesh::mBones is NULL (aiMesh::mNumBones is %i)",
  314. pMesh->mNumBones);
  315. }
  316. float* afSum = NULL;
  317. if (pMesh->mNumVertices)
  318. {
  319. afSum = new float[pMesh->mNumVertices];
  320. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  321. afSum[i] = 0.0f;
  322. }
  323. // check whether there are duplicate bone names
  324. for (unsigned int i = 0; i < pMesh->mNumBones;++i)
  325. {
  326. if (!pMesh->mBones[i])
  327. {
  328. delete[] afSum;
  329. this->ReportError("aiMesh::mBones[%i] is NULL (aiMesh::mNumBones is %i)",
  330. i,pMesh->mNumBones);
  331. }
  332. this->Validate(pMesh,pMesh->mBones[i],afSum);
  333. for (unsigned int a = i+1; a < pMesh->mNumBones;++a)
  334. {
  335. if (pMesh->mBones[i]->mName == pMesh->mBones[a]->mName)
  336. {
  337. delete[] afSum;
  338. this->ReportError("aiMesh::mBones[%i] has the same name as "
  339. "aiMesh::mBones[%i]",i,a);
  340. }
  341. }
  342. }
  343. // check whether all bone weights for a vertex sum to 1.0 ...
  344. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  345. {
  346. if (afSum[i] && (afSum[i] <= 0.995 || afSum[i] >= 1.005))
  347. {
  348. this->ReportWarning("aiMesh::mVertices[%i]: bone weight sum != 1.0 (sum is %f)",i,afSum[i]);
  349. }
  350. }
  351. delete[] afSum;
  352. }
  353. }
  354. // ------------------------------------------------------------------------------------------------
  355. void ValidateDSProcess::Validate( const aiMesh* pMesh,
  356. const aiBone* pBone,float* afSum)
  357. {
  358. this->Validate(&pBone->mName);
  359. if (!pBone->mNumWeights)
  360. {
  361. this->ReportError("aiBone::mNumWeights is zero");
  362. }
  363. // check whether all vertices affected by this bone are valid
  364. for (unsigned int i = 0; i < pBone->mNumWeights;++i)
  365. {
  366. if (pBone->mWeights[i].mVertexId > pMesh->mNumVertices)
  367. {
  368. this->ReportError("aiBone::mWeights[%i].mVertexId is out of range",i);
  369. }
  370. else if (!pBone->mWeights[i].mWeight || pBone->mWeights[i].mWeight > 1.0f)
  371. {
  372. this->ReportWarning("aiBone::mWeights[%i].mWeight has an invalid value",i);
  373. }
  374. afSum[pBone->mWeights[i].mVertexId] += pBone->mWeights[i].mWeight;
  375. }
  376. }
  377. // ------------------------------------------------------------------------------------------------
  378. void ValidateDSProcess::Validate( const aiAnimation* pAnimation)
  379. {
  380. this->Validate(&pAnimation->mName);
  381. // validate all materials
  382. if (pAnimation->mNumBones)
  383. {
  384. if (!pAnimation->mBones)
  385. {
  386. this->ReportError("aiAnimation::mBones is NULL (aiAnimation::mNumBones is %i)",
  387. pAnimation->mBones);
  388. }
  389. for (unsigned int i = 0; i < pAnimation->mNumBones;++i)
  390. {
  391. if (!pAnimation->mBones[i])
  392. {
  393. this->ReportError("aiAnimation::mBones[%i] is NULL (aiAnimation::mNumBones is %i)",
  394. i,pAnimation->mNumBones);
  395. }
  396. this->Validate(pAnimation, pAnimation->mBones[i]);
  397. }
  398. }
  399. else this->ReportError("aiAnimation::mNumBones is 0. At least one bone animation channel must be there.");
  400. // Animation duration is allowed to be zero in cases where the anim contains only a single key frame.
  401. // if (!pAnimation->mDuration)this->ReportError("aiAnimation::mDuration is zero");
  402. }
  403. // ------------------------------------------------------------------------------------------------
  404. void ValidateDSProcess::SearchForInvalidTextures(const aiMaterial* pMaterial,
  405. const char* szType)
  406. {
  407. ai_assert(NULL != szType);
  408. // search all keys of the material ...
  409. // textures must be specified with rising indices (e.g. diffuse #2 may not be
  410. // specified if diffuse #1 is not there ...)
  411. // "$tex.file.<szType>[<index>]"
  412. char szBaseBuf[512];
  413. int iLen;
  414. #if _MSC_VER >= 1400
  415. iLen = ::sprintf_s(szBaseBuf,"$tex.file.%s",szType);
  416. #else
  417. iLen = ::sprintf(szBaseBuf,"$tex.file.%s",szType);
  418. #endif
  419. if (0 >= iLen)return;
  420. int iNumIndices = 0;
  421. int iIndex = -1;
  422. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  423. {
  424. aiMaterialProperty* prop = pMaterial->mProperties[i];
  425. if (0 == ASSIMP_strincmp( prop->mKey.data, szBaseBuf, iLen ))
  426. {
  427. const char* sz = &prop->mKey.data[iLen];
  428. if (*sz)
  429. {
  430. ++sz;
  431. iIndex = std::max(iIndex, (int)strtol10(sz,NULL));
  432. ++iNumIndices;
  433. }
  434. if (aiPTI_String != prop->mType)
  435. this->ReportError("Material property %s is expected to be a string",prop->mKey);
  436. }
  437. }
  438. if (iIndex +1 != iNumIndices)
  439. {
  440. this->ReportError("%s #%i is set, but there are only %i %s textures",
  441. szType,iIndex,iNumIndices,szType);
  442. }
  443. // now check whether all UV indices are valid ...
  444. #if _MSC_VER >= 1400
  445. iLen = ::sprintf_s(szBaseBuf,"$tex.uvw.%s",szType);
  446. #else
  447. iLen = ::sprintf(szBaseBuf,"$tex.uvw.%s",szType);
  448. #endif
  449. if (0 >= iLen)return;
  450. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  451. {
  452. aiMaterialProperty* prop = pMaterial->mProperties[i];
  453. if (0 == ASSIMP_strincmp( prop->mKey.data, szBaseBuf, iLen ))
  454. {
  455. if (aiPTI_Integer != prop->mType || sizeof(int) > prop->mDataLength)
  456. this->ReportError("Material property %s is expected to be an integer",prop->mKey);
  457. const char* sz = &prop->mKey.data[iLen];
  458. if (*sz)
  459. {
  460. ++sz;
  461. iIndex = strtol10(sz,NULL);
  462. // ignore UV indices for texture channel that are not there ...
  463. if (iIndex >= iNumIndices)
  464. {
  465. // get the value
  466. iIndex = *((unsigned int*)prop->mData);
  467. // check whether there is a mesh using this material
  468. // which has not enough UV channels ...
  469. for (unsigned int a = 0; a < this->mScene->mNumMeshes;++a)
  470. {
  471. aiMesh* mesh = this->mScene->mMeshes[a];
  472. if(mesh->mMaterialIndex == iIndex)
  473. {
  474. int iChannels = 0;
  475. while (mesh->HasTextureCoords(iChannels++));
  476. if (iIndex >= iChannels)
  477. {
  478. this->ReportError("Invalid UV index: %i (key %s). Mesh %i has only %i UV channels",
  479. iIndex,prop->mKey,a,iChannels);
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. // ------------------------------------------------------------------------------------------------
  489. void ValidateDSProcess::Validate( const aiMaterial* pMaterial)
  490. {
  491. // check whether there are material keys that are obviously not legal
  492. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  493. {
  494. const aiMaterialProperty* prop = pMaterial->mProperties[i];
  495. if (!prop)
  496. {
  497. this->ReportError("aiMaterial::mProperties[%i] is NULL (aiMaterial::mNumProperties is %i)",
  498. i,pMaterial->mNumProperties);
  499. }
  500. if (!prop->mDataLength || !prop->mData)
  501. {
  502. this->ReportError("aiMaterial::mProperties[%i].mDataLength or "
  503. "aiMaterial::mProperties[%i].mData is 0",i,i);
  504. }
  505. // check all predefined types
  506. if (aiPTI_String == prop->mType)
  507. {
  508. // FIX: strings are now stored in a less expensive way ...
  509. if (prop->mDataLength < sizeof(size_t) + ((const aiString*)prop->mData)->length + 1)
  510. {
  511. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  512. "too small to contain a string (%i, needed: %i)",
  513. i,prop->mDataLength,sizeof(aiString));
  514. }
  515. this->Validate((const aiString*)prop->mData);
  516. }
  517. else if (aiPTI_Float == prop->mType)
  518. {
  519. if (prop->mDataLength < sizeof(float))
  520. {
  521. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  522. "too small to contain a float (%i, needed: %i)",
  523. i,prop->mDataLength,sizeof(float));
  524. }
  525. }
  526. else if (aiPTI_Integer == prop->mType)
  527. {
  528. if (prop->mDataLength < sizeof(int))
  529. {
  530. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  531. "too small to contain an integer (%i, needed: %i)",
  532. i,prop->mDataLength,sizeof(int));
  533. }
  534. }
  535. // TODO: check whether there is a key with an unknown name ...
  536. }
  537. // make some more specific tests
  538. float fTemp;
  539. int iShading;
  540. if (AI_SUCCESS == aiGetMaterialInteger( pMaterial,AI_MATKEY_SHADING_MODEL,&iShading))
  541. {
  542. switch ((aiShadingMode)iShading)
  543. {
  544. case aiShadingMode_Blinn:
  545. case aiShadingMode_CookTorrance:
  546. case aiShadingMode_Phong:
  547. if (AI_SUCCESS != aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS,&fTemp))
  548. {
  549. this->ReportWarning("A specular shading model is specified but there is no "
  550. "AI_MATKEY_SHININESS key");
  551. }
  552. if (AI_SUCCESS == aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS_STRENGTH,&fTemp) && !fTemp)
  553. {
  554. this->ReportWarning("A specular shading model is specified but the value of the "
  555. "AI_MATKEY_SHININESS_STRENGTH key is 0.0");
  556. }
  557. break;
  558. };
  559. }
  560. // check whether there are invalid texture keys
  561. SearchForInvalidTextures(pMaterial,"diffuse");
  562. SearchForInvalidTextures(pMaterial,"specular");
  563. SearchForInvalidTextures(pMaterial,"ambient");
  564. SearchForInvalidTextures(pMaterial,"emissive");
  565. SearchForInvalidTextures(pMaterial,"opacity");
  566. SearchForInvalidTextures(pMaterial,"shininess");
  567. SearchForInvalidTextures(pMaterial,"normals");
  568. SearchForInvalidTextures(pMaterial,"height");
  569. }
  570. // ------------------------------------------------------------------------------------------------
  571. void ValidateDSProcess::Validate( const aiTexture* pTexture)
  572. {
  573. // the data section may NEVER be NULL
  574. if (!pTexture->pcData)
  575. {
  576. this->ReportError("aiTexture::pcData is NULL");
  577. }
  578. if (pTexture->mHeight)
  579. {
  580. if (!pTexture->mWidth)this->ReportError("aiTexture::mWidth is zero "
  581. "(aiTexture::mHeight is %i, uncompressed texture)",pTexture->mHeight);
  582. }
  583. else
  584. {
  585. if (!pTexture->mWidth)this->ReportError("aiTexture::mWidth is zero (compressed texture)");
  586. if ('.' == pTexture->achFormatHint[0])
  587. {
  588. char szTemp[5];
  589. szTemp[0] = pTexture->achFormatHint[0];
  590. szTemp[1] = pTexture->achFormatHint[1];
  591. szTemp[2] = pTexture->achFormatHint[2];
  592. szTemp[3] = pTexture->achFormatHint[3];
  593. szTemp[4] = '\0';
  594. this->ReportWarning("aiTexture::achFormatHint should contain a file extension "
  595. "without a leading dot (format hint: %s).",szTemp);
  596. }
  597. }
  598. const char* sz = pTexture->achFormatHint;
  599. if ( sz[0] >= 'A' && sz[0] <= 'Z' ||
  600. sz[1] >= 'A' && sz[1] <= 'Z' ||
  601. sz[2] >= 'A' && sz[2] <= 'Z' ||
  602. sz[3] >= 'A' && sz[3] <= 'Z')
  603. {
  604. this->ReportError("aiTexture::achFormatHint contains non-lowercase characters");
  605. }
  606. }
  607. // ------------------------------------------------------------------------------------------------
  608. void ValidateDSProcess::Validate( const aiAnimation* pAnimation,
  609. const aiBoneAnim* pBoneAnim)
  610. {
  611. this->Validate(&pBoneAnim->mBoneName);
  612. #if 0
  613. // check whether there is a bone with this name ...
  614. unsigned int i = 0;
  615. for (; i < this->mScene->mNumMeshes;++i)
  616. {
  617. aiMesh* mesh = this->mScene->mMeshes[i];
  618. for (unsigned int a = 0; a < mesh->mNumBones;++a)
  619. {
  620. if (mesh->mBones[a]->mName == pBoneAnim->mBoneName)
  621. goto __break_out;
  622. }
  623. }
  624. __break_out:
  625. if (i == this->mScene->mNumMeshes)
  626. {
  627. this->ReportWarning("aiBoneAnim::mBoneName is \"%s\". However, no bone with this name was found",
  628. pBoneAnim->mBoneName.data);
  629. }
  630. if (!pBoneAnim->mNumPositionKeys && !pBoneAnim->mNumRotationKeys && !pBoneAnim->mNumScalingKeys)
  631. {
  632. this->ReportWarning("A bone animation channel has no keys");
  633. }
  634. #endif
  635. // otherwise check whether one of the keys exceeds the total duration of the animation
  636. if (pBoneAnim->mNumPositionKeys)
  637. {
  638. if (!pBoneAnim->mPositionKeys)
  639. {
  640. this->ReportError("aiBoneAnim::mPositionKeys is NULL (aiBoneAnim::mNumPositionKeys is %i)",
  641. pBoneAnim->mNumPositionKeys);
  642. }
  643. double dLast = -0.1;
  644. for (unsigned int i = 0; i < pBoneAnim->mNumPositionKeys;++i)
  645. {
  646. if (pBoneAnim->mPositionKeys[i].mTime > pAnimation->mDuration)
  647. {
  648. this->ReportError("aiBoneAnim::mPositionKeys[%i].mTime (%.5f) is larger "
  649. "than aiAnimation::mDuration (which is %.5f)",i,
  650. (float)pBoneAnim->mPositionKeys[i].mTime,
  651. (float)pAnimation->mDuration);
  652. }
  653. if (pBoneAnim->mPositionKeys[i].mTime <= dLast)
  654. {
  655. this->ReportError("aiBoneAnim::mPositionKeys[%i].mTime (%.5f) is larger "
  656. "than aiAnimation::mPositionKeys[%i] (which is %.5f)",i,
  657. (float)pBoneAnim->mPositionKeys[i].mTime,
  658. i, (float)dLast);
  659. }
  660. dLast = pBoneAnim->mPositionKeys[i].mTime;
  661. }
  662. }
  663. // rotation keys
  664. if (pBoneAnim->mNumRotationKeys)
  665. {
  666. if (!pBoneAnim->mRotationKeys)
  667. {
  668. this->ReportError("aiBoneAnim::mRotationKeys is NULL (aiBoneAnim::mNumRotationKeys is %i)",
  669. pBoneAnim->mNumRotationKeys);
  670. }
  671. double dLast = -0.1;
  672. for (unsigned int i = 0; i < pBoneAnim->mNumRotationKeys;++i)
  673. {
  674. if (pBoneAnim->mRotationKeys[i].mTime > pAnimation->mDuration)
  675. {
  676. this->ReportError("aiBoneAnim::mRotationKeys[%i].mTime (%.5f) is larger "
  677. "than aiAnimation::mDuration (which is %.5f)",i,
  678. (float)pBoneAnim->mRotationKeys[i].mTime,
  679. (float)pAnimation->mDuration);
  680. }
  681. if (pBoneAnim->mRotationKeys[i].mTime <= dLast)
  682. {
  683. this->ReportError("aiBoneAnim::mRotationKeys[%i].mTime (%.5f) is larger "
  684. "than aiAnimation::mRotationKeys[%i] (which is %.5f)",i,
  685. (float)pBoneAnim->mRotationKeys[i].mTime,
  686. i, (float)dLast);
  687. }
  688. dLast = pBoneAnim->mRotationKeys[i].mTime;
  689. }
  690. }
  691. // scaling keys
  692. if (pBoneAnim->mNumScalingKeys)
  693. {
  694. if (!pBoneAnim->mScalingKeys)
  695. {
  696. this->ReportError("aiBoneAnim::mScalingKeys is NULL (aiBoneAnim::mNumScalingKeys is %i)",
  697. pBoneAnim->mNumScalingKeys);
  698. }
  699. double dLast = -0.1;
  700. for (unsigned int i = 0; i < pBoneAnim->mNumScalingKeys;++i)
  701. {
  702. if (pBoneAnim->mScalingKeys[i].mTime > pAnimation->mDuration)
  703. {
  704. this->ReportError("aiBoneAnim::mScalingKeys[%i].mTime (%.5f) is larger "
  705. "than aiAnimation::mDuration (which is %.5f)",i,
  706. (float)pBoneAnim->mScalingKeys[i].mTime,
  707. (float)pAnimation->mDuration);
  708. }
  709. if (pBoneAnim->mScalingKeys[i].mTime <= dLast)
  710. {
  711. this->ReportError("aiBoneAnim::mScalingKeys[%i].mTime (%.5f) is larger "
  712. "than aiAnimation::mScalingKeys[%i] (which is %.5f)",i,
  713. (float)pBoneAnim->mScalingKeys[i].mTime,
  714. i, (float)dLast);
  715. }
  716. dLast = pBoneAnim->mScalingKeys[i].mTime;
  717. }
  718. }
  719. }
  720. // ------------------------------------------------------------------------------------------------
  721. void ValidateDSProcess::Validate( const aiNode* pNode)
  722. {
  723. if (!pNode)this->ReportError("A node of the scenegraph is NULL");
  724. if (pNode != this->mScene->mRootNode && !pNode->mParent)
  725. this->ReportError("A node has no valid parent (aiNode::mParent is NULL)");
  726. this->Validate(&pNode->mName);
  727. // validate all meshes
  728. if (pNode->mNumMeshes)
  729. {
  730. if (!pNode->mMeshes)
  731. {
  732. this->ReportError("aiNode::mMeshes is NULL (aiNode::mNumMeshes is %i)",
  733. pNode->mNumMeshes);
  734. }
  735. std::vector<bool> abHadMesh;
  736. abHadMesh.resize(this->mScene->mNumMeshes,false);
  737. for (unsigned int i = 0; i < pNode->mNumMeshes;++i)
  738. {
  739. if (pNode->mMeshes[i] >= this->mScene->mNumMeshes)
  740. {
  741. this->ReportError("aiNode::mMeshes[%i] is out of range (maximum is %i)",
  742. pNode->mMeshes[i],this->mScene->mNumMeshes-1);
  743. }
  744. if (abHadMesh[pNode->mMeshes[i]])
  745. {
  746. this->ReportError("aiNode::mMeshes[%i] is already referenced by this node (value: %i)",
  747. i,pNode->mMeshes[i]);
  748. }
  749. abHadMesh[pNode->mMeshes[i]] = true;
  750. }
  751. }
  752. if (pNode->mNumChildren)
  753. {
  754. if (!pNode->mChildren)
  755. {
  756. this->ReportError("aiNode::mChildren is NULL (aiNode::mNumChildren is %i)",
  757. pNode->mNumChildren);
  758. }
  759. for (unsigned int i = 0; i < pNode->mNumChildren;++i)
  760. {
  761. this->Validate(pNode->mChildren[i]);
  762. }
  763. }
  764. }
  765. // ------------------------------------------------------------------------------------------------
  766. void ValidateDSProcess::Validate( const aiString* pString)
  767. {
  768. if (pString->length > MAXLEN)
  769. {
  770. this->ReportError("aiString::length is too large (%i, maximum is %i)",
  771. pString->length,MAXLEN);
  772. }
  773. const char* sz = pString->data;
  774. while (true)
  775. {
  776. if ('\0' == *sz)
  777. {
  778. if (pString->length != (unsigned int)(sz-pString->data))
  779. this->ReportError("aiString::data is invalid: the terminal zero is at a wrong offset");
  780. break;
  781. }
  782. else if (sz >= &pString->data[MAXLEN])
  783. this->ReportError("aiString::data is invalid. There is no terminal character");
  784. ++sz;
  785. }
  786. }