-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from nschloe/2d
2d mesh generation
- Loading branch information
Showing
10 changed files
with
215 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#define CGAL_MESH_3_VERBOSE 1 | ||
|
||
#include "generate_2d.hpp" | ||
|
||
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> | ||
#include <CGAL/Constrained_Delaunay_triangulation_2.h> | ||
#include <CGAL/Delaunay_mesher_2.h> | ||
#include <CGAL/Delaunay_mesh_face_base_2.h> | ||
#include <CGAL/Delaunay_mesh_vertex_base_2.h> | ||
#include <CGAL/Delaunay_mesh_size_criteria_2.h> | ||
#include <CGAL/lloyd_optimize_mesh_2.h> | ||
|
||
namespace pygalmesh { | ||
|
||
typedef CGAL::Exact_predicates_inexact_constructions_kernel K; | ||
typedef CGAL::Delaunay_mesh_vertex_base_2<K> Vb; | ||
typedef CGAL::Delaunay_mesh_face_base_2<K> Fb; | ||
typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds; | ||
typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds> CDT; | ||
typedef CGAL::Delaunay_mesh_size_criteria_2<CDT> Criteria; | ||
typedef CDT::Vertex_handle Vertex_handle; | ||
typedef CDT::Point Point; | ||
|
||
std::tuple<std::vector<std::array<double, 2>>, std::vector<std::array<int, 3>>> | ||
generate_2d( | ||
const std::vector<std::array<double, 2>> & points, | ||
const std::vector<std::array<int, 2>> & constraints, | ||
// See | ||
// https://doc.cgal.org/latest/Mesh_2/classCGAL_1_1Delaunay__mesh__size__criteria__2.html#a58b0186eae407ba76b8f4a3d0aa85a1a | ||
// for what the bounds mean. Spoiler: | ||
// B = circumradius / shortest_edge, | ||
// relates to the smallest angle via sin(alpha_min) = 1 / (2B) | ||
// cell_size is "size", | ||
const double max_circumradius_shortest_edge_ratio, | ||
const double cell_size, | ||
const int num_lloyd_steps | ||
) | ||
{ | ||
CDT cdt; | ||
// construct a constrained triangulation | ||
std::vector<Vertex_handle> vertices(points.size()); | ||
int k = 0; | ||
for (auto pt: points) { | ||
vertices[k] = cdt.insert(Point(pt[0], pt[1])); | ||
k++; | ||
} | ||
for (auto c: constraints) { | ||
cdt.insert_constraint(vertices[c[0]], vertices[c[1]]); | ||
} | ||
|
||
// create proper mesh | ||
CGAL::refine_Delaunay_mesh_2( | ||
cdt, | ||
Criteria( | ||
0.25 / (max_circumradius_shortest_edge_ratio * max_circumradius_shortest_edge_ratio), | ||
cell_size | ||
) | ||
); | ||
|
||
if (num_lloyd_steps > 0) { | ||
CGAL::lloyd_optimize_mesh_2( | ||
cdt, | ||
CGAL::parameters::max_iteration_number = num_lloyd_steps | ||
); | ||
} | ||
|
||
// convert points to vector of arrays | ||
std::map<Vertex_handle, int> vertex_index; | ||
std::vector<std::array<double, 2>> out_points(cdt.number_of_vertices()); | ||
k = 0; | ||
for (auto vit = cdt.vertices_begin(); vit!= cdt.vertices_end(); ++vit) { | ||
out_points[k][0] = vit->point()[0]; | ||
out_points[k][1] = vit->point()[1]; | ||
vertex_index[vit] = k; | ||
k++; | ||
} | ||
|
||
std::vector<std::array<int, 3>> out_cells(cdt.number_of_faces()); | ||
k = 0; | ||
for (auto fit = cdt.faces_begin(); fit!= cdt.faces_end(); ++fit) { | ||
out_cells[k][0] = vertex_index[fit->vertex(0)]; | ||
out_cells[k][1] = vertex_index[fit->vertex(1)]; | ||
out_cells[k][2] = vertex_index[fit->vertex(2)]; | ||
k++; | ||
} | ||
|
||
return std::make_tuple(out_points, out_cells); | ||
} | ||
|
||
} // namespace pygalmesh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef GENERATE_2D_HPP | ||
#define GENERATE_2D_HPP | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
namespace pygalmesh { | ||
|
||
std::tuple<std::vector<std::array<double, 2>>, std::vector<std::array<int, 3>>> | ||
generate_2d( | ||
const std::vector<std::array<double, 2>> & points, | ||
const std::vector<std::array<int, 2>> & constraints, | ||
const double max_circumradius_shortest_edge_ratio, | ||
const double cell_size, | ||
const int num_lloyd_steps | ||
); | ||
|
||
} // namespace pygalmesh | ||
|
||
#endif // GENERATE_2D_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import numpy | ||
|
||
import pygalmesh | ||
|
||
|
||
def test_2d(): | ||
points = numpy.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]) | ||
constraints = [[0, 1], [1, 2], [2, 3], [3, 0]] | ||
|
||
mesh = pygalmesh.generate_2d( | ||
points, constraints, cell_size=1.0e-1, num_lloyd_steps=10 | ||
) | ||
|
||
assert mesh.points.shape == (276, 2) | ||
assert mesh.get_cells_type("triangle").shape == (486, 3) | ||
|
||
# # show mesh | ||
# import matplotlib.pyplot as plt | ||
# pts = points[cells] | ||
# for pt in pts: | ||
# plt.plot([pt[0][0], pt[1][0]], [pt[0][1], pt[1][1]], "-k") | ||
# plt.plot([pt[1][0], pt[2][0]], [pt[1][1], pt[2][1]], "-k") | ||
# plt.plot([pt[2][0], pt[0][0]], [pt[2][1], pt[0][1]], "-k") | ||
# # for pt in points: | ||
# # plt.plot(pt[0], pt[1], "or") | ||
# plt.gca().set_aspect("equal") | ||
# plt.show() | ||
|
||
# mesh.points *= 100 | ||
# mesh.write("rect.svg") | ||
|
||
|
||
if __name__ == "__main__": | ||
test_2d() |