0

我有一百万个xyzc点。

我将遍历python中的每个点。

  • 将其作为节点添加到图形中。
  • 使用 python 中的 python 绑定计算其最近的邻居
  • 将其每个最近的邻居作为节点添加到图形中
  • 从点添加边到它的每个邻居。
  • 将权重分配给边缘,这是两个点标量 c 值的乘积。

然后要求我的图表的最小生成树。

那么我如何告诉项目 x 从点 (2,1,3) 开始并启动深度搜索,遍历每个点,如果它们与连续点 c 值相乘 - 用 -c 替换后面的点 c 值

谢谢

4

1 回答 1

1
import numpy as np
from sklearn.neighbors import NearestNeighbors
import networkx as nx

#Get coordinates
f_name = 'horse'
coord = np.genfromtxt(str(f_name)+'.txt',autostrip=True)
coord = np.hstack((coord, np.zeros((coord.shape[0], 3), dtype=coord.dtype))) 


def Normals(XYZ):

    #The below code uses the PCA Eigen method to fit plane.

    #Get the covariance matrix
    average=sum(XYZ)/XYZ.shape[0]
    b = np.transpose(XYZ - average)
    cov=np.cov(b)

    #Get eigen val and vec
    e_val,e_vect = np.linalg.eigh(cov)

    #Diagonlize eigen vals
    e_val_d = np.diag(e_val)

    #Find min eigen val
    h = np.rank(min(e_val))

    #Find normal
    norm =  e_vect[:,h]

    #Calc curvature
    curvature = e_val[0]/(e_val[0]+e_val[1]+e_val[2])

    return norm[0],norm[1],norm[2]


print 'CALCULATE NORMALS'
k = 8
neigh = NearestNeighbors(k) 
neigh.fit(coord)

for i in range(0,len(coord)):

     #Determine the neighbours of point 
    d = neigh.kneighbors(coord[i]) 

    #Add coordinates of neighbours , dont include center point to array. Determine coordinate by the index of the neighbours.
    print i*100/len(coord)
##    k = 8 #number neighbours
    y = np.zeros((k-1,3))
    for c in range(1,k):
              #Get coordinate
              y[c-1,0] = coord[d[1][0][c],0]
              y[c-1,1] = coord[d[1][0][c],1]
              y[c-1,2] = coord[d[1][0][c],2]
    #Get information content 
    b = Normals(y) 

    #Assign information content to each point i.e xyzb
    coord[i,3] = b[0]
    coord[i,4] = b[1]
    coord[i,5] = b[2]



print 'CALCULATE NORMAL ORIENTATION'

#coord = np.random.randn(40, 6)

k=4
###Fit nearest neighbours to coordinates
neigh = NearestNeighbors(k) 
neigh.fit(coord[:,:3])

#Get the point with highest z value , this will be used as the starting point for my depth search
z_max_point = np.where(coord[:,2]==np.max(coord[:,2]))
z_max_point=int(z_max_point[0])
print z_max_point

if coord[z_max_point,5] < 0 : #ie normal doesnt point out
  coord[z_max_point,3:6]=-coord[z_max_point,3:6]
  print 'flip'
#Create a graph
G = nx.Graph() 

#Add all points and there neighbours to graph, make the weight equal to the distance between points
for i in range(0,len(coord)):


    d = neigh.kneighbors(coord[i,:3]) 

    for c in range(1,k):
        p1 = d[1][0][0]
        p2 = d[1][0][c]
        n1 = coord[d[1][0][0],3:6]
        n2 = coord[d[1][0][c],3:6]
        dot = np.dot(n1,n2)

        G.add_edge(p1,p2,weight =1-np.abs(dot))



T=nx.minimum_spanning_tree(G)

x=[]
for i in nx.dfs_edges(T,z_max_point):
    x+=i



inds = np.where(np.diff(x))[0]
out = np.split(x,inds[np.diff(inds)==1][1::2]+1)

print len(out)
for j in range(0,len(out)):
    print j*100/len(out)
    for i in range(0,len(out[j])-1):


        n1 = coord[out[j][i],3:6]

        n2 = coord[out[j][i+1],3:6]



        if np.dot(n2,n1)<0:

            coord[out[j][i+1],3:6]=-coord[out[j][i+1],3:6]




np.savetxt('horsenormalsorientated.txt',coord)
于 2013-09-26T22:06:15.000 回答