【问题标题】:How can I visualize truss using VTK?如何使用 VTK 可视化桁架?
【发布时间】:2020-03-25 15:34:46
【问题描述】:

我正在尝试运行下面的代码来可视化桁架上的应力,但出现错误。我使用的是 vtk 8.2.0,在谷歌搜索错误后,我找到了较低版本(低于 8.2)的解决方案,因此它们无法工作.代码如下。请有人帮我消除这个错误。

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 truss


    【解决方案1】:

    这需要vtkplotter中的3行代码:

    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")
    

    【讨论】:

      【解决方案2】:

      他们更改了 VTK 6 的接口。SetInput 被替换为 SetInputConnection 和 SetInputData。你可以在这里阅读:

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

      所以您想将代码更改为:

      mapper.SetInputData(grid)
      

      【讨论】:

      • 非常感谢。现在我明白了。
      猜你喜欢
      • 1970-01-01
      • 2020-03-12
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多