使用 vtk 给定一组点创建多条折线 [英] create multiple polylines given a set of points using vtk

查看:56
本文介绍了使用 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.

我想为每个粒子创建一个不同的 polydata 对象,这样我什至可以分别标记或着色它们(并消除全连接"问题).我对类似过滤器的第一个猜测是

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.

数据集可以在这里找到

推荐答案

问题出在迭代逻辑上:

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屋!

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