1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5
6 Copyright (c) 2006-2019, 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
49 #include "GenFaceNormalsProcess.h"
50 #include <assimp/postprocess.h>
51 #include <assimp/scene.h>
52 #include <assimp/DefaultLogger.hpp>
53 #include <assimp/Exceptional.h>
54 #include <assimp/qnan.h>
55
56
57 using namespace Assimp;
58
59 // ------------------------------------------------------------------------------------------------
60 // Constructor to be privately used by Importer
GenFaceNormalsProcess()61 GenFaceNormalsProcess::GenFaceNormalsProcess()
62 {
63 // nothing to do here
64 }
65
66 // ------------------------------------------------------------------------------------------------
67 // Destructor, private as well
~GenFaceNormalsProcess()68 GenFaceNormalsProcess::~GenFaceNormalsProcess()
69 {
70 // nothing to do here
71 }
72
73 // ------------------------------------------------------------------------------------------------
74 // Returns whether the processing step is present in the given flag field.
IsActive(unsigned int pFlags) const75 bool GenFaceNormalsProcess::IsActive( unsigned int pFlags) const {
76 force_ = (pFlags & aiProcess_ForceGenNormals) != 0;
77 return (pFlags & aiProcess_GenNormals) != 0;
78 }
79
80 // ------------------------------------------------------------------------------------------------
81 // Executes the post processing step on the given imported data.
Execute(aiScene * pScene)82 void GenFaceNormalsProcess::Execute( aiScene* pScene) {
83 ASSIMP_LOG_DEBUG("GenFaceNormalsProcess begin");
84
85 if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
86 throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
87 }
88
89 bool bHas = false;
90 for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
91 if(this->GenMeshFaceNormals( pScene->mMeshes[a])) {
92 bHas = true;
93 }
94 }
95 if (bHas) {
96 ASSIMP_LOG_INFO("GenFaceNormalsProcess finished. "
97 "Face normals have been calculated");
98 } else {
99 ASSIMP_LOG_DEBUG("GenFaceNormalsProcess finished. "
100 "Normals are already there");
101 }
102 }
103
104 // ------------------------------------------------------------------------------------------------
105 // Executes the post processing step on the given imported data.
GenMeshFaceNormals(aiMesh * pMesh)106 bool GenFaceNormalsProcess::GenMeshFaceNormals (aiMesh* pMesh)
107 {
108 if (NULL != pMesh->mNormals) {
109 if (force_) delete[] pMesh->mNormals;
110 else return false;
111 }
112
113 // If the mesh consists of lines and/or points but not of
114 // triangles or higher-order polygons the normal vectors
115 // are undefined.
116 if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
117 ASSIMP_LOG_INFO("Normal vectors are undefined for line and point meshes");
118 return false;
119 }
120
121 // allocate an array to hold the output normals
122 pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
123 const float qnan = get_qnan();
124
125 // iterate through all faces and compute per-face normals but store them per-vertex.
126 for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
127 const aiFace& face = pMesh->mFaces[a];
128 if (face.mNumIndices < 3) {
129 // either a point or a line -> no well-defined normal vector
130 for (unsigned int i = 0;i < face.mNumIndices;++i) {
131 pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
132 }
133 continue;
134 }
135
136 const aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
137 const aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
138 const aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices-1]];
139 const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe();
140
141 for (unsigned int i = 0;i < face.mNumIndices;++i) {
142 pMesh->mNormals[face.mIndices[i]] = vNor;
143 }
144 }
145 return true;
146 }
147