如何使用 VTK 可视化桁架?

How can I visualize truss using VTK?

我正在尝试 运行 下面的代码来可视化桁架上的应力,但我得到一个 error.I 我正在使用 vtk 8.2.0 并且在谷歌搜索错误后我找到了较低版本的解决方案(低于 8.2)所以他们不能 work.The 代码是 below.Please 有人帮我消除这个错误。

import vtk
import numpy as np


def displayTruss(elemNodes, nodeCords, stress, name="Quantity"):
    pts = vtk.vtkPoints()

    for x, y in nodeCords:
        pts.InsertNextPoint(x, y, 0.0)

    lines = vtk.vtkCellArray()
    for ii, jj in elemNodes:
        lines.InsertNextCell(2)
        lines.InsertCellPoint(ii)
        lines.InsertCellPoint(jj)

    stdata = vtk.vtkDoubleArray()
    stdata.SetName(name)
    for val in stress:
        stdata.InsertNextValue(val)

    grid = vtk.vtkPolyData()
    grid.SetPoints(pts)
    grid.SetLines(lines)

    grid.GetCellData().SetScalars(stdata)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInput(grid)
    mapper.SetScalarRange(np.min(stress), np.max(stress))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    sbar = vtk.vtkScalarBarActor()
    sbar.SetLookupTable(mapper.GetLookupTable())
    sbar.SetTitle(name)

    ren = vtk.vtkRenderer()

    ren.AddActor2D(sbar)
    ren.AddActor(actor)

    renwin = vtk.vtkRenderWindow()
    renwin.AddRenderer(ren)
    renwin.SetSize(900, 500)

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renwin)

    iren.Initialize()
    renwin.Render()
    iren.Start()


# example
elemNodes = np.array([[0, 1], [0, 2], [1, 2], [1, 3],
                      [0, 3], [2, 3], [2, 5], [3, 4], [3, 5], [2, 4], [4, 5]])

nodeCords = np.array([
    [0.0, 0.0], [0.0, 3000.0],
    [3000.0, 0.0], [3000.0, 3000.0],
    [6000.0, 0.0], [6000.0, 3000.0]
])

stress = np.array([-210.902, 122.432, 62.558, -44.235, -173.145, -88.47, 62.558, -173.145, -44.235, 122.432, -210.902])

displayTruss(elemNodes, nodeCords, stress)

我收到以下错误;提前致谢

line 29, in displayTruss
    mapper.SetInput(grid)
AttributeError: 'vtkRenderingOpenGL2Python.vtkOpenGLPolyDataMapper' object has no attribute 'SetInput'

他们更改了 VTK 6 的接口。SetInput 被 SetInputConnection 和 SetInputData 取代。您可以在这里阅读:

https://vtk.org/Wiki/VTK/VTK_6_Migration/Replacement_of_SetInput

所以您想将代码更改为:

mapper.SetInputData(grid)

这需要 3 行代码 vtkplotter:

from vtkplotter import Lines, show
import numpy as np

elemNodes = np.array([[0, 1], [0, 2], [1, 2], [1, 3],
                      [0, 3], [2, 3], [2, 5], [3, 4], [3, 5], [2, 4], [4, 5]])

nodeCords = np.array([[0.0, 0.0, 0],    [0.0, 3000.0, 0],
                      [3000.0, 0.0, 0], [3000.0, 3000.0, 0],
                      [6000.0, 0.0, 0], [6000.0, 3000.0, 0]])

stress = np.array([-210.902, 122.432, 62.558, -44.235, -173.145, -88.47, 62.558,
                   -173.145, -44.235, 122.432, -210.902])

truss = Lines(nodeCords[elemNodes])
truss.cellColors(stress, cmap="jet").lineWidth(4).addScalarBar(title='Quantity')

show(truss, axes=1, bg="k")