vrshoot
view libs/assimp/ProcessHelper.h @ 1:e7ca128b8713
looks nice :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 00:35:22 +0200 |
parents | |
children |
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 #ifndef AI_PROCESS_HELPER_H_INCLUDED
42 #define AI_PROCESS_HELPER_H_INCLUDED
44 #include "assimp/postprocess.h"
46 #include "SpatialSort.h"
47 #include "BaseProcess.h"
48 #include "ParsingUtils.h"
50 // -------------------------------------------------------------------------------
51 // Some extensions to std namespace. Mainly std::min and std::max for all
52 // flat data types in the aiScene. They're used to quickly determine the
53 // min/max bounds of data arrays.
54 #ifdef __cplusplus
55 namespace std {
57 // std::min for aiVector3D
58 template <typename TReal>
59 inline ::aiVector3t<TReal> min (const ::aiVector3t<TReal>& a, const ::aiVector3t<TReal>& b) {
60 return ::aiVector3t<TReal> (min(a.x,b.x),min(a.y,b.y),min(a.z,b.z));
61 }
63 // std::max for aiVector3t<TReal>
64 template <typename TReal>
65 inline ::aiVector3t<TReal> max (const ::aiVector3t<TReal>& a, const ::aiVector3t<TReal>& b) {
66 return ::aiVector3t<TReal> (max(a.x,b.x),max(a.y,b.y),max(a.z,b.z));
67 }
69 // std::min for aiVector2t<TReal>
70 template <typename TReal>
71 inline ::aiVector2t<TReal> min (const ::aiVector2t<TReal>& a, const ::aiVector2t<TReal>& b) {
72 return ::aiVector2t<TReal> (min(a.x,b.x),min(a.y,b.y));
73 }
75 // std::max for aiVector2t<TReal>
76 template <typename TReal>
77 inline ::aiVector2t<TReal> max (const ::aiVector2t<TReal>& a, const ::aiVector2t<TReal>& b) {
78 return ::aiVector2t<TReal> (max(a.x,b.x),max(a.y,b.y));
79 }
81 // std::min for aiColor4D
82 template <typename TReal>
83 inline ::aiColor4t<TReal> min (const ::aiColor4t<TReal>& a, const ::aiColor4t<TReal>& b) {
84 return ::aiColor4t<TReal> (min(a.r,b.r),min(a.g,b.g),min(a.b,b.b),min(a.a,b.a));
85 }
87 // std::max for aiColor4D
88 template <typename TReal>
89 inline ::aiColor4t<TReal> max (const ::aiColor4t<TReal>& a, const ::aiColor4t<TReal>& b) {
90 return ::aiColor4t<TReal> (max(a.r,b.r),max(a.g,b.g),max(a.b,b.b),max(a.a,b.a));
91 }
94 // std::min for aiQuaterniont<TReal>
95 template <typename TReal>
96 inline ::aiQuaterniont<TReal> min (const ::aiQuaterniont<TReal>& a, const ::aiQuaterniont<TReal>& b) {
97 return ::aiQuaterniont<TReal> (min(a.w,b.w),min(a.x,b.x),min(a.y,b.y),min(a.z,b.z));
98 }
100 // std::max for aiQuaterniont<TReal>
101 template <typename TReal>
102 inline ::aiQuaterniont<TReal> max (const ::aiQuaterniont<TReal>& a, const ::aiQuaterniont<TReal>& b) {
103 return ::aiQuaterniont<TReal> (max(a.w,b.w),max(a.x,b.x),max(a.y,b.y),max(a.z,b.z));
104 }
108 // std::min for aiVectorKey
109 inline ::aiVectorKey min (const ::aiVectorKey& a, const ::aiVectorKey& b) {
110 return ::aiVectorKey (min(a.mTime,b.mTime),min(a.mValue,b.mValue));
111 }
113 // std::max for aiVectorKey
114 inline ::aiVectorKey max (const ::aiVectorKey& a, const ::aiVectorKey& b) {
115 return ::aiVectorKey (max(a.mTime,b.mTime),max(a.mValue,b.mValue));
116 }
118 // std::min for aiQuatKey
119 inline ::aiQuatKey min (const ::aiQuatKey& a, const ::aiQuatKey& b) {
120 return ::aiQuatKey (min(a.mTime,b.mTime),min(a.mValue,b.mValue));
121 }
123 // std::max for aiQuatKey
124 inline ::aiQuatKey max (const ::aiQuatKey& a, const ::aiQuatKey& b) {
125 return ::aiQuatKey (max(a.mTime,b.mTime),max(a.mValue,b.mValue));
126 }
128 // std::min for aiVertexWeight
129 inline ::aiVertexWeight min (const ::aiVertexWeight& a, const ::aiVertexWeight& b) {
130 return ::aiVertexWeight (min(a.mVertexId,b.mVertexId),min(a.mWeight,b.mWeight));
131 }
133 // std::max for aiVertexWeight
134 inline ::aiVertexWeight max (const ::aiVertexWeight& a, const ::aiVertexWeight& b) {
135 return ::aiVertexWeight (max(a.mVertexId,b.mVertexId),max(a.mWeight,b.mWeight));
136 }
138 } // end namespace std
139 #endif // !! C++
141 namespace Assimp {
143 // -------------------------------------------------------------------------------
144 // Start points for ArrayBounds<T> for all supported Ts
145 template <typename T>
146 struct MinMaxChooser;
148 template <> struct MinMaxChooser<float> {
149 void operator ()(float& min,float& max) {
150 max = -1e10f;
151 min = 1e10f;
152 }};
153 template <> struct MinMaxChooser<double> {
154 void operator ()(double& min,double& max) {
155 max = -1e10;
156 min = 1e10;
157 }};
158 template <> struct MinMaxChooser<unsigned int> {
159 void operator ()(unsigned int& min,unsigned int& max) {
160 max = 0;
161 min = (1u<<(sizeof(unsigned int)*8-1));
162 }};
164 template <typename T> struct MinMaxChooser< aiVector3t<T> > {
165 void operator ()(aiVector3t<T>& min,aiVector3t<T>& max) {
166 max = aiVector3t<T>(-1e10f,-1e10f,-1e10f);
167 min = aiVector3t<T>( 1e10f, 1e10f, 1e10f);
168 }};
169 template <typename T> struct MinMaxChooser< aiVector2t<T> > {
170 void operator ()(aiVector2t<T>& min,aiVector2t<T>& max) {
171 max = aiVector2t<T>(-1e10f,-1e10f);
172 min = aiVector2t<T>( 1e10f, 1e10f);
173 }};
174 template <typename T> struct MinMaxChooser< aiColor4t<T> > {
175 void operator ()(aiColor4t<T>& min,aiColor4t<T>& max) {
176 max = aiColor4t<T>(-1e10f,-1e10f,-1e10f,-1e10f);
177 min = aiColor4t<T>( 1e10f, 1e10f, 1e10f, 1e10f);
178 }};
180 template <typename T> struct MinMaxChooser< aiQuaterniont<T> > {
181 void operator ()(aiQuaterniont<T>& min,aiQuaterniont<T>& max) {
182 max = aiQuaterniont<T>(-1e10f,-1e10f,-1e10f,-1e10f);
183 min = aiQuaterniont<T>( 1e10f, 1e10f, 1e10f, 1e10f);
184 }};
186 template <> struct MinMaxChooser<aiVectorKey> {
187 void operator ()(aiVectorKey& min,aiVectorKey& max) {
188 MinMaxChooser<double>()(min.mTime,max.mTime);
189 MinMaxChooser<aiVector3D>()(min.mValue,max.mValue);
190 }};
191 template <> struct MinMaxChooser<aiQuatKey> {
192 void operator ()(aiQuatKey& min,aiQuatKey& max) {
193 MinMaxChooser<double>()(min.mTime,max.mTime);
194 MinMaxChooser<aiQuaternion>()(min.mValue,max.mValue);
195 }};
197 template <> struct MinMaxChooser<aiVertexWeight> {
198 void operator ()(aiVertexWeight& min,aiVertexWeight& max) {
199 MinMaxChooser<unsigned int>()(min.mVertexId,max.mVertexId);
200 MinMaxChooser<float>()(min.mWeight,max.mWeight);
201 }};
203 // -------------------------------------------------------------------------------
204 /** @brief Find the min/max values of an array of Ts
205 * @param in Input array
206 * @param size Numebr of elements to process
207 * @param[out] min minimum value
208 * @param[out] max maximum value
209 */
210 template <typename T>
211 inline void ArrayBounds(const T* in, unsigned int size, T& min, T& max)
212 {
213 MinMaxChooser<T> ()(min,max);
214 for (unsigned int i = 0; i < size;++i) {
215 min = std::min(in[i],min);
216 max = std::max(in[i],max);
217 }
218 }
221 // -------------------------------------------------------------------------------
222 /** Little helper function to calculate the quadratic difference
223 * of two colours.
224 * @param pColor1 First color
225 * @param pColor2 second color
226 * @return Quadratic color difference */
227 inline float GetColorDifference( const aiColor4D& pColor1, const aiColor4D& pColor2)
228 {
229 const aiColor4D c (pColor1.r - pColor2.r, pColor1.g - pColor2.g, pColor1.b - pColor2.b, pColor1.a - pColor2.a);
230 return c.r*c.r + c.g*c.g + c.b*c.b + c.a*c.a;
231 }
234 // -------------------------------------------------------------------------------
235 /** @brief Extract single strings from a list of identifiers
236 * @param in Input string list.
237 * @param out Receives a list of clean output strings
238 * @sdee #AI_CONFIG_PP_OG_EXCLUDE_LIST */
239 void ConvertListToStrings(const std::string& in, std::list<std::string>& out);
242 // -------------------------------------------------------------------------------
243 /** @brief Compute the AABB of a mesh after applying a given transform
244 * @param mesh Input mesh
245 * @param[out] min Receives minimum transformed vertex
246 * @param[out] max Receives maximum transformed vertex
247 * @param m Transformation matrix to be applied */
248 void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D& max, const aiMatrix4x4& m);
251 // -------------------------------------------------------------------------------
252 /** @brief Helper function to determine the 'real' center of a mesh
253 *
254 * That is the center of its axis-aligned bounding box.
255 * @param mesh Input mesh
256 * @param[out] min Minimum vertex of the mesh
257 * @param[out] max maximum vertex of the mesh
258 * @param[out] out Center point */
259 void FindMeshCenter (aiMesh* mesh, aiVector3D& out, aiVector3D& min, aiVector3D& max);
262 // -------------------------------------------------------------------------------
263 // Helper function to determine the 'real' center of a mesh after applying a given transform
264 void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out, aiVector3D& min,aiVector3D& max, const aiMatrix4x4& m);
267 // -------------------------------------------------------------------------------
268 // Helper function to determine the 'real' center of a mesh
269 void FindMeshCenter (aiMesh* mesh, aiVector3D& out);
272 // -------------------------------------------------------------------------------
273 // Helper function to determine the 'real' center of a mesh after applying a given transform
274 void FindMeshCenterTransformed (aiMesh* mesh, aiVector3D& out,const aiMatrix4x4& m);
277 // -------------------------------------------------------------------------------
278 // Compute a good epsilon value for position comparisons on a mesh
279 float ComputePositionEpsilon(const aiMesh* pMesh);
282 // -------------------------------------------------------------------------------
283 // Compute a good epsilon value for position comparisons on a array of meshes
284 float ComputePositionEpsilon(const aiMesh* const* pMeshes, size_t num);
287 // -------------------------------------------------------------------------------
288 // Compute an unique value for the vertex format of a mesh
289 unsigned int GetMeshVFormatUnique(const aiMesh* pcMesh);
292 // defs for ComputeVertexBoneWeightTable()
293 typedef std::pair <unsigned int,float> PerVertexWeight;
294 typedef std::vector <PerVertexWeight> VertexWeightTable;
296 // -------------------------------------------------------------------------------
297 // Compute a per-vertex bone weight table
298 VertexWeightTable* ComputeVertexBoneWeightTable(const aiMesh* pMesh);
301 // -------------------------------------------------------------------------------
302 // Get a string for a given aiTextureType
303 const char* TextureTypeToString(aiTextureType in);
306 // -------------------------------------------------------------------------------
307 // Get a string for a given aiTextureMapping
308 const char* MappingTypeToString(aiTextureMapping in);
311 // flags for MakeSubmesh()
312 #define AI_SUBMESH_FLAGS_SANS_BONES 0x1
314 // -------------------------------------------------------------------------------
315 // Split a mesh given a list of faces to be contained in the sub mesh
316 aiMesh* MakeSubmesh(const aiMesh *superMesh, const std::vector<unsigned int> &subMeshFaces, unsigned int subFlags);
318 // -------------------------------------------------------------------------------
319 // Utility postprocess step to share the spatial sort tree between
320 // all steps which use it to speedup its computations.
321 class ComputeSpatialSortProcess : public BaseProcess
322 {
323 bool IsActive( unsigned int pFlags) const
324 {
325 return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
326 aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
327 }
329 void Execute( aiScene* pScene)
330 {
331 typedef std::pair<SpatialSort, float> _Type;
332 DefaultLogger::get()->debug("Generate spatially-sorted vertex cache");
334 std::vector<_Type>* p = new std::vector<_Type>(pScene->mNumMeshes);
335 std::vector<_Type>::iterator it = p->begin();
337 for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++it) {
338 aiMesh* mesh = pScene->mMeshes[i];
339 _Type& blubb = *it;
340 blubb.first.Fill(mesh->mVertices,mesh->mNumVertices,sizeof(aiVector3D));
341 blubb.second = ComputePositionEpsilon(mesh);
342 }
344 shared->AddProperty(AI_SPP_SPATIAL_SORT,p);
345 }
346 };
348 // -------------------------------------------------------------------------------
349 // ... and the same again to cleanup the whole stuff
350 class DestroySpatialSortProcess : public BaseProcess
351 {
352 bool IsActive( unsigned int pFlags) const
353 {
354 return NULL != shared && 0 != (pFlags & (aiProcess_CalcTangentSpace |
355 aiProcess_GenNormals | aiProcess_JoinIdenticalVertices));
356 }
358 void Execute( aiScene* /*pScene*/)
359 {
360 shared->RemoveProperty(AI_SPP_SPATIAL_SORT);
361 }
362 };
366 } // ! namespace Assimp
367 #endif // !! AI_PROCESS_HELPER_H_INCLUDED