From a6f0f69ed664f23ddc73f4f838e199532f293092 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 13 Feb 2022 12:46:01 +0100 Subject: [PATCH] Mesh: overload MeshAlgorithm::NearestFacetOnRay to set a max. angle between facet and ray --- src/Mod/Mesh/App/Core/Algorithm.cpp | 14 +++++++++++--- src/Mod/Mesh/App/Core/Algorithm.h | 11 +++++++++++ src/Mod/Mesh/App/MeshPyImp.cpp | 5 +++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Mod/Mesh/App/Core/Algorithm.cpp b/src/Mod/Mesh/App/Core/Algorithm.cpp index ae277455bcec..8f8f97f13631 100644 --- a/src/Mod/Mesh/App/Core/Algorithm.cpp +++ b/src/Mod/Mesh/App/Core/Algorithm.cpp @@ -68,6 +68,12 @@ bool MeshAlgorithm::IsVertexVisible (const Base::Vector3f &rcVertex, const Base: bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, Base::Vector3f &rclRes, FacetIndex &rulFacet) const +{ + return NearestFacetOnRay(rclPt, rclDir, Mathf::PI, rclRes, rulFacet); +} + +bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, float fMaxAngle, + Base::Vector3f &rclRes, FacetIndex &rulFacet) const { Base::Vector3f clProj, clRes; bool bSol = false; @@ -76,13 +82,15 @@ bool MeshAlgorithm::NearestFacetOnRay (const Base::Vector3f &rclPt, const Base:: // slow execution with no grid MeshFacetIterator clFIter(_rclMesh); for (clFIter.Init(); clFIter.More(); clFIter.Next()) { - if (clFIter->Foraminate( rclPt, rclDir, clRes ) == true) { - if (bSol == false) { // first solution + if (clFIter->Foraminate(rclPt, rclDir, clRes, fMaxAngle)) { + if (bSol == false) { + // first solution bSol = true; clProj = clRes; ulInd = clFIter.Position(); } - else { // is closer to the point + else { + // is closer to the point if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) { clProj = clRes; ulInd = clFIter.Position(); diff --git a/src/Mod/Mesh/App/Core/Algorithm.h b/src/Mod/Mesh/App/Core/Algorithm.h index 0c35235f0b30..4ab6700ac83b 100644 --- a/src/Mod/Mesh/App/Core/Algorithm.h +++ b/src/Mod/Mesh/App/Core/Algorithm.h @@ -71,6 +71,17 @@ class MeshExport MeshAlgorithm */ bool NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, Base::Vector3f &rclRes, FacetIndex &rulFacet) const; + /** + * Searches for the nearest facet to the ray defined by + * (\a rclPt, \a rclDir). + * The point \a rclRes holds the intersection point with the ray and the + * nearest facet with index \a rulFacet. The angle between the ray and the normal of the triangle + * must be less than or equal to \a fMaxAngle. + * \note This method tests all facets so it should only be used + * occasionally. + */ + bool NearestFacetOnRay (const Base::Vector3f &rclPt, const Base::Vector3f &rclDir, float fMaxAngle, Base::Vector3f &rclRes, + FacetIndex &rulFacet) const; /** * Searches for the nearest facet to the ray defined by * (\a rclPt, \a rclDir). diff --git a/src/Mod/Mesh/App/MeshPyImp.cpp b/src/Mod/Mesh/App/MeshPyImp.cpp index a6c4f6f39404..5b48dbc043f2 100644 --- a/src/Mod/Mesh/App/MeshPyImp.cpp +++ b/src/Mod/Mesh/App/MeshPyImp.cpp @@ -1830,7 +1830,8 @@ PyObject* MeshPy::nearestFacetOnRay(PyObject *args) { PyObject* pnt_p; PyObject* dir_p; - if (!PyArg_ParseTuple(args, "OO", &pnt_p, &dir_p)) + double maxAngle = MeshCore::Mathd::PI; + if (!PyArg_ParseTuple(args, "OO|d", &pnt_p, &dir_p, &maxAngle)) return nullptr; try { @@ -1854,7 +1855,7 @@ PyObject* MeshPy::nearestFacetOnRay(PyObject *args) if (alg.NearestFacetOnRay(pnt, dir, grid, res, index) || alg.NearestFacetOnRay(pnt, -dir, grid, res, index)) { #else - if (alg.NearestFacetOnRay(pnt, dir, res, index)) { + if (alg.NearestFacetOnRay(pnt, dir, static_cast(maxAngle), res, index)) { #endif Py::Tuple tuple(3); tuple.setItem(0, Py::Float(res.x));