Skip to content

Commit

Permalink
add method to project points on mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 26, 2019
1 parent 916fe7c commit c1d9087
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Mod/MeshPart/App/AppMeshPartPy.cpp
Expand Up @@ -79,6 +79,11 @@ class Module : public Py::ExtensionModule<Module>
"projectShapeOnMesh(Shape, Mesh, Vector) -> list of polygons\n"
"projectShapeOnMesh(list of polygons, Mesh, Vector) -> list of polygons\n"
);
add_varargs_method("projectPointsOnMesh",&Module::projectPointsOnMesh,
"Projects points onto a mesh with a given direction\n"
"and tolerance."
"projectPointsOnMesh(list of points, Mesh, Vector, [float]) -> list of points\n"
);
add_varargs_method("wireFromSegment",&Module::wireFromSegment,
"Create wire(s) from boundary of segment\n"
);
Expand Down Expand Up @@ -361,6 +366,47 @@ class Module : public Py::ExtensionModule<Module>
"Shape, Mesh, Vector or\n"
"Polygons, Mesh, Vector\n");
}
Py::Object projectPointsOnMesh(const Py::Tuple& args)
{
PyObject *seq, *m, *v;
double precision = -1;
if (PyArg_ParseTuple(args.ptr(), "OO!O!|d",
&seq,
&Mesh::MeshPy::Type, &m,
&Base::VectorPy::Type, &v,
&precision)) {
std::vector<Base::Vector3f> pointsIn;
Py::Sequence points(seq);
pointsIn.reserve(points.size());

// collect list of input points
for (Py::Sequence::iterator it = points.begin(); it != points.end(); ++it) {
Py::Vector pnt(*it);
pointsIn.push_back(Base::convertTo<Base::Vector3f>(pnt.toVector()));
}

const Mesh::MeshObject* mesh = static_cast<Mesh::MeshPy*>(m)->getMeshObjectPtr();
Base::Vector3d* vec = static_cast<Base::VectorPy*>(v)->getVectorPtr();
Base::Vector3f dir = Base::convertTo<Base::Vector3f>(*vec);

MeshCore::MeshKernel kernel(mesh->getKernel());
kernel.Transform(mesh->getTransform());

MeshProjection proj(kernel);
std::vector<Base::Vector3f> pointsOut;
proj.projectOnMesh(pointsIn, dir, static_cast<float>(precision), pointsOut);

Py::List list;
for (auto it : pointsOut) {
Py::Vector v(it);
list.append(v);
}

return list;
}

throw Py::Exception();
}
Py::Object wireFromSegment(const Py::Tuple& args)
{
PyObject *o, *m;
Expand Down
30 changes: 30 additions & 0 deletions src/Mod/MeshPart/App/CurveProjector.cpp
Expand Up @@ -844,6 +844,36 @@ void MeshProjection::projectToMesh (const TopoDS_Shape &aShape, float fMaxDist,
}
}

void MeshProjection::projectOnMesh(const std::vector<Base::Vector3f>& pointsIn,
const Base::Vector3f& dir,
float tolerance,
std::vector<Base::Vector3f>& pointsOut) const
{
// calculate the average edge length and create a grid
MeshAlgorithm clAlg(_rcMesh);
float fAvgLen = clAlg.GetAverageEdgeLength();
MeshFacetGrid cGrid(_rcMesh, 5.0f*fAvgLen);

Base::SequencerLauncher seq( "Project points on mesh", pointsIn.size() );

for (auto it : pointsIn) {
Base::Vector3f result;
unsigned long index;
if (clAlg.NearestFacetOnRay(it, dir, cGrid, result, index)) {
MeshCore::MeshGeomFacet geomFacet = _rcMesh.GetFacet(index);
if (tolerance > 0 && geomFacet.IntersectPlaneWithLine(it, dir, result)) {
if (geomFacet.IsPointOfFace(result, tolerance))
pointsOut.push_back(result);
}
else {
pointsOut.push_back(result);
}
}

seq.next();
}
}

void MeshProjection::projectParallelToMesh (const TopoDS_Shape &aShape, const Base::Vector3f& dir, std::vector<PolyLine>& rPolyLines) const
{
// calculate the average edge length and create a grid
Expand Down
12 changes: 12 additions & 0 deletions src/Mod/MeshPart/App/CurveProjector.h
Expand Up @@ -196,6 +196,18 @@ class MeshPartExport MeshProjection
* taken if the distance between the curve point and the projected point is <= \a fMaxDist.
*/
void projectToMesh (const TopoDS_Shape &aShape, float fMaxDist, std::vector<PolyLine>& rPolyLines) const;
/**
* @brief projectOnMesh
* Projects the given points onto the mesh along a given direction. The points can can be projected
* will be saved to \a pointsOut
* @brief projectOnMesh
* @param pointsIn
* @param dir
* @param tolerance
* @param pointsOut
*/
void projectOnMesh(const std::vector<Base::Vector3f>& pointsIn, const Base::Vector3f& dir,
float tolerance, std::vector<Base::Vector3f>& pointsOut) const;
/**
* Project all edges of the shape onto the mesh using parallel projection.
*/
Expand Down

0 comments on commit c1d9087

Please sign in to comment.