Skip to content

Commit

Permalink
STYLE: address variable nameing conventions comments
Browse files Browse the repository at this point in the history
  • Loading branch information
blowekamp committed Dec 18, 2024
1 parent 6bf2ffe commit d051e5f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 76 deletions.
66 changes: 32 additions & 34 deletions Modules/IO/MINC/src/itkMINCImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -449,20 +449,20 @@ MINCImageIO::ReadImageInformation()

this->SetNumberOfDimensions(spatial_dimension_count);

int numberOfComponents = 1;
int usable_dimensions = 0;
int numberOfComponents = 1;
unsigned int usableDimensions = 0;

auto dir_cos = Matrix<double, 3, 3>::GetIdentity();

// Conversion matrix for MINC PositiveCoordinateOrientation RAS (RightToLeft,AnteriorToPosterior,SuperiorToInferior)
// to ITK PositiveCoordinateOrientation LPS (LeftToRight,PosteriorToInferior,SuperiortoInferior)
auto RAS_tofrom_LPS = Matrix<double, 3, 3>::GetIdentity();
RAS_tofrom_LPS(0, 0) = -1.0;
RAS_tofrom_LPS(1, 1) = -1.0;
// Conversion matrix for MINC PositiveCoordinateOrientation RAS (LeftToRight, PosteriorToAnterior, InferiorToSuperior)
// to ITK PositiveCoordinateOrientation LPS (RightToLeft, AnteriorToPosterior, InferiorToSuperior)
auto RAStofromLPS = Matrix<double, 3, 3>::GetIdentity();
RAStofromLPS(0, 0) = -1.0;
RAStofromLPS(1, 1) = -1.0;
std::vector<double> dir_cos_temp(3);

Vector<double, 3> origin{};
Vector<double, 3> o_origin{};
Vector<double, 3> oOrigin{};

// minc api uses inverse order of dimensions , fastest varying are last
Vector<double, 3> sep;
Expand All @@ -472,19 +472,19 @@ MINCImageIO::ReadImageInformation()
{
// MINC2: bad design!
// micopy_dimension(hdim[m_MINCPImpl->m_DimensionIndices[i]],&apparent_dimension_order[usable_dimensions]);
m_MINCPImpl->m_MincApparentDims[usable_dimensions] =
m_MINCPImpl->m_MincApparentDims[usableDimensions] =
m_MINCPImpl->m_MincFileDims[m_MINCPImpl->m_DimensionIndices[i]];
// always use positive
miset_dimension_apparent_voxel_order(m_MINCPImpl->m_MincApparentDims[usable_dimensions], MI_POSITIVE);
miset_dimension_apparent_voxel_order(m_MINCPImpl->m_MincApparentDims[usableDimensions], MI_POSITIVE);
misize_t _sz;
miget_dimension_size(m_MINCPImpl->m_MincApparentDims[usable_dimensions], &_sz);
miget_dimension_size(m_MINCPImpl->m_MincApparentDims[usableDimensions], &_sz);

double _sep;
miget_dimension_separation(m_MINCPImpl->m_MincApparentDims[usable_dimensions], MI_ORDER_APPARENT, &_sep);
miget_dimension_separation(m_MINCPImpl->m_MincApparentDims[usableDimensions], MI_ORDER_APPARENT, &_sep);
std::vector<double> _dir(3);
miget_dimension_cosines(m_MINCPImpl->m_MincApparentDims[usable_dimensions], &_dir[0]);
miget_dimension_cosines(m_MINCPImpl->m_MincApparentDims[usableDimensions], &_dir[0]);
double _start;
miget_dimension_start(m_MINCPImpl->m_MincApparentDims[usable_dimensions], MI_ORDER_APPARENT, &_start);
miget_dimension_start(m_MINCPImpl->m_MincApparentDims[usableDimensions], MI_ORDER_APPARENT, &_start);

for (int j = 0; j < 3; ++j)
{
Expand All @@ -497,22 +497,22 @@ MINCImageIO::ReadImageInformation()
this->SetDimensions(i - 1, static_cast<unsigned int>(_sz));
this->SetSpacing(i - 1, _sep);

++usable_dimensions;
++usableDimensions;
}
}


