Skip to content

Commit

Permalink
FIX setup, review comments and CPP now supports polygonal meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
petrasvestartas committed Jan 15, 2024
1 parent 42c096f commit 2e5bebf
Show file tree
Hide file tree
Showing 8 changed files with 10,057 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
* Added Polygonal Surface Reconstruction module using RANSAC.
* Added Polygonal Surface Reconstruction module using RANSAC for polygonal meshes.
* Additional dependency called SCIP to find which planes defines the closed object.

### Removed
Expand Down
10,011 changes: 10,011 additions & 0 deletions data/polygonal_sphere.ply

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/two_intersecting_boxes.ply
Original file line number Diff line number Diff line change
Expand Up @@ -10008,4 +10008,4 @@ end_header
0.12666589586374677 9.30560621400625 0 0 0 0
6.040394239146446 -30 56.01960610412974 0 0 0
-25.557654996196575 -38.682899353412395 0 0 0 0
44.46280114094858 20.000000000000004 38.68042719488983 0 0 0
44.46280114094858 20.000000000000004 38.68042719488983 0 0 0
53 changes: 30 additions & 23 deletions docs/examples/polygonal_poinset_reconstruction.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from pathlib import Path
from compas.geometry import Pointcloud, Point, Line, Polyhedron, Polygon
from compas.geometry import Pointcloud, Point, Line, Polygon
from compas_view2.app import App
# from compas_view2.collections import Collection
from compas_view2.collections import Collection
from compas.colors import Color
from compas_cgal.reconstruction import pointset_normal_estimation
from compas_cgal.polygonal_surface_reconstruction import polygonal_surface_reconstruction_ransac
from compas_cgal.polygonal_surface_reconstruction import (
polygonal_surface_reconstruction_ransac,
)

# Define the file path for the point cloud data
FILE = Path(__file__).parent.parent.parent / "data" / "two_intersecting_boxes.ply"
Expand All @@ -17,19 +19,13 @@
vertices, faces = polygonal_surface_reconstruction_ransac(points, vectors)

# Create polygons from vertices and faces
polygons = [Polygon([Point(*vertices[i]) for i in face]) for face in faces]
polygons = []
for face in faces:
polygon = []
for i in face:
polygon.append(Point(*vertices[i]))
polygons.append(Polygon(polygon))

# Create a polyhedron from vertices and faces
shape = Polyhedron(vertices.tolist(), faces.tolist()) # revise the Shape API

# Create a viewer
viewer = App(width=1600, height=900)
viewer.view.camera.position = [5, -4, 2]
viewer.view.camera.look_at([0, 1, 0])

# Add polygons to the viewer
for polygon in polygons:
viewer.add(polygon, linewidth=2)

# Create lines and properties for visualizing normals
lines = []
Expand All @@ -39,22 +35,33 @@
# Iterate over points and vectors to create lines and color properties
for p, v in zip(points, vectors):
# Create lines
lines.append(Line(Point(p[0], p[1], p[2]), Point(p[0] + v[0] * line_scale, p[1] + v[1] * line_scale, p[2] + v[2] * line_scale)))
lines.append(
Line(
Point(p[0], p[1], p[2]),
Point(
p[0] + v[0] * line_scale,
p[1] + v[1] * line_scale,
p[2] + v[2] * line_scale,
),
)
)

# Normalize vector components to be in the range [0, 1] for color representation
r = (v[0] + 1) * 0.5
g = (v[1] + 1) * 0.5
b = (v[2] + 1) * 0.5

# Store line color properties
line_properties.append({'linecolor': Color(r, g, b)})
line_properties.append({"linecolor": Color(r, g, b)})

# Create a viewer
viewer = App(width=1600, height=900)
viewer.view.camera.position = [5, -4, 2]
viewer.view.camera.look_at([0, 1, 0])
viewer.add(Pointcloud(points))
line_collection = Collection(lines, line_properties)
# Uncomment to see the normals:
# line_collection = Collection(lines, line_properties)
# viewer.add(line_collection)

# Add point cloud to the viewer
viewer.add(Pointcloud(points))

# Run the viewer
for polygon in polygons:
viewer.add(polygon, linewidth=2)
viewer.run()
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ def get_library_dirs():
return os.path.join(conda_prefix, "Library", "lib")
return os.path.join(conda_prefix, "lib")

def get_scip_library():
if windows:
return "libscip"
return "scip"

ext_modules = [

Extension(
"compas_cgal._cgal",
sorted(
Expand All @@ -65,7 +70,7 @@ def get_library_dirs():
),
include_dirs=["./include", get_eigen_include(), get_pybind_include()],
library_dirs=[get_library_dirs()],
libraries=["mpfr", "gmp", "libscip"],
libraries=["mpfr", "gmp", get_scip_library()],
language="c++",
),
]
Expand Down
2 changes: 1 addition & 1 deletion src/compas_cgal/polygonal_surface_reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def polygonal_surface_reconstruction_ransac(
points: Union[list[Point], NDArray[Shape["Any, 3"], Float]],
normals: Union[list[Vector], NDArray[Shape["Any, 3"], Float]],
): # -> Tuple[NDArray[Shape["Any, 3"], Float], NDArray[Shape["Any, 3"], Int]]:
):
"""Reconstruct a surface from a point cloud using the Poisson surface reconstruction algorithm.
Parameters
Expand Down
11 changes: 6 additions & 5 deletions src/polygonal_surface_reconstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef CGAL::Polygonal_surface_reconstruction<compas::Kernel> Polygonal_surface
typedef CGAL::Surface_mesh<compas::Point> Surface_mesh;

// Function for polygonal surface reconstruction using RANSAC
std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
std::tuple<compas::RowMatrixXd, std::vector<std::vector<int>>>
polygonal_surface_reconstruction_ransac(
Eigen::Ref<const compas::RowMatrixXd> &P,
Eigen::Ref<const compas::RowMatrixXd> &N)
Expand Down Expand Up @@ -97,7 +97,7 @@ polygonal_surface_reconstruction_ransac(
std::size_t f = candidate_faces.number_of_faces();

compas::RowMatrixXd V(v, 3);
compas::RowMatrixXi F(f, 4);
std::vector<std::vector<int>> F;

compas::Mesh::Property_map<compas::Mesh::Vertex_index, compas::Kernel::Point_3> location = candidate_faces.points();

Expand All @@ -110,15 +110,16 @@ polygonal_surface_reconstruction_ransac(

for (compas::Mesh::Face_index fd : candidate_faces.faces())
{
std::vector<int> fv;
int i = 0;
for (compas::Mesh::Vertex_index vd : vertices_around_face(candidate_faces.halfedge(fd), candidate_faces))
{
F(fd, i) = (int)vd;
i++;
fv.emplace_back((int)vd);
}
F.emplace_back(fv);
}

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi> result = std::make_tuple(V, F);
std::tuple<compas::RowMatrixXd, std::vector<std::vector<int>>> result = std::make_tuple(V, F);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/polygonal_surface_reconstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <compas.h>

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
std::tuple<compas::RowMatrixXd, std::vector<std::vector<int>>>
polygonal_surface_reconstruction_ransac(
Eigen::Ref<const compas::RowMatrixXd> &P,
Eigen::Ref<const compas::RowMatrixXd> &N);
Expand Down

0 comments on commit 2e5bebf

Please sign in to comment.