Skip to content

Commit

Permalink
Switch boost with graphlite for use in Godot Engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Apr 19, 2022
1 parent a18784c commit d5c6a5a
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 102 deletions.
2 changes: 1 addition & 1 deletion manifold/include/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <functional>
#include <memory>

#include "structs.h"
#include "../../utilities/include/structs.h"

namespace manifold {

Expand Down
10 changes: 5 additions & 5 deletions manifold/src/constructors.cu
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Manifold Manifold::Cube(glm::vec3 size, bool center) {
Manifold Manifold::Cylinder(float height, float radiusLow, float radiusHigh,
int circularSegments, bool center) {
float scale = radiusHigh >= 0.0f ? radiusHigh / radiusLow : 1.0f;
float radius = max(radiusLow, radiusHigh);
float radius = fmax(radiusLow, radiusHigh);
int n = circularSegments > 2 ? circularSegments : GetCircularSegments(radius);
Polygons circle(1);
float dPhi = 360.0f / n;
Expand Down Expand Up @@ -283,7 +283,7 @@ Manifold Manifold::Revolve(const Polygons& crossSection, int circularSegments) {
float radius = 0.0f;
for (const auto& poly : crossSection) {
for (const auto& vert : poly) {
radius = max(radius, vert.pos.x);
radius = fmax(radius, vert.pos.x);
}
}
int nDivisions =
Expand Down Expand Up @@ -446,18 +446,18 @@ std::vector<Manifold> Manifold::Decompose() const {
graph.add_edge(halfedge.startVert, halfedge.endVert);
}
Components components = ConnectedComponents(graph);
const int numLabel = components.componentLabels.size();
const int numLabel = components.componentNodes.size();

if (numLabel == 1) {
std::vector<Manifold> meshes(1);
meshes[0] = *this;
return meshes;
}
VecDH<int> vertLabel(components.nodeLabels);
VecDH<int> vertLabel(components.nodeComponents);

std::vector<Manifold> meshes(numLabel);
for (int i = 0; i < numLabel; ++i) {
const int component = components.componentLabels[i];
const int component = components.componentNodes[i];
meshes[i].pImpl_->vertPos_.resize(NumVert());
VecDH<int> vertNew2Old(NumVert());
int nVert =
Expand Down
28 changes: 13 additions & 15 deletions manifold/src/impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
#include <thrust/logical.h>

#include <algorithm>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <map>

#include "graph.h"

#include "impl.cuh"

namespace {
Expand Down Expand Up @@ -414,22 +413,21 @@ int Manifold::Impl::InitializeNewReference(
CoplanarEdge({triArea.ptrD(), halfedge_.cptrD(), vertPos_.cptrD(),
triPropertiesD.cptrD(), propertiesD.cptrD(),
propertyToleranceD.cptrD(), numProps, precision_}));

boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> graph(
NumTri());
Graph graph;
for (int i = 0; i < NumTri(); ++i) {
graph.add_nodes(i);
}
for (int i = 0; i < face2face.size(); ++i) {
const thrust::pair<int, int> edge = face2face.H()[i];
if (edge.first < 0) continue;
boost::add_edge(edge.first, edge.second, graph);
graph.add_edge(edge.first, edge.second);
}
std::vector<int> components(NumTri());
const int numComponent =
boost::connected_components(graph, components.data());

std::vector<int> comp2tri(numComponent, -1);
Components components = ConnectedComponents(graph);
const int numNodes = components.nodeComponents.size();
std::vector<int> comp2tri(numNodes, -1);
for (int tri = 0; tri < NumTri(); ++tri) {
const int comp = components[tri];
const int current = comp2tri[comp];
const int comp = components.nodeComponents[tri];
const int current = comp2tri[components.nodeComponents[tri]];
if (current < 0 || triArea.H()[tri] > triArea.H()[current]) {
comp2tri[comp] = tri;
triArea.H()[comp] = triArea.H()[tri];
Expand All @@ -440,7 +438,7 @@ int Manifold::Impl::InitializeNewReference(
std::map<std::pair<int, int>, int> triVert2bary;

for (int tri = 0; tri < NumTri(); ++tri) {
const int refTri = comp2tri[components[tri]];
const int refTri = comp2tri[components.nodeComponents[tri]];
if (refTri == tri) continue;

glm::mat3 triPos;
Expand Down
3 changes: 1 addition & 2 deletions manifold/src/manifold.cu
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ Mesh Manifold::GetMesh() const {
result.halfedgeTangent.insert(result.halfedgeTangent.end(),
pImpl_->halfedgeTangent_.begin(),
pImpl_->halfedgeTangent_.end());

result.triVerts.resize(NumTri());
thrust::for_each_n(zip(result.triVerts.begin(), countAt(0)), NumTri(),
MakeTri({pImpl_->halfedge_.cptrH()}));
Expand Down Expand Up @@ -186,7 +185,7 @@ int Manifold::GetCircularSegments(float radius) {
if (Manifold::circularSegments_ > 0) return Manifold::circularSegments_;
int nSegA = 360.0f / Manifold::circularAngle_;
int nSegL = 2.0f * radius * glm::pi<float>() / Manifold::circularEdgeLength_;
int nSeg = min(nSegA, nSegL) + 3;
int nSeg = std::min(nSegA, nSegL) + 3;
nSeg -= nSeg % 4;
return nSeg;
}
Expand Down
58 changes: 29 additions & 29 deletions test/data/Csaszar.ply
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
ply
format ascii 1.0
element vertex 7
property float32 x
property float32 y
property float32 z
element face 14
property list uchar int vertex_indices
end_header
-20 -20 -10
-20 20 -15
-5 -8 8
0 0 30
5 8 8
20 -20 -15
20 20 -10
3 1 3 6
3 1 6 5
3 2 5 6
3 0 2 6
3 0 6 4
3 3 4 6
3 1 2 3
3 1 4 2
3 1 0 4
3 1 5 0
3 3 5 4
3 0 5 3
3 0 3 2
ply
format ascii 1.0
element vertex 7
property float32 x
property float32 y
property float32 z
element face 14
property list uchar int vertex_indices
end_header
-20 -20 -10
-20 20 -15
-5 -8 8
0 0 30
5 8 8
20 -20 -15
20 20 -10
3 1 3 6
3 1 6 5
3 2 5 6
3 0 2 6
3 0 6 4
3 3 4 6
3 1 2 3
3 1 4 2
3 1 0 4
3 1 5 0
3 3 5 4
3 0 5 3
3 0 3 2
3 2 4 5
82 changes: 41 additions & 41 deletions test/data/icosahedron.ply
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
ply
format ascii 1.0
element vertex 12
property float32 x
property float32 y
property float32 z
element face 20
property list uchar int vertex_indices
end_header
1.618 1 0
-1.618 1 0
0 1.618 1
0 -1.618 1
1 0 1.618
1 0 -1.618
-1.618 -1 0
1.618 -1 0
0 -1.618 -1
0 1.618 -1
-1 0 -1.618
-1 0 1.618
3 0 2 4
3 7 0 4
3 0 9 2
3 4 2 11
3 5 9 0
3 9 1 2
3 2 1 11
3 11 3 4
3 7 4 3
3 11 6 3
3 11 1 6
3 9 10 1
3 10 6 1
3 5 10 9
3 5 0 7
3 10 5 8
3 6 10 8
3 3 6 8
3 7 3 8
ply
format ascii 1.0
element vertex 12
property float32 x
property float32 y
property float32 z
element face 20
property list uchar int vertex_indices
end_header
1.618 1 0
-1.618 1 0
0 1.618 1
0 -1.618 1
1 0 1.618
1 0 -1.618
-1.618 -1 0
1.618 -1 0
0 -1.618 -1
0 1.618 -1
-1 0 -1.618
-1 0 1.618

3 0 2 4
3 7 0 4
3 0 9 2
3 4 2 11
3 5 9 0
3 9 1 2
3 2 1 11
3 11 3 4
3 7 4 3
3 11 6 3
3 11 1 6
3 9 10 1
3 10 6 1
3 5 10 9
3 5 0 7
3 10 5 8
3 6 10 8
3 3 6 8
3 7 3 8
3 5 7 8
4 changes: 2 additions & 2 deletions third_party/graphlite/include/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
namespace manifold {

struct Components {
std::vector<int> componentLabels;
std::vector<int> nodeLabels;
std::vector<int> componentNodes;
std::vector<int> nodeComponents;
};

typedef typename graph_lite::Graph<
Expand Down
14 changes: 7 additions & 7 deletions third_party/graphlite/src/connected_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ Components ConnectedComponents(const Graph& graph) {
if (!graph.size()) {
return components;
}
components.nodeLabels.resize(graph.size());
std::fill(components.nodeLabels.begin(), components.nodeLabels.end(), -1);
components.nodeComponents.resize(graph.size());
std::fill(components.nodeComponents.begin(), components.nodeComponents.end(), -1);

std::deque<int> queue;
const auto begin = graph.begin();
const auto end = graph.end();
for (auto it = begin; it != end; ++it) {
const int& root = *it;
if (components.nodeLabels[root] >= 0) continue; // skip visited nodes
if (components.nodeComponents[root] >= 0) continue; // skip visited nodes

// new component
components.nodeLabels[root] = root;
components.componentLabels.push_back(root);
components.nodeComponents[root] = root;
components.componentNodes.push_back(root);
queue.emplace_back(root);
// traverse all connected nodes
while (!queue.empty()) {
const auto [n_begin, n_end] = graph.neighbors(queue.front());
queue.pop_front();
for (auto n_it = n_begin; n_it != n_end; ++n_it) {
const int& neighbor = *n_it;
if (components.nodeLabels[neighbor] < 0) {
if (components.nodeComponents[neighbor] < 0) {
// traversed node is labeled with the root component label
components.nodeLabels[neighbor] = root;
components.nodeComponents[neighbor] = root;
queue.emplace_back(neighbor);
}
}
Expand Down
2 changes: 2 additions & 0 deletions utilities/include/sparse.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "utils.cuh"
#include "vec_dh.cuh"

#include <math.h>

namespace manifold {

/** @ingroup Private */
Expand Down

0 comments on commit d5c6a5a

Please sign in to comment.