From b6f14eb917833e9bd2329262d682d63a89530c7f Mon Sep 17 00:00:00 2001 From: Vitaly Ovchinnikov Date: Mon, 14 Dec 2015 15:31:49 +1300 Subject: [PATCH] Normalize() is replaced with NormalizeSafe() to prevent invalid vectors generation --- code/GenVertexNormalsProcess.cpp | 4 ++-- include/assimp/vector3.h | 2 ++ include/assimp/vector3.inl | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/code/GenVertexNormalsProcess.cpp b/code/GenVertexNormalsProcess.cpp index 1d50e2a1f..87ae253b9 100644 --- a/code/GenVertexNormalsProcess.cpp +++ b/code/GenVertexNormalsProcess.cpp @@ -192,7 +192,7 @@ bool GenVertexNormalsProcess::GenMeshVertexNormals (aiMesh* pMesh, unsigned int const aiVector3D& v = pMesh->mNormals[verticesFound[a]]; if (is_not_qnan(v.x))pcNor += v; } - pcNor.Normalize(); + pcNor.NormalizeSafe(); // Write the smoothed normal back to all affected normals for (unsigned int a = 0; a < verticesFound.size(); ++a) @@ -225,7 +225,7 @@ bool GenVertexNormalsProcess::GenMeshVertexNormals (aiMesh* pMesh, unsigned int if (v * vr >= fLimit * vrlen * v.Length()) pcNor += v; } - pcNew[i] = pcNor.Normalize(); + pcNew[i] = pcNor.NormalizeSafe(); } } diff --git a/include/assimp/vector3.h b/include/assimp/vector3.h index b19de34c8..482a544c3 100644 --- a/include/assimp/vector3.h +++ b/include/assimp/vector3.h @@ -116,6 +116,8 @@ public: /** @brief Normalize the vector */ aiVector3t& Normalize(); + /** @brief Normalize the vector with extra check for zero vectors */ + aiVector3t& NormalizeSafe(); /** @brief Componentwise multiplication of two vectors * diff --git a/include/assimp/vector3.inl b/include/assimp/vector3.inl index f06b077ff..b83367d7b 100644 --- a/include/assimp/vector3.inl +++ b/include/assimp/vector3.inl @@ -101,6 +101,14 @@ AI_FORCE_INLINE aiVector3t& aiVector3t::Normalize() { } // ------------------------------------------------------------------------------------------------ template +AI_FORCE_INLINE aiVector3t& aiVector3t::NormalizeSafe() { + TReal len = Length(); + if (len > static_cast(0)) + *this /= Length(); + return *this; +} +// ------------------------------------------------------------------------------------------------ +template AI_FORCE_INLINE const aiVector3t& aiVector3t::operator += (const aiVector3t& o) { x += o.x; y += o.y; z += o.z; return *this; }