Developer documentation
Version 3.0.3-105-gd3941f44
renderer.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_dwi_renderer_h__
18#define __gui_dwi_renderer_h__
19
20#include <QGLWidget>
21#include <Eigen/Eigenvalues>
22
23#include "gui/gui.h"
24#include "dwi/directions/set.h"
26#include "gui/opengl/gl.h"
27#include "gui/opengl/shader.h"
28#include "math/SH.h"
29
30namespace MR
31{
32 namespace GUI
33 {
34
35 class Projection;
36
37 namespace GL
38 {
39 class Lighting;
40 class mat4;
41 }
42
43 namespace DWI
44 {
45
47 { MEMALIGN(Renderer)
48
49 using matrix_t = Eigen::MatrixXf;
50 using vector_t = Eigen::VectorXf;
51 using tensor_t = Eigen::Matrix3f;
52
53 public:
54
55 enum class mode_t { SH, TENSOR, DIXEL };
56
57 Renderer (QGLWidget*);
58
59 bool ready () const { return shader; }
60
61 void initGL () {
62 sh.initGL();
63 tensor.initGL();
64 dixel.initGL();
65 }
66
67 void set_mode (const mode_t i) {
68 mode = i;
69 }
70
71 void start (const Projection& projection, const GL::Lighting& lighting, float scale,
72 bool use_lighting, bool color_by_direction, bool hide_neg_lobes, bool orthographic = false, const GL::mat4* colour_relative_to_projection = nullptr);
73
74 void draw (const Eigen::Vector3f& origin, int buffer_ID = 0) const {
75 (void) buffer_ID; // to silence unused-parameter warnings
76 gl::Uniform3fv (origin_ID, 1, origin.data());
77 gl::Uniform1i (reverse_ID, 0);
78 half_draw();
79 gl::Uniform1i (reverse_ID, 1);
80 half_draw();
81 }
82
83 void stop () const {
84 shader.stop();
85 }
86
87 QColor get_colour() const {
88 return QColor(object_color[0]*255.0f, object_color[1]*255.0f, object_color[2]*255.0f);
89 }
90
91 void set_colour (const QColor& c) {
92 object_color[0] = c.red() /255.0f;
93 object_color[1] = c.green()/255.0f;
94 object_color[2] = c.blue() /255.0f;
95 }
96
97
98 protected:
100 float object_color[3];
101 mutable GLuint reverse_ID, origin_ID;
102
104 public:
106 void start (mode_t mode, bool use_lighting, bool colour_by_direction, bool hide_neg_values, bool orthographic, bool colour_relative_to_projection);
107 protected:
110 std::string vertex_shader_source() const;
111 std::string geometry_shader_source() const;
112 std::string fragment_shader_source() const;
114
115 void half_draw() const
116 {
117 const GLuint num_indices = (mode == mode_t::SH ? sh.num_indices() : (mode == mode_t::TENSOR ? tensor.num_indices() : dixel.num_indices()));
118 gl::DrawElements (gl::TRIANGLES, num_indices, gl::UNSIGNED_INT, (void*)0);
119 }
120
121 private:
122 class ModeBase
123 { NOMEMALIGN
124 public:
125 ModeBase (Renderer& parent) : parent (parent) { }
126 virtual ~ModeBase() { }
127
128 virtual void initGL() = 0;
129 virtual void bind() = 0;
130 virtual void set_data (const vector_t&, int buffer_ID = 0) const = 0;
131 virtual GLuint num_indices() const = 0;
132
133 protected:
134 Renderer& parent;
135 };
136
137 public:
138 class SH : public ModeBase
139 { MEMALIGN(SH)
140 public:
141 SH (Renderer& parent) : ModeBase (parent), LOD (0) { }
142 ~SH();
143
144 void initGL() override;
145 void bind() override;
146 void set_data (const vector_t& r_del_daz, int buffer_ID = 0) const override;
147 GLuint num_indices() const override { return half_sphere.num_indices; }
148
149 void update_mesh (const size_t, const int);
150
151 void compute_r_del_daz (matrix_t& r_del_daz, const matrix_t& SH) const {
152 if (!SH.rows() || !SH.cols()) return;
153 assert (transform.rows());
154 r_del_daz.noalias() = SH * transform.transpose();
155 }
156
157 void compute_r_del_daz (vector_t& r_del_daz, const vector_t& SH) const {
158 if (!SH.size()) return;
159 assert (transform.rows());
160 r_del_daz.noalias() = transform * SH;
161 }
162
163 int get_LOD() const { return LOD; }
164
165 private:
166 int LOD;
167 matrix_t transform;
168 Shapes::HalfSphere half_sphere;
169 GL::VertexBuffer surface_buffer;
171
172 void update_transform (const vector<Shapes::HalfSphere::Vertex>&, int);
173
174 } sh;
175
176
177 class Tensor : public ModeBase
178 { MEMALIGN(Tensor)
179 public:
180 Tensor (Renderer& parent) : ModeBase (parent), LOD (0) { }
181 ~Tensor();
182
183 void initGL() override;
184 void bind() override;
185 void set_data (const vector_t& data, int buffer_ID = 0) const override;
186 GLuint num_indices() const override { return half_sphere.num_indices; }
187
188 void update_mesh (const size_t);
189
190 int get_LOD() const { return LOD; }
191
192 private:
193 int LOD;
194 Shapes::HalfSphere half_sphere;
196
197 mutable Eigen::SelfAdjointEigenSolver<tensor_t> eig;
198
200
201
202 class Dixel : public ModeBase
203 { MEMALIGN (Dixel)
204
206
207 public:
208 Dixel (Renderer& parent) :
209 ModeBase (parent),
210 vertex_count (0),
211 index_count (0) { }
213
214 void initGL() override;
215 void bind() override;
216 void set_data (const vector_t&, int buffer_ID = 0) const override;
217 GLuint num_indices() const override { return index_count; }
218
220
221 private:
222 GL::VertexBuffer vertex_buffer, value_buffer;
223 GL::IndexBuffer index_buffer;
225 GLuint vertex_count, index_count;
226
227 void update_dixels (const MR::DWI::Directions::Set&);
228
230
231 private:
232 QGLWidget* context_;
233
234
235 };
236
237
238 }
239 }
240}
241
242#endif
243
void set_data(const vector_t &, int buffer_ID=0) const override
void update_mesh(const MR::DWI::Directions::Set &)
Dixel(Renderer &parent)
Definition: renderer.h:208
GLuint num_indices() const override
Definition: renderer.h:217
std::string vertex_shader_source() const
std::string fragment_shader_source() const
std::string geometry_shader_source() const
void start(mode_t mode, bool use_lighting, bool colour_by_direction, bool hide_neg_values, bool orthographic, bool colour_relative_to_projection)
MR::GUI::DWI::Renderer::Dixel dixel
MR::GUI::DWI::Renderer::Shader shader
void half_draw() const
Definition: renderer.h:115
bool ready() const
Definition: renderer.h:59
void set_colour(const QColor &c)
Definition: renderer.h:91
void stop() const
Definition: renderer.h:83
void set_mode(const mode_t i)
Definition: renderer.h:67
void start(const Projection &projection, const GL::Lighting &lighting, float scale, bool use_lighting, bool color_by_direction, bool hide_neg_lobes, bool orthographic=false, const GL::mat4 *colour_relative_to_projection=nullptr)
MR::GUI::DWI::Renderer::SH sh
void draw(const Eigen::Vector3f &origin, int buffer_ID=0) const
Definition: renderer.h:74
MR::GUI::DWI::Renderer::Tensor tensor
QColor get_colour() const
Definition: renderer.h:87
#define NOMEMALIGN
Definition: memory.h:22
unsigned int index_type
Definition: set.h:34
mat4 scale(float x, float y, float z)
Definition: base.h:24