-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCTRScene.py
236 lines (187 loc) · 11.2 KB
/
CTRScene.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
"""Toolbox: compute reward, create scene, ...
"""
__authors__ = "PSC"
__contact__ = "[email protected]"
__version__ = "1.0.0"
__copyright__ = "(c) 2021, Robocath, CNRS, Inria"
__date__ = "Nov 28 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()))
from os.path import dirname, abspath
import SofaRuntime
from splib3.animation import AnimationManagerController
from CTRToolbox import RewardShaper, GoalSetter
# Register all the common component in the factory.
SofaRuntime.importPlugin("Sofa.Component")
path = dirname(abspath(__file__)) + '/'
def add_plugins(root):
root.addObject('RequiredPlugin', pluginName='BeamAdapter')
root.addObject('RequiredPlugin', name='SofaOpenglVisual')
root.addObject('RequiredPlugin', name='SofaBoundaryCondition')
root.addObject('RequiredPlugin', name='SofaConstraint')
root.addObject('RequiredPlugin', name='SofaDeformable')
root.addObject('RequiredPlugin', name='SofaGeneralLinearSolver')
root.addObject('RequiredPlugin', name='SofaImplicitOdeSolver')
root.addObject('RequiredPlugin', name='SofaLoader')
root.addObject('RequiredPlugin', name='SofaMeshCollision')
root.addObject('RequiredPlugin', name='SofaTopologyMapping')
return root
def add_visuals_and_solvers(root, config, mode_simu=True, mode_visu=True):
if mode_visu:
source = config["source"]
target = config["target"]
root.addObject('VisualStyle', displayFlags='showVisualModels hideBehaviorModels hideCollisionModels '
'hideMappings hideForceFields hideWireframe')
# root.addObject('OglSceneFrame', style="Arrows", alignment="TopRight")
root.addObject("LightManager")
root.addObject("SpotLight", position=source, direction=[target[i] - source[i] for i in range(len(source))])
root.addObject('InteractiveCamera', name='camera', position=source, lookAt=target, zFar=5000)
# root.addObject('BackgroundSetting', color=[1, 1, 1, 1])
root.addObject('FreeMotionAnimationLoop')
lcp_solver = root.addObject('LCPConstraintSolver', mu=0.1, tolerance=3e-4, maxIt=10000, build_lcp=False)
if mode_simu:
root.addObject('DefaultPipeline', draw=False, depth=6, verbose=False)
# root.addObject('BruteForceBroadPhase')
# root.addObject('BVHNarrowPhase')
root.addObject('GenericConstraintSolver', maxIterations=500, tolerance=1e-8)
root.addObject('BruteForceDetection', name="N2")
root.addObject('DefaultContactManager', name='Response', response="FrictionContactConstraint")
root.addObject('LocalMinDistance', contactDistance=0.1, alarmDistance=1.0, name='localmindistance',
angleCone=0.2)
root.addObject(AnimationManagerController(root))
return root, lcp_solver
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=2.0,
showColor=[0, 1, 0, 0.5], position=[0.0, 0.0, 0.0])
return goal_mo
def add_instrument_topology(root, name, i):
# Topo Guide
topology_node = root.addChild('topoLines_'+name)
topology_node.addObject('WireRestShape', name=name+'RestShape', straightLength=475+i*5, length=500, numEdges=200,
youngModulus=50000, spireDiameter=25-i*5, numEdgesCollis=[50, 10], printLog=False,
template='Rigid3d', spireHeight=0.0, densityOfBeams=[30, 5],
youngModulusExtremity=50000*(i+1))
topology_node.addObject('MechanicalObject', name='dofTopo2', template='Rigid3d')
topology_node.addObject('EdgeSetTopologyContainer', name='meshLines'+name)
topology_node.addObject('EdgeSetTopologyModifier', name='Modifier')
topology_node.addObject('EdgeSetGeometryAlgorithms', name='GeomAlgo', template='Rigid3d')
def add_instruments_combined(root, instrument_list, mode_simu=True):
# Instruments
instrument_combined_node = root.addChild('InstrumentCombined')
instrument_combined_node.addObject('VisualStyle', displayFlags='hideWireframe')
# if mode_simu:
instrument_combined_node.addObject('EulerImplicitSolver', rayleighStiffness=0.2, printLog=False, rayleighMass=0.1)
instrument_combined_node.addObject('BTDLinearSolver', verification=False, subpartSolve=False, verbose=False)
instrument_combined_node.addObject('RegularGridTopology', name='meshLinesCombined', zmax=1, zmin=1, nx=60, ny=1,
nz=1, xmax=1.0, xmin=0.0, ymin=0, ymax=0)
instrument_combined_node.addObject('MechanicalObject', showIndices=False, name='DOFs', template='Rigid3d', ry=-90)
for instrument in instrument_list:
instrument_combined_node.addObject('WireBeamInterpolation',
WireRestShape='@../topoLines_'+instrument+'/'+instrument+'RestShape',
radius=0.15, printLog=False, name='Interpol'+instrument,
edgeList=[k for k in range(60)])
instrument_combined_node.addObject('AdaptiveBeamForceFieldAndMass', massDensity=0.00000155,
name=instrument+'ForceField', interpolation='@Interpol'+instrument)
instrument_combined_node.addObject('InterventionalRadiologyController', xtip=[1, 0, 0], name='m_ircontroller',
instruments=['Interpol'+instrument for instrument in instrument_list], step=0.5,
printLog=False, listening=True, template='Rigid3d',
startingPos=[0, 0, 0, 0, 0, 0, 1], rotationInstrument=[0, 0, 0], speed=0,
controlledInstrument=0)
if mode_simu:
instrument_combined_node.addObject('LinearSolverConstraintCorrection', wire_optimization='true', printLog=False)
instrument_combined_node.addObject('FixedConstraint', indices=0, name='FixedConstraint')
instrument_combined_node.addObject('RestShapeSpringsForceField', points='@m_ircontroller.indexFirstNode',
angularStiffness=1e0, stiffness=1e0)
if mode_simu:
# Collision model
Collis = instrument_combined_node.addChild('Collis')
Collis.addObject('EdgeSetTopologyContainer', name='collisEdgeSet')
Collis.addObject('EdgeSetTopologyModifier', name='colliseEdgeModifier')
Collis.addObject('MechanicalObject', name='CollisionDOFs')
Collis.addObject('MultiAdaptiveBeamMapping', controller='../m_ircontroller',
useCurvAbs=True, printLog=False, name='collisMap')
Collis.addObject('LineCollisionModel', proximity=0.0, group=1)
Collis.addObject('PointCollisionModel', proximity=0.0, group=1)
return instrument_combined_node
def add_instrument_visual(instrument_combined_node, instrument_name, color, i):
visu_node = instrument_combined_node.addChild('Visu_'+instrument_name)
visu_node.addObject('MechanicalObject', name='Quads')
visu_node.addObject('QuadSetTopologyContainer', name='Container'+instrument_name)
visu_node.addObject('QuadSetTopologyModifier', name='Modifier')
visu_node.addObject('QuadSetGeometryAlgorithms', name='GeomAlgo', template='Vec3d')
visu_node.addObject('Edge2QuadTopologicalMapping', radius=0.5*(i+1), listening='true',
input='@../../topoLines_'+instrument_name+'/meshLines'+instrument_name,
nbPointsOnEachCircle='10', flipNormals='true', output='@Container'+instrument_name)
visu_node.addObject('AdaptiveBeamMapping', interpolation='@../Interpol'+instrument_name,
name='visuMap'+instrument_name, output='@Quads', isMechanical=False,
input='@../DOFs', useCurvAbs=True, printLog=False)
# Ogl model
visu_ogl = visu_node.addChild('VisuOgl')
visu_ogl.addObject('OglModel', color=color, quads='@../Container'+instrument_name+'.quads', name='Visual')
visu_ogl.addObject('IdentityMapping', input='@../Quads', output='@Visual')
def add_geometry(root, config, mode_simu=True, mode_visu=True):
geometry = root.addChild('Vessels')
geometry.addObject('MeshObjLoader', filename=path+config["mesh"], flipNormals=True, triangulate=True,
name='meshLoader', scale=config["scale"], translation=config["translation"],
rotation=config["rotation"], printLog=False)
geometry.addObject('MeshTopology', position='@meshLoader.position', triangles='@meshLoader.triangles')
mo = geometry.addObject('MechanicalObject', name='DOFs1')
if mode_simu:
geometry.addObject('TriangleCollisionModel', moving=False, simulated=False)
geometry.addObject('LineCollisionModel', moving=False, simulated=False)
geometry.addObject('PointCollisionModel', moving=False, simulated=False)
if mode_visu:
geometry.addObject('OglModel', color=[0.8, 0.8, 0.8, 0.2], src="@meshLoader")
return mo
DEFAULT_CONFIG = {"scene": "CTR",
"deterministic": True,
"source": [-150, 0, 30],
"target": [0, 0, 30],
"mesh": "mesh/nasal_cavity.obj",
"scale": 30,
"rotation": [140.0, 0.0, 0.0],
"translation": [0.0, 0.0, 0.0],
"goalList": [[0.0, 0.0, 10.0]],
"scale_factor": 10,
"timer_limit": 50,
"timeout": 30,
"display_size": (1600, 800),
"render": 1,
"save_data": False,
"save_path": path + "/Results" + "/CTR",
"planning": True,
"discrete": True,
"seed": 0,
"start_from_history": None,
"python_version": "python3.7",
"zFar": 5000,
"dt": 0.01
}
def createScene(root, config=DEFAULT_CONFIG, tube_list=['tube_1', 'tube_2', 'tube_3'], mode='simu_and_visu'):
print("SCENE ", config)
# Choose the mode: visualization or computations (or both)
visu, simu = False, False
if 'visu' in mode:
visu = True
if 'simu' in mode:
simu = True
add_plugins(root)
_, solver = add_visuals_and_solvers(root, config, simu, visu)
root.dt.value = config["dt"]
goal_mo = add_goal_node(root)
for i, name in enumerate(tube_list):
add_instrument_topology(root, name, i)
instrument_combined_node = add_instruments_combined(root, tube_list, simu)
color = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
if visu:
for i, name in enumerate(tube_list):
add_instrument_visual(instrument_combined_node, name, color[i % len(color)], i)
cavity_mo = add_geometry(root, config, simu, visu)
root.addObject(RewardShaper(name="Reward", rootNode=root, goalPos=config['goalPos']))
root.addObject(GoalSetter(name="GoalSetter", rootNode=root, goalMO=goal_mo, goalPos=config['goalPos']))
return root