Skip to content

Commit 1865d67

Browse files
authored
Merge pull request #62 from LIHPC-Computational-Geometry/61-quad-action-validity
61 quad action validity
2 parents 979e831 + e19108c commit 1865d67

File tree

20 files changed

+864
-192
lines changed

20 files changed

+864
-192
lines changed

environment/actions/quadrangular_actions.py

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

33
from mesh_model.mesh_struct.mesh import Mesh
44
from mesh_model.mesh_struct.mesh_elements import Node
5-
from mesh_model.mesh_analysis.global_mesh_analysis import adjacent_darts, degree, mesh_check
6-
from mesh_model.mesh_analysis.quadmesh_analysis import isFlipOk, isCollapseOk, isSplitOk, isCleanupOk
5+
from mesh_model.mesh_analysis.global_mesh_analysis import adjacent_darts, degree, mesh_check, on_boundary
6+
from mesh_model.mesh_analysis.quadmesh_analysis import isFlipCCWOk, isFlipCWOk, isCollapseOk, isSplitOk, isCleanupOk
77

88

9-
def flip_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:
10-
return flip_edge(mesh, Node(mesh, id1), Node(mesh, id2))
9+
def flip_edge_cntcw_ids(mesh: Mesh, id1: int, id2: int) -> True:
10+
return flip_edge_cntcw(mesh, Node(mesh, id1), Node(mesh, id2))
1111

1212

13-
def flip_edge(mesh: Mesh, n1: Node, n2: Node) -> (True, True, True):
13+
def flip_edge_cntcw(mesh: Mesh, n1: Node, n2: Node) -> (True, True, True):
1414
found, d = mesh.find_inner_edge(n1, n2)
1515

1616
if found:
17-
topo, geo = isFlipOk(d)
17+
topo, geo = isFlipCCWOk(d)
1818
if not geo or not topo:
1919
return False, topo, geo
2020
else:
@@ -52,6 +52,53 @@ def flip_edge(mesh: Mesh, n1: Node, n2: Node) -> (True, True, True):
5252

5353
return True, topo, geo
5454

55+
def flip_edge_cw_ids(mesh: Mesh, id1: int, id2: int) -> True:
56+
return flip_edge_cw(mesh, Node(mesh, id1), Node(mesh, id2))
57+
58+
59+
def flip_edge_cw(mesh: Mesh, n1: Node, n2: Node) -> (True, True, True):
60+
found, d = mesh.find_inner_edge(n1, n2)
61+
62+
if found:
63+
topo, geo = isFlipCWOk(d)
64+
if not geo or not topo:
65+
return False, topo, geo
66+
else:
67+
return False, False, False
68+
69+
d2, d1, d11, d111, d21, d211, d2111, n1, n2, n3, n4, n5, n6 = mesh.active_quadrangles(d)
70+
71+
f1 = d.get_face()
72+
f2 = d2.get_face()
73+
74+
# Update beta 1
75+
d.set_beta(1, d2111)
76+
d2111.set_beta(1, d1)
77+
d11.set_beta(1, d)
78+
79+
d111.set_beta(1, d21)
80+
d211.set_beta(1, d2)
81+
d2.set_beta(1, d111)
82+
83+
84+
if n1.get_dart().id == d.id:
85+
n1.set_dart(d21)
86+
if n2.get_dart().id == d2.id:
87+
n2.set_dart(d1)
88+
89+
if f1.get_dart().id == d111.id:
90+
f1.set_dart(d)
91+
92+
if f2.get_dart().id == d2111.id:
93+
f2.set_dart(d2)
94+
95+
d.set_node(n4)
96+
d2.set_node(n6)
97+
d2111.set_face(f1)
98+
d111.set_face(f2)
99+
100+
return True, topo, geo
101+
55102

56103
def split_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:
57104
return split_edge(mesh, Node(mesh, id1), Node(mesh, id2))
@@ -107,7 +154,8 @@ def collapse_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
107154
d112 = d11.get_beta(2)
108155

109156
# Move n3 node in the middle of [n3, n1]
110-
n3.set_xy((n3.x() + n1.x()) / 2, (n1.y() + n3.y()) / 2)
157+
if not on_boundary(n3):
158+
n3.set_xy((n3.x() + n1.x()) / 2, (n1.y() + n3.y()) / 2)
111159

