Skip to content

Commit d85bb67

Browse files
PranjalSahuhjmjohnson
authored andcommitted
ENH: Adding Test for Array PixelType in Mesh Wrapping
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.
1 parent 70351d1 commit d85bb67

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Modules/Core/Mesh/wrapping/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ if(ITK_WRAP_PYTHON)
2525
EXPRESSION "instance = itk.RegularSphereMeshSource.New()")
2626
itk_python_expression_add_test(NAME itkSphereMeshSourcePythonTest
2727
EXPRESSION "instance = itk.SphereMeshSource.New()")
28+
29+
itk_python_add_test(NAME itkMeshArrayPixelTypePythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkMeshArrayPixelTypeTest.py)
2830
endif()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
# http://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+
import itk
19+
import numpy as np
20+
21+
Dimension = 3
22+
PixelType = itk.Array.D
23+
NumberOfPoints = 10
24+
PixelDataSize = 5
25+
26+
MeshType = itk.Mesh[PixelType, Dimension]
27+
mesh = MeshType.New()
28+
29+
# Create Vector Container and Store values in it for each Point
30+
v = itk.VectorContainer[itk.UL, PixelType].New()
31+
v.Reserve(NumberOfPoints)
32+
33+
for i in range(NumberOfPoints):
34+
pixel_data_reference = v.CreateElementAt(i)
35+
pixel_data_reference.SetSize(PixelDataSize)
36+
pixel_data_reference.Fill(0)
37+
pixel_data_reference[0] = i
38+
pixel_data_reference[4] = i+4
39+
40+
# Set the point data container
41+
mesh.SetPointData(v)
42+
43+
assert mesh.GetPointData().Size() == NumberOfPoints
44+
assert mesh.GetPointData().ElementAt(0)[0] == 0
45+
assert mesh.GetPointData().ElementAt(0)[4] == 4
46+
assert mesh.GetPointData().ElementAt(2)[0] == 2+0
47+
assert mesh.GetPointData().ElementAt(2)[4] == 2+4
48+
49+
# resize the PixelDataSize to see if it can be altered succesfully
50+
PixelDataSize = 10
51+
for i in range(NumberOfPoints):
52+
pixel_data_reference = v.CreateElementAt(i)
53+
pixel_data_reference.SetSize(PixelDataSize)
54+
pixel_data_reference.Fill(0)
55+
pixel_data_reference[0] = i
56+
pixel_data_reference[9] = i+10
57+
58+
assert mesh.GetPointData().Size() == NumberOfPoints
59+
assert mesh.GetPointData().ElementAt(0)[0] == 0
60+
assert mesh.GetPointData().ElementAt(0)[9] == 10
61+
assert mesh.GetPointData().ElementAt(2)[0] == 2+0
62+
assert mesh.GetPointData().ElementAt(2)[9] == 2+10

0 commit comments

Comments
 (0)