Skip to content

Commit c29a860

Browse files
committed
New test functions for quad environment.
1 parent e79d5e5 commit c29a860

File tree

15 files changed

+162
-154
lines changed

15 files changed

+162
-154
lines changed

environment/actions/quadrangular_actions.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ def split_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
8484
# update beta2 relations
8585
mesh.set_face_beta2(f5,[d111,d1112,d21,d212])
8686

87-
adj_n1 = adjacent_darts(n1)
88-
adj_n2 = adjacent_darts(N10)
89-
90-
deg1 = degree(n1)
91-
deg2 = degree(N10)
92-
9387
return True, topo, geo
9488

9589

environment/actions/triangular_actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from mesh_model.mesh_struct.mesh import Mesh
4-
from mesh_model.mesh_struct.mesh_elements import Dart, Node
4+
from mesh_model.mesh_struct.mesh_elements import Node
55
from mesh_model.mesh_analysis.global_mesh_analysis import mesh_check
66
from mesh_model.mesh_analysis.trimesh_analysis import isFlipOk, isCollapseOk, isSplitOk
77

environment/gymnasium_envs/quadmesh_env/envs/quadmesh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from mesh_model.random_quadmesh import random_mesh
88
from mesh_model.mesh_struct.mesh_elements import Dart
9-
from mesh_model.mesh_analysis.quadmesh_analysis import global_score, isTruncated
9+
from mesh_model.mesh_analysis.global_mesh_analysis import global_score
10+
from mesh_model.mesh_analysis.quadmesh_analysis import isTruncated
1011
from environment.gymnasium_envs.quadmesh_env.envs.mesh_conv import get_x
1112
from environment.actions.quadrangular_actions import flip_edge, split_edge, collapse_edge, cleanup_edge
1213
from environment.observation_register import ObservationRegistry

environment/gymnasium_envs/trimesh_flip_env/envs/trimesh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from mesh_model.random_trimesh import random_flip_mesh
77
from mesh_model.mesh_struct.mesh_elements import Dart
8-
from mesh_model.mesh_analysis.trimesh_analysis import global_score, isTruncated
8+
from mesh_model.mesh_analysis.global_mesh_analysis import global_score
9+
from mesh_model.mesh_analysis.trimesh_analysis import isTruncated
910
from environment.gymnasium_envs.trimesh_flip_env.envs.mesh_conv import get_x
1011
from environment.actions.triangular_actions import flip_edge
1112

environment/gymnasium_envs/trimesh_full_env/envs/trimesh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from mesh_model.random_trimesh import random_mesh
88
from mesh_model.mesh_struct.mesh_elements import Dart
9-
from mesh_model.mesh_analysis.trimesh_analysis import global_score, isTruncated
9+
from mesh_model.mesh_analysis.global_mesh_analysis import global_score
10+
from mesh_model.mesh_analysis.trimesh_analysis import isTruncated
1011
from environment.gymnasium_envs.trimesh_full_env.envs.mesh_conv import get_x
1112
from environment.actions.triangular_actions import flip_edge, split_edge, collapse_edge
1213

environment/trimesh_env.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
import numpy as np
3-
from mesh_model.mesh_analysis.trimesh_analysis import global_score, find_template_opposite_node
3+
from mesh_model.mesh_analysis.trimesh_analysis import find_template_opposite_node
4+
from mesh_model.mesh_analysis.global_mesh_analysis import global_score
45
from mesh_model.mesh_struct.mesh_elements import Dart
56
from mesh_model.mesh_struct.mesh import Mesh
67
from environment.actions.triangular_actions import flip_edge, split_edge, collapse_edge

mesh_display.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from mesh_model.mesh_struct.mesh import Mesh
2-
from mesh_model.mesh_analysis.trimesh_analysis import global_score
2+
from mesh_model.mesh_analysis.global_mesh_analysis import global_score
33

44

55
class MeshDisplay:

mesh_model/mesh_analysis/quadmesh_analysis.py

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,8 @@
1-
from math import sqrt, degrees, radians, cos, sin, acos
21
import numpy as np
32

