v2.0.0
Loading...
Searching...
No Matches
mri_vol_data.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
39#include "mri_vol_data.h"
40
41//=============================================================================================================
42// USED NAMESPACES
43//=============================================================================================================
44
45using namespace MRILIB;
46using namespace Eigen;
47
48//=============================================================================================================
49// DEFINE MEMBER METHODS
50//=============================================================================================================
51
53: version(0)
54, width(0)
55, height(0)
56, depth(0)
57, nframes(0)
59, dof(0)
60, rasGood(false)
61, xsize(1.0f)
62, ysize(1.0f)
63, zsize(1.0f)
64, TR(0.0f)
65, flipAngle(0.0f)
66, TE(0.0f)
67, TI(0.0f)
68, FoV(0.0f)
69{
70 // Default direction cosines (FreeSurfer defaults when goodRASflag is false)
71 // See: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat
72 x_ras[0] = -1.0f; x_ras[1] = 0.0f; x_ras[2] = 0.0f; // xr=-1, xa=0, xs=0
73 y_ras[0] = 0.0f; y_ras[1] = 0.0f; y_ras[2] = -1.0f; // yr=0, ya=0, ys=-1
74 z_ras[0] = 0.0f; z_ras[1] = 1.0f; z_ras[2] = 0.0f; // zr=0, za=1, zs=0
75 c_ras[0] = 0.0f; c_ras[1] = 0.0f; c_ras[2] = 0.0f;
76}
77
78//=============================================================================================================
79
81{
82 return (version == MRI_MGH_VERSION && width > 0 && height > 0 && depth > 0);
83}
84
85//=============================================================================================================
86
88{
89 //
90 // Build voxel-to-surface-RAS transform (FreeSurfer convention).
91 //
92 // The direction cosine matrix Mdc stores the orientation of each voxel axis
93 // (columns are x, y, z directions). Scaling by voxel size gives the actual
94 // spacing matrix M:
95 //
96 // M = Mdc * diag(xsize, ysize, zsize)
97 //
98 // The origin P0 in RAS coordinates is:
99 //
100 // P0 = c_ras - M * (dim/2)
101 //
102 // The full 4x4 vox2ras matrix is:
103 //
104 // | M P0 | (in mm, then converted to meters for FIFF)
105 // | 0 1 |
106 //
107 // Reference: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat
108 //
109
110 // Construct M = Mdc * D (scale direction cosines by voxel sizes)
111 Matrix3f M;
112 // Column 0 (x voxel direction)
113 M(0, 0) = x_ras[0] * xsize;
114 M(1, 0) = x_ras[1] * xsize;
115 M(2, 0) = x_ras[2] * xsize;
116 // Column 1 (y voxel direction)
117 M(0, 1) = y_ras[0] * ysize;
118 M(1, 1) = y_ras[1] * ysize;
119 M(2, 1) = y_ras[2] * ysize;
120 // Column 2 (z voxel direction)
121 M(0, 2) = z_ras[0] * zsize;
122 M(1, 2) = z_ras[1] * zsize;
123 M(2, 2) = z_ras[2] * zsize;
124
125 // Compute center voxel
126 Vector3f center;
127 center(0) = static_cast<float>(width) / 2.0f;
128 center(1) = static_cast<float>(height) / 2.0f;
129 center(2) = static_cast<float>(depth) / 2.0f;
130
131 // Compute P0 = c_ras - M * center
132 Vector3f P0;
133 P0(0) = c_ras[0] - (M(0, 0) * center(0) + M(0, 1) * center(1) + M(0, 2) * center(2));
134 P0(1) = c_ras[1] - (M(1, 0) * center(0) + M(1, 1) * center(1) + M(1, 2) * center(2));
135 P0(2) = c_ras[2] - (M(2, 0) * center(0) + M(2, 1) * center(1) + M(2, 2) * center(2));
136
137 // Build 4x4 matrix in meters (FreeSurfer uses mm, FIFF uses meters)
138 Matrix4f vox2ras = Matrix4f::Identity();
139 vox2ras.block<3, 3>(0, 0) = M / 1000.0f;
140 vox2ras(0, 3) = P0(0) / 1000.0f;
141 vox2ras(1, 3) = P0(1) / 1000.0f;
142 vox2ras(2, 3) = P0(2) / 1000.0f;
143
144 return vox2ras;
145}
MriVolData class declaration.
MRI volume and coordinate-system I/O (volumes, voxel geometry, transforms).
constexpr int MRI_MGH_VERSION
Definition mri_types.h:57
constexpr int MRI_UCHAR
Definition mri_types.h:75
bool isValid() const
Eigen::Matrix4f computeVox2Ras() const