Skip to content

Commit

Permalink
Merge pull request #58 from LIHPC-Computational-Geometry/57-gymnasium…
Browse files Browse the repository at this point in the history
…-env

57 gymnasium env
  • Loading branch information
ArzhelaR authored Jan 10, 2025
2 parents 620e9df + d548165 commit fbecd6c
Show file tree
Hide file tree
Showing 79 changed files with 1,568 additions and 79 deletions.
35 changes: 23 additions & 12 deletions actions/triangular_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from mesh_model.mesh_struct.mesh import Mesh
from mesh_model.mesh_struct.mesh_elements import Dart, Node
from mesh_model.mesh_analysis import isFlipOk, isCollapseOk, adjacent_darts, isSplitOk
from mesh_model.mesh_analysis import isFlipOk, isCollapseOk, isSplitOk


def flip_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:
Expand All @@ -11,9 +11,12 @@ def flip_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:

def flip_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
found, d = mesh.find_inner_edge(n1, n2)

if not found or not isFlipOk(d):
return False
if found:
topo, geo = isFlipOk(d)
if not geo or not topo:
return False, topo, geo
else:
return False, False, False

d2, d1, d11, d21, d211, n1, n2, n3, n4 = mesh.active_triangles(d)

Expand Down Expand Up @@ -42,7 +45,7 @@ def flip_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
d2.set_node(n4)
d211.set_face(f1)
d11.set_face(f2)
return True
return True, topo, geo


def split_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:
Expand All @@ -51,8 +54,13 @@ def split_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:

def split_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
found, d = mesh.find_inner_edge(n1, n2)
if not found or not isSplitOk(d):
return False

if found:
topo, geo = isSplitOk(d)
if not geo or not topo:
return False, topo, geo
else:
return False, True, False

d2, d1, _, d21, _, n1, n2, n3, n4 = mesh.active_triangles(d)

Expand All @@ -78,7 +86,7 @@ def split_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
db2 = d.get_beta(2)
db21 = db2.get_beta(1)
mesh.set_beta2(db21)
return True
return True, topo, geo


def collapse_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:
Expand All @@ -87,9 +95,12 @@ def collapse_edge_ids(mesh: Mesh, id1: int, id2: int) -> True:

def collapse_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
found, d = mesh.find_inner_edge(n1, n2)

if not found or not isCollapseOk(d):
return False
if found:
topo, geo = isCollapseOk(d)
if not geo or not topo:
return False, topo, geo
else:
return False, False, False

_, d1, d11, d21, d211, n1, n2, _, _ = mesh.active_triangles(d)

Expand Down Expand Up @@ -145,7 +156,7 @@ def collapse_edge(mesh: Mesh, n1: Node, n2: Node) -> True:
#delete n2 node
mesh.del_node(n2)

return mesh_check(mesh)
return mesh_check(mesh), topo, geo


def check_beta2_relation(mesh: Mesh) -> bool:
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions environment/gymnasium_envs/trimesh_flip_env/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from gymnasium.envs.registration import register
from environment.gymnasium_envs.trimesh_flip_env.envs.trimesh import TriMeshEnvFlip

