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