65#include <QCoreApplication>
66#include <QtConcurrent>
68#define _USE_MATH_DEFINES
72#include <Eigen/Sparse>
77#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
113#define VEC_DOT_17(x,y) ((x)[X_17]*(y)[X_17] + (x)[Y_17]*(y)[Y_17] + (x)[Z_17]*(y)[Z_17])
115#define VEC_LEN_17(x) sqrt(VEC_DOT_17(x,x))
117#define VEC_DIFF_17(from,to,diff) {\
118 (diff)[X_17] = (to)[X_17] - (from)[X_17];\
119 (diff)[Y_17] = (to)[Y_17] - (from)[Y_17];\
120 (diff)[Z_17] = (to)[Z_17] - (from)[Z_17];\
123#define VEC_COPY_17(to,from) {\
124 (to)[X_17] = (from)[X_17];\
125 (to)[Y_17] = (from)[Y_17];\
126 (to)[Z_17] = (from)[Z_17];\
129#define CROSS_PRODUCT_17(x,y,xy) {\
130 (xy)[X_17] = (x)[Y_17]*(y)[Z_17]-(y)[Y_17]*(x)[Z_17];\
131 (xy)[Y_17] = -((x)[X_17]*(y)[Z_17]-(y)[X_17]*(x)[Z_17]);\
132 (xy)[Z_17] = (x)[X_17]*(y)[Y_17]-(y)[X_17]*(x)[Y_17];\
135#define MALLOC_17(x,t) (t *)malloc((x)*sizeof(t))
137#define REALLOC_17(x,y,t) (t *)((x == NULL) ? malloc((y)*sizeof(t)) : realloc((x),(y)*sizeof(t)))
139#define ALLOC_INT_17(x) MALLOC_17(x,int)
141#define ALLOC_CMATRIX_17(x,y) mne_cmatrix_17((x),(y))
143static void matrix_error_17(
int kind,
int nr,
int nc)
147 printf(
"Failed to allocate memory pointers for a %d x %d matrix\n",nr,nc);
149 printf(
"Failed to allocate memory for a %d x %d matrix\n",nr,nc);
151 printf(
"Allocation error for a %d x %d matrix\n",nr,nc);
152 if (
sizeof(
void *) == 4) {
153 printf(
"This is probably because you seem to be using a computer with 32-bit architecture.\n");
154 printf(
"Please consider moving to a 64-bit platform.");
156 printf(
"Cannot continue. Sorry.\n");
168 matrix_error_17( 1, numPoints, numDim);
171 whole =
MALLOC_17( numPoints * numDim,
float);
174 matrix_error_17(2, numPoints, numDim);
177 for(
int i = 0; i < numPoints; ++i)
179 m[i] = &whole[ i * numDim ];
185#define FREE_17(x) if ((char *)(x) != NULL) free((char *)(x))
187#define FREE_CMATRIX_17(m) mne_free_cmatrix_17((m))
189#define FREE_ICMATRIX_17(m) mne_free_icmatrix_17((m))
210#define CURVATURE_FILE_MAGIC_NUMBER (16777215)
212#define TAG_MGH_XFORM 31
213#define TAG_SURF_GEOM 21
214#define TAG_OLD_USEREALRAS 2
215#define TAG_COLORTABLE 5
216#define TAG_OLD_MGH_XFORM 30
217#define TAG_OLD_COLORTABLE 1
219#define TAG_USEREALRAS 4
221#define ALLOC_ICMATRIX_17(x,y) mne_imatrix_17((x),(y))
230 if (!m) matrix_error_17(1,nr,nc);
232 if (!whole) matrix_error_17(2,nr,nc);
241 Eigen::MatrixXf eigen_mat(m,n);
243 for (
int i = 0; i < m; ++i)
244 for (
int j = 0; j < n; ++j)
245 eigen_mat(i,j) = mat[i][j];
252 for (
int i = 0; i < m; ++i)
253 for (
int j = 0; j < n; ++j)
254 to_mat[i][j] = from_mat(i,j);
264 for (
int i = 0; i < m; ++i)
265 for (
int j = 0; j < n; ++j)
266 to_mat[i][j] = from_mat(i,j);
276static std::optional<FiffCoordTrans> make_voxel_ras_trans(
float *r0,
283 float rot[3][3],move[3];
288 for (j = 0; j < 3; j++) {
289 rot[j][0] = x_ras[j];
290 rot[j][1] = y_ras[j];
291 rot[j][2] = z_ras[j];
294 for (j = 0; j < 3; j++)
295 for (k = 0; k < 3; k++)
296 rot[j][k] = voxel_size[k]*rot[j][k];
299 Eigen::Map<Eigen::Matrix3f>(&rot[0][0]),
300 Eigen::Map<Eigen::Vector3f>(move));
317 curv = Eigen::VectorXf::Ones(
np);
345 Eigen::VectorXi result(
static_cast<int>(
nearest.size()));
346 for (
int i = 0; i < result.size(); ++i)
355 Eigen::VectorXd result(
static_cast<int>(
nearest.size()));
356 for (
int i = 0; i < result.size(); ++i)
365 const int n = nearestIdx.size();
367 for (
int i = 0; i < n; ++i) {
369 nearest[i].nearest = nearestIdx[i];
370 nearest[i].dist =
static_cast<float>(nearestDist[i]);
383 double v1[3],v2[3],v3[3];
384 double l1,l2,l3,s,triple;
399 return (2.0*atan2(triple,s));
423 for (k = 0, tri =
tris.data(); k <
ntri; k++, tri++) {
431#ifdef TRIANGLE_SIZE_WARNING
432 for (k = 0, tri =
tris.data(); k <
ntri; k++, tri++)
434 printf(
"Warning: Triangle area is only %g um^2 (%.5f %% of expected average)\n",
439 printf(
"\ttotal area = %-.1f cm^2\n",1e4*
tot_area);
465 cm[0] =
cm[1] =
cm[2] = 0.0;
466 for (q = 0; q <
np; q++) {
503 printf(
"\tDistances between neighboring vertices...");
504 for (k = 0, ndist = 0; k <
np; k++) {
508 for (p = 0; p < nneigh; p++) {
518 printf(
"[%d distances done]\n",ndist);
536 nn = MNESurfaceOrVolume::NormalsT::Zero(
np,3);
538 for (k = 0; k <
np; k++) {
545 for (p = 0, tri =
tris.data(); p <
ntri; p++, tri++) {
551 for (k = 0; k < 3; k++)
552 for (c = 0; c < 3; c++)
553 nn(ii[k],c) += w*tri->
nn[c];
555 for (k = 0; k <
np; k++) {
558 for (c = 0; c < 3; c++)
559 nn(k,c) =
nn(k,c)/size;
578 int nfix_distinct,nfix_no_neighbors,nfix_defect;
591 nn = MNESurfaceOrVolume::NormalsT::Zero(
np,3);
597 for (k = 0; k <
np; k++) {
605 for (p = 0, tri =
tris.data(); p <
ntri; p++, tri++)
607 printf(
"\tWarning : zero size triangle # %d\n",p);
608 printf(
"\tTriangle ");
610 printf(
"and vertex ");
611 printf(
"normals and neighboring triangles...");
612 for (p = 0, tri =
tris.data(); p <
ntri; p++, tri++) {
615 for (k = 0; k < 3; k++) {
620 for (c = 0; c < 3; c++)
621 nn(ii[k],c) += w*tri->
nn[c];
630 nfix_no_neighbors = 0;
632 for (k = 0; k <
np; k++) {
635 err_printf_set_error(
"Vertex %d does not have any neighboring triangles!",k);
638#ifdef REPORT_WARNINGS
639 printf(
"Warning: Vertex %d does not have any neighboring triangles!\n",k);
645#ifdef REPORT_WARNINGS
646 printf(
"\n\tTopological defect: Vertex %d has only %d neighboring triangle%s Vertex omitted.\n\t",
657 for (k = 0; k <
np; k++)
661 for (c = 0; c < 3; c++)
662 nn(k,c) =
nn(k,c)/size;
668 printf(
"\tVertex neighbors...");
675 for (k = 0; k <
np; k++) {
685 for (k = 0; k <
np; k++) {
692 for (c = 0; c < 3; c++) {
695 for (q = 0, found =
FALSE; q < nneighbors; q++) {
696 if (neighbors[q] == vert) {
703 neighbors[nneighbors++] = vert;
705 if (check_too_many_neighbors) {
706 printf(
"Too many neighbors for vertex %d.",k);
710 printf(
"\tWarning: Too many neighbors for vertex %d\n",k);
717#ifdef REPORT_WARNINGS
718 printf(
"\n\tIncorrect number of distinct neighbors for vertex %d (%d instead of %d) [fixed].",
735 printf(
"\tWarning: %d topological defects were fixed.\n",nfix_defect);
736 if (nfix_distinct > 0)
737 printf(
"\tWarning: %d vertices had incorrect number of distinct neighbors (fixed).\n",nfix_distinct);
738 if (nfix_no_neighbors > 0)
739 printf(
"\tWarning: %d vertices did not have any neighboring triangles (fixed)\n",nfix_no_neighbors);
741 for (k = 0; k <
np; k++) {
743 printf(
"No neighbors for vertex %d\n",k);
745 printf(
"No neighbor tris for vertex %d\n",k);
#define FIFFV_MNE_COORD_MRI_VOXEL
FiffStream class declaration.
FiffDigitizerData class declaration.
FiffCoordTrans class declaration.
FiffDigPoint class declaration.
IOUtils class declaration.
Sphere class declaration.
MNETriangle class declaration.
#define VEC_COPY_17(to, from)
void fromIntEigenMatrix_17(const Eigen::MatrixXi &from_mat, int **&to_mat, const int m, const int n)
float ** mne_cmatrix_17(int numPoints, int numDim)
int ** mne_imatrix_17(int nr, int nc)
void mne_free_cmatrix_17(float **m)
void fromFloatEigenMatrix_17(const Eigen::MatrixXf &from_mat, float **&to_mat, const int m, const int n)
Eigen::MatrixXf toFloatEigenMatrix_17(float **mat, const int m, const int n)
void mne_free_icmatrix_17(int **m)
MNEMghTagGroup class declaration.
MNEVolGeom class declaration.
Filter Thread Argument (FilterThreadArg) class declaration.
MNE Patch Information (MNEPatchInfo) class declaration.
#define MNE_SOURCE_SPACE_SURFACE
#define MNE_SOURCE_SPACE_VOLUME
MNESurface class declaration.
#define CROSS_PRODUCT_17(x, y, xy)
#define VEC_DIFF_17(from, to, diff)
MNEMghTag class declaration.
MNEMshDisplaySurface class declaration.
MNEProjData class declaration.
MNENearest class declaration.
MNESourceSpace class declaration.
MNE Surface or Volume (MNESurfaceOrVolume) class declaration.
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
Coordinate transformation description.
std::vector< Eigen::VectorXi > neighbor_tri
void setNearestData(const Eigen::VectorXi &nearestIdx, const Eigen::VectorXd &nearestDist)
int add_geometry_info(int do_normals, int check_too_many_neighbors)
std::vector< Eigen::VectorXi > neighbor_vert
MNESurfaceOrVolume()
Constructs the MNE Surface or Volume.
Eigen::VectorXd nearestDistVec() const
FIFFLIB::FiffSparseMatrix dist
std::vector< MNENearest > nearest
Eigen::VectorXi nneighbor_vert
Eigen::VectorXi nneighbor_tri
void calculate_vertex_distances()
static void compute_cm(const PointsT &rr, int np, float(&cm)[3])
static double solid_angle(const Eigen::Vector3f &from, const MNELIB::MNETriangle &tri)
void compute_surface_cm()
virtual ~MNESurfaceOrVolume()
Destroys the MNE Surface or Volume description.
std::vector< MNETriangle > tris
std::vector< Eigen::VectorXf > vert_dist
Eigen::Matrix< float, Eigen::Dynamic, 3, Eigen::RowMajor > PointsT
Eigen::VectorXi nearestVertIdx() const
int add_geometry_info2(int do_normals)
std::vector< MNETriangle > use_tris
Per-triangle geometric data for a cortical or BEM surface.