【发布时间】:2015-05-24 16:41:41
【问题描述】:
我有一个文件,其中:第 1 列是 x 坐标,第 2 列是 y,第 3 列是 z,第 4 列是与每个点关联的值。 我想绘制这些点,每个点都应该根据第 4 列进行着色。 我会在python中做到这一点。我在 Windows 上使用带有 vtk 和 vtk_visualizer 的 anaconda。 我有数百万积分。我发现更快的方法是使用 python-vtk。 这是我现在拥有的代码:
import vtk
import numpy as np
## DATA
# Generate random points w/ random RGB colors
n = 10**5
xyz = 100*np.random.rand(n, 3)
color = 10*np.random.rand(n, 1)
# Point size
point_size = 10
## COLORMAP
cmax = np.max(color)
cmin = np.min(color)
cmed = (cmax+cmin)/2
normalizzato = color / np.max( np.absolute(cmax), np.absolute(cmin) )
rgb = np.zeros((len(color), 3))
for i in range(0, len(color) ):
if color[i] >= cmed:
# Red
rgb[i][0] = 255*normalizzato[i]
if color[i] < cmed:
# Blue
rgb[i][2] = 255*normalizzato[i]
## VTK
# Create the geometry of a point (the coordinate)
points = vtk.vtkPoints()
# Create the topology of the point (a vertex)
vertices = vtk.vtkCellArray()
# Setup colors
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetName("Colors")
# Add points
for i in range(0, len(xyz)):
p = xyz[i]
id = points.InsertNextPoint(p)
vertices.InsertNextCell(1)
vertices.InsertCellPoint(id)
Colors.InsertNextTuple3(rgb[i][0], rgb[i][1], rgb[i][2])
# Create a polydata object
point = vtk.vtkPolyData()
# Set the points and vertices we created as the geometry and topology of the polydata
point.SetPoints(points)
point.SetVerts(vertices)
point.GetPointData().SetScalars(Colors)
point.Modified()
# Visualize
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
mapper.SetInput(point)
else:
mapper.SetInputData(point)
## ACTOR
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(point_size)
axes = vtk.vtkAxesActor()
## RENDER
renderer = vtk.vtkRenderer()
# Add actor to the scene
renderer.AddActor(actor)
# Background
renderer.SetBackground(0.1, 0.2, 0.3)
# Reset camera
renderer.ResetCamera()
# Render window
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
# Interactor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Begin interaction
renderWindow.Render()
renderWindowInteractor.Start()
这是相当快的。 如您所见,有一个颜色条,但我无法获得正确的范围和颜色。有任何想法吗? 您对如何替换 ## COLORMAP 部分有什么建议吗? 我也尝试使用 mayavi.mlab.point3d 但它很慢而且这里的 vtk_visualizer 是代码:
from vtk_visualizer import *
import numpy as np
# Generate random points w/ random RGB colors
n = 10**6
xyz = np.random.rand(n, 3)
color = 10*np.random.rand(n, 1)
## Colormap
cmax = np.max(color)
cmin = np.min(color)
cmed = (cmax+cmin)/2
normalizzato = color / np.max( np.absolute(cmax), np.absolute(cmin) )
rgb = np.zeros((len(color), 3))
for i in range(0, len(color) ):
if color[i] >= cmed:
# Red
rgb[i][0] = 255*normalizzato[i]
if color[i] < cmed:
# Blue
rgb[i][2] = 255*normalizzato[i]
# Stack arrays in sequence horizontally (column wise).
pc = np.hstack([xyz,rgb])
# Plot them
plotxyzrgb(pc)
但它比 vtk 慢,我无法更改点的大小,有一个颜色条和轴。
谢谢
【问题讨论】:
标签: python python-2.7 point vtk mayavi