35#define _USE_MATH_DEFINES
78 double tot_angle, angle;
79 for (k = 0, tot_angle = 0.0; k <
ntri; k++) {
90 double a, b, c, v1, v2, det;
93 this_tri = &
tris[tri];
95 Eigen::Vector3d
rr = (r - this_tri->
r1).cast<double>();
96 z =
rr.dot(this_tri->
nn.cast<
double>());
98 a = this_tri->
r12.cast<
double>().squaredNorm();
99 b = this_tri->
r13.cast<
double>().squaredNorm();
100 c = this_tri->
r12.cast<
double>().dot(this_tri->
r13.cast<
double>());
102 v1 =
rr.dot(this_tri->
r12.cast<
double>());
103 v2 =
rr.dot(this_tri->
r13.cast<
double>());
107 x = (b * v1 - c * v2) / det;
108 y = (a * v2 - c * v1) / det;
115 double p, q, p0, q0, t0;
116 double a, b, c, v1, v2, det;
117 double best,
dist, dist0;
121 this_tri = &
tris[tri];
122 Eigen::Vector3d
rr = (r - this_tri->
r1).cast<double>();
123 dist =
rr.dot(this_tri->
nn.cast<
double>());
133 a = this_tri->
r12.cast<
double>().squaredNorm();
134 b = this_tri->
r13.cast<
double>().squaredNorm();
135 c = this_tri->
r12.cast<
double>().dot(this_tri->
r13.cast<
double>());
138 v1 =
rr.dot(this_tri->
r12.cast<
double>());
139 v2 =
rr.dot(this_tri->
r13.cast<
double>());
143 p = (b * v1 - c * v2) / det;
144 q = (a * v2 - c * v1) / det;
146 if (p >= 0.0 && p <= 1.0 &&
147 q >= 0.0 && q <= 1.0 &&
157 p0 = p + 0.5 * (q * c) / a;
158 if (p0 < 0.0) p0 = 0.0;
159 else if (p0 > 1.0) p0 = 1.0;
161 dist0 = sqrt((p - p0) * (p - p0) * a +
162 (q - q0) * (q - q0) * b +
163 (p - p0) * (q - q0) * c +
172 t0 = 0.5 * ((2.0 * a - c) * (1.0 - p) + (2.0 * b - c) * q) / (a + b - c);
173 if (t0 < 0.0) t0 = 0.0;
174 else if (t0 > 1.0) t0 = 1.0;
177 dist0 = sqrt((p - p0) * (p - p0) * a +
178 (q - q0) * (q - q0) * b +
179 (p - p0) * (q - q0) * c +
191 q0 = q + 0.5 * (p * c) / b;
192 if (q0 < 0.0) q0 = 0.0;
193 else if (q0 > 1.0) q0 = 1.0;
194 dist0 = sqrt((p - p0) * (p - p0) * a +
195 (q - q0) * (q - q0) * b +
196 (p - p0) * (q - q0) * c +
220 return Eigen::Vector3f(
221 this_tri->
r1 + p * this_tri->
r12 + q * this_tri->
r13
246 for (best = -1, k = 0; k <
ntri; k++) {
248 if (best < 0 || std::fabs(
dist) < std::fabs(dist0)) {
263 Eigen::VectorXi& nearest_tri,
264 Eigen::VectorXf&
dist,
int nstep)
const
266 auto p = std::make_unique<MNEProjData>(
this);
269 qInfo(
"%s for %d points %d steps...", nearest_tri[0] < 0 ?
"Closest" :
"Approx closest", np_points, nstep);
271 for (k = 0; k < np_points; k++) {
272 was = nearest_tri[k];
273 Eigen::Vector3f pt = Eigen::Map<const Eigen::Vector3f>(r.row(k).data());
276 if (nearest_tri[k] < 0) {
291 const Eigen::Vector3f& r)
const
294 Eigen::Vector3f diff_vec;
295 float dist_val, mindist;
298 for (k = 0; k <
ntri; k++)
301 if (approx_best < 0) {
304 for (k = 0; k <
np; k++) {
305 diff_vec =
rr.row(k).transpose() - r;
306 dist_val = diff_vec.norm();
315 diff_vec = this_tri->
r1 - r;
316 mindist = diff_vec.norm();
317 minvert = this_tri->
vert[0];
319 diff_vec = this_tri->
r2 - r;
320 dist_val = diff_vec.norm();
321 if (dist_val < mindist) {
323 minvert = this_tri->
vert[1];
325 diff_vec = this_tri->
r3 - r;
326 dist_val = diff_vec.norm();
327 if (dist_val < mindist) {
329 minvert = this_tri->
vert[2];
387 QList<FiffDirNode::SPtr> surfs;
388 QList<FiffDirNode::SPtr> bems;
393 int nnode, ntri_count;
395 std::unique_ptr<MNESurface> s_ptr;
397 MatrixXf tmp_node_normals;
399 float sigmaLocal = -1.0;
401 MatrixXi tmp_triangles;
406 bems = stream->dirtree()->dir_tree_find(
FIFFB_BEM);
407 if (bems.size() > 0) {
414 if (surfs.size() == 0) {
415 qCritical(
"No BEM surfaces found in %s", name.toUtf8().constData());
416 stream->close();
return nullptr;
419 for (k = 0; k < surfs.size(); ++k) {
422 id = *t_pTag->toInt();
428 qCritical(
"Desired surface not found in %s", name.toUtf8().constData());
429 stream->close();
return nullptr;
436 stream->close();
return nullptr;
438 nnode = *t_pTag->toInt();
441 stream->close();
return nullptr;
443 ntri_count = *t_pTag->toInt();
446 stream->close();
return nullptr;
448 tmp_nodes = t_pTag->toFloatMatrix().transpose();
451 tmp_node_normals = t_pTag->toFloatMatrix().transpose();
455 stream->close();
return nullptr;
457 tmp_triangles = t_pTag->toIntMatrix().transpose();
466 sigmaLocal = *t_pTag->toFloat();
471 s_ptr = std::make_unique<MNESurface>();
473 tmp_triangles.array() -= 1;
474 s->
itris = tmp_triangles;
476 s->
sigma = sigmaLocal;
479 if (tmp_node_normals.rows() > 0)
480 s->
nn = tmp_node_normals;
481 s->
ntri = ntri_count;
489 s->
cm[0] = s->
cm[1] = s->
cm[2] = 0.0;
492 if (check_too_many_neighbors) {
503 else if (s->
nn.rows() == 0) {
512 s->
inuse = Eigen::VectorXi::Ones(s->
np);
513 s->
vertno = Eigen::VectorXi::LinSpaced(s->
np, 0, s->
np - 1);
Lightweight triangulated surface (vertices, triangles, normals) used by surface-based routines.
Single-hemisphere source space (cortical surface or volume grid) loaded from FIFF.
Legacy MNE-C aggregator for SSP projections plus the channel list they apply to.
Triangle descriptor with cached centroid, area and normal vectors.
#define FIFF_MNE_COORD_FRAME
#define FIFFV_MNE_SPACE_SURFACE
#define FIFF_BEM_SURF_NNODE
#define FIFF_BEM_SURF_NODES
#define FIFF_BEM_SURF_TRIANGLES
#define FIFF_BEM_COORD_FRAME
#define FIFF_BEM_SURF_NTRI
#define FIFF_BEM_SURF_NORMALS
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
FIFF binary tag-stream layer: wraps a QIODevice to read and write FIFF tags, directories,...
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O, in-memory data structures and high-level readers/writers.
QSharedPointer< FiffDirNode > SPtr
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
QSharedPointer< FiffStream > SPtr
std::unique_ptr< FiffTag > UPtr
Auxiliary projection data computed from MNEProjOp for efficient repeated application.
double sum_solids(const Eigen::Vector3f &from) const
void find_closest_on_surface_approx(const PointsT &r, int np, Eigen::VectorXi &nearest_tri, Eigen::VectorXf &dist, int nstep) const
Eigen::Vector3f project_to_triangle(int tri, float p, float q) const
static std::unique_ptr< MNESurface > read_bem_surface2(const QString &name, int which, bool add_geometry)
void triangle_coords(const Eigen::Vector3f &r, int tri, float &x, float &y, float &z) const
static std::unique_ptr< MNESurface > read_bem_surface(const QString &name, int which, bool add_geometry)
int project_to_surface(const MNEProjData *proj_data, const Eigen::Vector3f &r, float &distp) const
void decide_search_restriction(MNEProjData &p, int approx_best, int nstep, const Eigen::Vector3f &r) const
int nearest_triangle_point(const Eigen::Vector3f &r, const MNEProjData *user, int tri, float &x, float &y, float &z) const
void activate_neighbors(int start, Eigen::VectorXi &act, int nstep) const
std::vector< Eigen::VectorXi > neighbor_tri
std::vector< Eigen::VectorXi > neighbor_vert
int add_geometry_info(bool do_normals, bool check_too_many_neighbors)
FIFFLIB::FiffSparseMatrix dist
Eigen::VectorXi nneighbor_vert
Eigen::VectorXi nneighbor_tri
static double solid_angle(const Eigen::Vector3f &from, const MNELIB::MNETriangle &tri)
std::vector< MNETriangle > tris
Eigen::Matrix< float, Eigen::Dynamic, 3, Eigen::RowMajor > PointsT
int add_geometry_info2(bool do_normals)
Per-triangle geometric data for a cortical or BEM surface.