112160
#Delete the face F5
113161
f5 = d.get_face()
@@ -120,6 +168,7 @@ def collapse_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
120168
for d in adj_darts:
121169
if d.get_node() == n_from:
122170
d.set_node(n_to)
171+
123172
mesh.del_node(n_from)
124173

125174
#Update beta2 relations

environment/environment_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"env_name": "Quadmesh-v0",
33
"mesh_size": 16,
4-
"max_episode_steps": 30,
4+
"max_episode_steps": 20,
55
"n_darts_selected": 10,
66
"deep": 8,
77
"action_restriction": false,

environment/gymnasium_envs/quadmesh_env/envs/quadmesh.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
from mesh_model.mesh_analysis.global_mesh_analysis import global_score
1010
from mesh_model.mesh_analysis.quadmesh_analysis import isTruncated
1111
from environment.gymnasium_envs.quadmesh_env.envs.mesh_conv import get_x
12-
from environment.actions.quadrangular_actions import flip_edge, split_edge, collapse_edge, cleanup_edge
12+
from environment.actions.quadrangular_actions import flip_edge_cntcw, flip_edge_cw, split_edge, collapse_edge, cleanup_edge
1313
from environment.observation_register import ObservationRegistry
1414

1515

1616
class Actions(Enum):
17-
FLIP = 0
18-
SPLIT = 1
19-
COLLAPSE = 2
20-
CLEANUP = 3
17+
FLIP_CW = 0
18+
FLIP_CNTCW = 1
19+
SPLIT = 2
20+
COLLAPSE = 3
21+
CLEANUP = 4
2122

2223

