MNE-CPP  0.1.9
A Framework for Electrophysiology
mne_surface_or_volume.h
Go to the documentation of this file.
1 //=============================================================================================================
37 #ifndef MNESURFACEORVOLUME_H
38 #define MNESURFACEORVOLUME_H
39 
40 //=============================================================================================================
41 // INCLUDES
42 //=============================================================================================================
43 
44 #include "../mne_global.h"
45 #include <mne/c/mne_types.h>
46 
47 //=============================================================================================================
48 // EIGEN INCLUDES
49 //=============================================================================================================
50 
51 #include <Eigen/Core>
52 
53 //=============================================================================================================
54 // QT INCLUDES
55 //=============================================================================================================
56 
57 #include <QSharedPointer>
58 #include <QStringList>
59 #include <QDebug>
60 
61 //============================= mne_fiff.h =============================
62 
63 #define FIFF_MNE_SOURCE_SPACE_NNEIGHBORS 3594 /* Number of neighbors for each source space point (used for volume source spaces) */
64 #define FIFF_MNE_SOURCE_SPACE_NEIGHBORS 3595 /* Neighbors for each source space point (used for volume source spaces) */
65 
66 #define FIFFV_MNE_COORD_SURFACE_RAS FIFFV_COORD_MRI /* The surface RAS coordinates */
67 
68 //============================= mne_surface_io.c =============================
69 
70 #define TRIANGLE_FILE_MAGIC_NUMBER (0xfffffe)
71 #define NEW_QUAD_FILE_MAGIC_NUMBER (0xfffffd)
72 #define QUAD_FILE_MAGIC_NUMBER (0xffffff)
73 
74 //============================= mne_mgh_mri_io.c =============================
75 
76 #define TAG_OLD_SURF_GEOM 20
77 
78 //=============================================================================================================
79 // FORWARD DECLARATIONS
80 //=============================================================================================================
81 
82 namespace FIFFLIB {
83  class FiffDigitizerData;
84 }
85 
86 //=============================================================================================================
87 // DEFINE NAMESPACE MNELIB
88 //=============================================================================================================
89 
90 namespace MNELIB
91 {
92 
93 //=============================================================================================================
94 // MNELIB FORWARD DECLARATIONS
95 //=============================================================================================================
96 
97 class MneTriangle;
98 class MneNearest;
99 class MneVolGeom;
100 class MnePatchInfo;
101 class MneSourceSpaceOld;
102 class MneSurfaceOld;
103 class MneMshDisplaySurface;
104 class MneProjData;
105 class MneMghTagGroup;
106 
107 //=============================================================================================================
114 {
115 public:
116  typedef QSharedPointer<MneSurfaceOrVolume> SPtr;
117  typedef QSharedPointer<const MneSurfaceOrVolume> ConstSPtr;
119  /*
120  * These are the aliases
121  */
122 // typedef MneSurfaceOrVolume MneCSourceSpace; //TODO create a derived class
123  //typedef mneSurfaceOrVolume mneSourceVolume;
124 // typedef MneSurfaceOrVolume MneCSurface; //TODO create a derived class
125  //typedef mneSurfaceOrVolume mneVolume;
126 
127  //=========================================================================================================
132 
133  //=========================================================================================================
138  virtual ~MneSurfaceOrVolume();
139 
140  //============================= make_filter_source_sapces.c =============================
141 
142  static double solid_angle (float *from, /* From this point... */
143  MNELIB::MneTriangle* tri);
144 
145  static double sum_solids(float *from, MneSurfaceOld* surf);
146 
147  static int mne_filter_source_spaces(MneSurfaceOld* surf, /* The bounding surface must be provided */
148  float limit, /* Minimum allowed distance from the surface */
149  FIFFLIB::FiffCoordTransOld* mri_head_t, /* Coordinate transformation (may not be needed) */
150  MneSourceSpaceOld* *spaces, /* The source spaces */
151  int nspace,
152  FILE *filtered);
153 
154  //============================= mne_patches.c =============================
155 
156  static int mne_add_patch_stats(MneSourceSpaceOld* s);
157 
158  //============================= filter_source_sapces.c =============================
159 
160  static void rearrange_source_space(MneSourceSpaceOld* s);
161 
162  static void *filter_source_space(void *arg);
163 
164  static int filter_source_spaces(float limit, /* Omit vertices which are closer than this to the inner skull */
165  char *bemfile, /* Take the inner skull surface from here */
166  FIFFLIB::FiffCoordTransOld* mri_head_t, /* Coordinate transformation is needed */
167  MneSourceSpaceOld* *spaces, /* The source spaces */
168  int nspace, /* How many? */
169  FILE *filtered, /* Output the coordinates of the filtered points here */
170  bool use_threads);
171 
172  //============================= make_volume_source_space.c =============================
173 
174  static MneSourceSpaceOld* make_volume_source_space(MneSurfaceOld* surf,
175  float grid,
176  float exclude,
177  float mindist);
178 
179  //============================= mne_source_space.c =============================
180 
181  static MneSourceSpaceOld* mne_new_source_space(int np);
182 
183  //============================= mne_bem_surface_io.c =============================
184 
185  //Refactored: mne_read_bem_surface (mne_bem_surface_io.c)
186  static MneSurfaceOld* read_bem_surface(const QString& name, /* Filename */
187  int which, /* Which surface are we looking for (-1 loads the first one)*/
188  int add_geometry, /* Add the geometry information */
189  float *sigmap);
190 
191  static MneSurfaceOld* mne_read_bem_surface2(char *name, /* Filename */
192  int which, /* Which surface are we looking for (-1 loads the first one)*/
193  int add_geometry, /* Add the geometry information */
194  float *sigmap); /* Conductivity? */
195 
196  //Refactored: read_bem_surface (mne_bem_surface_io.c)
197  static MneSurfaceOld* read_bem_surface( const QString& name, /* Filename */
198  int which, /* Which surface are we looking for (-1 loads the first one)*/
199  int add_geometry, /* Add the geometry information */
200  float *sigmap, /* Conductivity? */
201  bool check_too_many_neighbors);
202 
203  //============================= mne_project_to_surface.c =============================
204 
205  static void mne_triangle_coords(float *r, /* Location of a point */
206  MneSurfaceOld* s, /* The surface */
207  int tri, /* Which triangle */
208  float *x, /* Coordinates of the point on the triangle */
209  float *y,
210  float *z);
211 
212  static int nearest_triangle_point(float *r, /* Location of a point */
213  MneSurfaceOld* s, /* The surface */
214  void *user, /* Something precomputed */
215  int tri, /* Which triangle */
216  float *x, /* Coordinates of the point on the triangle */
217  float *y,
218  float *z);
219 
220  static void project_to_triangle(MneSurfaceOld* s,
221  int tri,
222  float p,
223  float q,
224  float *r);
225 
226  static int mne_nearest_triangle_point(float *r, /* Location of a point */
227  MneSurfaceOld* s, /* The surface */
228  int tri, /* Which triangle */
229  float *x, /* Coordinates of the point on the triangle */
230  float *y,
231  float *z);
232 
233  static int mne_project_to_surface(MneSurfaceOld* s, void *proj_data, float *r, int project_it, float *distp);
234 
235  static void mne_project_to_triangle(MneSurfaceOld* s,
236  int best,
237  float *r,
238  float *proj);
239 
240  static void mne_find_closest_on_surface_approx(MneSurfaceOld* s, float **r, int np, int *nearest, float *dist, int nstep);
241 
242  static void decide_search_restriction(MneSurfaceOld* s,
243  MneProjData* p,
244  int approx_best, /* We know the best triangle approximately
245  * already */
246  int nstep,
247  float *r);
248 
249  static void activate_neighbors(MneSurfaceOld* s, int start, int *act, int nstep);
250 
251  //============================= mne_source_space.c =============================
252 
253  static int mne_read_source_spaces(const QString& name, /* Read from here */
254  MneSourceSpaceOld* **spacesp, /* These are the results */
255  int *nspacep);
256 
257  static void mne_source_space_update_inuse(MneSourceSpaceOld* s,
258  int *new_inuse);
259 
260  static int mne_is_left_hemi_source_space(MneSourceSpaceOld* s);
261 
262  static int mne_transform_source_space(MneSourceSpaceOld* ss, FIFFLIB::FiffCoordTransOld* t);
263 
264  static int mne_transform_source_spaces_to(int coord_frame, /* Which coord frame do we want? */
265  FIFFLIB::FiffCoordTransOld* t, /* The coordinate transformation */
266  MneSourceSpaceOld* *spaces, /* A list of source spaces */
267  int nspace);
268 
269  //============================= mne_forward_solution.c =============================
270 
271  static void enable_all_sources(MneSourceSpaceOld* s);
272 
273  //============================= resrict_sources.c =============================
274 
275  static int restrict_sources_to_labels(MneSourceSpaceOld* *spaces,
276  int nspace,
277  const QStringList& labels,
278  int nlabel);
279 
280  //============================= mne_labels.c =============================
281  //TODO Move to separate label class
282  static int mne_find_sources_in_label(char *label, /* The label file */
283  MneSourceSpaceOld* s, /* The corresponding source space hemisphere */
284  int off, /* Offset to the complete source space */
285  int **selp, /* Sources selected */
286  int *nselp);
287 
288  static int mne_read_label(const QString& label, /* The label file */
289  char **commentp, /* The comment in the file */
290  int **selp, /* Points in label */
291  int *nselp);
292 
293  static int mne_write_label(char *label, /* The label file */
294  char *comment,
295  int *sel, /* Points in label */
296  int nsel, /* How many? */
297  float **rr);
298 
299  static int mne_label_area(char *label, /* The label file */
300  MneSourceSpaceOld* s, /* The associated source space */
301  float *areap);
302 
303  //============================= mne_add_geometry_info.c =============================
304 
305  static void mne_add_triangle_data(MneSourceSpaceOld* s);
306 
307  //============================= mne_add_geometry_info.c =============================
308 
309  static void mne_compute_cm(float **rr, int np, float *cm);
310 
311  static void mne_compute_surface_cm(MneSurfaceOld* s);
312 
313  static void calculate_vertex_distances(MneSourceSpaceOld* s);
314 
315  static int mne_add_vertex_normals(MneSourceSpaceOld* s);
316 
317  static int add_geometry_info(MneSourceSpaceOld* s, int do_normals, int *border, int check_too_many_neighbors);
318 
319  static int mne_source_space_add_geometry_info(MneSourceSpaceOld* s, int do_normals);
320 
321  static int mne_source_space_add_geometry_info2(MneSourceSpaceOld* s, int do_normals);
322 
323  //============================= digitizer.c =============================
324 
325  static int align_fiducials(FIFFLIB::FiffDigitizerData* head_dig,
327  MneMshDisplaySurface* head_surf,
328  int niter,
329  int scale_head,
330  float omit_dist,
331  float *scales);
332 
333  static void get_head_scale(FIFFLIB::FiffDigitizerData* dig,
334  float **mri_fid,
335  MneMshDisplaySurface* head_surf,
336  float *scales);
337 
338  static int discard_outlier_digitizer_points(FIFFLIB::FiffDigitizerData* d,
339  MneMshDisplaySurface* head,
340  float maxdist);
341 
342  static void calculate_digitizer_distances(FIFFLIB::FiffDigitizerData* dig, MneMshDisplaySurface* head,
343  int do_all, int do_approx);
344 
345  static int iterate_alignment_once(FIFFLIB::FiffDigitizerData* dig, /* The digitizer data */
346  MneMshDisplaySurface* head, /* The head surface */
347  int nasion_weight, /* Weight for the nasion */
348  float *nasion_mri, /* Fixed correspondence point for the nasion (optional) */
349  int last_step); /* Is this the last iteration step */
350 
351  static float rms_digitizer_distance(FIFFLIB::FiffDigitizerData* dig, MneMshDisplaySurface* head);
352 
353  //============================= display_surfaces.c =============================
354 
355  static void scale_display_surface(MneMshDisplaySurface* surf,
356  float *scales);
357 
358  static void add_uniform_curv(MneSurfaceOld* s);
359 
360  //============================= mne_filename_util.c =============================
361 
362  static char * mne_compose_surf_name(const char *subj,
363  const char *name,
364  const char *prefix);
365 
366  //============================= mne_surface_io.c =============================
367 
368  static MneSourceSpaceOld* mne_load_surface(char *surf_file,
369  char *curv_file);
370 
371  static MneSourceSpaceOld* mne_load_surface_geom(char *surf_file,
372  char *curv_file,
373  int add_geometry,
374  int check_too_many_neighbors);
375 
376  static int mne_read_triangle_file(char *fname,
377  int *nvertp,
378  int *ntrip,
379  float ***vertp,
380  int ***trip,
381  void **tagsp);
382 
383  static int mne_read_curvature_file(char *fname,
384  float **curvsp,
385  int *ncurvp);
386 
387  static int check_quad(float **rr);
388 
389  static int check_vertex(int no, int maxno);
390 
391  //============================= mne_mgh_mri_io.c =============================
392 
393  static MneVolGeom* mne_get_volume_geom_from_tag(void *tagsp);
394 
395  static MneVolGeom* mne_dup_vol_geom(MneVolGeom* g);
396 
397  static int mne_read_mgh_tags(FILE *fp, void **tagsp);
398 
399  static int read_next_tag(FILE *fp, int *tagp, long long *lenp, unsigned char **datap);
400 
401  static int read_tag_data(FILE *fp, int tag, long long nbytes, unsigned char **val, long long *nbytesp);
402 
403  static MneMghTagGroup* mne_add_mgh_tag_to_group(MneMghTagGroup* g, int tag, long long len, unsigned char *data);
404 
405  static MneVolGeom* read_vol_geom(FILE *fp);
406 
407  //============================= mne_binio.c =============================
408 
409  static int mne_read_int3(FILE *in, int *ival);
410 
411  static int mne_read_int(FILE *in, qint32 *ival);
412 
413  static int mne_read_int2(FILE *in, int *ival);
414 
415  static int mne_read_float(FILE *in, float *fval);
416 
417  static int mne_read_long(FILE *in, long long *lval);
418 
419  //============================= mne_misc.c =============================
420 
421  static char *mne_strdup(const char *s);
422 
423 public:
424  int type; /* Is this a volume or a surface */
425  QString subject; /* Name (id) of the subject */
426  int id; /* Surface id */
427  int coord_frame; /* Which coordinate system are the data in now */
428  /*
429  * These relate to the FreeSurfer way
430  */
431  MneVolGeom *vol_geom; /* MRI volume geometry information as FreeSurfer likes it */
432  void *mgh_tags; /* Tags listed in the file */
433  /*
434  * These are meaningful for both surfaces and volumes
435  */
436  int np; /* Number of vertices */
437  float **rr; /* The vertex locations */
438  float **nn; /* Surface normals at these points */
439  float cm[3]; /* Center of mass */
440 
441  int *inuse; /* Is this point in use in the source space */
442  int *vertno; /* Vertex numbers of the used vertices in the full source space */
443  int nuse; /* Number of points in use */
444 
445  int **neighbor_vert; /* Vertices neighboring each vertex */
446  int *nneighbor_vert; /* Number of vertices neighboring each vertex */
447  float **vert_dist; /* Distances between neigboring vertices */
448  /*
449  * These are for surfaces only
450  */
451  int ntri; /* Number of triangles */
452  MneTriangle* tris; /* The triangulation information */
453  int **itris; /* The vertex numbers */
454  float tot_area; /* Total area of the surface, computed from the triangles */
455 
456  int nuse_tri; /* The triangulation corresponding to the vertices in use */
457  MneTriangle* use_tris; /* The triangulation information for the vertices in use */
458  int **use_itris; /* The vertex numbers for the 'use' triangulation */
459 
460  int **neighbor_tri; /* Neighboring triangles for each vertex Note: number of entries varies for vertex to vertex */
461  int *nneighbor_tri; /* Number of neighboring triangles for each vertex */
462 
463  MneNearest* nearest; /* Nearest inuse vertex info (number of these is the same as the number vertices) */
464  MnePatchInfo* *patches; /* Patch information (number of these is the same as the number of points in use) */
465  int npatch; /* How many (should be same as nuse) */
466 
467  FIFFLIB::FiffSparseMatrix* dist; /* Distances between the (used) vertices (along the surface). */
468  float dist_limit; /* Distances above this (in volume) have not been calculated. If negative, only used vertices have been considered */
469 
470  float *curv; /* The FreeSurfer curvature values */
471  float *val; /* Some other values associated with the vertices */
472  /*
473  * These are for volumes only
474  */
475  FIFFLIB::FiffCoordTransOld* voxel_surf_RAS_t; /* Transform from voxel coordinate to the surface RAS (MRI) coordinates */
476  int vol_dims[3]; /* Dimensions of the volume grid (width x height x depth) NOTE: This will be present only if the source space is a complete rectangular grid with unused vertices included */
477  float voxel_size[3]; /* Derived from the above */
478  FIFFLIB::FiffSparseMatrix* interpolator; /* Matrix to interpolate into an MRI volume */
479  QString MRI_volume; /* The name of the file the above interpolator is based on */
480  FIFFLIB::FiffCoordTransOld* MRI_voxel_surf_RAS_t;
481  FIFFLIB::FiffCoordTransOld* MRI_surf_RAS_RAS_t; /* Transform from surface RAS to RAS coordinates in the associated MRI volume */
482  int MRI_vol_dims[3]; /* Dimensions of the MRI volume (width x height x depth) */
483  /*
484  * Possibility to add user-defined data
485  */
486  void *user_data; /* Anything else we want */
487  mneUserFreeFunc user_data_free; /* Function to set the above free */
488 
489 // ### OLD STRUCT ###
490 //typedef struct { /* This defines a source space or a surface */
491 // int type; /* Is this a volume or a surface */
492 // char *subject; /* Name (id) of the subject */
493 // int id; /* Surface id */
494 // int coord_frame; /* Which coordinate system are the data in now */
495 // /*
496 // * These relate to the FreeSurfer way
497 // */
498 // mneVolGeom vol_geom; /* MRI volume geometry information as FreeSurfer likes it */
499 // void *mgh_tags; /* Tags listed in the file */
500 // /*
501 // * These are meaningful for both surfaces and volumes
502 // */
503 // int np; /* Number of vertices */
504 // float **rr; /* The vertex locations */
505 // float **nn; /* Surface normals at these points */
506 // float cm[3]; /* Center of mass */
507 
508 // int *inuse; /* Is this point in use in the source space */
509 // int *vertno; /* Vertex numbers of the used vertices in the full source space */
510 // int nuse; /* Number of points in use */
511 
512 // int **neighbor_vert; /* Vertices neighboring each vertex */
513 // int *nneighbor_vert; /* Number of vertices neighboring each vertex */
514 // float **vert_dist; /* Distances between neigboring vertices */
515 // /*
516 // * These are for surfaces only
517 // */
518 // int ntri; /* Number of triangles */
519 // MneTriangle* tris; /* The triangulation information */
520 // int **itris; /* The vertex numbers */
521 // float tot_area; /* Total area of the surface, computed from the triangles */
522 
523 // int nuse_tri; /* The triangulation corresponding to the vertices in use */
524 // MneTriangle* use_tris; /* The triangulation information for the vertices in use */
525 // int **use_itris; /* The vertex numbers for the 'use' triangulation */
526 
527 // int **neighbor_tri; /* Neighboring triangles for each vertex Note: number of entries varies for vertex to vertex */
528 // int *nneighbor_tri; /* Number of neighboring triangles for each vertex */
529 
530 // mneNearest nearest; /* Nearest inuse vertex info (number of these is the same as the number vertices) */
531 // mnePatchInfo *patches; /* Patch information (number of these is the same as the number of points in use) */
532 // int npatch; /* How many (should be same as nuse) */
533 
534 // mneSparseMatrix dist; /* Distances between the (used) vertices (along the surface). */
535 // float dist_limit; /* Distances above this (in volume) have not been calculated. If negative, only used vertices have been considered */
536 
537 // float *curv; /* The FreeSurfer curvature values */
538 // float *val; /* Some other values associated with the vertices */
539 // /*
540 // * These are for volumes only
541 // */
542 // MNELIB::FiffCoordTransOld* voxel_surf_RAS_t; /* Transform from voxel coordinate to the surface RAS (MRI) coordinates */
543 // int vol_dims[3]; /* Dimensions of the volume grid (width x height x depth) NOTE: This will be present only if the source space is a complete rectangular grid with unused vertices included */
544 // float voxel_size[3]; /* Derived from the above */
545 // mneSparseMatrix interpolator; /* Matrix to interpolate into an MRI volume */
546 // char *MRI_volume; /* The name of the file the above interpolator is based on */
547 // MNELIB::FiffCoordTransOld* MRI_voxel_surf_RAS_t;
548 // MNELIB::FiffCoordTransOld* MRI_surf_RAS_RAS_t; /* Transform from surface RAS to RAS coordinates in the associated MRI volume */
549 // int MRI_vol_dims[3]; /* Dimensions of the MRI volume (width x height x depth) */
550 // /*
551 // * Possibility to add user-defined data
552 // */
553 // void *user_data; /* Anything else we want */
554 // mneUserFreeFunc user_data_free; /* Function to set the above free */
555 //} *mneSurfaceOrVolume,mneSurfaceOrVolumeRec;
556 };
557 
558 //=============================================================================================================
559 // INLINE DEFINITIONS
560 //=============================================================================================================
561 } // NAMESPACE MNELIB
562 
563 #endif // MNESURFACEORVOLUME_H
MNELIB::MneNearest
This is used in the patch definitions.
Definition: mne_nearest.h:77
MNELIB::MneMghTagGroup
The MneMghTagGroup class.
Definition: mne_mgh_tag_group.h:78
FIFFLIB::FiffSparseMatrix
Data associated with MNE computations for each mneMeasDataSet.
Definition: fiff_sparse_matrix.h:74
MNELIB::MneVolGeom
MRI data volume geometry information like FreeSurfer keeps it.
Definition: mne_vol_geom.h:76
MNELIB::MneProjData
The MneProjData class.
Definition: mne_proj_data.h:74
MNESHARED_EXPORT
#define MNESHARED_EXPORT
Definition: mne_global.h:56
MNELIB::MneMshDisplaySurface
The MNE Msh Display Surface class holds information about a surface to be rendered.
Definition: mne_msh_display_surface.h:79
MNELIB::MneSurfaceOrVolume
This defines a source space or a surface.
Definition: mne_surface_or_volume.h:113
FIFFLIB::FiffCoordTransOld
Coordinate transformation descriptor.
Definition: fiff_coord_trans_old.h:80
MNELIB::MneSurfaceOrVolume::ConstSPtr
QSharedPointer< const MneSurfaceOrVolume > ConstSPtr
Definition: mne_surface_or_volume.h:117
MNELIB::MneSourceSpaceOld
This defines a source space.
Definition: mne_source_space_old.h:76
MNELIB::MneTriangle
Triangle data.
Definition: mne_triangle.h:75
MNELIB::MneSurfaceOrVolume::SPtr
QSharedPointer< MneSurfaceOrVolume > SPtr
Definition: mne_surface_or_volume.h:116
FIFFLIB::FiffDigitizerData
Digitization points container and description.
Definition: fiff_digitizer_data.h:73
MNELIB::MnePatchInfo
One item in a derivation data set.
Definition: mne_patch_info.h:77
MNELIB::MneSurfaceOld
This defines a surface.
Definition: mne_surface_old.h:76