43
from mesh_model.mesh_struct.mesh_elements import Dart, Node, Face
54
from mesh_model.mesh_struct.mesh import Mesh
6-
from mesh_model.mesh_analysis.global_mesh_analysis import test_degree, on_boundary, get_angle_by_coord, degree, \
7-
get_boundary_angle, adjacent_darts, adjacent_faces
8-
9-
10-
def global_score(m: Mesh):
11-
"""
12-
Calculate the overall mesh score. The mesh cannot achieve a better score than the ideal one.
13-
And the current score is the mesh score.
14-
:param m: the mesh to be analyzed
15-
:return: 4 return: a list of the nodes score, the current mesh score and the ideal mesh score, and the adjacency
16-
"""
17-
mesh_ideal_score = 0
18-
mesh_score = 0
19-
nodes_score = []
20-
nodes_adjacency = []
21-
for i in range(len(m.nodes)):
22-
if m.nodes[i, 2] >= 0:
23-
n_id = i
24-
node = Node(m, n_id)
25-
n_score, adjacency= score_calculation(node)
26-
nodes_score.append(n_score)
27-
nodes_adjacency.append(adjacency)
28-
mesh_ideal_score += n_score
29-
mesh_score += abs(n_score)
30-
else:
31-
nodes_score.append(0)
32-
nodes_adjacency.append(6)
33-
return nodes_score, mesh_score, mesh_ideal_score, nodes_adjacency
34-
35-
36-
def score_calculation(n: Node) -> (int, int):
37-
"""
38-
Function to calculate the irregularity of a node in the mesh.
39-
:param n: a node in the mesh.
40-
:return: the irregularity of the node
41-
"""
42-
adjacency = degree(n)
43-
if on_boundary(n):
44-
angle = get_boundary_angle(n)
45-
ideal_adjacency = max(round(angle/90)+1, 2)
46-
else:
47-
ideal_adjacency = 360/90
48-
49-
return ideal_adjacency-adjacency, adjacency
5+
from mesh_model.mesh_analysis.global_mesh_analysis import test_degree, on_boundary, adjacent_faces
506

517

528
def isValidAction(mesh: Mesh, dart_id: int, action: int) -> (bool, bool):
@@ -271,50 +227,7 @@ def isValidQuad(A: Node, B: Node, C: Node, D: Node):
271227
return False
272228
273229
"""
274-
if 0< signe(cp_A)+signe(cp_B)+signe(cp_C)+signe(cp_D) <2 :
230+
if 0<= signe(cp_A)+signe(cp_B)+signe(cp_C)+signe(cp_D) <2 :
275231
return True
276232
else:
277233
return False
278-
279-
280-
def orientation(p, q, r):
281-
""" Calcule l'orientation de trois points.
282-
Retourne :
283-
0 si colinéaire,
284-
1 si sens anti-horaire,
285-
2 si sens horaire.
286-
"""
287-
val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
288-
if val == 0:
289-
return 0
290-
return 1 if val > 0 else 2
291-
292-
def do_intersect(p1, q1, p2, q2):
293-
""" Vérifie si les segments [p1,q1] et [p2,q2] s'intersectent """
294-
o1 = orientation(p1, q1, p2)
295-
o2 = orientation(p1, q1, q2)
296-
o3 = orientation(p2, q2, p1)
297-
o4 = orientation(p2, q2, q1)
298-
299-
# Cas général : les orientations sont différentes
300-
if o1 != o2 and o3 != o4:
301-
return True
302-
303-
return False # Pas d'intersection
304-
305-
def is_self_intersecting_quadrilateral(A, B, C, D):
306-
""" Vérifie si le quadrilatère ABCD est croisé """
307-
return do_intersect(A, C, B, D) # Vérifie l'intersection des diagonales
308-
309-
310-
"""
311-
d12 = d1.get_beta(2)
312-
d112 = d11.get_beta(2)
313-
d1112 = d111.get_beta(2)
314-
n7 = ((d1112.get_beta(1)).get_beta(1)).get_node()
315-
n8 = ((d112.get_beta(1)).get_beta(1)).get_node()
316-
n9 = (((d112.get_beta(1)).get_beta(1)).get_beta(1)).get_node()
317-
n11 = ((d12.get_beta(1)).get_beta(1)).get_node()
318-
n10 = mesh.add_node((n1.x() + n3.x()) / 2, (n1.y() + n3.y()) / 2)
319-
topo = isValidQuad(n10, n5, n6, n2) and isValidQuad(n4, n10, n2, n3) and isValidQuad(n10, n5, n6, n2)
320-
"""

mesh_model/mesh_analysis/trimesh_analysis.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,10 @@
1-
from math import sqrt, degrees, radians, cos, sin, acos
2-
import numpy as np
1+
from math import sqrt, degrees, radians, cos, sin
32

43
from mesh_model.mesh_struct.mesh_elements import Dart, Node, Face
54
from mesh_model.mesh_struct.mesh import Mesh
65
from mesh_model.mesh_analysis.global_mesh_analysis import get_boundary_darts, test_degree, on_boundary, get_angle_by_coord, angle_from_sides, degree, get_boundary_angle
76

87

