From ee18bc05ae54bd4b9a959ef544181dd8f597d955 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 13 May 2016 23:59:07 +0200 Subject: pose-widget: avoid too small denominator in perpendicular alignment With a very small denominator in barycentric coords formula we get a dissociated octopus that took PCP and LSD in one sitting. A dead Octopus is just a bunch of blue spheres. IOW, in division with too small a denominator operand the results are numerically unstable. This is done in the constructor in a convoluted way as to avoid branching during calculating for each pixel. Issue: #356 Reported-by: @MathijsG --- pose-widget/glwidget.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'pose-widget') diff --git a/pose-widget/glwidget.cpp b/pose-widget/glwidget.cpp index a2ed422e..f85e2ae1 100644 --- a/pose-widget/glwidget.cpp +++ b/pose-widget/glwidget.cpp @@ -11,7 +11,6 @@ #include #include -//#include GLWidget::GLWidget(QWidget *parent) : QWidget(parent) { @@ -68,7 +67,17 @@ public: dot00 = v0.dot(v0); dot01 = v0.dot(v1); dot11 = v1.dot(v1); - invDenom = 1 / (dot00 * dot11 - dot01 * dot01); + const num denom = dot00 * dot11 - dot01 * dot01; + if (std::fabs(denom) < 1e-1f) + { + // for perpendicular plane, ensure u and v don't come out right + // this is done here to avoid branching below, in a hot loop + invDenom = -1; + dot00 = dot01 = dot11 = 1; + v0 = v1 = vec2(1, 1); + } + else + invDenom = 1 / denom; } bool barycentric_coords(const vec2& px, vec2& uv) const { -- cgit v1.2.3