VTK Learning Twenty-five- PointInterpolator One

vtkPointInterpolator

描述

interpolate over point cloud using various kernels
使用各種核函數插值點雲數據

vtkPointInterpolator probes a point cloud Pc (the filter Source) with a set of points P (the filter Input), interpolating the data values from Pc onto P. Note however that the descriptive phrase “point cloud” is a misnomer: Pc can be represented by any vtkDataSet type, with the points of the dataset forming Pc. Similarly, the output P can also be represented by any vtkDataSet type; and the topology/geometry structure of P is passed through to the output along with the newly interpolated arrays.

vtkPointInterpolator用點集P(算法過濾器輸入數據input)來描述點雲Pc(算法過濾器源數據source),即P的數值是從Pc中插值來的。但是請注意,這裏所指“點雲”是不恰當的:Pc可以是任何vtkDataSet類型,即數據集的點構成Pc。簡單的說,P的輸出可以是任何vtkDataSet類型;並且P的幾何拓撲結構和插值的屬性數據一同輸出。

A key input to this filter is the specification of the interpolation kernel, and the parameters which control the associated interpolation process. Interpolation kernels include Voronoi, Gaussian, Shepard, and SPH (smoothed particle hydrodynamics), with additional kernels to be added in the future.
這個插值算法的一個重要輸入是具體的插值核函數,參數控制着與之關聯的插值過程。插值內核包括 Voronoi, Gaussian, Shepard, and SPH (smoothed particle hydrodynamics),以後會有更多核可以使用。

An overview of the algorithm is as follows. For each p from P, Np “close” points to p are found. (The meaning of what is “close” can be specified as either the N closest points, or all points within a given radius Rp. This depends on how the kernel is defined.) Once the Np close points are found, then the interpolation kernel is applied to compute new data values located on p. Note that for reasonable performance, finding the Np closest points requires a point locator. The locator may be specified as input to the algorithm. (By default, a vtkStaticPointLocator is used because generally it is much faster to build, delete, and search with. However, with highly non-uniform point distributions, octree- or kd-tree based locators may perform better.)
算法的大概如下。對P中的每一個點p,在Pc中查找N個鄰近的點。(鄰近可以是N個最近的點,也可以是給定半徑Rp內的所有點,取決於如何定義核函數)。首先查找到Np,然後依據插值內核函數的規則來計算點p處的值。爲了提高查找Np的效率,需要一個點的定位器(索引器)。定位器也可以描述爲插值算法的一個輸入。(默認使用vtkStaticPointLocator,因爲一般情況下,vtkStaticPointLocator能夠更快的構建、刪除和查找。但是,對於非常不均勻的點集,基於八叉樹和kd樹的定位器性能會更好。

讀入“點雲”Pc

# create pipeline
#
pl3d = vtk.vtkMultiBlockPLOT3DReader()
pl3d.SetXYZFileName(VTK_DATA_ROOT + "/Data/combxyz.bin")
pl3d.SetQFileName(VTK_DATA_ROOT + "/Data/combq.bin")
pl3d.SetScalarFunctionNumber(100)
pl3d.SetVectorFunctionNumber(202)
pl3d.Update()

output = pl3d.GetOutput().GetBlock(0)

PLOT3D 數據格式
PLOT3D數據格式源於NASA,廣泛用於規則網格的CFD數據文件。PLOT3D文件分爲網格文件(XYZ 文件), 空氣動力學結果文件 (Q 文件)和通用結果文件(函數文件 + 函數名稱文件)。網格文件中可加入所謂的IBlank參數。combxyz.bin網格文件(XYZ 文件)。combq.bin空氣動力學結果文件 (Q 文件)。

創建點集P

# Create a probe plane
center = output.GetCenter()

plane = vtk.vtkPlaneSource()
plane.SetResolution(res,res)
plane.SetOrigin(0,0,0)
plane.SetPoint1(10,0,0)
plane.SetPoint2(0,10,0)
plane.SetCenter(center)
plane.SetNormal(0,1,0)

定義插值算法對象

# Reuse the locator 定位器
locator = vtk.vtkStaticPointLocator()
locator.SetDataSet(output)
locator.BuildLocator()

# Voronoi kernel核
voronoiKernel = vtk.vtkVoronoiKernel()

interpolator = vtk.vtkPointInterpolator()
interpolator.SetInputConnection(plane.GetOutputPort())
interpolator.SetSourceData(output)
interpolator.SetKernel(voronoiKernel)
interpolator.SetLocator(locator)

插值內核

vtk.vtkVoronoiKernel
vtk.vtkGaussianKernel
vtk.vtkShepardKernel
vtk.vtkLinearKernel

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
res = 200

# create pipeline
#
pl3d = vtk.vtkMultiBlockPLOT3DReader()
pl3d.SetXYZFileName(VTK_DATA_ROOT + "/Data/combxyz.bin")
pl3d.SetQFileName(VTK_DATA_ROOT + "/Data/combq.bin")
pl3d.SetScalarFunctionNumber(100)
pl3d.SetVectorFunctionNumber(202)
pl3d.Update()

output = pl3d.GetOutput().GetBlock(0)

# Create a probe plane
center = output.GetCenter()

plane = vtk.vtkPlaneSource()
plane.SetResolution(res,res)
plane.SetOrigin(0,0,0)
plane.SetPoint1(10,0,0)
plane.SetPoint2(0,10,0)
plane.SetCenter(center)
plane.SetNormal(0,1,0)

# Reuse the locator
locator = vtk.vtkStaticPointLocator()
locator.SetDataSet(output)
locator.BuildLocator()

# Voronoi kernel------------------------------------------------
voronoiKernel = vtk.vtkVoronoiKernel()

interpolator = vtk.vtkPointInterpolator()
interpolator.SetInputConnection(plane.GetOutputPort())
interpolator.SetSourceData(output)
interpolator.SetKernel(voronoiKernel)
interpolator.SetLocator(locator)

# Time execution
timer = vtk.vtkTimerLog()
timer.StartTimer()
interpolator.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Interpolate Points (Voronoi): {0}".format(time))

intMapper = vtk.vtkPolyDataMapper()
intMapper.SetInputConnection(interpolator.GetOutputPort())

intActor = vtk.vtkActor()
intActor.SetMapper(intMapper)

# Create an outline
outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(output)

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

outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)

