繁体   English   中英

如何使用 Python 中的规则网格的点创建线和曲面

[英]how to create lines and surfaces using points of a regular grid in Python

我有一堆点,想对它们做一些几何处理。 点创建一个规则网格,包括垂直(红色)和水平(蓝色)线(上传了无花果)。 我想使用数字以及点的坐标( x,y,z )。 例如,我的无花果场景 A 中的点数和坐标为:

point_no= np.arange (1,9)
point_A_coordinate=np.array([[0,0,2], [0,1,3], [0,2,2], [1,0,1], [1,1,3], [2,0,1], [2,1,1], [3,0,1]]

我有每列中的点数(我的意思是红线)(如图中的红色圆圈所示)。 在场景 A 中是:

chunk_val_A=np.array([3, 2, 2, 1])

chunk_val_A[0]为 3 时,这意味着我在该块中有 2 条红线。 第一个是通过将点 1 连接到 2 来创建的,第二行是通过点 2 到 3 创建的。然后,我的算法应该知道点号 3 一定不能连接到 4,因为 chunk_val_A[0] 是 3。在下一个块中,我开始通过将 4 连接到 5 并在 5 处停止,因为chunk_val_A[0] + chunk_val_A[1]等于 5。作为 output 我想要将创建红线的点数成对:

[(1, 2), (2, 3), (4, 5), (6, 7)]

对于蓝色(连接块的线)线,它有点复杂。 关于从块i到块i+1的连接线数,如果chunk_val数组是下降的(场景 A 和 C),它等于chunk_val_A[i+1] ,如果它是上升的(场景 B 和 D)它等于chunk_val_A[i] 但在场景中,EI 既有上升又有下降,我想将规则视为上升的位置,然后从开始下降的位置更改它。 我可能有一些高峰,但我想保持和以前一样的规则。 到 select 哪个对创建蓝线,如果chunk_val[i]chunk_val[i+1]的值相同,只需简单地将每个块的第一个配对,然后,秒等(如场景 AI 配对点数字 4-6 和 8-7 连接第二和第三块)。 在其他情况下(例如所有场景的第一块和第二块),我应该检查坐标。 在场景 A 中,第二个块的点小于第一个,如果我检查点point_A_coordinate[:,0:2]xy ,缺失点是第二个的最后一个点(它的xy应该是 1 和 2)块。 所以两条连接线将从第一个块的下部开始。 在从第一个块到第二个块的场景 B 中,第一个错过了较低的点,因此唯一的线与第二个块的上点相连。 最后,我想为场景 A 的蓝线设置这样的对:

[(1, 4), (2, 5), (4, 6), (5, 7), (6, 8)]

创建这样的线条后,我想用它们来制作表面。 如果算法可以先重新排列并编号它们,然后是蓝色的,我认为有可能找出这些线条可以配对的位置。 在场景中,AI 在矩形中显示了行号。 我想让这些对在场景 A 中创建表面:

[(1, 5, 3, 6), (3, 7, 4, 8)]

作为最后一个问题,我想提取接近红色虚线的行数。 在场景 A 中,它们是(我在所有场景中都用叉号标记了它们):

[(2, 6, 8, 4, 9)]

这些是我想在 Python 中做的事情。 如果有人在任何步骤中帮助我,我将不胜感激。 对于这么长的问题,我非常感谢任何帮助。 提前致谢。 我尝试了以下代码来创建行,但并非所有情况都成功(我只是使用 print function 来显示这些对):

cord_A=np.array([[0,0,2], [0,1,3], [0,2,2], [1,0,1], [1,1,3], [2,0,1], [2,1,1], [3,0,1]])
cord_B=np.array([[0,2,2], [1,1,3], [1,2,2], [2,1,1], [2,2,3], [3,0,1], [3,1,1], [3,2,1]])
cord_C=np.array([[0,0,2], [0,1,3], [0,2,2], [1,1,1], [1,2,3], [2,1,1], [2,2,1], [3,2,1]])
cord_D=np.array([[0,0,2], [1,0,3], [1,1,2], [2,0,1], [2,1,3], [3,0,1], [3,1,1], [3,2,1]])

chunk_val_A=np.array([3, 2, 2, 1])
chunk_val_C=np.array([3, 2, 2, 1])
chunk_val_B=np.array([1, 2, 2, 3])
chunk_val_D=np.array([1, 2, 2, 3])

cord=np.array([[0,0,2], [0,1,3], [0,2,2], [1,0,1], [1,1,3], [2,0,1], [2,1,1], [3,0,1]]) # each time use one scenario
chunk_val=np.array([3, 2, 2, 1]) # again from the same scenario
summ=np.cumsum(chunk_val)
countinuous_point=np.arange(1,9)
splited_point=np.split(countinuous_point,np.cumsum(chunk_val))[:-1]
for i in countinuous_point:
    if i in summ:
        continue
    print (i, i+1) # it gives red lines
print ('All')
for j in range (len (chunk_val)-1):
    if chunk_val[j]==chunk_val[j+1]:
        for m, n in zip (splited_point[j], splited_point[j+1]):
            print (m, n) # it gives the blue lines when the chunks have the same length
    else:
        if cord[splited_point[j][-1]-1,1] > cord[splited_point[j+1][-1]-1,1]:
            for h, p in zip (splited_point[j], splited_point[j+1]):
                print (h,p) # it gives the blue lines when the chunks are like scenario A
        if cord[splited_point[j][-1],1] > cord[splited_point[j+1][0],1]:
            for h, p in zip (splited_point[j][1:], splited_point[j+1]):
                print (h,p) # it gives the blue lines when the chunks are like scenario B               
        if cord[splited_point[j][0],1] < cord[splited_point[j+1][0],1]:
            for h, p in zip (splited_point[j][1:], splited_point[j+1]):
                print (h,p) # it gives the blue lines when the chunks are like scenario C
        if cord[splited_point[j][-1],1] < cord[splited_point[j+1][-1],1]:
            for h, p in zip (splited_point[j][1:], splited_point[j+1]):
                print (h,p) # it gives the blue lines when the chunks are like scenario D

在此处输入图像描述

这将是我提取表面的解决方案。 我希望这可以帮助你。 虽然没有实现找到靠近红色虚线的边缘的解决方案。

import numpy as np

point_no= np.arange (1,9)
point_A_coordinate=np.array([[0,0,2], [0,1,3], [0,2,2], [1,0,1], [1,1,3], [2,0,1], [2,1,1], [3,0,1]])
chunk_val_A=np.array([3, 2, 2, 1])

max_x = max([x for x,y,z in point_A_coordinate])
max_y = max([y for x,y,z in point_A_coordinate])

points = np.full((max_x+1,max_y+1), -1) # Grid for storing points -1 marks that there is no point.
red_lines = []
blue_lines = []
n = 0
for chunk, chunk_val in enumerate(chunk_val_A):
    for i in range(chunk_val):
        points[chunk,i] = 1 # 1 markes that there is a point
        n += 1
        if i > 0 and points[chunk,i-1] >= 0: red_lines.append( ((chunk,i-1), (chunk,i)) ) #adds red lines
        if chunk > 0 and points[chunk-1,i] >= 0: blue_lines.append(((chunk-1, i), (chunk,i))) # adds blue lines

print(points) # contains all your points like you would draw them on the paper. -1=no point . otherwise points z-kordinate
print(red_lines) # contains all red lines but named by there x,y cord-pairs
print(blue_lines) # contains all blue lines but named by there x,y cord-pairs

#surfaces
surfaces = []
for x in range(max_x):
    for y in range(max_y):
        if( points[x,y] >= 0 and # checks if all for points exist
            points[x+1,y] >= 0 and
            points[x,y+1] >= 0 and
            points[x+1,y+1] >= 0):
                surfaces.append((
                    red_lines.index( ((x,y), (x,y+1)) ) +1, # gets the number of each line and adds it to the surface
                    blue_lines.index(((x, y), (x + 1, y))) + len(red_lines) + 1,
                    red_lines.index(((x + 1, y), (x + 1, y + 1))) + 1,
                    blue_lines.index( ((x, y+1), (x+1, y+1))) + len(red_lines) + 1,
                ))

print(surfaces) # contains all surfaces

red_lines_numbers = [red_lines.index( red_line ) +1 for red_line in red_lines] 
blue_lines_numbers = [blue_lines.index( blue_line ) + len(red_lines) +1 for blue_line in blue_lines]

red_lines_points_numbers = [(
        [ i for i,cords in enumerate(point_A_coordinate) if all(cords[:2] == red_line[0])][0]+1,
        [ i for i,cords in enumerate(point_A_coordinate) if all(cords[:2] == red_line[1])][0]+1
    ) for red_line in red_lines]

blue_lines_points_numbers = [(
        [ i for i,cords in enumerate(point_A_coordinate) if all(cords[:2] == blue_line[0])][0]+1,
        [ i for i,cords in enumerate(point_A_coordinate) if all(cords[:2] == blue_line[1])][0]+1
    ) for blue_line in blue_lines]

你真的加强了编号游戏。 大多数代码仅用于处理您的复杂编号。 我首先将所有内容都转换为各自的 x,y 坐标,然后再转换回来,因为使用它进行数学运算更直观。

暂无
暂无

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

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