我需要在python中用固体四面体写一个xml vtk文件。特别是在Polydata中。我有一组四面体,每条边有x,y,z坐标。我尝试使用vtk.vtkXMLPolyDataWriter(),但我得到了这个(只有2D三角形):
我的代码:
#!/usr/bin/env python
import vtk
points = vtk.vtkPoints()
points.InsertNextPoint(0, 0, 0)
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(1, 1, 0)
points.InsertNextPoint(0, 1, 1)
tetra = vtk.vtkTetra()
tetra.GetPointIds().SetId(0, 0)
tetra.GetPointIds().SetId(1, 1)
tetra.GetPointIds().SetId(2, 2)
tetra.GetPointIds().SetId(3, 3)
cellArray = vtk.vtkCellArray()
cellArray.InsertNextCell(tetra)
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetPolys(cellArray)
polydata.Modified()
if vtk.VTK_MAJOR_VERSION <= 5:
polydata.Update()
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName("Primal_geo.vtp");
if vtk.VTK_MAJOR_VERSION <= 5:
writer.SetInput(polydata)
else:
writer.SetInputData(polydata)
writer.Write()
那么,我怎么写这个文件? 谢谢!
答案 0 :(得分:0)
XML is information in tags。这是一种自由的手json。 所以我们走了。
现在让我首先说我不熟悉vtk或它的功能,所以虽然我希望我在这里提供的代码符合您的目的,但我不确定是否存在更令人满意的(预先存在的vtk片段) 。
那说我写了这个小东西。
#!/usr/bin/env python
import vtk
class vtkXMLparser:
points = None
scellArray = None
def tagSplit(self,line):
for n,k in enumerate(line):
go = none
fin = none
if(k == ">"):
if(go == none):
if(n>0):
if(line[n-1]!="\\"):
go = n+1
if(go != none):
if(k == "<"):
if(n>0):
if(line[n-1]!="\\"):
if(n<len(line)-1):
if(line[n+1]=="/"):
fin = n+1
tag = line[fin+2:len(line)-1]
content = line[go:fin]
return([tag,content])
def addNice(self,di,key,cont):
try:
di[key].append(cont)
except(KeyError):
di[key]=[]
di[key].append(cont)
def parse(self,f):
input = open(f,"r")
xml = input.read()
xml = xml.split("\n")
data = {}
for lineNumber,line in enumerate(xml):
parsedXmlLine = self.tagSplit(line)
self.addNice(data,parsedXmlLine[0],parsedXmlLine[1])
self.self.points(data)
self.tetra(data)
self.write()
def self.points(self,di):
self.points = vtk.vtkself.points()
for point in di["point"]:
self.points.InsertNextPoint(eval(point))
def tetra(self,di):
tetra = vtk.vtkTetra()
for id in di["tetra"]:
tetra.GetPointIds().SetId(eval(id))
self.cellArray = vtk.vtkself.cellArray()
self.cellArray.InsertNextCell(tetra)
def write(self):
polydata = vtk.vtkPolyData()
polydata.Setself.points(self.points)
polydata.SetPolys(self.cellArray)
polydata.Modified()
if vtk.VTK_MAJOR_VERSION <= 5:
polydata.Update()
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName("Primal_geo.vtp");
if vtk.VTK_MAJOR_VERSION <= 5:
writer.SetInput(polydata)
else:
writer.SetInputData(polydata)
writer.Write()
i = vtkXMLparser()
i.parse("input.xml")
我会为你的例子编写一个看起来像这样的输入文件。
<point>0, 0, 0</point>
<point>1, 0, 0</point>
<point>1, 1, 0</point>
<point>0, 1, 1</point>
<tetra>0, 0</tetra>
<tetra>1, 1</tetra>
<tetra>2, 2</tetra>
<tetra>3, 3</tetra>
我希望这有帮助!