簡體   English   中英

如何在vtk中可視化二維數組

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

我有二維的雙精度數組(大小為20x30):

double a[20*30];

如何使用VTK對其進行可視化? 很難找到適當的文檔。 我發現的最接近的示例是this ,但是它使用3個代表顏色的無符號字符作為輸入。 據我了解,我應該使用vtkScalarsToColors類以某種方式將標量映射到顏色,但是我不知道如何將所有內容放入單個代碼中。

您可能要做的是將標量分配給曲面或體積網格的點或單元。 然后,VTK可以處理可視化。 在以下示例中對此進行了演示: ScalarBarActor 對於基本用法,請遵循Scalars示例。

但是,您需要自己提供要將值映射到的合適的網格。 從您的問題來看,“值如何可視化二維數組”的含義並不完全清楚。 如果要在平面20x30網格中分配標量值,則需要首先創建具有三角形或四邊形像元的表面對象(類型為vtkPolyData ),然后使用surface->GetPointData()->SetScalars()將值分配給網格的點surface->GetPointData()->SetScalars() ,如以上示例所示。

在這種情況下,很方便的是vtkPlaneSource在這里查看相應的示例。 您可以分別使用SetXResolution()SetYResolution()設置的網格點數。 (如果尚不清楚: vtkPlaneSource繼承vtkPolyDataAlgorithm ,要訪問基礎vtkPolyData對象,請使用方法GetOutput()


更新 :我添加了示例代碼來演示該過程-使用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()

結果將類似於以下內容:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM