Developer documentation
Version 3.0.3-105-gd3941f44
displayable.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_displayable_h__
18#define __gui_mrview_displayable_h__
19
20#include "math/math.h"
21
22#include "colourmap.h"
23#include "gui/opengl/gl.h"
24#include "gui/opengl/shader.h"
25#include "gui/projection.h"
26
27
28namespace MR
29{
30 class ProgressBar;
31
32 namespace GUI
33 {
34
35 namespace MRView
36 {
37
38 class Window;
39
40 const uint32_t InvertScale = 0x08000000;
41 const uint32_t DiscardLower = 0x20000000;
42 const uint32_t DiscardUpper = 0x40000000;
43 const uint32_t Transparency = 0x80000000;
44 const uint32_t Lighting = 0x01000000;
45 const uint32_t DiscardLowerEnabled = 0x00100000;
46 const uint32_t DiscardUpperEnabled = 0x00200000;
47 const uint32_t TransparencyEnabled = 0x00400000;
48 const uint32_t LightingEnabled = 0x00800000;
49
50 class Image;
51 namespace Tool { class BaseFixel; }
52 namespace Tool { class Connectome; }
53 namespace Tool { class Tractogram; }
56 public:
57 virtual void render_image_colourbar (const Image&) {}
58 virtual void render_fixel_colourbar (const Tool::BaseFixel&) {}
60 };
61
62 class Displayable : public QAction
64 Q_OBJECT
65
66 public:
67 Displayable (const std::string& filename);
68
69 virtual ~Displayable ();
70
71 virtual void request_render_colourbar(DisplayableVisitor&) {}
72
73 const std::string& get_filename () const {
74 return filename;
75 }
76
77 float scaling_min () const {
78 return display_midpoint - 0.5f * display_range;
79 }
80
81 float scaling_max () const {
82 return display_midpoint + 0.5f * display_range;
83 }
84
85 float scaling_min_thresholded () const {
86 return std::max(scaling_min(), lessthan);
87 }
88
89 float scaling_max_thresholded () const {
90 return std::min(scaling_max(), greaterthan);
91 }
92
93 float scaling_rate () const {
94 return 1e-3 * (value_max - value_min);
95 }
96
97 float intensity_min () const {
98 return value_min;
99 }
100
101 float intensity_max () const {
102 return value_max;
103 }
104
105 void set_min_max (float min, float max) {
106 value_min = min;
107 value_max = max;
109 }
110
111 void set_windowing (float min, float max) {
112 display_range = max - min;
113 display_midpoint = 0.5 * (min + max);
114 emit scalingChanged();
115 }
116 void adjust_windowing (const QPoint& p) {
117 adjust_windowing (p.x(), p.y());
118 }
119
120 void reset_windowing () {
121 set_windowing (value_min, value_max);
122 }
123
124 void adjust_windowing (float brightness, float contrast) {
125 display_midpoint -= 0.0005f * display_range * brightness;
126 display_range *= std::exp (-0.002f * contrast);
127 emit scalingChanged();
128 }
129
130 uint32_t flags () const { return flags_; }
131
132 void set_allowed_features (bool thresholding, bool transparency, bool lighting) {
133 uint32_t cmap = flags_;
134 set_bit (cmap, DiscardLowerEnabled, thresholding);
135 set_bit (cmap, DiscardUpperEnabled, thresholding);
136 set_bit (cmap, TransparencyEnabled, transparency);
137 set_bit (cmap, LightingEnabled, lighting);
138 flags_ = cmap;
139 }
140
141 void set_colour (std::array<GLubyte,3> &c) {
142 colour = c;
143 }
144
145 void set_use_discard_lower (bool yesno) {
146 if (!discard_lower_enabled()) return;
147 set_bit (DiscardLower, yesno);
148 }
149
150 void set_use_discard_upper (bool yesno) {
151 if (!discard_upper_enabled()) return;
152 set_bit (DiscardUpper, yesno);
153 }
154
155 void set_use_transparency (bool yesno) {
156 if (!transparency_enabled()) return;
157 set_bit (Transparency, yesno);
158 }
159
160 void set_use_lighting (bool yesno) {
161 if (!lighting_enabled()) return;
162 set_bit (LightingEnabled, yesno);
163 }
164
165 void set_invert_scale (bool yesno) {
166 set_bit (InvertScale, yesno);
167 }
168
169 bool scale_inverted () const {
170 return flags_ & InvertScale;
171 }
172
173 bool discard_lower_enabled () const {
175 }
176
177 bool discard_upper_enabled () const {
179 }
180
181 bool transparency_enabled () const {
183 }
184
185 bool lighting_enabled () const {
186 return flags_ & LightingEnabled;
187 }
188
189 bool use_discard_lower () const {
190 return discard_lower_enabled() && ( flags_ & DiscardLower );
191 }
192
193 bool use_discard_upper () const {
194 return discard_upper_enabled() && ( flags_ & DiscardUpper );
195 }
196
197 bool use_transparency () const {
198 return transparency_enabled() && ( flags_ & Transparency );
199 }
200
201 bool use_lighting () const {
202 return lighting_enabled() && ( flags_ & Lighting );
203 }
204
205
206 class Shader : public GL::Shader::Program { NOMEMALIGN
207 public:
208 virtual std::string fragment_shader_source (const Displayable& object) = 0;
209 virtual std::string geometry_shader_source (const Displayable&) { return std::string(); }
210 virtual std::string vertex_shader_source (const Displayable& object) = 0;
211
212 virtual bool need_update (const Displayable& object) const;
213 virtual void update (const Displayable& object);
214
215 void start (const Displayable& object) {
216 if (*this == 0 || need_update (object))
217 recompile (object);
219 }
220 protected:
221 uint32_t flags;
222 size_t colourmap;
223
224 void recompile (const Displayable& object) {
225 if (*this != 0)
226 clear();
227
228 update (object);
229
230 GL::Shader::Vertex vertex_shader (vertex_shader_source (object));
231 GL::Shader::Geometry geometry_shader (geometry_shader_source (object));
232 GL::Shader::Fragment fragment_shader (fragment_shader_source (object));
233
234 attach (vertex_shader);
235 if((GLuint)geometry_shader)
236 attach (geometry_shader);
237 attach (fragment_shader);
238 link();
239 }
240 };
241
242
243 std::string declare_shader_variables (const std::string& with_prefix = "") const {
244 std::string source =
245 "uniform float " + with_prefix+"offset;\n"
246 "uniform float " + with_prefix+"scale;\n";
247 if (use_discard_lower())
248 source += "uniform float " + with_prefix+"lower;\n";
249 if (use_discard_upper())
250 source += "uniform float " + with_prefix+"upper;\n";
251 if (use_transparency()) {
252 source +=
253 "uniform float " + with_prefix+"alpha_scale;\n"
254 "uniform float " + with_prefix+"alpha_offset;\n"
255 "uniform float " + with_prefix+"alpha;\n";
256 }
257 if (ColourMap::maps[colourmap].is_colour)
258 source += "uniform vec3 " + with_prefix + "colourmap_colour;\n";
259 return source;
260 }
261
262 void start (Shader& shader_program, float scaling = 1.0, const std::string& with_prefix = "") {
263 shader_program.start (*this);
264 set_shader_variables (shader_program, scaling, with_prefix);
265 }
266
267 void set_shader_variables (Shader& shader_program, float scaling = 1.0, const std::string& with_prefix = "") {
268 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"offset").c_str()), (display_midpoint - 0.5f * display_range) / scaling);
269 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"scale").c_str()), scaling / display_range);
270 if (use_discard_lower())
271 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"lower").c_str()), lessthan / scaling);
272 if (use_discard_upper())
273 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"upper").c_str()), greaterthan / scaling);
274 if (use_transparency()) {
275 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"alpha_scale").c_str()), scaling / (opaque_intensity - transparent_intensity));
276 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"alpha_offset").c_str()), transparent_intensity / scaling);
277 gl::Uniform1f (gl::GetUniformLocation (shader_program, (with_prefix+"alpha").c_str()), alpha);
278 }
279 if (ColourMap::maps[colourmap].is_colour)
280 gl::Uniform3f (gl::GetUniformLocation (shader_program, (with_prefix+"colourmap_colour").c_str()),
281 colour[0]/255.0, colour[1]/255.0, colour[2]/255.0);
282 }
283
284 void stop (Shader& shader_program) {
285 shader_program.stop();
286 }
287
288 float lessthan, greaterthan;
289 float display_midpoint, display_range;
290 float transparent_intensity, opaque_intensity, alpha;
291 std::array<GLubyte,3> colour;
292 size_t colourmap;
293 bool show;
294 bool show_colour_bar;
295
296
297 signals:
299
300
301 protected:
302 std::string filename;
304 uint32_t flags_;
305
306 void set_bit (uint32_t& field, uint32_t bit, bool value) {
307 if (value) field |= bit;
308 else field &= ~bit;
309 }
310
311 void set_bit (uint32_t bit, bool value) {
312 uint32_t cmap = flags_;
313 set_bit (cmap, bit, value);
314 flags_ = cmap;
315 }
316
318 assert (std::isfinite (value_min));
319 assert (std::isfinite (value_max));
320 if (!std::isfinite (transparent_intensity))
321 transparent_intensity = value_min + 0.1 * (value_max - value_min);
322 if (!std::isfinite (opaque_intensity))
323 opaque_intensity = value_min + 0.5 * (value_max - value_min);
324 if (!std::isfinite (alpha))
325 alpha = 0.5;
326 if (!std::isfinite (lessthan))
327 lessthan = value_min;
328 if (!std::isfinite (greaterthan))
329 greaterthan = value_max;
330 }
331
332 };
333
334
335
336
337 }
338 }
339}
340
341#endif
342
void set_bit(uint32_t bit, bool value)
Definition: displayable.h:311
void set_bit(uint32_t &field, uint32_t bit, bool value)
Definition: displayable.h:306
virtual void render_image_colourbar(const Image &)
Definition: displayable.h:57
virtual void render_fixel_colourbar(const Tool::BaseFixel &)
Definition: displayable.h:58
virtual void render_tractogram_colourbar(const Tool::Tractogram &)
Definition: displayable.h:59
constexpr double e
Definition: math.h:39
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
const Entry maps[]
const uint32_t InvertScale
Definition: displayable.h:40
const uint32_t TransparencyEnabled
Definition: displayable.h:47
const uint32_t DiscardLower
Definition: displayable.h:41
const uint32_t DiscardLowerEnabled
Definition: displayable.h:45
const uint32_t DiscardUpperEnabled
Definition: displayable.h:46
const uint32_t Transparency
Definition: displayable.h:43
const uint32_t DiscardUpper
Definition: displayable.h:42
const uint32_t Lighting
Definition: displayable.h:44
const uint32_t LightingEnabled
Definition: displayable.h:48
Definition: base.h:24
#define MEMALIGN(...)
Definition: types.h:185