You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

235 lines
8.2 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the post processing step to generate face
  35. * normals for all imported faces.
  36. */
  37. #include "AssimpPCH.h"
  38. // internal headers
  39. #include "GenVertexNormalsProcess.h"
  40. #include "ProcessHelper.h"
  41. using namespace Assimp;
  42. // ------------------------------------------------------------------------------------------------
  43. // Constructor to be privately used by Importer
  44. GenVertexNormalsProcess::GenVertexNormalsProcess()
  45. {
  46. this->configMaxAngle = AI_DEG_TO_RAD(175.f);
  47. }
  48. // ------------------------------------------------------------------------------------------------
  49. // Destructor, private as well
  50. GenVertexNormalsProcess::~GenVertexNormalsProcess()
  51. {
  52. // nothing to do here
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the processing step is present in the given flag field.
  56. bool GenVertexNormalsProcess::IsActive( unsigned int pFlags) const
  57. {
  58. return (pFlags & aiProcess_GenSmoothNormals) != 0;
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Executes the post processing step on the given imported data.
  62. void GenVertexNormalsProcess::SetupProperties(const Importer* pImp)
  63. {
  64. // Get the current value of the AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE property
  65. configMaxAngle = pImp->GetPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE,175.f);
  66. configMaxAngle = AI_DEG_TO_RAD(std::max(std::min(configMaxAngle,175.0f),0.0f));
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Executes the post processing step on the given imported data.
  70. void GenVertexNormalsProcess::Execute( aiScene* pScene)
  71. {
  72. DefaultLogger::get()->debug("GenVertexNormalsProcess begin");
  73. if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT)
  74. throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
  75. bool bHas = false;
  76. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  77. {
  78. if(GenMeshVertexNormals( pScene->mMeshes[a],a))
  79. bHas = true;
  80. }
  81. if (bHas) {
  82. DefaultLogger::get()->info("GenVertexNormalsProcess finished. "
  83. "Vertex normals have been calculated");
  84. }
  85. else DefaultLogger::get()->debug("GenVertexNormalsProcess finished. "
  86. "Normals are already there");
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. // Executes the post processing step on the given imported data.
  90. bool GenVertexNormalsProcess::GenMeshVertexNormals (aiMesh* pMesh, unsigned int meshIndex)
  91. {
  92. if (NULL != pMesh->mNormals)
  93. return false;
  94. // If the mesh consists of lines and/or points but not of
  95. // triangles or higher-order polygons the normal vectors
  96. // are undefined.
  97. if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON)))
  98. {
  99. DefaultLogger::get()->info("Normal vectors are undefined for line and point meshes");
  100. return false;
  101. }
  102. // Allocate the array to hold the output normals
  103. const float qnan = std::numeric_limits<float>::quiet_NaN();
  104. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  105. // Compute per-face normals but store them per-vertex
  106. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  107. {
  108. const aiFace& face = pMesh->mFaces[a];
  109. if (face.mNumIndices < 3)
  110. {
  111. // either a point or a line -> no normal vector
  112. for (unsigned int i = 0;i < face.mNumIndices;++i) {
  113. pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
  114. }
  115. continue;
  116. }
  117. const aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
  118. const aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
  119. const aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices-1]];
  120. const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1));
  121. for (unsigned int i = 0;i < face.mNumIndices;++i) {
  122. pMesh->mNormals[face.mIndices[i]] = vNor;
  123. }
  124. }
  125. // Set up a SpatialSort to quickly find all vertices close to a given position
  126. // check whether we can reuse the SpatialSort of a previous step.
  127. SpatialSort* vertexFinder = NULL;
  128. SpatialSort _vertexFinder;
  129. float posEpsilon = 1e-5f;
  130. if (shared) {
  131. std::vector<std::pair<SpatialSort,float> >* avf;
  132. shared->GetProperty(AI_SPP_SPATIAL_SORT,avf);
  133. if (avf)
  134. {
  135. std::pair<SpatialSort,float>& blubb = avf->operator [] (meshIndex);
  136. vertexFinder = &blubb.first;
  137. posEpsilon = blubb.second;
  138. }
  139. }
  140. if (!vertexFinder) {
  141. _vertexFinder.Fill(pMesh->mVertices, pMesh->mNumVertices, sizeof( aiVector3D));
  142. vertexFinder = &_vertexFinder;
  143. posEpsilon = ComputePositionEpsilon(pMesh);
  144. }
  145. std::vector<unsigned int> verticesFound;
  146. aiVector3D* pcNew = new aiVector3D[pMesh->mNumVertices];
  147. if (configMaxAngle >= AI_DEG_TO_RAD( 175.f )) {
  148. // There is no angle limit. Thus all vertices with positions close
  149. // to each other will receive the same vertex normal. This allows us
  150. // to optimize the whole algorithm a little bit ...
  151. std::vector<bool> abHad(pMesh->mNumVertices,false);
  152. for (unsigned int i = 0; i < pMesh->mNumVertices;++i) {
  153. if (abHad[i]) {
  154. continue;
  155. }
  156. // Get all vertices that share this one ...
  157. vertexFinder->FindPositions( pMesh->mVertices[i], posEpsilon, verticesFound);
  158. aiVector3D pcNor;
  159. for (unsigned int a = 0; a < verticesFound.size(); ++a) {
  160. const aiVector3D& v = pMesh->mNormals[verticesFound[a]];
  161. if (is_not_qnan(v.x))pcNor += v;
  162. }
  163. pcNor.Normalize();
  164. // Write the smoothed normal back to all affected normals
  165. for (unsigned int a = 0; a < verticesFound.size(); ++a)
  166. {
  167. register unsigned int vidx = verticesFound[a];
  168. pcNew[vidx] = pcNor;
  169. abHad[vidx] = true;
  170. }
  171. }
  172. }
  173. // Slower code path if a smooth angle is set. There are many ways to achieve
  174. // the effect, this one is the most straightforward one.
  175. else {
  176. const float fLimit = ::cos(configMaxAngle);
  177. for (unsigned int i = 0; i < pMesh->mNumVertices;++i) {
  178. // Get all vertices that share this one ...
  179. vertexFinder->FindPositions( pMesh->mVertices[i] , posEpsilon, verticesFound);
  180. aiVector3D vr = pMesh->mNormals[i];
  181. float vrlen = vr.Length();
  182. aiVector3D pcNor;
  183. for (unsigned int a = 0; a < verticesFound.size(); ++a) {
  184. aiVector3D v = pMesh->mNormals[verticesFound[a]];
  185. // check whether the angle between the two normals is not too large
  186. // HACK: if v.x is qnan the dot product will become qnan, too
  187. // therefore the comparison against fLimit should be false
  188. // in every case.
  189. if (v * vr >= fLimit * vrlen * v.Length())
  190. pcNor += v;
  191. }
  192. pcNew[i] = pcNor.Normalize();
  193. }
  194. }
  195. delete[] pMesh->mNormals;
  196. pMesh->mNormals = pcNew;
  197. return true;
  198. }