python - 使用 VTK Python API,将多个标量添加到非结构化网格单元

标签 python c++ vtk

这个 Python 脚本:

import numpy as np
import vtk
from vtk.util.numpy_support import numpy_to_vtk

# Open a file, and create an unstructured grid.
filename = 'example.vtk'
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName(filename)
grid = vtk.vtkUnstructuredGrid()

# Create 3 points
A,B,C = (0,0,0), (0,1,0), (1,0,0)
points = np.array( (A,B,C) )
vtk_points = vtk.vtkPoints()
vtk_points.SetData( numpy_to_vtk(points) )
grid.SetPoints(vtk_points)

# Cells: just 1 triangle
ntriangles = 1
npoints_per_triangle = 3
cells = np.array( [npoints_per_triangle, 0, 1, 2] )
vtk_cells = vtk.vtkCellArray()
id_array = vtk.vtkIdTypeArray()
id_array.SetVoidArray(cells, len(cells), 1)
vtk_cells.SetCells(ntriangles, id_array)

# Cell types: just 1 triangle.
cell_types = np.array( [vtk.VTK_TRIANGLE] , 'B')
vtk_cell_types = numpy_to_vtk(cell_types)

# Cell locations: the triangle is in `cells` at index 0.
cell_locations = np.array( [0,])
vtk_cell_locations = numpy_to_vtk(cell_locations, deep=1,
                                   array_type=vtk.VTK_ID_TYPE)

# Cells: add to grid
grid.SetCells(vtk_cell_types, vtk_cell_locations, vtk_cells)
data = grid.GetCellData()

# Add scalar data to the triangle
data.SetActiveScalars('foo')
foo = np.array( [11.,] )
vtk_foo = numpy_to_vtk(foo)
vtk_foo.SetName("foo")
data.SetScalars(vtk_foo)

# Add other scalar data to the triangle
data.SetActiveScalars('bar')
bar = np.array( [12.,] )
vtk_bar = numpy_to_vtk(bar)
vtk_bar.SetName("bar")
data.SetScalars(vtk_bar)

# Write to file.
writer.SetInput(grid)
writer.Write()

print open(filename).read()

生成文件:

# vtk DataFile Version 3.0
vtk output
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 3 long
0 0 0 0 1 0 1 0 0 

CELLS 1 4
3 0 1 2 

CELL_TYPES 1
5

CELL_DATA 1
SCALARS bar double
LOOKUP_TABLE default
12 
FIELD FieldData 1
foo 1 1 double
11 

但我希望 CELL_DATA 部分是:

CELL_DATA 1
SCALARS foo double
LOOKUP_TABLE default
11 
SCALARS bar double
LOOKUP_TABLE default
12 

编辑

查看源代码(WriteCellDataWriteScalarData 和更深层次),这似乎是不可能的。

最佳答案

您可以使用 AddArray 而不是 SetActiveScalars

添加您想要的数组数量

另见 http://public.kitware.com/pipermail/vtkusers/2004-August/026366.html

http://www.vtk.org/doc/nightly/html/classvtkCellData-members.html

关于python - 使用 VTK Python API,将多个标量添加到非结构化网格单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30596972/

相关文章:

C++/错误 : expression must have integral or unscoped enum type

c++ - OpenGL/VTK : setting camera intrinsic parameters

Python shutil copyfile - 缺少最后几行

python - Matplotlib - 动态绘图高度,水平条总是 1px 高度

python - Django 根据检索到的值进行过滤

c++ - 无法链接到 vtk 6.3/usr/bin/ld : cannot find -lvtkCommon

fortran - 来自 Fortran 代码的 RECTILINEAR_GRID 的二进制 VTK

python - 处理 Celery 任务代码中的错误的推荐方法是什么?

c++ - DllMain Attach 和 Detach 过程中的使用限制

c++ - Ogre Projector 透过物体发光