GenFaceNormalsProcess.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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. /**
  35. * @file Implementation of the post-processing step to generate face
  36. * normals for all imported faces.
  37. */
  38. #include "GenFaceNormalsProcess.h"
  39. #include <assimp/Exceptional.h>
  40. #include <assimp/postprocess.h>
  41. #include <assimp/qnan.h>
  42. #include <assimp/scene.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. using namespace Assimp;
  45. // ------------------------------------------------------------------------------------------------
  46. // Returns whether the processing step is in the given flag field.
  47. bool GenFaceNormalsProcess::IsActive(unsigned int pFlags) const {
  48. force_ = (pFlags & aiProcess_ForceGenNormals) != 0;
  49. flippedWindingOrder_ = (pFlags & aiProcess_FlipWindingOrder) != 0;
  50. leftHanded_ = (pFlags & aiProcess_MakeLeftHanded) != 0;
  51. return (pFlags & aiProcess_GenNormals) != 0;
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Executes the post-processing step on the given imported data.
  55. void GenFaceNormalsProcess::Execute(aiScene *pScene) {
  56. ASSIMP_LOG_DEBUG("GenFaceNormalsProcess begin");
  57. if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
  58. throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
  59. }
  60. bool bHas = false;
  61. for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  62. if (this->GenMeshFaceNormals(pScene->mMeshes[a])) {
  63. bHas = true;
  64. }
  65. }
  66. if (bHas) {
  67. ASSIMP_LOG_INFO("GenFaceNormalsProcess finished. "
  68. "Face normals have been calculated");
  69. } else {
  70. ASSIMP_LOG_DEBUG("GenFaceNormalsProcess finished. "
  71. "Normals are already there");
  72. }
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Executes the post-processing step on the given imported data.
  76. bool GenFaceNormalsProcess::GenMeshFaceNormals(aiMesh *pMesh) {
  77. if (nullptr != pMesh->mNormals) {
  78. if (force_) {
  79. delete[] pMesh->mNormals;
  80. } else {
  81. return false;
  82. }
  83. }
  84. // If the mesh consists of lines and/or points but not of
  85. // triangles or higher-order polygons the normal vectors
  86. // are undefined.
  87. if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
  88. ASSIMP_LOG_INFO("Normal vectors are undefined for line and point meshes");
  89. return false;
  90. }
  91. // allocate an array to hold the output normals
  92. std::vector<aiVector3D> normals;
  93. normals.resize(pMesh->mNumVertices);
  94. // mask to indicate if a vertex was already referenced and needs to be duplicated
  95. std::vector<bool> alreadyReferenced;
  96. alreadyReferenced.resize(pMesh->mNumVertices, false);
  97. std::vector<aiVector3D> duplicatedVertices;
  98. auto storeNormalSplitVertex = [&](unsigned int index, const aiVector3D& normal) {
  99. if (!alreadyReferenced[index]) {
  100. normals[index] = normal;
  101. alreadyReferenced[index] = true;
  102. } else {
  103. duplicatedVertices.push_back(pMesh->mVertices[index]);
  104. normals.push_back(normal);
  105. index = pMesh->mNumVertices + static_cast<unsigned int>(duplicatedVertices.size() - 1);
  106. }
  107. return index;
  108. };
  109. const aiVector3D undefinedNormal = aiVector3D(get_qnan());
  110. // iterate through all faces and compute per-face normals but store them per-vertex.
  111. for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
  112. const aiFace &face = pMesh->mFaces[a];
  113. if (face.mNumIndices < 3) {
  114. // either a point or a line -> no well-defined normal vector
  115. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  116. face.mIndices[i] = storeNormalSplitVertex(face.mIndices[i], undefinedNormal);
  117. }
  118. continue;
  119. }
  120. const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]];
  121. const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]];
  122. const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]];
  123. // Boolean XOR - if either but not both of these flags are set, then the winding order has
  124. // changed and the cross-product to calculate the normal needs to be reversed
  125. if (flippedWindingOrder_ != leftHanded_)
  126. std::swap(pV2, pV3);
  127. const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe();
  128. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  129. face.mIndices[i] = storeNormalSplitVertex(face.mIndices[i], vNor);
  130. }
  131. }
  132. // store normals (and additional vertices) back into the mesh
  133. if (!duplicatedVertices.empty()) {
  134. const aiVector3D * oldVertices = pMesh->mVertices;
  135. auto oldNumVertices = pMesh->mNumVertices;
  136. pMesh->mNumVertices += static_cast<unsigned int>(duplicatedVertices.size());
  137. pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
  138. memcpy(pMesh->mVertices, oldVertices, oldNumVertices * sizeof(aiVector3D));
  139. memcpy(pMesh->mVertices + oldNumVertices, duplicatedVertices.data(), duplicatedVertices.size() * sizeof(aiVector3D));
  140. delete[] oldVertices;
  141. }
  142. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  143. memcpy(pMesh->mNormals, normals.data(), normals.size() * sizeof(aiVector3D));
  144. return true;
  145. }