Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
extend MeshPart.projectShapeOnMesh to allow list of points for projec…
…tion, support keywords
  • Loading branch information
wwmayer committed Jun 8, 2019
1 parent 4494d38 commit f8b715a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 10 deletions.
84 changes: 74 additions & 10 deletions src/Mod/MeshPart/App/AppMeshPartPy.cpp
Expand Up @@ -61,11 +61,16 @@ class Module : public Py::ExtensionModule<Module>
" upVector ((x, y, z) tuple):\n"
" MaxSize (float):\n"
);
add_varargs_method("projectShapeOnMesh",&Module::projectShapeOnMesh,
add_keyword_method("projectShapeOnMesh",&Module::projectShapeOnMesh,
"Projects a shape onto a mesh with a given maximum distance\n"
"projectShapeOnMesh(Shape, Mesh, float) -> polygon\n"
"or projects the shape in a given direction\n"
"projectShapeOnMesh(Shape, Mesh, Vector) -> list of polygons"
"\n"
"Multiple signatures are available:\n"
"\n"
"projectShapeOnMesh(Shape, Mesh, float) -> list of polygons\n"
"projectShapeOnMesh(Shape, Mesh, Vector) -> list of polygons\n"
"projectShapeOnMesh(list of polygons, Mesh, Vector) -> list of polygons\n"
);
add_varargs_method("wireFromSegment",&Module::wireFromSegment,
"Create wire(s) from boundary of segment\n"
Expand Down Expand Up @@ -203,13 +208,16 @@ class Module : public Py::ExtensionModule<Module>
MeshPart::MeshAlgos::LoftOnCurve(M,aShape,poly,Base::Vector3f(x,y,z),size);
return Py::asObject(new Mesh::MeshPy(new Mesh::MeshObject(M)));
}
Py::Object projectShapeOnMesh(const Py::Tuple& args)
Py::Object projectShapeOnMesh(const Py::Tuple& args, const Py::Dict& kwds)
{
static char* kwds_maxdist[] = {"Shape", "Mesh", "MaxDistance", NULL};
PyObject *s, *m;
double maxDist;
if (PyArg_ParseTuple(args.ptr(), "O!O!d", &Part::TopoShapePy::Type, &s,
&Mesh::MeshPy::Type, &m,
&maxDist)) {
if (PyArg_ParseTupleAndKeywords(args.ptr(), kwds.ptr(),
"O!O!d", kwds_maxdist,
&Part::TopoShapePy::Type, &s,
&Mesh::MeshPy::Type, &m,
&maxDist)) {
TopoDS_Shape shape = static_cast<Part::TopoShapePy*>(s)->getTopoShapePtr()->getShape();
const Mesh::MeshObject* mesh = static_cast<Mesh::MeshPy*>(m)->getMeshObjectPtr();
MeshCore::MeshKernel kernel(mesh->getKernel());
Expand All @@ -232,11 +240,14 @@ class Module : public Py::ExtensionModule<Module>
return list;
}

static char* kwds_dir[] = {"Shape", "Mesh", "Direction", NULL};
PyErr_Clear();
PyObject *v;
if (PyArg_ParseTuple(args.ptr(), "O!O!O!", &Part::TopoShapePy::Type, &s,
&Mesh::MeshPy::Type, &m,
&Base::VectorPy::Type, &v)) {
if (PyArg_ParseTupleAndKeywords(args.ptr(), kwds.ptr(),
"O!O!O!", kwds_dir,
&Part::TopoShapePy::Type, &s,
&Mesh::MeshPy::Type, &m,
&Base::VectorPy::Type, &v)) {
TopoDS_Shape shape = static_cast<Part::TopoShapePy*>(s)->getTopoShapePtr()->getShape();
const Mesh::MeshObject* mesh = static_cast<Mesh::MeshPy*>(m)->getMeshObjectPtr();
Base::Vector3d* vec = static_cast<Base::VectorPy*>(v)->getVectorPtr();
Expand All @@ -261,7 +272,60 @@ class Module : public Py::ExtensionModule<Module>
return list;
}

throw Py::TypeError("Expected arguments are: Shape, Mesh, float or Vector");
static char* kwds_poly[] = {"Polygons", "Mesh", "Direction", NULL};
PyErr_Clear();
PyObject *seq;
if (PyArg_ParseTupleAndKeywords(args.ptr(), kwds.ptr(),
"OO!O!", kwds_poly,
&seq,
&Mesh::MeshPy::Type, &m,
&Base::VectorPy::Type, &v)) {
std::vector<MeshProjection::PolyLine> polylinesIn;
Py::Sequence edges(seq);
polylinesIn.reserve(edges.size());

// collect list of sampled input edges
for (Py::Sequence::iterator it = edges.begin(); it != edges.end(); ++it) {
Py::Sequence edge(*it);
MeshProjection::PolyLine poly;
poly.points.reserve(edge.size());

for (Py::Sequence::iterator jt = edge.begin(); jt != edge.end(); ++jt) {
Py::Vector pnt(*jt);
poly.points.push_back(Base::convertTo<Base::Vector3f>(pnt.toVector()));
}

polylinesIn.push_back(poly);
}

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<MeshProjection::PolyLine> polylines;
proj.projectParallelToMesh(polylinesIn, dir, polylines);

Py::List list;
for (auto it : polylines) {
Py::List poly;
for (auto jt : it.points) {
Py::Vector v(jt);
poly.append(v);
}
list.append(poly);
}

return list;
}

throw Py::TypeError("Expected arguments are:\n"
"Shape, Mesh, float or\n"
"Shape, Mesh, Vector or\n"
"Polygons, Mesh, Vector\n");
}
Py::Object wireFromSegment(const Py::Tuple& args)
{
Expand Down
45 changes: 45 additions & 0 deletions src/Mod/MeshPart/App/CurveProjector.cpp
Expand Up @@ -813,6 +813,51 @@ void MeshProjection::projectParallelToMesh (const TopoDS_Shape &aShape, const Ba
}
}

void MeshProjection::projectParallelToMesh (const std::vector<PolyLine> &aEdges, const Base::Vector3f& dir, std::vector<PolyLine>& rPolyLines) 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 curve on mesh", aEdges.size() );

for (auto it : aEdges) {
std::vector<Base::Vector3f> points = it.points;

typedef std::pair<Base::Vector3f, unsigned long> HitPoint;
std::vector<HitPoint> hitPoints;
typedef std::pair<HitPoint, HitPoint> HitPoints;
std::vector<HitPoints> hitPointPairs;
for (auto it : points) {
Base::Vector3f result;
unsigned long index;
if (clAlg.NearestFacetOnRay(it, dir, cGrid, result, index)) {
hitPoints.push_back(std::make_pair(result, index));

if (hitPoints.size() > 1) {
HitPoint p1 = hitPoints[hitPoints.size()-2];
HitPoint p2 = hitPoints[hitPoints.size()-1];
hitPointPairs.push_back(std::make_pair(p1, p2));
}
}
}

MeshCore::MeshProjection meshProjection(_rcMesh);
PolyLine polyline;
for (auto it : hitPointPairs) {
points.clear();
if (meshProjection.projectLineOnMesh(cGrid, it.first.first, it.first.second,
it.second.first, it.second.second, dir, points)) {
polyline.points.insert(polyline.points.end(), points.begin(), points.end());
}
}
rPolyLines.push_back(polyline);

seq.next();
}
}

void MeshProjection::projectEdgeToEdge( const TopoDS_Edge &aEdge, float fMaxDist, const MeshFacetGrid& rGrid,
std::vector<SplitEdge>& rSplitEdges ) const
{
Expand Down
4 changes: 4 additions & 0 deletions src/Mod/MeshPart/App/CurveProjector.h
Expand Up @@ -191,6 +191,10 @@ class MeshPartExport MeshProjection
* Project all edges of the shape onto the mesh using parallel projection.
*/
void projectParallelToMesh (const TopoDS_Shape &aShape, const Base::Vector3f& dir, std::vector<PolyLine>& rPolyLines) const;
/**
* Project all polylines onto the mesh using parallel projection.
*/
void projectParallelToMesh (const std::vector<PolyLine>& aEdges, const Base::Vector3f& dir, std::vector<PolyLine>& rPolyLines) const;
/**
* Cuts the mesh at the curve defined by \a aShape. This method call @ref projectToMesh() to get the
* split the facet at the found points. @see projectToMesh() for more details.
Expand Down

0 comments on commit f8b715a

Please sign in to comment.