ProcessHelper.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. typedef std::pair< unsigned int,float > PerVertexWeight;
  40. typedef std::vector< PerVertexWeight > VertexWeightTable;
  41. // ------------------------------------------------------------------------------------------------
  42. // compute a good epsilon value for position comparisons
  43. inline float ComputePositionEpsilon(const aiMesh* pMesh)
  44. {
  45. const float epsilon = 1e-5f;
  46. // calculate the position bounds so we have a reliable epsilon to check position differences against
  47. aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f);
  48. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  49. {
  50. minVec.x = std::min( minVec.x, pMesh->mVertices[a].x);
  51. minVec.y = std::min( minVec.y, pMesh->mVertices[a].y);
  52. minVec.z = std::min( minVec.z, pMesh->mVertices[a].z);
  53. maxVec.x = std::max( maxVec.x, pMesh->mVertices[a].x);
  54. maxVec.y = std::max( maxVec.y, pMesh->mVertices[a].y);
  55. maxVec.z = std::max( maxVec.z, pMesh->mVertices[a].z);
  56. }
  57. return (maxVec - minVec).Length() * epsilon;
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Compute a per-vertex bone weight table
  61. // NOTE: delete result with operator delete[] ...
  62. inline VertexWeightTable* ComputeVertexBoneWeightTable(aiMesh* pMesh)
  63. {
  64. if (!pMesh || !pMesh->mNumVertices || !pMesh->mNumBones) return NULL;
  65. VertexWeightTable* avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices];
  66. for (unsigned int i = 0; i < pMesh->mNumBones;++i)
  67. {
  68. aiBone* bone = pMesh->mBones[i];
  69. for (unsigned int a = 0; a < bone->mNumWeights;++a)
  70. {
  71. aiVertexWeight& weight = bone->mWeights[a];
  72. avPerVertexWeights[weight.mVertexId].push_back(
  73. std::pair<unsigned int,float>(i,weight.mWeight));
  74. }
  75. }
  76. return avPerVertexWeights;
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Get a string for a given aiTextureType
  80. inline const char* TextureTypeToString(aiTextureType in)
  81. {
  82. switch (in)
  83. {
  84. case aiTextureType_DIFFUSE:
  85. return "Diffuse";
  86. case aiTextureType_SPECULAR:
  87. return "Specular";
  88. case aiTextureType_AMBIENT:
  89. return "Ambient";
  90. case aiTextureType_EMISSIVE:
  91. return "Emissive";
  92. case aiTextureType_OPACITY:
  93. return "Opacity";
  94. case aiTextureType_NORMALS:
  95. return "Normals";
  96. case aiTextureType_HEIGHT:
  97. return "Height";
  98. case aiTextureType_SHININESS:
  99. return "Shininess";
  100. default:
  101. return "LARGE ERROR, please leave the room immediately and call the police";
  102. }
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // Get a string for a given aiTextureMapping
  106. inline const char* MappingTypeToString(aiTextureMapping in)
  107. {
  108. switch (in)
  109. {
  110. case aiTextureMapping_UV:
  111. return "UV";
  112. case aiTextureMapping_BOX:
  113. return "Box";
  114. case aiTextureMapping_SPHERE:
  115. return "Sphere";
  116. case aiTextureMapping_CYLINDER:
  117. return "Cylinder";
  118. case aiTextureMapping_PLANE:
  119. return "Plane";
  120. case aiTextureMapping_OTHER:
  121. return "Other";
  122. default:
  123. return "LARGE ERROR, please leave the room immediately and call the police";
  124. }
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. class ComputeSpatialSortProcess : public BaseProcess
  128. {
  129. bool IsActive( unsigned int pFlags) const
  130. {
  131. return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  132. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  133. }
  134. void Execute( aiScene* pScene)
  135. {
  136. typedef std::pair<SpatialSort, float> _Type;
  137. std::vector<_Type>* p = new std::vector<_Type>(pScene->mNumMeshes);
  138. std::vector<_Type>::iterator it = p->begin();
  139. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++it)
  140. {
  141. aiMesh* mesh = pScene->mMeshes[i];
  142. _Type& blubb = *it;
  143. blubb.first.Fill(mesh->mVertices,mesh->mNumVertices,sizeof(aiVector3D));
  144. blubb.second = ComputePositionEpsilon(mesh);
  145. }
  146. shared->AddProperty(AI_SPP_SPATIAL_SORT,p);
  147. }
  148. };
  149. // ------------------------------------------------------------------------------------------------
  150. class DestroySpatialSortProcess : public BaseProcess
  151. {
  152. bool IsActive( unsigned int pFlags) const
  153. {
  154. return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  155. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  156. }
  157. void Execute( aiScene* pScene)
  158. {
  159. shared->RemoveProperty(AI_SPP_SPATIAL_SORT);
  160. }
  161. };
  162. } // !! Assimp
  163. #endif // !! AI_PROCESS_HELPER_H_INCLUDED