MNE-CPP  0.1.9
A Framework for Electrophysiology
mne_surface.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 #include <QtCore/QtDebug>
41 
42 #include <fiff/fiff_constants.h>
43 #include <fiff/fiff_dir_entry.h>
44 #include <fiff/fiff_tag.h>
45 
46 #include "mne_surface.h"
47 
48 //=============================================================================================================
49 // USED NAMESPACES
50 //=============================================================================================================
51 
52 using namespace MNELIB;
53 using namespace FIFFLIB;
54 
55 //=============================================================================================================
56 // DEFINE MEMBER METHODS
57 //=============================================================================================================
58 
60 {
61  id = FIFFV_BEM_SURF_ID_UNKNOWN;
62  sigma = 1.0;
63  np = 0;
64  ntri = 0;
65  coord_frame = FIFFV_COORD_MRI;
66 }
67 
68 //=============================================================================================================
69 
70 bool MNESurface::read(QIODevice& p_IODevice, QList<MNESurface::SPtr>& surfaces)
71 {
72  FiffStream::SPtr fiffStream(new FiffStream(&p_IODevice));
73 
74  if(!fiffStream->open())
75  {
76  qCritical() << "Could not open FIFF stream!";
77  return false;
78  }
79 
80  return read(fiffStream, false, fiffStream->dirtree(), surfaces);
81 }
82 
83 //=============================================================================================================
84 
85 bool MNESurface::read(FiffStream::SPtr& p_pStream, bool add_geom, const FiffDirNode::SPtr& p_Tree, QList<MNESurface::SPtr>& surfaces)
86 {
87  if(add_geom)
88  {
89  // TODO Implement complete_surface_info from mne-matlab.
90  qWarning() << "add_geom flag is not yet implemented!";
91  }
92 
93  QList<FiffDirNode::SPtr>bem = p_Tree->dir_tree_find(FIFFB_BEM);
94  if(bem.isEmpty())
95  {
96  qCritical() << "No BEM block found!";
97  return false;
98  }
99 
100  QList<FiffDirNode::SPtr>bemsurf = p_Tree->dir_tree_find(FIFFB_BEM_SURF);
101  if(bemsurf.isEmpty())
102  {
103  qCritical() << "No BEM surfaces found!";
104  return false;
105  }
106 
107  FiffTag::SPtr tag(new FiffTag());
108  fiff_int_t coord_frame;
109  if(bem[0]->find_tag(p_pStream, FIFF_BEM_COORD_FRAME, tag))
110  {
111  coord_frame = *tag->toInt();
112  }else
113  {
114  qWarning() << "No FIFF_BEM_COORD_FRAME found!";
115  coord_frame = FIFFV_COORD_MRI;
116  }
117 
118  QList<FiffDirNode::SPtr>::Iterator itBemsurf;
119  for(itBemsurf = bemsurf.begin(); itBemsurf != bemsurf.end(); ++itBemsurf)
120  {
121  MNESurface::SPtr surf;
122  if(read(p_pStream, *itBemsurf, coord_frame, surf))
123  {
124  surfaces.append(surf);
125  }else
126  {
127  qWarning() << "Could not read surface!";
128  }
129  }
130 
131  return true;
132 }
133 
134 //=============================================================================================================
135 
137  const FIFFLIB::FiffDirNode::SPtr& dir, const fiff_int_t def_coord_frame,
138  MNESurface::SPtr& surf)
139 {
140  surf.reset(new MNESurface);
141  FiffTag::SPtr tag;
142 
143  // Read attributes //
144  //-----------------//
145 
146  if(dir->find_tag(fiffStream, FIFF_BEM_SURF_ID, tag))
147  {
148  surf->id = *tag->toInt();
149  }else
150  {
151  surf->id = FIFFV_BEM_SURF_ID_UNKNOWN;
152  qWarning() << "ID not found! Default: " << surf->id;
153  }
154 
155  if(dir->find_tag(fiffStream, FIFF_BEM_SIGMA, tag))
156  {
157  surf->sigma = *tag->toFloat();
158  }else
159  {
160  surf->sigma = 1.0;
161  qWarning() << "sigma not found! Default: " << surf->sigma;
162  }
163 
164  if(dir->find_tag(fiffStream, FIFF_BEM_SURF_NNODE, tag))
165  {
166  surf->np = *tag->toInt();
167  }else
168  {
169  qCritical() << "np not found!";
170  return false;
171  }
172 
173  if(dir->find_tag(fiffStream, FIFF_BEM_SURF_NTRI, tag))
174  {
175  surf->ntri = *tag->toInt();
176  }else
177  {
178  qCritical() << "ntri not found!";
179  return false;
180  }
181 
182  if(dir->find_tag(fiffStream, FIFF_MNE_COORD_FRAME, tag))
183  {
184  surf->coord_frame = *tag->toInt();
185  }else
186  {
187  qWarning()
188  << "FIFF_MNE_COORD_FRAME not found, trying FIFF_BEM_COORD_FRAME.";
189  if(dir->find_tag(fiffStream, FIFF_BEM_COORD_FRAME, tag))
190  {
191  surf->coord_frame = *tag->toInt();
192  }else
193  {
194  surf->coord_frame = def_coord_frame;
195  qWarning() << "FIFF_BEM_COORD_FRAME not found! Default: "
196  << surf->coord_frame;
197  }
198  }
199 
200  // Read data //
201  //-----------//
202 
203  if(dir->find_tag(fiffStream, FIFF_BEM_SURF_NODES, tag) && surf->np > 0)
204  {
205  //ToDo: rows, cols are exchanged - usually 3 cols -> ToDo: transpose
206  surf->rr.resize(3, surf->np);
207  surf->rr = tag->toFloatMatrix();
208  }else
209  {
210  qCritical() << "Vertices not found!";
211  return false;
212  }
213 
214  if(dir->find_tag(fiffStream, FIFF_MNE_SOURCE_SPACE_NORMALS, tag)
215  && surf->np > 0)
216  {
217  //ToDo: rows, cols are exchanged - usually 3 cols -> ToDo: transpose
218  surf->nn.resize(3, surf->np);
219  surf->nn = tag->toFloatMatrix();
220  }else
221  {
222  qWarning() << "Vertex normals not found!";
223  }
224 
225  if(dir->find_tag(fiffStream, FIFF_BEM_SURF_TRIANGLES, tag) && surf->ntri > 0)
226  {
227  //ToDo: rows, cols are exchanged - usually 3 cols -> ToDo: transpose
228  surf->tris.resize(3, surf->ntri);
229  surf->tris = tag->toIntMatrix();
230  }else
231  {
232  qCritical() << "Triangulation not found!";
233  return false;
234  }
235 
236  return true;
237 }
#define FIFF_BEM_SURF_TRIANGLES
Definition: fiff_file.h:739
#define FIFF_BEM_SURF_NNODE
Definition: fiff_file.h:736
BEM Surface.
Definition: mne_surface.h:82
QSharedPointer< FiffDirNode > SPtr
Definition: fiff_dir_node.h:77
Contains the declaration of the MNESurface class.
#define FIFF_BEM_SURF_ID
Definition: fiff_file.h:734
FiffDirEntry class declaration.
FIFF data tag.
Definition: fiff_tag.h:148
Fiff constants.
#define FIFF_MNE_SOURCE_SPACE_NORMALS
QSharedPointer< FiffTag > SPtr
Definition: fiff_tag.h:152
QSharedPointer< MNESurface > SPtr
Definition: mne_surface.h:85
#define FIFFB_BEM
Definition: fiff_file.h:403
FIFF File I/O routines.
Definition: fiff_stream.h:104
#define FIFFB_BEM_SURF
Definition: fiff_file.h:404
#define FIFF_BEM_SURF_NTRI
Definition: fiff_file.h:737
static bool read(QIODevice &p_IODevice, QList< MNESurface::SPtr > &surfaces)
Definition: mne_surface.cpp:70
QSharedPointer< FiffStream > SPtr
Definition: fiff_stream.h:107
FiffTag class declaration, which provides fiff tag I/O and processing methods.
#define FIFF_BEM_COORD_FRAME
Definition: fiff_file.h:746
#define FIFF_BEM_SURF_NODES
Definition: fiff_file.h:738
#define FIFF_BEM_SIGMA
Definition: fiff_file.h:747
#define FIFF_MNE_COORD_FRAME