Python:Vtk - 使用坐标

时间:2017-04-24 09:02:00

标签: python arrays numpy vtk itertools

是否有更快的方法将x,y,z的所有坐标组合比itertools.product更多地放入polyData?中。例如,我有范围x:10-310,y:10-310和z:0-65。也许就像将一个由mgrid创建的整个numpy数组放到polyData?

修改

例如,我必须将所有坐标放在这样的聚合数据中:

import itertools
import vtk 

x1=[10,11,12...310]
y1=[10,11,12...310]
z1=[0,1,2...65]
points1 = vtk.vtkPoints()                      
for coords in itertools.product(x1,y1,z1):
   points1.InsertNextPoint(coords)
boxPolyData1 = vtk.vtkPolyData()
boxPolyData1.SetPoints(points1)

EDIT2:整个示例

这里Grid非常小,因为只有2个点。但是整个数据集的网格将是310 * 310 * 50

import vtk
import itertools
import math
import csv

radius=3
fcr_size=2
centoEdge = radius+fcr_size

line = vtk.vtkLineSource()
line.SetPoint1(310.868,52.443,9.462)
line.SetPoint2(302.123,65.211,24.095)
line.Update()

tube1 = vtk.vtkTubeFilter()
tube1.SetInputConnection(line.GetOutputPort())
tube1.SetRadius(radius)
tube1.SetNumberOfSides(10)
tube1.CappingOn()
tube1.Update()

tubePolyData1 = tube1.GetOutput()

tube2 = vtk.vtkTubeFilter()
tube2.SetInputConnection(line.GetOutputPort())
tube2.SetRadius(radius+fcr_size)
tube2.SetNumberOfSides(10)
tube2.CappingOn()
tube2.Update()

tubePolyData2 = tube2.GetOutput()


x1 = range(int(math.floor(min(line.GetPoint1()[0],line.GetPoint2()[0])-centoEdge)),int(math.floor(max(line.GetPoint1()[0],line.GetPoint2()[0])+centoEdge)+1))
y1 = range(int(math.floor(min(line.GetPoint1()[1],line.GetPoint2()[1])-centoEdge)),int(math.floor(max(line.GetPoint1()[1],line.GetPoint2()[1])+centoEdge)+1))
z1 = range(int(math.floor(min(line.GetPoint1()[2],line.GetPoint2()[2])-centoEdge)),int(math.floor(max(line.GetPoint1()[1],line.GetPoint2()[1])+centoEdge)+1))


 ####The Important Part########## 

 points1 = vtk.vtkPoints()                      
 for coords in itertools.product(x1,y1,z1):
   points1.InsertNextPoint(coords)

 ################################## 




boxPolyData1 = vtk.vtkPolyData()
boxPolyData1.SetPoints(points1)

selectEnclosedPoints1 = vtk.vtkSelectEnclosedPoints()
selectEnclosedPoints1.SetInputData(boxPolyData1)
selectEnclosedPoints1.SetSurfaceData(tubePolyData1)
selectEnclosedPoints1.Update()

selectEnclosedPoints2 = vtk.vtkSelectEnclosedPoints()
selectEnclosedPoints2.SetInputData(boxPolyData1)
selectEnclosedPoints2.SetSurfaceData(tubePolyData2)
selectEnclosedPoints2.Update()

tubeCoor=[]
count=0
for coords in range(points1.GetNumberOfPoints()):
    if (selectEnclosedPoints.IsInside(count)==False) and (selectEnclosedPoints2.IsInside(count)==True):
                val1 = int(sparse1.get(points1.GetPoint(coords),"0")[0]) + 1
                sparse1[points1.GetPoint(coords)] = [str(val1)]
    count+=1

with open('TubeTest2.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file, delimiter=";")
    for i in tubeCoor:
        # Writing floats to decimals to string for less storage in csv file
        writer.writerow(['{:.1f}'.format(t) for t in i])

enter image description here

0 个答案:

没有答案