diff --git a/Modules/Segmentation/Voronoi/include/itkVoronoiDiagram2DGenerator.hxx b/Modules/Segmentation/Voronoi/include/itkVoronoiDiagram2DGenerator.hxx index fb56ba15d58..8f9534f9218 100644 --- a/Modules/Segmentation/Voronoi/include/itkVoronoiDiagram2DGenerator.hxx +++ b/Modules/Segmentation/Voronoi/include/itkVoronoiDiagram2DGenerator.hxx @@ -274,8 +274,10 @@ VoronoiDiagram2DGenerator::ConstructDiagram() buildEdges.push_back(curr); EdgeInfo front = curr; EdgeInfo back = curr; - while (!(rawEdges[i].empty())) + auto maxStop = rawEdges[i].size(); + while (!(rawEdges[i].empty()) && (maxStop != 0)) { + --maxStop; curr = rawEdges[i].front(); rawEdges[i].pop_front(); unsigned char frontbnd = Pointonbnd(front[0]); diff --git a/Modules/Segmentation/Voronoi/test/CMakeLists.txt b/Modules/Segmentation/Voronoi/test/CMakeLists.txt index c1a263e918e..5924f267c8c 100644 --- a/Modules/Segmentation/Voronoi/test/CMakeLists.txt +++ b/Modules/Segmentation/Voronoi/test/CMakeLists.txt @@ -5,6 +5,7 @@ set( itkVoronoiSegmentationRGBImageFilterTest.cxx itkVoronoiDiagram2DTest.cxx itkVoronoiPartitioningImageFilterTest.cxx + itkVoronoiDiagram2DInfiniteLoopTest.cxx ) createtestdriver(ITKVoronoi "${ITKVoronoi-Test_LIBRARIES}" "${ITKVoronoiTests}") @@ -29,6 +30,13 @@ itk_add_test( ITKVoronoiTestDriver itkVoronoiSegmentationRGBImageFilterTest ) +itk_add_test( + NAME itkVoronoiDiagram2DInfiniteLoopTest + COMMAND + ITKVoronoiTestDriver + itkVoronoiDiagram2DInfiniteLoopTest +) + itk_add_test( NAME itkVoronoiDiagram2DTest COMMAND diff --git a/Modules/Segmentation/Voronoi/test/itkVoronoiDiagram2DInfiniteLoopTest.cxx b/Modules/Segmentation/Voronoi/test/itkVoronoiDiagram2DInfiniteLoopTest.cxx new file mode 100644 index 00000000000..0bc337c22ec --- /dev/null +++ b/Modules/Segmentation/Voronoi/test/itkVoronoiDiagram2DInfiniteLoopTest.cxx @@ -0,0 +1,48 @@ +/*========================================================================= + * + * 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 "itkVoronoiDiagram2DGenerator.h" +#include "itkTestingMacros.h" + +int +itkVoronoiDiagram2DInfiniteLoopTest(int argc, char * argv[]) +{ + if (argc != 1) + { + std::cerr << "Takes no parameters, but " << argc << "parameters given" << std::endl; + std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << std::endl; + return EXIT_FAILURE; + } + + // From https://github.com/InsightSoftwareConsortium/ITK/issues/4386 + using VoronoiDiagramType = itk::VoronoiDiagram2D; + using VoronoiGeneratorType = itk::VoronoiDiagram2DGenerator; + using PointType = VoronoiDiagramType::PointType; + VoronoiGeneratorType::Pointer vg = VoronoiGeneratorType::New(); + vg->SetOrigin(PointType{ { -1.61569, -1.76726 } }); + vg->SetBoundary(PointType{ { 1.60174, 1.76345 } }); + vg->AddOneSeed(PointType{ { -1.39649, 0.322212 } }); + vg->AddOneSeed(PointType{ { -1.30128, 0.231786 } }); + vg->AddOneSeed(PointType{ { -1.21509, 0.0515039 } }); + vg->AddOneSeed(PointType{ { -1.22364, -0.030281 } }); + vg->AddOneSeed(PointType{ { -1.22125, -0.120815 } }); + vg->AddOneSeed(PointType{ { -1.25159, -0.23593 } }); + vg->Update(); + + return EXIT_SUCCESS; +}