56#include <Eigen/Geometry>
74: r1(MatrixX3f::Zero(1,3))
75, r12(MatrixX3f::Zero(1,3))
76, r13(MatrixX3f::Zero(1,3))
77, nn(MatrixX3f::Zero(1,3))
81, det(VectorXf::Zero(1))
88: r1(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
89, r12(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
90, r13(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
91, nn(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
92, a(VectorXf::Zero(p_MNEBemSurf.ntri))
93, b(VectorXf::Zero(p_MNEBemSurf.ntri))
94, c(VectorXf::Zero(p_MNEBemSurf.ntri))
95, det(VectorXf::Zero(p_MNEBemSurf.ntri))
97 for (
int i = 0; i < p_MNEBemSurf.
ntri; ++i)
99 r1.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i,0));
100 r12.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i,1)) - r1.row(i);
101 r13.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i,2)) - r1.row(i);
102 a(i) = r12.row(i) * r12.row(i).transpose();
103 b(i) = r13.row(i) * r13.row(i).transpose();
104 c(i) = r12.row(i) * r13.row(i).transpose();
107 if (!(p_MNEBemSurf.
tri_nn.isZero(0)))
109 nn = p_MNEBemSurf.tri_nn.cast<float>();
113 for (int i = 0; i < p_MNEBemSurf.ntri; ++i)
115 nn.row(i) = r12.row(i).transpose().cross(r13.row(i).transpose()).transpose();
118 det = (a.array()*b.array() - c.array()*c.array()).matrix();
124 VectorXi &nearest, VectorXf &dist)
131 if (this->r1.isZero(0))
133 qDebug() <<
"No surface loaded to make the projection./n";
139 for (
int k = 0; k < np; ++k)
145 if (!this->project_to_surface(r.row(k).transpose(), rTriK, bestTri, bestDist))
147 qDebug() <<
"The projection of point number " << k <<
" didn't work./n";
150 rTri.row(k) = rTriK.transpose();
151 nearest[k] = bestTri;
159bool MNEProjectToSurface::project_to_surface(
const Vector3f &r, Vector3f &rTri,
int &bestTri,
float &bestDist)
161 float p = 0, q = 0, p0 = 0, q0 = 0, dist0 = 0;
164 for (
int tri = 0; tri < a .size(); ++tri)
166 if (!this->nearest_triangle_point(r, tri, p0, q0, dist0))
168 qDebug() <<
"The projection on triangle " << tri <<
" didn't work./n";
172 if ((bestTri < 0) || (std::fabs(dist0) < std::fabs(bestDist)))
183 if (!this->project_to_triangle(rTri, p, q, bestTri))
185 qDebug() <<
"The coordinate transform to cartesian system didn't work./n";
191 qDebug() <<
"No best Triangle found./n";
197bool MNEProjectToSurface::nearest_triangle_point(
const Vector3f &r,
const int tri,
float &p,
float &q,
float &dist)
200 Vector3f rr = r - this->r1.row(tri).transpose();
201 float v1 = this->r12.row(tri)*rr;
202 float v2 = this->r13.row(tri)*rr;
205 dist = this->nn.row(tri)*rr;
206 p = (this->b(tri)*v1 - this->c(tri)*v2)/det(tri);
207 q = (this->a(tri)*v2 - this->c(tri)*v1)/det(tri);
210 if (p >= 0.0 && p <= 1.0 && q >= 0.0 && q <= 1.0 && (p+q) <= 1.0)
220 float p0, q0, t0, dist0, best, bestp, bestq;
225 p0 = p + (q * this->c(tri)) / this->a(tri);
237 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
238 (q-q0)*(q-q0)*this->b(tri) +
239 2*(p-p0)*(q-q0)*this->c(tri) +
248 t0 = ((a(tri)-c(tri))*(-p) + (b(tri)-c(tri))*q)/(a(tri)+b(tri)-2*c(tri));
261 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
262 (q-q0)*(q-q0)*this->b(tri) +
263 2*(p-p0)*(q-q0)*this->c(tri) +
275 q0 = q + (p * c(tri))/b(tri);
287 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
288 (q-q0)*(q-q0)*this->b(tri) +
289 2*(p-p0)*(q-q0)*this->c(tri) +
305bool MNEProjectToSurface::project_to_triangle(Vector3f &rTri,
const float p,
const float q,
const int tri)
307 rTri = this->r1.row(tri) + p*this->r12.row(tri) + q*this->r13.row(tri);
MNEBemSurface class declaration.
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)
find_closest_on_surface