|
| 1 | +# ========================================================================== |
| 2 | +# |
| 3 | +# Copyright NumFOCUS |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# ==========================================================================*/ |
| 18 | + |
| 19 | +import itk |
| 20 | +import unittest |
| 21 | +import numpy as np |
| 22 | + |
| 23 | +class PointSetTestCase(unittest.TestCase): |
| 24 | + """Tests itk.PointSet""" |
| 25 | + |
| 26 | + def test_SetPointsByCoordinates_from_NumPy_array(self) -> None: |
| 27 | + """Tests the SetPointsByCoordinates method, having a NumPy array as input""" |
| 28 | + |
| 29 | + # The pixel type is irrelevant for this test, just use unsigned char. |
| 30 | + PixelType = itk.UC |
| 31 | + |
| 32 | + # Test the most common dimensions, and a few different numbers as number of points. |
| 33 | + for dimension in [2, 3]: |
| 34 | + for number_of_points in [1, 4, 9]: |
| 35 | + # Create a NumPy array of random coordinates between -1.0 and 1.0 |
| 36 | + np_array = 2.0 * np.random.rand(number_of_points * dimension) - 1.0 |
| 37 | + |
| 38 | + point_set = itk.PointSet[PixelType, dimension].New() |
| 39 | + point_set.SetPointsByCoordinates(np_array) |
| 40 | + |
| 41 | + points = point_set.GetPoints() |
| 42 | + |
| 43 | + self.assertEqual(points.Size(), number_of_points) |
| 44 | + |
| 45 | + for i in range(number_of_points): |
| 46 | + point = points.GetElement(i) |
| 47 | + for j in range(dimension): |
| 48 | + self.assertAlmostEqual(point[j], np_array[i * dimension + j]) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + unittest.main() |
0 commit comments