9-
def global_score(m: Mesh):
10-
"""
11-
Calculate the overall mesh score. The mesh cannot achieve a better score than the ideal one.
12-
And the current score is the mesh score.
13-
:param m: the mesh to be analyzed
14-
:return: 4 return: a list of the nodes score, the current mesh score and the ideal mesh score, and the adjacency
15-
"""
16-
mesh_ideal_score = 0
17-
mesh_score = 0
18-
nodes_score = []
19-
nodes_adjacency = []
20-
for i in range(len(m.nodes)):
21-
if m.nodes[i, 2] >= 0:
22-
n_id = i
23-
node = Node(m, n_id)
24-
n_score, adjacency= score_calculation(node)
25-
nodes_score.append(n_score)
26-
nodes_adjacency.append(adjacency)
27-
mesh_ideal_score += n_score
28-
mesh_score += abs(n_score)
29-
else:
30-
nodes_score.append(0)
31-
nodes_adjacency.append(6)
32-
return nodes_score, mesh_score, mesh_ideal_score, nodes_adjacency
33-
34-
35-
def score_calculation(n: Node) -> (int, int):
36-
"""
37-
Function to calculate the irregularity of a node in the mesh.
38-
:param n: a node in the mesh.
39-
:return: the irregularity of the node
40-
"""
41-
adjacency = degree(n)
42-
if on_boundary(n):
43-
angle = get_boundary_angle(n)
44-
ideal_adjacency = max(round(angle/60)+1, 2)
45-
else:
46-
ideal_adjacency = 360/60
47-
48-
return ideal_adjacency-adjacency, adjacency
49-
50-
518
def isValidAction(mesh: Mesh, dart_id: int, action: int) -> (bool, bool):
529
flip = 0
5310
split = 1

test_modules/test_actions_quad.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ def test_flip(self):
3333
self.assertEqual(d2.get_node(), n10)
3434

3535

36-
flip_edge(cmap, n11, n10)
36+
self.assertEqual(flip_edge(cmap, n11, n10), (True,True,True))
3737
self.assertEqual(2, cmap.nb_faces())
3838
self.assertEqual(6, cmap.nb_nodes())
3939
plot_mesh(cmap)
40+
self.assertFalse(flip_edge(cmap, n11, n10)[0])
4041

4142
def test_split(self):
4243
cmap = mesh.Mesh()
@@ -56,8 +57,13 @@ def test_split(self):
5657
q4 = cmap.add_quad(n01, n11, n12, n02)
5758
cmap.set_twin_pointers()
5859
plot_mesh(cmap)
59-
split_edge(cmap, n11, n21)
60+
found, d = cmap.find_inner_edge(n11, n21)
61+
self.assertTrue(found)
62+
self.assertEqual(split_edge(cmap, n11, n21), (True,True,True))
63+
self.assertEqual(10, cmap.nb_nodes())
64+
self.assertEqual(5, cmap.nb_faces())
6065
plot_mesh(cmap)
66+
self.assertFalse(split_edge(cmap, n20, n21)[0])
6167

6268
def test_collapse(self):
6369
cmap = mesh.Mesh()
@@ -79,8 +85,14 @@ def test_collapse(self):
7985
q5 = cmap.add_quad(n051, n10, n151, n12)
8086
cmap.set_twin_pointers()
8187
plot_mesh(cmap)
82-
collapse_edge(cmap, n151, n12)
88+
found, d = cmap.find_inner_edge(n151, n12)
89+
self.assertTrue(found)
8390
plot_mesh(cmap)
91+
self.assertEqual(collapse_edge(cmap, n151, n12), (True, True, True))
92+
self.assertEqual(9, cmap.nb_nodes())
93+
self.assertEqual(4, cmap.nb_faces())
94+
plot_mesh(cmap)
95+
self.assertFalse(split_edge(cmap, n20, n21)[0])
8496

8597
def test_cleanup(self):
8698
cmap = mesh.Mesh()
@@ -102,7 +114,11 @@ def test_cleanup(self):
102114
q5 = cmap.add_quad(n051, n10, n151, n12)
103115
cmap.set_twin_pointers()
104116
plot_mesh(cmap)
105-
cleanup_edge(cmap, n151, n21)
117+
found, d = cmap.find_inner_edge(n151, n12)
118+
self.assertTrue(found)
119+
self.assertEqual(cleanup_edge(cmap, n151, n21), (True, True, True))
120+
self.assertEqual(7, cmap.nb_nodes())
121+
self.assertEqual(3, cmap.nb_faces())
106122
plot_mesh(cmap)
107123

108124

@@ -113,12 +129,12 @@ def test_actions(self):
113129
d = Dart(cmap, 14)
114130
n1= d.get_node()
115131
n2 = (d.get_beta(1)).get_node()
116-
collapse_edge(cmap, n1, n2)
132+
self.assertEqual(collapse_edge(cmap, n1, n2), (True,True,True))
117133
plot_mesh(cmap)
118134
d = Dart(cmap, 32)
119135
n1 = d.get_node()
120136
n2 = (d.get_beta(1)).get_node()
121-
flip_edge(cmap, n1, n2)
137+
self.assertEqual(flip_edge(cmap, n1, n2), (True,True,True))
122138

123139
plot_mesh(cmap)
124140

0 commit comments

Comments
 (0)