ProcessHelper.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, 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. #include "ParsingUtils.h"
  39. // -------------------------------------------------------------------------------
  40. // Some extensions to std namespace. Mainly std::min and std::max for all
  41. // flat data types in the aiScene. They're used to quickly determine the
  42. // min/max bounds of data arrays.
  43. #ifdef __cplusplus
  44. namespace std {
  45. // std::min for aiVector3D
  46. inline ::aiVector3D min (const ::aiVector3D& a, const ::aiVector3D& b) {
  47. return ::aiVector3D (min(a.x,b.x),min(a.y,b.y),min(a.z,b.z));
  48. }
  49. // std::max for aiVector3D
  50. inline ::aiVector3D max (const ::aiVector3D& a, const ::aiVector3D& b) {
  51. return ::aiVector3D (max(a.x,b.x),max(a.y,b.y),max(a.z,b.z));
  52. }
  53. // std::min for aiVector2D
  54. inline ::aiVector2D min (const ::aiVector2D& a, const ::aiVector2D& b) {
  55. return ::aiVector2D (min(a.x,b.x),min(a.y,b.y));
  56. }
  57. // std::max for aiVector2D
  58. inline ::aiVector2D max (const ::aiVector2D& a, const ::aiVector2D& b) {
  59. return ::aiVector2D (max(a.x,b.x),max(a.y,b.y));
  60. }
  61. // std::min for aiColor4D
  62. inline ::aiColor4D min (const ::aiColor4D& a, const ::aiColor4D& b) {
  63. return ::aiColor4D (min(a.r,b.r),min(a.g,b.g),min(a.b,b.b),min(a.a,b.a));
  64. }
  65. // std::max for aiColor4D
  66. inline ::aiColor4D max (const ::aiColor4D& a, const ::aiColor4D& b) {
  67. return ::aiColor4D (max(a.r,b.r),max(a.g,b.g),max(a.b,b.b),max(a.a,b.a));
  68. }
  69. // std::min for aiQuaternion
  70. inline ::aiQuaternion min (const ::aiQuaternion& a, const ::aiQuaternion& b) {
  71. return ::aiQuaternion (min(a.w,b.w),min(a.x,b.x),min(a.y,b.y),min(a.z,b.z));
  72. }
  73. // std::max for aiQuaternion
  74. inline ::aiQuaternion max (const ::aiQuaternion& a, const ::aiQuaternion& b) {
  75. return ::aiQuaternion (max(a.w,b.w),max(a.x,b.x),max(a.y,b.y),max(a.z,b.z));
  76. }
  77. // std::min for aiVectorKey
  78. inline ::aiVectorKey min (const ::aiVectorKey& a, const ::aiVectorKey& b) {
  79. return ::aiVectorKey (min(a.mTime,b.mTime),min(a.mValue,b.mValue));
  80. }
  81. // std::max for aiVectorKey
  82. inline ::aiVectorKey max (const ::aiVectorKey& a, const ::aiVectorKey& b) {
  83. return ::aiVectorKey (max(a.mTime,b.mTime),max(a.mValue,b.mValue));
  84. }
  85. // std::min for aiQuatKey
  86. inline ::aiQuatKey min (const ::aiQuatKey& a, const ::aiQuatKey& b) {
  87. return ::aiQuatKey (min(a.mTime,b.mTime),min(a.mValue,b.mValue));
  88. }
  89. // std::max for aiQuatKey
  90. inline ::aiQuatKey max (const ::aiQuatKey& a, const ::aiQuatKey& b) {
  91. return ::aiQuatKey (max(a.mTime,b.mTime),max(a.mValue,b.mValue));
  92. }
  93. // std::min for aiVertexWeight
  94. inline ::aiVertexWeight min (const ::aiVertexWeight& a, const ::aiVertexWeight& b) {
  95. return ::aiVertexWeight (min(a.mVertexId,b.mVertexId),min(a.mWeight,b.mWeight));
  96. }
  97. // std::max for aiVertexWeight
  98. inline ::aiVertexWeight max (const ::aiVertexWeight& a, const ::aiVertexWeight& b) {
  99. return ::aiVertexWeight (max(a.mVertexId,b.mVertexId),max(a.mWeight,b.mWeight));
  100. }
  101. } // end namespace std
  102. #endif // !! C++
  103. namespace Assimp {
  104. // -------------------------------------------------------------------------------
  105. // Start points for ArrayBounds<T> for all supported Ts
  106. template <typename T>
  107. struct MinMaxChooser;
  108. template <> struct MinMaxChooser<float> {
  109. void operator ()(float& min,float& max) {
  110. max = -1e10f;
  111. min = 1e10f;
  112. }};
  113. template <> struct MinMaxChooser<double> {
  114. void operator ()(double& min,double& max) {
  115. max = -1e10;
  116. min = 1e10;
  117. }};
  118. template <> struct MinMaxChooser<unsigned int> {
  119. void operator ()(unsigned int& min,unsigned int& max) {
  120. max = 0;
  121. min = (1u<<(sizeof(unsigned int)*8-1));
  122. }};
  123. template <> struct MinMaxChooser<aiVector3D> {
  124. void operator ()(aiVector3D& min,aiVector3D& max) {
  125. max = aiVector3D(-1e10f,-1e10f,-1e10f);
  126. min = aiVector3D( 1e10f, 1e10f, 1e10f);
  127. }};
  128. template <> struct MinMaxChooser<aiVector2D> {
  129. void operator ()(aiVector2D& min,aiVector2D& max) {
  130. max = aiVector2D(-1e10f,-1e10f);
  131. min = aiVector2D( 1e10f, 1e10f);
  132. }};
  133. template <> struct MinMaxChooser<aiColor4D> {
  134. void operator ()(aiColor4D& min,aiColor4D& max) {
  135. max = aiColor4D(-1e10f,-1e10f,-1e10f,-1e10f);
  136. min = aiColor4D( 1e10f, 1e10f, 1e10f, 1e10f);
  137. }};
  138. template <> struct MinMaxChooser<aiQuaternion> {
  139. void operator ()(aiQuaternion& min,aiQuaternion& max) {
  140. max = aiQuaternion(-1e10f,-1e10f,-1e10f,-1e10f);
  141. min = aiQuaternion( 1e10f, 1e10f, 1e10f, 1e10f);
  142. }};
  143. template <> struct MinMaxChooser<aiVectorKey> {
  144. void operator ()(aiVectorKey& min,aiVectorKey& max) {
  145. MinMaxChooser<double>()(min.mTime,max.mTime);
  146. MinMaxChooser<aiVector3D>()(min.mValue,max.mValue);
  147. }};
  148. template <> struct MinMaxChooser<aiQuatKey> {
  149. void operator ()(aiQuatKey& min,aiQuatKey& max) {
  150. MinMaxChooser<double>()(min.mTime,max.mTime);
  151. MinMaxChooser<aiQuaternion>()(min.mValue,max.mValue);
  152. }};
  153. template <> struct MinMaxChooser<aiVertexWeight> {
  154. void operator ()(aiVertexWeight& min,aiVertexWeight& max) {
  155. MinMaxChooser<unsigned int>()(min.mVertexId,max.mVertexId);
  156. MinMaxChooser<float>()(min.mWeight,max.mWeight);
  157. }};
  158. // -------------------------------------------------------------------------------
  159. /** @brief Find the min/max values of an array of Ts
  160. * @param in Input array
  161. * @param size Numebr of elements to process
  162. * @param[out] min minimum value
  163. * @param[out] max maximum value
  164. */
  165. template <typename T>
  166. inline void ArrayBounds(const T* in, unsigned int size, T& min, T& max)
  167. {
  168. MinMaxChooser<T> ()(min,max);
  169. for (unsigned int i = 0; i < size;++i) {
  170. min = std::min(in[i],min);
  171. max = std::max(in[i],max);
  172. }
  173. }
  174. // -------------------------------------------------------------------------------
  175. /** Little helper function to calculate the quadratic difference
  176. * of two colours.
  177. * @param pColor1 First color
  178. * @param pColor2 second color
  179. * @return Quadratic color difference */
  180. inline float GetColorDifference( const aiColor4D& pColor1, const aiColor4D& pColor2)
  181. {
  182. const aiColor4D c (pColor1.r - pColor2.r, pColor1.g - pColor2.g, pColor1.b - pColor2.b, pColor1.a - pColor2.a);
  183. return c.r*c.r + c.g*c.g + c.b*c.b + c.a*c.a;
  184. }
  185. // -------------------------------------------------------------------------------
  186. /** @brief Extract single strings from a list of identifiers
  187. * @param in Input string list.
  188. * @param out Receives a list of clean output strings
  189. * @sdee #AI_CONFIG_PP_OG_EXCLUDE_LIST */
  190. void ConvertListToStrings(const std::string& in, std::list<std::string>& out);
  191. // -------------------------------------------------------------------------------
  192. /** @brief Compute the AABB of a mesh after applying a given transform
  193. * @param mesh Input mesh
  194. * @param[out] min Receives minimum transformed vertex
  195. * @param[out] max Receives maximum transformed vertex
  196. * @param m Transformation matrix to be applied */
  197. void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D& max, const aiMatrix4x4& m);
  198. // -------------------------------------------------------------------------------
  199. /** @brief Helper function to determine the 'real' center of a mesh
  200. *
  201. * That is the center of its axis-aligned bounding box.
  202. * @param mesh Input mesh
  203. * @param[out] min Minimum vertex of the mesh
  204. * @param[out] max maximum vertex of the mesh
  205. * @param[out] out Center point */
  206. void FindMeshCenter (aiMesh* mesh, aiVector3D& out, aiVector3D& min, aiVector3D& max);
  207. // -------------------------------------------------------------------------------
  208. // Helper function to determine the 'real' center of a mesh after applying a given transform
  209. void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out, aiVector3D& min,aiVector3D& max, const aiMatrix4x4& m);
  210. // -------------------------------------------------------------------------------
  211. // Helper function to determine the 'real' center of a mesh
  212. void FindMeshCenter (aiMesh* mesh, aiVector3D& out);
  213. // -------------------------------------------------------------------------------
  214. // Helper function to determine the 'real' center of a mesh after applying a given transform
  215. void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out,const aiMatrix4x4& m);
  216. // -------------------------------------------------------------------------------
  217. // Compute a good epsilon value for position comparisons on a mesh
  218. float ComputePositionEpsilon(const aiMesh* pMesh);
  219. // -------------------------------------------------------------------------------
  220. // Compute a good epsilon value for position comparisons on a array of meshes
  221. float ComputePositionEpsilon(const aiMesh* const* pMeshes, size_t num);
  222. // -------------------------------------------------------------------------------
  223. // Compute an unique value for the vertex format of a mesh
  224. unsigned int GetMeshVFormatUnique(const aiMesh* pcMesh);
  225. // defs for ComputeVertexBoneWeightTable()
  226. typedef std::pair <unsigned int,float> PerVertexWeight;
  227. typedef std::vector <PerVertexWeight> VertexWeightTable;
  228. // -------------------------------------------------------------------------------
  229. // Compute a per-vertex bone weight table
  230. VertexWeightTable* ComputeVertexBoneWeightTable(const aiMesh* pMesh);
  231. // -------------------------------------------------------------------------------
  232. // Get a string for a given aiTextureType
  233. const char* TextureTypeToString(aiTextureType in);
  234. // -------------------------------------------------------------------------------
  235. // Get a string for a given aiTextureMapping
  236. const char* MappingTypeToString(aiTextureMapping in);
  237. // flags for MakeSubmesh()
  238. #define AI_SUBMESH_FLAGS_SANS_BONES 0x1
  239. // -------------------------------------------------------------------------------
  240. // Split a mesh given a list of faces to be contained in the sub mesh
  241. aiMesh* MakeSubmesh(const aiMesh *superMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags);
  242. // -------------------------------------------------------------------------------
  243. // Utility postprocess step to share the spatial sort tree between
  244. // all steps which use it to speedup its computations.
  245. class ComputeSpatialSortProcess : public BaseProcess
  246. {
  247. bool IsActive( unsigned int pFlags) const
  248. {
  249. return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  250. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  251. }
  252. void Execute( aiScene* pScene)
  253. {
  254. typedef std::pair<SpatialSort, float> _Type;
  255. DefaultLogger::get()->debug("Generate spatially-sorted vertex cache");
  256. std::vector<_Type>* p = new std::vector<_Type>(pScene->mNumMeshes);
  257. std::vector<_Type>::iterator it = p->begin();
  258. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++it) {
  259. aiMesh* mesh = pScene->mMeshes[i];
  260. _Type& blubb = *it;
  261. blubb.first.Fill(mesh->mVertices,mesh->mNumVertices,sizeof(aiVector3D));
  262. blubb.second = ComputePositionEpsilon(mesh);
  263. }
  264. shared->AddProperty(AI_SPP_SPATIAL_SORT,p);
  265. }
  266. };
  267. // -------------------------------------------------------------------------------
  268. // ... and the same again to cleanup the whole stuff
  269. class DestroySpatialSortProcess : public BaseProcess
  270. {
  271. bool IsActive( unsigned int pFlags) const
  272. {
  273. return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  274. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  275. }
  276. void Execute( aiScene* /*pScene*/)
  277. {
  278. shared->RemoveProperty(AI_SPP_SPATIAL_SORT);
  279. }
  280. };
  281. } // ! namespace Assimp
  282. #endif // !! AI_PROCESS_HELPER_H_INCLUDED