2011-12-20 33 views
2

を使用して、複数の従来のASCII .vtkファイルを1つのファイルに結合します。計算中に、1つのタイムステップに関連する.vtkファイルを保存します。すべてのファイルは、POLYDATA ASCIIファイル形式を使用して多面体(私が定義したC++クラス)を記述します。多面体は、多面体クラスの単純なメンバ関数によって記述されます。python-vtk

多面体コレクションの完全な新しいクラスを定義するのを避けるために、私のC++コードを視覚化して混乱させる必要があるため、複数の.vtkファイルを1つの.vtkファイルに結合したいと考えています。私は重複したポイントを持つことになります。これにより

from vtk import * 

reader = vtkPolyDataReader() 
reader.SetFileName("file1.vtk") 

reader.Update() 
polyData1 = reader.GetOutput() 

reader.SetFileName('file2.vtk') 

reader.Update() 
polyData2 = reader.GetOutput() 


# Expand the output points 

points1 = polyData1.GetPoints() 

points2 = polyData2.GetPoints() 

insertPosition = points1.GetNumberOfPoints() 

for i in xrange(points2.GetNumberOfPoints()): 
    insertPoint = points2.GetPoint(i) 
    points1.InsertPoint(insertPosition, 
         insertPoint[0], insertPoint[1], insertPoint[2]) 
    insertPosition += 1 

print points1.GetNumberOfPoints() 

# Change the cell ids of every cell in the polydata2 to correspond with 
# the new points (appended point array) 

increment = points1.GetNumberOfPoints(); 

for i in xrange(polyData2.GetNumberOfCells()): 
    cell = polyData2.GetCell(i) 
    cellIds = cell.GetPointIds() 
    for j in xrange(cellIds.GetNumberOfIds()): 
     oldId = cellIds.GetId(j) 
     cellIds.SetId(j, oldId + increment) 


polyData1.Allocate(polyData1.GetNumberOfCells(), 1) 

for i in xrange(polyData2.GetNumberOfCells()): 
    cell = polyData2.GetCell(i) 
    polyData1.InsertNextCell(cell.GetCellType(), cell.GetPointIds()) 


writer = vtkPolyDataWriter() 
writer.SetFileName("output.vtk") 
writer.SetInput(polyData1) 
writer.Write() 

、それはo.k.です:のpython-VTKを使用して

は私にいくつかの問題が発生します問題は、このスクリプトは、以下.vtkファイル上で実行されることである。

FILE1:

# vtk DataFile Version 2.0 
surface written 2011-12-19T15:30:18 
ASCII 

DATASET POLYDATA 
POINTS 8 float 
0.48999999999999999112 0.4000000000000000222 0.5999999999999999778 
0.48999999999999999112 0.5 0.5999999999999999778 
0.48999999999999999112 0.5 0.69999999999999995559 
0.48999999999999999112 0.4000000000000000222 0.69999999999999995559 
0.5 0.5 0.5999999999999999778 
0.5 0.5 0.69999999999999995559 
0.5 0.4000000000000000222 0.69999999999999995559 
0.5 0.4000000000000000222 0.5999999999999999778 

POLYGONS 6 30 
4 0 1 2 3 
4 4 5 6 7 
4 4 1 2 5 
4 6 5 2 3 
4 7 0 1 4 
4 0 7 6 3 

CELL_DATA 6 
FIELD attributes 1 
zone 1 6 float 
1 1 1 1 1 1 

FILE2:

# vtk DataFile Version 2.0 
surface written 2011-12-19T15:30:18 
ASCII 

DATASET POLYDATA 
POINTS 8 float 
0.58999999999999996891 0.5999999999999999778 0.5 
0.58999999999999996891 0.69999999999999995559 0.5 
0.58999999999999996891 0.69999999999999995559 0.5999999999999999778 
0.58999999999999996891 0.5999999999999999778 0.5999999999999999778 
0.5999999999999999778 0.69999999999999995559 0.5 
0.5999999999999999778 0.69999999999999995559 0.5999999999999999778 
0.5999999999999999778 0.5999999999999999778 0.5999999999999999778 
0.5999999999999999778 0.5999999999999999778 0.5 

