c++ - How to visualize 2d array of doubles in vtk?
问题描述
I have 2d array (of size 20x30) of doubles:
double a[20*30];
How to visualize it using 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. 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.
解决方案
您可能想要做的是将标量分配给表面或体积网格的点或单元。VTK 然后可以处理可视化。这在以下示例中进行了演示:ScalarBarActor。对于基本用法,请遵循标量示例。
但是,您需要自己提供要将值映射到的合适网格。从您的问题来看,“如何可视化二维数组”值的含义并不完全清楚。如果要在平面 20x30 网格中分配标量值,则需要首先创建一个vtkPolyData
具有三角形或四边形单元的表面对象(类型为 ),然后使用 将值分配给网格的点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()
结果将如下所示:
推荐阅读
- c++ - 持有对地图矢量的地图引用
- opencv - 如何在立体三角测量中找到与特定相机的欧几里得距离而不是与基线的垂直距离?
- docker - Docker 中的集群是什么?
- salesforce - Salesforce 将 Apex Class 部署到生产环境 0% 的代码覆盖率
- sql - 是否可以在历史事件表上按日期创建计数?
- c# - System.ArgumentException 与 C# MongoDB.Driver
- java - 为什么我使用捆绑包在片段中获得空值
- javascript - 获取List的元素
- ios - 如何根据图像的类别(例如汽车,抽象)从 Firebase 存储中检索图像
- android - 如何在单独的 Java 文件中制作警报对话框?