Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

368 строки
13 KiB

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