Skip to content

Commit

Permalink
ENH: Add itk::PointSetToImageFilter unit tests
Browse files Browse the repository at this point in the history
Add `itk::PointSetToImageFilter` unit tests.
  • Loading branch information
jhlegarreta authored and hjmjohnson committed Mar 14, 2022
1 parent 00a2987 commit 3adda5a
Show file tree
Hide file tree
Showing 6 changed files with 703 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4676daffcc379ed78b411cebb9966722609e26fb2b7d4e709454d4fcd0e75ed040ec8bac9da98326f4a86cd75aa17fb0531f3a2de9dcac0188adbec190a5bec3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a76fc3e9eac7c8c2cf7e1a77091e8de94712a1843f25eae0cc3629202223e40cd148bcd0457c1f48c03ccead138c7ed4c2d2aaeb29ac8a578ff65c8888149e95
10 changes: 10 additions & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ itkMultipleLogOutputTest.cxx
itkVectorTest.cxx
itkImageTest.cxx
itkPointSetTest.cxx
itkPointSetToImageFilterTest1.cxx
itkPointSetToImageFilterTest2.cxx
itkBresenhamLineTest.cxx
itkSparseFieldLayerTest.cxx
itkDataObjectTest.cxx
Expand Down Expand Up @@ -345,6 +347,14 @@ itk_add_test(NAME itkIteratorTests COMMAND ITKCommon1TestDriver itkIteratorTests
itk_add_test(NAME itkObjectFactoryTest COMMAND ITKCommon1TestDriver itkObjectFactoryTest)
itk_add_test(NAME itkVectorTest COMMAND ITKCommon1TestDriver itkVectorTest)
itk_add_test(NAME itkPointSetTest COMMAND ITKCommon1TestDriver itkPointSetTest)
itk_add_test(NAME itkPointSetToImageFilterTest1
COMMAND ITKCommon1TestDriver
--compare DATA{Baseline/itkPointSetToImageFilterTest1.png} ${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest1.png
itkPointSetToImageFilterTest1 ${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest1.png)
itk_add_test(NAME itkPointSetToImageFilterTest2
COMMAND ITKCommon1TestDriver
--compare DATA{Baseline/itkPointSetToImageFilterTest2.mha} ${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest2.mha
itkPointSetToImageFilterTest2 DATA{${ITK_DATA_ROOT}/Input/VascularTreePointSet.txt} ${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest2.mha)
itk_add_test(NAME itkBresenhamLineTest COMMAND ITKCommon1TestDriver itkBresenhamLineTest)
itk_add_test(NAME itkSparseFieldLayerTest COMMAND ITKCommon1TestDriver itkSparseFieldLayerTest)
itk_add_test(NAME itkDataObjectTest COMMAND ITKCommon1TestDriver itkDataObjectTest)
Expand Down
114 changes: 114 additions & 0 deletions Modules/Core/Common/test/itkPointSetToImageFilterTest1.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*=========================================================================
*
* 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
*
* http://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 "itkImageFileWriter.h"
#include "itkPointSetToImageFilter.h"
#include "itkPointSet.h"
#include "itkTestingMacros.h"


int
itkPointSetToImageFilterTest1(int argc, char * argv[])
{
if (argc != 2)
{
std::cerr << "Missing parameters." << std::endl;
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv);
std::cerr << " outputImageFile" << std::endl;
return EXIT_FAILURE;
}

constexpr unsigned int PointSetDimension = 2;
constexpr unsigned int ImageDimension = 2;

using PointSetPointType = float;
using PixelType = unsigned char;

using PointSetType = itk::PointSet<PointSetPointType, PointSetDimension>;
using BinaryImageType = itk::Image<PixelType, ImageDimension>;

using PointSetType = itk::PointSet<PointSetPointType, PointSetDimension>;

using PointType = PointSetType::PointType;

auto pointSet = PointSetType::New();

// Create a point set describing a circle
float radius = 100.0;
unsigned int count = 0;
for (float theta = 0; theta < 2.0 * itk::Math::pi; theta += 0.1)
{
PointType point;
point[0] = radius * std::cos(theta);
point[1] = radius * std::sin(theta);

pointSet->SetPoint(count, point);
count++;
}

using FilterType = itk::PointSetToImageFilter<PointSetType, BinaryImageType>;
auto filter = FilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, PointSetToImageFilter, ImageSource);


BinaryImageType::SpacingType::ValueType spacingValue = 1.0;
BinaryImageType::SpacingType spacing;
spacing.Fill(spacingValue);
filter->SetSpacing(spacing);
ITK_TEST_SET_GET_VALUE(spacing, filter->GetSpacing());

BinaryImageType::PointType origin{ { -125, -125 } };
filter->SetOrigin(origin);
ITK_TEST_SET_GET_VALUE(origin, filter->GetOrigin());

typename BinaryImageType::DirectionType direction;
direction.SetIdentity();
filter->SetDirection(direction);
ITK_TEST_SET_GET_VALUE(direction, filter->GetDirection());

typename BinaryImageType::ValueType insideValue = itk::NumericTraits<typename BinaryImageType::ValueType>::OneValue();
filter->SetInsideValue(insideValue);
ITK_TEST_SET_GET_VALUE(insideValue, filter->GetInsideValue());

typename BinaryImageType::ValueType outsideValue =
itk::NumericTraits<typename BinaryImageType::ValueType>::ZeroValue();
filter->SetOutsideValue(outsideValue);
ITK_TEST_SET_GET_VALUE(outsideValue, filter->GetOutsideValue());

typename BinaryImageType::SizeType size = { { 250, 250 } };
filter->SetSize(size);
ITK_TEST_SET_GET_VALUE(size, filter->GetSize());

filter->SetInput(pointSet);
ITK_TEST_SET_GET_VALUE(pointSet, filter->GetInput());

unsigned int idx = 0;
ITK_TEST_SET_GET_VALUE(pointSet, filter->GetInput(idx));

ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());


BinaryImageType::Pointer binaryImage = filter->GetOutput();

itk::WriteImage(binaryImage, argv[1]);


std::cout << "Test finished." << std::endl;
return EXIT_SUCCESS;
}
91 changes: 91 additions & 0 deletions Modules/Core/Common/test/itkPointSetToImageFilterTest2.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
*
* http://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 "itkImageFileWriter.h"
#include "itkPointSetToImageFilter.h"
#include "itkPointSet.h"
#include "itkTestingMacros.h"


int
itkPointSetToImageFilterTest2(int argc, char * argv[])
{
if (argc != 3)
{
std::cerr << "Missing parameters." << std::endl;
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv);
std::cerr << " pointSetFile outputImageFile" << std::endl;
return EXIT_FAILURE;
}

constexpr unsigned int PointSetDimension = 3;
constexpr unsigned int ImageDimension = 3;

using PointSetPointType = float;
using PixelType = unsigned char;

using PointSetType = itk::PointSet<PointSetPointType, PointSetDimension>;
using BinaryImageType = itk::Image<PixelType, ImageDimension>;

using PointSetType = itk::PointSet<PointSetPointType, PointSetDimension>;

auto pointSet = PointSetType::New();

using PointType = PointSetType::PointType;

using PointsContainer = PointSetType::PointsContainer;
auto pointContainer = PointsContainer::New();

PointType point;

// Read the point set
std::ifstream file;
file.open(argv[1]);
if (file.fail())
{
std::cerr << "Error opening point set file with name : " << std::endl;
std::cerr << argv[1] << std::endl;
return EXIT_FAILURE;
}

unsigned int pointId = 0;
file >> point;
while (!file.eof())
{
pointContainer->InsertElement(pointId, point);
file >> point;
pointId++;
}
pointSet->SetPoints(pointContainer);

using FilterType = itk::PointSetToImageFilter<PointSetType, BinaryImageType>;
auto filter = FilterType::New();

filter->SetInput(pointSet);

ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());


BinaryImageType::Pointer binaryImage = filter->GetOutput();

itk::WriteImage(binaryImage, argv[2]);


std::cout << "Test finished." << std::endl;
return EXIT_SUCCESS;
}
Loading

0 comments on commit 3adda5a

Please sign in to comment.