register(
id="TrimeshFlip-v0",
entry_point="environment.gymnasium_envs.trimesh_flip_env.envs:TriMeshEnvFlip",
max_episode_steps=100,
kwargs={"mesh": None, "mesh_size": 30, "n_darts_selected": 20, "deep": 6, "with_degree_obs": True, "action_restriction": False},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from environment.gymnasium_envs.trimesh_flip_env.envs.trimesh import TriMeshEnvFlip
183 changes: 183 additions & 0 deletions environment/gymnasium_envs/trimesh_flip_env/envs/mesh_conv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import numpy as np
from mesh_model.mesh_analysis import isValidAction, find_template_opposite_node
from mesh_model.mesh_struct.mesh_elements import Dart
from mesh_model.mesh_struct.mesh import Mesh


def get_x(state: Mesh, n_darts_selected: int, deep :int, degree: bool, restricted:bool, nodes_scores: list[int], nodes_adjacency: list[int]):
mesh = state
if degree:
template, darts_id = get_template_deg(mesh, deep, nodes_scores, nodes_adjacency)
else:
template, darts_id = get_template(mesh, deep, nodes_scores)

if restricted:
darts_to_delete = []
darts_id = []
for i, d_info in enumerate(mesh.active_darts()):
d_id = d_info[0]
if d_info[2] == -1 or not isValidAction(mesh, d_info[0], 0)[0]: # test the validity of all action type
darts_to_delete.append(i)
else:
darts_id.append(d_id)
valid_template = np.delete(template, darts_to_delete, axis=0)
else:
valid_template = template
score_sum = np.sum(np.abs(valid_template[:,:deep]), axis=1)
indices_selected_darts = np.argsort(score_sum)[-n_darts_selected:][::-1]
valid_dart_ids = [darts_id[i] for i in indices_selected_darts]
X = valid_template[indices_selected_darts, :]
return X, np.array(valid_dart_ids)


def get_template(mesh: Mesh, deep: int, nodes_scores):
size = len(mesh.dart_info)
template = np.zeros((size, deep), dtype=np.int64)
dart_ids = []
n_darts = 0

for d_info in mesh.active_darts():
n_darts += 1
d_id = d_info[0]
dart_ids.append(d_id)
d = Dart(mesh, d_id)
A = d.get_node()
d1 = d.get_beta(1)
B = d1.get_node()
d11 = d1.get_beta(1)
C = d11.get_node()

# Template niveau 1
template[n_darts - 1, 0] = nodes_scores[C.id]
template[n_darts - 1, 1] = nodes_scores[A.id]
template[n_darts - 1, 2] = nodes_scores[B.id]

if deep>3:
# template niveau 2 deep = 6
n_id = find_template_opposite_node(d)
if n_id is not None:
template[n_darts - 1, 3] = nodes_scores[n_id]
n_id = find_template_opposite_node(d1)
if n_id is not None:
template[n_darts - 1, 4] = nodes_scores[n_id]
n_id = find_template_opposite_node(d11)
if n_id is not None:
template[n_darts - 1, 5] = nodes_scores[n_id]

if deep>6:
# template niveau 3 - deep = 12
d2, d1, d11, d21, d211, n1, n2, n3, n4 = mesh.active_triangles(d)
#Triangle F2
n_id = find_template_opposite_node(d21)
if n_id is not None:
template[n_darts - 1, 6] = nodes_scores[n_id]
n_id = find_template_opposite_node(d211)
if n_id is not None:
template[n_darts - 1, 7] = nodes_scores[n_id]
# Triangle T3
d12 = d1.get_beta(2)
d121 = d12.get_beta(1)
d1211 = d121.get_beta(1)
n_id = find_template_opposite_node(d121)
if n_id is not None:
template[n_darts - 1, 8] = nodes_scores[n_id]
n_id = find_template_opposite_node(d1211)
if n_id is not None:
template[n_darts - 1, 9] = nodes_scores[n_id]
# Triangle T4
d112 = d11.get_beta(2)
d1121 = d112.get_beta(1)
d11211 = d1121.get_beta(1)
n_id = find_template_opposite_node(d1121)
if n_id is not None:
template[n_darts - 1, 10] = nodes_scores[n_id]
n_id = find_template_opposite_node(d11211)
if n_id is not None:
template[n_darts - 1, 11] = nodes_scores[n_id]

template = template[:n_darts, :]

return template, dart_ids

def get_template_deg(mesh: Mesh, deep: int, nodes_scores, nodes_adjacency):
size = len(mesh.dart_info)
template = np.zeros((size, deep*2), dtype=np.int64)
dart_ids = []
n_darts = 0

for d_info in mesh.active_darts():
n_darts += 1
d_id = d_info[0]
dart_ids.append(d_id)
d = Dart(mesh, d_id)
A = d.get_node()
d1 = d.get_beta(1)
B = d1.get_node()
d11 = d1.get_beta(1)
C = d11.get_node()

# Template niveau 1
template[n_darts - 1, 0] = nodes_scores[C.id]
template[n_darts - 1, deep] = nodes_adjacency[C.id]
template[n_darts - 1, 1] = nodes_scores[A.id]
template[n_darts - 1, deep+1] = nodes_adjacency[A.id]
template[n_darts - 1, 2] = nodes_scores[B.id]
template[n_darts - 1, deep+2] = nodes_adjacency[B.id]

if deep>3:
# template niveau 2
n_id = find_template_opposite_node(d)
if n_id is not None:
template[n_darts - 1, 3] = nodes_scores[n_id]
template[n_darts - 1, deep+3] = nodes_adjacency[n_id]
n_id = find_template_opposite_node(d1)
if n_id is not None:
template[n_darts - 1, 4] = nodes_scores[n_id]
template[n_darts - 1, deep+4] = nodes_adjacency[n_id]
n_id = find_template_opposite_node(d11)
if n_id is not None:
template[n_darts - 1, 5] = nodes_scores[n_id]
template[n_darts - 1, deep+5] = nodes_adjacency[n_id]

if deep>6:
# template niveau 3 - deep = 12
if d.get_beta(2) is not None:
d2, d1, d11, d21, d211, n1, n2, n3, n4 = mesh.active_triangles(d)
#Triangle F2
n_id = find_template_opposite_node(d21)
if n_id is not None:
template[n_darts - 1, 6] = nodes_scores[n_id]
template[n_darts - 1, deep+6] = nodes_adjacency[n_id]
n_id = find_template_opposite_node(d211)
if n_id is not None:
template[n_darts - 1, 7] = nodes_scores[n_id]
template[n_darts - 1, deep+7] = nodes_adjacency[n_id]
# Triangle T3
d12 = d1.get_beta(2)
if d12 is not None:
d121 = d12.get_beta(1)
d1211 = d121.get_beta(1)
n_id = find_template_opposite_node(d121)
if n_id is not None:
template[n_darts - 1, 8] = nodes_scores[n_id]
template[n_darts - 1, deep+8] = nodes_adjacency[n_id]
n_id = find_template_opposite_node(d1211)
if n_id is not None:
template[n_darts - 1, 9] = nodes_scores[n_id]
template[n_darts - 1, deep+9] = nodes_adjacency[n_id]
# Triangle T4
d112 = d11.get_beta(2)
if d112 is not None:
d1121 = d112.get_beta(1)
d11211 = d1121.get_beta(1)
n_id = find_template_opposite_node(d1121)
if n_id is not None:
template[n_darts - 1, 10] = nodes_scores[n_id]
template[n_darts - 1, deep+10] = nodes_adjacency[n_id]
n_id = find_template_opposite_node(d11211)
if n_id is not None:
template[n_darts - 1, 11] = nodes_scores[n_id]
template[n_darts - 1, deep+11] = nodes_adjacency[n_id]

template = template[:n_darts, :]
return template, dart_ids
Loading

0 comments on commit fbecd6c

Please sign in to comment.