问题
I am trying to ensure a global correct orientation of my normals. I.e make sure they point outwards everywhere.
This is my method:
- I have a coordinate list
x y c n
. At each point in coordinate list I create edges from the point to its 5 nearest neighbours. - I Weight each edge with (
1-n1.n2
). - Compute a minimal spanning tree.
- Traverse this tree in depth first order.
- Start traversing at the point with the greatest
z
value, force the normal of this point to be orientated towards the positivez
axis. - Assign orientation to subsequent points that are consistent with their preceeding point, for example if current point
p1
has orientationn1
, and next point visited isp2
, ifn1.n2<0
thenn2
is replaced by-n2
.
I am not sure how to do the depth first traversing and implement steps 5,6. My code is below.
Many thanks!
import numpy as np
from sklearn.neighbors import NearestNeighbors
import networkx as nx
#Get coordinates
f_name = 'Bunnylarge'
coord = np.genfromtxt(str(f_name)+'.txt',autostrip=True)
#Fit nearest neighbours to coordinates
neigh = NearestNeighbors(5)
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])
#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])
k = 5
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-dot)
#Get minimum spanning tree of graph
#M = nx.minimum_spanning_edges(G,weight = 'weight',data=True)
M = nx.minimum_spanning_edges(G)
edgelist = list(M)
#Convert minimum spanning tree into graph to be able to depth searched
D = nx.from_edgelist(edgelist)
if coord[z_max_point,5] < 0 : #ie normal doesnt point out
coord[z_max_point,3:6]=-coord[z_max_point,3:6]
#Compute the DFS, start at the z_max_point
T = nx.dfs_tree(D,source = z_max_point)
#Go through the depth searched order and if subsequent points have for example a n1*n2<0 then make n2=-n2
#for i in list(T.edges()):
#for i in nx.dfs_successors(D,z_max_point):
n1 = coord[z_max_point,3:6]
#for i in nx.dfs_postorder_nodes(D,z_max_point):
for i in nx.dfs_tree(D,z_max_point):
print i
n2 = coord[i,3:6]
if np.dot(n1,n2)<0:
coord[i,3:6]=-coord[i,3:6]
n2 = n1
print 'done'
np.savetxt('testnormals2.xyz',coord)
来源:https://stackoverflow.com/questions/18928264/network-x-which-depth-searching-option-to-select-for-this-use