Write mesh data (cell data) with python - it is not working?

Hi guys,
I’m trying to write the mesh data with point and cell data using python (based on C++).
This is my Python codes:

from itk import BinaryMask3DMeshSource

MeshType = itk.Mesh[itk.F, 3]
ImageType = itk.Image[itk.UC, 3]

MeshSourceType = itk.BinaryMask3DMeshSource[ImageType, MeshType]
meshSource = MeshSourceType.New()
meshSource.SetObjectValue(objectValue)
meshSource.SetInput(inputImage)

NodeNum = meshSource.GetNumberOfNodes()
CellNum = meshSource.GetNumberOfCells()
print("\tNodes =", NodeNum)
print("\tCells =", CellNum)

mesh = meshSource.GetOutput()

outputMeshFile = 'a.dat'
OutMesh = open(outputMeshFile, 'w')

points = mesh.GetPoints()
cells = mesh.GetCells()
numCells = mesh.GetNumberOfCells()

#it works for points
for i in range(points.Size()):
    p = points.ElementAt(i)
    OutMesh.write(str(p[0]) + " " + str(p[1]) + " " + str(p[2]) + "\n")
    points.SetElement(i, p)
    
    
for i in range(numCells):
    cell = cells.GetElement(i)
    numPts = cell.GetNumberOfPoints()
    point_ids = cell.GetPointIds()

    line = ""
    for j in range(numPts):
        line += str(point_ids.GetId(j) + 1) + " "
    OutMesh.write(line + "\n")

OutMesh.close()

I got the error like this:

---> 21         line += str(point_ids.GetId(j) + 1) + " "
     22     OutMesh.write(line + "\n")
     25 OutMesh.close()

AttributeError: 'SwigPyObject' object has no attribute 'GetId'

I follow CellIterator from this examplehttps://itk.org/Doxygen/html/Examples_2DataRepresentation_2Mesh_2MeshCellsIteration_8cxx-example.html.
In C++, it is like this:

    while(PointIterator != points->End() )
    {
       PointType p=(PointIterator++).Value();
       OutMesh<<p[0]<<" "<<p[1]<<" "<<p[2]<<std::endl;
    }

    while(cellIterator != end)  
    {
       CellType *cell=cellIterator.Value();

       TriangleType *triangle= dynamic_cast<TriangleType *>(cell);
       TriangleType::PointIdIterator pit=triangle->PointIdsBegin();

       while(pit !=triangle->PointIdsEnd())
	  OutMesh<<(*pit++)+1<<" ";
	  OutMesh<<std::endl;
      ++cellIterator;
    }
    OutMesh.close();

Do you have any idea?

Thank you,

@Pranjal_Sahu is there some example or unit test in Python which does this, or something similar?

Hi there,

All you need is:

output_mesh = itk.binary_mask3_d_mesh_source(input_image)
itk.meshwrite(output_mesh, args.output_mesh)

Here is a full example:

Thanks @matt.mccormick ,
I did the same as you mentioned,

output_mesh = itk.binary_mask3_d_mesh_source(inputImage)
itk.meshwrite(output_mesh,'a.vtk') #cannot write .dat file

It wrote the “a.vtk” mesh file with the information:

# vtk DataFile Version 2.0
File written by itkPolyDataMeshIO
ASCII
DATASET POLYDATA

And it does not include any points and cells.
Could you guide me more?

It may be helpful to verify that there is content in the mesh before writing, e.g.:

print(output_mesh.GetNumberOfCells())
print(output_mesh.GetNumberOfPoints())

hi @matt.mccormick ,
As you can see from my first post:

MeshSourceType = itk.BinaryMask3DMeshSource[ImageType, MeshType]
meshSource = MeshSourceType.New()
meshSource.SetObjectValue(objectValue)
meshSource.SetInput(inputImage)

NodeNum = meshSource.GetNumberOfNodes()
CellNum = meshSource.GetNumberOfCells()
print("\tNodes =", NodeNum)
print("\tCells =", CellNum)

The output is:

	Nodes = 119740
	Cells = 239488

Thanks,

Hi,
I fixed it by following this example:https://github.com/InsightSoftwareConsortium/ITKSphinxExamples/blob/master/src/Core/Mesh/AddPointsAndEdges/Code.py

for i in range(numCells):
    cell = cells.GetElement(i)
    numPts = cell.GetNumberOfPoints()
    point_ids = cell.GetPointIdsContainer()
    for j in range(num_pts):
        line += str(point_ids[j] + 1) + " "
    OutMesh.write(line + "\n")

Thank you =))

2 Likes