ProcessHelper.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, 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/anim.h>
  36. #include <assimp/material.h>
  37. #include <assimp/mesh.h>
  38. #include <assimp/postprocess.h>
  39. #include <assimp/scene.h>
  40. #include <assimp/DefaultLogger.hpp>
  41. #include "Common/BaseProcess.h"
  42. #include <assimp/ParsingUtils.h>
  43. #include <assimp/SpatialSort.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 <>
  123. struct MinMaxChooser<float> {
  124. void operator()(float &min, float &max) {
  125. max = -1e10f;
  126. min = 1e10f;
  127. }
  128. };
  129. template <>
  130. struct MinMaxChooser<double> {
  131. void operator()(double &min, double &max) {
  132. max = -1e10;
  133. min = 1e10;
  134. }
  135. };
  136. template <>
  137. struct MinMaxChooser<unsigned int> {
  138. void operator()(unsigned int &min, unsigned int &max) {
  139. max = 0;
  140. min = (1u << (sizeof(unsigned int) * 8 - 1));
  141. }
  142. };
  143. template <typename T>
  144. struct MinMaxChooser<aiVector3t<T>> {
  145. void operator()(aiVector3t<T> &min, aiVector3t<T> &max) {
  146. max = aiVector3t<T>(-1e10f, -1e10f, -1e10f);
  147. min = aiVector3t<T>(1e10f, 1e10f, 1e10f);
  148. }
  149. };
  150. template <typename T>
  151. struct MinMaxChooser<aiVector2t<T>> {
  152. void operator()(aiVector2t<T> &min, aiVector2t<T> &max) {
  153. max = aiVector2t<T>(-1e10f, -1e10f);
  154. min = aiVector2t<T>(1e10f, 1e10f);
  155. }
  156. };
  157. template <typename T>
  158. struct MinMaxChooser<aiColor4t<T>> {
  159. void operator()(aiColor4t<T> &min, aiColor4t<T> &max) {
  160. max = aiColor4t<T>(-1e10f, -1e10f, -1e10f, -1e10f);
  161. min = aiColor4t<T>(1e10f, 1e10f, 1e10f, 1e10f);
  162. }
  163. };
  164. template <typename T>
  165. struct MinMaxChooser<aiQuaterniont<T>> {
  166. void operator()(aiQuaterniont<T> &min, aiQuaterniont<T> &max) {
  167. max = aiQuaterniont<T>(-1e10f, -1e10f, -1e10f, -1e10f);
  168. min = aiQuaterniont<T>(1e10f, 1e10f, 1e10f, 1e10f);
  169. }
  170. };
  171. template <>
  172. struct MinMaxChooser<aiVectorKey> {
  173. void operator()(aiVectorKey &min, aiVectorKey &max) {
  174. MinMaxChooser<double>()(min.mTime, max.mTime);
  175. MinMaxChooser<aiVector3D>()(min.mValue, max.mValue);
  176. }
  177. };
  178. template <>
  179. struct MinMaxChooser<aiQuatKey> {
  180. void operator()(aiQuatKey &min, aiQuatKey &max) {
  181. MinMaxChooser<double>()(min.mTime, max.mTime);
  182. MinMaxChooser<aiQuaternion>()(min.mValue, max.mValue);
  183. }
  184. };
  185. template <>
  186. struct MinMaxChooser<aiVertexWeight> {
  187. void operator()(aiVertexWeight &min, aiVertexWeight &max) {
  188. MinMaxChooser<unsigned int>()(min.mVertexId, max.mVertexId);
  189. MinMaxChooser<ai_real>()(min.mWeight, max.mWeight);
  190. }
  191. };
  192. // -------------------------------------------------------------------------------
  193. /** @brief Find the min/max values of an array of Ts
  194. * @param in Input array
  195. * @param size Number of elements to process
  196. * @param[out] min minimum value
  197. * @param[out] max maximum value
  198. */
  199. template <typename T>
  200. inline void ArrayBounds(const T *in, unsigned int size, T &min, T &max) {
  201. MinMaxChooser<T>()(min, max);
  202. for (unsigned int i = 0; i < size; ++i) {
  203. min = std::min(in[i], min);
  204. max = std::max(in[i], max);
  205. }
  206. }
  207. // -------------------------------------------------------------------------------
  208. /** Little helper function to calculate the quadratic difference
  209. * of two colours.
  210. * @param pColor1 First color
  211. * @param pColor2 second color
  212. * @return Quadratic color difference */
  213. inline ai_real GetColorDifference(const aiColor4D &pColor1, const aiColor4D &pColor2) {
  214. const aiColor4D c(pColor1.r - pColor2.r, pColor1.g - pColor2.g, pColor1.b - pColor2.b, pColor1.a - pColor2.a);
  215. return c.r * c.r + c.g * c.g + c.b * c.b + c.a * c.a;
  216. }
  217. // -------------------------------------------------------------------------------
  218. /** @brief Extract single strings from a list of identifiers
  219. * @param in Input string list.
  220. * @param out Receives a list of clean output strings
  221. * @sdee #AI_CONFIG_PP_OG_EXCLUDE_LIST */
  222. void ConvertListToStrings(const std::string &in, std::list<std::string> &out);
  223. // -------------------------------------------------------------------------------
  224. /** @brief Compute the AABB of a mesh after applying a given transform
  225. * @param mesh Input mesh
  226. * @param[out] min Receives minimum transformed vertex
  227. * @param[out] max Receives maximum transformed vertex
  228. * @param m Transformation matrix to be applied */
  229. void FindAABBTransformed(const aiMesh *mesh, aiVector3D &min, aiVector3D &max, const aiMatrix4x4 &m);
  230. // -------------------------------------------------------------------------------
  231. /** @brief Helper function to determine the 'real' center of a mesh
  232. *
  233. * That is the center of its axis-aligned bounding box.
  234. * @param mesh Input mesh
  235. * @param[out] min Minimum vertex of the mesh
  236. * @param[out] max maximum vertex of the mesh
  237. * @param[out] out Center point */
  238. void FindMeshCenter(aiMesh *mesh, aiVector3D &out, aiVector3D &min, aiVector3D &max);
  239. // -------------------------------------------------------------------------------
  240. /** @brief Helper function to determine the 'real' center of a scene
  241. *
  242. * That is the center of its axis-aligned bounding box.
  243. * @param scene Input scene
  244. * @param[out] min Minimum vertex of the scene
  245. * @param[out] max maximum vertex of the scene
  246. * @param[out] out Center point */
  247. void FindSceneCenter(aiScene *scene, aiVector3D &out, aiVector3D &min, aiVector3D &max);
  248. // -------------------------------------------------------------------------------
  249. // Helper function to determine the 'real' center of a mesh after applying a given transform
  250. void FindMeshCenterTransformed(aiMesh *mesh, aiVector3D &out, aiVector3D &min, aiVector3D &max, const aiMatrix4x4 &m);
  251. // -------------------------------------------------------------------------------
  252. // Helper function to determine the 'real' center of a mesh
  253. void FindMeshCenter(aiMesh *mesh, aiVector3D &out);
  254. // -------------------------------------------------------------------------------
  255. // Helper function to determine the 'real' center of a mesh after applying a given transform
  256. void FindMeshCenterTransformed(aiMesh *mesh, aiVector3D &out, const aiMatrix4x4 &m);
  257. // -------------------------------------------------------------------------------
  258. // Compute a good epsilon value for position comparisons on a mesh
  259. ai_real ComputePositionEpsilon(const aiMesh *pMesh);
  260. // -------------------------------------------------------------------------------
  261. // Compute a good epsilon value for position comparisons on a array of meshes
  262. ai_real ComputePositionEpsilon(const aiMesh *const *pMeshes, size_t num);
  263. // -------------------------------------------------------------------------------
  264. // Compute an unique value for the vertex format of a mesh
  265. unsigned int GetMeshVFormatUnique(const aiMesh *pcMesh);
  266. // defs for ComputeVertexBoneWeightTable()
  267. typedef std::pair<unsigned int, float> PerVertexWeight;
  268. typedef std::vector<PerVertexWeight> VertexWeightTable;
  269. // -------------------------------------------------------------------------------
  270. // Compute a per-vertex bone weight table
  271. VertexWeightTable *ComputeVertexBoneWeightTable(const aiMesh *pMesh);
  272. // -------------------------------------------------------------------------------
  273. // Get a string for a given aiTextureMapping
  274. const char *MappingTypeToString(aiTextureMapping in);
  275. // flags for MakeSubmesh()
  276. #define AI_SUBMESH_FLAGS_SANS_BONES 0x1
  277. // -------------------------------------------------------------------------------
  278. // Split a mesh given a list of faces to be contained in the sub mesh
  279. aiMesh *MakeSubmesh(const aiMesh *superMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags);
  280. // -------------------------------------------------------------------------------
  281. // Utility postprocess step to share the spatial sort tree between
  282. // all steps which use it to speedup its computations.
  283. class ComputeSpatialSortProcess : public BaseProcess {
  284. bool IsActive(unsigned int pFlags) const {
  285. return nullptr != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  286. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  287. }
  288. void Execute(aiScene *pScene) {
  289. typedef std::pair<SpatialSort, ai_real> _Type;
  290. ASSIMP_LOG_DEBUG("Generate spatially-sorted vertex cache");
  291. std::vector<_Type> *p = new std::vector<_Type>(pScene->mNumMeshes);
  292. std::vector<_Type>::iterator it = p->begin();
  293. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++it) {
  294. aiMesh *mesh = pScene->mMeshes[i];
  295. _Type &blubb = *it;
  296. blubb.first.Fill(mesh->mVertices, mesh->mNumVertices, sizeof(aiVector3D));
  297. blubb.second = ComputePositionEpsilon(mesh);
  298. }
  299. shared->AddProperty(AI_SPP_SPATIAL_SORT, p);
  300. }
  301. };
  302. // -------------------------------------------------------------------------------
  303. // ... and the same again to cleanup the whole stuff
  304. class DestroySpatialSortProcess : public BaseProcess {
  305. bool IsActive(unsigned int pFlags) const {
  306. return nullptr != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
  307. aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
  308. }
  309. void Execute(aiScene * /*pScene*/) {
  310. shared->RemoveProperty(AI_SPP_SPATIAL_SORT);
  311. }
  312. };
  313. } // namespace Assimp
  314. #endif // !! AI_PROCESS_HELPER_H_INCLUDED