43#include <Eigen/Geometry>
61: r1(MatrixX3f::Zero(1,3))
62, r12(MatrixX3f::Zero(1,3))
63, r13(MatrixX3f::Zero(1,3))
64, nn(MatrixX3f::Zero(1,3))
68, det(VectorXf::Zero(1))
75: r1(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
76, r12(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
77, r13(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
78, nn(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
79, a(VectorXf::Zero(p_MNEBemSurf.ntri))
80, b(VectorXf::Zero(p_MNEBemSurf.ntri))
81, c(VectorXf::Zero(p_MNEBemSurf.ntri))
82, det(VectorXf::Zero(p_MNEBemSurf.ntri))
84 for (
int i = 0; i < p_MNEBemSurf.
ntri; ++i)
86 r1.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i,0));
87 r12.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i,1)) - r1.row(i);
88 r13.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i,2)) - r1.row(i);
89 a(i) = r12.row(i) * r12.row(i).transpose();
90 b(i) = r13.row(i) * r13.row(i).transpose();
91 c(i) = r12.row(i) * r13.row(i).transpose();
94 if (!(p_MNEBemSurf.
tri_nn.isZero(0)))
96 nn = p_MNEBemSurf.tri_nn.cast<float>();
100 for (int i = 0; i < p_MNEBemSurf.ntri; ++i)
102 nn.row(i) = r12.row(i).transpose().cross(r13.row(i).transpose()).transpose();
105 det = (a.array()*b.array() - c.array()*c.array()).matrix();
111 VectorXi &nearest, VectorXf &dist)
118 if (this->r1.isZero(0))
120 qDebug() <<
"No surface loaded to make the projection./n";
126 for (
int k = 0; k < np; ++k)
132 if (!this->project_to_surface(r.row(k).transpose(), rTriK, bestTri, bestDist))
134 qDebug() <<
"The projection of point number " << k <<
" didn't work./n";
137 rTri.row(k) = rTriK.transpose();
138 nearest[k] = bestTri;
146bool MNEProjectToSurface::project_to_surface(
const Vector3f &r, Vector3f &rTri,
int &bestTri,
float &bestDist)
148 float p = 0, q = 0, p0 = 0, q0 = 0, dist0 = 0;
151 for (
int tri = 0; tri < a .size(); ++tri)
153 if (!this->nearest_triangle_point(r, tri, p0, q0, dist0))
155 qDebug() <<
"The projection on triangle " << tri <<
" didn't work./n";
159 if ((bestTri < 0) || (std::fabs(dist0) < std::fabs(bestDist)))
170 if (!this->project_to_triangle(rTri, p, q, bestTri))
172 qDebug() <<
"The coordinate transform to cartesian system didn't work./n";
178 qDebug() <<
"No best Triangle found./n";
184bool MNEProjectToSurface::nearest_triangle_point(
const Vector3f &r,
const int tri,
float &p,
float &q,
float &dist)
187 Vector3f rr = r - this->r1.row(tri).transpose();
188 float v1 = this->r12.row(tri)*rr;
189 float v2 = this->r13.row(tri)*rr;
192 dist = this->nn.row(tri)*rr;
193 p = (this->b(tri)*v1 - this->c(tri)*v2)/det(tri);
194 q = (this->a(tri)*v2 - this->c(tri)*v1)/det(tri);
197 if (p >= 0.0 && p <= 1.0 && q >= 0.0 && q <= 1.0 && (p+q) <= 1.0)
207 float p0, q0, t0, dist0, best, bestp, bestq;
212 p0 = p + (q * this->c(tri)) / this->a(tri);
224 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
225 (q-q0)*(q-q0)*this->b(tri) +
226 2*(p-p0)*(q-q0)*this->c(tri) +
235 t0 = ((a(tri)-c(tri))*(-p) + (b(tri)-c(tri))*q)/(a(tri)+b(tri)-2*c(tri));
248 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
249 (q-q0)*(q-q0)*this->b(tri) +
250 2*(p-p0)*(q-q0)*this->c(tri) +
262 q0 = q + (p * c(tri))/b(tri);
274 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
275 (q-q0)*(q-q0)*this->b(tri) +
276 2*(p-p0)*(q-q0)*this->c(tri) +
292bool MNEProjectToSurface::project_to_triangle(Vector3f &rTri,
const float p,
const float q,
const int tri)
294 rTri = (this->r1.row(tri) + p*this->r12.row(tri) + q*this->r13.row(tri)).transpose();
Single closed BEM surface (triangulation, normals, conductivity).
Geometric projection of a 3D point onto the closest cortex triangle.
Core MNE data structures (source spaces, source estimates, hemispheres).
BEM surface provides geometry information.
bool find_closest_on_surface(const Eigen::MatrixXf &r, const int np, Eigen::MatrixXf &rTri, Eigen::VectorXi &nearest, Eigen::VectorXf &dist)
Project a set of points onto the surface and return the nearest triangle and signed distance per poin...