python VTK example (5)-- bool运算(交,并,补)

核心代码:

    booleanOperation = vtk.vtkBooleanOperationPolyDataFilter()
    booleanOperation.SetOperationToUnion()  #并
    # booleanOperation.SetOperationToIntersection() #交
    # booleanOperation.SetOperationToDifference() #补(差)

sphereSource1,sphereSource2分别表示需要做bool运算的两个球

    booleanOperation.SetInputConnection(0, sphereSource1.GetOutputPort())
    booleanOperation.SetInputConnection(1, sphereSource2.GetOutputPort())

结果图(并):
红色和绿色小球是原始小球,中间是两小球做并运算结果图(差):

在这里插入图片描述

python 代码:

#!/usr/bin/env python

import vtk


def main():
    colors = vtk.vtkNamedColors()

    sphereSource1 = vtk.vtkSphereSource()
    sphereSource1.SetCenter(0.25, 0, 0)
    sphereSource1.Update()
    input1 = sphereSource1.GetOutput()
    # sphere1Tri = vtk.vtkTriangleFilter()
    # sphere1Tri.SetInputData(input1)

    sphereSource2 = vtk.vtkSphereSource()
    sphereSource2.Update()
    input2 = sphereSource2.GetOutput()
    # sphere2Tri = vtk.vtkTriangleFilter()
    # sphere2Tri.SetInputData(input2)

    input1Mapper = vtk.vtkPolyDataMapper()
    input1Mapper.SetInputData(input1)
    input1Mapper.ScalarVisibilityOff()
    input1Actor = vtk.vtkActor()
    input1Actor.SetMapper(input1Mapper)
    input1Actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
    input1Actor.SetPosition(input1.GetBounds()[1] - input1.GetBounds()[0], 0, 0)

    input2Mapper = vtk.vtkPolyDataMapper()
    input2Mapper.SetInputData(input2)
    input2Mapper.ScalarVisibilityOff()
    input2Actor = vtk.vtkActor()
    input2Actor.SetMapper(input2Mapper)
    input2Actor.GetProperty().SetColor(colors.GetColor3d("Mint"))
    input2Actor.SetPosition(-(input2.GetBounds()[1] - input2.GetBounds()[0]), 0, 0)

    booleanOperation = vtk.vtkBooleanOperationPolyDataFilter()
    booleanOperation.SetOperationToUnion()
    # booleanOperation.SetOperationToIntersection()
    # booleanOperation.SetOperationToDifference()

    # booleanOperation.SetInputConnection(0, sphere1Tri.GetOutputPort())
    # booleanOperation.SetInputConnection(1, sphere2Tri.GetOutputPort())

    booleanOperation.SetInputConnection(0, sphereSource1.GetOutputPort())
    booleanOperation.SetInputConnection(1, sphereSource2.GetOutputPort())

    booleanOperation.Update()

    booleanOperationMapper = vtk.vtkPolyDataMapper()
    booleanOperationMapper.SetInputConnection(booleanOperation.GetOutputPort())
    booleanOperationMapper.ScalarVisibilityOff()

    booleanOperationActor = vtk.vtkActor()
    booleanOperationActor.SetMapper(booleanOperationMapper)
    booleanOperationActor.GetProperty().SetDiffuseColor(colors.GetColor3d("Banana"))

    renderer = vtk.vtkRenderer()
    renderer.AddViewProp(input1Actor)
    renderer.AddViewProp(input2Actor)
    renderer.AddViewProp(booleanOperationActor)
    renderer.SetBackground(colors.GetColor3d("Silver"))
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)

    renWinInteractor = vtk.vtkRenderWindowInteractor()
    renWinInteractor.SetRenderWindow(renderWindow)

    renderWindow.Render()
    renWinInteractor.Start()


if __name__ == '__main__':
    main()

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
我理解您的问题是如何用 Python 代码输出 trame-vtk 的圆颜色变化动画。我建议您可以使用 VTK 库和 Matplotlib 库来实现这个功能。下面是一个简单的示例代码: ``` import vtk import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 创建圆的源 points = vtk.vtkPoints() polys = vtk.vtkCellArray() circle = vtk.vtkPolyData() numPts = 50 for i in range(numPts): x = np.cos(2*np.pi/numPts*i) y = np.sin(2*np.pi/numPts*i) z = 0 points.InsertNextPoint(x, y, z) if i < numPts-1: quad = vtk.vtkQuad() quad.GetPointIds().SetId(0, i) quad.GetPointIds().SetId(1, i+1) quad.GetPointIds().SetId(2, i+numPts+1) quad.GetPointIds().SetId(3, i+numPts) polys.InsertNextCell(quad) circle.SetPoints(points) circle.SetPolys(polys) # 创建圆的渲染器 renderer = vtk.vtkRenderer() renderWindow = vtk.vtkRenderWindow() renderWindow.SetSize(800, 800) renderWindow.AddRenderer(renderer) interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(renderWindow) # 创建圆的mapper和actor mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(circle) actor = vtk.vtkActor() actor.SetMapper(mapper) renderer.AddActor(actor) # 添加颜色映射器 colorLUT = vtk.vtkColorTransferFunction() colorLUT.AddRGBPoint(0.0, 1.0, 0.0, 0.0) colorLUT.AddRGBPoint(1.0, 0.0, 1.0, 0.0) # 创建颜色映射器的bar scalarBar = vtk.vtkScalarBarActor() scalarBar.SetLookupTable(colorLUT) # 将颜色映射器的bar添加到渲染器 renderer.AddActor2D(scalarBar) # 创建颜色变化的动画 animationSteps = 100 for i in range(animationSteps): t = float(i) / float(animationSteps-1) color = colorLUT.GetColor(t) actor.GetProperty().SetColor(color) scalarBar.SetOrientationToHorizontal() scalarBar.SetWidth(0.8) scalarBar.SetHeight(0.15) scalarBar.SetPosition(0.1, 0.1) renderWindow.Render() ``` 这个代码会输出一个圆形的动画,圆的颜色会随着时间变化。您可以根据自己的需求修改代码来输出不同的动画效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值