Skip to content

Commit

Permalink
loop closing
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor committed Jan 7, 2025
1 parent 8a41503 commit 03ab1a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
13 changes: 9 additions & 4 deletions src/addons/Engineering/CSG/CSGGeometryAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ float calcArea(Vec3d p1, Vec3d p2, Vec3d p3) {
;
}*/

#include <CGAL/Polygon_mesh_processing/repair.h>

// Converts geometry to a polyhedron && applies the geometry node's world transform to the polyhedron.
// OpenSG geometry data isn't transformed itself but has an associated transform core. Both are unified for CGAL.
CGALPolyhedron* CSGGeometry::toPolyhedron(VRGeometryPtr geo, PosePtr worldTransform, bool& success) {
cout << "CSGGeometry::toPolyhedron " << geo->getName() << endl;
TriangleIterator it;
auto gpos = geo->getMesh()->geo->getPositions();

Expand Down Expand Up @@ -64,7 +66,7 @@ CGALPolyhedron* CSGGeometry::toPolyhedron(VRGeometryPtr geo, PosePtr worldTransf
for (int i=1; i<3; i++) if (v[i].squareLength() > v[imax2].squareLength() && i != imax) imax2 = i;

int j = imax2;//(im+1)%3;
cout << "set p[" << j << "] = " << p[j] << " with index i[" << imax << "] = " << vi[imax] << endl;
//cout << "set p[" << j << "] = " << p[j] << " with index i[" << imax << "] = " << vi[imax] << endl;
gpos->setValue(p[j], vi[imax]);
NA++;
for (int i=0; i<3; i++) p[i] = it.getPosition(i);
Expand Down Expand Up @@ -106,8 +108,8 @@ CGALPolyhedron* CSGGeometry::toPolyhedron(VRGeometryPtr geo, PosePtr worldTransf

bool verbose = false;
vector<size_t> debIDs;
debIDs.push_back(754);
debIDs.push_back(755);
//debIDs.push_back(754);
//debIDs.push_back(755);
for (int i : IDs) for (int j : debIDs) if (i == j) verbose = true;
if (verbose) {
cout << "IDs " << IDs[0] << " " << IDs[1] << " " << IDs[2] << endl;
Expand All @@ -125,9 +127,12 @@ CGALPolyhedron* CSGGeometry::toPolyhedron(VRGeometryPtr geo, PosePtr worldTransf

// Construct the polyhedron from raw data
success = true;
CGALPolyhedron *result = new CGALPolyhedron();
CGALPolyhedron* result = new CGALPolyhedron();
PolyhedronBuilder<CGAL::HalfedgeDS> builder(positions, indices);
result->polyhedron->delegate(builder);

//CGAL::Polygon_mesh_processing::remove_degenerate_faces(*(result->polyhedron)); // crashes :(

if (!result->polyhedron->is_closed()) {
success = false;
cout << "Error: The polyhedron is not a closed mesh!" << endl;
Expand Down
23 changes: 8 additions & 15 deletions src/addons/Semantics/Segmentation/VRAdjacencyGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ vector<int> VRAdjacencyGraph::getBorderVertices() {
auto it = unique(borders.begin(), borders.end());
borders.erase(it, borders.end());
}
cout << "getBorderVertices " << borders.size() << endl;
return borders;
}

Expand All @@ -209,39 +208,33 @@ vector< vector<int> > VRAdjacencyGraph::getBorderLoops() {
};

auto finishLoop = [&](const vector<int>& loop) {
cout << " finishLoop " << loop.size() << endl;
for (auto l : loop) processed[l] = true;
loops.push_back(loop);
};

auto continueLooping = [&](vector<int>& loop, int v, int v0) {
if (v == v0) { finishLoop(loop); return false; }
if (find(loop.begin(), loop.end(), v) != loop.end()) return false;
loop.push_back(v);
return true;
};

function<void(vector<int>&, int, int)> gatherLoop = [&](vector<int>& loop, int v, int v0) {
function<bool(vector<int>&, int, int)> gatherLoop = [&](vector<int>& loop, int v, int v0) {
do {
int n = loop.size();
auto vs = getNextBVerts( n>1 ? loop[n-2] : -1, loop[n-1] );

if (vs.size() == 0) break;

if (vs.size() == 1) {
if (!continueLooping(loop, vs[0], v0)) break;
int v = vs[0];
if (v == v0) { finishLoop(loop); return true; }
if (find(loop.begin(), loop.end(), v) != loop.end()) return false;
loop.push_back(v);
} else {
cout << "detected subloops: " << vs.size() << endl;
for (auto& v2 : vs) {
cout << " continue subloop v2: " << v2 << ", v0 " << v0 << endl;
vector<int> subloop = loop;
subloop.push_back(v2);
gatherLoop(subloop, v2, v0);
if (gatherLoop(subloop, v2, v0)) return true;
}
cout << "subloops processed" << endl;
break;
}
} while (true);

return false;
};

for (size_t i=0; i<bverts.size(); i++) {
Expand Down

0 comments on commit 03ab1a4

Please sign in to comment.