Mercurial > hg > orthanc-stone
changeset 151:c5044bbfc303 wasm
CoordinateSystem3D::IntersectSegment()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 01 Feb 2018 17:37:03 +0100 |
parents | 62670cc2bb50 |
children | 2e023be0563c |
files | Framework/Toolbox/CoordinateSystem3D.cpp Framework/Toolbox/CoordinateSystem3D.h Framework/Toolbox/GeometryToolbox.cpp Framework/Toolbox/GeometryToolbox.h Framework/Toolbox/OrientedBoundingBox.cpp |
diffstat | 5 files changed, 116 insertions(+), 73 deletions(-) [+] |
line wrap: on
line diff
--- 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; + } + } }
--- 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; }; }
--- 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; + } + } + } } }
--- 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)
--- 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<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; - } - } - } - - 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); }