gpt4 book ai didi

Python/vtk - 在 vtkPolyData 对象中单独设置每个点大小?

转载 作者:太空宇宙 更新时间:2023-11-04 03:57:00 58 4
gpt4 key购买 nike

我正在使用以下代码来填充点云:How to display point cloud in vtk in different colors?

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

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

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

代码:

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

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

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)在那个循环步骤中。

关于Python/vtk - 在 vtkPolyData 对象中单独设置每个点大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779491/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com