Quantum Phenomena Explorer 7.9.8
Interactive quantum-phenomena Explorer Tutor — source documentation
Loading...
Searching...
No Matches
quantumviewcamera2d.h
Go to the documentation of this file.
1/**
2 * @file quantumviewcamera2d.h
3 * @brief Declares the quantumviewcamera2d visualization component used by QPE.
4 * @ingroup qpe_graphics
5 *
6 * Source: @c graphics/renderer/quantumviewcamera2d.h. This file is part of Quantum Phenomena Explorer (QPE),
7 * maintained by Officina Turini. See LICENSE_IT.txt / LICENSE.txt for the
8 * QPE freeware source-available terms and THIRD_PARTY_NOTICES.txt for
9 * third-party components.
10 */
11
12#pragma once
13
14#include <algorithm>
15#include <cmath>
16
17namespace qpe {
18
19// Pure display-space camera used by QuantumGLView.
20// It never modifies psi, V, the simulation grid, dt, or any observable.
21/**
22 * @brief Implements the Quantum View Camera2D component.
23 *
24 * The class provides one focused service within the QPE architecture and is documented here independently of the experiment-level Help.
25 */
27{
28public:
29 /**
30 * @brief Sets domain.
31 * @param width The width value used by the operation.
32 * @param width The width value used by the operation.
33 */
34 void setDomain(float width, float height)
35 {
36 const float safeWidth = std::max(0.0f, width);
37 /**
38 * @brief Carries out max.
39 * @param f The f value used by the operation.
40 * @param height The height value used by the operation.
41 */
42 const float safeHeight = std::max(0.0f, height);
43 /**
44 * @brief Carries out abs.
45 * @param domainHeight_ The domain height value used by the operation.
46 */
47 const bool changed = std::abs(safeWidth - domainWidth_) > 1.0e-6f ||
48 std::abs(safeHeight - domainHeight_) > 1.0e-6f;
49 domainWidth_ = safeWidth;
50 domainHeight_ = safeHeight;
51 if (changed) {
52 /**
53 * @brief Resets the component to its initial state.
54 */
55 reset();
56 } else {
57 /**
58 * @brief Carries out clamp center.
59 */
61 }
62 }
63
64 /**
65 * @brief Resets the component to its initial state.
66 */
67 void reset()
68 {
69 zoom_ = 1.0f;
70 centerX_ = 0.0f;
71 centerY_ = 0.0f;
72 }
73
74 /**
75 * @brief Carries out zoom.
76 */
77 float zoom() const { return zoom_; }
78 /**
79 * @brief Carries out center x.
80 */
81 float centerX() const { return centerX_; }
82 /**
83 * @brief Carries out center y.
84 */
85 float centerY() const { return centerY_; }
86 /**
87 * @brief Carries out domain width.
88 */
89 float domainWidth() const { return domainWidth_; }
90 /**
91 * @brief Carries out domain height.
92 */
93 float domainHeight() const { return domainHeight_; }
94 /**
95 * @brief Carries out view width.
96 */
97 float viewWidth() const { return zoom_ > 0.0f ? domainWidth_ / zoom_ : domainWidth_; }
98 /**
99 * @brief Carries out view height.
100 */
101 float viewHeight() const { return zoom_ > 0.0f ? domainHeight_ / zoom_ : domainHeight_; }
102 /**
103 * @brief Carries out max zoom.
104 */
105 float maxZoom() const { return 32.0f; }
106
107 /**
108 * @brief Carries out max.
109 * @param factor The factor value used by the operation.
110 * @param u The u value used by the operation.
111 * @param f The f value used by the operation.
112 */
113 bool zoomAtNormalized(float factor, double u, double v)
114 {
115 if (!(domainWidth_ > 0.0f) || !(domainHeight_ > 0.0f) || !(factor > 0.0f)) {
116 return false;
117 }
118
119 /**
120 * @brief Performs max.
121 * @param u The u value used by the operation.
122 */
123 u = std::max(0.0, std::min(1.0, u));
124 /**
125 * @brief Carries out max.
126 * @param v The v value used by the operation.
127 */
128 v = std::max(0.0, std::min(1.0, v));
129
130 float anchorX = 0.0f;
131 float anchorY = 0.0f;
132 /**
133 * @brief Carries out physical from normalized.
134 * @param u The u value used by the operation.
135 * @param v The v value used by the operation.
136 * @param anchorX The anchor x value used by the operation.
137 * @param anchorY The anchor y value used by the operation.
138 */
139 physicalFromNormalized(u, v, anchorX, anchorY);
140
141 /**
142 * @brief Carries out max.
143 * @param f The f value used by the operation.
144 * @param factor The factor value used by the operation.
145 */
146 const float newZoom = std::max(1.0f, std::min(maxZoom(), zoom_ * factor));
147 if (std::abs(newZoom - zoom_) <= 1.0e-6f) {
148 return false;
149 }
150
151 zoom_ = newZoom;
152 /**
153 * @brief Carries out view width.
154 */
155 centerX_ = anchorX - static_cast<float>(u - 0.5) * viewWidth();
156 /**
157 * @brief Carries out view height.
158 */
159 centerY_ = anchorY - static_cast<float>(v - 0.5) * viewHeight();
160 /**
161 * @brief Carries out clamp center.
162 */
163 clampCenter();
164 return true;
165 }
166
167 /**
168 * @brief Carries out clamp center.
169 * @param dx The dx value used by the operation.
170 * @param f The f value used by the operation.
171 */
172 void panPhysical(float dx, float dy)
173 {
174 if (!(domainWidth_ > 0.0f) || !(domainHeight_ > 0.0f)) {
175 return;
176 }
177 centerX_ += dx;
178 centerY_ += dy;
179 /**
180 * @brief Performs clamp center.
181 */
182 clampCenter();
183 }
184
185 /**
186 * @brief Carries out physical from normalized.
187 * @param u The u value used by the operation.
188 * @param v The v value used by the operation.
189 * @param x X coordinate/value.
190 * @param viewWidth The view width value used by the operation.
191 */
192 void physicalFromNormalized(double u, double v, float &x, float &y) const
193 {
194 x = centerX_ + static_cast<float>(u - 0.5) * viewWidth();
195 /**
196 * @brief Performs view height.
197 */
198 y = centerY_ + static_cast<float>(v - 0.5) * viewHeight();
199 }
200
201 /**
202 * @brief Returns normalized from physical.
203 * @param x X coordinate/value.
204 * @param y Y coordinate/value.
205 * @param u The u value used by the operation.
206 * @param viewWidth The view width value used by the operation.
207 */
208 void normalizedFromPhysical(float x, float y, double &u, double &v) const
209 {
210 const float vw = viewWidth();
211 /**
212 * @brief Performs view height.
213 */
214 const float vh = viewHeight();
215 u = vw > 0.0f ? static_cast<double>((x - centerX_) / vw) + 0.5 : 0.5;
216 v = vh > 0.0f ? static_cast<double>((y - centerY_) / vh) + 0.5 : 0.5;
217 }
218
219 /**
220 * @brief Carries out texture scale x.
221 */
222 float textureScaleX() const
223 {
224 return zoom_ > 0.0f ? 1.0f / zoom_ : 1.0f;
225 }
226
227 /**
228 * @brief Carries out texture scale y.
229 */
230 float textureScaleY() const
231 {
232 return zoom_ > 0.0f ? 1.0f / zoom_ : 1.0f;
233 }
234
235 /**
236 * @brief Carries out max.
237 * @param f The f value used by the operation.
238 */
239 float textureOffsetX() const
240 {
241 if (!(domainWidth_ > 0.0f)) {
242 return 0.0f;
243 }
244 return 0.5f + centerX_ / domainWidth_ - 0.5f * textureScaleX();
245 }
246
247 /**
248 * @brief Performs texture offset y.
249 * @param f The f value used by the operation.
250 */
251 float textureOffsetY() const
252 {
253 if (!(domainHeight_ > 0.0f)) {
254 return 0.0f;
255 }
256 return 0.5f + centerY_ / domainHeight_ - 0.5f * textureScaleY();
257 }
258
259private:
260 /**
261 * @brief Performs clamp center.
262 * @param f The f value used by the operation.
263 */
265 {
266 if (!(domainWidth_ > 0.0f) || !(domainHeight_ > 0.0f)) {
267 centerX_ = 0.0f;
268 centerY_ = 0.0f;
269 zoom_ = 1.0f;
270 return;
271 }
272
273 /**
274 * @brief Performs max.
275 * @param f The f value used by the operation.
276 * @param viewWidth The view width value used by the operation.
277 */
278 const float maxCenterX = std::max(0.0f, 0.5f * (domainWidth_ - viewWidth()));
279 /**
280 * @brief Carries out max.
281 * @param f The f value used by the operation.
282 * @param viewHeight The view height value used by the operation.
283 */
284 const float maxCenterY = std::max(0.0f, 0.5f * (domainHeight_ - viewHeight()));
285 /**
286 * @brief Carries out max.
287 * @param maxCenterX The max center x value used by the operation.
288 * @param centerX_ The center x value used by the operation.
289 */
290 centerX_ = std::max(-maxCenterX, std::min(maxCenterX, centerX_));
291 /**
292 * @brief Carries out max.
293 * @param maxCenterY The max center y value used by the operation.
294 * @param centerY_ The center y value used by the operation.
295 */
296 centerY_ = std::max(-maxCenterY, std::min(maxCenterY, centerY_));
297 }
298
299 float domainWidth_ = 0.0f;
300 float domainHeight_ = 0.0f;
301 float zoom_ = 1.0f;
302 float centerX_ = 0.0f;
303 float centerY_ = 0.0f;
304};
305
306} // namespace qpe
Implements the Quantum View Camera2D component.
float domainWidth() const
Carries out domain width.
void panPhysical(float dx, float dy)
Carries out clamp center.
float textureOffsetY() const
Performs texture offset y.
float centerY() const
Carries out center y.
void setDomain(float width, float height)
Sets domain.
void normalizedFromPhysical(float x, float y, double &u, double &v) const
Returns normalized from physical.
void reset()
Resets the component to its initial state.
float textureScaleY() const
Carries out texture scale y.
float maxZoom() const
Carries out max zoom.
bool zoomAtNormalized(float factor, double u, double v)
Carries out max.
float centerX() const
Carries out center x.
void clampCenter()
Performs clamp center.
float textureOffsetX() const
Carries out max.
float textureScaleX() const
Carries out texture scale x.
float viewWidth() const
Carries out view width.
float domainHeight() const
Carries out domain height.
float zoom() const
Carries out zoom.
float viewHeight() const
Carries out view height.
void physicalFromNormalized(double u, double v, float &x, float &y) const
Carries out physical from normalized.