Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfArcBall.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20/*
21Derived from Magnum, with the following notice:
22
23 Original authors — credit is appreciated but not required:
24
25 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
26 Vladimír Vondruš <mosra@centrum.cz>
27 2020 — Nghia Truong <nghiatruong.vn@gmail.com>
28
29 This is free and unencumbered software released into the public domain.
30
31 Anyone is free to copy, modify, publish, use, compile, sell, or distribute
32 this software, either in source code form or as a compiled binary, for any
33 purpose, commercial or non-commercial, and by any means.
34
35 In jurisdictions that recognize copyright laws, the author or authors of
36 this software dedicate any and all copyright interest in the software to
37 the public domain. We make this dedication for the benefit of the public
38 at large and to the detriment of our heirs and successors. We intend this
39 dedication to be an overt act of relinquishment in perpetuity of all
40 present and future rights to this software under copyright law.
41
42 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
46 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
47 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48*/
49
50#ifndef Magnum_Examples_ArcBall_h
51#define Magnum_Examples_ArcBall_h
52
53#include <Magnum/Magnum.h>
54#include <Magnum/Math/Functions.h>
55#include <Magnum/Math/DualQuaternion.h>
56#include <Magnum/Math/Vector2.h>
57#include <Magnum/Math/Vector3.h>
58
59
60using namespace Magnum;
61
62
63namespace TissueForge::rendering {
64
65
66 /* Implementation of Ken Shoemake's arcball camera with smooth navigation
67 feature: https://www.talisman.org/~erlkonig/misc/shoemake92-arcball.pdf */
68 class ArcBall {
69 public:
70 ArcBall(const Vector3& cameraPosition, const Vector3& viewCenter,
71 const Vector3& upDir, Deg fov, const Magnum::Vector2i& windowSize);
72
73 /* Set the camera view parameters: eye position, view center, up
74 direction */
75 void setViewParameters(const Vector3& eye, const Vector3& viewCenter,
76 const Vector3& upDir);
77
78 /*
79 * Set the camera view parameters: eye position, view center, up
80 * direction, only rotates the view to the given eye position.
81 */
82 void rotateToAxis(const Vector3& axis, float distance);
83
84 /* Reset the camera to its initial position, view center, and up dir */
85 void reset();
86
87 /* Update screen size after the window has been resized */
88 void reshape(const Magnum::Vector2i& windowSize) { _windowSize = windowSize; }
89
90 /* Update any unfinished transformation due to lagging, return true if
91 the camera matrices have changed */
92 bool updateTransformation();
93
94 /* Get/set the amount of lagging such that the camera will (slowly)
95 smoothly navigate. Lagging must be in [0, 1) */
96 Float lagging() const { return _lagging; }
97 void setLagging(Float lagging);
98
99 /* Initialize the first (screen) mouse position for camera
100 transformation. This should be called in mouse pressed event. */
101 void initTransformation(const Magnum::Vector2i& mousePos);
102
103 /* Rotate the camera from the previous (screen) mouse position to the
104 current (screen) position */
105 void rotate(const Magnum::Vector2i& mousePos);
106
107 /* Translate the camera from the previous (screen) mouse position to
108 the current (screen) mouse position */
109 void translate(const Magnum::Vector2i& mousePos);
110
111 /* Translate the camera by the delta amount of (NDC) mouse position.
112 Note that NDC position must be in [-1, -1] to [1, 1]. */
113 void translateDelta(const Vector2& translationNDC);
114
115 /* Zoom the camera (positive delta = zoom in, negative = zoom out) */
116 void zoom(Float delta);
117
118 /* zoom absolute */
119 void zoomTo(Float delta);
120
121 /* Get the camera's view transformation as a qual quaternion */
122 const DualQuaternion& view() const { return _view; }
123
124 /* Get the camera's view transformation as a matrix */
125 Matrix4 viewMatrix() const { return _view.toMatrix(); }
126
127 /* Get the camera's inverse view matrix (which also produces
128 transformation of the camera) */
129 Matrix4 inverseViewMatrix() const { return _inverseView.toMatrix(); }
130
131 /* Get the camera's transformation as a dual quaternion */
132 const DualQuaternion& transformation() const { return _inverseView; }
133
134 /* Get the camera's transformation matrix */
135 Matrix4 transformationMatrix() const { return _inverseView.toMatrix(); }
136
137 /* Return the distance from the camera position to the center view */
138 Float viewDistance() const { return Math::abs(_targetZooming); }
139
143 void rotateByEulerAngles(const Vector3& eulerAngles);
144
145 void rotateToEulerAngles(const Vector3& eulerAngles);
146
147 protected:
148 /* Update the camera transformations */
149 void updateInternalTransformations();
150
151 /* Transform from screen coordinate to NDC - normalized device
152 coordinate. The top-left of the screen corresponds to [-1, 1] NDC,
153 and the bottom right is [1, -1] NDC. */
154 Vector2 screenCoordToNDC(const Magnum::Vector2i& mousePos) const;
155
156 Deg _fov;
157 Magnum::Vector2i _windowSize;
158
159 Vector2 _prevMousePosNDC;
160 Float _lagging{};
161
162 Vector3 _targetPosition, _currentPosition, _positionT0;
163 Quaternion _targetQRotation, _currentQRotation, _qRotationT0;
164 Float _targetZooming, _currentZooming, _zoomingT0;
165 DualQuaternion _view, _inverseView;
166 };
167
168}
169
170#endif
void rotateByEulerAngles(const Vector3 &eulerAngles)
Tissue Forge rendering and visualization.
Definition tfAngle.h:38