使用vtk给定一组点创建多条折线 [英] create multiple polylines given a set of points using vtk
问题描述
我需要使用vtk和paraview显示50个粒子的空间中的轨迹。目前,我的数据是 pos(x,y,t,n)
,其中n是第n个粒子的标签。我已将所有数据保存在一个vtk文件中,该文件的组织方式为:
I need to display the trajectories in space of 50 particles using vtk and paraview. Currently my data is pos(x,y,t,n)
where n is the label of the n-th particle. I have saved all my data in a vtk file which is organized as:
# vtk DataFile Version 3.0
VTK from Matlab
BINARY
DATASET POLYDATA
POINTS 199250 double
[PROPERLY ORGANIZED BINARY COORDINATES OF THE 199250 POINTS]
以上文件可以正确导入ParaView,乍一看,我可以应用以下programmagle过滤器
The above file can be imported correctly into ParaView, and at a first glance I can apply the following programmagle filter
pdi = self.GetPolyDataInput()
pdo = self.GetPolyDataOutput()
newPoints = vtk.vtkPoints()
numPoints = pdi.GetNumberOfPoints()
for i in range(0, numPoints):
coord = pdi.GetPoint(i)
x, y, z = coord[:3]
x = x * 0.03
y = y * 0.03
z = z * 2
newPoints.InsertPoint(i, x, y, z)
pdo.SetPoints(newPoints)
aPolyLine = vtk.vtkPolyLine()
aPolyLine.GetPointIds().SetNumberOfIds(numPoints)
for i in range(0,numPoints):
aPolyLine.GetPointIds().SetId(i, i)
pdo.Allocate(1, 1)
pdo.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds())
创建以下图形
过滤器基本上会创建一条连接其他所有点的线,但在在这种情况下,它将一个粒子轨迹的最后一个点连接到以下轨迹的第一个点。
The filter basically creates a line connecting every other point, but in my case it connects the last point of one particle trajectory to the first one of the following trajectory.
我想为每个粒子创建一个不同的多数据对象,所以我可以甚至分别给它们贴标签或上色(并消除全连接问题)。我对类似过滤器的首次猜测是
I would like to create a different polydata object for every particle, so I could even label or color them separately (and eliminate the "all-connected" issue). My first guess for a similar Filter would be
pdi = self.GetPolyDataInput()
pdo = self.GetPolyDataOutput()
nParts = 50
newPoints = vtk.vtkPoints()
numPoints = pdi.GetNumberOfPoints()
numPointsPart = int(numPoints/nParts)
for i in range(0, numPoints):
coord = pdi.GetPoint(i)
x, y, z = coord[:3]
x = x * 0.03
y = y * 0.03
z = z * 2
newPoints.InsertPoint(i, x, y, z)
pdo.SetPoints(newPoints)
pdo.Allocate(nParts, 1)
for i in range(0,nParts):
aPolyLine = vtk.vtkPolyLine()
aPolyLine.GetPointIds().SetNumberOfIds(numPointsPart)
indStart=int(i*numPointsPart)
indEnd=int((i+1)*numPointsPart)
for i in range(indStart,indEnd):
aPolyLine.GetPointIds().SetId(i, i)
pdo.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds())
但是,当我运行它时,ParaView只是关闭由于段错误。奖励:代码编写正确:如果将nParts设置为1,则将获得与上述相同的图形。
However, when I run this, ParaView just closes due to segfault. Bonus: the code is written correctly: if I set nParts to 1, I get the same graph as above.
编辑:可以找到数据集此处
EDIT : the dataset can be found here
推荐答案
问题出在迭代逻辑中:
for i in range(indStart, indEnd):
aPolyLine.GetPointIds().SetId(i, i)
更改为以下内容:
for i in range(indStart, indEnd):
aPolyLine.GetPointIds().SetId(i - indStart, i)
原始代码调用了 SetId
具有错误的索引。另外,请注意带阴影的变量 i
。尽管这不是问题的根源,但如果在第二个 for
循环之后使用 i
,则结果可能不是您会期望的。
The original code was calling SetId
with an incorrect index. Also, note the shadowed variable i
. Although that's not what's causing the issue, if you use i
after the second for
loop, the result may not be what you'd be expecting.
这篇关于使用vtk给定一组点创建多条折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!