Browse Source

2 bugfix in get_smallest_pos_quad_zero

Also optimized control flow here.

Former-commit-id: 9b7a96a25070744c989b044a2d57d7788541eaab
Zhongshi Jiang 8 years ago
parent
commit
77c4a50aa2
1 changed files with 18 additions and 26 deletions
  1. 18 26
      include/igl/flip_avoiding_line_search.cpp

+ 18 - 26
include/igl/flip_avoiding_line_search.cpp

@@ -63,46 +63,38 @@ namespace igl
     IGL_INLINE double get_smallest_pos_quad_zero(double a,double b, double c)
     {
       using namespace std;
-      double t1,t2;
-      if (std::abs(a)>1.0e-10)
+      double t1, t2;
+      if(std::abs(a) > 1.0e-10)
       {
-        double delta_in = pow(b,2) - 4*a*c;
-        if (delta_in < 0)
+        double delta_in = pow(b, 2) - 4 * a * c;
+        if(delta_in <= 0)
         {
           return INFINITY;
         }
+
         double delta = sqrt(delta_in);
-        t1 = (-b + delta)/ (2*a);
-        t2 = (-b - delta)/ (2*a);
-      }
-      else
-      {
-        t1 = t2 = -b/c;
-      }
-      assert (std::isfinite(t1));
-      assert (std::isfinite(t2));
+        t1 = (-b + delta) / (2 * a);
+        t2 = (-b - delta) / (2 * a);
 
-      double tmp_n = min(t1,t2);
-      t1 = max(t1,t2); t2 = tmp_n;
-      if (t1 == t2)
-      {
-        return INFINITY; // means the orientation flips twice = doesn't flip
-      }
-      // return the smallest negative root if it exists, otherwise return infinity
-      if (t1 > 0)
-      {
-        if (t2 > 0)
+        assert (std::isfinite(t1));
+        assert (std::isfinite(t2));
+
+        if(a < 0) std::swap(t1, t2); // make t1 > t2
+        // return the smaller positive root if it exists, otherwise return infinity
+        if(t1 > 0)
         {
-          return t2;
+          return t2 > 0 ? t2 : t1;
         }
         else
         {
-          return t1;
+          return INFINITY;
         }
       }
       else
       {
-        return INFINITY;
+        if(b == 0) return INFINITY; // just to avoid divide-by-zero
+        t1 = -c / b;
+        return t1 > 0 ? t1 : INFINITY;
       }
     }