57 #include <Eigen/Geometry>
63 using namespace MNELIB;
64 using namespace Eigen;
75 : r1(MatrixX3f::Zero(1,3))
76 , r12(MatrixX3f::Zero(1,3))
77 , r13(MatrixX3f::Zero(1,3))
78 , nn(MatrixX3f::Zero(1,3))
79 , a(VectorXf::Zero(1))
80 , b(VectorXf::Zero(1))
81 , c(VectorXf::Zero(1))
82 , det(VectorXf::Zero(1))
89 : r1(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
90 , r12(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
91 , r13(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
92 , nn(MatrixX3f::Zero(p_MNEBemSurf.ntri,3))
93 , a(VectorXf::Zero(p_MNEBemSurf.ntri))
94 , b(VectorXf::Zero(p_MNEBemSurf.ntri))
95 , c(VectorXf::Zero(p_MNEBemSurf.ntri))
96 , det(VectorXf::Zero(p_MNEBemSurf.ntri))
98 for (
int i = 0; i < p_MNEBemSurf.
ntri; ++i)
100 r1.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
tris(i,0));
101 r12.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
tris(i,1)) - r1.row(i);
102 r13.row(i) = p_MNEBemSurf.
rr.row(p_MNEBemSurf.
tris(i,2)) - r1.row(i);
103 a(i) = r12.row(i) * r12.row(i).transpose();
104 b(i) = r13.row(i) * r13.row(i).transpose();
105 c(i) = r12.row(i) * r13.row(i).transpose();
108 if (!(p_MNEBemSurf.
tri_nn.isZero(0)))
110 nn = p_MNEBemSurf.
tri_nn.cast<
float>();
114 for (
int i = 0; i < p_MNEBemSurf.
ntri; ++i)
116 nn.row(i) = r12.row(i).transpose().cross(r13.row(i).transpose()).transpose();
119 det = (a.array()*b.array() - c.array()*c.array()).matrix();
125 : r1(MatrixX3f::Zero(p_MNESurf.ntri,3))
126 , r12(MatrixX3f::Zero(p_MNESurf.ntri,3))
127 , r13(MatrixX3f::Zero(p_MNESurf.ntri,3))
128 , nn(MatrixX3f::Zero(p_MNESurf.ntri,3))
129 , a(VectorXf::Zero(p_MNESurf.ntri))
130 , b(VectorXf::Zero(p_MNESurf.ntri))
131 , c(VectorXf::Zero(p_MNESurf.ntri))
132 , det(VectorXf::Zero(p_MNESurf.ntri))
134 for (
int i = 0; i < p_MNESurf.
ntri; ++i)
136 r1.row(i) = p_MNESurf.
rr.row(p_MNESurf.
tris(i,0));
137 r12.row(i) = p_MNESurf.
rr.row(p_MNESurf.
tris(i,1)) - r1.row(i);
138 r13.row(i) = p_MNESurf.
rr.row(p_MNESurf.
tris(i,2)) - r1.row(i);
139 nn.row(i) = r12.row(i).transpose().cross(r13.row(i).transpose()).transpose();
140 a(i) = r12.row(i) * r12.row(i).transpose();
141 b(i) = r13.row(i) * r13.row(i).transpose();
142 c(i) = r12.row(i) * r13.row(i).transpose();
145 det = (a.array()*b.array() - c.array()*c.array()).matrix();
151 VectorXi &nearest, VectorXf &dist)
158 if (this->r1.isZero(0))
160 qDebug() <<
"No surface loaded to make the projection./n";
166 for (
int k = 0;
k < np; ++
k)
172 if (!this->mne_project_to_surface(r.row(
k).transpose(), rTriK, bestTri, bestDist))
174 qDebug() <<
"The projection of point number " <<
k <<
" didn't work./n";
177 rTri.row(
k) = rTriK.transpose();
178 nearest[
k] = bestTri;
186 bool MNEProjectToSurface::mne_project_to_surface(
const Vector3f &r, Vector3f &rTri,
int &bestTri,
float &bestDist)
188 float p = 0, q = 0, p0 = 0, q0 = 0, dist0 = 0;
191 for (
int tri = 0; tri < a .size(); ++tri)
193 if (!this->nearest_triangle_point(r, tri, p0, q0, dist0))
195 qDebug() <<
"The projection on triangle " << tri <<
" didn't work./n";
199 if ((bestTri < 0) || (std::fabs(dist0) < std::fabs(bestDist)))
210 if (!this->project_to_triangle(rTri, p, q, bestTri))
212 qDebug() <<
"The coordinate transform to cartesian system didn't work./n";
218 qDebug() <<
"No best Triangle found./n";
224 bool MNEProjectToSurface::nearest_triangle_point(
const Vector3f &r,
const int tri,
float &p,
float &q,
float &dist)
227 Vector3f rr = r - this->r1.row(tri).transpose();
228 float v1 = this->r12.row(tri)*rr;
229 float v2 = this->r13.row(tri)*rr;
232 dist = this->nn.row(tri)*rr;
233 p = (this->b(tri)*v1 - this->c(tri)*v2)/det(tri);
234 q = (this->a(tri)*v2 - this->c(tri)*v1)/det(tri);
237 if (p >= 0.0 && p <= 1.0 && q >= 0.0 && q <= 1.0 && (p+q) <= 1.0)
247 float p0, q0, t0, dist0, best, bestp, bestq;
252 p0 = p + (q * this->c(tri)) / this->a(tri);
264 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
265 (q-q0)*(q-q0)*this->b(tri) +
266 2*(p-p0)*(q-q0)*this->c(tri) +
275 t0 = ((a(tri)-c(tri))*(-p) + (b(tri)-c(tri))*q)/(a(tri)+b(tri)-2*c(tri));
288 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
289 (q-q0)*(q-q0)*this->b(tri) +
290 2*(p-p0)*(q-q0)*this->c(tri) +
302 q0 = q + (p * c(tri))/b(tri);
314 dist0 = sqrt((p-p0)*(p-p0)*this->a(tri) +
315 (q-q0)*(q-q0)*this->b(tri) +
316 2*(p-p0)*(q-q0)*this->c(tri) +
332 bool MNEProjectToSurface::project_to_triangle(Vector3f &rTri,
const float p,
const float q,
const int tri)
334 rTri = this->r1.row(tri) + p*this->r12.row(tri) + q*this->r13.row(tri);