簡體   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