summaryrefslogtreecommitdiff
path: root/silx/gui/plot3d/scene/interaction.py
blob: e5cfb6d71e8d61c606e02fd87c4c0cbaa3049c0b (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This module provides interaction to plug on the scene graph."""

from __future__ import absolute_import

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "25/07/2016"

import logging
import numpy

from silx.gui import qt
from silx.gui.plot.Interaction import \
    StateMachine, State, LEFT_BTN, RIGHT_BTN  # , MIDDLE_BTN

from . import transform


_logger = logging.getLogger(__name__)


# ClickOrDrag #################################################################

# TODO merge with silx.gui.plot.Interaction.ClickOrDrag
class ClickOrDrag(StateMachine):
    """Click or drag interaction for a given button."""

    DRAG_THRESHOLD_SQUARE_DIST = 5 ** 2

    class Idle(State):
        def onPress(self, x, y, btn):
            if btn == self.machine.button:
                self.goto('clickOrDrag', x, y)
                return True

    class ClickOrDrag(State):
        def enterState(self, x, y):
            self.initPos = x, y

        enter = enterState  # silx v.0.3 support, remove when 0.4 out

        def onMove(self, x, y):
            dx = (x - self.initPos[0]) ** 2
            dy = (y - self.initPos[1]) ** 2
            if (dx ** 2 + dy ** 2) >= self.machine.DRAG_THRESHOLD_SQUARE_DIST:
                self.goto('drag', self.initPos, (x, y))

        def onRelease(self, x, y, btn):
            if btn == self.machine.button:
                self.machine.click(x, y)
                self.goto('idle')

    class Drag(State):
        def enterState(self, initPos, curPos):
            self.initPos = initPos
            self.machine.beginDrag(*initPos)
            self.machine.drag(*curPos)

        enter = enterState  # silx v.0.3 support, remove when 0.4 out

        def onMove(self, x, y):
            self.machine.drag(x, y)

        def onRelease(self, x, y, btn):
            if btn == self.machine.button:
                self.machine.endDrag(self.initPos, (x, y))
                self.goto('idle')

    def __init__(self, button=LEFT_BTN):
        self.button = button
        states = {
            'idle': ClickOrDrag.Idle,
            'clickOrDrag': ClickOrDrag.ClickOrDrag,
            'drag': ClickOrDrag.Drag
        }
        super(ClickOrDrag, self).__init__(states, 'idle')

    def click(self, x, y):
        """Called upon a left or right button click.
        To override in a subclass.
        """
        pass

    def beginDrag(self, x, y):
        """Called at the beginning of a drag gesture with left button
        pressed.
        To override in a subclass.
        """
        pass

    def drag(self, x, y):
        """Called on mouse moved during a drag gesture.
        To override in a subclass.
        """
        pass

    def endDrag(self, x, y):
        """Called at the end of a drag gesture when the left button is
        released.
        To override in a subclass.
        """
        pass


# CameraRotate ################################################################

class CameraRotate(ClickOrDrag):
    """Camera rotation using an arcball-like interaction."""

    def __init__(self, viewport, orbitAroundCenter=True, button=RIGHT_BTN):
        self._viewport = viewport
        self._orbitAroundCenter = orbitAroundCenter
        self._reset()
        super(CameraRotate, self).__init__(button)

    def _reset(self):
        self._origin, self._center = None, None
        self._startExtrinsic = None

    def click(self, x, y):
        pass  # No interaction yet

    def beginDrag(self, x, y):
        centerPos = None
        if not self._orbitAroundCenter:
            # Try to use picked object position as center of rotation
            ndcZ = self._viewport._pickNdcZGL(x, y)
            if ndcZ != 1.:
                # Hit an object, use picked point as center
                centerPos = self._viewport._getXZYGL(x, y)  # Can return None

        if centerPos is None:
            # Not using picked position, use scene center
            bounds = self._viewport.scene.bounds(transformed=True)
            centerPos = 0.5 * (bounds[0] + bounds[1])

        self._center = transform.Translate(*centerPos)
        self._origin = x, y
        self._startExtrinsic = self._viewport.camera.extrinsic.copy()

    def drag(self, x, y):
        if self._center is None:
            return

        dx, dy = self._origin[0] - x, self._origin[1] - y

        if dx == 0 and dy == 0:
            direction = self._startExtrinsic.direction
            up = self._startExtrinsic.up
            position = self._startExtrinsic.position
        else:
            minsize = min(self._viewport.size)
            distance = numpy.sqrt(dx ** 2 + dy ** 2)
            angle = distance / minsize * numpy.pi

            # Take care of y inversion
            direction = dx * self._startExtrinsic.side - \
                dy * self._startExtrinsic.up
            direction /= numpy.linalg.norm(direction)
            axis = numpy.cross(direction, self._startExtrinsic.direction)
            axis /= numpy.linalg.norm(axis)

            # Orbit start camera with current angle and axis
            # Rotate viewing direction
            rotation = transform.Rotate(numpy.degrees(angle), *axis)
            direction = rotation.transformDir(self._startExtrinsic.direction)
            up = rotation.transformDir(self._startExtrinsic.up)

            # Rotate position around center
            trlist = transform.StaticTransformList((
                self._center,
                rotation,
                self._center.inverse()))
            position = trlist.transformPoint(self._startExtrinsic.position)

        camerapos = self._viewport.camera.extrinsic
        camerapos.setOrientation(direction, up)
        camerapos.position = position

    def endDrag(self, x, y):
        self._reset()


# CameraSelectPan #############################################################

class CameraSelectPan(ClickOrDrag):
    """Picking on click and pan camera on drag."""

    def __init__(self, viewport, button=LEFT_BTN, selectCB=None):
        self._viewport = viewport
        self._selectCB = selectCB
        self._lastPosNdc = None
        super(CameraSelectPan, self).__init__(button)

    def click(self, x, y):
        if self._selectCB is not None:
            ndcZ = self._viewport._pickNdcZGL(x, y)
            position = self._viewport._getXZYGL(x, y)
            # This assume no object lie on the far plane
            # Alternative, change the depth range so that far is < 1
            if ndcZ != 1. and position is not None:
                self._selectCB((x, y, ndcZ), position)

    def beginDrag(self, x, y):
        ndc = self._viewport.windowToNdc(x, y)
        ndcZ = self._viewport._pickNdcZGL(x, y)
        # ndcZ is the panning plane
        if ndc is not None and ndcZ is not None:
            self._lastPosNdc = numpy.array((ndc[0], ndc[1], ndcZ, 1.),
                                           dtype=numpy.float32)
        else:
            self._lastPosNdc = None

    def drag(self, x, y):
        if self._lastPosNdc is not None:
            ndc = self._viewport.windowToNdc(x, y)
            if ndc is not None:
                ndcPos = numpy.array((ndc[0], ndc[1], self._lastPosNdc[2], 1.),
                                     dtype=numpy.float32)

                # Convert last and current NDC positions to scene coords
                scenePos = self._viewport.camera.transformPoint(
                    ndcPos, direct=False, perspectiveDivide=True)
                lastScenePos = self._viewport.camera.transformPoint(
                    self._lastPosNdc, direct=False, perspectiveDivide=True)

                # Get translation in scene coords
                translation = scenePos[:3] - lastScenePos[:3]
                self._viewport.camera.extrinsic.position -= translation

                # Store for next drag
                self._lastPosNdc = ndcPos

    def endDrag(self, x, y):
        self._lastPosNdc = None


# CameraWheel #################################################################

class CameraWheel(object):
    """StateMachine like class, just handling wheel events."""

    # TODO choose scale of motion? Translation or Scale?
    def __init__(self, viewport, mode='center', scaleTransform=None):
        assert mode in ('center', 'position', 'scale')
        self._viewport = viewport
        if mode == 'center':
            self._zoomTo = self._zoomToCenter
        elif mode == 'position':
            self._zoomTo = self._zoomToPosition
        elif mode == 'scale':
            self._zoomTo = self._zoomByScale
            self._scale = scaleTransform
        else:
            raise ValueError('Unsupported mode: %s' % mode)

    def handleEvent(self, eventName, *args, **kwargs):
        if eventName == 'wheel':
            return self._zoomTo(*args, **kwargs)

    def _zoomToCenter(self, x, y, angleInDegrees):
        """Zoom to center of display.

        Only works with perspective camera.
        """
        direction = 'forward' if angleInDegrees > 0 else 'backward'
        self._viewport.camera.move(direction)
        return True

    def _zoomToPositionAbsolute(self, x, y, angleInDegrees):
        """Zoom while keeping pixel under mouse invariant.

        Only works with perspective camera.
        """
        ndc = self._viewport.windowToNdc(x, y)
        if ndc is not None:
            near = numpy.array((ndc[0], ndc[1], -1., 1.), dtype=numpy.float32)

            nearscene = self._viewport.camera.transformPoint(
                near, direct=False, perspectiveDivide=True)

            far = numpy.array((ndc[0], ndc[1], 1., 1.), dtype=numpy.float32)
            farscene = self._viewport.camera.transformPoint(
                far, direct=False, perspectiveDivide=True)

            dirscene = farscene[:3] - nearscene[:3]
            dirscene /= numpy.linalg.norm(dirscene)

            if angleInDegrees < 0:
                dirscene *= -1.

            # TODO which scale
            self._viewport.camera.extrinsic.position += dirscene
        return True

    def _zoomToPosition(self, x, y, angleInDegrees):
        """Zoom while keeping pixel under mouse invariant."""
        projection = self._viewport.camera.intrinsic
        extrinsic = self._viewport.camera.extrinsic

        if isinstance(projection, transform.Perspective):
            # For perspective projection, move camera
            ndc = self._viewport.windowToNdc(x, y)
            if ndc is not None:
                ndcz = self._viewport._pickNdcZGL(x, y)

                position = numpy.array((ndc[0], ndc[1], ndcz),
                                       dtype=numpy.float32)
                positionscene = self._viewport.camera.transformPoint(
                    position, direct=False, perspectiveDivide=True)

                camtopos = extrinsic.position - positionscene

                step = 0.2 * (1. if angleInDegrees < 0 else -1.)
                extrinsic.position += step * camtopos

        elif isinstance(projection, transform.Orthographic):
            # For orthographic projection, change projection borders
            ndcx, ndcy = self._viewport.windowToNdc(x, y, checkInside=False)

            step = 0.2 * (1. if angleInDegrees < 0 else -1.)

            dx = (ndcx + 1) / 2.
            stepwidth = step * (projection.right - projection.left)
            left = projection.left - dx * stepwidth
            right = projection.right + (1. - dx) * stepwidth

            dy = (ndcy + 1) / 2.
            stepheight = step * (projection.top - projection.bottom)
            bottom = projection.bottom - dy * stepheight
            top = projection.top + (1. - dy) * stepheight

            projection.setClipping(left, right, bottom, top)

        else:
            raise RuntimeError('Unsupported camera', projection)
        return True

    def _zoomByScale(self, x, y, angleInDegrees):
        """Zoom by scaling scene (do not keep pixel under mouse invariant)."""
        scalefactor = 1.1
        if angleInDegrees < 0.:
            scalefactor = 1. / scalefactor
        self._scale.scale = scalefactor * self._scale.scale

        self._viewport.adjustCameraDepthExtent()
        return True


# FocusManager ################################################################

class FocusManager(StateMachine):
    """Manages focus across multiple event handlers

    On press an event handler can acquire focus.
    By default it looses focus when all buttons are released.
    """
    class Idle(State):
        def onPress(self, x, y, btn):
            for eventHandler in self.machine.currentEventHandler:
                requestFocus = eventHandler.handleEvent('press', x, y, btn)
                if requestFocus:
                    self.goto('focus', eventHandler, btn)
                    break

        def _processEvent(self, *args):
            for eventHandler in self.machine.currentEventHandler:
                consumeEvent = eventHandler.handleEvent(*args)
                if consumeEvent:
                    break

        def onMove(self, x, y):
            self._processEvent('move', x, y)

        def onRelease(self, x, y, btn):
            self._processEvent('release', x, y, btn)

        def onWheel(self, x, y, angle):
            self._processEvent('wheel', x, y, angle)

    class Focus(State):
        def enterState(self, eventHandler, btn):
            self.eventHandler = eventHandler
            self.focusBtns = {btn}  # Set

        enter = enterState  # silx v.0.3 support, remove when 0.4 out

        def onPress(self, x, y, btn):
            self.focusBtns.add(btn)
            self.eventHandler.handleEvent('press', x, y, btn)

        def onMove(self, x, y):
            self.eventHandler.handleEvent('move', x, y)

        def onRelease(self, x, y, btn):
            self.focusBtns.discard(btn)
            requestfocus = self.eventHandler.handleEvent('release', x, y, btn)
            if len(self.focusBtns) == 0 and not requestfocus:
                self.goto('idle')

        def onWheel(self, x, y, angleInDegrees):
            self.eventHandler.handleEvent('wheel', x, y, angleInDegrees)

    def __init__(self, eventHandlers=(), ctrlEventHandlers=None):
        self.defaultEventHandlers = eventHandlers
        self.ctrlEventHandlers = ctrlEventHandlers
        self.currentEventHandler = self.defaultEventHandlers

        states = {
            'idle': FocusManager.Idle,
            'focus': FocusManager.Focus
        }
        super(FocusManager, self).__init__(states, 'idle')

    def onKeyPress(self, key):
        if key == qt.Qt.Key_Control and self.ctrlEventHandlers is not None:
            self.currentEventHandler = self.ctrlEventHandlers

    def onKeyRelease(self, key):
        if key == qt.Qt.Key_Control:
            self.currentEventHandler = self.defaultEventHandlers

    def cancel(self):
        for handler in self.currentEventHandler:
            handler.cancel()


# CameraControl ###############################################################

class RotateCameraControl(FocusManager):
    """Combine wheel and rotate state machine for left button
    and pan when ctrl is pressed
    """
    def __init__(self, viewport,
                 orbitAroundCenter=False,
                 mode='center', scaleTransform=None,
                 selectCB=None):
        handlers = (CameraWheel(viewport, mode, scaleTransform),
                    CameraRotate(viewport, orbitAroundCenter, LEFT_BTN))
        ctrlHandlers = (CameraWheel(viewport, mode, scaleTransform),
                        CameraSelectPan(viewport, LEFT_BTN, selectCB))
        super(RotateCameraControl, self).__init__(handlers, ctrlHandlers)


class PanCameraControl(FocusManager):
    """Combine wheel, selectPan and rotate state machine for left button
    and rotate when ctrl is pressed"""
    def __init__(self, viewport,
                 orbitAroundCenter=False,
                 mode='center', scaleTransform=None,
                 selectCB=None):
        handlers = (CameraWheel(viewport, mode, scaleTransform),
                    CameraSelectPan(viewport, LEFT_BTN, selectCB))
        ctrlHandlers = (CameraWheel(viewport, mode, scaleTransform),
                        CameraRotate(viewport, orbitAroundCenter, LEFT_BTN))
        super(PanCameraControl, self).__init__(handlers, ctrlHandlers)


class CameraControl(FocusManager):
    """Combine wheel, selectPan and rotate state machine."""
    def __init__(self, viewport,
                 orbitAroundCenter=False,
                 mode='center', scaleTransform=None,
                 selectCB=None):
        handlers = (CameraWheel(viewport, mode, scaleTransform),
                    CameraSelectPan(viewport, LEFT_BTN, selectCB),
                    CameraRotate(viewport, orbitAroundCenter, RIGHT_BTN))
        super(CameraControl, self).__init__(handlers)


# PlaneRotate #################################################################

class PlaneRotate(ClickOrDrag):
    """Plane rotation using arcball interaction.

    Arcball ref.:
    Ken Shoemake. ARCBALL: A user interface for specifying three-dimensional
    orientation using a mouse. In Proc. GI '92. (1992). pp. 151-156.
    """

    def __init__(self, viewport, plane, button=RIGHT_BTN):
        self._viewport = viewport
        self._plane = plane
        self._reset()
        super(PlaneRotate, self).__init__(button)

    def _reset(self):
        self._beginNormal, self._beginCenter = None, None

    def click(self, x, y):
        pass  # No interaction

    @staticmethod
    def _sphereUnitVector(radius, center, position):
        """Returns the unit vector of the projection of position on a sphere.

        It assumes an orthographic projection.
        For perspective projection, it gives an approximation, but it
        simplifies computations and results in consistent arcball control
        in control space.

        All parameters must be in screen coordinate system
        (either pixels or normalized coordinates).

        :param float radius: The radius of the sphere.
        :param center: (x, y) coordinates of the center.
        :param position: (x, y) coordinates of the cursor position.
        :return: Unit vector.
        :rtype: numpy.ndarray of 3 floats.
        """
        center, position = numpy.array(center), numpy.array(position)

        # Normalize x and y on a unit circle
        spherecoords = (position - center) / float(radius)
        squarelength = numpy.sum(spherecoords ** 2)

        # Project on the unit sphere and compute z coordinates
        if squarelength > 1.0:  # Outside sphere: project
            spherecoords /= numpy.sqrt(squarelength)
            zsphere = 0.0
        else:  # In sphere: compute z
            zsphere = numpy.sqrt(1. - squarelength)

        spherecoords = numpy.append(spherecoords, zsphere)
        return spherecoords

    def beginDrag(self, x, y):
        # Makes sure the point defining the plane is at the center as
        # it will be the center of rotation (as rotation is applied to normal)
        self._plane.plane.point = self._plane.center

        # Store the plane normal
        self._beginNormal = self._plane.plane.normal

        _logger.debug(
            'Begin arcball, plane center %s', str(self._plane.center))

        # Do the arcball on the screen
        radius = min(self._viewport.size)
        if self._plane.center is None:
            self._beginCenter = None

        else:
            center = self._plane.objectToNDCTransform.transformPoint(
                self._plane.center, perspectiveDivide=True)
            self._beginCenter = self._viewport.ndcToWindow(
                center[0], center[1], checkInside=False)

            self._startVector = self._sphereUnitVector(
                radius, self._beginCenter, (x, y))

    def drag(self, x, y):
        if self._beginCenter is None:
            return

        # Compute rotation: this is twice the rotation of the arcball
        radius = min(self._viewport.size)
        currentvector = self._sphereUnitVector(
            radius, self._beginCenter, (x, y))
        crossprod = numpy.cross(self._startVector, currentvector)
        dotprod = numpy.dot(self._startVector, currentvector)

        quaternion = numpy.append(crossprod, dotprod)
        # Rotation was computed with Y downward, but apply in NDC, invert Y
        quaternion[1] *= -1.

        rotation = transform.Rotate()
        rotation.quaternion = quaternion

        # Convert to NDC, rotate, convert back to object
        normal = self._plane.objectToNDCTransform.transformNormal(
            self._beginNormal)
        normal = rotation.transformNormal(normal)
        normal = self._plane.objectToNDCTransform.transformNormal(
            normal, direct=False)
        self._plane.plane.normal = normal

    def endDrag(self, x, y):
        self._reset()


# PlanePan ###################################################################

class PlanePan(ClickOrDrag):
    """Pan a plane along its normal on drag."""

    def __init__(self, viewport, plane, button=LEFT_BTN):
        self._plane = plane
        self._viewport = viewport
        self._beginPlanePoint = None
        self._beginPos = None
        self._dragNdcZ = 0.
        super(PlanePan, self).__init__(button)

    def click(self, x, y):
        pass

    def beginDrag(self, x, y):
        ndc = self._viewport.windowToNdc(x, y)
        ndcZ = self._viewport._pickNdcZGL(x, y)
        # ndcZ is the panning plane
        if ndc is not None and ndcZ is not None:
            ndcPos = numpy.array((ndc[0], ndc[1], ndcZ, 1.),
                                 dtype=numpy.float32)
            scenePos = self._viewport.camera.transformPoint(
                ndcPos, direct=False, perspectiveDivide=True)
            self._beginPos = self._plane.objectToSceneTransform.transformPoint(
                scenePos, direct=False)
            self._dragNdcZ = ndcZ
        else:
            self._beginPos = None
            self._dragNdcZ = 0.

        self._beginPlanePoint = self._plane.plane.point

    def drag(self, x, y):
        if self._beginPos is not None:
            ndc = self._viewport.windowToNdc(x, y)
            if ndc is not None:
                ndcPos = numpy.array((ndc[0], ndc[1], self._dragNdcZ, 1.),
                                     dtype=numpy.float32)

                # Convert last and current NDC positions to scene coords
                scenePos = self._viewport.camera.transformPoint(
                    ndcPos, direct=False, perspectiveDivide=True)
                curPos = self._plane.objectToSceneTransform.transformPoint(
                    scenePos, direct=False)

                # Get translation in scene coords
                translation = curPos[:3] - self._beginPos[:3]

                newPoint = self._beginPlanePoint + translation

                # Keep plane point in bounds
                bounds = self._plane.parent.bounds(dataBounds=True)
                if bounds is not None:
                    newPoint = numpy.clip(
                        newPoint, a_min=bounds[0], a_max=bounds[1])

                    # Only update plane if it is in some bounds
                    self._plane.plane.point = newPoint

    def endDrag(self, x, y):
        self._beginPlanePoint = None


# PlaneControl ################################################################

class PlaneControl(FocusManager):
    """Combine wheel, selectPan and rotate state machine for plane control."""
    def __init__(self, viewport, plane,
                 mode='center', scaleTransform=None):
        handlers = (CameraWheel(viewport, mode, scaleTransform),
                    PlanePan(viewport, plane, LEFT_BTN),
                    PlaneRotate(viewport, plane, RIGHT_BTN))
        super(PlaneControl, self).__init__(handlers)


class PanPlaneRotateCameraControl(FocusManager):
    """Combine wheel, pan plane and camera rotate state machine."""
    def __init__(self, viewport, plane,
                 mode='center', scaleTransform=None):
        handlers = (CameraWheel(viewport, mode, scaleTransform),
                    PlanePan(viewport, plane, LEFT_BTN),
                    CameraRotate(viewport,
                                 orbitAroundCenter=False,
                                 button=RIGHT_BTN))
        super(PanPlaneRotateCameraControl, self).__init__(handlers)


class PanPlaneZoomOnWheelControl(FocusManager):
    """Combine zoom on wheel and pan plane state machines."""
    def __init__(self, viewport, plane,
                 mode='center',
                 orbitAroundCenter=False,
                 scaleTransform=None):
        handlers = (CameraWheel(viewport, mode, scaleTransform),
                    PlanePan(viewport, plane, LEFT_BTN))
        ctrlHandlers = (CameraWheel(viewport, mode, scaleTransform),
                        CameraRotate(viewport, orbitAroundCenter, LEFT_BTN))
        super(PanPlaneZoomOnWheelControl, self).__init__(handlers, ctrlHandlers)