VTK Learning Twenty-three- PointInterpolator2D
Description
對地形數據高程偏移(根據高程屬性來確定Z
值),並根據模擬的屬性值確定地形數據的屬性。
Code
#!/usr/bin/env python
import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
VTK_DATA_ROOT = "D:/codelearnning/vtk/VTK_BUILD/ExternalData/Testing/"
# Parameters for debugging
NPts = 1000000
math = vtk.vtkMath()
# create pipeline: use terrain dataset
#
# Read the data: a height field results
demReader = vtk.vtkDEMReader()
demReader.SetFileName(VTK_DATA_ROOT + "/Data/SainteHelens.dem")
demReader.Update()
lo = demReader.GetOutput().GetScalarRange()[0]
hi = demReader.GetOutput().GetScalarRange()[1]
# vtkImageDataGeometryFilter is a filter that extracts geometry from a structured points dataset.
geom = vtk.vtkImageDataGeometryFilter()
geom.SetInputConnection(demReader.GetOutputPort())
#vtkWarpScalar is a filter that modifies point coordinates by moving points along point normals by the scalar amount times the scale factor.
warp = vtk.vtkWarpScalar()
warp.SetInputConnection(geom.GetOutputPort())
warp.SetNormal(0, 0, 1)
warp.UseNormalOn()
# 拉伸因子,即z=高程值*2
warp.SetScaleFactor(2)
warp.Update()
bds = warp.GetOutput().GetBounds()
center = warp.GetOutput().GetCenter()
# A randomized point cloud, whose attributes are set via implicit function
points = vtk.vtkPoints()
points.SetDataTypeToFloat()
points.SetNumberOfPoints(NPts)
# 注意z值不影響插值效果
for i in range(0,NPts):
#points.SetPoint(i,math.Random(bds[0],bds[1]),math.Random(bds[2],bds[3]),math.Random(bds[4],bds[5]))
points.SetPoint(i,math.Random(bds[0],bds[1]),math.Random(bds[2],bds[3]),0)
source = vtk.vtkPolyData()
source.SetPoints(points)
sphere = vtk.vtkSphere()
sphere.SetCenter(center[0],center[1]-7500,center[2])
#vtkSampleImplicitFunctionFilter is a filter that evaluates an implicit function and (optional) gradients at each point in an input vtkDataSet
attr = vtk.vtkSampleImplicitFunctionFilter()
attr.SetInputData(source)
attr.SetImplicitFunction(sphere)
attr.Update()
# Gaussian kernel-------------------------------------------------------
gaussianKernel = vtk.vtkGaussianKernel()
gaussianKernel.SetSharpness(4)
gaussianKernel.SetRadius(50)
voronoiKernel = vtk.vtkVoronoiKernel()
interpolator1 = vtk.vtkPointInterpolator2D()
interpolator1.SetInputConnection(warp.GetOutputPort())
interpolator1.SetSourceConnection(attr.GetOutputPort())
#interpolator1.SetKernel(gaussianKernel)
interpolator1.SetKernel(voronoiKernel)
interpolator1.SetNullPointsStrategyToClosestPoint()
# Time execution
timer = vtk.vtkTimerLog()
timer.StartTimer()
interpolator1.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Interpolate Terrain Points (Gaussian): {0}".format(time))
scalarRange = attr.GetOutput().GetScalarRange()
intMapper1 = vtk.vtkPolyDataMapper()
intMapper1.SetInputConnection(interpolator1.GetOutputPort())
intMapper1.SetScalarRange(scalarRange)
intActor1 = vtk.vtkActor()
intActor1.SetMapper(intMapper1)
# Create an outline
outline1 = vtk.vtkOutlineFilter()
outline1.SetInputConnection(warp.GetOutputPort())
outlineMapper1 = vtk.vtkPolyDataMapper()
outlineMapper1.SetInputConnection(outline1.GetOutputPort())
outlineActor1 = vtk.vtkActor()
outlineActor1.SetMapper(outlineMapper1)
# Create the RenderWindow, Renderer and both Actors
#
ren0 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren0)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the renderer, set the background and size
#
ren0.AddActor(intActor1)
ren0.AddActor(outlineActor1)
ren0.SetBackground(0.1, 0.2, 0.4)
renWin.SetSize(250, 250)
cam = ren0.GetActiveCamera()
cam.SetFocalPoint(center)
fp = cam.GetFocalPoint()
cam.SetPosition(fp[0]+.2,fp[1]+.1,fp[2]+1)
ren0.ResetCamera()
iren.Initialize()
# render the image
#
renWin.Render()
iren.Start()