2

我正在使用以下代码填充点云:如何在 vtk 中以不同颜色显示点云?

重点是新西兰目前地震的长度、纬度和深度。

我想改变点的大小,将其缩放第四个值 -mag这是群中每次地震的震级数据。

我真的不熟悉 vtk 包装器,想知道是否有人可以指出我可以在什么时候向演员讲话,并更改每个点的大小:

代码:

import vtk
import csv
import numpy


class Points_Maker(object):
    def __init__(self, fname):
        self.fname = fname
        self.points = []
        self.points_mag = []
        self.get_data()

    def get_data(self):
        reader = csv.reader(open(self.fname, "rb"))
        for row in reader:
            if "FID" in row[0]:
                pass
            else:
                longitude = (float(row[3]) - 174) * 10 #de-localises value
                latitude = (float(row[4]) + 41) * 10 #de-localises value
                depth = float(row[5])
                magnitude = float(row[6])
                point = [longitude, latitude, depth]
                point_and_mag = [[longitude, latitude, depth], magnitude]
                point = numpy.asarray(point)
                point_and_mag = numpy.asarray(point_and_mag)
                self.points.append(point)
                self.points_mag.append(point_and_mag)

class VtkPointCloud:

    def __init__(self, zMin=-0.0, zMax=100.0, maxNumPoints=1e6): #sets colou limits
        self.maxNumPoints = maxNumPoints
        self.vtkPolyData = vtk.vtkPolyData()
        self.clearPoints()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInput(self.vtkPolyData)
        mapper.SetColorModeToDefault()
        mapper.SetScalarRange(zMin, zMax)
        mapper.SetScalarVisibility(1)
        self.vtkActor = vtk.vtkActor()
        self.vtkActor.SetMapper(mapper)

    def addPoint(self, point):
        mag = 10
        if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
            pointId = self.vtkPoints.InsertNextPoint(point[:])
            self.vtkDepth.InsertNextValue(point[2])
            self.vtkCells.InsertNextCell(1)
            self.vtkCells.InsertCellPoint(pointId)
        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')


def main():
    pm = Points_Maker("quake.csv")
    pointCloud = VtkPointCloud()
    for point_and_mag in pm.points_mag:
        mag = point_and_mag[1]
        point = point_and_mag[0]
        pointCloud.addPoint(point)
    renderer = vtk.vtkRenderer()
    renderer.AddActor(pointCloud.vtkActor)
    renderer.SetBackground(.2, .3, .3)  #colour
    renderer.ResetCamera()

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

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

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

if __name__ == '__main__':
    main()

points_and_mag 数据片段:

[[4.45699999999988, -5.540999999999983, 10.0391] 2.38]
[[3.9390000000000214, -5.606999999999971, 17.7148] 3.7352]
[[4.182999999999879, -5.519999999999996, 11.0938] 2.16]
[[3.9979999999999905, -5.390999999999977, 8.5713] 2.4826]
[[3.9560000000000173, -5.568000000000026, 12.1685] 3.5205]
[[4.41900000000004, -5.381, 15.1953] 2.1109]
[[4.507000000000119, -5.360999999999976, 14.3164] 2.5587]
[[3.973000000000013, -5.688999999999993, 14.1406] 2.7651]
[[4.139999999999873, -5.290000000000035, 10.9766] 2.6873]
[[7.182999999999993, -3.92000000000003, 11.6797] 2.5306]
[[4.07999999999987, -5.489999999999995, 17.4805] 5.7216]
[[4.113000000000113, -5.416000000000025, 15.0195] 2.8919]
[[3.7520000000000664, -6.462999999999965, 5.8203] 2.0667]
[[7.727999999999895, -6.178999999999988, 29.0234] 2.0115]

使用这个:http ://www.vtk.org/Wiki/VTK/Examples/Python/GeometricObjects/Display/Point

我已经弄清楚如何更改整个集合的大小,这让我怀疑点云是一个演员,而不是点云中点的每个实例:-

def addPoint(self, point, mag):
    if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
        pointId = self.vtkPoints.InsertNextPoint(point[:])
        self.vtkDepth.InsertNextValue(point[2])
        self.vtkCells.InsertNextCell(1)
        self.vtkCells.InsertCellPoint(pointId)
        self.vtkActor.GetProperty().SetPointSize(mag*10)
    self.vtkCells.Modified()
    self.vtkPoints.Modified()
    self.vtkDepth.Modified()
    self.vtkActor.Modified()
4

1 回答 1

2

啊,我想通了。

我只需要pointCloud = VtkPointCloud()在 for 循环中设置构造函数,该循环遍历点,然后renderer.AddActor(pointCloud.vtkActor)在该循​​环步骤中。

于 2013-07-22T06:05:24.297 回答