ProcessHelper.h 14 KB

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