Developer documentation
Version 3.0.3-105-gd3941f44
base_fixel.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_tool_fixel_fixelimage_h__
18#define __gui_mrview_tool_fixel_fixelimage_h__
19
20#include <unordered_map>
21
22#include "header.h"
23#include "image.h"
24#include "transform.h"
25
26#include "algo/loop.h"
27#include "fixel/legacy/image.h"
29#include "fixel/helpers.h"
30
34
35
36namespace MR
37{
38 namespace GUI
39 {
40 namespace MRView
41 {
42 namespace Tool
43 {
44
46 public:
47 BaseFixel (const std::string&, Fixel&);
48 ~BaseFixel();
49
50 class Shader : public Displayable::Shader { MEMALIGN (Shader)
51 public:
52 Shader () : do_crop_to_slice (false), bidirectional (false), color_type (Direction), scale_type (Value) { }
53 std::string vertex_shader_source (const Displayable&) override;
54 std::string geometry_shader_source (const Displayable&) override;
55 std::string fragment_shader_source (const Displayable&) override;
56 virtual bool need_update (const Displayable&) const override;
57 virtual void update (const Displayable&) override;
58 protected:
59 bool do_crop_to_slice, bidirectional;
60 FixelColourType color_type;
62 } fixel_shader;
63
64
65 void render (const Projection& projection);
66
67 void request_render_colourbar (DisplayableVisitor& visitor) override {
68 if(colour_type == CValue && show_colour_bar)
69 visitor.render_fixel_colourbar(*this);
70 }
71
72 void load_image (const std::string &filename);
73
74 void reload_directions_buffer ();
75
76 void reload_colours_buffer ();
77
78 void reload_values_buffer ();
79
80 void reload_threshold_buffer ();
81
82 void set_line_length_multiplier (float value) {
83 user_line_length_multiplier = value;
84 }
85
86 float get_line_length_multiplier () const {
87 return user_line_length_multiplier;
88 }
89
90 void set_line_thickness (float value) {
91 line_thickness = value;
92 }
93
94 float get_line_thickenss () const {
95 return line_thickness;
96 }
97
98 size_t get_scale_type_index () const {
99 return scale_type_index;
100 }
101
102 void set_scale_type_index (size_t index)
103 {
104 if (index != scale_type_index) {
106 scale_type = index == 0 ? Unity : Value;
107 value_buffer_dirty = true;
108 }
109 }
110
111 size_t get_threshold_type_index () const {
113 }
114
115 void set_threshold_type_index (size_t index)
116 {
119 if (colour_type == CValue) {
120 lessthan = get_threshold_lower ();
121 greaterthan = get_threshold_upper ();
122 }
123
125 }
126 }
127
128 size_t get_colour_type_index () const {
129 return colour_type_index;
130 }
131
132 void set_colour_type_index (size_t index) {
133 auto& fixel_val = current_fixel_colour_state ();
134 fixel_val.current_min = std::isfinite (scaling_min ()) ? scaling_min () : fixel_val.current_min;
135 fixel_val.current_max = std::isfinite (scaling_max ()) ? scaling_max () : fixel_val.current_max;
136
137 if (index != colour_type_index) {
140 colour_buffer_dirty = true;
141 }
142
143 auto& new_fixel_val = current_fixel_colour_state ();
144 value_min = new_fixel_val.value_min;
145 value_max = new_fixel_val.value_max;
146 if (colour_type == CValue) {
147 lessthan = get_threshold_lower ();
148 greaterthan = get_threshold_upper ();
149 }
150 set_windowing (new_fixel_val.current_min, new_fixel_val.current_max);
151 }
152
153 FixelColourType get_colour_type () const {
154 return colour_type;
155 }
156
157 float get_threshold_lower () const {
158 FixelValue& fixel_thresh = current_fixel_threshold_state ();
160 return fixel_thresh.get_relative_threshold_lower (fixel_val);
161 }
162
163 float get_unscaled_threshold_lower () const {
165 }
166
167 void set_threshold_lower (float value) {
168 FixelValue& fixel_threshold = current_fixel_threshold_state ();
169 fixel_threshold.lessthan = value;
170 if (colour_type == CValue)
171 lessthan = get_threshold_lower ();
172 }
173
174 float get_threshold_upper () const {
175 FixelValue& fixel_thresh = current_fixel_threshold_state ();
177 return fixel_thresh.get_relative_threshold_upper (fixel_val);
178 }
179
180 float get_unscaled_threshold_upper () const {
182 }
183
184 void set_threshold_upper (float value) {
185 FixelValue& fixel_threshold = current_fixel_threshold_state ();
186 fixel_threshold.greaterthan = value;
187 if (colour_type == CValue)
188 greaterthan = get_threshold_upper ();
189 }
190
191 float get_unscaled_threshold_rate () const {
192 FixelValue& fixel_threshold = current_fixel_threshold_state ();
193 return 1e-3 * (fixel_threshold.value_max - fixel_threshold.value_min);
194 }
195
196 void load_colourby_combobox_options (ComboBoxWithErrorMsg& combo_box) const {
197 combo_box.clear ();
198 for (size_t i = 0, N = colour_types.size (); i < N; ++i)
199 combo_box.addItem (qstr (colour_types[i]));
200 combo_box.setCurrentIndex (colour_type_index);
201 }
202
203 void load_scaleby_combobox_options (ComboBoxWithErrorMsg& combo_box) const {
204 combo_box.clear ();
205 for (const auto& value_name: value_types)
206 combo_box.addItem (qstr (value_name));
207 combo_box.setCurrentIndex (scale_type_index);
208 }
209
210 void load_threshold_combobox_options (ComboBoxWithErrorMsg& combo_box) const {
211 combo_box.clear ();
212 for (size_t i = 1, N = value_types.size (); i < N; ++i)
213 combo_box.addItem (qstr (value_types[i]));
214 combo_box.setCurrentIndex (threshold_type_index);
215 }
216
217 bool has_values () const {
218 return fixel_values.size();
219 }
220
221 protected:
223 size_t operator () (const std::array<int, 3>& v) const {
224 // This hashing function works best if the fixel image dimensions
225 // are bounded above by 2^10 x 2^10 x 2^10 = 1024 x 1024 x 1024
226 return (v[0] + (v[1] << 10) + (v[2] << 20));
227 }
228 };
229
230 virtual void update_image_buffers ();
231 virtual void load_image_buffer() = 0;
234
237 }
238
241 }
242
245 }
246
247 virtual FixelValue& get_fixel_value (const std::string& key) const {
248 return fixel_values[key];
249 }
250
255 mutable std::map<const std::string, FixelValue> fixel_values;
257
260
266
270
271 // Flattened buffer used when cropping to slice
272 // To support off-axis rendering, we maintain dict mapping voxels to buffer_pos indices
273 std::unordered_map <std::array<int, 3>, vector<GLint>, IntPointHasher> voxel_to_indices_map;
274
280
285
286 private:
287 Fixel& fixel_tool;
288 GL::VertexBuffer vertex_buffer;
289 GL::VertexBuffer direction_buffer;
290 GL::VertexBuffer colour_buffer;
291 GL::VertexBuffer value_buffer;
292 GL::VertexBuffer threshold_buffer;
293 GL::VertexArrayObject vertex_array_object;
294
295 GL::VertexArrayObject regular_grid_vao;
296 GL::VertexBuffer regular_grid_vertex_buffer;
297 GL::VertexBuffer regular_grid_dir_buffer;
298 GL::VertexBuffer regular_grid_colour_buffer;
299 GL::VertexBuffer regular_grid_val_buffer;
300 GL::VertexBuffer regular_grid_threshold_buffer;
301
302 float voxel_size_length_multipler;
303 float user_line_length_multiplier;
304 float line_thickness;
305 };
306
307
308 // Wrapper to generically store fixel data
309
310 template <typename ImageType> class FixelType : public BaseFixel
311 { MEMALIGN (FixelType<ImageType>)
312 public:
313 FixelType (const std::string& filename, Fixel& fixel_tool) :
314 BaseFixel (filename, fixel_tool),
315 transform (header) { }
316
317 protected:
318 std::unique_ptr<ImageType> fixel_data;
320
321 void request_update_interp_image_buffer (const Projection& projection) override {
323 }
324 };
325
329 }
330 }
331 }
332}
333#endif
virtual void render_fixel_colourbar(const Tool::BaseFixel &)
Definition: displayable.h:58
virtual void load_image_buffer()=0
virtual void request_update_interp_image_buffer(const Projection &)=0
virtual FixelValue & get_fixel_value(const std::string &key) const
Definition: base_fixel.h:247
FixelValue & current_fixel_colour_state() const
Definition: base_fixel.h:243
FixelValue & current_fixel_value_state() const
Definition: base_fixel.h:235
FixelValue & current_fixel_threshold_state() const
Definition: base_fixel.h:239
vector< float > regular_grid_buffer_threshold
Definition: base_fixel.h:265
vector< Eigen::Vector3f > regular_grid_buffer_dir
Definition: base_fixel.h:262
vector< std::string > threshold_types
Definition: base_fixel.h:254
void update_interp_image_buffer(const Projection &, const MR::Header &, const MR::Transform &)
std::unordered_map< std::array< int, 3 >, vector< GLint >, IntPointHasher > voxel_to_indices_map
Definition: base_fixel.h:273
vector< std::string > colour_types
Definition: base_fixel.h:252
vector< Eigen::Vector3f > regular_grid_buffer_pos
Definition: base_fixel.h:261
vector< std::string > value_types
Definition: base_fixel.h:253
vector< vector< GLsizei > > slice_fixel_counts
Definition: base_fixel.h:269
vector< vector< vector< GLint > > > slice_fixel_indices
Definition: base_fixel.h:267
vector< float > regular_grid_buffer_colour
Definition: base_fixel.h:263
vector< vector< vector< GLsizei > > > slice_fixel_sizes
Definition: base_fixel.h:268
std::map< const std::string, FixelValue > fixel_values
Definition: base_fixel.h:255
vector< Eigen::Vector3f > dir_buffer_store
Definition: base_fixel.h:259
vector< Eigen::Vector3f > pos_buffer_store
Definition: base_fixel.h:258
vector< float > regular_grid_buffer_val
Definition: base_fixel.h:264
std::unique_ptr< ImageType > fixel_data
Definition: base_fixel.h:318
void request_update_interp_image_buffer(const Projection &projection) override
Definition: base_fixel.h:321
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
QString qstr(const std::string &s)
Definition: gui.h:31
Definition: base.h:24
size_t index
#define MEMALIGN(...)
Definition: types.h:185
NOMEMALIGN size_t operator()(const std::array< int, 3 > &v) const
Definition: base_fixel.h:223
float get_relative_threshold_lower(FixelValue &fixel_value) const
float get_relative_threshold_upper(FixelValue &fixel_value) const