Python/vtk - 在 vtkPolyData 对象中单独设置每个点的大小? [英] Python/vtk - set each point size individually in a vtkPolyData object?

查看:131
本文介绍了Python/vtk - 在 vtkPolyData 对象中单独设置每个点的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following code to populate a point cloud: How to display point cloud in vtk in different colors?

这些点是新西兰现在地震的长度、纬度和深度.

The points are the long, lat and depth of the quakes in New Zealand right now.

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

I want to change the point size, an scale it by a fourth value - mag - this is the magnitude data for each quake in the swarm.

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

I'm really not familiar with the vtk wrapper, and wondered if anyone could point out at which point I can address the actor, and change each point size:

代码:

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 数据片段:

Snippet of points_and_mag data:

[[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

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

I have figure out how to change the whole set size, which makes me suspect the point cloud is one actor, not each instance of the point in the pointcloud:-

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()

推荐答案

啊,我想通了.

我只需要在遍历点的 for 循环中包含 pointCloud = VtkPointCloud() 的构造函数,然后 renderer.AddActor(pointCloud.vtkActor)在该循环步骤内.

I just needed to have the constructor for pointCloud = VtkPointCloud() inside the for loop that steps through the points, and then renderer.AddActor(pointCloud.vtkActor) inside that loop step.

这篇关于Python/vtk - 在 vtkPolyData 对象中单独设置每个点的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