Developer documentation
Version 3.0.3-105-gd3941f44
base.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __gui_mrview_mode_base_h__
18#define __gui_mrview_mode_base_h__
19
20#include "gui/opengl/gl.h"
22#include "gui/projection.h"
23#include "gui/mrview/window.h"
25
26#define ROTATION_INC 0.002
27#define MOVE_IN_OUT_FOV_MULTIPLIER 1.0e-3f
28
29namespace MR
30{
31 namespace GUI
32 {
33 namespace MRView
34 {
35
36 namespace Tool {
37 class Dock;
38 }
39
40 namespace Mode
41 {
42
43 const int FocusContrast = 0x00000001;
44 const int MoveTarget = 0x00000002;
45 const int TiltRotate = 0x00000004;
46 const int MoveSlice = 0x00000008;
47 const int ShaderThreshold = 0x10000000;
48 const int ShaderTransparency = 0x20000000;
49 const int ShaderLighting = 0x40000000;
50 const int ShaderClipping = 0x80000008;
51
52
53
54 class Slice;
55 class Ortho;
56 class Volume;
57 class LightBox;
59 { MEMALIGN(ModeGuiVisitor)
60 public:
61 virtual void update_base_mode_gui(const Base&) {}
62 virtual void update_slice_mode_gui(const Slice&) {}
63 virtual void update_ortho_mode_gui(const Ortho&) {}
64 virtual void update_volume_mode_gui(const Volume&) {}
65 virtual void update_lightbox_mode_gui(const LightBox&) {}
66 };
67
68
69
70 class Base : public QObject
71 { MEMALIGN(Base)
72 public:
73 Base (int flags = FocusContrast | MoveTarget);
74 virtual ~Base ();
75
76 Window& window () const { return *Window::main; }
77 Projection projection;
78 const int features;
79 QList<ImageBase*> overlays_for_3D;
80 bool update_overlays;
81
82 virtual void paint (Projection& projection);
83 virtual void mouse_press_event ();
84 virtual void mouse_release_event ();
85 virtual void reset_event ();
86 virtual void slice_move_event (float x);
87 virtual void set_focus_event ();
88 virtual void contrast_event ();
89 virtual void pan_event ();
90 virtual void panthrough_event ();
91 virtual void tilt_event ();
92 virtual void rotate_event ();
93 virtual void image_changed_event () {}
94 virtual const Projection* get_current_projection() const;
95 virtual void reset_windowing ();
96
97 virtual void request_update_mode_gui(ModeGuiVisitor& visitor) const {
98 visitor.update_base_mode_gui(*this); }
99
100 void paintGL ();
101
102 const Image* image () const { return window().image(); }
103 const Eigen::Vector3f& focus () const { return window().focus(); }
104 const Eigen::Vector3f& target () const { return window().target(); }
105 float FOV () const { return window().FOV(); }
106 int plane () const { return window().plane(); }
107 Eigen::Quaternionf orientation () const {
108 if (snap_to_image()) {
109 if (image())
110 return Eigen::Quaternionf (image()->header().transform().rotation().cast<float>());
111 else
112 return Eigen::Quaternionf::Identity();
113 }
114 return window().orientation();
115 }
116
117 int width () const { return glarea()->width(); }
118 int height () const { return glarea()->height(); }
119 bool snap_to_image () const { return window().snap_to_image(); }
120
121 Image* image () { return window().image(); }
122
123 void move_target_to_focus_plane (const ModelViewProjection& projection) {
124 Eigen::Vector3f in_plane_target = projection.model_to_screen (target());
125 in_plane_target[2] = projection.depth_of (focus());
126 set_target (projection.screen_to_model (in_plane_target));
127 }
128 void set_visible (bool v) { if(visible != v) { visible = v; updateGL(); } }
129 void set_focus (const Eigen::Vector3f& p) { window().set_focus (p); }
130 void set_target (const Eigen::Vector3f& p) { window().set_target (p); }
131 void set_FOV (float value) { window().set_FOV (value); }
132 void set_plane (int p) { window().set_plane (p); }
133 void set_orientation (const Eigen::Quaternionf& V) { window().set_orientation (V); }
134 void reset_orientation () {
135 if (image())
136 set_orientation (Eigen::Quaternionf (image()->header().transform().rotation().cast<float>()));
137 else
138 set_orientation (Eigen::Quaternionf::Identity());
139 }
140
141 GL::Area* glarea () const {
142 return reinterpret_cast <GL::Area*> (window().glarea);
143 }
144
145 Eigen::Vector3f get_through_plane_translation (float distance, const ModelViewProjection& projection) const {
146 Eigen::Vector3f move (projection.screen_normal());
147 move.normalize();
148 move *= distance;
149 return move;
150 }
151
152 Eigen::Vector3f get_through_plane_translation_FOV (int increment, const ModelViewProjection& projection) {
153 return get_through_plane_translation (MOVE_IN_OUT_FOV_MULTIPLIER * increment * FOV(), projection);
154 }
155
156 void render_tools (const Projection& projection, bool is_3D = false, int axis = 0, int slice = 0) {
157 QList<QAction*> tools = window().tools()->actions();
158 for (int i = 0; i < tools.size(); ++i) {
159 Tool::Dock* dock = dynamic_cast<Tool::__Action__*>(tools[i])->dock;
160 if (dock) {
162 dock->tool->draw (projection, is_3D, axis, slice);
164 }
165 }
166 }
167
168 void setup_projection (const int, ModelViewProjection&) const;
169 void setup_projection (const Eigen::Quaternionf&, ModelViewProjection&) const;
170 void setup_projection (const GL::mat4&, ModelViewProjection&) const;
171
172 Eigen::Quaternionf get_tilt_rotation (const ModelViewProjection& proj) const;
173 Eigen::Quaternionf get_rotate_rotation (const ModelViewProjection& proj) const;
174
175 Eigen::Vector3f voxel_at (const Eigen::Vector3f& pos) const {
176 if (!image()) return Eigen::Vector3f { NAN, NAN, NAN };
177 const Eigen::Vector3f result = image()->scanner2voxel().cast<float>() * pos;
178 return result;
179 }
180
181 void draw_crosshairs (const Projection& with_projection) const {
182 if (window().show_crosshairs())
183 with_projection.render_crosshairs (focus());
184 }
185
186 void draw_orientation_labels (const Projection& with_projection) const {
187 if (window().show_orientation_labels())
188 with_projection.draw_orientation_labels();
189 }
190
191 int slice (int axis) const { return std::round (voxel_at (focus())[axis]); }
192 int slice () const { return slice (plane()); }
193
194 void updateGL () { window().updateGL(); }
195
196 protected:
197 void slice_move_event (const ModelViewProjection& proj, float x);
199 void pan_event (const ModelViewProjection& proj);
201 void tilt_event (const ModelViewProjection& proj);
203
204 GL::mat4 adjust_projection_matrix (const GL::mat4& Q, int proj) const;
206 return adjust_projection_matrix (Q, plane());
207 }
208
209 void reset_view ();
210
212 };
213
214
215
216
218 class __Action__ : public QAction
219 { NOMEMALIGN
220 public:
221 __Action__ (QActionGroup* parent,
222 const char* const name,
223 const char* const description,
224 int index) :
225 QAction (name, parent) {
226 setCheckable (true);
227 setShortcut (tr (std::string ("F"+str (index)).c_str()));
228 setStatusTip (tr (description));
229 }
230
231 virtual Base* create() const = 0;
232 };
234
235
236
237 template <class T> class Action : public __Action__
238 { NOMEMALIGN
239 public:
240 Action (QActionGroup* parent,
241 const char* const name,
242 const char* const description,
243 int index) :
244 __Action__ (parent, name, description, index) { }
245
246 virtual Base* create() const {
247 return new T;
248 }
249 };
250
251
252 }
253
254
255 }
256 }
257}
258
259
260#endif
261
262
virtual Base * create() const
Definition: base.h:246
Action(QActionGroup *parent, const char *const name, const char *const description, int index)
Definition: base.h:240
void slice_move_event(const ModelViewProjection &proj, float x)
void set_focus_event(const ModelViewProjection &proj)
GL::mat4 adjust_projection_matrix(const GL::mat4 &Q) const
Definition: base.h:205
GL::mat4 adjust_projection_matrix(const GL::mat4 &Q, int proj) const
void pan_event(const ModelViewProjection &proj)
void panthrough_event(const ModelViewProjection &proj)
void tilt_event(const ModelViewProjection &proj)
void rotate_event(const ModelViewProjection &proj)
virtual void draw(const Projection &transform, bool is_3D, int axis, int slice)
void set_plane(int p)
Definition: window.h:123
static Window * main
Definition: window.h:160
const Eigen::Vector3f & focus() const
Definition: window.h:112
const Image * image() const
Definition: window.h:97
bool snap_to_image() const
Definition: window.h:117
void set_target(const Eigen::Vector3f &p)
Definition: window.h:121
QActionGroup * tools() const
Definition: window.h:100
const Eigen::Quaternionf & orientation() const
Definition: window.h:116
int plane() const
Definition: window.h:115
float FOV() const
Definition: window.h:114
const Eigen::Vector3f & target() const
Definition: window.h:113
void set_FOV(float value)
Definition: window.h:122
void set_orientation(const Eigen::Quaternionf &V)
Definition: window.h:124
void set_focus(const Eigen::Vector3f &p)
Definition: window.h:120
Eigen::Vector3f screen_normal() const
Definition: projection.h:133
float depth_of(const Eigen::Vector3f &x) const
Definition: projection.h:73
Eigen::Vector3f model_to_screen(const Eigen::Vector3f &x) const
Definition: projection.h:79
Eigen::Vector3f screen_to_model(float x, float y, float depth) const
Definition: projection.h:101
void draw_orientation_labels() const
void render_crosshairs(const Eigen::Vector3f &focus) const
Definition: projection.h:196
constexpr I round(const T x)
Definition: math.h:64
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
Definition: SH.h:233
#define NOMEMALIGN
Definition: memory.h:22
void assert_context_is_current(QWidget *glarea=nullptr)
Definition: gl.h:100
const int ShaderClipping
Definition: base.h:50
const int TiltRotate
Definition: base.h:45
const int MoveTarget
Definition: base.h:44
const int MoveSlice
Definition: base.h:46
const int ShaderThreshold
Definition: base.h:47
const int ShaderLighting
Definition: base.h:49
const int ShaderTransparency
Definition: base.h:48
const int FocusContrast
Definition: base.h:43
Dock * create(const QString &text, bool floating)
Definition: base.h:210
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247
int axis
size_t index
#define MOVE_IN_OUT_FOV_MULTIPLIER
Definition: base.h:27
#define MEMALIGN(...)
Definition: types.h:185
const std::string name
Definition: thread.h:108