Skip to content

Commit

Permalink
ENH: Adding Python test for itkPointsLocator
Browse files Browse the repository at this point in the history
  • Loading branch information
PranjalSahu committed Jul 14, 2022
1 parent a4002b1 commit ba3c191
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Modules/Registration/Common/wrapping/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if(ITK_WRAP_PYTHON)
itk_python_add_test(NAME itkPointsLocatorPythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkPointsLocatorTest.py)
endif()
50 changes: 50 additions & 0 deletions Modules/Registration/Common/wrapping/test/itkPointsLocatorTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ==========================================================================
#
# 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.
#
# ==========================================================================

# For testing itk PointsLocator in Python
import itk

# Create points on a plane
pointset = itk.PointSet[itk.F, 3].New()
pointset.SetPoint(0, [0, 0, 0])
pointset.SetPoint(1, [0, 1, 0])
pointset.SetPoint(2, [1, 0, 0])
pointset.SetPoint(3, [1, 1, 0])

# Create PointsLocator
pl = itk.PointsLocator.New()
pl.SetPoints(pointset.GetPoints())
pl.Initialize()

# Query tree for one point
vl = itk.VectorContainer[itk.UL, itk.UL].New()
pl.FindClosestNPoints(pointset.GetPoint(0), 3, vl)
assert vl.Size() == 3
assert vl.GetElement(0) == 0
assert vl.GetElement(1) == 1
assert vl.GetElement(2) == 2

# Check points within radius
pl.FindPointsWithinRadius(pointset.GetPoint(0), 2, vl)
assert vl.Size() == 4

pl.FindPointsWithinRadius(pointset.GetPoint(0), 1.1, vl)
assert vl.Size() == 3
assert vl.GetElement(0) == 0
assert vl.GetElement(1) == 1
assert vl.GetElement(2) == 2

0 comments on commit ba3c191

Please sign in to comment.