POLYGONS 6 30 
4 0 1 2 3 
4 4 5 6 7 
4 4 1 2 5 
4 6 5 2 3 
4 7 0 1 4 
4 0 7 6 3 

CELL_DATA 6 
FIELD attributes 1 
zone 1 6 float 
1 1 1 1 1 1 

8点の座標及び細胞(面)のように持つ点での結果であること全く追加されません。

vtkArrayとそれに類するvtkObjectsのPythonラッパーには、繰り返し処理するオプションがありませんか?

答えて

2

は、私はここで仕事をして、小さな速い書かれたクラスは、誰かがそれが役に立つかもしれないですが、この目的のためのpython-VTK APIをあきらめた:

class polyDataVtk(object): 
    """Class representing the polydata vtk information stored in legacy ASCII .vtk POLYDATA files.""" 
    def __init__(self, fileName = None): 
     self.__points = [] 
     self.__polygons = [] 
     if fileName is not None: 
      self.__fileName = fileName 

    def parse(self, fileName): 
     """Parse the POLYDATA information from a .vtk file and append the data to the object. 
      Does not check for the file consistency.""" 
     file = open(fileName, 'r') 

     # Use local data first. 
     points = [] 
     polygons = [] 

     line = "" 
     while(True): 
      line = file.readline() 
      if 'POINTS' in line: 
       break 

     nPoints = 0 

     if (line == ""): 
      print "No POINTS defined in the .vtk file" 
      return 

     # Set the number of points 
     nPoints = int(line.split()[1]) 

     # Append the numbers. 
     for i in xrange(nPoints): 
      points.append(map(lambda x : float(x), file.readline().split())) 

     # Append polygons. 
     line = "" 

     while(True): 
      line = file.readline() 
      if 'POLYGONS' in line: 
       break 
     if (line == ""): 
      print "No POLYGONS defined in the .vtk file" 
      return 
     # Set the number of polygons. 
     nPolygons = int(line.split()[1]) 

     # Read the polygons. 
     for i in xrange(nPolygons): 
      line = file.readline() 
      polygons.append(map(lambda x : int(x) + len(self.__points), line.split())[1:]) 

     # File parsed without a problem. 
     self.__points.extend(points) 
     self.__polygons.extend(polygons) 
     file.close() 

    def write(self,fileName=None, append=False): 
     # Overwrite the filename provided to the constructor. 
     if fileName is not None: 
      self.__fileName = fileName 

     # No fileName is provided by the constructor or the write method. 
     if self.__fileName is None: 
      self.__fileName = "result.vtk" 

     # Append or overwrite? 
     if append: 
      file = open(self.__fileName, 'a') 
     else: 
      file = open(self.__fileName, 'w') 

     file.writelines("# vtk DataFile Version 2.0\n") 
     file.writelines("appended vtk files\n") 
     file.writelines("ASCII\n") 
     file.writelines("DATASET POLYDATA\n") 

     file.writelines("POINTS %d float \n" % len(self.__points)) 

     for point in self.__points: 
      file.writelines("%.10f %.10f %.10f\n" % (point[0], point[1], point[2])) 

     size = 0 
     for polygon in self.__polygons: 
      size += len(polygon) 
     file.writelines("POLYGONS %d %d \n" % (len(self.__polygons), 
               size 
              + len(self.__polygons))) 

     for polygon in self.__polygons: 
      file.writelines("%d " % len(polygon)) 
      for label in polygon: 
       # I don't know how many labels are there in general. 
       file.writelines("%d " % label) 
      file.writelines("\n") 

     file.close() 

    def fileName(self): 
     return self.__fileName 
1

あなたがファイルをマージするvtkAppendPolyDataを使用することを検討すべきです。

また、リーダーをもう一度呼び出すと、最初の入力ファイルの出力データセットが2番目の入力ファイルで上書きされます。同じリーダーを使用する場合は、ShallowCopy(上記のスクリプトのように)を実行する必要があります。