# Gaussian kernel-------------------------------------------------------
gaussianKernel = vtk.vtkGaussianKernel()
#gaussianKernel = vtk.vtkEllipsoidalGaussianKernel()
#gaussianKernel.UseScalarsOn()
#gaussianKernel.UseNormalsOn()
gaussianKernel.SetSharpness(4)
gaussianKernel.SetRadius(0.5)

interpolator1 = vtk.vtkPointInterpolator()
interpolator1.SetInputConnection(plane.GetOutputPort())
interpolator1.SetSourceData(output)
interpolator1.SetKernel(gaussianKernel)
interpolator1.SetLocator(locator)
interpolator1.SetNullPointsStrategyToNullValue()

# Time execution
timer.StartTimer()
interpolator1.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Interpolate Points (Gaussian): {0}".format(time))

intMapper1 = vtk.vtkPolyDataMapper()
intMapper1.SetInputConnection(interpolator1.GetOutputPort())

intActor1 = vtk.vtkActor()
intActor1.SetMapper(intMapper1)

# Create an outline
outline1 = vtk.vtkStructuredGridOutlineFilter()
outline1.SetInputData(output)

outlineMapper1 = vtk.vtkPolyDataMapper()
outlineMapper1.SetInputConnection(outline1.GetOutputPort())

outlineActor1 = vtk.vtkActor()
outlineActor1.SetMapper(outlineMapper1)

# Shepard kernel-------------------------------------------------------
shepardKernel = vtk.vtkShepardKernel()
shepardKernel.SetPowerParameter(2)
shepardKernel.SetRadius(0.5)

interpolator2 = vtk.vtkPointInterpolator()
interpolator2.SetInputConnection(plane.GetOutputPort())
interpolator2.SetSourceData(output)
interpolator2.SetKernel(shepardKernel)
interpolator2.SetLocator(locator)
interpolator2.SetNullPointsStrategyToMaskPoints()

