vrshoot

diff libs/assimp/SmoothingGroups.inl @ 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/SmoothingGroups.inl	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,138 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +
    1.45 +/** @file Generation of normal vectors basing on smoothing groups */
    1.46 +
    1.47 +#ifndef AI_SMOOTHINGGROUPS_INL_INCLUDED
    1.48 +#define AI_SMOOTHINGGROUPS_INL_INCLUDED
    1.49 +
    1.50 +// internal headers
    1.51 +#include "SGSpatialSort.h"
    1.52 +
    1.53 +// CRT header
    1.54 +#include <algorithm>
    1.55 +
    1.56 +using namespace Assimp;
    1.57 +
    1.58 +// ------------------------------------------------------------------------------------------------
    1.59 +template <class T>
    1.60 +void ComputeNormalsWithSmoothingsGroups(MeshWithSmoothingGroups<T>& sMesh)
    1.61 +{
    1.62 +	// First generate face normals
    1.63 +	sMesh.mNormals.resize(sMesh.mPositions.size(),aiVector3D());
    1.64 +	for( unsigned int a = 0; a < sMesh.mFaces.size(); a++)
    1.65 +	{
    1.66 +		T& face = sMesh.mFaces[a];
    1.67 +
    1.68 +		aiVector3D* pV1 = &sMesh.mPositions[face.mIndices[0]];
    1.69 +		aiVector3D* pV2 = &sMesh.mPositions[face.mIndices[1]];
    1.70 +		aiVector3D* pV3 = &sMesh.mPositions[face.mIndices[2]];
    1.71 +
    1.72 +		aiVector3D pDelta1 = *pV2 - *pV1;
    1.73 +		aiVector3D pDelta2 = *pV3 - *pV1;
    1.74 +		aiVector3D vNor = pDelta1 ^ pDelta2;
    1.75 +
    1.76 +		for (unsigned int c = 0; c < 3;++c)
    1.77 +			sMesh.mNormals[face.mIndices[c]] = vNor;
    1.78 +	}
    1.79 +
    1.80 +	// calculate the position bounds so we have a reliable epsilon to check position differences against 
    1.81 +	aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f);
    1.82 +	for( unsigned int a = 0; a < sMesh.mPositions.size(); a++)
    1.83 +	{
    1.84 +		minVec.x = std::min( minVec.x, sMesh.mPositions[a].x);
    1.85 +		minVec.y = std::min( minVec.y, sMesh.mPositions[a].y);
    1.86 +		minVec.z = std::min( minVec.z, sMesh.mPositions[a].z);
    1.87 +		maxVec.x = std::max( maxVec.x, sMesh.mPositions[a].x);
    1.88 +		maxVec.y = std::max( maxVec.y, sMesh.mPositions[a].y);
    1.89 +		maxVec.z = std::max( maxVec.z, sMesh.mPositions[a].z);
    1.90 +	}
    1.91 +	const float posEpsilon = (maxVec - minVec).Length() * 1e-5f;
    1.92 +	std::vector<aiVector3D> avNormals;
    1.93 +	avNormals.resize(sMesh.mNormals.size());
    1.94 +	
    1.95 +	// now generate the spatial sort tree
    1.96 +	SGSpatialSort sSort;
    1.97 +	for( typename std::vector<T>::iterator i =  sMesh.mFaces.begin();
    1.98 +		i != sMesh.mFaces.end();++i)
    1.99 +	{
   1.100 +		for (unsigned int c = 0; c < 3;++c)
   1.101 +			sSort.Add(sMesh.mPositions[(*i).mIndices[c]],(*i).mIndices[c],(*i).iSmoothGroup);
   1.102 +	}
   1.103 +	sSort.Prepare();
   1.104 +
   1.105 +	std::vector<bool> vertexDone(sMesh.mPositions.size(),false);
   1.106 +	for( typename std::vector<T>::iterator i =  sMesh.mFaces.begin();
   1.107 +		i != sMesh.mFaces.end();++i)
   1.108 +	{
   1.109 +		std::vector<unsigned int> poResult;
   1.110 +		for (unsigned int c = 0; c < 3;++c)
   1.111 +		{
   1.112 +			register unsigned int idx = (*i).mIndices[c];
   1.113 +			if (vertexDone[idx])continue;
   1.114 +
   1.115 +			sSort.FindPositions(sMesh.mPositions[idx],(*i).iSmoothGroup,
   1.116 +				posEpsilon,poResult);
   1.117 +
   1.118 +			aiVector3D vNormals;
   1.119 +			for (std::vector<unsigned int>::const_iterator
   1.120 +				a =  poResult.begin();
   1.121 +				a != poResult.end();++a)
   1.122 +			{
   1.123 +				vNormals += sMesh.mNormals[(*a)];
   1.124 +			}
   1.125 +			vNormals.Normalize();
   1.126 +
   1.127 +			// write back into all affected normals
   1.128 +			for (std::vector<unsigned int>::const_iterator
   1.129 +				a =  poResult.begin();
   1.130 +				a != poResult.end();++a)
   1.131 +			{
   1.132 +				idx = *a;
   1.133 +				avNormals [idx] = vNormals;
   1.134 +				vertexDone[idx] = true;
   1.135 +			}
   1.136 +		}
   1.137 +	}
   1.138 +	sMesh.mNormals = avNormals;
   1.139 +}
   1.140 +
   1.141 +#endif // !! AI_SMOOTHINGGROUPS_INL_INCLUDED