-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8cddb2
commit bf282a3
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <TopoDS_Shape.hxx> | ||
#include <TopExp_Explorer.hxx> | ||
#include <TopoDS_Edge.hxx> | ||
#include <BRep_Tool.hxx> | ||
#include <Geom_Curve.hxx> | ||
#include <GeomAPI_Interpolate.hxx> | ||
#include <TColgp_HArray1OfPnt.hxx> | ||
#include <GCPnts_AbscissaPoint.hxx> | ||
#include <GeomAdaptor_Curve.hxx> | ||
#include <vector> | ||
#include <GeomAPI_PointsToBSpline.hxx> | ||
#include <TopoDS_Vertex.hxx> | ||
#include <TopExp.hxx> | ||
#include <TopoDS.hxx> | ||
#include <gp_Pnt.hxx> | ||
|
||
bool VerticesMatch(const TopoDS_Vertex& v1, const TopoDS_Vertex& v2) { | ||
gp_Pnt p1 = BRep_Tool::Pnt(v1); | ||
gp_Pnt p2 = BRep_Tool::Pnt(v2); | ||
return p1.IsEqual(p2, Precision::Confusion()); | ||
} | ||
|
||
std::vector<TopoDS_Edge> OrderEdges(const TopoDS_Shape& shape) { | ||
std::vector<TopoDS_Edge> unorderedEdges, orderedEdges; | ||
|
||
// Fill unorderedEdges with all edges in the shape | ||
for (TopExp_Explorer exp(shape, TopAbs_EDGE); exp.More(); exp.Next()) { | ||
unorderedEdges.push_back(TopoDS::Edge(exp.Current())); | ||
} | ||
|
||
if (unorderedEdges.empty()) return orderedEdges; | ||
|
||
// Start with a random edge | ||
orderedEdges.push_back(unorderedEdges.front()); | ||
unorderedEdges.erase(unorderedEdges.begin()); | ||
|
||
while (!unorderedEdges.empty()) { | ||
bool matchFound = false; | ||
|
||
TopoDS_Vertex startV, endV; | ||
TopExp::Vertices(orderedEdges.front(), startV, endV); // Get start/end vertices of the current path | ||
|
||
for (auto it = unorderedEdges.begin(); it != unorderedEdges.end(); ++it) { | ||
TopoDS_Vertex startVNext, endVNext; | ||
TopExp::Vertices(*it, startVNext, endVNext); // Get vertices of the next candidate edge | ||
|
||
if (VerticesMatch(endV, startVNext)) { // Match end to start | ||
orderedEdges.push_back(*it); // Place it at the end | ||
endV = endVNext; // Update end vertex of the path | ||
unorderedEdges.erase(it); | ||
matchFound = true; | ||
break; | ||
} else if (VerticesMatch(startV, endVNext)) { // Match start to end | ||
orderedEdges.insert(orderedEdges.begin(), *it); // Place it at the beginning | ||
startV = startVNext; // Update start vertex of the path | ||
unorderedEdges.erase(it); | ||
matchFound = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!matchFound) { | ||
break; | ||
} | ||
} | ||
|
||
return orderedEdges; | ||
} | ||
|
||
// Function to sample points from an edge | ||
std::vector<gp_Pnt> SamplePointsFromEdge(const TopoDS_Edge& edge, int numPoints) { | ||
std::vector<gp_Pnt> points; | ||
double first, last; | ||
Handle(Geom_Curve) curve = BRep_Tool::Curve(edge, first, last); | ||
if (!curve.IsNull()) { | ||
double step = (last - first) / (numPoints - 1); | ||
for (int i = 0; i < numPoints; ++i) { | ||
gp_Pnt pnt = curve->Value(first + i * step); | ||
points.push_back(pnt); | ||
} | ||
} | ||
return points; | ||
} | ||
|
||
Handle(Geom_BSplineCurve) CreateBSplineFromShape(const TopoDS_Shape& shape) { | ||
std::vector<TopoDS_Edge> orderedEdges = OrderEdges(shape); | ||
std::vector<gp_Pnt> allPoints; | ||
const int SAMPLE_NUM = 10; | ||
|
||
for (const auto& edge : orderedEdges) { | ||
std::vector<gp_Pnt> edgePoints = SamplePointsFromEdge(edge, SAMPLE_NUM); | ||
allPoints.insert(allPoints.end(), edgePoints.begin(), edgePoints.end()); | ||
} | ||
|
||
// Convert std::vector<gp_Pnt> to TColgp_Array1OfPnt | ||
TColgp_Array1OfPnt arrayPoints(1, allPoints.size()); | ||
for (Standard_Integer i = 0; i < allPoints.size(); ++i) { | ||
arrayPoints.SetValue(i + 1, allPoints[i]); | ||
} | ||
|
||
GeomAPI_PointsToBSpline splineBuilder(arrayPoints); | ||
return splineBuilder.Curve(); | ||
} | ||
|