1
1
import macro from 'vtk.js/Sources/macros' ;
2
2
import { MouseButton } from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor/Constants' ;
3
3
import vtkInteractorStyle from 'vtk.js/Sources/Rendering/Core/InteractorStyle' ;
4
+ import { mat4 , vec3 , vec4 } from 'gl-matrix' ;
4
5
5
6
const { vtkDebugMacro } = macro ;
6
7
const { States } = vtkInteractorStyle ;
@@ -134,6 +135,47 @@ function dollyByFactor(interactor, renderer, factor) {
134
135
}
135
136
}
136
137
138
+ function getCameraMatrix ( renderer ) {
139
+ const cam = renderer . getActiveCamera ( ) ;
140
+ if ( cam ) {
141
+ const M = mat4 . create ( ) ;
142
+ mat4 . copy ( M , cam . getViewMatrix ( ) ) ;
143
+ return M ;
144
+ }
145
+ return null ;
146
+ }
147
+
148
+ function computeNewCenterOfRotation ( beforeM , renderer , oldCenterOfRotation ) {
149
+ const cam = renderer . getActiveCamera ( ) ;
150
+ if ( ! cam || ! beforeM ) {
151
+ return oldCenterOfRotation ;
152
+ }
153
+
154
+ // The view matrix from vtk.js is row-major, but gl-matrix expects column-major.
155
+ // We need to transpose them before use.
156
+ const beforeMatrix = mat4 . create ( ) ;
157
+ mat4 . transpose ( beforeMatrix , beforeM ) ;
158
+
159
+ const afterMatrixRowMajor = cam . getViewMatrix ( ) ;
160
+ const afterMatrix = mat4 . create ( ) ;
161
+ mat4 . transpose ( afterMatrix , afterMatrixRowMajor ) ;
162
+
163
+ // Now we can proceed with column-major matrices.
164
+ const invAfterM = mat4 . create ( ) ;
165
+ mat4 . invert ( invAfterM , afterMatrix ) ;
166
+
167
+ const deltaM = mat4 . create ( ) ;
168
+ // deltaM = inv(afterM) * beforeM
169
+ mat4 . multiply ( deltaM , invAfterM , beforeMatrix ) ;
170
+
171
+ if ( ! mat4 . equals ( deltaM , mat4 . identity ( mat4 . create ( ) ) ) ) {
172
+ const center = vec4 . fromValues ( ...oldCenterOfRotation , 1 ) ;
173
+ vec4 . transformMat4 ( center , center , deltaM ) ;
174
+ return vec3 . fromValues ( center [ 0 ] , center [ 1 ] , center [ 2 ] ) ;
175
+ }
176
+ return oldCenterOfRotation ;
177
+ }
178
+
137
179
// ----------------------------------------------------------------------------
138
180
// Static API
139
181
// ----------------------------------------------------------------------------
@@ -504,11 +546,25 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
504
546
publicAPI . handleMouseMove = ( callData ) => {
505
547
model . cachedMousePosition = callData . position ;
506
548
if ( model . currentManipulator && model . currentManipulator . onMouseMove ) {
549
+ const renderer = model . getRenderer ( callData ) ;
550
+ const beforeM = getCameraMatrix ( renderer ) ;
551
+
507
552
model . currentManipulator . onMouseMove (
508
553
model . _interactor ,
509
- model . getRenderer ( callData ) ,
554
+ renderer ,
510
555
callData . position
511
556
) ;
557
+
558
+ const newCenter = computeNewCenterOfRotation (
559
+ beforeM ,
560
+ renderer ,
561
+ model . centerOfRotation
562
+ ) ;
563
+
564
+ if ( ! vec3 . equals ( model . centerOfRotation , newCenter ) ) {
565
+ publicAPI . setCenterOfRotation ( newCenter ) ;
566
+ }
567
+
512
568
publicAPI . invokeInteractionEvent ( INTERACTION_EVENT ) ;
513
569
}
514
570
} ;
@@ -672,6 +728,9 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
672
728
673
729
//----------------------------------------------------------------------------
674
730
publicAPI . handlePan = ( callData ) => {
731
+ const renderer = model . getRenderer ( callData ) ;
732
+ const beforeM = getCameraMatrix ( renderer ) ;
733
+
675
734
let count = model . gestureManipulators . length ;
676
735
let actionCount = 0 ;
677
736
while ( count -- ) {
@@ -686,6 +745,15 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
686
745
}
687
746
}
688
747
if ( actionCount ) {
748
+ const newCenter = computeNewCenterOfRotation (
749
+ beforeM ,
750
+ renderer ,
751
+ model . centerOfRotation
752
+ ) ;
753
+
754
+ if ( ! vec3 . equals ( model . centerOfRotation , newCenter ) ) {
755
+ publicAPI . setCenterOfRotation ( newCenter ) ;
756
+ }
689
757
publicAPI . invokeInteractionEvent ( INTERACTION_EVENT ) ;
690
758
}
691
759
} ;
0 commit comments