Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
18 changes: 4 additions & 14 deletions Modules/Core/Common/include/itkNeighborhood.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkNeighborhood_hxx

#include "itkNumericTraits.h"
#include "itkPrintHelper.h"

namespace itk
{
Expand Down Expand Up @@ -115,22 +116,11 @@ template <typename TPixel, unsigned int VDimension, typename TContainer>
void
Neighborhood<TPixel, VDimension, TContainer>::PrintSelf(std::ostream & os, Indent indent) const
{
using namespace itk::print_helper;
os << indent << "Size: " << static_cast<typename NumericTraits<SizeType>::PrintType>(m_Size) << std::endl;
os << indent << "Radius: " << static_cast<typename NumericTraits<SizeType>::PrintType>(m_Radius) << std::endl;

os << indent << "StrideTable: [ ";
for (DimensionValueType i = 0; i < VDimension; ++i)
{
os << indent.GetNextIndent() << m_StrideTable[i] << ' ';
}
os << ']' << std::endl;

os << indent << "OffsetTable: [ ";
for (DimensionValueType i = 0; i < m_OffsetTable.size(); ++i)
{
os << indent.GetNextIndent() << m_OffsetTable[i] << ' ';
}
os << ']' << std::endl;
os << indent << "StrideTable: " << m_StrideTable << std::endl;
os << indent << "OffsetTable: " << m_OffsetTable << std::endl;
}
} // namespace itk

Expand Down
38 changes: 36 additions & 2 deletions Modules/Core/Common/include/itkPrintHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <iostream>
#include <iterator>
#include <list>
#include <type_traits>

// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112467
#if defined(ITK_WRAPPING_PARSER) && defined(__GNUC__) && !defined(__clang__)
Expand All @@ -41,13 +43,45 @@ std::ostream &
operator<<(std::ostream & os, const std::vector<T> & v)
{
if (v.empty())
{
return os << "[]";
}

os << '[';
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", "));
return os << v.back() << ']';
}

template <typename T>
std::ostream &
operator<<(std::ostream & os, const std::list<T> & l)
{
if (l.empty())
{
return os << "[]";
}

os << '[';
std::copy(l.begin(), std::prev(l.end()), std::ostream_iterator<T>(os, ", "));
return os << l.back() << ']';
}

