在vtk python中将pointcloud转换为多面体

时间:2019-01-29 16:39:06

标签: python vtk point-clouds polyhedra

目标1:获取对象的点云并生成多面体几何

目标2:将多面体几何存储在PostGIS数据库中

好的,让我们专注于目标1。以下脚本显示了点云-实际上,它从数据库中获取了两个点云,并将它们显示为一个。

点云是对对象的扫描,我想从点云生成实体几何体,即多面体。我是vtk的新手,所以我正在学习教程。我假设我需要某种分析步骤Delaunay 3D三角剖分?然后转换为多面体?我不是程序员,但我会尽力而为,因此,如果您看到任何明显的错误,请同时指出/提供改进。

如果有人也想对目标2发表评论,也将受到欢迎。

import vtk
import psycopg2

class VtkPointCloud:

    def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e6):
        self.maxNumPoints = maxNumPoints
        self.vtkPolyData = vtk.vtkPolyData()
        self.clearPoints()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputData(self.vtkPolyData)
        mapper.SetColorModeToDefault()
        mapper.SetScalarRange(zMin, zMax)
        mapper.SetScalarVisibility(1)
        self.vtkActor = vtk.vtkActor()
        self.vtkActor.SetMapper(mapper)

    def addPoint(self, point):
        if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
            pointId = self.vtkPoints.InsertNextPoint(point[:])
            self.vtkDepth.InsertNextValue(point[2])
            self.vtkCells.InsertNextCell(1)
            self.vtkCells.InsertCellPoint(pointId)
        else:
            r = random.randint(0, self.maxNumPoints)
            self.vtkPoints.SetPoint(r, point[:])
        self.vtkCells.Modified()
        self.vtkPoints.Modified()
        self.vtkDepth.Modified()

    def clearPoints(self):
        self.vtkPoints = vtk.vtkPoints()
        self.vtkCells = vtk.vtkCellArray()
        self.vtkDepth = vtk.vtkDoubleArray()
        self.vtkDepth.SetName('DepthArray')
        self.vtkPolyData.SetPoints(self.vtkPoints)
        self.vtkPolyData.SetVerts(self.vtkCells)
        self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
        self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')





conn = psycopg2.connect("<connection details>")
cur = conn.cursor()
cur.execute("""SELECT ROUND((x - 580000)::numeric,4),ROUND((y - 4275000)::numeric,4),ROUND((z)::numeric,4) FROM pc_processing.filtered_top_dist;""")
# cur.execute("""SELECT ROUND((x)::numeric,4),ROUND((y)::numeric,4),ROUND((z)::numeric,4) FROM pc_processing.topsample;""")
T = cur.fetchall()
cur.close()

conn = psycopg2.connect("<connection details>")
cur = conn.cursor()
cur.execute("""SELECT ROUND((x - 580000)::numeric,4),ROUND((y - 4275000)::numeric,4),ROUND((z)::numeric,4) FROM pc_processing.filtered_base_dist;""")
# cur.execute("""SELECT ROUND((x)::numeric,4),ROUND((y)::numeric,4),ROUND((z)::numeric,4) FROM pc_processing.topsample;""")
B = cur.fetchall()
cur.close()

#Get the columns of the tuple (must be a better way)
xi = [i[0] for i in T]
yi = [i[1] for i in T]
zi = [i[2] for i in T]

xj = [i[0] for i in B]
yj = [i[1] for i in B]
zj = [i[2] for i in B]

#collect all points from the two layers
pointCloud = VtkPointCloud()
for k in range(len(T)):
    point = xi[k],yi[k],zi[k]
    pointCloud.addPoint(point)

for k in range(len(B)):
    point = xj[k],yj[k],zj[k]
    pointCloud.addPoint(point)






# Renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(pointCloud.vtkActor)
# renderer.AddActor(pointCloud2.vtkActor)

renderer.SetBackground(.2, .3, .4)
renderer.ResetCamera()

# Render Window
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)

# Interactor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Begin Interaction
renderWindow.Render()
renderWindowInteractor.Start()

0 个答案:

没有答案