SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SciBall.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2013 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// \author James Hughes
30 /// \date April 2013
31 
32 #ifndef SPIRE_APPSPECIFIC_SCIRUN_SCIBALL_H
33 #define SPIRE_APPSPECIFIC_SCIRUN_SCIBALL_H
34 
35 #include <cstdint>
36 
38 
39 #include "spire/Interface.h"
40 
41 namespace SCIRun {
42 namespace Gui {
43 
44 /// A reimplementation of Ken Shoemake's arcball camera. SCIRun 4's camera
45 /// system is based off of Ken's code. The Code appears in Graphics Gems 4,
46 /// III.1.
47 ///
48 /// Unless specified otherwise, all calculations and variables stored in this
49 /// class are relative to the target coordinate system (TCS) for which there is
50 /// a transformation from screen space to TCS given by the screenToTCS
51 /// constructor parameter.
52 ///
53 /// If the screenToTCS parameter in the constructor is left as the identity
54 /// matrix then all values are given in screen coordinates.
55 /// Screen coordinates are (x \in [-1,1]) and (y \in [-1,1]) where (0,0) is the
56 /// center of the screen.
57 /// \todo Extend this class to include Mouse screen coords -> object space
58 /// calculations. That way we can rotate around a particular object.
59 /// May also want to visually represent the sphere when we perform this
60 /// calculation.
61 /// \todo Provide method of setting the default orientation of the object.
62 class SciBall
63 {
64 public:
65  /// \param center Center of the arcball in TCS (screen coordinates if
66  /// screenToTCS = identity). Generally this will
67  /// always be (0,0,0). But you may move the center
68  /// in and out of the screen plane to various effect.
69  /// \param radius Radius in TCS. For screen coordinates, a good
70  /// default is 0.75.
71  /// \param screenToTCS Transformation from screen coordinates
72  /// to TCS. 'center' and 'radius' are given in TCS.
73  SciBall(const spire::V3& center, float radius,
74  const spire::M44& screenToTCS = spire::M44());
75  virtual ~SciBall();
76 
77  /// Initiate an arc ball drag given the mouse click in screen coordinates.
78  /// \param mouseScreenCoords Mouse screen coordinates.
79  void beginDrag(const spire::V2& mouseScreenCoords);
80 
81  /// Informs the arcball when the mouse has been dragged.
82  /// \param mouseScreenCoords Mouse screen coordinates.
83  void drag(const spire::V2& mouseScreenCoords);
84 
85  /// Retrieves the current transformation in TCS.
86  /// Obtains full transformation of object in question. If the arc ball is
87  /// being used to control camera rotation, then this will contain all
88  /// concatenated camera transformations. The current state of the camera
89  /// is stored in the quaternions mQDown and mQNow. mMatNow is calculated
90  /// from mQNow.
91  spire::M44 getTransformation() const;
92 
93 private:
94 
95  /// Calculates our position on the ArcBall from 2D mouse position.
96  /// \param tscMouse TSC coordinates of mouse click.
97  spire::V3 mouseOnSphere(const spire::V3& tscMouse);
98 
99  /// Construct a unit quaternion from two points on the unit sphere.
100  static spire::Quat quatFromUnitSphere(const spire::V3& from, const spire::V3& to);
101 
102  spire::V3 mCenter; ///< Center of the arcball in target coordinate system.
103  float mRadius; ///< Radius of the arcball in target coordinate system.
104 
105  /// \note Both mQNow and mQDown would need to be updated if we allowed
106  /// default transformations.
107 
108  spire::Quat mQNow; ///< Current state of the rotation taking into account mouse.
109  ///< Essentially QDrag * QDown (QDown is a applied first, just
110  ///< as in matrix multiplication).
111  spire::Quat mQDown; ///< State of the rotation since mouse down.
112  spire::Quat mQDrag; ///< Dragged transform. Knows nothing of any prior
113  ///< transformations.
114 
115  spire::V3 mVNow; ///< Most current TCS position of mouse (during drag).
116  spire::V3 mVDown; ///< TCS position of mouse when the drag was begun.
117  spire::V3 mVSphereFrom; ///< vDown mapped to the sphere of 'mRadius' centered at 'mCenter' in TCS.
118  spire::V3 mVSphereTo; ///< vNow mapped to the sphere of 'mRadius' centered at 'mCenter' in TCS.
119 
120  spire::M44 mMatNow; ///< Matrix representing the current rotation.
121 
122  /// \todo Add in constraint sets (you can display handles and constrain
123  /// rotations along those handles).
124 
125  /// Transform from screen coordinates to the target coordinate system.
126  spire::M44 mScreenToTCS;
127 };
128 
129 } // namespace Gui
130 } // namespace SCIRun
131 
132 #endif
spire::M44 getTransformation() const
Definition: SciBall.cc:137
void drag(const spire::V2 &mouseScreenCoords)
Definition: SciBall.cc:103
void beginDrag(const spire::V2 &mouseScreenCoords)
Definition: SciBall.cc:92
SciBall(const spire::V3 &center, float radius, const spire::M44 &screenToTCS=spire::M44())
Definition: SciBall.cc:42
Definition: SciBall.h:62
virtual ~SciBall()
Definition: SciBall.cc:59