我正在處理一些可操縱3D三角形網(wǎng)格的代碼。導(dǎo)入網(wǎng)格數(shù)據(jù)后,需要“統(tǒng)一”空間中同一點(diǎn)的頂點(diǎn)。我一直以為numpy數(shù)組是存儲(chǔ)和處理數(shù)據(jù)的最快方法,但是我似乎找不到一種快速構(gòu)建頂點(diǎn)列表同時(shí)又避免添加重復(fù)條目的方法。因此,要測(cè)試方法,請(qǐng)創(chuàng)建一個(gè)具有10000個(gè)唯一行的3x30000數(shù)組:import numpy as nppoints = np.random.random((10000,3))raw_data = np.concatenate((points,points,points))np.random.shuffle(raw_data)這可以很好地近似為網(wǎng)格數(shù)據(jù),每個(gè)點(diǎn)作為小平面頂點(diǎn)出現(xiàn)3次。在統(tǒng)一的同時(shí),我需要建立一個(gè)唯一的頂點(diǎn)列表。如果一個(gè)點(diǎn)已經(jīng)在列表中,則必須存儲(chǔ)對(duì)該點(diǎn)的引用。到目前為止,我能夠使用numpy得出的最好的結(jié)果是:def unify(raw_data): # first point must be new unified_verts = np.zeros((1,3),dtype=np.float64) unified_verts[0] = raw_data[0] ref_list = [0] for i in range(1,len(raw_data)): point = raw_data[i] index_array = np.where(np.all(point==unified_verts,axis=1))[0] # point not in array yet if len(index_array) == 0: point = np.expand_dims(point,0) unified_verts = np.concatenate((unified_verts,point)) ref_list.append(len(unified_verts)-1) # point already exists else: ref_list.append(index_array[0]) return unified_verts, ref_list使用cProfile進(jìn)行測(cè)試:import cProfilecProfile.run("unify(raw_data)")在我的計(jì)算機(jī)上,此過(guò)程運(yùn)行了5.275秒。盡管我已經(jīng)使用Cython加快了速度,但是據(jù)我所知,Cython的運(yùn)行速度通常不會(huì)比numpy方法快得多。關(guān)于如何更有效地執(zhí)行此操作的任何建議?
添加回答
舉報(bào)
0/150
提交
取消