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,