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) {
85 r1.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i, 0));
86 r12.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i, 1)) - r1.row(i);
87 r13.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
itris(i, 2)) - r1.row(i);
88 a(i) = r12.row(i) * r12.row(i).transpose();
89 b(i) = r13.row(i) * r13.row(i).transpose();
90 c(i) = r12.row(i) * r13.row(i).transpose();
93 if (!(p_MNEBemSurf.
tri_nn.isZero(0))) {
94 nn = p_MNEBemSurf.tri_nn.cast<float>();
96 for (int i = 0; i < p_MNEBemSurf.ntri; ++i) {
97 nn.row(i) = r12.row(i).transpose().cross(r13.row(i).transpose()).transpose();
100 det = (a.array() * b.array() - c.array() * c.array()).matrix();
106 VectorXi& nearest, VectorXf& dist)
113 if (this->r1.isZero(0)) {
114 qDebug() <<
"No surface loaded to make the projection./n";
120 for (
int k = 0; k < np; ++k) {
125 if (!this->project_to_surface(r.row(k).transpose(), rTriK, bestTri, bestDist)) {
126 qDebug() <<
"The projection of point number " << k <<
" didn't work./n";
129 rTri.row(k) = rTriK.transpose();
130 nearest[k] = bestTri;
138bool MNEProjectToSurface::project_to_surface(
const Vector3f& r, Vector3f& rTri,
int& bestTri,
float& bestDist)
140 float p = 0, q = 0, p0 = 0, q0 = 0, dist0 = 0;
143 for (
int tri = 0; tri < a.size(); ++tri) {
144 if (!this->nearest_triangle_point(r, tri, p0, q0, dist0)) {
145 qDebug() <<
"The projection on triangle " << tri <<
" didn't work./n";
149 if ((bestTri < 0) || (std::fabs(dist0) < std::fabs(bestDist))) {
158 if (!this->project_to_triangle(rTri, p, q, bestTri)) {
159 qDebug() <<
"The coordinate transform to cartesian system didn't work./n";
165 qDebug() <<
"No best Triangle found./n";
171bool MNEProjectToSurface::nearest_triangle_point(
const Vector3f& r,
const int tri,
float& p,
float& q,
float& dist)
174 Vector3f rr = r - this->r1.row(tri).transpose();
175 float v1 = this->r12.row(tri) * rr;
176 float v2 = this->r13.row(tri) * rr;
179 dist = this->nn.row(tri) * rr;
180 p = (this->b(tri) * v1 - this->c(tri) * v2) / det(tri);
181 q = (this->a(tri) * v2 - this->c(tri) * v1) / det(tri);
184 if (p >= 0.0 && p <= 1.0 && q >= 0.0 && q <= 1.0 && (p + q) <= 1.0) {
193 float p0, q0, t0, dist0, best, bestp, bestq;
198 p0 = p + (q * this->c(tri)) / this->a(tri);
202 }
else if (p0 > 1.0) {
207 dist0 = sqrt((p - p0) * (p - p0) * this->a(tri) +
208 (q - q0) * (q - q0) * this->b(tri) +
209 2 * (p - p0) * (q - q0) * this->c(tri) +
218 t0 = ((a(tri) - c(tri)) * (-p) + (b(tri) - c(tri)) * q) / (a(tri) + b(tri) - 2 * c(tri));
222 }
else if (t0 > 1.0) {
228 dist0 = sqrt((p - p0) * (p - p0) * this->a(tri) +
229 (q - q0) * (q - q0) * this->b(tri) +
230 2 * (p - p0) * (q - q0) * this->c(tri) +
241 q0 = q + (p * c(tri)) / b(tri);
246 }
else if (q0 > 1.0) {
250 dist0 = sqrt((p - p0) * (p - p0) * this->a(tri) +
251 (q - q0) * (q - q0) * this->b(tri) +
252 2 * (p - p0) * (q - q0) * this->c(tri) +
267bool MNEProjectToSurface::project_to_triangle(Vector3f& rTri,
const float p,
const float q,
const int tri)
269 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...