v2.0.0
Loading...
Searching...
No Matches
mne_msh_display_surface_set.cpp
Go to the documentation of this file.
1//=============================================================================================================
35
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
41
43#include "mne_surface.h"
44#include "mne_surface_patch.h"
45#include "mne_source_space.h"
46#include "mne_msh_light_set.h"
47#include "mne_msh_light.h"
48#include "mne_msh_eyes.h"
49
50//=============================================================================================================
51// EIGEN INCLUDES
52//=============================================================================================================
53
54#include <Eigen/Core>
55
56//=============================================================================================================
57// QT INCLUDES
58//=============================================================================================================
59
60#include <qmath.h>
61
62//=============================================================================================================
63// CONSTANTS
64//=============================================================================================================
65
66namespace {
67
70constexpr int SURF_LEFT_MORPH_HEMI = (1 << 16 | FIFFV_MNE_SURF_LEFT_HEMI);
71constexpr int SURF_RIGHT_MORPH_HEMI = (1 << 16 | FIFFV_MNE_SURF_RIGHT_HEMI);
72
73constexpr int SHOW_CURVATURE_NONE = 0;
74constexpr int SHOW_CURVATURE_OVERLAY = 1;
75constexpr int SHOW_OVERLAY_HEAT = 1;
76
77constexpr float POS_CURV_COLOR = 0.25f;
78constexpr float NEG_CURV_COLOR = 0.375f;
79constexpr float EVEN_CURV_COLOR = 0.375f;
80
81} // anonymous namespace
82
83//=============================================================================================================
84// STATIC DATA
85//=============================================================================================================
86
87static MNELIB::MNEMshEyes default_eyes;
88static MNELIB::MNEMshEyes* all_eyes = nullptr;
89static int neyes = 0;
90static int current_eyes = -1;
91
92static std::unique_ptr<MNELIB::MNEMshLightSet> custom_lights;
93
94//=============================================================================================================
95// USED NAMESPACES
96//=============================================================================================================
97
98using namespace MNELIB;
99
100//=============================================================================================================
101// DEFINE MEMBER METHODS
102//=============================================================================================================
103
105{
106 Eigen::Map<Eigen::Vector3f>(default_eyes.left) = Eigen::Vector3f(-0.2f, 0.0f, 0.0f);
107 Eigen::Map<Eigen::Vector3f>(default_eyes.right) = Eigen::Vector3f( 0.2f, 0.0f, 0.0f);
108 Eigen::Map<Eigen::Vector3f>(default_eyes.left_up) = Eigen::Vector3f(0.0f, 0.0f, 1.0f);
109 Eigen::Map<Eigen::Vector3f>(default_eyes.right_up) = Eigen::Vector3f(0.0f, 0.0f, 1.0f);
110
111 this->nsurf = nsurf;
112 if (nsurf > 0) {
113 surfs.resize(nsurf);
114 patches.resize(nsurf);
115 patch_rot.resize(nsurf, 0.0f);
116 active = Eigen::VectorXi::Zero(nsurf);
117 drawable = Eigen::VectorXi::Ones(nsurf);
118 }
119
120 use_patches = false;
121
122 Eigen::Vector3f::Map(rot) = Eigen::Vector3f::Zero();
123 Eigen::Vector3f::Map(move) = Eigen::Vector3f::Zero();
124 Eigen::Vector3f::Map(eye) = Eigen::Vector3f(1.0f, 0.0f, 0.0f);
125 Eigen::Vector3f::Map(up) = Eigen::Vector3f(0.0f, 0.0f, 1.0f);
126
127 Eigen::Vector3f::Map(bg_color) = Eigen::Vector3f::Zero();
128 Eigen::Vector3f::Map(text_color) = Eigen::Vector3f::Ones();
129}
130
131//=============================================================================================================
132
134
135//=============================================================================================================
136
137std::unique_ptr<MNEMshDisplaySurfaceSet> MNEMshDisplaySurfaceSet::load(const QString &subject_id, const QString &surf, const QString &subjects_dir)
138 /*
139 * Load new display surface data
140 */
141{
142 QString pathLh = QString("%1/%2/surf/%3.%4").arg(subjects_dir).arg(subject_id).arg("lh").arg(surf);
143 QString pathLhCurv = QString("%1/%2/surf/%3.%4").arg(subjects_dir).arg(subject_id).arg("lh").arg("curv");
144 QString pathRh = QString("%1/%2/surf/%3.%4").arg(subjects_dir).arg(subject_id).arg("rh").arg(surf);
145 QString pathRhCurv = QString("%1/%2/surf/%3.%4").arg(subjects_dir).arg(subject_id).arg("rh").arg("curv");
146
147 printf("Loading surface %s ...\n", pathLh.toUtf8().constData());
148 auto left = MNESourceSpace::load_surface(pathLh, pathLhCurv);
149 if (!left) {
150 left = MNESourceSpace::load_surface(pathLh, QString());
151 if (!left)
152 return nullptr;
153 left->add_uniform_curv();
154 }
155
156 printf("Loading surface %s ...\n", pathRh.toUtf8().constData());
157 auto right = MNESourceSpace::load_surface(pathRh, pathRhCurv);
158 if (!right) {
159 right = MNESourceSpace::load_surface(pathRh, QString());
160 if (!right)
161 return nullptr;
162 right->add_uniform_curv();
163 }
164
165 auto result = std::make_unique<MNEMshDisplaySurfaceSet>(2);
166
167 result->surfs[0] = std::make_unique<MNEMshDisplaySurface>();
168 result->surfs[1] = std::make_unique<MNEMshDisplaySurface>();
169
170 result->active[0] = true;
171 result->active[1] = false;
172 result->drawable[0] = true;
173 result->drawable[1] = true;
174
175 auto* pThis = result->surfs[0].get();
176 pThis->filename = pathLh;
177 static_cast<MNESurfaceOrVolume&>(*pThis) = std::move(static_cast<MNESurfaceOrVolume&>(*left));
178 pThis->id = SURF_LEFT_HEMI;
179 pThis->subj = subject_id;
180 pThis->surf_name = surf;
181
182 pThis->decide_surface_extent("Left hemisphere");
183 pThis->decide_curv_display(surf);
184 pThis->setup_curvature_colors();
185
186 pThis = result->surfs[1].get();
187 pThis->filename = pathRh;
188 static_cast<MNESurfaceOrVolume&>(*pThis) = std::move(static_cast<MNESurfaceOrVolume&>(*right));
189 pThis->id = SURF_RIGHT_HEMI;
190 pThis->subj = subject_id;
191 pThis->surf_name = surf;
192
193 pThis->decide_surface_extent("Right hemisphere");
194 pThis->decide_curv_display(surf);
195 pThis->setup_curvature_colors();
196
197 result->apply_left_right_eyes();
198 result->setup_current_lights();
199
200 return result;
201}
202
203//=============================================================================================================
204
206 int kind,
207 const QString& bemname,
208 int full_geom,
209 int check)
210{
211 printf("Loading BEM surface %s (id = %d) from %s ...\n",
212 bemname.toUtf8().constData(), kind, filepath.toUtf8().constData());
213
214 std::unique_ptr<MNESurface> surf(MNESurface::read_bem_surface2(filepath,kind,full_geom,nullptr));
215 if (!surf)
216 return -1;
217
218 if (check) {
219 surf->compute_surface_cm();
220 double sum = surf->sum_solids(Eigen::Map<const Eigen::Vector3f>(surf->cm)) / (4*M_PI);
221 if (std::fabs(sum - 1.0) > 1e-4) {
222 printf( "%s surface is not closed "
223 "(sum of solid angles = %g * 4*PI).",
224 bemname.toUtf8().constData(), sum);
225 return -1;
226 }
227 }
228
229 auto newSurf = std::make_unique<MNEMshDisplaySurface>();
230 newSurf->filename = filepath;
231 //newSurf->time_loaded = time(nullptr); //Comment out due to unknown timestamp function ToDo
232 static_cast<MNESurfaceOrVolume&>(*newSurf) = std::move(static_cast<MNESurfaceOrVolume&>(*surf));
233 newSurf->id = kind;
234 newSurf->surf_name = bemname;
235
236 newSurf->curvature_color_mode = SHOW_CURVATURE_NONE;
237 newSurf->overlay_color_mode = SHOW_OVERLAY_HEAT;
238
239 newSurf->decide_surface_extent(bemname);
240 add_replace_surface(std::move(newSurf), true, true);
243
244 return 0;
245}
246
247//=============================================================================================================
248
249void MNEMshDisplaySurfaceSet::add_replace_surface(std::unique_ptr<MNEMshDisplaySurface> newSurf,
250 bool replace,
251 bool drawable)
252{
253 if (replace) {
254 for (int k = 0; k < nsurf; k++) {
255 auto& surf = surfs[k];
256 if (surf->id == newSurf->id) {
257 newSurf->transparent = surf->transparent;
258 newSurf->show_aux_data = surf->show_aux_data;
259 surfs[k] = std::move(newSurf);
260 if (!drawable) {
261 active[k] = false;
262 this->drawable[k] = false;
263 }
264 return;
265 }
266 }
267 }
268 if (newSurf) { /* New surface */
269 surfs.push_back(std::move(newSurf));
270 patches.push_back(nullptr);
271 patch_rot.push_back(0.0f);
272 active.conservativeResize(nsurf+1);
273 this->drawable.conservativeResize(nsurf+1);
275 this->drawable[nsurf] = drawable;
276 nsurf++;
277 }
278}
279
280//=============================================================================================================
281
283{
284 MNEMshEyes* eyes = nullptr;
285
286 if (neyes == 0 || current_eyes < 0 || current_eyes > neyes-1) {
287 eyes = &default_eyes;
288 } else {
289 eyes = all_eyes+current_eyes;
290 }
291
292 for (int k = 0; k < nsurf; k++) {
293 auto* surf = surfs[k].get();
294 switch(surf->id) {
295 case SURF_LEFT_HEMI :
297 surf->eye = Eigen::Vector3f::Map(eyes->left);
298 surf->up = Eigen::Vector3f::Map(eyes->left_up);
299 break;
300 case SURF_RIGHT_HEMI :
302 surf->eye = Eigen::Vector3f::Map(eyes->right);
303 surf->up = Eigen::Vector3f::Map(eyes->right_up);
304 break;
305 default :
306 surf->eye = Eigen::Vector3f::Map(eyes->left);
307 surf->up = Eigen::Vector3f::Map(eyes->left_up);
308 break;
309 }
310 }
311}
312
313//=============================================================================================================
314
316{
317 for (int k = 0; k < nsurf; k++) {
318 if (neyes == 0 || current_eyes < 0 || current_eyes > neyes-1) {
319 surfs[k]->eye = Eigen::Vector3f::Map(default_eyes.left);
320 surfs[k]->up = Eigen::Vector3f::Map(default_eyes.left_up);
321 }
322 else {
323 surfs[k]->eye = Eigen::Vector3f::Map(all_eyes[current_eyes].left);
324 surfs[k]->up = Eigen::Vector3f::Map(all_eyes[current_eyes].left_up);
325 }
326 }
327}
328
329//=============================================================================================================
330
336
337//=============================================================================================================
338
340{
341 if (!custom_lights) {
342 custom_lights = std::make_unique<MNEMshLightSet>();
343
344 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, 0.0f, 0.0f, 1.0f, 0.8f, 0.8f, 0.8f));
345 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, 0.0f, 0.0f, -1.0f, 0.8f, 0.8f, 0.8f));
346 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, 0.6f, -1.0f, -1.0f, 0.6f, 0.6f, 0.6f));
347 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, -0.6f, -1.0f, -1.0f, 0.6f, 0.6f, 0.6f));
348 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, 1.0f, 0.0f, 0.0f, 0.8f, 0.8f, 0.8f));
349 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, -1.0f, 0.0f, 0.0f, 0.8f, 0.8f, 0.8f));
350 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(true, 0.0f, 1.0f, 0.5f, 0.6f, 0.6f, 0.6f));
351 custom_lights->lights.push_back(std::make_unique<MNEMshLight>(false, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f));
352 }
353}
354
355//=============================================================================================================
356
357std::unique_ptr<MNEMshLightSet> MNEMshDisplaySurfaceSet::dup_light_set(const MNEMshLightSet& s)
358{
359 auto res = std::make_unique<MNEMshLightSet>();
360
361 for (const auto &light : s.lights)
362 res->lights.push_back(std::make_unique<MNEMshLight>(*light));
363
364 return res;
365}
366
367//=============================================================================================================
368
373
#define M_PI
#define FIFFV_MNE_SURF_RIGHT_HEMI
#define FIFFV_MNE_SURF_LEFT_HEMI
MNEMshDisplaySurfaceSet class declaration.
MNEMshLight class declaration.
MNESurface class declaration.
MNESurfacePatch class declaration.
constexpr float NEG_CURV_COLOR
constexpr float POS_CURV_COLOR
constexpr float EVEN_CURV_COLOR
MNEMshDisplaySurface class declaration.
MNEMshLightSet class declaration.
MNEMshEyes class declaration.
MNESourceSpace class declaration.
#define SHOW_CURVATURE_NONE
#define SURF_RIGHT_HEMI
#define SURF_LEFT_HEMI
#define SURF_RIGHT_MORPH_HEMI
#define SHOW_CURVATURE_OVERLAY
#define SURF_LEFT_MORPH_HEMI
#define SHOW_OVERLAY_HEAT
Core MNE data structures (source spaces, source estimates, hemispheres).
void setup_lights(const MNEMshLightSet &set)
static std::unique_ptr< MNEMshDisplaySurfaceSet > load(const QString &subject_id, const QString &surf, const QString &subjects_dir)
std::unique_ptr< MNEMshLightSet > lights
static std::unique_ptr< MNEMshLightSet > dup_light_set(const MNEMshLightSet &s)
std::vector< std::unique_ptr< MNEMshDisplaySurface > > surfs
void add_replace_surface(std::unique_ptr< MNEMshDisplaySurface > newSurf, bool replace, bool drawable)
std::vector< std::unique_ptr< MNESurfacePatch > > patches
int add_bem_surface(const QString &filepath, int kind, const QString &bemname, int full_geom, int check)
Eye/camera position and gaze direction for 3-D surface rendering.
Collection of lights defining the lighting setup for 3-D rendering.
std::vector< std::unique_ptr< MNEMshLight > > lights
static std::unique_ptr< MNESourceSpace > load_surface(const QString &surf_file, const QString &curv_file)
static MNESurface * read_bem_surface2(const QString &name, int which, int add_geometry, float *sigmap)
Defines a source space or a surface.