简体   繁体   English

Python:获取两个3D点之间的所有坐标或绘制3D线

[英]Python: Get all coordinates between two 3D points or draw a 3D Line

I have the following problem. 我有以下问题。 I have 3D point coordinates and I want to conenct them in an array or "draw a line" like you can do it in 2D with skimage( http://scikit-image.org/docs/0.13.x/api/skimage.draw.html#skimage.draw.line ). 我有3D点坐标,我想将它们连接到数组中或“画一条线”,就像可以使用skimage( http://scikit-image.org/docs/0.13.x/api/skimage在2D中做到的那样。 draw.html#skimage.draw.line )。 The optimal case would be if i could directly draw a cylinder with a radius in an array and give different radius different values like rings arround the line.(wrinkles could be a problem here) . 最佳情况是,如果我可以直接在数组中绘制一个半径为半径的圆柱体,并给不同的半径不同的值,例如圆环环绕在直线上。(这里的皱纹可能是个问题) There have been approaches to do that but they are not the right thing i think like here: 有一些方法可以做到这一点,但我认为这不是正确的选择:

"Draw" a 3d line into an array 将3D线“绘制”到数组中

With the two approaches of @Paul Panzer ( Fastest way to get all the points between two (X,Y) coordinates in python ) you get all coordinates between two 2D Points, but how would it look like in 3D, esspecially the second approach which is faster?: 使用@Paul Panzer的两种方法( 最快的方法来获取python中两个(X,Y)坐标之间的所有点 ),您将获得两个2D点之间的所有坐标,但是在3D中会是什么样子,尤其是第二种方法是比较快的?:

import numpy as np
from timeit import timeit

def connect(ends):
    d0, d1 = np.abs(np.diff(ends, axis=0))[0]
    if d0 > d1: 
        return np.c_[np.linspace(ends[0, 0], ends[1, 0], d0+1, dtype=np.int32),
                     np.linspace(ends[0, 1]+0.5, ends[1, 1]+0.5, d0+1, dtype=np.int32)]
    else:
        return np.c_[np.linspace(ends[0, 0]+0.5, ends[1, 0]+0.5, d1+1, dtype=np.int32),
                     np.linspace(ends[0, 1], ends[1, 1], d1+1, dtype=np.int32)]


def connect2(ends):
    d0, d1 = np.diff(ends, axis=0)[0]
    if np.abs(d0) > np.abs(d1): 
        return np.c_[np.arange(ends[0, 0], ends[1,0] + np.sign(d0), np.sign(d0), dtype=np.int32),
                     np.arange(ends[0, 1] * np.abs(d0) + np.abs(d0)//2,
                               ends[0, 1] * np.abs(d0) + np.abs(d0)//2 + (np.abs(d0)+1) * d1, d1, dtype=np.int32) // np.abs(d0)]
    else:
        return np.c_[np.arange(ends[0, 0] * np.abs(d1) + np.abs(d1)//2,
                               ends[0, 0] * np.abs(d1) + np.abs(d1)//2 + (np.abs(d1)+1) * d0, d0, dtype=np.int32) // np.abs(d1),
                     np.arange(ends[0, 1], ends[1,1] + np.sign(d1), np.sign(d1), dtype=np.int32)]


ends = np.array([[ 1520, -1140],
                 [ 1412,  -973]])

Since you tagged the question 'vtk', I assume you can use it in your code. 由于您标记了问题“ vtk”,因此我假设您可以在代码中使用它。 In that case, I think vtkTubeFilter does exactly what you are looking for - you create a vtkPolyData with all your line segments (you have your endpoints for that) and for each segment, the filter generates a cylinder. 在那种情况下,我认为vtkTubeFilter可以完全满足您的需求-您创建一个包含所有线段的vtkPolyData (具有该线段的端点),并且对于每个线段,过滤器都会生成一个圆柱。 You can also specify different radius for each segment as you wanted: call SetVaryRadiusToVaryRadiusByScalar() on the filter to turn it on and provide the array with radii as scalar data. 您还可以根据需要为每个线段指定不同的半径:在过滤器上调用SetVaryRadiusToVaryRadiusByScalar()将其打开,并为半径数组提供标量数据。

You can find a code example that does all that (and more) here: https://www.vtk.org/Wiki/VTK/Examples/Cxx/VisualizationAlgorithms/TubesWithVaryingRadiusAndColors 您可以在此处找到执行所有(以及更多)操作的代码示例: https : //www.vtk.org/Wiki/VTK/Examples/Cxx/VisualizationAlgorithms/TubesWithVaryingRadiusAndColors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM