1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5
6 Copyright (c) 2006-2021, assimp team
7
8
9
10 All rights reserved.
11
12 Redistribution and use of this software in source and binary forms,
13 with or without modification, are permitted provided that the following
14 conditions are met:
15
16 * Redistributions of source code must retain the above
17 copyright notice, this list of conditions and the
18 following disclaimer.
19
20 * Redistributions in binary form must reproduce the above
21 copyright notice, this list of conditions and the
22 following disclaimer in the documentation and/or other
23 materials provided with the distribution.
24
25 * Neither the name of the assimp team, nor the names of its
26 contributors may be used to endorse or promote products
27 derived from this software without specific prior
28 written permission of the assimp team.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ---------------------------------------------------------------------------
42 */
43
44 /** @file Implementation of the post processing step to generate face
45 * normals for all imported faces.
46 */
47
48 #include "GenFaceNormalsProcess.h"
49 #include <assimp/Exceptional.h>
50 #include <assimp/postprocess.h>
51 #include <assimp/qnan.h>
52 #include <assimp/scene.h>
53 #include <assimp/DefaultLogger.hpp>
54
55 using namespace Assimp;
56
57 // ------------------------------------------------------------------------------------------------
58 // Constructor to be privately used by Importer
GenFaceNormalsProcess()59 GenFaceNormalsProcess::GenFaceNormalsProcess() {
60 // nothing to do here
61 }
62
63 // ------------------------------------------------------------------------------------------------
64 // Destructor, private as well
~GenFaceNormalsProcess()65 GenFaceNormalsProcess::~GenFaceNormalsProcess() {
66 // nothing to do here
67 }
68
69 // ------------------------------------------------------------------------------------------------
70 // Returns whether the processing step is present in the given flag field.
IsActive(unsigned int pFlags) const71 bool GenFaceNormalsProcess::IsActive(unsigned int pFlags) const {
72 force_ = (pFlags & aiProcess_ForceGenNormals) != 0;
73 flippedWindingOrder_ = (pFlags & aiProcess_FlipWindingOrder) != 0;
74 return (pFlags & aiProcess_GenNormals) != 0;
75 }
76
77 // ------------------------------------------------------------------------------------------------
78 // Executes the post processing step on the given imported data.
Execute(aiScene * pScene)79 void GenFaceNormalsProcess::Execute(aiScene *pScene) {
80 ASSIMP_LOG_DEBUG("GenFaceNormalsProcess begin");
81
82 if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
83 throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
84 }
85
86 bool bHas = false;
87 for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
88 if (this->GenMeshFaceNormals(pScene->mMeshes[a])) {
89 bHas = true;
90 }
91 }
92 if (bHas) {
93 ASSIMP_LOG_INFO("GenFaceNormalsProcess finished. "
94 "Face normals have been calculated");
95 } else {
96 ASSIMP_LOG_DEBUG("GenFaceNormalsProcess finished. "
97 "Normals are already there");
98 }
99 }
100
101 // ------------------------------------------------------------------------------------------------
102 // Executes the post processing step on the given imported data.
GenMeshFaceNormals(aiMesh * pMesh)103 bool GenFaceNormalsProcess::GenMeshFaceNormals(aiMesh *pMesh) {
104 if (nullptr != pMesh->mNormals) {
105 if (force_) {
106 delete[] pMesh->mNormals;
107 } else {
108 return false;
109 }
110 }
111
112 // If the mesh consists of lines and/or points but not of
113 // triangles or higher-order polygons the normal vectors
114 // are undefined.
115 if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
116 ASSIMP_LOG_INFO("Normal vectors are undefined for line and point meshes");
117 return false;
118 }
119
120 // allocate an array to hold the output normals
121 pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
122 const float qnan = get_qnan();
123
124 // iterate through all faces and compute per-face normals but store them per-vertex.
125 for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
126 const aiFace &face = pMesh->mFaces[a];
127 if (face.mNumIndices < 3) {
128 // either a point or a line -> no well-defined normal vector
129 for (unsigned int i = 0; i < face.mNumIndices; ++i) {
130 pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
131 }
132 continue;
133 }
134
135 const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]];
136 const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]];
137 const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]];
138 if (flippedWindingOrder_)
139 std::swap( pV2, pV3 );
140 const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe();
141
142 for (unsigned int i = 0; i < face.mNumIndices; ++i) {
143 pMesh->mNormals[face.mIndices[i]] = vNor;
144 }
145 }
146 return true;
147 }
148