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