ValidateDataStructure.cpp 26 KB

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