1/* 2--------------------------------------------------------------------------- 3Open Asset Import Library (assimp) 4--------------------------------------------------------------------------- 5 6Copyright (c) 2006-2012, assimp team 7 8All rights reserved. 9 10Redistribution and use of this software in source and binary forms, 11with or without modification, are permitted provided that the following 12conditions are met: 13 14* Redistributions of source code must retain the above 15 copyright notice, this list of conditions and the 16 following disclaimer. 17 18* Redistributions in binary form must reproduce the above 19 copyright notice, this list of conditions and the 20 following disclaimer in the documentation and/or other 21 materials provided with the distribution. 22 23* Neither the name of the assimp team, nor the names of its 24 contributors may be used to endorse or promote products 25 derived from this software without specific prior 26 written permission of the assimp team. 27 28THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39--------------------------------------------------------------------------- 40*/ 41 42/** @file Generation of normal vectors basing on smoothing groups */ 43 44#pragma once 45#ifndef AI_SMOOTHINGGROUPS_INL_INCLUDED 46#define AI_SMOOTHINGGROUPS_INL_INCLUDED 47 48#ifdef __GNUC__ 49# pragma GCC system_header 50#endif 51 52#include <assimp/SGSpatialSort.h> 53 54#include <algorithm> 55 56using namespace Assimp; 57 58// ------------------------------------------------------------------------------------------------ 59template <class T> 60void ComputeNormalsWithSmoothingsGroups(MeshWithSmoothingGroups<T>& sMesh) 61{ 62 // First generate face normals 63 sMesh.mNormals.resize(sMesh.mPositions.size(),aiVector3D()); 64 for( unsigned int a = 0; a < sMesh.mFaces.size(); a++) 65 { 66 T& face = sMesh.mFaces[a]; 67 68 aiVector3D* pV1 = &sMesh.mPositions[face.mIndices[0]]; 69 aiVector3D* pV2 = &sMesh.mPositions[face.mIndices[1]]; 70 aiVector3D* pV3 = &sMesh.mPositions[face.mIndices[2]]; 71 72 aiVector3D pDelta1 = *pV2 - *pV1; 73 aiVector3D pDelta2 = *pV3 - *pV1; 74 aiVector3D vNor = pDelta1 ^ pDelta2; 75 76 for (unsigned int c = 0; c < 3;++c) 77 sMesh.mNormals[face.mIndices[c]] = vNor; 78 } 79 80 // calculate the position bounds so we have a reliable epsilon to check position differences against 81 aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f); 82 for( unsigned int a = 0; a < sMesh.mPositions.size(); a++) 83 { 84 minVec.x = std::min( minVec.x, sMesh.mPositions[a].x); 85 minVec.y = std::min( minVec.y, sMesh.mPositions[a].y); 86 minVec.z = std::min( minVec.z, sMesh.mPositions[a].z); 87 maxVec.x = std::max( maxVec.x, sMesh.mPositions[a].x); 88 maxVec.y = std::max( maxVec.y, sMesh.mPositions[a].y); 89 maxVec.z = std::max( maxVec.z, sMesh.mPositions[a].z); 90 } 91 const float posEpsilon = (maxVec - minVec).Length() * 1e-5f; 92 std::vector<aiVector3D> avNormals; 93 avNormals.resize(sMesh.mNormals.size()); 94 95 // now generate the spatial sort tree 96 SGSpatialSort sSort; 97 for( typename std::vector<T>::iterator i = sMesh.mFaces.begin(); 98 i != sMesh.mFaces.end();++i) 99 { 100 for (unsigned int c = 0; c < 3;++c) 101 sSort.Add(sMesh.mPositions[(*i).mIndices[c]],(*i).mIndices[c],(*i).iSmoothGroup); 102 } 103 sSort.Prepare(); 104 105 std::vector<bool> vertexDone(sMesh.mPositions.size(),false); 106 for( typename std::vector<T>::iterator i = sMesh.mFaces.begin(); 107 i != sMesh.mFaces.end();++i) 108 { 109 std::vector<unsigned int> poResult; 110 for (unsigned int c = 0; c < 3;++c) 111 { 112 unsigned int idx = (*i).mIndices[c]; 113 if (vertexDone[idx])continue; 114 115 sSort.FindPositions(sMesh.mPositions[idx],(*i).iSmoothGroup, 116 posEpsilon,poResult); 117 118 aiVector3D vNormals; 119 for (std::vector<unsigned int>::const_iterator 120 a = poResult.begin(); 121 a != poResult.end();++a) 122 { 123 vNormals += sMesh.mNormals[(*a)]; 124 } 125 vNormals.NormalizeSafe(); 126 127 // write back into all affected normals 128 for (std::vector<unsigned int>::const_iterator 129 a = poResult.begin(); 130 a != poResult.end();++a) 131 { 132 idx = *a; 133 avNormals [idx] = vNormals; 134 vertexDone[idx] = true; 135 } 136 } 137 } 138 sMesh.mNormals = avNormals; 139} 140 141#endif // !! AI_SMOOTHINGGROUPS_INL_INCLUDED 142