Skip to content

Commit 8abb8b1

Browse files
authored
Merge pull request #120 from nschloe/degen-cells-2d
Fix degenerate cells in 2D
2 parents d8bf3b5 + 0705259 commit 8abb8b1

5 files changed

Lines changed: 40 additions & 10 deletions

File tree

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pygalmesh
3-
version = 0.8.1
3+
version = 0.8.2
44
url = https://github.com/nschloe/pygalmesh
55
author = Nico Schlömer
66
author_email = nico.schloemer@gmail.com

src/generate_2d.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,22 @@ generate_2d(
6868
std::map<Vertex_handle, int> vertex_index;
6969
std::vector<std::array<double, 2>> out_points(cdt.number_of_vertices());
7070
k = 0;
71-
for (auto vit = cdt.vertices_begin(); vit!= cdt.vertices_end(); ++vit) {
71+
for (auto vit: cdt.finite_vertex_handles()) {
7272
out_points[k][0] = vit->point()[0];
7373
out_points[k][1] = vit->point()[1];
7474
vertex_index[vit] = k;
7575
k++;
7676
}
7777

78-
std::vector<std::array<int, 3>> out_cells(cdt.number_of_faces());
78+
// https://github.com/CGAL/cgal/issues/5068#issuecomment-706213606
79+
auto nb_faces = 0u;
80+
for (auto fit: cdt.finite_face_handles()) {
81+
if(fit->is_in_domain()) ++nb_faces;
82+
}
83+
std::vector<std::array<int, 3>> out_cells(nb_faces);
7984
k = 0;
80-
for (auto fit = cdt.faces_begin(); fit!= cdt.faces_end(); ++fit) {
85+
for (auto fit: cdt.finite_face_handles()) {
86+
if(!fit->is_in_domain()) continue;
8187
out_cells[k][0] = vertex_index[fit->vertex(0)];
8288
out_cells[k][1] = vertex_index[fit->vertex(1)];
8389
out_cells[k][2] = vertex_index[fit->vertex(2)];

test/helpers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ def compute_triangle_areas(vertices, triangles):
2424
e0 = vertices[triangles[:, 1]] - vertices[triangles[:, 0]]
2525
e1 = vertices[triangles[:, 2]] - vertices[triangles[:, 1]]
2626

27-
e0_cross_e1 = numpy.cross(e0, e1)
28-
areas = 0.5 * numpy.sqrt(_row_dot(e0_cross_e1, e0_cross_e1))
29-
30-
return areas
27+
assert e0.shape == e1.shape
28+
if e0.shape[1] == 2:
29+
z_component_of_e0_cross_e1 = numpy.cross(e0, e1)
30+
cross_magnitude = z_component_of_e0_cross_e1
31+
else:
32+
assert e0.shape[1] == 3
33+
e0_cross_e1 = numpy.cross(e0, e1)
34+
cross_magnitude = numpy.sqrt(_row_dot(e0_cross_e1, e0_cross_e1))
35+
36+
return 0.5 * cross_magnitude

test/test_2d.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import pygalmesh
44

5+
from helpers import compute_triangle_areas
56

6-
def test_2d():
7+
8+
def test_rectangle():
79
points = numpy.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])
810
constraints = [[0, 1], [1, 2], [2, 3], [3, 0]]
911

@@ -30,5 +32,20 @@ def test_2d():
3032
# mesh.write("rect.svg")
3133

3234

35+
def test_disk():
36+
h = 0.1
37+
n = int(2 * numpy.pi / h)
38+
points = numpy.array(
39+
[
40+
[numpy.cos(alpha), numpy.sin(alpha)]
41+
for alpha in numpy.linspace(0.0, 2 * numpy.pi, n + 1, endpoint=False)
42+
]
43+
)
44+
constraints = [[k, k + 1] for k in range(n)] + [[n, 0]]
45+
mesh = pygalmesh.generate_2d(points, constraints, edge_size=h, num_lloyd_steps=0)
46+
areas = compute_triangle_areas(mesh.points, mesh.get_cells_type("triangle"))
47+
assert numpy.all(areas > 1.0e-5)
48+
49+
3350
if __name__ == "__main__":
34-
test_2d()
51+
test_disk()

test/test_remesh_surface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_inr():
5252
assert abs(max(mesh.points[:, 2]) - ref[4]) < tol * abs(ref[4])
5353
assert abs(min(mesh.points[:, 2]) - ref[5]) < tol * abs(ref[5])
5454

55+
print(helpers.compute_triangle_areas(mesh.points, mesh.get_cells_type("triangle")))
5556
vol = sum(
5657
helpers.compute_triangle_areas(mesh.points, mesh.get_cells_type("triangle"))
5758
)

0 commit comments

Comments
 (0)