diff Framework/Toolbox/GeometryToolbox.cpp @ 151:c5044bbfc303 wasm

CoordinateSystem3D::IntersectSegment()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Feb 2018 17:37:03 +0100
parents 62670cc2bb50
children 441cfe8e7440
line wrap: on
line diff
--- a/Framework/Toolbox/GeometryToolbox.cpp	Thu Feb 01 16:07:54 2018 +0100
+++ b/Framework/Toolbox/GeometryToolbox.cpp	Thu Feb 01 17:37:03 2018 +0100
@@ -445,5 +445,42 @@
       r(2,2) = 1;
       return r;
     }    
+
+
+    bool IntersectPlaneAndSegment(Vector& p,
+                                  const Vector& normal,
+                                  double d,
+                                  const Vector& edgeFrom,
+                                  const Vector& edgeTo)
+    {
+      // http://geomalgorithms.com/a05-_intersect-1.html#Line-Plane-Intersection
+
+      // Check for parallel line and plane
+      Vector direction = edgeTo - edgeFrom;
+      double denominator = boost::numeric::ublas::inner_prod(direction, normal);
+
+      if (fabs(denominator) < 100.0 * std::numeric_limits<double>::epsilon())
+      {
+        return false;
+      }
+      else
+      {
+        // Compute intersection
+        double t = -(normal[0] * edgeFrom[0] + 
+                     normal[1] * edgeFrom[1] + 
+                     normal[2] * edgeFrom[2] + d) / denominator;
+
+        if (t >= 0 && t <= 1)
+        {
+          // The intersection lies inside edge segment
+          p = edgeFrom + t * direction;
+          return true;
+        }
+        else
+        {
+          return false;
+        }
+      }
+    }
   }
 }