简体   繁体   English

如何在vtk中可视化二维数组

[英]How to visualize 2d array of doubles in vtk?

I have 2d array (of size 20x30) of doubles: 我有二维的双精度数组(大小为20x30):

double a[20*30];

How to visualize it using VTK? 如何使用VTK对其进行可视化? It is extremely difficult to find proper documentation. 很难找到适当的文档。 The closest example which I found is this , however it uses as input 3 unsigned chars which represents color. 我发现的最接近的示例是this ,但是它使用3个代表颜色的无符号字符作为输入。 As I understand I should use vtkScalarsToColors class to somehow map scalars to colors, but I can't figure out how to put everything into single piece of code. 据我了解,我应该使用vtkScalarsToColors类以某种方式将标量映射到颜色,但是我不知道如何将所有内容放入单个代码中。

What you probably want to do is to assign scalars to points or cells of a surface or volume mesh. 您可能要做的是将标量分配给曲面或体积网格的点或单元。 VTK then can take care of the visualization. 然后,VTK可以处理可视化。 This is demonstrated in the following example: ScalarBarActor . 在以下示例中对此进行了演示: ScalarBarActor For the basic usage, follow the Scalars example. 对于基本用法,请遵循Scalars示例。

However, you need to provide the suitable mesh yourself to which you want to map the values. 但是,您需要自己提供要将值映射到的合适的网格。 From your question, it is not entirely clear what you mean with "how to visualize a 2d array" of values. 从您的问题来看,“值如何可视化二维数组”的含义并不完全清楚。 If you want to assign scalar values in a planar 20x30 grid, you need to first create a surface object (of type vtkPolyData ) with triangular or quadrangular cells, and then assign the values to the points of the mesh using surface->GetPointData()->SetScalars() , as demonstrated in the above examples. 如果要在平面20x30网格中分配标量值,则需要首先创建具有三角形或四边形像元的表面对象(类型为vtkPolyData ),然后使用surface->GetPointData()->SetScalars()将值分配给网格的点surface->GetPointData()->SetScalars() ,如以上示例所示。

Convenient in this case would be the vtkPlaneSource , look here for the corresponding example. 在这种情况下,很方便的是vtkPlaneSource在这里查看相应的示例。 The number of grid points you can set by using SetXResolution() or SetYResolution() respectively. 您可以分别使用SetXResolution()SetYResolution()设置的网格点数。 (In case this is not clear: vtkPlaneSource inherits vtkPolyDataAlgorithm , to access the underlying vtkPolyData object, use the method GetOutput() ) (如果尚不清楚: vtkPlaneSource继承vtkPolyDataAlgorithm ,要访问基础vtkPolyData对象,请使用方法GetOutput()


Update : I added sample code that demonstrates the procedure - in python, for better readability. 更新 :我添加了示例代码来演示该过程-使用python,以提高可读性。

# This code has been written by normanius under the CC BY-SA 4.0 license.
# License:    https://creativecommons.org/licenses/by-sa/4.0/
# Author:     normanius: https://stackoverflow.com/users/3388962/normanius
# Date:       August 2018
# Reference:  https://stackoverflow.com/a/51754466/3388962

import vtk
import numpy as np

###########################################################
# CREATE ARRAY VALUES
###########################################################
# Just create some fancy looking values for z.
n = 100
m = 50
xmin = -1; xmax = 1
ymin = -1; ymax = 1
x = np.linspace(xmin, xmax, n)
y = np.linspace(ymin, ymax, m)
x, y = np.meshgrid(x, y)
x, y = x.flatten(), y.flatten()
z = (x+y)*np.exp(-3.0*(x**2+y**2))

###########################################################
# CREATE PLANE
###########################################################
# Create a planar mesh of quadriliterals with nxm points.
# (SetOrigin and SetPointX only required if the extent
# of the plane should be the same. For the mapping
# of the scalar values, this is not required.)
plane = vtk.vtkPlaneSource()
plane.SetResolution(n-1,m-1)
plane.SetOrigin([xmin,ymin,0])  # Lower left corner
plane.SetPoint1([xmax,ymin,0])
plane.SetPoint2([xmin,ymax,0])
plane.Update()

# Map the values to the planar mesh.
# Assumption: same index i for scalars z[i] and mesh points
nPoints = plane.GetOutput().GetNumberOfPoints()
assert(nPoints == len(z))
# VTK has its own array format. Convert the input
# array (z) to a vtkFloatArray.
scalars = vtk.vtkFloatArray()
scalars.SetNumberOfValues(nPoints)
for i in range(nPoints):
    scalars.SetValue(i, z[i])
# Assign the scalar array.
plane.GetOutput().GetPointData().SetScalars(scalars)

###########################################################
# WRITE DATA
###########################################################
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName('output.vtp')
writer.SetInputConnection(plane.GetOutputPort())
writer.Write() # => Use for example ParaView to see scalars

###########################################################
# VISUALIZATION
###########################################################
# This is a bit annoying: ensure a proper color-lookup.
colorSeries = vtk.vtkColorSeries()
colorSeries.SetColorScheme(vtk.vtkColorSeries.BREWER_DIVERGING_SPECTRAL_10)
lut = vtk.vtkColorTransferFunction()
lut.SetColorSpaceToHSV()
nColors = colorSeries.GetNumberOfColors()
zMin = np.min(z)
zMax = np.max(z)
for i in range(0, nColors):
    color = colorSeries.GetColor(i)
    color = [c/255.0 for c in color]
    t = zMin + float(zMax - zMin)/(nColors - 1) * i
    lut.AddRGBPoint(t, color[0], color[1], color[2])

# Mapper.
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(plane.GetOutputPort())
mapper.ScalarVisibilityOn()
mapper.SetScalarModeToUsePointData()
mapper.SetLookupTable(lut)
mapper.SetColorModeToMapScalars()
# Actor.
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# Renderer.
renderer = vtk.vtkRenderer()
renderer.SetBackground([0.5]*3)
# Render window and interactor.
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetWindowName('Demo')
renderWindow.AddRenderer(renderer)
renderer.AddActor(actor)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderWindow)
renderWindow.Render()
interactor.Start()

The result will look similar to this: 结果将类似于以下内容:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM