ProcessHelper.h 14 KB

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