OpenMEEG
sensors.h
Go to the documentation of this file.
1 // Project Name: OpenMEEG (http://openmeeg.github.io)
2 // © INRIA and ENPC under the French open source license CeCILL-B.
3 // See full copyright notice in the file LICENSE.txt
4 // If you make a copy of this file, you must either:
5 // - provide also LICENSE.txt and modify this header to refer to it.
6 // - replace this header by the LICENSE.txt content.
7 
8 #pragma once
9 
10 #include <fstream>
11 #include <sstream>
12 #include <stdlib.h>
13 #include <string>
14 #include <vector>
15 
16 #include <IOUtils.H>
17 #include <vector.h>
18 #include <matrix.h>
19 #include <sparse_matrix.h>
20 
21 #include <geometry.h>
22 #include <om_common.h>
23 
24 #include <OpenMEEG_Export.h>
25 
26 namespace OpenMEEG {
27 
60 
61  class OPENMEEG_EXPORT Sensors {
62 
63  void init_labels(const Strings& labels) {
64  m_pointSensorIdx = std::vector<size_t>(labels.size());
65  for (std::size_t i=0; i<labels.size(); ++i)
66  m_pointSensorIdx[i] = getSensorIdx(m_names[i]);
67  }
68 
69  public:
70 
72 
73  Sensors(): m_nb(0),geometry(nullptr) { }
74 
76 
77  Sensors(const Geometry& g): m_nb(0),geometry(&g) { }
78 
80 
81  Sensors(const char* filename): geometry(nullptr) { load(filename,'t'); }
82 
84 
85  Sensors(const char* filename,const Geometry& g): geometry(&g) { load(filename,'t'); }
86 
87  Sensors(const Matrix& positions,const Geometry& g):
88  m_nb(positions.nlin()),m_positions(positions),m_radii(m_nb),geometry(&g)
89  {
90  m_radii.set(0.0);
91  findInjectionTriangles();
92  }
93 
94  Sensors(const Strings& labels,const Matrix& positions,const Matrix& orientations,const Vector& weights,const Vector& radii):
95  m_nb(labels.size()),m_names(labels),m_positions(positions),m_orientations(orientations),m_weights(weights),m_radii(radii)
96  {
97  init_labels(labels);
98  }
99 
100  Sensors(const Strings& labels,const Matrix& positions,const Matrix& orientations,const Vector& weights,const Vector& radii,const Geometry& g):
101  m_nb(labels.size()),m_names(labels),m_positions(positions),m_orientations(orientations),m_weights(weights),m_radii(radii),geometry(&g)
102  {
103  // find triangles on which to inject the currents and compute weights
104  findInjectionTriangles();
105  init_labels(labels);
106  }
107 
109 
110  void load(const char* filename,const char filetype='t');
111  void load(const std::string& filename,const char filetype='t') { load(filename.c_str(),filetype); }
112 
114 
115  void load(std::istream& in);
116 
117  void save(const char* filename) const;
118  void save(const std::string& filename) const { save(filename.c_str()); }
119 
121 
122  size_t getNumberOfSensors() const { return m_nb; }
123 
125 
126  size_t getNumberOfPositions() const { return m_positions.nlin(); }
127 
129 
130  Matrix& getPositions() { return m_positions; }
131  const Matrix& getPositions() const { return m_positions; }
132 
134 
135  Matrix& getOrientations() { return m_orientations; }
136  const Matrix& getOrientations() const { return m_orientations; }
137 
139 
140  Strings& getNames() { return m_names; }
141  Strings getNames() const { return m_names; }
142 
143  bool hasRadii() const { return m_radii.nlin()>0; }
144  bool hasOrientations() const { return m_orientations.nlin()>0; }
145  bool hasNames() const { return m_names.size()==m_nb; }
146 
148 
149  Vector getPosition(const size_t idx) const { return m_positions.getlin(idx); }
150 
152 
153  Vector getOrientation(const size_t idx) const { return m_orientations.getlin(idx); }
154 
156 
157  std::string getName(const size_t idx) const {
158  om_assert(idx<m_names.size());
159  return m_names[idx];
160  }
161 
163 
164  void setPosition(const size_t idx,const Vector& pos) { return m_positions.setlin(idx,pos); }
165 
167 
168  void setOrientation(const size_t idx,const Vector& orient) { return m_orientations.setlin(idx,orient); }
169 
170  bool hasSensor(const std::string& name) const {
171  return std::find(m_names.begin(),m_names.end(),name)!=m_names.end();
172  }
173 
174  size_t getSensorIdx(const std::string& name) const {
175  const auto& it = std::find(m_names.begin(),m_names.end(),name);
176  if (it==m_names.end())
177  throw OpenMEEG::SensorError("Unknown sensor \"" + name + "\"");
178  return std::distance(m_names.begin(),it);
179  }
180 
182 
183  Triangles getInjectionTriangles(const size_t idx) const {
184  om_assert(idx<m_triangles.size());
185  return m_triangles[idx];
186  }
187 
188  Vector getRadii() const { return m_radii; }
189  Vector getWeights() const { return m_weights; }
190 
192  SparseMatrix weight_matrix(getNumberOfSensors(),getNumberOfPositions());
193  for(size_t i=0; i<getNumberOfPositions(); ++i)
194  weight_matrix(m_pointSensorIdx[i],i) = m_weights(i);
195  return weight_matrix;
196  }
197 
199 
200  bool isEmpty() { return m_nb==0; }
201 
203 
204  void info() const;
205 
206  private:
207 
208  void findInjectionTriangles();
209 
210  size_t m_nb;
211  Strings m_names;
212  Matrix m_positions;
213  Matrix m_orientations;
214  Vector m_weights;
215  Vector m_radii;
216  std::vector<Triangles> m_triangles;
217  const Geometry* geometry;
218  std::vector<size_t> m_pointSensorIdx;
219  };
220 }
Geometry contains the electrophysiological model Vertices, meshes and domains are stored in this geom...
Definition: geometry.h:31
Matrix class Matrix class.
Definition: matrix.h:28
Sensors class for EEG and MEG sensors.
Definition: sensors.h:61
size_t getSensorIdx(const std::string &name) const
Definition: sensors.h:174
bool hasRadii() const
Return true if contains radii.
Definition: sensors.h:143
Vector getRadii() const
Definition: sensors.h:188
Strings & getNames()
Return sensors names.
Definition: sensors.h:140
Sensors(const Geometry &g)
Default constructor with a geometry. Number of sensors = 0.
Definition: sensors.h:77
Sensors(const Strings &labels, const Matrix &positions, const Matrix &orientations, const Vector &weights, const Vector &radii)
Definition: sensors.h:94
Vector getPosition(const size_t idx) const
Return the position (3D point) of the integration point idx.
Definition: sensors.h:149
void save(const char *filename) const
bool isEmpty()
Return if the sensors object is empty. The sensors object is empty if its number of sensors is null.
Definition: sensors.h:200
Sensors()
Default constructor. Number of sensors = 0.
Definition: sensors.h:73
Vector getWeights() const
Definition: sensors.h:189
const Matrix & getOrientations() const
Definition: sensors.h:136
std::string getName(const size_t idx) const
Return the name of the idx_th sensor.
Definition: sensors.h:157
bool hasNames() const
Return true if contains all sensors names.
Definition: sensors.h:145
size_t getNumberOfPositions() const
Return the number of integration points.
Definition: sensors.h:126
Strings getNames() const
Definition: sensors.h:141
Sensors(const Strings &labels, const Matrix &positions, const Matrix &orientations, const Vector &weights, const Vector &radii, const Geometry &g)
Definition: sensors.h:100
Vector getOrientation(const size_t idx) const
Return the orientations (3D point) of the integration point idx.
Definition: sensors.h:153
SparseMatrix getWeightsMatrix() const
Definition: sensors.h:191
void load(std::istream &in)
Load description file of sensors from stream.
void save(const std::string &filename) const
Definition: sensors.h:118
void info() const
get info about sensors.
size_t getNumberOfSensors() const
Return the number of sensors.
Definition: sensors.h:122
const Matrix & getPositions() const
Definition: sensors.h:131
Sensors(const Matrix &positions, const Geometry &g)
Definition: sensors.h:87
Triangles getInjectionTriangles(const size_t idx) const
For EIT, get triangles under the current injection electrode.
Definition: sensors.h:183
void load(const char *filename, const char filetype='t')
Load sensors from file. Filetype is 't' for text file or 'b' for binary file.
Matrix & getOrientations()
Return sensors orientations.
Definition: sensors.h:135
bool hasSensor(const std::string &name) const
Definition: sensors.h:170
Matrix & getPositions()
Return sensors positions.
Definition: sensors.h:130
void setOrientation(const size_t idx, const Vector &orient)
Set the orientation (3D point) of the integration point i.
Definition: sensors.h:168
bool hasOrientations() const
Return true if contains orientations.
Definition: sensors.h:144
Sensors(const char *filename, const Geometry &g)
Construct from file and geometry (for EIT).
Definition: sensors.h:85
void setPosition(const size_t idx, const Vector &pos)
Set the position (3D point) of the integration point i.
Definition: sensors.h:164
void load(const std::string &filename, const char filetype='t')
Definition: sensors.h:111
Sensors(const char *filename)
Construct from text file.
Definition: sensors.h:81
std::vector< Triangle > Triangles
Definition: triangle.h:138
std::vector< std::string > Strings
Definition: om_common.h:20