# Time execution
timer.StartTimer()
interpolator2.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Interpolate Points (Shepard): {0}".format(time))

intMapper2 = vtk.vtkPolyDataMapper()
intMapper2.SetInputConnection(interpolator2.GetOutputPort())

intActor2 = vtk.vtkActor()
intActor2.SetMapper(intMapper2)

# Create an outline
outline2 = vtk.vtkStructuredGridOutlineFilter()
outline2.SetInputData(output)

outlineMapper2 = vtk.vtkPolyDataMapper()
outlineMapper2.SetInputConnection(outline2.GetOutputPort())

outlineActor2 = vtk.vtkActor()
outlineActor2.SetMapper(outlineMapper2)

# Linear kernel-------------------------------------------------------
linearKernel = vtk.vtkLinearKernel()
linearKernel.SetRadius(0.5)

interpolator3 = vtk.vtkPointInterpolator()
interpolator3.SetInputConnection(plane.GetOutputPort())
interpolator3.SetSourceData(output)
interpolator3.SetKernel(linearKernel)
interpolator3.SetLocator(locator)
interpolator3.SetNullPointsStrategyToNullValue()
interpolator3.AddExcludedArray("StagnationEnergy")

# Time execution
timer.StartTimer()
interpolator3.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Interpolate Points (Linear): {0}".format(time))

intMapper3 = vtk.vtkPolyDataMapper()
intMapper3.SetInputConnection(interpolator3.GetOutputPort())

intActor3 = vtk.vtkActor()
intActor3.SetMapper(intMapper3)

# Create an outline
outline3 = vtk.vtkStructuredGridOutlineFilter()
outline3.SetInputData(output)

outlineMapper3 = vtk.vtkPolyDataMapper()
outlineMapper3.SetInputConnection(outline3.GetOutputPort())

outlineActor3 = vtk.vtkActor()
outlineActor3.SetMapper(outlineMapper3)

# Create the RenderWindow, Renderer and both Actors
#
ren0 = vtk.vtkRenderer()
ren0.SetViewport(0,0,.5,.5)
ren1 = vtk.vtkRenderer()
ren1.SetViewport(0.5,0,1,.5)
ren2 = vtk.vtkRenderer()
ren2.SetViewport(0,0.5,.5,1)
ren3 = vtk.vtkRenderer()
ren3.SetViewport(0.5,0.5,1,1)
renWin = vtk.vtkRenderWindow()
renWin.SetMultiSamples(0)
renWin.AddRenderer(ren0)
renWin.AddRenderer(ren1)
renWin.AddRenderer(ren2)
renWin.AddRenderer(ren3)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Add the actors to the renderer, set the background and size
#
ren0.AddActor(intActor)
ren0.AddActor(outlineActor)
ren0.SetBackground(0.1, 0.2, 0.4)

ren1.AddActor(intActor1)
ren1.AddActor(outlineActor1)
ren1.SetBackground(0.1, 0.2, 0.4)

ren2.AddActor(intActor2)
ren2.AddActor(outlineActor2)
ren2.SetBackground(0.1, 0.2, 0.4)

ren3.AddActor(intActor3)
ren3.AddActor(outlineActor3)
ren3.SetBackground(0.1, 0.2, 0.4)

renWin.SetSize(500, 500)

cam = ren0.GetActiveCamera()
cam.SetClippingRange(3.95297, 50)
cam.SetFocalPoint(8.88908, 0.595038, 29.3342)
cam.SetPosition(-12.3332, 31.7479, 41.2387)
cam.SetViewUp(0.060772, -0.319905, 0.945498)

ren1.SetActiveCamera(cam)
ren2.SetActiveCamera(cam)
ren3.SetActiveCamera(cam)

iren.Initialize()

# render the image
#
renWin.Render()

iren.Start()

Result

四種插值內核的比較。
左下角:Interpolate Points (Voronoi): 0.04800009727478027
右下角:Interpolate Points (Gaussian): 0.0710000991821289
左上角:Interpolate Points (Shepard): 0.06699991226196289
右上角:Interpolate Points (Linear): 0.05900001525878906
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章