-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathpyqt_example.py
More file actions
163 lines (138 loc) · 6.37 KB
/
pyqt_example.py
File metadata and controls
163 lines (138 loc) · 6.37 KB
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
from qtpy.QtCore import *
from qtpy.QtWidgets import *
from qtpy.QtOpenGL import *
import Sofa.SofaGL
import Sofa
import SofaRuntime
import Sofa.Simulation as sim
import os
sofa_directory = os.environ['SOFA_ROOT']
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
from PIL import Image
"""
With something like this setup, we can use Sofa with our own GUI and not have to give over control of the main thread.
Simple pyqt signals can be added to manually rotate the view with the mouse (could be a tedious task to get it to
be intuitive).
"""
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.sofa_sim = SofaSim()
self.sofa_sim.init_sim()
self.sofa_view = glSofaWidget(self.sofa_sim.visuals)
mainLayout = QHBoxLayout()
mainLayout.addWidget(self.sofa_view)
self.setLayout(mainLayout)
self.simulation_timer = QTimer()
self.simulation_timer.timeout.connect(self.step_sim)
self.simulation_timer.setInterval(self.sofa_sim.root.getDt())
self.simulation_timer.start()
def step_sim(self):
self.sofa_sim.step_sim()
self.sofa_view.update()
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Space:
self.sofa_view.get_depth_image()
class glSofaWidget(QGLWidget):
def __init__(self, sofa_visuals_node):
QGLWidget.__init__(self)
self.visuals_node = sofa_visuals_node
self.setMinimumSize(800, 600)
self.z_far = sofa_visuals_node.camera.findData('zFar').value
self.z_near = sofa_visuals_node.camera.findData('zNear').value
def initializeGL(self):
glViewport(0,0, self.width(), self.height())
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
Sofa.SofaGL.glewInit()
Sofa.Simulation.initVisual(self.visuals_node)
Sofa.Simulation.initTextures(self.visuals_node)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (self.width() / self.height()), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def paintGL(self):
glViewport(0,0, self.width(), self.height())
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (self.width()/ self.height()), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
cameraMVM = self.visuals_node.camera.getOpenGLModelViewMatrix()
glMultMatrixd(cameraMVM)
Sofa.SofaGL.draw(self.visuals_node)
def get_depth_image(self):
_, _, width, height = glGetIntegerv(GL_VIEWPORT)
buff = glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT)
image = np.frombuffer(buff, dtype=np.float32)
image = image.reshape(height, width)
image = np.flipud(image) #<-- image is now a numpy array you can use
depth_image = -self.z_far*self.z_near/(self.z_far + image*(self.z_near-self.z_far))
depth_image = (depth_image - depth_image.min()) / (depth_image.max() - depth_image.min())
depth_image = depth_image * 255
img2 = Image.fromarray(depth_image.astype(np.uint8), 'L')
img2.show()
class SofaSim():
def __init__(self):
# Register all the common component in the factory.
SofaRuntime.PluginRepository.addFirstPath(os.path.join(sofa_directory, 'bin'))
SofaRuntime.importPlugin('SofaOpenglVisual')
SofaRuntime.importPlugin("SofaComponentAll")
SofaRuntime.importPlugin("SofaGeneralLoader")
SofaRuntime.importPlugin("SofaImplicitOdeSolver")
SofaRuntime.importPlugin("SofaLoader")
SofaRuntime.importPlugin("SofaSimpleFem")
SofaRuntime.importPlugin("SofaBoundaryCondition")
SofaRuntime.importPlugin("SofaMiscForceField")
self.root = Sofa.Core.Node("Root")
root = self.root
root.gravity = [0, -1., 0]
root.addObject("VisualStyle", displayFlags="showBehaviorModels showAll")
root.addObject("MeshGmshLoader", name="meshLoaderCoarse",
filename="mesh/liver.msh")
root.addObject("MeshObjLoader", name="meshLoaderFine",
filename="mesh/liver-smooth.obj")
root.addObject("EulerImplicitSolver")
root.addObject("CGLinearSolver", iterations="200",
tolerance="1e-09", threshold="1e-09")
liver = root.addChild("liver")
liver.addObject("TetrahedronSetTopologyContainer",
name="topo", src="@../meshLoaderCoarse")
liver.addObject("TetrahedronSetGeometryAlgorithms",
template="Vec3d", name="GeomAlgo")
liver.addObject("MechanicalObject",
template="Vec3d",
name="MechanicalModel", showObject="1", showObjectScale="3")
liver.addObject("TetrahedronFEMForceField", name="fem", youngModulus="1000",
poissonRatio="0.4", method="large")
liver.addObject("MeshMatrixMass", massDensity="1")
liver.addObject("FixedConstraint", indices="2 3 50")
visual = liver.addChild("visual")
visual.addObject('MeshObjLoader', name="meshLoader_0", filename="mesh/liver-smooth.obj", handleSeams="1")
visual.addObject('OglModel', name="VisualModel", src="@meshLoader_0", color='red')
visual.addObject('BarycentricMapping', input="@..", output="@VisualModel", name="visual mapping")
# place light and a camera
self.visuals = root.addChild('visuals')
root.addObject("LightManager")
root.addObject("DirectionalLight", direction=[0, 1, 0])
self.visuals.addObject("InteractiveCamera", name="camera", position=[0, 15, 0],
lookAt=[0, 0, 0], distance=37,
fieldOfView=45, zNear=0.63, zFar=55.69)
def init_sim(self):
# start the simulator
Sofa.Simulation.init(self.root)
def step_sim(self):
self.visuals.camera.position = self.visuals.camera.position + [-0.0002, 0, 0]
Sofa.Simulation.animate(self.root, self.root.getDt()) # uncomment to animated sim
Sofa.Simulation.updateVisual(self.root)
if __name__ == '__main__':
app = QApplication(['Yo'])
window = MainWindow()
window.show()
app.exec_()