vtk中二次曲面的显示

官方示例地址:

https://examples.vtk.org/site/Cxx/Visualization/DisplayQuadricSurfaces/

显示效果:
在这里插入图片描述

源码:


import vtk
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonDataModel import vtkQuadric
from vtkmodules.vtkFiltersCore import vtkContourFilter
from vtkmodules.vtkFiltersModeling import vtkOutlineFilter
from vtkmodules.vtkImagingHybrid import vtkSampleFunction
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)
import random

# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
def Sphere(): # 球面
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, 1, 1, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2 + 1*z^2
    return quadric

def EllipticParaboloid(): # 椭圆抛物面
    quadric = vtkQuadric()
    quadric.SetCoefficients(3, 1, 0, 0, 0, 0, 0, 0, -1, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2
    return quadric

def HyperbolicParaboloid(): # 双曲抛物面
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, -1, 0, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 - 1*y^2
    return quadric

def Cylinder():  # 圆柱体
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, 1, 0, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2
    return quadric

def HyperboloidOneSheet(): # 双曲面单张
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, 1, -1, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2
    return quadric

def HyperboloidTwoSheets(): # 双曲面双张
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, 1, -1, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2
    return quadric

def Ellipsoid(): # 椭球体
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, 1, 2, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2 + 1*z^2
    return quadric

def Cone(): # 锥
    quadric = vtkQuadric()
    quadric.SetCoefficients(1, 1, -1, 0, 0, 0, 0, 0, 0, 0)
    # F(x,y,z) = 1*x^2 + 1*y^2 - 1*z^2
    return quadric

def Other():
    quadric = vtkQuadric()
    quadric.SetCoefficients(.5, 1, .2, 0, .1, 0, 0, .2, 0, 0)
    # quadric.SetCoefficients(2, 1, 0, 0, 0, 0, 0, 0, 0, 0) # 椭圆柱
    # F(x,y,z) = 0.5*x^2 + 1*y^2 + 0.2*z^2 + 0*x*y + 0.1*y*z + 0*x*z + 0*x + 0.2*y + 0*z + 0
    return quadric

def PlotFunction():
    # setup the window
    ren1 = vtkRenderer()
    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren1)
    renWin.SetWindowName('DisplayQuadricSurfaces')

    implicit_list = [Sphere(), EllipticParaboloid(), HyperbolicParaboloid(), Cylinder(),
                     HyperboloidOneSheet(), HyperboloidTwoSheets(), Ellipsoid(), Cone(), Other()]
    # contour_value = [1.0, 10.0, 10.0, 1.0,
    #                  1.0, -1.0, 1.0, 0.0, 1.0]
    # contour_value表示公式 F(x,y,z) = contour_value
    contour_value = [8.0, 5.0, 3.0, 9.0,
                     2.0, -2.0, 7.0, 0.0, 1.0]

    delta_value = 25
    rows = 3
    cols = 3
    for j in range(rows):
        for i in range(cols):
            index = j * cols + i
            sample = vtkSampleFunction()
            sample.SetSampleDimensions(100, 100, 100)
            sample.SetImplicitFunction(implicit_list[index])
            bounds = [-10, 11, -10, 10, -10, 10]
            sample.SetModelBounds(bounds)

            contours = vtkContourFilter()
            contours.SetInputConnection(sample.GetOutputPort())
            # contours.GenerateValues(1, 1.0, 1.0)
            contours.GenerateValues(1, contour_value[index], contour_value[index])

            transform = vtk.vtkTransform()
            transform.Translate(delta_value*i, delta_value*j, 0)

            contourMapper = vtk.vtkPolyDataMapper()
            contourMapper.SetInputConnection(contours.GetOutputPort())
            contourMapper.SetScalarRange(0.0, 10.0)
            contourMapper.ScalarVisibilityOff()

            contourActor = vtkActor()
            contourActor.SetMapper(contourMapper)
            contourActor.GetProperty().SetColor(random.random(), random.random(), random.random())
            contourActor.SetUserMatrix(transform.GetMatrix())

            outline = vtkOutlineFilter()
            outline.SetInputConnection(sample.GetOutputPort())

            outlineMapper = vtkPolyDataMapper()
            outlineMapper.SetInputConnection(outline.GetOutputPort())

            outlineActor = vtkActor()
            outlineActor.SetMapper(outlineMapper)
            outlineActor.GetProperty().SetColor(1.0, 1.0, 1.0)
            outlineActor.SetUserMatrix(transform.GetMatrix())

            ren1.AddActor(contourActor)
            ren1.AddActor(outlineActor)

    iren = vtkRenderWindowInteractor()
    style = vtk.vtkInteractorStyleTrackballCamera()
    iren.SetInteractorStyle(style)
    iren.SetRenderWindow(renWin)
    ren1.ResetCamera()
    # add the actors to the scene
    ren1.SetBackground(0.0, 0.0, 0.0)
    # render and interact
    renWin.Render()
    iren.Start()

PlotFunction()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力减肥的小胖子5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
>