Skip to content

Commit

Permalink
ENH: Adding Test for Array PixelType in Mesh Wrapping
Browse files Browse the repository at this point in the history
Added test in Python to demonstrate the usage of Array PixelType in Mesh.
Usage of Array allows to change the size of pixeldata as per requirements later.
Using Array, however breaks the API support for SetElement and SetPointData in PointSet.
The only way to alter the data is through reference and for that CreateElementAt method is
used in the VectorContainer.
  • Loading branch information
PranjalSahu authored and hjmjohnson committed Dec 15, 2021
1 parent 70351d1 commit d85bb67
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Modules/Core/Mesh/wrapping/test/CMakeLists.txt
Expand Up @@ -25,4 +25,6 @@ if(ITK_WRAP_PYTHON)
EXPRESSION "instance = itk.RegularSphereMeshSource.New()")
itk_python_expression_add_test(NAME itkSphereMeshSourcePythonTest
EXPRESSION "instance = itk.SphereMeshSource.New()")

itk_python_add_test(NAME itkMeshArrayPixelTypePythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkMeshArrayPixelTypeTest.py)
endif()
62 changes: 62 additions & 0 deletions Modules/Core/Mesh/wrapping/test/itkMeshArrayPixelTypeTest.py
@@ -0,0 +1,62 @@
# ==========================================================================
#
# 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.
#
# ==========================================================================*/
import itk
import numpy as np

Dimension = 3
PixelType = itk.Array.D
NumberOfPoints = 10
PixelDataSize = 5

MeshType = itk.Mesh[PixelType, Dimension]
mesh = MeshType.New()

# Create Vector Container and Store values in it for each Point
v = itk.VectorContainer[itk.UL, PixelType].New()
v.Reserve(NumberOfPoints)

for i in range(NumberOfPoints):
pixel_data_reference = v.CreateElementAt(i)
pixel_data_reference.SetSize(PixelDataSize)
pixel_data_reference.Fill(0)
pixel_data_reference[0] = i
pixel_data_reference[4] = i+4

# Set the point data container
mesh.SetPointData(v)

assert mesh.GetPointData().Size() == NumberOfPoints
assert mesh.GetPointData().ElementAt(0)[0] == 0
assert mesh.GetPointData().ElementAt(0)[4] == 4
assert mesh.GetPointData().ElementAt(2)[0] == 2+0
assert mesh.GetPointData().ElementAt(2)[4] == 2+4

# resize the PixelDataSize to see if it can be altered succesfully
PixelDataSize = 10
for i in range(NumberOfPoints):
pixel_data_reference = v.CreateElementAt(i)
pixel_data_reference.SetSize(PixelDataSize)
pixel_data_reference.Fill(0)
pixel_data_reference[0] = i
pixel_data_reference[9] = i+10

assert mesh.GetPointData().Size() == NumberOfPoints
assert mesh.GetPointData().ElementAt(0)[0] == 0
assert mesh.GetPointData().ElementAt(0)[9] == 10
assert mesh.GetPointData().ElementAt(2)[0] == 2+0
assert mesh.GetPointData().ElementAt(2)[9] == 2+10

0 comments on commit d85bb67

Please sign in to comment.