Skip to content

Commit 828c64a

Browse files
committed
Mesh: consider placement in MeshPy::nearestFacetOnRay
1 parent 14451f7 commit 828c64a

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

src/Mod/Mesh/App/Mesh.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,32 @@ std::vector<PointIndex> MeshObject::getPointsFromFacets(const std::vector<FacetI
815815
return _kernel.GetFacetPoints(facets);
816816
}
817817

818+
bool MeshObject::nearestFacetOnRay(const MeshObject::TRay& ray, double maxAngle, MeshObject::TFaceSection& output) const
819+
{
820+
Base::Vector3f pnt = Base::toVector<float>(ray.first);
821+
Base::Vector3f dir = Base::toVector<float>(ray.second);
822+
823+
Base::Placement plm = getPlacement();
824+
Base::Placement inv = plm.inverse();
825+
826+
// transform the ray relative to the mesh kernel
827+
inv.multVec(pnt, pnt);
828+
inv.getRotation().multVec(dir, dir);
829+
830+
FacetIndex index = 0;
831+
Base::Vector3f res;
832+
MeshCore::MeshAlgorithm alg(getKernel());
833+
834+
if (alg.NearestFacetOnRay(pnt, dir, static_cast<float>(maxAngle), res, index)) {
835+
plm.multVec(res, res);
836+
output.first = index;
837+
output.second = Base::toVector<double>(res);
838+
return true;
839+
}
840+
841+
return false;
842+
}
843+
818844
void MeshObject::updateMesh(const std::vector<FacetIndex>& facets) const
819845
{
820846
std::vector<PointIndex> points;

src/Mod/Mesh/App/Mesh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class MeshExport MeshObject : public Data::ComplexGeoData
9494
// typedef needed for cross-section
9595
using TPlane = std::pair<Base::Vector3f, Base::Vector3f>;
9696
using TPolylines = std::list<std::vector<Base::Vector3f>>;
97+
using TRay = std::pair<Base::Vector3d, Base::Vector3d>;
98+
using TFaceSection = std::pair<FacetIndex, Base::Vector3d>;
9799

98100
MeshObject();
99101
explicit MeshObject(const MeshCore::MeshKernel& Kernel);
@@ -152,6 +154,7 @@ class MeshExport MeshObject : public Data::ComplexGeoData
152154
virtual void getFaces(std::vector<Base::Vector3d> &Points,std::vector<Facet> &Topo,
153155
float Accuracy, uint16_t flags=0) const;
154156
std::vector<PointIndex> getPointsFromFacets(const std::vector<FacetIndex>& facets) const;
157+
bool nearestFacetOnRay(const TRay& ray, double maxAngle, TFaceSection& output) const;
155158
//@}
156159

157160
void setKernel(const MeshCore::MeshKernel& m);

src/Mod/Mesh/App/MeshPyImp.cpp

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,53 +1833,20 @@ PyObject* MeshPy::nearestFacetOnRay(PyObject *args)
18331833
return nullptr;
18341834

18351835
try {
1836-
Py::Tuple pnt_t(pnt_p);
1837-
Py::Tuple dir_t(dir_p);
1836+
Py::Vector pnt_t(pnt_p, false);
1837+
Py::Vector dir_t(dir_p, false);
18381838
Py::Dict dict;
1839-
Base::Vector3f pnt((float)Py::Float(pnt_t.getItem(0)),
1840-
(float)Py::Float(pnt_t.getItem(1)),
1841-
(float)Py::Float(pnt_t.getItem(2)));
1842-
Base::Vector3f dir((float)Py::Float(dir_t.getItem(0)),
1843-
(float)Py::Float(dir_t.getItem(1)),
1844-
(float)Py::Float(dir_t.getItem(2)));
18451839

1846-
FacetIndex index = 0;
1847-
Base::Vector3f res;
1848-
MeshCore::MeshAlgorithm alg(getMeshObjectPtr()->getKernel());
1849-
1850-
#if 0 // for testing only
1851-
MeshCore::MeshFacetGrid grid(getMeshObjectPtr()->getKernel(),10);
1852-
// With grids we might search in the opposite direction, too
1853-
if (alg.NearestFacetOnRay(pnt, dir, grid, res, index) ||
1854-
alg.NearestFacetOnRay(pnt, -dir, grid, res, index)) {
1855-
#else
1856-
if (alg.NearestFacetOnRay(pnt, dir, static_cast<float>(maxAngle), res, index)) {
1857-
#endif
1840+
MeshObject::TRay ray = std::make_pair(pnt_t.toVector(),
1841+
dir_t.toVector());
1842+
MeshObject::TFaceSection output;
1843+
if (getMeshObjectPtr()->nearestFacetOnRay(ray, maxAngle, output)) {
18581844
Py::Tuple tuple(3);
1859-
tuple.setItem(0, Py::Float(res.x));
1860-
tuple.setItem(1, Py::Float(res.y));
1861-
tuple.setItem(2, Py::Float(res.z));
1862-
dict.setItem(Py::Long((int)index), tuple);
1863-
}
1864-
1865-
#if 0 // for testing only
1866-
char szBuf[200];
1867-
std::ofstream str("grid_test.iv");
1868-
Base::InventorBuilder builder(str);
1869-
MeshCore::MeshGridIterator g_it(grid);
1870-
for (g_it.Init(); g_it.More(); g_it.Next()) {
1871-
Base::BoundBox3f box = g_it.GetBoundBox();
1872-
unsigned long uX,uY,uZ;
1873-
g_it.GetGridPos(uX,uY,uZ);
1874-
builder.addBoundingBox(Base::Vector3f(box.MinX,box.MinY, box.MinZ),
1875-
Base::Vector3f(box.MaxX,box.MaxY, box.MaxZ));
1876-
sprintf(szBuf, "(%lu,%lu,%lu)", uX, uY, uZ);
1877-
builder.addText(box.GetCenter(), szBuf);
1845+
tuple.setItem(0, Py::Float(output.second.x));
1846+
tuple.setItem(1, Py::Float(output.second.y));
1847+
tuple.setItem(2, Py::Float(output.second.z));
1848+
dict.setItem(Py::Long(static_cast<int>(output.first)), tuple);
18781849
}
1879-
builder.addSingleArrow(pnt-20.0f*dir, pnt+10.0f*dir);
1880-
builder.close();
1881-
str.close();
1882-
#endif
18831850

18841851
return Py::new_reference_to(dict);
18851852
}

src/Mod/Mesh/App/MeshTestsApp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ def testFindNearest(self):
200200
len(self.mesh.nearestFacetOnRay((0.2,0.1,0.2),(0,0,-1))))
201201
self.assertEqual(len(self.mesh.nearestFacetOnRay((0.2,0.1,0.2),(0,0, 1), math.pi/2)),
202202
len(self.mesh.nearestFacetOnRay((0.2,0.1,0.2),(0,0,-1), math.pi/2)))
203+
# Apply placement to mesh
204+
plm = Base.Placement(Base.Vector(1,2,3), Base.Rotation(1,1,1,1))
205+
pnt = Base.Vector(0.5, 0.5, 0.5)
206+
vec = Base.Vector(0.0, 0.0, 1.0)
207+
208+
self.mesh.Placement = plm
209+
self.assertEqual(len(self.mesh.nearestFacetOnRay(pnt,vec)), 0)
210+
211+
# Apply the placement on the ray as well
212+
pnt = plm.multVec(pnt)
213+
vec = plm.Rotation.multVec(vec)
214+
self.assertEqual(len(self.mesh.nearestFacetOnRay(pnt,vec)), 1)
203215

204216
def testForaminate(self):
205217
class FilterAngle:

0 commit comments

Comments
 (0)