C++ 如何在vtk中可视化二维双精度阵列?

C++ 如何在vtk中可视化二维双精度阵列?,c++,vtk,C++,Vtk,我有一个二维阵列(尺寸为20x30)的双打: double a[20*30]; 如何使用VTK将其可视化?很难找到合适的文件。我发现的最接近的例子是,它使用3个表示颜色的无符号字符作为输入。据我所知,我应该使用类以某种方式将标量映射到颜色,但我不知道如何将所有内容都放到一段代码中。您可能想做的是将标量指定给曲面或体积网格的点或单元。然后,VTK可以处理可视化。以下示例演示了这一点:。对于基本用法,请遵循以下示例 但是,您需要自己提供要将值映射到的合适网格。从您的问题来看,还不完全清楚“如何可视

我有一个二维阵列(尺寸为20x30)的双打:

double a[20*30];

如何使用VTK将其可视化?很难找到合适的文件。我发现的最接近的例子是,它使用3个表示颜色的无符号字符作为输入。据我所知,我应该使用类以某种方式将标量映射到颜色,但我不知道如何将所有内容都放到一段代码中。

您可能想做的是将标量指定给曲面或体积网格的点或单元。然后,VTK可以处理可视化。以下示例演示了这一点:。对于基本用法,请遵循以下示例

但是,您需要自己提供要将值映射到的合适网格。从您的问题来看,还不完全清楚“如何可视化二维数组”的值是什么意思。如果要在平面20x30栅格中指定标量值,需要首先创建一个带有三角形或四边形单元的曲面对象(类型),然后使用
surface->GetPointData()->SetScalars()
,将值指定给网格的点,如上述示例所示

在这种情况下,方便的方法是,查找相应的示例。可以分别使用
SetXResolution()
SetYResolution()
设置的网格点数量。(如果不清楚:
vtkPlaneSource
继承,要访问底层
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()
结果将类似于此:


您可能需要将标量指定给曲面或体积网格的点或单元。然后,VTK可以处理可视化。以下示例演示了这一点:。对于基本用法,请遵循以下示例

但是,您需要自己提供要将值映射到的合适网格。从您的问题来看,还不完全清楚“如何可视化二维数组”的值是什么意思。如果要在平面20x30栅格中指定标量值,需要首先创建一个带有三角形或四边形单元的曲面对象(类型),然后使用
surface->GetPointData()->SetScalars()
,将值指定给网格的点,如上述示例所示

在这种情况下,方便的方法是,查找相应的示例。可以分别使用
SetXResolution()
SetYResolution()
设置的网格点数量。(如果不清楚:
vtkPlaneSource
继承,要访问底层
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()
结果将类似于此:


这不是2d数组,而是包含20*30个元素的1d数组。@Kaldrr我的意思是我想在屏幕上绘制2d矩形,并将此数组中的值映射到一些颜色。这不是2d数组,而是包含20*30个元素的1d数组。@Kaldrr我的意思是我想在屏幕上绘制2d矩形,并将此数组中的值映射到一些颜色。谢谢!vtkPlaneSource的案例正是我想要的。谢谢!vtkPlaneSource的案例正是我要寻找的。