Skip to content

Commit

Permalink
Merge branch 'main' into feature/step-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
muneerlalji authored Feb 11, 2024
2 parents 50a8740 + 0b1f859 commit 82510f7
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 1 deletion.
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
}
}
29 changes: 29 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/*
5 changes: 4 additions & 1 deletion backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ include_directories(${OCE_INCLUDE_DIRS})
add_executable(cad_to_gcode src/cad_to_gcode.cpp)
add_executable(iges_to_topo src/iges_to_topo.cpp)
add_executable(step_to_topo src/step_to_topo.cpp)
add_executable(shape_to_bspline src/shape_to_bspline.cpp)
add_executable(tangent_vectors_geom_bspline src/tangent_vectors_geom_bspline.cpp)
target_link_libraries(cad_to_gcode ${OpenCASCADE_LIBRARIES})
target_link_libraries(iges_to_topo ${OpenCASCADE_LIBRARIES})
target_link_libraries(shape_to_bspline ${OpenCASCADE_LIBRARIES})
target_link_libraries(tangent_vectors_geom_bspline ${OpenCASCADE_LIBRARIES})
target_link_libraries(step_to_topo ${OpenCASCADE_LIBRARIES})

104 changes: 104 additions & 0 deletions backend/src/shape_to_bspline.cpp
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();
}

28 changes: 28 additions & 0 deletions backend/src/tangent_vectors_geom_bspline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#include <Geom_Curve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <GeomLProp_SLProps.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <vector>
#include <iostream>

// outputs a list of vectors containing the tangents to the knots on the curve
std::vector<gp_Vec> calculate_tangent_vectors(const Handle(Geom_BSplineCurve)& bsplineCurve) {
std::vector<gp_Vec> tangentVectors;
gp_Vec tangentVector;
gp_Pnt point;
int knots = bsplineCurve->NbKnots();
Standard_Real step_size = 1 / knots;
Standard_Real first_parameter = bsplineCurve->FirstParameter();
Standard_Real last_parameter = bsplineCurve->LastParameter();
for (int u = first_parameter; u < last_parameter; u += step_size) {
bsplineCurve->D1(u, point, tangentVector);
tangentVectors.push_back(tangentVector);
}
return tangentVectors;
}

int main(int argc, char* argv[]) {
return 1;
}

0 comments on commit 82510f7

Please sign in to comment.