ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Core/Mesh/AddPointsAndEdges/Code.py
1 #!/usr/bin/env python
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 import itk
19 import numpy as np
20 
21 Dimension = 3
22 MeshType = itk.Mesh[itk.F, Dimension]
23 
24 
25 def CreatePointOnlyMesh():
26  mesh = MeshType.New()
27 
28  # Create points
29  p0 = [-1.0, -1.0, 0.0] # first point ( -1, -1, 0 )
30  p1 = [1.0, -1.0, 0.0] # second point ( 1, -1, 0 )
31  p2 = [1.0, 1.0, 0.0] # third point ( 1, 1, 0 )
32  p3 = [1.0, 1.0, 1.0] # fourth point ( 1, 1, 1 )
33 
34  mesh.SetPoint(0, p0)
35  mesh.SetPoint(1, p1)
36  mesh.SetPoint(2, p2)
37  mesh.SetPoint(3, p3)
38 
39  print("Points = ", mesh.GetNumberOfPoints())
40 
41  # Access points
42  points = mesh.GetPoints()
43  for i in range(points.Size()):
44  p = mesh.GetPoint(i)
45  print(p)
46 
47  return mesh
48 
49 
50 def CreateMeshWithEdges():
51  mesh = CreatePointOnlyMesh()
52 
53  # Point Ids for the two cells
54  pointId0 = 0
55  pointId1 = 1
56 
57  pointId2 = 1
58  pointId3 = 2
59 
60  # Create cells array and insert points in them
61  # Each cell is of line type here
62  cells_array = np.zeros(4, dtype=np.uint64)
63  cells_array[0] = pointId0
64  cells_array[1] = pointId1
65 
66  cells_array[2] = pointId2
67  cells_array[3] = pointId3
68 
69  cells_vector = itk.vector_container_from_array(cells_array.flatten())
70 
71  # When all cells are same use the second arguement to pass the cell type
72  mesh.SetCellsArray(cells_vector, itk.CommonEnums.CellGeometry_LINE_CELL)
73 
74  number_of_cells = mesh.GetNumberOfCells()
75  all_cells = mesh.GetCells()
76 
77  # Iterate over the cells and print the points present in them
78  for i in range(number_of_cells):
79  current_cell = all_cells.GetElement(i)
80  point_ids = current_cell.GetPointIdsContainer()
81  print(point_ids)
82 
83 
84 CreateMeshWithEdges()
itk::Mesh
Implements the N-dimensional mesh structure.
Definition: itkMesh.h:126