-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.py
278 lines (225 loc) · 9.27 KB
/
controls.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import OpenGL.GL as gl
import numpy as np
from PySide import QtCore, QtGui
import material
from external import igl
from common import normalize
import operators
class BrushBase(QtCore.QObject):
def __init__(self, radius=10., *args):
super(BrushBase, self).__init__(*args)
self.radius = radius
self.lastHit = None
self.lastHitID = None
self.activeMesh = None
self.active = False
self.adjustingRadius = False
self.operating = False
self.alternative = False
self.matrix = None
def setActiveMesh(self, activeMesh):
self.activeMesh = activeMesh
def init(self):
pass
def updateViewProjection(self, view, projection):
pass
def updateRadius(self, radius):
self.radius = radius
def mouseMove(self):
pass
# TODO tidy up event handling
def mousePress(self):
pass
# TODO tidy up event handling
def handleMouseButton(self, x, y, modifiers, buttons, dx, dy):
self.operating = False
if buttons == QtCore.Qt.LeftButton:
if modifiers == QtCore.Qt.ControlModifier:
self.alternative = True
elif buttons == QtCore.Qt.MidButton:
self.adjustingRadius = True
self.operating = True
if abs(dx) > abs(dy):
self.updateRadius(self.radius + -dx * self.radius * 3.)
else:
self.updateRadius(self.radius + dy * self.radius * 3.)
else:
self.operating = False
self.adjustingRadius = False
def mouseMoveEvent(self, x, y, modifiers, buttons, viewportCoords, viewMatrix, projectionMatrix, cameraPosition, upsign, dx, dy):
if self.activeMesh is None:
return False
if self.adjustingRadius:
self.handleMouseButton(x, y, modifiers, buttons, dx, dy)
return False
mX = float(x)
mY = float(viewportCoords[3] - y)
model = igl.eigen.MatrixXd(self.activeMesh.matrix.T.astype(float).tolist())
view = igl.eigen.MatrixXd(viewMatrix.T.astype(float).tolist())
projection = igl.eigen.MatrixXd(projectionMatrix.T.astype(float).tolist())
viewport = igl.eigen.MatrixXd(map(float, viewportCoords))
barycentricCoords = igl.eigen.MatrixXd()
# Cast a ray in the view direction starting from the mouse position
hitIDs = igl.eigen.MatrixXi([-1])
coord = igl.eigen.MatrixXd([mX, mY])
hit = igl.unproject_onto_mesh(coord, view * model, projection, viewport, self.activeMesh.V, self.activeMesh.F, hitIDs, barycentricCoords)
hitID = hitIDs[0, 0]
if hit and hitID != -1:
face = self.activeMesh.points[self.activeMesh.trimap[hitID]] + self.activeMesh.offsets[self.activeMesh.trimap[hitID]]
hitPos = face[0] * barycentricCoords[0] + face[1] * barycentricCoords[1] + face[2] * barycentricCoords[2]
self.lastHit = hitPos.astype(float).tolist()
self.lastHitID = int(self.activeMesh.trimap[hitID][0])
self.lastHitNormal = normalize(np.cross(face[1] - face[0], face[2] - face[0]))[0]
self.active = True
tempMat = QtGui.QMatrix4x4()
tempMat.translate(*self.lastHit)
tempMat.rotate(QtGui.QQuaternion(1., 0., 0., -1.).normalized())
tempMat.rotate(QtGui.QQuaternion(1. / np.linalg.norm(self.lastHitNormal), *self.lastHitNormal).normalized())
self.matrix = np.array(tempMat.data(), np.float32).reshape(4, 4)
else:
self.active = False
if hit:
self.handleMouseButton(x, y, modifiers, buttons, dx, dy)
else:
self.operating = False
self.alternative = False
return hit
def draw(self):
pass
class PinPoint(QtCore.QObject):
def __init__(self, color=[1., 0., 0., 1.], *args):
super(PinPoint, self).__init__(*args)
self.verts = np.array([0, 0, 0], np.float32)
self.count = 1
self.matrix = np.identity(4).T.reshape(4, 4)
self.vao = None
self.vboVerts = None
self.material = material.ConstantMaterial()
self.color = color
def init(self):
self.vao = gl.glGenVertexArrays(1)
gl.glBindVertexArray(self.vao)
self.vboVerts = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vboVerts)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
self.verts.nbytes,
self.verts,
gl.GL_DYNAMIC_DRAW
)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
gl.glBindVertexArray(0)
del self.verts
self.verts = None
def updateViewProjection(self, view, projection):
gl.glUseProgram(self.material.shaderProg)
gl.glUniformMatrix4fv(
gl.glGetUniformLocation(self.material.shaderProg, 'view'),
1,
gl.GL_FALSE,
view
)
gl.glUniformMatrix4fv(
gl.glGetUniformLocation(self.material.shaderProg, 'projection'),
1,
gl.GL_FALSE,
projection
)
gl.glUseProgram(0)
def draw(self):
gl.glUseProgram(self.material.shaderProg)
gl.glBindVertexArray(self.vao)
gl.glEnableVertexAttribArray(0)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vboVerts)
gl.glVertexAttribPointer(
0,
3,
gl.GL_FLOAT,
gl.GL_FALSE,
0,
None
)
gl.glUniformMatrix4fv(
gl.glGetUniformLocation(self.material.shaderProg, 'model'),
1,
gl.GL_FALSE,
self.matrix
)
gl.glUniform4f(
gl.glGetUniformLocation(self.material.shaderProg, 'inputColor'),
*self.color
)
gl.glDisable(gl.GL_DEPTH_TEST)
gl.glDrawArrays(
gl.GL_POINTS,
0,
self.count
)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glDisableVertexAttribArray(0)
gl.glBindVertexArray(0)
gl.glUseProgram(0)
class RubberBrush(BrushBase):
def __init__(self, radius=10., *args):
super(RubberBrush, self).__init__(radius, *args)
self.cursorPin = PinPoint()
self.pins = []
self.view = None
self.projection = None
self.operator = operators.Rubber()
self.pinInfo = []
def setActiveMesh(self, activeMesh):
super(RubberBrush, self).setActiveMesh(activeMesh)
self.operator.activeMesh = activeMesh
def init(self):
super(RubberBrush, self).init()
self.cursorPin.init()
def updateViewProjection(self, view, projection):
super(RubberBrush, self).updateViewProjection(view, projection)
self.cursorPin.updateViewProjection(view, projection)
for pin in self.pins:
pin.updateViewProjection(view, projection)
self.view = view
self.projection = projection
def updateRadius(self, radius):
super(RubberBrush, self).updateRadius(radius)
# TODO: i need press and release events
def handleMouseButton(self, x, y, modifiers, buttons, dx, dy):
super(RubberBrush, self).handleMouseButton(x, y, modifiers, buttons, dx, dy)
if not self.active:
return
if self.alternative and self.view is not None and self.projection is not None:
newPin = PinPoint()
newPin.init()
tempMat = QtGui.QMatrix4x4()
tempMat.translate(*self.lastHit)
tempMat.rotate(QtGui.QQuaternion(1., 0., 0., -1.).normalized())
tempMat.rotate(QtGui.QQuaternion(1. / np.linalg.norm(self.lastHitNormal), *self.lastHitNormal).normalized())
newPin.matrix = np.array(tempMat.data(), np.float32).reshape(4, 4)
newPin.updateViewProjection(self.view, self.projection)
newPin.draw()
self.pins.append(newPin)
self.operator.appendPin(self.lastHitID, self.lastHit)
self.alternative = False
def mouseMoveEvent(self, x, y, modifiers, buttons, viewportCoords, viewMatrix, projectionMatrix, cameraPosition, upsign, dx, dy):
hit = super(RubberBrush, self).mouseMoveEvent(x, y, modifiers, buttons, viewportCoords, viewMatrix, projectionMatrix, cameraPosition, upsign, dx, dy)
self.cursorPin.matrix = self.matrix
self.operating = False
if buttons == QtCore.Qt.LeftButton and not self.alternative:
self.operator.solveDelta(-1, dx, dy, cameraPosition, upsign)
self.operating = True
vertID = self.operator.pinVertIDs[-1][0]
tempMat = QtGui.QMatrix4x4()
tempMat.translate(*(self.activeMesh.points[vertID] + self.activeMesh.offsets[vertID]))
tempMat.rotate(QtGui.QQuaternion(1., 0., 0., -1.).normalized())
tempMat.rotate(QtGui.QQuaternion(1. / np.linalg.norm(self.lastHitNormal), *self.lastHitNormal).normalized())
self.pins[-1].matrix = np.array(tempMat.data(), np.float32).reshape(4, 4)
if buttons == QtCore.Qt.MidButton:
self.operating = True
self.adjustingRadius = True
return hit
def draw(self):
if (self.active and not self.operating) or self.adjustingRadius:
self.cursorPin.draw()
for pin in self.pins:
pin.draw()