ProcessHelper.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef AI_PROCESS_HELPER_H_INCLUDED
  34. #define AI_PROCESS_HELPER_H_INCLUDED
  35. #include "../include/aiPostProcess.h"
  36. #include "SpatialSort.h"
  37. #include "BaseProcess.h"
  38. namespace Assimp {
  39. // some aliases to make the whole stuff easier to read
  40. typedef std::pair < unsigned int,float > PerVertexWeight;
  41. typedef std::vector < PerVertexWeight > VertexWeightTable;
  42. // -------------------------------------------------------------------------------
  43. /** Little helper function to calculate the quadratic difference
  44. * of two colours.
  45. * @param pColor1 First color
  46. * @param pColor2 second color
  47. * @return Quadratic color difference
  48. */
  49. inline float GetColorDifference( const aiColor4D& pColor1, const aiColor4D& pColor2)
  50. {
  51. const aiColor4D c (pColor1.r - pColor2.r, pColor1.g - pColor2.g,
  52. pColor1.b - pColor2.b, pColor1.a - pColor2.a);
  53. return c.r*c.r + c.g*c.g + c.b*c.b + c.a*c.a;
  54. }
  55. // -------------------------------------------------------------------------------
  56. // Compute the AABB of a mesh
  57. inline void FindAABB (const aiMesh* mesh, aiVector3D& min, aiVector3D& max)
  58. {
  59. min = aiVector3D (10e10f, 10e10f, 10e10f);
  60. max = aiVector3D (-10e10f,-10e10f,-10e10f);
  61. for (unsigned int i = 0;i < mesh->mNumVertices;++i)
  62. {
  63. const aiVector3D& v = mesh->mVertices[i];
  64. min.x = ::std::min(v.x,min.x);
  65. min.y = ::std::min(v.y,min.y);
  66. min.z = ::std::min(v.z,min.z);
  67. max.x = ::std::max(v.x,max.x);
  68. max.y = ::std::max(v.y,max.y);
  69. max.z = ::std::max(v.z,max.z);
  70. }
  71. }
  72. // -------------------------------------------------------------------------------
  73. // Compute the AABB of a mesh after applying a given transform
  74. inline void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D& max,
  75. const aiMatrix4x4& m)
  76. {
  77. min = aiVector3D (10e10f, 10e10f, 10e10f);
  78. max = aiVector3D (-10e10f,-10e10f,-10e10f);
  79. for (unsigned int i = 0;i < mesh->mNumVertices;++i)
  80. {
  81. const aiVector3D v = m * mesh->mVertices[i];
  82. min.x = ::std::min(v.x,min.x);
  83. min.y = ::std::min(v.y,min.y);
  84. min.z = ::std::min(v.z,min.z);
  85. max.x = ::std::max(v.x,max.x);
  86. max.y = ::std::max(v.y,max.y);
  87. max.z = ::std::max(v.z,max.z);
  88. }
  89. }
  90. // -------------------------------------------------------------------------------
  91. // Helper function to determine the 'real' center of a mesh
  92. inline void FindMeshCenter (aiMesh* mesh, aiVector3D& out, aiVector3D& min, aiVector3D& max)
  93. {
  94. FindAABB(mesh,min,max);
  95. out = min + (max-min)*0.5f;
  96. }
  97. // -------------------------------------------------------------------------------
  98. // Helper function to determine the 'real' center of a mesh after applying a given transform
  99. inline void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out, aiVector3D& min,
  100. aiVector3D& max, const aiMatrix4x4& m)
  101. {
  102. FindAABBTransformed(mesh,min,max,m);
  103. out = min + (max-min)*0.5f;
  104. }
  105. // -------------------------------------------------------------------------------
  106. // Helper function to determine the 'real' center of a mesh
  107. inline void FindMeshCenter (aiMesh* mesh, aiVector3D& out)
  108. {
  109. aiVector3D min,max;
  110. FindMeshCenter(mesh,out,min,max);
  111. }
  112. // -------------------------------------------------------------------------------
  113. // Helper function to determine the 'real' center of a mesh after applying a given transform
  114. inline void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out,
  115. const aiMatrix4x4& m)
  116. {
  117. aiVector3D min,max;
  118. FindMeshCenterTransformed(mesh,out,min,max,m);
  119. }
  120. // -------------------------------------------------------------------------------
  121. // Compute a good epsilon value for position comparisons on a mesh
  122. inline float ComputePositionEpsilon(const aiMesh* pMesh)
  123. {
  124. const float epsilon = 1e-5f;
  125. // calculate the position bounds so we have a reliable epsilon to check position differences against
  126. aiVector3D minVec, maxVec;
  127. FindAABB(pMesh,minVec,maxVec);
  128. return (maxVec - minVec).Length() * epsilon;
  129. }
  130. // -------------------------------------------------------------------------------
  131. // Compute an unique value for the vertex format of a mesh
  132. inline unsigned int GetMeshVFormatUnique(aiMesh* pcMesh)
  133. {
  134. ai_assert(NULL != pcMesh);
  135. // FIX: the hash may never be 0. Otherwise a comparison against
  136. // nullptr could be successful
  137. unsigned int iRet = 1;
  138. // normals
  139. if (pcMesh->HasNormals())iRet |= 0x2;
  140. // tangents and bitangents
  141. if (pcMesh->HasTangentsAndBitangents())iRet |= 0x4;
  142. BOOST_STATIC_ASSERT(8 >= AI_MAX_NUMBER_OF_COLOR_SETS);
  143. BOOST_STATIC_ASSERT(8 >= AI_MAX_NUMBER_OF_TEXTURECOORDS);
  144. // texture coordinates
  145. unsigned int p = 0;
  146. while (pcMesh->HasTextureCoords(p))
  147. {
  148. iRet |= (0x100 << p);
  149. if (3 == pcMesh->mNumUVComponents[p])
  150. iRet |= (0x10000 << p);
  151. ++p;
  152. }
  153. // vertex colors
  154. p = 0;
  155. while (pcMesh->HasVertexColors(p))iRet |= (0x1000000 << p++);
  156. return iRet;
  157. }
  158. // -------------------------------------------------------------------------------
  159. // Compute a per-vertex bone weight table
  160. // please .... delete result with operator delete[] ...
  161. inline VertexWeightTable* ComputeVertexBoneWeightTable(aiMesh* pMesh)
  162. {
  163. if (!pMesh || !pMesh->mNumVertices || !pMesh->mNumBones)
  164. return NULL;
  165. VertexWeightTable* avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices];
  166. for (unsigned int i = 0; i < pMesh->mNumBones;++i)
  167. {
  168. aiBone* bone = pMesh->mBones[i];
  169. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  170. const aiVertexWeight& weight = bone->mWeights[a];
  171. avPerVertexWeights[weight.mVertexId].push_back(
  172. std::pair<unsigned int,float>(i,weight.mWeight));
  173. }
  174. }
  175. return avPerVertexWeights;
  176. }
  177. // -------------------------------------------------------------------------------
  178. // Get a string for a given aiTextureType
  179. inline const char* TextureTypeToString(aiTextureType in)
  180. {
  181. switch (in)
  182. {
  183. case aiTextureType_DIFFUSE:
  184. return "Diffuse";
  185. case aiTextureType_SPECULAR:
  186. return "Specular";
  187. case aiTextureType_AMBIENT:
  188. return "Ambient";
  189. case aiTextureType_EMISSIVE:
  190. return "Emissive";
  191. case aiTextureType_OPACITY:
  192. return "Opacity";
  193. case aiTextureType_NORMALS:
  194. return "Normals";
  195. case aiTextureType_HEIGHT:
  196. return "Height";
  197. case aiTextureType_SHININESS:
  198. return "Shininess";
  199. case aiTextureType_DISPLACEMENT:
  200. return "Displacement";
  201. case aiTextureType_LIGHTMAP:
  202. return "Lightmap";
  203. case aiTextureType_REFLECTION:
  204. return "Reflection";
  205. case aiTextureType_UNKNOWN:
  206. return "Unknown";
  207. default:
  208. return "HUGE ERROR, please leave the room immediately and call the police";
  209. }
  210. }
  211. // -------------------------------------------------------------------------------
  212. // Get a string for a given aiTextureMapping
  213. inline const char* MappingTypeToString(aiTextureMapping in)
  214. {
  215. switch (in)
  216. {
  217. case aiTextureMapping_UV:
  218. return "UV";
  219. case aiTextureMapping_BOX:
  220. return "Box";
  221. case aiTextureMapping_SPHERE:
  222. return "Sphere";
  223. case aiTextureMapping_CYLINDER:
  224. return "Cylinder";
  225. case aiTextureMapping_PLANE:
  226. return "Plane";
  227. case aiTextureMapping_OTHER:
  228. return "Other";
  229. default:
  230. return "HUGE ERROR, please leave the room immediately and call the police";
  231. }
  232. }
  233. // -------------------------------------------------------------------------------
  234. // Utility postprocess step to share the spatial sort tree between
  235. // all steps which use it to speedup its computations.
  236. class ComputeSpatialSortProcess : public BaseProcess
  237. {
  238. bool IsActive( unsigned int pFlags) const
  239. {
  240. return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  241. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  242. }
  243. void Execute( aiScene* pScene)
  244. {
  245. typedef std::pair<SpatialSort, float> _Type;
  246. std::vector<_Type>* p = new std::vector<_Type>(pScene->mNumMeshes);
  247. std::vector<_Type>::iterator it = p->begin();
  248. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++it) {
  249. aiMesh* mesh = pScene->mMeshes[i];
  250. _Type& blubb = *it;
  251. blubb.first.Fill(mesh->mVertices,mesh->mNumVertices,sizeof(aiVector3D));
  252. blubb.second = ComputePositionEpsilon(mesh);
  253. }
  254. shared->AddProperty(AI_SPP_SPATIAL_SORT,p);
  255. }
  256. };
  257. // -------------------------------------------------------------------------------
  258. // ... and the same again to cleanup the whole stuff
  259. class DestroySpatialSortProcess : public BaseProcess
  260. {
  261. bool IsActive( unsigned int pFlags) const
  262. {
  263. return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  264. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  265. }
  266. void Execute( aiScene* pScene)
  267. {
  268. shared->RemoveProperty(AI_SPP_SPATIAL_SORT);
  269. }
  270. };
  271. } // ! namespace Assimp
  272. #endif // !! AI_PROCESS_HELPER_H_INCLUDED