zoukankan      html  css  js  c++  java
  • VTK使用矢量数据弯曲几何体

       vtkWarpVector is a filter that modifies point coordinates by moving points along vector times the scale factor. Useful for showing flow profiles or mechanical deformation. The filter passes both its point data and cell data to its output. 

      沿法向膨胀

    #!/usr/bin/env python
    import vtk
    
    inputPolyData = vtk.vtkPolyData()
    
    sphereSource = vtk.vtkSphereSource()
    sphereSource.SetPhiResolution(15)
    sphereSource.SetThetaResolution(15)
    sphereSource.Update()
    inputPolyData = sphereSource.GetOutput()
    
    # merge duplicate points, and/or remove unused points and/or remove degenerate cells
    clean = vtk.vtkCleanPolyData()
    clean.SetInputData(inputPolyData)
    
    # Generate normals
    normals = vtk.vtkPolyDataNormals()
    normals.SetInputConnection(clean.GetOutputPort())
    normals.SplittingOff()
    
    # vtkWarpVector is a filter that modifies point coordinates by moving points along vector times the scale factor
    # Warp using the normals (deform geometry with vector data)
    warp = vtk.vtkWarpVector() 
    warp.SetInputConnection(normals.GetOutputPort())
    
    # Set the input data arrays that this algorithm will process
    # The fieldAssociation refers to which field in the data object the array is stored
    warp.SetInputArrayToProcess(0, 0, 0,vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, vtk.vtkDataSetAttributes.NORMALS)
    
    # Specify value to scale displacement
    warp.SetScaleFactor(1.0) 
    
    # Visualize the original and warped models
    colors = vtk.vtkNamedColors()
    
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(warp.GetOutputPort())
    
    warpedActor = vtk.vtkActor()
    warpedActor.SetMapper(mapper)
    warpedActor.GetProperty().SetColor(colors.GetColor3d("Flesh"))
    
    originalMapper = vtk.vtkPolyDataMapper()
    originalMapper.SetInputConnection(normals.GetOutputPort())
    
    originalActor = vtk.vtkActor()
    originalActor.SetMapper(originalMapper)
    originalActor.GetProperty().SetInterpolationToFlat() # Set the shading interpolation method for an object
    originalActor.GetProperty().SetColor(colors.GetColor3d("Flesh"))
    
    renderWindow =vtk.vtkRenderWindow()
    renderWindow.SetSize(640, 480)
    
    # Create a camera for all renderers
    camera = vtk.vtkCamera()
    
    # Define viewport ranges: (xmin, ymin, xmax, ymax)
    leftViewport = [0.0, 0.0, 0.5, 1.0]
    rightViewport =[0.5, 0.0, 1.0, 1.0]
    
    # Setup both renderers
    leftRenderer = vtk.vtkRenderer()
    leftRenderer.SetViewport(leftViewport)
    leftRenderer.SetBackground(colors.GetColor3d("Burlywood"))
    leftRenderer.SetActiveCamera(camera)
    
    rightRenderer = vtk.vtkRenderer()
    rightRenderer.SetViewport(rightViewport)
    rightRenderer.SetBackground(colors.GetColor3d("CornFlower"))
    rightRenderer.SetActiveCamera(camera)
    
    leftRenderer.AddActor(originalActor)
    rightRenderer.AddActor(warpedActor)
    
    rightRenderer.ResetCamera()
    
    renderWindow.AddRenderer(rightRenderer)
    renderWindow.AddRenderer(leftRenderer)
    
    style = vtk.vtkInteractorStyleTrackballCamera()
    
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderWindow)
    interactor.SetInteractorStyle(style)
    
    renderWindow.Render()
    interactor.Start()
    View Code

       膨胀导入的STL模型

    #!/usr/bin/env python
    import vtk
    
    inputPolyData = vtk.vtkPolyData()
    
    readerSTL = vtk.vtkSTLReader()
    readerSTL.SetFileName("Suzanne.stl")
    readerSTL.Update()
    inputPolyData = readerSTL.GetOutput()
    
    # merge duplicate points, and/or remove unused points and/or remove degenerate cells
    clean = vtk.vtkCleanPolyData()
    clean.SetInputData(inputPolyData)
    
    # Generate normals
    normals = vtk.vtkPolyDataNormals()
    normals.SetInputConnection(clean.GetOutputPort())
    normals.SplittingOff() # Turn off the splitting of sharp edges
    
    # vtkWarpVector is a filter that modifies point coordinates by moving points along vector times the scale factor
    # Warp using the normals (deform geometry with vector data)
    warp = vtk.vtkWarpVector() 
    warp.SetInputConnection(normals.GetOutputPort())
    
    # Set the input data arrays that this algorithm will process
    # The fieldAssociation refers to which field in the data object the array is stored
    warp.SetInputArrayToProcess(0, 0, 0,vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, vtk.vtkDataSetAttributes.NORMALS)
    
    # Specify value to scale displacement
    warp.SetScaleFactor(0.3) 
    
    # Visualize the original and warped models
    colors = vtk.vtkNamedColors()
    
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(warp.GetOutputPort())
    
    warpedActor = vtk.vtkActor()
    warpedActor.SetMapper(mapper)
    warpedActor.GetProperty().SetColor(colors.GetColor3d("Flesh"))
    
    originalMapper = vtk.vtkPolyDataMapper()
    originalMapper.SetInputConnection(normals.GetOutputPort())
    
    originalActor = vtk.vtkActor()
    originalActor.SetMapper(originalMapper)
    originalActor.GetProperty().SetInterpolationToFlat() # Set the shading interpolation method for an object
    originalActor.GetProperty().SetColor(colors.GetColor3d("Flesh"))
    
    renderWindow =vtk.vtkRenderWindow()
    renderWindow.SetSize(640, 480)
    
    # Create a camera for all renderers
    camera = vtk.vtkCamera()
    
    # Define viewport ranges: (xmin, ymin, xmax, ymax)
    leftViewport = [0.0, 0.0, 0.5, 1.0]
    rightViewport =[0.5, 0.0, 1.0, 1.0]
    
    # Setup both renderers
    leftRenderer = vtk.vtkRenderer()
    leftRenderer.SetViewport(leftViewport)
    leftRenderer.SetBackground(colors.GetColor3d("Burlywood"))
    leftRenderer.SetActiveCamera(camera)
    
    rightRenderer = vtk.vtkRenderer()
    rightRenderer.SetViewport(rightViewport)
    rightRenderer.SetBackground(colors.GetColor3d("CornFlower"))
    rightRenderer.SetActiveCamera(camera)
    
    leftRenderer.AddActor(originalActor)
    rightRenderer.AddActor(warpedActor)
    
    rightRenderer.ResetCamera()
    
    renderWindow.AddRenderer(rightRenderer)
    renderWindow.AddRenderer(leftRenderer)
    
    style = vtk.vtkInteractorStyleTrackballCamera()
    
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderWindow)
    interactor.SetInteractorStyle(style)
    
    renderWindow.Render()
    interactor.Start()
    View Code

     

     

     

     

    参考:

    VTK: vtkWarpVector Class Reference

    VTK/Examples/Cxx/PolyData/WarpSurface

    VTK/Examples/Python/PolyData/WarpVector.py

    An algorithm for inflating/deflating (offsetting, buffering) polygons

  • 相关阅读:
    nginx 指定多个域名跨域请求配置 find ./ ! -path "./node_modules/*" -name *.js |xargs egrep basePath
    所以 if a 代表如果a有值的时候执行的内容,有值才能执行是True if not a 代表 a无值是空的时候执行的内容 not False 是True才能执行 代表空值的时候执行
    python 逻辑运算符
    git的突出解决--git rebase之abort、continue、skip
    chromium source get
    How JavaScript works in browser and node?
    chromium windows compile 浏览器编译 vs2017 win10
    Git Reset 三种模式
    webkit js
    用electron自己的nodejs写的depot_tools 工具下载 构建源码
  • 原文地址:https://www.cnblogs.com/21207-iHome/p/9130186.html
Copyright © 2011-2022 走看看