// Transform MINC PositiveCoordinateOrientation RAS coordinates to
// internal ITK PositiveCoordinateOrientation LPS Coordinates
if (this->m_RAStoLPS)
dir_cos = RAS_tofrom_LPS * dir_cos;
dir_cos = RAStofromLPS * dir_cos;

// Transform origin coordinates
o_origin = dir_cos * origin;
oOrigin = dir_cos * origin;

for (int i = 0; i < spatial_dimension_count; ++i)
{
this->SetOrigin(i, o_origin[i]);
this->SetOrigin(i, oOrigin[i]);
for (unsigned int j = 0; j < 3; j++)
{
dir_cos_temp[j] = dir_cos[j][i];
Expand All @@ -523,31 +523,29 @@ MINCImageIO::ReadImageInformation()
if (m_MINCPImpl->m_DimensionIndices[0] != -1) // have vector dimension
{
// micopy_dimension(m_MINCPImpl->m_MincFileDims[m_MINCPImpl->m_DimensionIndices[0]],&apparent_dimension_order[usable_dimensions]);
m_MINCPImpl->m_MincApparentDims[usable_dimensions] =
m_MINCPImpl->m_MincFileDims[m_MINCPImpl->m_DimensionIndices[0]];
m_MINCPImpl->m_MincApparentDims[usableDimensions] = m_MINCPImpl->m_MincFileDims[m_MINCPImpl->m_DimensionIndices[0]];
// always use positive, vector dimension does not supposed to have notion of positive step size, so leaving as is
// miset_dimension_apparent_voxel_order(m_MINCPImpl->m_MincApparentDims[usable_dimensions],MI_POSITIVE);
misize_t _sz;
miget_dimension_size(m_MINCPImpl->m_MincApparentDims[usable_dimensions], &_sz);
miget_dimension_size(m_MINCPImpl->m_MincApparentDims[usableDimensions], &_sz);
numberOfComponents = _sz;
++usable_dimensions;
++usableDimensions;
}

if (m_MINCPImpl->m_DimensionIndices[4] != -1) // have time dimension
{
// micopy_dimension(hdim[m_MINCPImpl->m_DimensionIndices[4]],&apparent_dimension_order[usable_dimensions]);
m_MINCPImpl->m_MincApparentDims[usable_dimensions] =
m_MINCPImpl->m_MincFileDims[m_MINCPImpl->m_DimensionIndices[4]];
m_MINCPImpl->m_MincApparentDims[usableDimensions] = m_MINCPImpl->m_MincFileDims[m_MINCPImpl->m_DimensionIndices[4]];
// always use positive
miset_dimension_apparent_voxel_order(m_MINCPImpl->m_MincApparentDims[usable_dimensions], MI_POSITIVE);
miset_dimension_apparent_voxel_order(m_MINCPImpl->m_MincApparentDims[usableDimensions], MI_POSITIVE);
misize_t _sz;
miget_dimension_size(m_MINCPImpl->m_MincApparentDims[usable_dimensions], &_sz);
miget_dimension_size(m_MINCPImpl->m_MincApparentDims[usableDimensions], &_sz);
numberOfComponents = _sz;
++usable_dimensions;
++usableDimensions;
}

// Set apparent dimension order to the MINC2 api
if (miset_apparent_dimension_order(m_MINCPImpl->m_Volume, usable_dimensions, m_MINCPImpl->m_MincApparentDims) < 0)
if (miset_apparent_dimension_order(m_MINCPImpl->m_Volume, usableDimensions, m_MINCPImpl->m_MincApparentDims) < 0)
{
itkExceptionMacro(" Can't set apparent dimension order!");
}
Expand Down Expand Up @@ -967,8 +965,8 @@ MINCImageIO::WriteImageInformation()
}

// allocating dimensions
vnl_matrix<double> dircosmatrix(nDims, nDims);
dircosmatrix.set_identity();
vnl_matrix<double> directionCosineMatrix(nDims, nDims);
directionCosineMatrix.set_identity();
vnl_vector<double> origin(nDims);

// MINC stores direction cosines in PositiveCoordinateOrientation RAS
Expand All @@ -982,18 +980,18 @@ MINCImageIO::WriteImageInformation()
{
for (unsigned int j = 0; j < nDims; ++j)
{
dircosmatrix[i][j] = this->GetDirection(i)[j];
directionCosineMatrix[i][j] = this->GetDirection(i)[j];
}
origin[i] = this->GetOrigin(i);
}

const vnl_matrix<double> inverseDirectionCosines{ vnl_matrix_inverse<double>(dircosmatrix).as_matrix() };
const vnl_matrix<double> inverseDirectionCosines{ vnl_matrix_inverse<double>(directionCosineMatrix).as_matrix() };
origin *= inverseDirectionCosines; // transform to minc convention


// Convert ITK direction cosines from PositiveCoordinateOrientation LPS to PositiveCoordinateOrientation RAS
if (this->m_RAStoLPS)
dircosmatrix *= RAS_tofrom_LPS;
directionCosineMatrix *= RAS_tofrom_LPS;

for (unsigned int i = 0; i < nDims; ++i)
{
Expand All @@ -1003,7 +1001,7 @@ MINCImageIO::WriteImageInformation()
{
if (k < nDims)
{
dir_cos[k] = dircosmatrix[i][k];
dir_cos[k] = directionCosineMatrix[i][k];
}
else
{
Expand Down
17 changes: 9 additions & 8 deletions Modules/IO/MINC/test/itkMINCImageIOTest4.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ test_image_moments(const char * input_image,
double my,
double mz,
double epsilon,
bool ras_to_lps)
bool RAStofromLPS)
{
if (ras_to_lps)
if (RAStofromLPS)
{ // need to flip expected moments to match flipped coordinate system
mx = -mx;
my = -my;
}

itk::MINCImageIO::Pointer mincIO1 = itk::MINCImageIO::New();
mincIO1->SetRAStoLPS(ras_to_lps);
mincIO1->SetRAStoLPS(RAStofromLPS);

using ReaderType = itk::ImageFileReader<ImageType>;

Expand Down Expand Up @@ -100,7 +100,7 @@ test_image_moments(const char * input_image,
auto writer = WriterType::New();
writer->SetFileName(output_image);
// writer should use default RAS to LPS flag, to satisfy comparison after
mincIO1->SetRAStoLPS(ras_to_lps);
mincIO1->SetRAStoLPS(RAStofromLPS);
if (itksys::SystemTools::StringEndsWith(output_image, ".mnc")) // HACK to enable use .mhd files
writer->SetImageIO(mincIO1);

Expand Down Expand Up @@ -129,8 +129,8 @@ itkMINCImageIOTest4(int argc, char * argv[])

const char * input = argv[1];
const char * output = argv[2];
int ras_to_lps_test = atoi(argv[3]);
bool ras_to_lps = ras_to_lps_test < 0 ? false : ras_to_lps_test == 1;
int RAStofromLPSTest = atoi(argv[3]);
bool RAStofromLPS = RAStofromLPSTest < 0 ? false : RAStofromLPSTest == 1;

double total = 0.0;
double mx = 0.0;
Expand Down Expand Up @@ -164,13 +164,14 @@ itkMINCImageIOTest4(int argc, char * argv[])
int ret = EXIT_SUCCESS;

std::cout.precision(10);
if (test_image_moments<itk::Image<double, 3>>(input, nullptr, total, mx, my, mz, epsilon, ras_to_lps) !=
if (test_image_moments<itk::Image<double, 3>>(input, nullptr, total, mx, my, mz, epsilon, RAStofromLPS) !=
EXIT_SUCCESS)
{
ret = EXIT_FAILURE;
}
// write out only float image
if (test_image_moments<itk::Image<float, 3>>(input, output, total, mx, my, mz, epsilon, ras_to_lps) != EXIT_SUCCESS)
if (test_image_moments<itk::Image<float, 3>>(input, output, total, mx, my, mz, epsilon, RAStofromLPS) !=
EXIT_SUCCESS)
{
ret = EXIT_FAILURE;
}
Expand Down
68 changes: 34 additions & 34 deletions Modules/IO/TransformMINC/src/itkMINCTransformIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,37 @@ MINCTransformIOTemplate<TParametersValueType>::ReadOneTransform(VIO_General_tran
parameterArray.SetSize(12);


Matrix<double, 4, 4> _affine_transform;
Matrix<double, 4, 4> affineMatrix;

_affine_transform.SetIdentity();
affineMatrix.SetIdentity();

// MINC stores transforms in PositiveCoordinateOrientation RAS
// need to convert to PositiveCoordinateOrientation LPS for ITK
Matrix<double, 4, 4> RAS_tofrom_LPS;
RAS_tofrom_LPS.SetIdentity();
RAS_tofrom_LPS(0, 0) = -1.0;
RAS_tofrom_LPS(1, 1) = -1.0;
Matrix<double, 4, 4> RAStofromLPS;
RAStofromLPS.SetIdentity();
RAStofromLPS(0, 0) = -1.0;
RAStofromLPS(1, 1) = -1.0;

for (int j = 0; j < 3; ++j)
{
for (int i = 0; i < 3; ++i)
{
_affine_transform(i, j) = Transform_elem(*lin, j, i);
affineMatrix(i, j) = Transform_elem(*lin, j, i);
}
// shifts
_affine_transform(3, j) = Transform_elem(*lin, j, 3);
affineMatrix(3, j) = Transform_elem(*lin, j, 3);
}

if (this->m_RAStoLPS) // flip RAS PositiveCoordinateOrientation to LPS PositiveCoordinateOrientation
_affine_transform = RAS_tofrom_LPS * _affine_transform * RAS_tofrom_LPS;
affineMatrix = RAStofromLPS * affineMatrix * RAStofromLPS;

for (int j = 0; j < 3; ++j)
{
for (int i = 0; i < 3; ++i)
{
parameterArray.SetElement(i + j * 3, _affine_transform(i, j));
parameterArray.SetElement(i + j * 3, affineMatrix(i, j));
}
parameterArray.SetElement(j + 9, _affine_transform(3, j));
parameterArray.SetElement(j + 9, affineMatrix(3, j));
}

if (xfm->inverse_flag)
Expand Down Expand Up @@ -167,7 +167,7 @@ MINCTransformIOTemplate<TParametersValueType>::ReadOneTransform(VIO_General_tran
using GridImageType = typename DisplacementFieldTransformType::DisplacementFieldType;
using MincReaderType = ImageFileReader<GridImageType>;
using OutputPixelType = vnl_vector_fixed<TParametersValueType, 3>;
const OutputPixelType RAS_tofrom_LPS_vector = { -1, -1, 1 };
const OutputPixelType RAStofromLPSVector = { -1, -1, 1 };

auto mincIO = MINCImageIO::New();
mincIO->SetRAStoLPS(this->m_RAStoLPS);
Expand All @@ -188,13 +188,13 @@ MINCTransformIOTemplate<TParametersValueType>::ReadOneTransform(VIO_General_tran
itk::MultiThreaderBase::Pointer mt = itk::MultiThreaderBase::New();
mt->ParallelizeImageRegion<3>(
grid->GetBufferedRegion(),
[grid, LPSgrid, RAS_tofrom_LPS_vector](const typename GridImageType::RegionType & region) {
[grid, LPSgrid, RAStofromLPSVector](const typename GridImageType::RegionType & region) {
itk::Vector<TParametersValueType, 3> p;
itk::ImageRegionConstIterator<GridImageType> iIt(grid, region);
itk::ImageRegionIterator<GridImageType> oIt(LPSgrid, region);
for (; !iIt.IsAtEnd(); ++iIt, ++oIt)
{
p.SetVnlVector(element_product(iIt.Get().GetVnlVector(), RAS_tofrom_LPS_vector));
p.SetVnlVector(element_product(iIt.Get().GetVnlVector(), RAStofromLPSVector));
oIt.Set(p);
}
},
Expand Down Expand Up @@ -286,34 +286,34 @@ MINCTransformIOTemplate<TParametersValueType>::WriteOneTransform(const int

// MINC stores everything in PositiveCoordinateOrientation RAS
// need to convert from PositiveCoordinateOrientation LPS
Matrix<double, 4, 4> RAS_tofrom_LPS;
Matrix<double, 4, 4> _affine_transform;
Matrix<double, 4, 4> RAStofromLPS;
Matrix<double, 4, 4> affineMatrix;

RAS_tofrom_LPS.SetIdentity();
RAS_tofrom_LPS(0, 0) = -1.0;
RAS_tofrom_LPS(1, 1) = -1.0;
_affine_transform.SetIdentity();
RAStofromLPS.SetIdentity();
RAStofromLPS(0, 0) = -1.0;
RAStofromLPS(1, 1) = -1.0;
affineMatrix.SetIdentity();


for (int j = 0; j < 3; ++j)
{
for (int i = 0; i < 3; ++i)
{
_affine_transform(j, i) = matrix(j, i);
affineMatrix(j, i) = matrix(j, i);
}
_affine_transform(3, j) = offset[j];
affineMatrix(3, j) = offset[j];
}

if (this->m_RAStoLPS)
_affine_transform = RAS_tofrom_LPS * _affine_transform * RAS_tofrom_LPS;
affineMatrix = RAStofromLPS * affineMatrix * RAStofromLPS;

for (int j = 0; j < 3; ++j)
{
for (int i = 0; i < 3; ++i)
{
Transform_elem(lin, j, i) = _affine_transform(j, i);
Transform_elem(lin, j, i) = affineMatrix(j, i);
}
Transform_elem(lin, j, 3) = _affine_transform(3, j);
Transform_elem(lin, j, 3) = affineMatrix(3, j);
}
// add 4th normalization row (not stored)
Transform_elem(lin, 3, 3) = 1.0;
Expand All @@ -330,7 +330,7 @@ MINCTransformIOTemplate<TParametersValueType>::WriteOneTransform(const int
using MincWriterType = ImageFileWriter<GridImageType>;
typename GridImageType::Pointer grid = GridImageType::New();
using OutputPixelType = vnl_vector_fixed<TParametersValueType, 3>;
const OutputPixelType RAS_tofrom_LPS_vector = { -1, -1, 1 };
const OutputPixelType RAStofromLPSVector = { -1, -1, 1 };
auto * _grid_transform = static_cast<DisplacementFieldTransformType *>(const_cast<TransformType *>(curTransform));
char tmp[1024];
snprintf(tmp, sizeof(tmp), "%s_grid_%d.mnc", xfm_file_base, serial);
Expand Down Expand Up @@ -361,27 +361,27 @@ MINCTransformIOTemplate<TParametersValueType>::WriteOneTransform(const int

if (this->m_RAStoLPS)
{
typename GridImageType::Pointer ras_grid = GridImageType::New(); // flipped grid for RAS->LPS conversion
ras_grid->CopyInformation(grid);
ras_grid->SetRegions(grid->GetBufferedRegion());
ras_grid->Allocate(true);
typename GridImageType::Pointer rasGrid = GridImageType::New(); // flipped grid for RAS->LPS conversion
rasGrid->CopyInformation(grid);
rasGrid->SetRegions(grid->GetBufferedRegion());
rasGrid->Allocate(true);

itk::MultiThreaderBase::Pointer mt = itk::MultiThreaderBase::New();
mt->ParallelizeImageRegion<3>(
grid->GetBufferedRegion(),
[grid, ras_grid, RAS_tofrom_LPS_vector](const typename GridImageType::RegionType & region) {
[grid, rasGrid, RAStofromLPSVector](const typename GridImageType::RegionType & region) {
itk::Vector<TParametersValueType, 3> p;
itk::ImageRegionConstIterator<GridImageType> iIt(grid, region);
itk::ImageRegionIterator<GridImageType> oIt(ras_grid, region);
itk::ImageRegionIterator<GridImageType> oIt(rasGrid, region);
for (; !iIt.IsAtEnd(); ++iIt, ++oIt)
{
p.SetVnlVector(element_product(iIt.Get().GetVnlVector(), RAS_tofrom_LPS_vector));
p.SetVnlVector(element_product(iIt.Get().GetVnlVector(), RAStofromLPSVector));
oIt.Set(p);
}
},
nullptr);

writer->SetInput(ras_grid);
writer->SetInput(rasGrid);
}
else
{
Expand Down

0 comments on commit d051e5f

Please sign in to comment.