官方示例地址:
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()