52#define _USE_MATH_DEFINES
95 double tot_angle, angle;
96 for (k = 0, tot_angle = 0.0; k <
ntri; k++) {
107 double a, b, c, v1, v2, det;
110 this_tri = &
tris[tri];
112 Eigen::Vector3d
rr = (r - this_tri->
r1).cast<double>();
113 z =
rr.dot(this_tri->
nn.cast<
double>());
115 a = this_tri->
r12.cast<
double>().squaredNorm();
116 b = this_tri->
r13.cast<
double>().squaredNorm();
117 c = this_tri->
r12.cast<
double>().dot(this_tri->
r13.cast<
double>());
119 v1 =
rr.dot(this_tri->
r12.cast<
double>());
120 v2 =
rr.dot(this_tri->
r13.cast<
double>());
124 x = (b * v1 - c * v2) / det;
125 y = (a * v2 - c * v1) / det;
132 double p, q, p0, q0, t0;
133 double a, b, c, v1, v2, det;
134 double best,
dist, dist0;
138 this_tri = &
tris[tri];
139 Eigen::Vector3d
rr = (r - this_tri->
r1).cast<double>();
140 dist =
rr.dot(this_tri->
nn.cast<
double>());
150 a = this_tri->
r12.cast<
double>().squaredNorm();
151 b = this_tri->
r13.cast<
double>().squaredNorm();
152 c = this_tri->
r12.cast<
double>().dot(this_tri->
r13.cast<
double>());
155 v1 =
rr.dot(this_tri->
r12.cast<
double>());
156 v2 =
rr.dot(this_tri->
r13.cast<
double>());
160 p = (b * v1 - c * v2) / det;
161 q = (a * v2 - c * v1) / det;
163 if (p >= 0.0 && p <= 1.0 &&
164 q >= 0.0 && q <= 1.0 &&
174 p0 = p + 0.5 * (q * c) / a;
175 if (p0 < 0.0) p0 = 0.0;
176 else if (p0 > 1.0) p0 = 1.0;
178 dist0 = sqrt((p - p0) * (p - p0) * a +
179 (q - q0) * (q - q0) * b +
180 (p - p0) * (q - q0) * c +
189 t0 = 0.5 * ((2.0 * a - c) * (1.0 - p) + (2.0 * b - c) * q) / (a + b - c);
190 if (t0 < 0.0) t0 = 0.0;
191 else if (t0 > 1.0) t0 = 1.0;
194 dist0 = sqrt((p - p0) * (p - p0) * a +
195 (q - q0) * (q - q0) * b +
196 (p - p0) * (q - q0) * c +
208 q0 = q + 0.5 * (p * c) / b;
209 if (q0 < 0.0) q0 = 0.0;
210 else if (q0 > 1.0) q0 = 1.0;
211 dist0 = sqrt((p - p0) * (p - p0) * a +
212 (q - q0) * (q - q0) * b +
213 (p - p0) * (q - q0) * c +
237 return Eigen::Vector3f(
238 this_tri->
r1 + p * this_tri->
r12 + q * this_tri->
r13
263 for (best = -1, k = 0; k <
ntri; k++) {
265 if (best < 0 || std::fabs(
dist) < std::fabs(dist0)) {
280 Eigen::VectorXi& nearest_tri,
281 Eigen::VectorXf&
dist,
int nstep)
const
283 auto p = std::make_unique<MNEProjData>(
this);
286 qInfo(
"%s for %d points %d steps...", nearest_tri[0] < 0 ?
"Closest" :
"Approx closest", np_points, nstep);
288 for (k = 0; k < np_points; k++) {
289 was = nearest_tri[k];
290 Eigen::Vector3f pt = Eigen::Map<const Eigen::Vector3f>(r.row(k).data());
293 if (nearest_tri[k] < 0) {
308 const Eigen::Vector3f& r)
const
311 Eigen::Vector3f diff_vec;
312 float dist_val, mindist;
315 for (k = 0; k <
ntri; k++)
318 if (approx_best < 0) {
321 for (k = 0; k <
np; k++) {
322 diff_vec =
rr.row(k).transpose() - r;
323 dist_val = diff_vec.norm();
332 diff_vec = this_tri->
r1 - r;
333 mindist = diff_vec.norm();
334 minvert = this_tri->
vert[0];
336 diff_vec = this_tri->
r2 - r;
337 dist_val = diff_vec.norm();
338 if (dist_val < mindist) {
340 minvert = this_tri->
vert[1];
342 diff_vec = this_tri->
r3 - r;
343 dist_val = diff_vec.norm();
344 if (dist_val < mindist) {
346 minvert = this_tri->
vert[2];
404 QList<FiffDirNode::SPtr> surfs;
405 QList<FiffDirNode::SPtr> bems;
410 int nnode, ntri_count;
412 std::unique_ptr<MNESurface> s_ptr;
414 MatrixXf tmp_node_normals;
416 float sigmaLocal = -1.0;
418 MatrixXi tmp_triangles;
423 bems = stream->dirtree()->dir_tree_find(
FIFFB_BEM);
424 if (bems.size() > 0) {
431 if (surfs.size() == 0) {
432 qCritical(
"No BEM surfaces found in %s", name.toUtf8().constData());
433 stream->close();
return nullptr;
436 for (k = 0; k < surfs.size(); ++k) {
439 id = *t_pTag->toInt();
445 qCritical(
"Desired surface not found in %s", name.toUtf8().constData());
446 stream->close();
return nullptr;
453 stream->close();
return nullptr;
455 nnode = *t_pTag->toInt();
458 stream->close();
return nullptr;
460 ntri_count = *t_pTag->toInt();
463 stream->close();
return nullptr;
465 tmp_nodes = t_pTag->toFloatMatrix().transpose();
468 tmp_node_normals = t_pTag->toFloatMatrix().transpose();
472 stream->close();
return nullptr;
474 tmp_triangles = t_pTag->toIntMatrix().transpose();
483 sigmaLocal = *t_pTag->toFloat();
488 s_ptr = std::make_unique<MNESurface>();
490 tmp_triangles.array() -= 1;
491 s->
itris = tmp_triangles;
493 s->
sigma = sigmaLocal;
496 if (tmp_node_normals.rows() > 0)
497 s->
nn = tmp_node_normals;
498 s->
ntri = ntri_count;
506 s->
cm[0] = s->
cm[1] = s->
cm[2] = 0.0;
509 if (check_too_many_neighbors) {
520 else if (s->
nn.rows() == 0) {
529 s->
inuse = Eigen::VectorXi::Ones(s->
np);
530 s->
vertno = Eigen::VectorXi::LinSpaced(s->
np, 0, s->
np - 1);
FiffCoordTrans class declaration.
#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
FiffStream class declaration.
#define FIFF_MNE_COORD_FRAME
#define FIFFV_MNE_SPACE_SURFACE
MNESourceSpace class declaration.
MNEProjData class declaration.
MNETriangle class declaration.
MNESurface class declaration.
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
QSharedPointer< FiffDirNode > SPtr
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.