Skip to content

Commit 648edb3

Browse files
committed
temp
1 parent fbc31b1 commit 648edb3

File tree

5 files changed

+175
-43
lines changed

5 files changed

+175
-43
lines changed

scripts/cdt.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
3+
from compas_plotters import Plotter
4+
from compas.geometry import Point, Polygon
5+
from compas_cgal._cgal import triangulations
6+
7+
polygon = Polygon.from_sides_and_radius_xy(5, 10)
8+
9+
vertices = np.array([point[:2] for point in polygon.points], dtype=np.float64)
10+
edges = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 0]], dtype=np.int32)
11+
12+
result = triangulations.constrained_delaunay_triangulation(vertices, edges)
13+
14+
plotter = Plotter()
15+
16+
for face in result:
17+
triangle = Polygon([Point(vertices[index][0], vertices[index][1], 0.0) for index in face])
18+
plotter.add(triangle)
19+
20+
plotter.zoom_extents()
21+
plotter.show()

setup.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ def get_library_dirs():
5555
sorted([
5656
'src/compas_cgal.cpp',
5757
'src/compas.cpp',
58-
'src/meshing.cpp',
59-
'src/booleans.cpp',
60-
'src/slicer.cpp',
61-
'src/intersections.cpp',
62-
'src/measure.cpp',
58+
# 'src/meshing.cpp',
59+
# 'src/booleans.cpp',
60+
# 'src/slicer.cpp',
61+
# 'src/intersections.cpp',
62+
# 'src/measure.cpp',
63+
'src/triangulations.cpp',
6364
]),
6465
include_dirs=[
6566
'./include',

src/compas_cgal.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
namespace py = pybind11;
55

6-
void init_meshing(py::module&);
7-
void init_booleans(py::module&);
8-
void init_slicer(py::module&);
9-
void init_intersections(py::module&);
10-
void init_measure(py::module&);
6+
// void init_meshing(py::module&);
7+
// void init_booleans(py::module&);
8+
// void init_slicer(py::module&);
9+
// void init_intersections(py::module&);
10+
// void init_measure(py::module&);
11+
void init_triangulations(py::module&);
1112

1213
PYBIND11_MODULE(_cgal, m) {
1314
m.doc() = "";
@@ -16,9 +17,10 @@ PYBIND11_MODULE(_cgal, m) {
1617
.def_readonly("vertices", &compas::Result::vertices)
1718
.def_readonly("faces", &compas::Result::faces);
1819

19-
init_meshing(m);
20-
init_booleans(m);
21-
init_slicer(m);
22-
init_intersections(m);
23-
init_measure(m);
20+
// init_meshing(m);
21+
// init_booleans(m);
22+
// init_slicer(m);
23+
// init_intersections(m);
24+
// init_measure(m);
25+
init_triangulations(m);
2426
}

src/triangulations.cpp

Lines changed: 126 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,135 @@
11
#include "triangulations.h"
22

3-
// #include <CGAL/Polygon_mesh_processing/remesh.h>
4-
// #include <CGAL/Polygon_mesh_processing/detect_features.h>
3+
#include <CGAL/Delaunay_triangulation_2.h>
4+
#include <CGAL/Constrained_triangulation_2.h>
5+
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
6+
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
7+
8+
using DT = CGAL::Delaunay_triangulation_2<Kernel>;
9+
using DT_Point = DT::Point;
10+
using DT_Vertex = DT::Vertex_handle;
11+
12+
using CDT_VertexBase = CGAL::Triangulation_vertex_base_with_info_2<unsigned, Kernel>;
13+
using CDT_DS = CGAL::Triangulation_data_structure_2<CDT_VertexBase>;
14+
using CDT = CGAL::Constrained_Delaunay_triangulation_2<Kernel, CDT_DS>;
15+
using CDT_Point = CDT::Point;
16+
using CDT_Vertex = CDT::Vertex_handle;
517

6-
// namespace PMP = CGAL::Polygon_mesh_processing;
7-
// namespace params = PMP::parameters;
818
namespace py = pybind11;
919

10-
std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
11-
pmp_tetrahedralisation(
12-
Eigen::Ref<const compas::RowMatrixXd> & Points)
20+
compas::RowMatrixXi
21+
pmp_delaunay_triangulation(
22+
Eigen::Ref<const compas::RowMatrixXd> & V)
1323
{
14-
mesh.collect_garbage();
24+
DT dt;
1525

16-
// Result
17-
std::tuple<compas::RowMatrixXd, compas::RowMatrixXi> R = compas::mesh_to_vertices_and_faces(mesh);
26+
int v = V.rows();
1827

19-
return R;
20-
};
28+
std::vector< std::pair<Point, unsigned> > points(v);
29+
// std::vector< Vertex > vertex_handles(v);
30+
31+
for (int i=0; i < v; i++) {
32+
points[i] = std::make_pair(Point(V(i, 0), V(i, 1)), i);
33+
}
34+
35+
cdt.insert(points.begin(), points.end());
36+
37+
// for (int i=0; i < v; i++) {
38+
// std::pair<Point, unsigned> point;
39+
// point = std::make_pair(Point(V(i, 0), V(i, 1)), i);
40+
// vertex_handles[i] = cdt.insert(point);
41+
// }
42+
43+
for (int i=0; i < e; i++) {
44+
cdt.insert_constraint(vertex_handles[E(i, 0)], vertex_handles[E(i, 1)]);
45+
}
46+
47+
// int cdt_v = cdt.number_of_vertices();
48+
int cdt_f = cdt.number_of_faces();
49+
50+
compas::RowMatrixXi RF(cdt_f, 3);
51+
52+
// int i = 0;
53+
// for (auto point = cdt.points_begin(); point != cdt.points_end(); point++) {
54+
// RV(i, 0) = point->hx();
55+
// RV(i, 1) = point->hy();
56+
// i++;
57+
// }
58+
59+
int j = 0;
60+
for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); fit++) {
61+
CDT::Face_handle face = fit;
62+
RF(j, 0) = face->vertex(0)->info();
63+
RF(j, 1) = face->vertex(1)->info();
64+
RF(j, 2) = face->vertex(2)->info();
65+
j++;
66+
}
67+
68+
// std::tuple<compas::RowMatrixXd, compas::RowMatrixXi> result = std::make_tuple(RV, RF);
69+
return RF;
70+
}
71+
72+
void
73+
pmp_constrained_delaunay_triangulation(
74+
Eigen::Ref<const compas::RowMatrixXd> & V,
75+
Eigen::Ref<const compas::RowMatrixXi> & E)
76+
{
77+
// CDT cdt;
2178

22-
// void init_meshing(py::module & m) {
23-
// py::module submodule = m.def_submodule("meshing");
24-
25-
// submodule.def(
26-
// "remesh",
27-
// &pmp_remesh,
28-
// py::arg("V").noconvert(),
29-
// py::arg("F").noconvert(),
30-
// py::arg("target_edge_length"),
31-
// py::arg("number_of_iterations") = 10,
32-
// py::arg("do_project") = true
33-
// );
34-
// };
79+
// int v = V.rows();
80+
// int e = E.rows();
81+
82+
// std::vector< std::pair<Point, unsigned> > points(v);
83+
// // std::vector< Vertex > vertex_handles(v);
84+
85+
// for (int i=0; i < v; i++) {
86+
// points[i] = std::make_pair(Point(V(i, 0), V(i, 1)), i);
87+
// }
88+
89+
// cdt.insert(points.begin(), points.end());
90+
91+
// // for (int i=0; i < v; i++) {
92+
// // std::pair<Point, unsigned> point;
93+
// // point = std::make_pair(Point(V(i, 0), V(i, 1)), i);
94+
// // vertex_handles[i] = cdt.insert(point);
95+
// // }
96+
97+
// for (int i=0; i < e; i++) {
98+
// cdt.insert_constraint(vertex_handles[E(i, 0)], vertex_handles[E(i, 1)]);
99+
// }
100+
101+
// // int cdt_v = cdt.number_of_vertices();
102+
// int cdt_f = cdt.number_of_faces();
103+
104+
// compas::RowMatrixXi RF(cdt_f, 3);
105+
106+
// // int i = 0;
107+
// // for (auto point = cdt.points_begin(); point != cdt.points_end(); point++) {
108+
// // RV(i, 0) = point->hx();
109+
// // RV(i, 1) = point->hy();
110+
// // i++;
111+
// // }
112+
113+
// int j = 0;
114+
// for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); fit++) {
115+
// CDT::Face_handle face = fit;
116+
// RF(j, 0) = face->vertex(0)->info();
117+
// RF(j, 1) = face->vertex(1)->info();
118+
// RF(j, 2) = face->vertex(2)->info();
119+
// j++;
120+
// }
121+
122+
// // std::tuple<compas::RowMatrixXd, compas::RowMatrixXi> result = std::make_tuple(RV, RF);
123+
// return RF;
124+
}
125+
126+
void init_triangulations(py::module & m) {
127+
py::module submodule = m.def_submodule("triangulations");
128+
129+
submodule.def(
130+
"constrained_delaunay_triangulation",
131+
&pmp_constrained_delaunay_triangulation,
132+
py::arg("V").noconvert(),
133+
py::arg("E").noconvert()
134+
);
135+
};

src/triangulations.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33

44
#include <compas.h>
55

6-
std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
7-
pmp_tetrahedralisation(
8-
Eigen::Ref<const compas::RowMatrixXd> & Points);
6+
7+
compas::RowMatrixXi
8+
pmp_delaunay_triangulation(
9+
Eigen::Ref<const compas::RowMatrixXd> & V);
10+
11+
12+
compas::RowMatrixXi
13+
pmp_constrained_delaunay_triangulation(
14+
Eigen::Ref<const compas::RowMatrixXd> & V,
15+
Eigen::Ref<const compas::RowMatrixXi> & E);
916

1017
#endif /* COMPAS_TRIANGULATIONS_H */

0 commit comments

Comments
 (0)