GenVertexNormalsProcess.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. // internal headers
  38. #include "GenVertexNormalsProcess.h"
  39. #include "ProcessHelper.h"
  40. #include <assimp/Exceptional.h>
  41. #include <assimp/qnan.h>
  42. using namespace Assimp;
  43. // ------------------------------------------------------------------------------------------------
  44. // Constructor to be privately used by Importer
  45. GenVertexNormalsProcess::GenVertexNormalsProcess() :
  46. configMaxAngle(AI_DEG_TO_RAD(175.f)) {
  47. // empty
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Destructor, private as well
  51. GenVertexNormalsProcess::~GenVertexNormalsProcess() {
  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. force_ = (pFlags & aiProcess_ForceGenNormals) != 0;
  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. // Get the current value of the AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE property
  64. configMaxAngle = pImp->GetPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, (ai_real)175.0);
  65. configMaxAngle = AI_DEG_TO_RAD(std::max(std::min(configMaxAngle, (ai_real)175.0), (ai_real)0.0));
  66. }
  67. // ------------------------------------------------------------------------------------------------
  68. // Executes the post processing step on the given imported data.
  69. void GenVertexNormalsProcess::Execute(aiScene *pScene) {
  70. ASSIMP_LOG_DEBUG("GenVertexNormalsProcess begin");
  71. if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
  72. throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
  73. }
  74. bool bHas = false;
  75. for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
  76. if (GenMeshVertexNormals(pScene->mMeshes[a], a))
  77. bHas = true;
  78. }
  79. if (bHas) {
  80. ASSIMP_LOG_INFO("GenVertexNormalsProcess finished. "
  81. "Vertex normals have been calculated");
  82. } else {
  83. ASSIMP_LOG_DEBUG("GenVertexNormalsProcess finished. "
  84. "Normals are already there");
  85. }
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // Executes the post processing step on the given imported data.
  89. bool GenVertexNormalsProcess::GenMeshVertexNormals(aiMesh *pMesh, unsigned int meshIndex) {
  90. if (nullptr != pMesh->mNormals) {
  91. if (force_)
  92. delete[] pMesh->mNormals;
  93. else
  94. return false;
  95. }
  96. // If the mesh consists of lines and/or points but not of
  97. // triangles or higher-order polygons the normal vectors
  98. // are undefined.
  99. if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
  100. ASSIMP_LOG_INFO("Normal vectors are undefined for line and point meshes");
  101. return false;
  102. }
  103. // Allocate the array to hold the output normals
  104. const float qnan = std::numeric_limits<ai_real>::quiet_NaN();
  105. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  106. // Compute per-face normals but store them per-vertex
  107. for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  108. const aiFace &face = pMesh->mFaces[a];
  109. if (face.mNumIndices < 3) {
  110. // either a point or a line -> no normal vector
  111. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  112. pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
  113. }
  114. continue;
  115. }
  116. const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]];
  117. const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]];
  118. const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]];
  119. const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe();
  120. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  121. pMesh->mNormals[face.mIndices[i]] = vNor;
  122. }
  123. }
  124. // Set up a SpatialSort to quickly find all vertices close to a given position
  125. // check whether we can reuse the SpatialSort of a previous step.
  126. SpatialSort *vertexFinder = nullptr;
  127. SpatialSort _vertexFinder;
  128. ai_real posEpsilon = ai_real(1e-5);
  129. if (shared) {
  130. std::vector<std::pair<SpatialSort, ai_real>> *avf;
  131. shared->GetProperty(AI_SPP_SPATIAL_SORT, avf);
  132. if (avf) {
  133. std::pair<SpatialSort, ai_real> &blubb = avf->operator[](meshIndex);
  134. vertexFinder = &blubb.first;
  135. posEpsilon = blubb.second;
  136. }
  137. }
  138. if (!vertexFinder) {
  139. _vertexFinder.Fill(pMesh->mVertices, pMesh->mNumVertices, sizeof(aiVector3D));
  140. vertexFinder = &_vertexFinder;
  141. posEpsilon = ComputePositionEpsilon(pMesh);
  142. }
  143. std::vector<unsigned int> verticesFound;
  144. aiVector3D *pcNew = new aiVector3D[pMesh->mNumVertices];
  145. if (configMaxAngle >= AI_DEG_TO_RAD(175.f)) {
  146. // There is no angle limit. Thus all vertices with positions close
  147. // to each other will receive the same vertex normal. This allows us
  148. // to optimize the whole algorithm a little bit ...
  149. std::vector<bool> abHad(pMesh->mNumVertices, false);
  150. for (unsigned int i = 0; i < pMesh->mNumVertices; ++i) {
  151. if (abHad[i]) {
  152. continue;
  153. }
  154. // Get all vertices that share this one ...
  155. vertexFinder->FindPositions(pMesh->mVertices[i], posEpsilon, verticesFound);
  156. aiVector3D pcNor;
  157. for (unsigned int a = 0; a < verticesFound.size(); ++a) {
  158. const aiVector3D &v = pMesh->mNormals[verticesFound[a]];
  159. if (is_not_qnan(v.x)) pcNor += v;
  160. }
  161. pcNor.NormalizeSafe();
  162. // Write the smoothed normal back to all affected normals
  163. for (unsigned int a = 0; a < verticesFound.size(); ++a) {
  164. unsigned int vidx = verticesFound[a];
  165. pcNew[vidx] = pcNor;
  166. abHad[vidx] = true;
  167. }
  168. }
  169. }
  170. // Slower code path if a smooth angle is set. There are many ways to achieve
  171. // the effect, this one is the most straightforward one.
  172. else {
  173. const ai_real fLimit = std::cos(configMaxAngle);
  174. for (unsigned int i = 0; i < pMesh->mNumVertices; ++i) {
  175. // Get all vertices that share this one ...
  176. vertexFinder->FindPositions(pMesh->mVertices[i], posEpsilon, verticesFound);
  177. aiVector3D vr = pMesh->mNormals[i];
  178. aiVector3D pcNor;
  179. for (unsigned int a = 0; a < verticesFound.size(); ++a) {
  180. aiVector3D v = pMesh->mNormals[verticesFound[a]];
  181. // Check whether the angle between the two normals is not too large.
  182. // Skip the angle check on our own normal to avoid false negatives
  183. // (v*v is not guaranteed to be 1.0 for all unit vectors v)
  184. if (is_not_qnan(v.x) && (verticesFound[a] == i || (v * vr >= fLimit)))
  185. pcNor += v;
  186. }
  187. pcNew[i] = pcNor.NormalizeSafe();
  188. }
  189. }
  190. delete[] pMesh->mNormals;
  191. pMesh->mNormals = pcNew;
  192. return true;
  193. }