forked from SofaDefrost/SofaGym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleMazeScene.py
140 lines (110 loc) · 6.03 KB
/
SimpleMazeScene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-
"""Toolbox: compute reward, create scene, ...
"""
__authors__ = "PSC"
__contact__ = "[email protected]"
__version__ = "1.0.0"
__copyright__ = "(c) 2021, Robocath, CNRS, Inria"
__date__ = "Mar 23 2021"
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute())+"/../")
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute()))
import os
from SimpleMazeToolbox import rewardShaper, goalSetter
from splib3.animation import AnimationManagerController
path = os.path.dirname(os.path.abspath(__file__))
path_mesh = path + '/mesh/'
def add_goal_node(root):
goal = root.addChild("Goal")
goal.addObject('VisualStyle', displayFlags="showCollisionModels")
goal_mo = goal.addObject('MechanicalObject', name='GoalMO', showObject=True, drawMode="1", showObjectScale=3,
showColor=[0, 1, 0, 1], position=[0.0, 0.0, 0.0])
return goal_mo
def createScene(root, config={"source": [0, 1000, 0],
"target": [0, 0, 0],
"goal_node": 0,
"goalPos": [0.0, 0.0, 0.0]}, mode='simu_and_visu'):
# Chose the mode: visualization or computations (or both)
visu, simu = False, False
if 'visu' in mode:
visu = True
if 'simu' in mode:
simu = True
root.addObject("RequiredPlugin", name="SoftRobots")
root.addObject("RequiredPlugin", name="SofaPython3")
root.addObject("RequiredPlugin", name="SofaOpenglVisual")
root.addObject("RequiredPlugin", name="SofaMiscCollision")
root.addObject("RequiredPlugin", name="SofaSparseSolver")
root.gravity = [0.0, -981, 0.0]
# VISU ###################
if visu:
root.addObject('VisualStyle', displayFlags="showBehaviorModels showCollisionModels hideInteractionForceFields")
if True:
root.addObject('DefaultPipeline')
root.addObject('BruteForceDetection')
root.addObject('RuleBasedContactManager', responseParams="mu=0.0001", name='Response', response='FrictionContactConstraint')
root.addObject('LocalMinDistance', alarmDistance=6, contactDistance=0.1, angleCone=0.01)
root.addObject('FreeMotionAnimationLoop')
root.addObject('GenericConstraintSolver', tolerance="1e-6", maxIterations="1000")
root.addObject(AnimationManagerController(root, name="AnimationManager"))
model = root.addChild('model')
model.addObject('EulerImplicitSolver', firstOrder=True)
model.addObject('CGLinearSolver', iterations=100, threshold=1e-5, tolerance=1e-5)
maze_mo = model.addObject('MechanicalObject', name='rigid_maze_mo', template='Rigid3d',
position=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], showObject=True, showObjectScale=10)
model.addObject('RestShapeSpringsForceField', angularStiffness=100, stiffness=100)
model.addObject('UncoupledConstraintCorrection', compliance='1e-10 1e-10 0 0 1e-10 0 1e-10 ')
sphere = root.addChild("sphere")
if True:
sphere.addObject("EulerImplicitSolver", rayleighMass='5')
sphere.addObject("SparseLDLSolver", name="ldl")
sphere.addObject("GenericConstraintCorrection", solverName='ldl')
ball_mo = sphere.addObject("MechanicalObject", name="sphere_mo", template='Vec3', position=[0.0, 6.0, 2.0])
sphere.addObject("UniformMass", totalMass=1000)
visu = object.addChild('Visu')
visu.addObject('MeshOBJLoader', name='loader', filename=ball.obj, scale3d=1)
visu.addObject('OglModel', src='@loader', color=[255, 0, 0, 255])
visu.addObject('RigidMapping')
object.addObject('GenerateRigidMass', name='mass', density=density, src=visu.loader.getLinkPath())
object.mass.init()
translation = list(object.mass.centerToOrigin.value)
object.addObject('UniformMass', vertexMass="@mass.rigidMass")
visu.loader.translation = translation
if withCollision:
collision = object.addChild('Collision')
collision.addObject('MeshOBJLoader', name='loader', filename=collisionFilename, scale3d=scale)
collision.addObject('MeshTopology', src='@loader')
collision.addObject('MechanicalObject', translation=translation)
collision.addObject('TriangleCollisionModel', group = collisionGroup)
collision.addObject('LineCollisionModel', group = collisionGroup)
collision.addObject('PointCollisionModel', group = collisionGroup)
collision.addObject('RigidMapping')
maze = model.addChild("maze")
maze.addObject("MeshSTLLoader", name="loader", filename=path_mesh+"maze_4_coarse.stl",
translation=[-50.0, 0.0, 50.0], rotation=[-90.0, 0.0, 0.0])
maze.addObject("Mesh", src='@loader')
maze.addObject("MechanicalObject", name='maze_mesh_mo')
if True:
maze.addObject("Triangle", group=1)
maze.addObject("Line", group=1)
maze.addObject("Point", group=1)
maze.addObject("RigidMapping")
path_node = maze.addChild("Path")
p_mesh = path_node.addObject('MeshObjLoader', filename=path_mesh+"path.obj", flipNormals=True, triangulate=True,
name='meshLoader', translation=[-50.0, 0.0, 50.0])
p_mo = path_node.addObject("MechanicalObject", template="Rigid3d", name="dofs", position="@meshLoader.position",
showObject=True, showObjectScale=1.0)
if True:
path_node.addObject("RigidMapping", input=maze_mo.getLinkPath(), output=p_mo.getLinkPath())
goal_mo = add_goal_node(root)
root.addObject(rewardShaper(name="Reward", rootNode=root, goal_node=config['goalList'][config['goal_node']],
path_mesh=p_mesh, path_mo=p_mo, ball_mo=ball_mo))
root.addObject(goalSetter(name="GoalSetter", rootNode=root, goalMO=goal_mo, goalPos=config['goalPos']))
if visu:
source = config["source"]
target = config["target"]
root.addObject("LightManager")
root.addObject("SpotLight", position=source, direction=[0, -1, 0])
root.addObject("InteractiveCamera", name="camera", position=source, lookAt=target, zFar=500)
return root