Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add C++17 [[nodiscard]] to Image "Transform" member functions #3993

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Modules/Core/Common/include/itkImageBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
\endcode
* \sa Transform */
template <typename TCoordRep>
IndexType
[[nodiscard]] IndexType
TransformPhysicalPointToIndex(const Point<TCoordRep, VImageDimension> & point) const
{
IndexType index;
Expand All @@ -459,9 +459,13 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
/** Get the index (discrete) of a voxel from a physical point.
* Floating point index results are rounded to integers
* Returns true if the resulting index is within the image, false otherwise
*
* \note For performance reasons, if you do not need to use the `bool` return value, please call the corresponding
* overload instead, which has only one parameter (the point), and returns the index.
*
* \sa Transform */
template <typename TCoordRep>
bool
[[nodiscard]] bool
TransformPhysicalPointToIndex(const Point<TCoordRep, VImageDimension> & point, IndexType & index) const
{
index = TransformPhysicalPointToIndex(point);
Expand All @@ -487,7 +491,7 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
\endcode
* \sa Transform */
template <typename TIndexRep, typename TCoordRep>
ContinuousIndex<TIndexRep, VImageDimension>
[[nodiscard]] ContinuousIndex<TIndexRep, VImageDimension>
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, VImageDimension> & point) const
{
ContinuousIndex<TIndexRep, VImageDimension> index;
Expand All @@ -508,9 +512,13 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
/** \brief Get the continuous index from a physical point
*
* Returns true if the resulting index is within the image, false otherwise.
*
* \note For performance reasons, if you do not need to use the `bool` return value, please call the corresponding
* overload instead, which has only one parameter (the point), and returns the continuous index.
*
* \sa Transform */
template <typename TCoordRep, typename TIndexRep>
bool
[[nodiscard]] bool
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, VImageDimension> & point,
ContinuousIndex<TIndexRep, VImageDimension> & index) const
{
Expand Down Expand Up @@ -546,7 +554,7 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
* from a continuous index (in the index space)
* \sa Transform */
template <typename TCoordRep, typename TIndexRep>
Point<TCoordRep, VImageDimension>
[[nodiscard]] Point<TCoordRep, VImageDimension>
TransformContinuousIndexToPhysicalPoint(const ContinuousIndex<TIndexRep, VImageDimension> & index) const
{
Point<TCoordRep, VImageDimension> point;
Expand Down Expand Up @@ -579,7 +587,7 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
*
* \sa Transform */
template <typename TCoordRep>
Point<TCoordRep, VImageDimension>
[[nodiscard]] Point<TCoordRep, VImageDimension>
TransformIndexToPhysicalPoint(const IndexType & index) const
{
Point<TCoordRep, VImageDimension> point;
Expand Down Expand Up @@ -629,7 +637,7 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
* \sa Image
*/
template <typename TVector>
TVector
[[nodiscard]] TVector
TransformLocalVectorToPhysicalVector(const TVector & inputGradient) const
{
TVector outputGradient;
Expand Down Expand Up @@ -678,7 +686,7 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
*
*/
template <typename TVector>
TVector
[[nodiscard]] TVector
TransformPhysicalVectorToLocalVector(const TVector & inputGradient) const
{
TVector outputGradient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class ITK_TEMPLATE_EXPORT PhasedArray3DSpecialCoordinatesImage : public SpecialC

/** Returns the continuous index from a physical point */
template <typename TIndexRep, typename TCoordRep>
ContinuousIndex<TIndexRep, 3>
[[nodiscard]] ContinuousIndex<TIndexRep, 3>
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, 3> & point) const
{
const RegionType region = this->GetLargestPossibleRegion();
Expand Down Expand Up @@ -211,9 +211,13 @@ class ITK_TEMPLATE_EXPORT PhasedArray3DSpecialCoordinatesImage : public SpecialC
/** \brief Get the continuous index from a physical point
*
* Returns true if the resulting index is within the image, false otherwise.
*
* \note For performance reasons, if you do not need to use the `bool` return value, please call the corresponding
* overload instead, which has only one parameter (the point), and returns the continuous index.
*
* \sa Transform */
template <typename TCoordRep, typename TIndexRep>
bool
[[nodiscard]] bool
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, 3> & point,
ContinuousIndex<TIndexRep, 3> & index) const
{
Expand All @@ -229,7 +233,7 @@ class ITK_TEMPLATE_EXPORT PhasedArray3DSpecialCoordinatesImage : public SpecialC
* Floating point index results are truncated to integers.
*/
template <typename TCoordRep>
IndexType
[[nodiscard]] IndexType
TransformPhysicalPointToIndex(const Point<TCoordRep, 3> & point) const
{
const RegionType region = this->GetLargestPossibleRegion();
Expand Down Expand Up @@ -257,9 +261,13 @@ class ITK_TEMPLATE_EXPORT PhasedArray3DSpecialCoordinatesImage : public SpecialC
/** Get the index (discrete) from a physical point.
* Floating point index results are truncated to integers.
* Returns true if the resulting index is within the image, false otherwise
*
* \note For performance reasons, if you do not need to use the `bool` return value, please call the corresponding
* overload instead, which has only one parameter (the point), and returns the index.
*
* \sa Transform */
template <typename TCoordRep>
bool
[[nodiscard]] bool
TransformPhysicalPointToIndex(const Point<TCoordRep, 3> & point, IndexType & index) const
{
index = this->TransformPhysicalPointToIndex(point);
Expand Down Expand Up @@ -300,7 +308,7 @@ class ITK_TEMPLATE_EXPORT PhasedArray3DSpecialCoordinatesImage : public SpecialC

/** Returns a physical point from a continuous index. */
template <typename TCoordRep, typename TIndexRep>
Point<TCoordRep, 3>
[[nodiscard]] Point<TCoordRep, 3>
TransformContinuousIndexToPhysicalPoint(const ContinuousIndex<TIndexRep, 3> & index) const
{
Point<TCoordRep, 3> point;
Expand Down Expand Up @@ -338,7 +346,7 @@ class ITK_TEMPLATE_EXPORT PhasedArray3DSpecialCoordinatesImage : public SpecialC

/** Returns a physical point from a discrete index. */
template <typename TCoordRep>
Point<TCoordRep, 3>
[[nodiscard]] Point<TCoordRep, 3>
TransformIndexToPhysicalPoint(const IndexType & index) const
{
Point<TCoordRep, 3> point;
Expand Down
10 changes: 6 additions & 4 deletions Modules/Core/Common/test/itkImageTransformTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ TestTransform()
std::cout << " Image: " << index << " -> " << point << std::endl;

std::cout << "TransformPhysicalPointToIndex..." << std::endl;
orientedImage->TransformPhysicalPointToIndex(point, index);
std::cout << " Image: " << point << " -> " << index << std::endl;
const bool isInsideOrientedImage = orientedImage->TransformPhysicalPointToIndex(point, index);
std::cout << " Image: " << point << " -> " << index << (isInsideOrientedImage ? " inside" : " outside")
<< std::endl;

image->TransformPhysicalPointToIndex(point, index);
std::cout << " Image: " << point << " -> " << index << std::endl;
const bool isInsideImage = image->TransformPhysicalPointToIndex(point, index);
std::cout << " Image: " << point << " -> " << index << (isInsideImage ? " inside" : " outside")
<< std::endl;
}

int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ itkPhasedArray3DSpecialCoordinatesImageTest(int, char *[])

point.Fill(0.05);
point[2] = 3.1;
image->TransformPhysicalPointToIndex(point, index);
std::cout << "Point " << point << " -> Index " << index << std::endl;
image->TransformPhysicalPointToContinuousIndex(point, continuousIndex);
std::cout << "Point " << point << " -> Continuous Index " << continuousIndex << std::endl;
const bool isIndexInside = image->TransformPhysicalPointToIndex(point, index);
std::cout << "Point " << point << " -> Index " << index << (isIndexInside ? " inside" : " outside") << std::endl;
const bool isContinuousIndexInside = image->TransformPhysicalPointToContinuousIndex(point, continuousIndex);
std::cout << "Point " << point << " -> Continuous Index " << continuousIndex
<< (isContinuousIndexInside ? " inside" : " outside") << std::endl;

if (index[0] != 2 || index[1] != 2 || index[2] != 1)
{
Expand Down
24 changes: 16 additions & 8 deletions Modules/Core/ImageAdaptors/include/itkImageAdaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension

/** Returns the continuous index from a physical point. */
template <typename TIndexRep, typename TCoordRep>
ContinuousIndex<TIndexRep, TImage::ImageDimension>
[[nodiscard]] ContinuousIndex<TIndexRep, TImage::ImageDimension>
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, TImage::ImageDimension> & point) const
{
return m_Image->template TransformPhysicalPointToContinuousIndex<TIndexRep>(point);
Expand All @@ -392,9 +392,13 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension
/** \brief Get the continuous index from a physical point
*
* Returns true if the resulting index is within the image, false otherwise.
*
* \note For performance reasons, if you do not need to use the `bool` return value, please call the corresponding
* overload instead, which has only one parameter (the point), and returns the continuous index.
*
* \sa Transform */
template <typename TCoordRep>
bool
[[nodiscard]] bool
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, Self::ImageDimension> & point,
ContinuousIndex<TCoordRep, Self::ImageDimension> & index) const
{
Expand All @@ -403,7 +407,7 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension

/** Returns the index (discrete) of a voxel from a physical point. */
template <typename TCoordRep>
IndexType
[[nodiscard]] IndexType
TransformPhysicalPointToIndex(const Point<TCoordRep, Self::ImageDimension> & point) const
{
return m_Image->TransformPhysicalPointToIndex(point);
Expand All @@ -412,9 +416,13 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension
/** Get the index (discrete) from a physical point.
* Floating point index results are truncated to integers.
* Returns true if the resulting index is within the image, false otherwise
*
* \note For performance reasons, if you do not need to use the `bool` return value, please call the corresponding
* overload instead, which has only one parameter (the point), and returns the index.
*
* \sa Transform */
template <typename TCoordRep>
bool
[[nodiscard]] bool
TransformPhysicalPointToIndex(const Point<TCoordRep, Self::ImageDimension> & point, IndexType & index) const
{
return m_Image->TransformPhysicalPointToIndex(point, index);
Expand All @@ -434,7 +442,7 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension

/** Returns a physical point from a continuous index (in the index space) */
template <typename TCoordRep, typename TIndexRep>
Point<TCoordRep, TImage::ImageDimension>
[[nodiscard]] Point<TCoordRep, TImage::ImageDimension>
TransformContinuousIndexToPhysicalPoint(const ContinuousIndex<TIndexRep, Self::ImageDimension> & index) const
{
return m_Image->template TransformContinuousIndexToPhysicalPoint<TIndexRep>(index);
Expand All @@ -454,7 +462,7 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension

/** Returns a physical point from a discrete index (in the index space) */
template <typename TCoordRep>
Point<TCoordRep, Self::ImageDimension>
[[nodiscard]] Point<TCoordRep, Self::ImageDimension>
TransformIndexToPhysicalPoint(const IndexType & index) const
{
return m_Image->template TransformIndexToPhysicalPoint<TCoordRep>(index);
Expand All @@ -469,7 +477,7 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension
}

template <typename TVector>
TVector
[[nodiscard]] TVector
TransformLocalVectorToPhysicalVector(const TVector & inputGradient) const
{
TVector outputGradient;
Expand All @@ -486,7 +494,7 @@ class ITK_TEMPLATE_EXPORT ImageAdaptor : public ImageBase<TImage::ImageDimension
}

template <typename TVector>
TVector
[[nodiscard]] TVector
TransformPhysicalVectorToLocalVector(const TVector & inputGradient) const
{
TVector outputGradient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ SLICImageFilter<TInputImage, TOutputImage, TDistancePixel>::BeforeThreadedGenera
const IndexType & idx = it.GetIndex();
typename InputImageType::PointType pt;
shrunkImage->TransformIndexToPhysicalPoint(idx, pt);
ContinuousIndexType cidx;
inputImage->TransformPhysicalPointToContinuousIndex(pt, cidx);
const ContinuousIndexType cidx =
inputImage->template TransformPhysicalPointToContinuousIndex<typename PointType::ValueType>(pt);
for (unsigned int i = 0; i < ImageDimension; ++i)
{
cluster[numberOfComponents + i] = cidx[i];
Expand Down