# HG changeset patch # User Sebastien Jodogne # Date 1517503023 -3600 # Node ID c5044bbfc3039d47a55ca76a6acdc2347b8ce57e # Parent 62670cc2bb50f95728377f8ecc28b2f52b429a54 CoordinateSystem3D::IntersectSegment() diff -r 62670cc2bb50 -r c5044bbfc303 Framework/Toolbox/CoordinateSystem3D.cpp --- a/Framework/Toolbox/CoordinateSystem3D.cpp Thu Feb 01 16:07:54 2018 +0100 +++ b/Framework/Toolbox/CoordinateSystem3D.cpp Thu Feb 01 17:37:03 2018 +0100 @@ -168,4 +168,24 @@ offsetX = boost::numeric::ublas::inner_prod(axisX_, projection - origin_); offsetY = boost::numeric::ublas::inner_prod(axisY_, projection - origin_); } + + + bool CoordinateSystem3D::IntersectSegment(double& x, + double& y, + const Vector& edgeFrom, + const Vector& edgeTo) const + { + Vector p; + double d = -(normal_[0] * origin_[0] + normal_[1] * origin_[1] + normal_[2] * origin_[2]); + if (GeometryToolbox::IntersectPlaneAndSegment(p, normal_, d, edgeFrom, edgeTo)) + { + ProjectPoint(x, y, p); // TODO could be optimized, as the point + // is on the plane by construction + return true; + } + else + { + return false; + } + } } diff -r 62670cc2bb50 -r c5044bbfc303 Framework/Toolbox/CoordinateSystem3D.h --- a/Framework/Toolbox/CoordinateSystem3D.h Thu Feb 01 16:07:54 2018 +0100 +++ b/Framework/Toolbox/CoordinateSystem3D.h Thu Feb 01 17:37:03 2018 +0100 @@ -91,5 +91,10 @@ void ProjectPoint(double& offsetX, double& offsetY, const Vector& point) const; + + bool IntersectSegment(double& x, + double& y, + const Vector& edgeFrom, + const Vector& edgeTo) const; }; } diff -r 62670cc2bb50 -r c5044bbfc303 Framework/Toolbox/GeometryToolbox.cpp --- 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::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; + } + } + } } } diff -r 62670cc2bb50 -r c5044bbfc303 Framework/Toolbox/GeometryToolbox.h --- a/Framework/Toolbox/GeometryToolbox.h Thu Feb 01 16:07:54 2018 +0100 +++ b/Framework/Toolbox/GeometryToolbox.h Thu Feb 01 17:37:03 2018 +0100 @@ -130,6 +130,12 @@ Matrix CreateRotationMatrixAlongZ(double a); + bool IntersectPlaneAndSegment(Vector& p, + const Vector& normal, + double d, + const Vector& edgeFrom, + const Vector& edgeTo); + inline float ComputeBilinearInterpolationInternal(float x, float y, float f00, // source(x, y) diff -r 62670cc2bb50 -r c5044bbfc303 Framework/Toolbox/OrientedBoundingBox.cpp --- a/Framework/Toolbox/OrientedBoundingBox.cpp Thu Feb 01 16:07:54 2018 +0100 +++ b/Framework/Toolbox/OrientedBoundingBox.cpp Thu Feb 01 17:37:03 2018 +0100 @@ -27,43 +27,6 @@ namespace OrthancStone { - static 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::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; - } - } - } - - OrientedBoundingBox::OrientedBoundingBox(const ImageBuffer3D& image) { unsigned int n = image.GetDepth(); @@ -115,88 +78,100 @@ // bounding box, and check whether they intersect the plane // X-aligned edges - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, - c_ + u_ * hu_ - v_ * hv_ - w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, + c_ + u_ * hu_ - v_ * hv_ - w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ + v_ * hv_ - w_ * hw_, - c_ + u_ * hu_ + v_ * hv_ - w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ + v_ * hv_ - w_ * hw_, + c_ + u_ * hu_ + v_ * hv_ - w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ - v_ * hv_ + w_ * hw_, - c_ + u_ * hu_ - v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ - v_ * hv_ + w_ * hw_, + c_ + u_ * hu_ - v_ * hv_ + w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ + v_ * hv_ + w_ * hw_, - c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ + v_ * hv_ + w_ * hw_, + c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) { points.push_back(p); } // Y-aligned edges - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, - c_ - u_ * hu_ + v_ * hv_ - w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, + c_ - u_ * hu_ + v_ * hv_ - w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ + u_ * hu_ - v_ * hv_ - w_ * hw_, - c_ + u_ * hu_ + v_ * hv_ - w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ + u_ * hu_ - v_ * hv_ - w_ * hw_, + c_ + u_ * hu_ + v_ * hv_ - w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ - v_ * hv_ + w_ * hw_, - c_ - u_ * hu_ + v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ - v_ * hv_ + w_ * hw_, + c_ - u_ * hu_ + v_ * hv_ + w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ + u_ * hu_ - v_ * hv_ + w_ * hw_, - c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ + u_ * hu_ - v_ * hv_ + w_ * hw_, + c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) { points.push_back(p); } // Z-aligned edges - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, - c_ - u_ * hu_ - v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, + c_ - u_ * hu_ - v_ * hv_ + w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ + u_ * hu_ - v_ * hv_ - w_ * hw_, - c_ + u_ * hu_ - v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ + u_ * hu_ - v_ * hv_ - w_ * hw_, + c_ + u_ * hu_ - v_ * hv_ + w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ - u_ * hu_ + v_ * hv_ - w_ * hw_, - c_ - u_ * hu_ + v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ - u_ * hu_ + v_ * hv_ - w_ * hw_, + c_ - u_ * hu_ + v_ * hv_ + w_ * hw_)) { points.push_back(p); } - if (IntersectPlaneAndSegment(p, normal, d, - c_ + u_ * hu_ + v_ * hv_ - w_ * hw_, - c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) + if (GeometryToolbox::IntersectPlaneAndSegment + (p, normal, d, + c_ + u_ * hu_ + v_ * hv_ - w_ * hw_, + c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) { points.push_back(p); }