From 9b326322acc6e8dad3645b4b357d9d27669e0950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20=C3=9Clgen?= Date: Tue, 10 Oct 2023 13:20:28 +0100 Subject: [PATCH] Use ConvKernelType instead of NREG_CONV_KERNEL_TYPE --- niftyreg_build_version.txt | 2 +- reg-apps/reg_f3d.cpp | 2 +- reg-apps/reg_tools.cpp | 18 +++---- reg-lib/Compute.cpp | 4 +- reg-lib/ConvolutionKernel.h | 4 +- reg-lib/_reg_aladin.cpp | 4 +- reg-lib/_reg_base.cpp | 6 +-- reg-lib/_reg_base.h | 2 +- reg-lib/cl/ClConvolutionKernel.cpp | 2 +- reg-lib/cl/ClConvolutionKernel.h | 2 +- reg-lib/cpu/CpuConvolutionKernel.cpp | 2 +- reg-lib/cpu/CpuConvolutionKernel.h | 2 +- reg-lib/cpu/_reg_lncc.cpp | 26 +++++----- reg-lib/cpu/_reg_lncc.h | 4 +- reg-lib/cpu/_reg_mind.cpp | 4 +- reg-lib/cpu/_reg_tools.cpp | 52 ++++++++++---------- reg-lib/cpu/_reg_tools.h | 27 +++++----- reg-lib/cuda/CudaCompute.cpp | 4 +- reg-lib/cuda/CudaConvolutionKernel.cpp | 2 +- reg-lib/cuda/CudaConvolutionKernel.h | 2 +- reg-lib/cuda/CudaKernelConvolution.cu | 18 +++---- reg-lib/cuda/CudaKernelConvolution.hpp | 2 +- reg-test/reg_test_nmi.cpp | 2 +- reg-test/reg_test_regr_kernelConvolution.cpp | 4 +- 24 files changed, 96 insertions(+), 101 deletions(-) diff --git a/niftyreg_build_version.txt b/niftyreg_build_version.txt index f59a90f3..87537f49 100644 --- a/niftyreg_build_version.txt +++ b/niftyreg_build_version.txt @@ -1 +1 @@ -337 +338 diff --git a/reg-apps/reg_f3d.cpp b/reg-apps/reg_f3d.cpp index 28fc968c..104803a4 100755 --- a/reg-apps/reg_f3d.cpp +++ b/reg-apps/reg_f3d.cpp @@ -625,7 +625,7 @@ int main(int argc, char **argv) { } } if (useMeanLNCC) - reg->SetLNCCKernelType(2); + reg->SetLNCCKernelType(ConvKernelType::Gaussian); NR_DEBUG("*******************************************"); NR_DEBUG("*******************************************"); diff --git a/reg-apps/reg_tools.cpp b/reg-apps/reg_tools.cpp index 002686c6..70ff5741 100755 --- a/reg-apps/reg_tools.cpp +++ b/reg-apps/reg_tools.cpp @@ -520,24 +520,24 @@ int main(int argc, char **argv) bool boolX[3]= {1,0,0}; for(int i=0; int*smoothImg->nu; ++i) kernelSize[i]=param->smoothValueX; if(flag->smoothMeanFlag) - reg_tools_kernelConvolution(smoothImg,kernelSize,MEAN_KERNEL,nullptr,timePoint,boolX); + reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Mean,nullptr,timePoint,boolX); else if(flag->smoothSplineFlag) - reg_tools_kernelConvolution(smoothImg,kernelSize,CUBIC_SPLINE_KERNEL,nullptr,timePoint,boolX); - else reg_tools_kernelConvolution(smoothImg,kernelSize,GAUSSIAN_KERNEL,nullptr,timePoint,boolX); + reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Cubic,nullptr,timePoint,boolX); + else reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Gaussian,nullptr,timePoint,boolX); bool boolY[3]= {0,1,0}; for(int i=0; int*smoothImg->nu; ++i) kernelSize[i]=param->smoothValueY; if(flag->smoothMeanFlag) - reg_tools_kernelConvolution(smoothImg,kernelSize,MEAN_KERNEL,nullptr,timePoint,boolY); + reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Mean,nullptr,timePoint,boolY); else if(flag->smoothSplineFlag) - reg_tools_kernelConvolution(smoothImg,kernelSize,CUBIC_SPLINE_KERNEL,nullptr,timePoint,boolY); - else reg_tools_kernelConvolution(smoothImg,kernelSize,GAUSSIAN_KERNEL,nullptr,timePoint,boolY); + reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Cubic,nullptr,timePoint,boolY); + else reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Gaussian,nullptr,timePoint,boolY); bool boolZ[3]= {0,0,1}; for(int i=0; int*smoothImg->nu; ++i) kernelSize[i]=param->smoothValueZ; if(flag->smoothMeanFlag) - reg_tools_kernelConvolution(smoothImg,kernelSize,MEAN_KERNEL,nullptr,timePoint,boolZ); + reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Mean,nullptr,timePoint,boolZ); else if(flag->smoothSplineFlag) - reg_tools_kernelConvolution(smoothImg,kernelSize,CUBIC_SPLINE_KERNEL,nullptr,timePoint,boolZ); - else reg_tools_kernelConvolution(smoothImg,kernelSize,GAUSSIAN_KERNEL,nullptr,timePoint,boolZ); + reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Cubic,nullptr,timePoint,boolZ); + else reg_tools_kernelConvolution(smoothImg,kernelSize,ConvKernelType::Gaussian,nullptr,timePoint,boolZ); delete []kernelSize; delete []timePoint; if(flag->outputImageFlag) diff --git a/reg-lib/Compute.cpp b/reg-lib/Compute.cpp index 68397be8..6814785d 100644 --- a/reg-lib/Compute.cpp +++ b/reg-lib/Compute.cpp @@ -187,7 +187,7 @@ void Compute::NormaliseGradient(double maxGradLength, bool optimiseX, bool optim void Compute::SmoothGradient(float sigma) { if (sigma != 0) { sigma = fabs(sigma); - reg_tools_kernelConvolution(dynamic_cast(con).GetTransformationGradient(), &sigma, GAUSSIAN_KERNEL); + reg_tools_kernelConvolution(dynamic_cast(con).GetTransformationGradient(), &sigma, ConvKernelType::Gaussian); } } /* *************************************************************** */ @@ -231,7 +231,7 @@ void Compute::GetDefFieldFromVelocityGrid(const bool updateStepNumber) { /* *************************************************************** */ void Compute::ConvolveImage(nifti_image *image) { const nifti_image *controlPointGrid = dynamic_cast(con).F3dContent::GetControlPointGrid(); - constexpr int kernelType = CUBIC_SPLINE_KERNEL; + constexpr ConvKernelType kernelType = ConvKernelType::Cubic; float currentNodeSpacing[3]; currentNodeSpacing[0] = currentNodeSpacing[1] = currentNodeSpacing[2] = controlPointGrid->dx; bool activeAxis[3] = { 1, 0, 0 }; diff --git a/reg-lib/ConvolutionKernel.h b/reg-lib/ConvolutionKernel.h index 9acc6446..8d4fdd52 100644 --- a/reg-lib/ConvolutionKernel.h +++ b/reg-lib/ConvolutionKernel.h @@ -1,7 +1,7 @@ #pragma once #include "Kernel.h" -#include "RNifti.h" +#include "_reg_tools.h" class ConvolutionKernel: public Kernel { public: @@ -10,5 +10,5 @@ class ConvolutionKernel: public Kernel { } ConvolutionKernel() : Kernel() {} virtual ~ConvolutionKernel() {} - virtual void Calculate(nifti_image *image, float *sigma, int kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr) = 0; + virtual void Calculate(nifti_image *image, float *sigma, ConvKernelType kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr) = 0; }; diff --git a/reg-lib/_reg_aladin.cpp b/reg-lib/_reg_aladin.cpp index 37e3619c..f8445e3f 100644 --- a/reg-lib/_reg_aladin.cpp +++ b/reg-lib/_reg_aladin.cpp @@ -174,7 +174,7 @@ void reg_aladin::InitialiseRegistration() { for (int i = 1; i < this->referencePyramid[l]->nt; ++i) active[i] = false; sigma[0] = this->referenceSigma; - convolutionKernel->castTo()->Calculate(this->referencePyramid[l], sigma.get(), 0, nullptr, active.get()); + convolutionKernel->castTo()->Calculate(this->referencePyramid[l], sigma.get(), ConvKernelType::Mean, nullptr, active.get()); } if (this->floatingSigma != 0) { // Only the first image is smoothed @@ -184,7 +184,7 @@ void reg_aladin::InitialiseRegistration() { for (int i = 1; i < this->floatingPyramid[l]->nt; ++i) active[i] = false; sigma[0] = this->floatingSigma; - convolutionKernel->castTo()->Calculate(this->floatingPyramid[l], sigma.get(), 0, nullptr, active.get()); + convolutionKernel->castTo()->Calculate(this->floatingPyramid[l], sigma.get(), ConvKernelType::Mean, nullptr, active.get()); } } diff --git a/reg-lib/_reg_base.cpp b/reg-lib/_reg_base.cpp index 80882617..903f3731 100644 --- a/reg-lib/_reg_base.cpp +++ b/reg-lib/_reg_base.cpp @@ -434,7 +434,7 @@ void reg_base::Initialise() { for (int i = 1; i < referencePyramid[l]->nt; ++i) active[i] = false; sigma[0] = referenceSmoothingSigma; - reg_tools_kernelConvolution(referencePyramid[l], sigma.get(), GAUSSIAN_KERNEL, nullptr, active.get()); + reg_tools_kernelConvolution(referencePyramid[l], sigma.get(), ConvKernelType::Gaussian, nullptr, active.get()); } if (floatingSmoothingSigma != 0) { // Only the first image is smoothed @@ -444,7 +444,7 @@ void reg_base::Initialise() { for (int i = 1; i < floatingPyramid[l]->nt; ++i) active[i] = false; sigma[0] = floatingSmoothingSigma; - reg_tools_kernelConvolution(floatingPyramid[l], sigma.get(), GAUSSIAN_KERNEL, nullptr, active.get()); + reg_tools_kernelConvolution(floatingPyramid[l], sigma.get(), ConvKernelType::Gaussian, nullptr, active.get()); } } @@ -623,7 +623,7 @@ void reg_base::UseLNCC(int timepoint, float stddev) { } /* *************************************************************** */ template -void reg_base::SetLNCCKernelType(int type) { +void reg_base::SetLNCCKernelType(ConvKernelType type) { if (!measure_lncc) NR_FATAL_ERROR("The LNCC object has to be created first"); measure_lncc->SetKernelType(type); diff --git a/reg-lib/_reg_base.h b/reg-lib/_reg_base.h index eb5d4d3d..01155ebe 100644 --- a/reg-lib/_reg_base.h +++ b/reg-lib/_reg_base.h @@ -169,7 +169,7 @@ class reg_base: public InterfaceOptimiser { virtual void UseKLDivergence(int); virtual void UseDTI(bool*); virtual void UseLNCC(int, float); - virtual void SetLNCCKernelType(int type); + virtual void SetLNCCKernelType(ConvKernelType type); virtual void SetLocalWeightSim(NiftiImage); virtual void SetNMIWeight(int, double); diff --git a/reg-lib/cl/ClConvolutionKernel.cpp b/reg-lib/cl/ClConvolutionKernel.cpp index 299cef9c..1fb8932a 100644 --- a/reg-lib/cl/ClConvolutionKernel.cpp +++ b/reg-lib/cl/ClConvolutionKernel.cpp @@ -2,7 +2,7 @@ #include "_reg_tools.h" /* *************************************************************** */ -void ClConvolutionKernel::Calculate(nifti_image *image, float *sigma, int kernelType, int *mask, bool *timePoints, bool *axis) { +void ClConvolutionKernel::Calculate(nifti_image *image, float *sigma, ConvKernelType kernelType, int *mask, bool *timePoints, bool *axis) { reg_tools_kernelConvolution(image, sigma, kernelType, mask, timePoints, axis); } /* *************************************************************** */ diff --git a/reg-lib/cl/ClConvolutionKernel.h b/reg-lib/cl/ClConvolutionKernel.h index 4d1b31d1..824578d5 100644 --- a/reg-lib/cl/ClConvolutionKernel.h +++ b/reg-lib/cl/ClConvolutionKernel.h @@ -7,5 +7,5 @@ class ClConvolutionKernel: public ConvolutionKernel { public: ClConvolutionKernel() : ConvolutionKernel() {} ~ClConvolutionKernel() {} - void Calculate(nifti_image *image, float *sigma, int kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr); + void Calculate(nifti_image *image, float *sigma, ConvKernelType kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr); }; diff --git a/reg-lib/cpu/CpuConvolutionKernel.cpp b/reg-lib/cpu/CpuConvolutionKernel.cpp index f91b3133..57b78b48 100644 --- a/reg-lib/cpu/CpuConvolutionKernel.cpp +++ b/reg-lib/cpu/CpuConvolutionKernel.cpp @@ -2,7 +2,7 @@ #include "_reg_globalTrans.h" /* *************************************************************** */ -void CpuConvolutionKernel::Calculate(nifti_image *image, float *sigma, int kernelType, int *mask, bool *timePoints, bool *axis) { +void CpuConvolutionKernel::Calculate(nifti_image *image, float *sigma, ConvKernelType kernelType, int *mask, bool *timePoints, bool *axis) { reg_tools_kernelConvolution(image, sigma, kernelType, mask, timePoints, axis); } /* *************************************************************** */ diff --git a/reg-lib/cpu/CpuConvolutionKernel.h b/reg-lib/cpu/CpuConvolutionKernel.h index 49e2b333..3e960308 100644 --- a/reg-lib/cpu/CpuConvolutionKernel.h +++ b/reg-lib/cpu/CpuConvolutionKernel.h @@ -6,5 +6,5 @@ class CpuConvolutionKernel: public ConvolutionKernel { public: CpuConvolutionKernel() : ConvolutionKernel() {} - void Calculate(nifti_image *image, float *sigma, int kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr); + void Calculate(nifti_image *image, float *sigma, ConvKernelType kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr); }; diff --git a/reg-lib/cpu/_reg_lncc.cpp b/reg-lib/cpu/_reg_lncc.cpp index 51e4c82b..76145602 100644 --- a/reg-lib/cpu/_reg_lncc.cpp +++ b/reg-lib/cpu/_reg_lncc.cpp @@ -29,7 +29,7 @@ reg_lncc::reg_lncc(): reg_measure() { this->backwardMask = nullptr; // Gaussian kernel is used by default - this->kernelType = GAUSSIAN_KERNEL; + this->kernelType = ConvKernelType::Gaussian; for (int i = 0; i < 255; ++i) kernelStandardDeviation[i] = -5.f; @@ -201,8 +201,8 @@ void UpdateLocalStatImages(const nifti_image *refImage, const int *refMask, int *combinedMask, const float *kernelStandardDeviation, - const int& kernelType, - const int& currentTimepoint) { + const ConvKernelType kernelType, + const int currentTimepoint) { // Generate the combined mask to ignore all NaN values #ifdef _WIN32 long voxel; @@ -258,8 +258,8 @@ double reg_getLnccValue(const nifti_image *referenceImage, const int *combinedMask, const float *kernelStandardDeviation, nifti_image *correlationImage, - const int& kernelType, - const int& currentTimepoint) { + const ConvKernelType kernelType, + const int currentTimepoint) { #ifdef _WIN32 long voxel; const long voxelNumber = (long)NiftiImage::calcVoxelNumber(referenceImage, 3); @@ -318,8 +318,8 @@ double GetSimilarityMeasureValue(const nifti_image *referenceImage, int *forwardMask, const float *kernelStandardDeviation, nifti_image *correlationImage, - const int& kernelType, - const int& referenceTimePoint, + const ConvKernelType kernelType, + const int referenceTimePoint, const double *timePointWeight) { double lncc = 0; for (int currentTimepoint = 0; currentTimepoint < referenceTimePoint; ++currentTimepoint) { @@ -401,9 +401,9 @@ void reg_getVoxelBasedLnccGradient(const nifti_image *referenceImage, nifti_image *correlationImage, const nifti_image *warpedGradient, nifti_image *measureGradient, - const int& kernelType, - const int& currentTimepoint, - const double& timepointWeight) { + const ConvKernelType kernelType, + const int currentTimepoint, + const double timepointWeight) { #ifdef _WIN32 long voxel; long voxelNumber = (long)NiftiImage::calcVoxelNumber(referenceImage, 3); @@ -529,9 +529,9 @@ void GetVoxelBasedSimilarityMeasureGradient(const nifti_image *referenceImage, nifti_image *correlationImage, const nifti_image *warpedGradient, nifti_image *measureGradient, - const int& kernelType, - const int& currentTimepoint, - const double& timepointWeight) { + const ConvKernelType kernelType, + const int currentTimepoint, + const double timepointWeight) { std::visit([&](auto&& refImgDataType) { using RefImgDataType = std::decay_t; // Compute the mean and variance of the reference and warped floating diff --git a/reg-lib/cpu/_reg_lncc.h b/reg-lib/cpu/_reg_lncc.h index b59b48fd..fea5e464 100644 --- a/reg-lib/cpu/_reg_lncc.h +++ b/reg-lib/cpu/_reg_lncc.h @@ -47,7 +47,7 @@ class reg_lncc: public reg_measure { this->kernelStandardDeviation[t] = stddev; } /// @brief Set the kernel type - virtual void SetKernelType(int t) { + virtual void SetKernelType(ConvKernelType t) { this->kernelType = t; } @@ -67,6 +67,6 @@ class reg_lncc: public reg_measure { nifti_image *warpedSdevImageBw; int *backwardMask; - int kernelType; + ConvKernelType kernelType; }; /* *************************************************************** */ diff --git a/reg-lib/cpu/_reg_mind.cpp b/reg-lib/cpu/_reg_mind.cpp index 3fa94f11..b620e9e6 100644 --- a/reg-lib/cpu/_reg_mind.cpp +++ b/reg-lib/cpu/_reg_mind.cpp @@ -100,7 +100,7 @@ void GetMindImageDescriptorCore(const nifti_image *inputImage, ShiftImage(currentInputImage, shiftedImage, mask, rSamplingX[i], rSamplingY[i], rSamplingZ[i]); reg_tools_subtractImageFromImage(currentInputImage, shiftedImage, diffImage); reg_tools_multiplyImageToImage(diffImage, diffImage, diffImage); - reg_tools_kernelConvolution(diffImage, &sigma, GAUSSIAN_KERNEL, mask); + reg_tools_kernelConvolution(diffImage, &sigma, ConvKernelType::Gaussian, mask); reg_tools_addImageToImage(meanImage, diffImage, meanImage); // Store the current descriptor const size_t index = i * diffImage->nvox; @@ -217,7 +217,7 @@ void GetMindSscImageDescriptorCore(const nifti_image *inputImage, ShiftImage(currentInputImage, shiftedImage, mask, rSamplingX[i], rSamplingY[i], rSamplingZ[i]); reg_tools_subtractImageFromImage(currentInputImage, shiftedImage, diffImage); reg_tools_multiplyImageToImage(diffImage, diffImage, diffImage); - reg_tools_kernelConvolution(diffImage, &sigma, GAUSSIAN_KERNEL, mask); + reg_tools_kernelConvolution(diffImage, &sigma, ConvKernelType::Gaussian, mask); for (int j = 0; j < 2; j++) { ShiftImage(diffImage, diffImageShifted, maskDiffImage, tx[compteurId], ty[compteurId], tz[compteurId]); diff --git a/reg-lib/cpu/_reg_tools.cpp b/reg-lib/cpu/_reg_tools.cpp index ae9d6e2a..a0255b23 100755 --- a/reg-lib/cpu/_reg_tools.cpp +++ b/reg-lib/cpu/_reg_tools.cpp @@ -334,9 +334,9 @@ template void reg_thresholdImage(nifti_image*, double, double); /* *************************************************************** */ template PrecisionType reg_getMaximalLength(const nifti_image *image, - const bool& optimiseX, - const bool& optimiseY, - const bool& optimiseZ) { + const bool optimiseX, + const bool optimiseY, + const bool optimiseZ) { const size_t voxelNumber = NiftiImage::calcVoxelNumber(image, 3); const DataType *dataPtrX = static_cast(image->data); const DataType *dataPtrY = &dataPtrX[voxelNumber]; @@ -354,9 +354,9 @@ PrecisionType reg_getMaximalLength(const nifti_image *image, /* *************************************************************** */ template PrecisionType reg_getMaximalLength(const nifti_image *image, - const bool& optimiseX, - const bool& optimiseY, - const bool& optimiseZ) { + const bool optimiseX, + const bool optimiseY, + const bool optimiseZ) { switch (image->datatype) { case NIFTI_TYPE_FLOAT32: return reg_getMaximalLength(image, optimiseX, optimiseY, image->nz > 1 ? optimiseZ : false); @@ -367,8 +367,8 @@ PrecisionType reg_getMaximalLength(const nifti_image *image, } return EXIT_SUCCESS; } -template float reg_getMaximalLength(const nifti_image*, const bool&, const bool&, const bool&); -template double reg_getMaximalLength(const nifti_image*, const bool&, const bool&, const bool&); +template float reg_getMaximalLength(const nifti_image*, const bool, const bool, const bool); +template double reg_getMaximalLength(const nifti_image*, const bool, const bool, const bool); /* *************************************************************** */ template void reg_tools_changeDatatype(nifti_image *image, int type) { @@ -650,7 +650,7 @@ void reg_tools_divideImageToImage(const nifti_image *img1, template void reg_tools_operationValueToImage(const nifti_image *img, nifti_image *res, - const double& val, + const double val, const Operation& operation) { const Type *imgPtr = static_cast(img->data); Type *resPtr = static_cast(res->data); @@ -678,7 +678,7 @@ void reg_tools_operationValueToImage(const nifti_image *img, /* *************************************************************** */ void reg_tools_addValueToImage(const nifti_image *img, nifti_image *res, - const double& val) { + const double val) { if (img->datatype != res->datatype) NR_FATAL_ERROR("Input and output image are expected to be of the same type"); if (img->nvox != res->nvox) @@ -716,7 +716,7 @@ void reg_tools_addValueToImage(const nifti_image *img, /* *************************************************************** */ void reg_tools_subtractValueFromImage(const nifti_image *img, nifti_image *res, - const double& val) { + const double val) { if (img->datatype != res->datatype) NR_FATAL_ERROR("Input and output image are expected to be of the same type"); if (img->nvox != res->nvox) @@ -754,7 +754,7 @@ void reg_tools_subtractValueFromImage(const nifti_image *img, /* *************************************************************** */ void reg_tools_multiplyValueToImage(const nifti_image *img, nifti_image *res, - const double& val) { + const double val) { if (img->datatype != res->datatype) NR_FATAL_ERROR("Input and output image are expected to be of the same type"); if (img->nvox != res->nvox) @@ -792,7 +792,7 @@ void reg_tools_multiplyValueToImage(const nifti_image *img, /* *************************************************************** */ void reg_tools_divideValueToImage(const nifti_image *img, nifti_image *res, - const double& val) { + const double val) { if (img->datatype != res->datatype) NR_FATAL_ERROR("Input and output image are expected to be of the same type"); if (img->nvox != res->nvox) @@ -831,7 +831,7 @@ void reg_tools_divideValueToImage(const nifti_image *img, template void reg_tools_kernelConvolution(nifti_image *image, const float *sigma, - const int& kernelType, + const ConvKernelType kernelType, const int *mask, const bool *timePoints, const bool *axes) { @@ -873,13 +873,13 @@ void reg_tools_kernelConvolution(nifti_image *image, else temp = fabs(sigma[t]); // voxel-based if negative value int radius = 0; // Define the kernel size - if (kernelType == MEAN_KERNEL || kernelType == LINEAR_KERNEL) { + if (kernelType == ConvKernelType::Mean || kernelType == ConvKernelType::Linear) { // Mean or linear filtering radius = static_cast(temp); - } else if (kernelType == GAUSSIAN_KERNEL) { + } else if (kernelType == ConvKernelType::Gaussian) { // Gaussian kernel radius = static_cast(temp * 3.0f); - } else if (kernelType == CUBIC_SPLINE_KERNEL) { + } else if (kernelType == ConvKernelType::Cubic) { // Spline kernel radius = static_cast(temp * 2.0f); } else { @@ -890,7 +890,7 @@ void reg_tools_kernelConvolution(nifti_image *image, float kernel[4096]; double kernelSum = 0; // Fill the kernel - if (kernelType == CUBIC_SPLINE_KERNEL) { + if (kernelType == ConvKernelType::Cubic) { // Compute the Cubic Spline kernel for (int i = -radius; i <= radius; i++) { // temp contains the kernel node spacing @@ -902,7 +902,7 @@ void reg_tools_kernelConvolution(nifti_image *image, else kernel[i + radius] = 0; kernelSum += kernel[i + radius]; } - } else if (kernelType == GAUSSIAN_KERNEL) { + } else if (kernelType == ConvKernelType::Gaussian) { // Compute the Gaussian kernel for (int i = -radius; i <= radius; i++) { // 2.506... = sqrt(2*pi) @@ -910,13 +910,13 @@ void reg_tools_kernelConvolution(nifti_image *image, kernel[radius + i] = static_cast(exp(-Square(i) / (2.0 * Square(temp))) / (temp * 2.506628274631)); kernelSum += kernel[radius + i]; } - } else if (kernelType == LINEAR_KERNEL) { + } else if (kernelType == ConvKernelType::Linear) { // Compute the linear kernel for (int i = -radius; i <= radius; i++) { kernel[radius + i] = 1.f - fabs(i / static_cast(radius)); kernelSum += kernel[radius + i]; } - } else if (kernelType == MEAN_KERNEL && imageDims[2] == 1) { + } else if (kernelType == ConvKernelType::Mean && imageDims[2] == 1) { // Compute the mean kernel for (int i = -radius; i <= radius; i++) { kernel[radius + i] = 1.f; @@ -925,7 +925,7 @@ void reg_tools_kernelConvolution(nifti_image *image, } // No kernel is required for the mean filtering // No need for kernel normalisation as this is handled by the density function - NR_DEBUG("Convolution type[" << kernelType << "] dim[" << n << "] tp[" << t << "] radius[" << radius << "] kernelSum[" << kernelSum << "]"); + NR_DEBUG("Convolution type[" << int(kernelType) << "] dim[" << n << "] tp[" << t << "] radius[" << radius << "] kernelSum[" << kernelSum << "]"); int planeNumber, planeIndex, lineOffset; int lineIndex, shiftPre, shiftPst, k; @@ -1305,7 +1305,7 @@ void reg_tools_labelKernelConvolution(nifti_image *image, /* *************************************************************** */ void reg_tools_kernelConvolution(nifti_image *image, const float *sigma, - const int& kernelType, + const ConvKernelType kernelType, const int *mask, const bool *timePoints, const bool *axes) { @@ -1346,7 +1346,7 @@ void reg_downsampleImage(nifti_image *image, int type, bool *downsampleAxis) { /* the input image is first smooth */ float *sigma = new float[image->nt]; for (int i = 0; i < image->nt; ++i) sigma[i] = -0.7355f; - reg_tools_kernelConvolution(image, sigma, GAUSSIAN_KERNEL); + reg_tools_kernelConvolution(image, sigma, ConvKernelType::Gaussian); delete[] sigma; } @@ -2556,7 +2556,7 @@ void coordinateFromLinearIndex(int index, int maxValue_x, int maxValue_y, int& x z = index; } /* *************************************************************** */ -nifti_image* nifti_dup(const nifti_image& image, const bool& copyData) { +nifti_image* nifti_dup(const nifti_image& image, const bool copyData) { nifti_image *newImage = nifti_copy_nim_info(&image); newImage->data = calloc(image.nvox, image.nbyper); if (copyData) @@ -2564,7 +2564,7 @@ nifti_image* nifti_dup(const nifti_image& image, const bool& copyData) { return newImage; } /* *************************************************************** */ -void PrintCmdLine(const int& argc, const char * const *argv, const bool& verbose) { +void PrintCmdLine(const int& argc, const char * const *argv, const bool verbose) { #ifdef NDEBUG if (!verbose) return; #endif diff --git a/reg-lib/cpu/_reg_tools.h b/reg-lib/cpu/_reg_tools.h index 77d01e55..c014e6d1 100755 --- a/reg-lib/cpu/_reg_tools.h +++ b/reg-lib/cpu/_reg_tools.h @@ -32,12 +32,7 @@ using RNifti::NiftiImage; using RNifti::NiftiImageData; using NiftiDim = NiftiImage::Dim; -typedef enum { - MEAN_KERNEL, - LINEAR_KERNEL, - GAUSSIAN_KERNEL, - CUBIC_SPLINE_KERNEL -} NREG_CONV_KERNEL_TYPE; +enum class ConvKernelType { Mean, Linear, Gaussian, Cubic }; /* *************************************************************** */ /** @brief This function check some header parameters and correct them in @@ -94,7 +89,7 @@ void reg_getRealImageSpacing(nifti_image *image, */ void reg_tools_kernelConvolution(nifti_image *image, const float *sigma, - const int& kernelType, + const ConvKernelType kernelType, const int *mask = nullptr, const bool *timePoints = nullptr, const bool *axes = nullptr); @@ -136,9 +131,9 @@ void reg_downsampleImage(nifti_image *image, */ template PrecisionType reg_getMaximalLength(const nifti_image *image, - const bool& optimiseX, - const bool& optimiseY, - const bool& optimiseZ); + const bool optimiseX, + const bool optimiseY, + const bool optimiseZ); /* *************************************************************** */ /** @brief Change the datatype of a nifti image * @param image Image to be updated. @@ -194,7 +189,7 @@ void reg_tools_divideImageToImage(const nifti_image *img1, */ void reg_tools_addValueToImage(const nifti_image *img, nifti_image *out, - const double& val); + const double val); /* *************************************************************** */ /** @brief Subtract a scalar from all image intensity * @param img Input image @@ -203,7 +198,7 @@ void reg_tools_addValueToImage(const nifti_image *img, */ void reg_tools_subtractValueFromImage(const nifti_image *img, nifti_image *out, - const double& val); + const double val); /* *************************************************************** */ /** @brief Multiply a scalar to all image intensity * @param img Input image @@ -212,7 +207,7 @@ void reg_tools_subtractValueFromImage(const nifti_image *img, */ void reg_tools_multiplyValueToImage(const nifti_image *img, nifti_image *out, - const double& val); + const double val); /* *************************************************************** */ /** @brief Divide a scalar to all image intensity * @param img Input image @@ -221,7 +216,7 @@ void reg_tools_multiplyValueToImage(const nifti_image *img, */ void reg_tools_divideValueToImage(const nifti_image *img, nifti_image *out, - const double& val); + const double val); /* *************************************************************** */ /** @brief Binarise an input image. All values different * from 0 are set to 1, 0 otherwise. @@ -428,8 +423,8 @@ void coordinateFromLinearIndex(int index, int maxValue_x, int maxValue_y, int& x * @param copyData Boolean to specify if the image data should be copied * @return The duplicated image */ -nifti_image* nifti_dup(const nifti_image& image, const bool& copyData = true); +nifti_image* nifti_dup(const nifti_image& image, const bool copyData = true); /* *************************************************************** */ /// @brief Prints the command line -void PrintCmdLine(const int& argc, const char * const *argv, const bool& verbose); +void PrintCmdLine(const int& argc, const char * const *argv, const bool verbose); /* *************************************************************** */ diff --git a/reg-lib/cuda/CudaCompute.cpp b/reg-lib/cuda/CudaCompute.cpp index 53e54d04..dc573e42 100644 --- a/reg-lib/cuda/CudaCompute.cpp +++ b/reg-lib/cuda/CudaCompute.cpp @@ -145,7 +145,7 @@ void CudaCompute::SmoothGradient(float sigma) { if (sigma == 0) return; sigma = fabs(sigma); CudaF3dContent& con = dynamic_cast(this->con); - Cuda::KernelConvolution(con.F3dContent::GetTransformationGradient(), con.GetTransformationGradientCuda(), &sigma, GAUSSIAN_KERNEL); + Cuda::KernelConvolution(con.F3dContent::GetTransformationGradient(), con.GetTransformationGradientCuda(), &sigma, ConvKernelType::Gaussian); } /* *************************************************************** */ void CudaCompute::GetApproximatedGradient(InterfaceOptimiser& opt) { @@ -165,7 +165,7 @@ void CudaCompute::GetDefFieldFromVelocityGrid(const bool updateStepNumber) { /* *************************************************************** */ void CudaCompute::ConvolveImage(const nifti_image *image, float4 *imageCuda) { const nifti_image *controlPointGrid = dynamic_cast(con).F3dContent::GetControlPointGrid(); - constexpr int kernelType = CUBIC_SPLINE_KERNEL; + constexpr ConvKernelType kernelType = ConvKernelType::Cubic; float currentNodeSpacing[3]; currentNodeSpacing[0] = currentNodeSpacing[1] = currentNodeSpacing[2] = controlPointGrid->dx; bool activeAxis[3] = { 1, 0, 0 }; diff --git a/reg-lib/cuda/CudaConvolutionKernel.cpp b/reg-lib/cuda/CudaConvolutionKernel.cpp index 60d7b9cd..9e0882a6 100644 --- a/reg-lib/cuda/CudaConvolutionKernel.cpp +++ b/reg-lib/cuda/CudaConvolutionKernel.cpp @@ -4,7 +4,7 @@ /* *************************************************************** */ void CudaConvolutionKernel::Calculate(nifti_image *image, float *sigma, - int kernelType, + ConvKernelType kernelType, int *mask, bool *timePoint, bool *axis) { diff --git a/reg-lib/cuda/CudaConvolutionKernel.h b/reg-lib/cuda/CudaConvolutionKernel.h index 832ec853..f0d9ca74 100644 --- a/reg-lib/cuda/CudaConvolutionKernel.h +++ b/reg-lib/cuda/CudaConvolutionKernel.h @@ -9,7 +9,7 @@ class CudaConvolutionKernel: public ConvolutionKernel { CudaConvolutionKernel() : ConvolutionKernel() {} void Calculate(nifti_image *image, float *sigma, - int kernelType, + ConvKernelType kernelType, int *mask = nullptr, bool *timePoints = nullptr, bool *axis = nullptr); diff --git a/reg-lib/cuda/CudaKernelConvolution.cu b/reg-lib/cuda/CudaKernelConvolution.cu index 7f446c53..2f8ddcaf 100644 --- a/reg-lib/cuda/CudaKernelConvolution.cu +++ b/reg-lib/cuda/CudaKernelConvolution.cu @@ -4,7 +4,7 @@ void NiftyReg::Cuda::KernelConvolution(const nifti_image *image, float4 *imageCuda, const float *sigma, - const int kernelType, + const ConvKernelType kernelType, const bool *timePoints, const bool *axes) { if (image->nx > 2048 || image->ny > 2048 || image->nz > 2048) @@ -56,13 +56,13 @@ void NiftyReg::Cuda::KernelConvolution(const nifti_image *image, else temp = fabs(sigma[t]); // voxel-based if negative value int radius = 0; // Define the kernel size - if (kernelType == MEAN_KERNEL || kernelType == LINEAR_KERNEL) { + if (kernelType == ConvKernelType::Mean || kernelType == ConvKernelType::Linear) { // Mean or linear filtering radius = static_cast(temp); - } else if (kernelType == GAUSSIAN_KERNEL) { + } else if (kernelType == ConvKernelType::Gaussian) { // Gaussian kernel radius = static_cast(temp * 3.0); - } else if (kernelType == CUBIC_SPLINE_KERNEL) { + } else if (kernelType == ConvKernelType::Cubic) { // Spline kernel radius = static_cast(temp * 2.0); } else { @@ -74,7 +74,7 @@ void NiftyReg::Cuda::KernelConvolution(const nifti_image *image, vector kernel(2 * radius + 1); double kernelSum = 0; // Fill the kernel - if (kernelType == CUBIC_SPLINE_KERNEL) { + if (kernelType == ConvKernelType::Cubic) { // Compute the Cubic Spline kernel for (int i = -radius; i <= radius; i++) { // temp contains the kernel node spacing @@ -86,7 +86,7 @@ void NiftyReg::Cuda::KernelConvolution(const nifti_image *image, else kernel[i + radius] = 0; kernelSum += kernel[i + radius]; } - } else if (kernelType == GAUSSIAN_KERNEL) { + } else if (kernelType == ConvKernelType::Gaussian) { // Compute the Gaussian kernel for (int i = -radius; i <= radius; i++) { // 2.506... = sqrt(2*pi) @@ -94,13 +94,13 @@ void NiftyReg::Cuda::KernelConvolution(const nifti_image *image, kernel[i + radius] = static_cast(exp(-Square(i) / (2.0 * Square(temp))) / (temp * 2.506628274631)); kernelSum += kernel[i + radius]; } - } else if (kernelType == LINEAR_KERNEL) { + } else if (kernelType == ConvKernelType::Linear) { // Compute the linear kernel for (int i = -radius; i <= radius; i++) { kernel[i + radius] = 1.f - fabs(i / static_cast(radius)); kernelSum += kernel[i + radius]; } - } else if (kernelType == MEAN_KERNEL && imageDims.z == 1) { + } else if (kernelType == ConvKernelType::Mean && imageDims.z == 1) { // Compute the mean kernel for (int i = -radius; i <= radius; i++) { kernel[i + radius] = 1.f; @@ -109,7 +109,7 @@ void NiftyReg::Cuda::KernelConvolution(const nifti_image *image, } // No kernel is required for the mean filtering // No need for kernel normalisation as this is handled by the density function - NR_DEBUG("Convolution type[" << kernelType << "] dim[" << n << "] tp[" << t << "] radius[" << radius << "] kernelSum[" << kernelSum << "]"); + NR_DEBUG("Convolution type[" << int(kernelType) << "] dim[" << n << "] tp[" << t << "] radius[" << radius << "] kernelSum[" << kernelSum << "]"); int planeCount, lineOffset; switch (n) { diff --git a/reg-lib/cuda/CudaKernelConvolution.hpp b/reg-lib/cuda/CudaKernelConvolution.hpp index 7d74c944..de1a3c0c 100644 --- a/reg-lib/cuda/CudaKernelConvolution.hpp +++ b/reg-lib/cuda/CudaKernelConvolution.hpp @@ -19,7 +19,7 @@ namespace NiftyReg::Cuda { void KernelConvolution(const nifti_image *image, float4 *imageCuda, const float *sigma, - const int kernelType, + const ConvKernelType kernelType, const bool *timePoints = nullptr, const bool *axes = nullptr); /* *************************************************************** */ diff --git a/reg-test/reg_test_nmi.cpp b/reg-test/reg_test_nmi.cpp index 39841b80..1ad4bd2c 100644 --- a/reg-test/reg_test_nmi.cpp +++ b/reg-test/reg_test_nmi.cpp @@ -121,7 +121,7 @@ class NmiTest { *jhPtr++ = jh[i][j] / ref.nVoxels(); // Apply a convolution to mimic the parzen windowing float sigma[1] = { 1.f }; - reg_tools_kernelConvolution(jointHistogram, sigma, CUBIC_SPLINE_KERNEL); + reg_tools_kernelConvolution(jointHistogram, sigma, ConvKernelType::Cubic); // Restore the jh array jhPtr = static_cast(jointHistogram->data); for (unsigned i = 0; i < 68; ++i) diff --git a/reg-test/reg_test_regr_kernelConvolution.cpp b/reg-test/reg_test_regr_kernelConvolution.cpp index c4fe1bd8..034a9fd4 100644 --- a/reg-test/reg_test_regr_kernelConvolution.cpp +++ b/reg-test/reg_test_regr_kernelConvolution.cpp @@ -126,8 +126,8 @@ class KernelConvolutionTest { contentCuda->SetDeformationField(imageCuda.disown()); // Compute the kernel convolution for CPU and CUDA - reg_tools_kernelConvolution(contentCpu->GetDeformationField(), sigmaValues.data(), kernelType, nullptr, activeTimePoints, activeAxes); - Cuda::KernelConvolution(contentCuda->Content::GetDeformationField(), contentCuda->GetDeformationFieldCuda(), sigmaValues.data(), kernelType, activeTimePoints, activeAxes); + reg_tools_kernelConvolution(contentCpu->GetDeformationField(), sigmaValues.data(), ConvKernelType(kernelType), nullptr, activeTimePoints, activeAxes); + Cuda::KernelConvolution(contentCuda->Content::GetDeformationField(), contentCuda->GetDeformationFieldCuda(), sigmaValues.data(), ConvKernelType(kernelType), activeTimePoints, activeAxes); // Get the images imageCpu = NiftiImage(contentCpu->GetDeformationField(), NiftiImage::Copy::Image);