vrshoot
diff libs/assimp/ProcessHelper.h @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/ProcessHelper.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,367 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +#ifndef AI_PROCESS_HELPER_H_INCLUDED 1.45 +#define AI_PROCESS_HELPER_H_INCLUDED 1.46 + 1.47 +#include "assimp/postprocess.h" 1.48 + 1.49 +#include "SpatialSort.h" 1.50 +#include "BaseProcess.h" 1.51 +#include "ParsingUtils.h" 1.52 + 1.53 +// ------------------------------------------------------------------------------- 1.54 +// Some extensions to std namespace. Mainly std::min and std::max for all 1.55 +// flat data types in the aiScene. They're used to quickly determine the 1.56 +// min/max bounds of data arrays. 1.57 +#ifdef __cplusplus 1.58 +namespace std { 1.59 + 1.60 + // std::min for aiVector3D 1.61 + template <typename TReal> 1.62 + inline ::aiVector3t<TReal> min (const ::aiVector3t<TReal>& a, const ::aiVector3t<TReal>& b) { 1.63 + return ::aiVector3t<TReal> (min(a.x,b.x),min(a.y,b.y),min(a.z,b.z)); 1.64 + } 1.65 + 1.66 + // std::max for aiVector3t<TReal> 1.67 + template <typename TReal> 1.68 + inline ::aiVector3t<TReal> max (const ::aiVector3t<TReal>& a, const ::aiVector3t<TReal>& b) { 1.69 + return ::aiVector3t<TReal> (max(a.x,b.x),max(a.y,b.y),max(a.z,b.z)); 1.70 + } 1.71 + 1.72 + // std::min for aiVector2t<TReal> 1.73 + template <typename TReal> 1.74 + inline ::aiVector2t<TReal> min (const ::aiVector2t<TReal>& a, const ::aiVector2t<TReal>& b) { 1.75 + return ::aiVector2t<TReal> (min(a.x,b.x),min(a.y,b.y)); 1.76 + } 1.77 + 1.78 + // std::max for aiVector2t<TReal> 1.79 + template <typename TReal> 1.80 + inline ::aiVector2t<TReal> max (const ::aiVector2t<TReal>& a, const ::aiVector2t<TReal>& b) { 1.81 + return ::aiVector2t<TReal> (max(a.x,b.x),max(a.y,b.y)); 1.82 + } 1.83 + 1.84 + // std::min for aiColor4D 1.85 + template <typename TReal> 1.86 + inline ::aiColor4t<TReal> min (const ::aiColor4t<TReal>& a, const ::aiColor4t<TReal>& b) { 1.87 + return ::aiColor4t<TReal> (min(a.r,b.r),min(a.g,b.g),min(a.b,b.b),min(a.a,b.a)); 1.88 + } 1.89 + 1.90 + // std::max for aiColor4D 1.91 + template <typename TReal> 1.92 + inline ::aiColor4t<TReal> max (const ::aiColor4t<TReal>& a, const ::aiColor4t<TReal>& b) { 1.93 + return ::aiColor4t<TReal> (max(a.r,b.r),max(a.g,b.g),max(a.b,b.b),max(a.a,b.a)); 1.94 + } 1.95 + 1.96 + 1.97 + // std::min for aiQuaterniont<TReal> 1.98 + template <typename TReal> 1.99 + inline ::aiQuaterniont<TReal> min (const ::aiQuaterniont<TReal>& a, const ::aiQuaterniont<TReal>& b) { 1.100 + return ::aiQuaterniont<TReal> (min(a.w,b.w),min(a.x,b.x),min(a.y,b.y),min(a.z,b.z)); 1.101 + } 1.102 + 1.103 + // std::max for aiQuaterniont<TReal> 1.104 + template <typename TReal> 1.105 + inline ::aiQuaterniont<TReal> max (const ::aiQuaterniont<TReal>& a, const ::aiQuaterniont<TReal>& b) { 1.106 + return ::aiQuaterniont<TReal> (max(a.w,b.w),max(a.x,b.x),max(a.y,b.y),max(a.z,b.z)); 1.107 + } 1.108 + 1.109 + 1.110 + 1.111 + // std::min for aiVectorKey 1.112 + inline ::aiVectorKey min (const ::aiVectorKey& a, const ::aiVectorKey& b) { 1.113 + return ::aiVectorKey (min(a.mTime,b.mTime),min(a.mValue,b.mValue)); 1.114 + } 1.115 + 1.116 + // std::max for aiVectorKey 1.117 + inline ::aiVectorKey max (const ::aiVectorKey& a, const ::aiVectorKey& b) { 1.118 + return ::aiVectorKey (max(a.mTime,b.mTime),max(a.mValue,b.mValue)); 1.119 + } 1.120 + 1.121 + // std::min for aiQuatKey 1.122 + inline ::aiQuatKey min (const ::aiQuatKey& a, const ::aiQuatKey& b) { 1.123 + return ::aiQuatKey (min(a.mTime,b.mTime),min(a.mValue,b.mValue)); 1.124 + } 1.125 + 1.126 + // std::max for aiQuatKey 1.127 + inline ::aiQuatKey max (const ::aiQuatKey& a, const ::aiQuatKey& b) { 1.128 + return ::aiQuatKey (max(a.mTime,b.mTime),max(a.mValue,b.mValue)); 1.129 + } 1.130 + 1.131 + // std::min for aiVertexWeight 1.132 + inline ::aiVertexWeight min (const ::aiVertexWeight& a, const ::aiVertexWeight& b) { 1.133 + return ::aiVertexWeight (min(a.mVertexId,b.mVertexId),min(a.mWeight,b.mWeight)); 1.134 + } 1.135 + 1.136 + // std::max for aiVertexWeight 1.137 + inline ::aiVertexWeight max (const ::aiVertexWeight& a, const ::aiVertexWeight& b) { 1.138 + return ::aiVertexWeight (max(a.mVertexId,b.mVertexId),max(a.mWeight,b.mWeight)); 1.139 + } 1.140 + 1.141 +} // end namespace std 1.142 +#endif // !! C++ 1.143 + 1.144 +namespace Assimp { 1.145 + 1.146 +// ------------------------------------------------------------------------------- 1.147 +// Start points for ArrayBounds<T> for all supported Ts 1.148 +template <typename T> 1.149 +struct MinMaxChooser; 1.150 + 1.151 +template <> struct MinMaxChooser<float> { 1.152 + void operator ()(float& min,float& max) { 1.153 + max = -1e10f; 1.154 + min = 1e10f; 1.155 +}}; 1.156 +template <> struct MinMaxChooser<double> { 1.157 + void operator ()(double& min,double& max) { 1.158 + max = -1e10; 1.159 + min = 1e10; 1.160 +}}; 1.161 +template <> struct MinMaxChooser<unsigned int> { 1.162 + void operator ()(unsigned int& min,unsigned int& max) { 1.163 + max = 0; 1.164 + min = (1u<<(sizeof(unsigned int)*8-1)); 1.165 +}}; 1.166 + 1.167 +template <typename T> struct MinMaxChooser< aiVector3t<T> > { 1.168 + void operator ()(aiVector3t<T>& min,aiVector3t<T>& max) { 1.169 + max = aiVector3t<T>(-1e10f,-1e10f,-1e10f); 1.170 + min = aiVector3t<T>( 1e10f, 1e10f, 1e10f); 1.171 +}}; 1.172 +template <typename T> struct MinMaxChooser< aiVector2t<T> > { 1.173 + void operator ()(aiVector2t<T>& min,aiVector2t<T>& max) { 1.174 + max = aiVector2t<T>(-1e10f,-1e10f); 1.175 + min = aiVector2t<T>( 1e10f, 1e10f); 1.176 + }}; 1.177 +template <typename T> struct MinMaxChooser< aiColor4t<T> > { 1.178 + void operator ()(aiColor4t<T>& min,aiColor4t<T>& max) { 1.179 + max = aiColor4t<T>(-1e10f,-1e10f,-1e10f,-1e10f); 1.180 + min = aiColor4t<T>( 1e10f, 1e10f, 1e10f, 1e10f); 1.181 +}}; 1.182 + 1.183 +template <typename T> struct MinMaxChooser< aiQuaterniont<T> > { 1.184 + void operator ()(aiQuaterniont<T>& min,aiQuaterniont<T>& max) { 1.185 + max = aiQuaterniont<T>(-1e10f,-1e10f,-1e10f,-1e10f); 1.186 + min = aiQuaterniont<T>( 1e10f, 1e10f, 1e10f, 1e10f); 1.187 +}}; 1.188 + 1.189 +template <> struct MinMaxChooser<aiVectorKey> { 1.190 + void operator ()(aiVectorKey& min,aiVectorKey& max) { 1.191 + MinMaxChooser<double>()(min.mTime,max.mTime); 1.192 + MinMaxChooser<aiVector3D>()(min.mValue,max.mValue); 1.193 +}}; 1.194 +template <> struct MinMaxChooser<aiQuatKey> { 1.195 + void operator ()(aiQuatKey& min,aiQuatKey& max) { 1.196 + MinMaxChooser<double>()(min.mTime,max.mTime); 1.197 + MinMaxChooser<aiQuaternion>()(min.mValue,max.mValue); 1.198 +}}; 1.199 + 1.200 +template <> struct MinMaxChooser<aiVertexWeight> { 1.201 + void operator ()(aiVertexWeight& min,aiVertexWeight& max) { 1.202 + MinMaxChooser<unsigned int>()(min.mVertexId,max.mVertexId); 1.203 + MinMaxChooser<float>()(min.mWeight,max.mWeight); 1.204 +}}; 1.205 + 1.206 +// ------------------------------------------------------------------------------- 1.207 +/** @brief Find the min/max values of an array of Ts 1.208 + * @param in Input array 1.209 + * @param size Numebr of elements to process 1.210 + * @param[out] min minimum value 1.211 + * @param[out] max maximum value 1.212 + */ 1.213 +template <typename T> 1.214 +inline void ArrayBounds(const T* in, unsigned int size, T& min, T& max) 1.215 +{ 1.216 + MinMaxChooser<T> ()(min,max); 1.217 + for (unsigned int i = 0; i < size;++i) { 1.218 + min = std::min(in[i],min); 1.219 + max = std::max(in[i],max); 1.220 + } 1.221 +} 1.222 + 1.223 + 1.224 +// ------------------------------------------------------------------------------- 1.225 +/** Little helper function to calculate the quadratic difference 1.226 + * of two colours. 1.227 + * @param pColor1 First color 1.228 + * @param pColor2 second color 1.229 + * @return Quadratic color difference */ 1.230 +inline float GetColorDifference( const aiColor4D& pColor1, const aiColor4D& pColor2) 1.231 +{ 1.232 + const aiColor4D c (pColor1.r - pColor2.r, pColor1.g - pColor2.g, pColor1.b - pColor2.b, pColor1.a - pColor2.a); 1.233 + return c.r*c.r + c.g*c.g + c.b*c.b + c.a*c.a; 1.234 +} 1.235 + 1.236 + 1.237 +// ------------------------------------------------------------------------------- 1.238 +/** @brief Extract single strings from a list of identifiers 1.239 + * @param in Input string list. 1.240 + * @param out Receives a list of clean output strings 1.241 + * @sdee #AI_CONFIG_PP_OG_EXCLUDE_LIST */ 1.242 +void ConvertListToStrings(const std::string& in, std::list<std::string>& out); 1.243 + 1.244 + 1.245 +// ------------------------------------------------------------------------------- 1.246 +/** @brief Compute the AABB of a mesh after applying a given transform 1.247 + * @param mesh Input mesh 1.248 + * @param[out] min Receives minimum transformed vertex 1.249 + * @param[out] max Receives maximum transformed vertex 1.250 + * @param m Transformation matrix to be applied */ 1.251 +void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D& max, const aiMatrix4x4& m); 1.252 + 1.253 + 1.254 +// ------------------------------------------------------------------------------- 1.255 +/** @brief Helper function to determine the 'real' center of a mesh 1.256 + * 1.257 + * That is the center of its axis-aligned bounding box. 1.258 + * @param mesh Input mesh 1.259 + * @param[out] min Minimum vertex of the mesh 1.260 + * @param[out] max maximum vertex of the mesh 1.261 + * @param[out] out Center point */ 1.262 +void FindMeshCenter (aiMesh* mesh, aiVector3D& out, aiVector3D& min, aiVector3D& max); 1.263 + 1.264 + 1.265 +// ------------------------------------------------------------------------------- 1.266 +// Helper function to determine the 'real' center of a mesh after applying a given transform 1.267 +void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out, aiVector3D& min,aiVector3D& max, const aiMatrix4x4& m); 1.268 + 1.269 + 1.270 +// ------------------------------------------------------------------------------- 1.271 +// Helper function to determine the 'real' center of a mesh 1.272 +void FindMeshCenter (aiMesh* mesh, aiVector3D& out); 1.273 + 1.274 + 1.275 +// ------------------------------------------------------------------------------- 1.276 +// Helper function to determine the 'real' center of a mesh after applying a given transform 1.277 +void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out,const aiMatrix4x4& m); 1.278 + 1.279 + 1.280 +// ------------------------------------------------------------------------------- 1.281 +// Compute a good epsilon value for position comparisons on a mesh 1.282 +float ComputePositionEpsilon(const aiMesh* pMesh); 1.283 + 1.284 + 1.285 +// ------------------------------------------------------------------------------- 1.286 +// Compute a good epsilon value for position comparisons on a array of meshes 1.287 +float ComputePositionEpsilon(const aiMesh* const* pMeshes, size_t num); 1.288 + 1.289 + 1.290 +// ------------------------------------------------------------------------------- 1.291 +// Compute an unique value for the vertex format of a mesh 1.292 +unsigned int GetMeshVFormatUnique(const aiMesh* pcMesh); 1.293 + 1.294 + 1.295 +// defs for ComputeVertexBoneWeightTable() 1.296 +typedef std::pair <unsigned int,float> PerVertexWeight; 1.297 +typedef std::vector <PerVertexWeight> VertexWeightTable; 1.298 + 1.299 +// ------------------------------------------------------------------------------- 1.300 +// Compute a per-vertex bone weight table 1.301 +VertexWeightTable* ComputeVertexBoneWeightTable(const aiMesh* pMesh); 1.302 + 1.303 + 1.304 +// ------------------------------------------------------------------------------- 1.305 +// Get a string for a given aiTextureType 1.306 +const char* TextureTypeToString(aiTextureType in); 1.307 + 1.308 + 1.309 +// ------------------------------------------------------------------------------- 1.310 +// Get a string for a given aiTextureMapping 1.311 +const char* MappingTypeToString(aiTextureMapping in); 1.312 + 1.313 + 1.314 +// flags for MakeSubmesh() 1.315 +#define AI_SUBMESH_FLAGS_SANS_BONES 0x1 1.316 + 1.317 +// ------------------------------------------------------------------------------- 1.318 +// Split a mesh given a list of faces to be contained in the sub mesh 1.319 +aiMesh* MakeSubmesh(const aiMesh *superMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags); 1.320 + 1.321 +// ------------------------------------------------------------------------------- 1.322 +// Utility postprocess step to share the spatial sort tree between 1.323 +// all steps which use it to speedup its computations. 1.324 +class ComputeSpatialSortProcess : public BaseProcess 1.325 +{ 1.326 + bool IsActive( unsigned int pFlags) const 1.327 + { 1.328 + return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace | 1.329 + aiProcess_GenNormals | aiProcess_JoinIdenticalVertices)); 1.330 + } 1.331 + 1.332 + void Execute( aiScene* pScene) 1.333 + { 1.334 + typedef std::pair<SpatialSort, float> _Type; 1.335 + DefaultLogger::get()->debug("Generate spatially-sorted vertex cache"); 1.336 + 1.337 + std::vector<_Type>* p = new std::vector<_Type>(pScene->mNumMeshes); 1.338 + std::vector<_Type>::iterator it = p->begin(); 1.339 + 1.340 + for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++it) { 1.341 + aiMesh* mesh = pScene->mMeshes[i]; 1.342 + _Type& blubb = *it; 1.343 + blubb.first.Fill(mesh->mVertices,mesh->mNumVertices,sizeof(aiVector3D)); 1.344 + blubb.second = ComputePositionEpsilon(mesh); 1.345 + } 1.346 + 1.347 + shared->AddProperty(AI_SPP_SPATIAL_SORT,p); 1.348 + } 1.349 +}; 1.350 + 1.351 +// ------------------------------------------------------------------------------- 1.352 +// ... and the same again to cleanup the whole stuff 1.353 +class DestroySpatialSortProcess : public BaseProcess 1.354 +{ 1.355 + bool IsActive( unsigned int pFlags) const 1.356 + { 1.357 + return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace | 1.358 + aiProcess_GenNormals | aiProcess_JoinIdenticalVertices)); 1.359 + } 1.360 + 1.361 + void Execute( aiScene* /*pScene*/) 1.362 + { 1.363 + shared->RemoveProperty(AI_SPP_SPATIAL_SORT); 1.364 + } 1.365 +}; 1.366 + 1.367 + 1.368 + 1.369 +} // ! namespace Assimp 1.370 +#endif // !! AI_PROCESS_HELPER_H_INCLUDED