2324
class QuadMeshEnv(gym.Env):
@@ -45,17 +46,17 @@ def __init__(self, mesh=None, max_episode_steps=30, n_darts_selected=20, deep=6,
4546
self.darts_selected = [] # darts id observed
4647
self.deep = deep*2 if self.degree_observation else deep
4748
self.observation_space = spaces.Box(
48-
low=-6, # nodes min degree : -15
49-
high=2, # nodes max degree : 15
49+
low=-6, # nodes min degree : -6
50+
high=2, # nodes max degree : 2
5051
shape=(self.n_darts_selected, deep),
5152
dtype=np.int64
5253
)
5354
self.observation_count = ObservationRegistry(self.n_darts_selected, self.deep, -6, 2)
5455

5556
self.observation = None
5657

57-
# We have 4 actions, flip, split, collapse, cleanup
58-
self.action_space = spaces.MultiDiscrete([3, self.n_darts_selected])
58+
# We have 4 actions, flip clockwise, flip counterclockwise, split, collapse, cleanup
59+
self.action_space = spaces.MultiDiscrete([4, self.n_darts_selected])
5960

6061

6162
def reset(self, seed=None, options=None):
@@ -96,11 +97,12 @@ def _get_info(self, terminated, valid_act, action, mesh_reward):
9697
"valid_action": 1.0 if valid_action else 0.0,
9798
"invalid_topo": 1.0 if not valid_topo else 0.0,
9899
"invalid_geo": 1.0 if not valid_geo else 0.0,
99-
"flip": 1.0 if action[0]==Actions.FLIP.value else 0.0,
100+
"flip_cw": 1.0 if action[0]==Actions.FLIP_CW.value else 0.0,
101+
"flip_cntcw": 1.0 if action[0]==Actions.FLIP_CNTCW.value else 0.0,
100102
"split": 1.0 if action[0]==Actions.SPLIT.value else 0.0,
101103
"collapse": 1.0 if action[0]==Actions.COLLAPSE.value else 0.0,
102104
"cleanup": 1.0 if action[0]==Actions.CLEANUP.value else 0.0,
103-
"invalid_flip": 1.0 if action[0]==Actions.FLIP.value and not valid_action else 0.0,
105+
"invalid_flip": 1.0 if (action[0]==Actions.FLIP_CW.value or action[0]==Actions.FLIP_CNTCW.value) and not valid_action else 0.0,
104106
"invalid_split": 1.0 if action[0]==Actions.SPLIT.value and not valid_action else 0.0,
105107
"invalid_collapse": 1.0 if action[0]==Actions.COLLAPSE.value and not valid_action else 0.0,
106108
"invalid_cleanup": 1.0 if action[0]==Actions.CLEANUP.value and not valid_action else 0.0,
@@ -125,8 +127,10 @@ def step(self, action: np.ndarray):
125127
n2 = d1.get_node()
126128
valid_action, valid_topo, valid_geo = False, False, False
127129

128-
if action[0] == Actions.FLIP.value:
129-
valid_action, valid_topo, valid_geo = flip_edge(self.mesh, n1, n2)
130+
if action[0] == Actions.FLIP_CW.value:
131+
valid_action, valid_topo, valid_geo = flip_edge_cw(self.mesh, n1, n2)
132+
elif action[0] == Actions.FLIP_CNTCW.value:
133+
valid_action, valid_topo, valid_geo = flip_edge_cntcw(self.mesh, n1, n2)
130134
elif action[0] == Actions.SPLIT.value:
131135
valid_action, valid_topo, valid_geo = split_edge(self.mesh, n1, n2)
132136
elif action[0] == Actions.COLLAPSE.value:

main.py

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

33
from user_game import user_game
4-
from training.train_quadmesh import train
4+
from training.train import train
55
from training.exploit import exploit
66
#from mesh_model.reader import read_gmsh
77
#from mesh_display import MeshDisplay

mesh_files/medium_quad.msh

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
$MeshFormat
2+
4.1 0 8
3+
$EndMeshFormat
4+
$Entities
5+
4 4 1 0
6+
1 4.9999998 -4.9999998 5.551114901081e-16 0
7+
2 4.9999998 4.9999998 5.551114901081e-16 0
8+
3 -4.9999998 4.9999998 -5.551114901081e-16 0
9+
4 -4.9999998 -4.9999998 -5.551114901081e-16 0
10+
1 4.9999997 -4.999999900000001 -9.999999944488851e-08 4.999999900000001 4.999999900000001 1.000000005551115e-07 0 2 1 -2
11+
2 -4.999999900000001 4.9999997 -1.000000005551115e-07 4.999999900000001 4.999999900000001 1.000000005551115e-07 0 2 3 -2
12+
3 -4.999999900000001 -4.999999900000001 -1.000000005551115e-07 -4.9999997 4.999999900000001 9.999999944488851e-08 0 2 4 -3
13+
4 -4.999999900000001 -4.999999900000001 -1.000000005551115e-07 4.999999900000001 -4.9999997 1.000000005551115e-07 0 2 4 -1
14+
1 -4.999999900000001 -4.999999900000001 -1.000000005551115e-07 4.999999900000001 4.999999900000001 1.000000005551115e-07 0 4 -4 3 2 -1
15+
$EndEntities
16+
$Nodes
17+
9 27 1 27
18+
0 1 0 1
19+
1
20+
4.9999998 -4.9999998 5.551114901081e-16
21+
0 2 0 1
22+
2
23+
4.9999998 4.9999998 5.551114901081e-16
24+
0 3 0 1
25+
3
26+
-4.9999998 4.9999998 -5.551114901081e-16
27+
0 4 0 1
28+
4
29+
-4.9999998 -4.9999998 -5.551114901081e-16
30+
1 1 0 3
31+
5
32+
6
33+
7
34+
4.9999998 -2.4999999 5.551114901081e-16
35+
4.9999998 8.881784197001252e-16 5.551114901081e-16
36+
4.9999998 2.499999900000001 5.551114901081e-16
37+
1 2 0 3
38+
8
39+
9
40+
10
41+
-2.4999999 4.9999998 -2.7755574505405e-16
42+
8.881784197001252e-16 4.9999998 9.860761315262648e-32
43+
2.499999900000001 4.9999998 2.775557450540501e-16
44+
1 3 0 3
45+
11
46+
12
47+
13
48+
-4.9999998 -2.4999999 -5.551114901081e-16
49+
-4.9999998 8.881784197001252e-16 -5.551114901081e-16
50+
-4.9999998 2.499999900000001 -5.551114901081e-16
51+
1 4 0 3
52+
14
53+
15
54+
16
55+
-2.4999999 -4.9999998 -2.7755574505405e-16
56+
8.881784197001252e-16 -4.9999998 9.860761315262648e-32
57+
2.499999900000001 -4.9999998 2.775557450540501e-16
58+
2 1 0 11
59+
17
60+
18
61+
19
62+
20
63+
21
64+
22
65+
23
66+
24
67+
25
68+
26
69+
27
70+
2.499999900000001 2.499999900000001 2.775557450540501e-16
71+
-2.4999999 2.4999999 -2.7755574505405e-16
72+
0 8.881784197001252e-16 0
73+
2.4999999 -2.4999999 2.7755574505405e-16
74+
-2.4999999 -2.4999999 -2.7755574505405e-16
75+
8.881783841729886e-16 1.666666600000001 9.860760920831879e-32
76+
3.333333200000001 -3.3333332 3.700743267387334e-16
77+
-3.3333332 -3.3333332 -3.700743267387333e-16
78+
0 -1.6666666 0
79+
-3.3333332 3.333333200000001 -3.700743267387333e-16
80+
3.333333200000001 3.333333200000001 3.700743267387334e-16
81+
$EndNodes
82+
$Elements
83+
9 38 1 38
84+
0 1 15 1
85+
1 1
86+
0 2 15 1
87+
2 2
88+
0 3 15 1
89+
3 3
90+
0 4 15 1
91+
4 4
92+
1 1 1 4
93+
5 1 5
94+
6 5 6
95+
7 6 7
96+
8 7 2
97+
1 2 1 4
98+
9 3 8
99+
10 8 9
100+
11 9 10
101+
12 10 2
102+
1 3 1 4
103+
13 4 11
104+
14 11 12
105+
15 12 13
106+
16 13 3
107+
1 4 1 4
108+
17 4 14
109+
18 14 15
110+
19 15 16
111+
20 16 1
112+
2 1 3 18
113+
21 6 17 22 19
114+
22 17 9 18 22
115+
23 19 22 18 12
116+
24 6 20 23 5
117+
25 20 15 16 23
118+
26 5 23 16 1
119+
27 15 21 24 14
120+
28 21 12 11 24
121+
29 14 24 11 4
122+
30 6 19 25 20
123+
31 19 12 21 25
124+
32 20 25 21 15
125+
33 12 18 26 13
126+
34 18 9 8 26
127+
35 13 26 8 3
128+
36 9 17 27 10
129+
37 17 6 7 27
130+
38 10 27 7 2
131+
$EndElements

mesh_files/simple_quad.msh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
$MeshFormat
2+
4.1 0 8
3+
$EndMeshFormat
4+
$Entities
5+
4 4 1 0
6+
1 0 0 0 0
7+
2 1 0 0 0
8+
3 1 1 0 0
9+
4 0 1 0 0
10+
1 -9.999999994736442e-08 -1e-07 -1e-07 1.0000001 1e-07 1e-07 0 2 1 -2
11+
2 0.9999999000000001 -9.999999994736442e-08 -1e-07 1.0000001 1.0000001 1e-07 0 2 2 -3
12+
3 -9.999999994736442e-08 0.9999999000000001 -1e-07 1.0000001 1.0000001 1e-07 0 2 3 -4
13+
4 -1e-07 -9.999999994736442e-08 -1e-07 1e-07 1.0000001 1e-07 0 2 4 -1
14+
1 -9.999999994736442e-08 -9.999999994736442e-08 -1e-07 1.0000001 1.0000001 1e-07 0 4 1 2 3 4
15+
$EndEntities
16+
$Nodes
17+
9 11 1 11
18+
0 1 0 1
19+
1
20+
0 0 0
21+
0 2 0 1
22+
2
23+
1 0 0
24+
0 3 0 1
25+
3
26+
1 1 0
27+
0 4 0 1
28+
4
29+
0 1 0
30+
1 1 0 1
31+
5
32+
0.4999999999999999 0 0
33+
1 2 0 1
34+
6
35+
1 0.4999999999999999 0
36+
1 3 0 1
37+
7
38+
0.5 1 0
39+
1 4 0 1
40+
8
41+
0 0.5 0
42+
2 1 0 3
43+
9
44+
10
45+
11
46+
0.5 0.5 0
47+
0.3333333333333333 0.3333333333333333 0
48+
0.6666666666666666 0.6666666666666666 0
49+
$EndNodes
50+
$Elements
51+
9 18 1 18
52+
0 1 15 1
53+
1 1
54+
0 2 15 1
55+
2 2
56+
0 3 15 1
57+
3 3
58+
0 4 15 1
59+
4 4
60+
1 1 1 2
61+
5 1 5
62+
6 5 2
63+
1 2 1 2
64+
7 2 6
65+
8 6 3
66+
1 3 1 2
67+
9 3 7
68+
10 7 4
69+
1 4 1 2
70+
11 4 8
71+
12 8 1
72+
2 1 3 6
73+
13 4 8 10 9
74+
14 8 1 5 10
75+
15 9 10 5 2
76+
16 4 9 11 7
77+
17 9 2 6 11
78+
18 7 11 6 3
79+
$EndElements

0 commit comments

Comments
 (0)