1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  *   https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  *   Redistribution and use in source and binary forms, with or
10  *   without modification, are permitted provided that the following
11  *   conditions are met:
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above
15  *     copyright notice, this list of conditions and the following
16  *     disclaimer in the documentation and/or other materials provided
17  *     with the distribution.
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  *   POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_GUI_TRACKBALL_HPP_
34 #define DART_GUI_TRACKBALL_HPP_
35 
36 #include <Eigen/Core>
37 #include <Eigen/Geometry>
38 
39 namespace dart {
40 namespace gui {
41 
42 /// \brief
43 class Trackball
44 {
45 public:
46   // To get byte-aligned Eigen vectors
47   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
48 
49   /// \brief Default constructor
50   Trackball();
51 
52   /// \brief Constructor
53   Trackball(const Eigen::Vector2d& _center, double _radius);
54 
55   /// \brief Set the starting position to the project of (x,y) on the trackball
56   void startBall(double _x, double _y);
57 
58   /// \brief Update the current rotation to rotate from mStartPos to the
59   ///        projection of (x,y) on trackball, then update mStartPos
60   void updateBall(double _x, double _y);
61 
62   /// \brief Apply the current rotation to openGL env
63   void applyGLRotation();
64 
65   /// \brief Draw the trackball on screen
66   void draw(int _winWidth, int _winHeight);
67 
68   /// \brief
69   void setTrackball(const Eigen::Vector2d& _center, const double _radius);
70 
71   /// \brief
72   void setCenter(const Eigen::Vector2d& _center);
73 
74   /// \brief
75   void setRadius(const double _radius);
76 
77   /// \brief
78   void setQuaternion(const Eigen::Quaterniond& _q);
79 
80   /// \brief
81   Eigen::Quaterniond getCurrQuat() const;
82 
83   /// \brief
84   Eigen::Matrix3d getRotationMatrix() const;
85 
86   /// \brief
87   Eigen::Vector2d getCenter() const;
88 
89   /// \brief
90   double getRadius() const;
91 
92 private:
93   /// \brief Project screen coordinate (x,y) to the trackball
94   Eigen::Vector3d mouseOnSphere(double _mouseX, double _mouseY) const;
95 
96   /// \brief Compute the quaternion that rotates from vector "from" to vector
97   ///        "to"
98   Eigen::Quaterniond quatFromVectors(
99       const Eigen::Vector3d& _from, const Eigen::Vector3d& _to) const;
100 
101   /// \brief
102   Eigen::Vector2d mCenter;
103 
104   /// \brief
105   double mRadius;
106 
107   /// \brief
108   Eigen::Vector3d mStartPos;
109 
110   /// \brief
111   Eigen::Quaterniond mCurrQuat;
112 };
113 
114 } // namespace gui
115 } // namespace dart
116 
117 #endif // DART_GUI_TRACKBALL_HPP_
118