我正在寻找一种连接所有具有相同坡度并共享一个公共点的线的解决方案。例如,加载STL文件并使用平面进行剪切后,刀具输出包括定义轮廓的点。将它们逐个连接形成一条(或多条)多段线。但是,如果某些直线的坡度相同并且共享一个公共点,则可以合并这些直线。E、 例如,[[0,0,0]、[0,0,1]]和[[0,0,1]、[0,0,2]]可以用一条直线[[0,0,0]、[0,0,2]]表示。
我写了一个函数,可以分析所有的行,如果可以合并的话,可以连接它们。但当线路数量巨大时,这一过程就很慢。我在想,在VTK管道中,有没有办法进行线路合并?
干杯
plane = vtk.vtkPlane()
plane.SetOrigin([0,0,5])
plane.SetNormal([0,0,1])
cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInput(triangleFilter.GetOutput())
cutter.Update()
cutStrips = vtk.vtkStripper()
cutStrips.SetInputConnection(cutter.GetOutputPort())
cutStrips.Update()
cleanDataFilter = vtk.vtkCleanPolyData()
cleanDataFilter.AddInput(cutStrips.GetOutput())
cleanDataFilter.Update()
cleanData = cleanDataFilter.GetOutput()
print cleanData.GetPoint(0)
print cleanData.GetPoint(1)
print cleanData.GetPoint(2)
print cleanData.GetPoint(3)
print cleanData.GetPoint(4)
输出为:
(0.0, 0.0, 5.0)
(5.0, 0.0, 5.0)
(10.0, 0.0, 5.0)
(10.0, 5.0, 5.0)
(10.0, 10.0, 5.0)
将上述点逐个连接将形成表示切割结果的多段线。如我们所见,[点0,点1]和[点1,点2]可以合并。
下面是合并行的代码:
假设行由列表表示:[[(p0),(p1)],[(p1),(p2)],[(p2),(p3)],…]
appended = 0
CurrentLine = LINES[0]
CurrentConnectedLine = CurrentLine
tempLineCollection = LINES[1:len(LINES)]
while True:
for HL in tempLineCollection:
QCoreApplication.processEvents()
if checkParallelAndConnect(CurrentConnectedLine, HL):
appended = 1
LINES.remove(HL)
CurrentConnectedLine = ConnectLines(CurrentConnectedLine, HL)
processedPool.append(CurrentConnectedLine)
if len(tempLineCollection) == 1:
processedPool.append(tempLineCollection[0])
LINES.remove(CurrentLine)
if len(LINES) >= 2:
CurrentLine = LINES[0]
CurrentConnectedLine = CurrentLine
tempLineCollection = LINES[1:len(LINES)]
appended = 0
else:
break
解决方案:
我找到了一种使用vtk数据结构进一步加速此过程的方法。我发现多段线将存储在单元格中,可以使用GetCellType()进行检查。由于多段线的点顺序已经排序,因此不需要全局搜索哪些线与当前线共线。对于多段线上的每个点,我只需要检查点[I-1]、点[I]、点[I+1]。如果它们是共线的,则线的端点将更新到下一个点。此过程将继续,直到到达多段线的终点。与全局搜索方法相比,速度大幅提高。