// Stream insertion operator for C-style arrays, excluding character arrays (strings)
template <typename T, size_t N, typename = std::enable_if_t<!std::is_same_v<T, char>>>
std::ostream &
operator<<(std::ostream & os, const T (&arr)[N])
{
if constexpr (N == 0)
{
return os << "()";
}

os << '(';
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", "));
return os << v.back() << ')';
for (size_t i = 0; i < N - 1; ++i)
{
os << arr[i] << ", ";
}
return os << arr[N - 1] << ')';
}

} // namespace itk::print_helper
Expand Down
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,7 @@ set(
itkOptimizerParametersGTest.cxx
itkPointGTest.cxx
itkPointSetGTest.cxx
itkPrintHelperGTest.cxx
itkRGBAPixelGTest.cxx
itkRGBPixelGTest.cxx
itkShapedImageNeighborhoodRangeGTest.cxx
Expand Down
91 changes: 91 additions & 0 deletions Modules/Core/Common/test/itkPrintHelperGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

#include "itkPrintHelper.h"
#include "itkOffset.h"
#include "gtest/gtest.h"
#include <sstream>
#include <vector>
#include <list>

TEST(PrintHelper, Vector)
{
using namespace itk::print_helper;
std::vector<int> v{ 1, 2, 3, 4, 5 };
std::ostringstream oss;
oss << v;
EXPECT_EQ(oss.str(), "[1, 2, 3, 4, 5]");
}

TEST(PrintHelper, EmptyVector)
{
using namespace itk::print_helper;
std::vector<int> v;
std::ostringstream oss;
oss << v;
EXPECT_EQ(oss.str(), "[]");
}

TEST(PrintHelper, List)
{
using namespace itk::print_helper;
std::list<int> l{ 1, 2, 3, 4, 5 };
std::ostringstream oss;
oss << l;
EXPECT_EQ(oss.str(), "[1, 2, 3, 4, 5]");
}

TEST(PrintHelper, EmptyList)
{
using namespace itk::print_helper;
std::list<int> l;
std::ostringstream oss;
oss << l;
EXPECT_EQ(oss.str(), "[]");
}

TEST(PrintHelper, CStyleArray)
{
using namespace itk::print_helper;
int arr[5] = { 1, 2, 3, 4, 5 };
std::ostringstream oss;
oss << arr;
EXPECT_EQ(oss.str(), "(1, 2, 3, 4, 5)");
}

TEST(PrintHelper, CStyleArraySingleElement)
{
using namespace itk::print_helper;
int arr[1] = { 42 };
std::ostringstream oss;
oss << arr;
EXPECT_EQ(oss.str(), "(42)");
}

TEST(PrintHelper, VectorOfOffsets)
{
using namespace itk::print_helper;
std::vector<itk::Offset<2>> v;
itk::Offset<2> o1{ { 1, 2 } };
itk::Offset<2> o2{ { 3, 4 } };
v.push_back(o1);
v.push_back(o2);
std::ostringstream oss;
oss << v;
EXPECT_EQ(oss.str(), "[[1, 2], [3, 4]]");
}
16 changes: 4 additions & 12 deletions Modules/Core/Transform/include/itkScalableAffineTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "itkMath.h"
#include "itkNumericTraits.h"
#include "itkPrintHelper.h"
#include "vnl/algo/vnl_matrix_inverse.h"

namespace itk
Expand Down Expand Up @@ -77,19 +78,10 @@ template <typename TParametersValueType, unsigned int VDimension>
void
ScalableAffineTransform<TParametersValueType, VDimension>::PrintSelf(std::ostream & os, Indent indent) const
{
using namespace itk::print_helper;
Superclass::PrintSelf(os, indent);
os << indent << "Scale : ";
for (unsigned int i = 0; i < VDimension; ++i)
{
os << m_Scale[i] << ' ';
}
os << std::endl;
os << indent << "MatrixScale : ";
for (unsigned int i = 0; i < VDimension; ++i)
{
os << m_MatrixScale[i] << ' ';
}
os << std::endl;
os << indent << "Scale: " << m_Scale << std::endl;
os << indent << "MatrixScale: " << m_MatrixScale << std::endl;
}

template <typename TParametersValueType, unsigned int VDimension>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkGaussianDerivativeImageFunction.h"
#include "itkMinimumMaximumImageCalculator.h"
#include "itkMath.h"
#include "itkPrintHelper.h"

namespace itk
{
Expand Down Expand Up @@ -282,6 +283,7 @@ void
HoughTransform2DCirclesImageFilter<TInputPixelType, TOutputPixelType, TRadiusPixelType>::PrintSelf(std::ostream & os,
Indent indent) const
{
using namespace itk::print_helper;
Superclass::PrintSelf(os, indent);

os << indent << "Threshold: " << m_Threshold << std::endl;
Expand All @@ -297,15 +299,7 @@ HoughTransform2DCirclesImageFilter<TInputPixelType, TOutputPixelType, TRadiusPix

itkPrintSelfObjectMacro(RadiusImage);

os << indent << "CirclesList: " << std::endl;
unsigned int i = 0;
auto it = m_CirclesList.begin();
while (it != m_CirclesList.end())
{
os << indent << '[' << i << "]: " << *it << std::endl;
++it;
++i;
}
os << indent << "CirclesList: " << m_CirclesList << std::endl;

os << indent << "OldModifiedTime: " << NumericTraits<ModifiedTimeType>::PrintType(m_OldModifiedTime) << std::endl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkMinimumMaximumImageCalculator.h"
#include "itkCastImageFilter.h"
#include "itkMath.h"
#include "itkPrintHelper.h"

namespace itk
{
Expand Down Expand Up @@ -356,6 +357,7 @@ template <typename TInputPixelType, typename TOutputPixelType>
void
HoughTransform2DLinesImageFilter<TInputPixelType, TOutputPixelType>::PrintSelf(std::ostream & os, Indent indent) const
{
using namespace itk::print_helper;
Superclass::PrintSelf(os, indent);

os << indent << "Threshold: " << m_Threshold << std::endl;
Expand All @@ -365,15 +367,7 @@ HoughTransform2DLinesImageFilter<TInputPixelType, TOutputPixelType>::PrintSelf(s
os << indent << "Accumulator blur variance: " << m_Variance << std::endl;
itkPrintSelfObjectMacro(SimplifyAccumulator);

os << indent << "LinesList: " << std::endl;
unsigned int i = 0;
auto it = m_LinesList.begin();
while (it != m_LinesList.end())
{
os << indent << '[' << i << "]: " << *it << std::endl;
++it;
++i;
}
os << indent << "LinesList: " << m_LinesList << std::endl;

os << indent << "OldModifiedTime: " << NumericTraits<ModifiedTimeType>::PrintType(m_OldModifiedTime) << std::endl;
}
Expand Down
Loading