From 2b12fe5c1a37fb3ccf5f2bfb7ed4be1ab9a50ca8 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 29 Sep 2021 12:35:28 +0200 Subject: [PATCH] Mesh: [skip ci] when points lie exactly on a plane it can happen that a plane fit creates U,V vectors with NaN while W is valid --- src/Mod/Mesh/App/Core/Approximation.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Mod/Mesh/App/Core/Approximation.cpp b/src/Mod/Mesh/App/Core/Approximation.cpp index e8aa9e3148..c7d5778f98 100644 --- a/src/Mod/Mesh/App/Core/Approximation.cpp +++ b/src/Mod/Mesh/App/Core/Approximation.cpp @@ -220,12 +220,26 @@ float PlaneFit::Fit() // It may happen that the result have nan values for (int i=0; i<3; i++) { - if (boost::math::isnan(U[i]) || - boost::math::isnan(V[i]) || - boost::math::isnan(W[i])) + if (boost::math::isnan(W[i])) return FLOAT_MAX; } + // In some cases when the points exactly lie on a plane it can happen that + // U or V have nan values but W is valid. + // In this case create an orthonormal basis + bool validUV = true; + for (int i = 0; i < 3; i++) { + if (boost::math::isnan(U[i]) || + boost::math::isnan(V[i])) { + validUV = false; + break; + } + } + + if (!validUV) { + Wm4::Vector3::GenerateOrthonormalBasis(U, V, W); + } + _vDirU.Set(float(U.X()), float(U.Y()), float(U.Z())); _vDirV.Set(float(V.X()), float(V.Y()), float(V.Z())); _vDirW.Set(float(W.X()), float(W.Y()), float(W.Z()));