#!/usr/bin/python # (c) by H. Weinhandl 04.Nov.2003 import math import dislin def dist( ia,ib ) : return math.sqrt( (X1[ia]-X1[ib])**2 + (Y1[ia]-Y1[ib])**2 ) def find_nearest_neighb() : print 'extracting neighbours ... ', for i in range( nr_dat+3 ) : # initualize list wit the point i itself neighb.append( [i] ) for i in range( nr_tri ) : # get a indes-triple, dislin seems to start indices with 1, # so I'm subtracting 1 to get a zero-based index U,V,W = I1[i]-1, I2[i]-1, I3[i]-1 # add indices from all triangles, which contain the point itself if ( U in neighb[U] ) : if not (V in neighb[U] ) : neighb[U].append( V ) # do not append V if already in the list if not (W in neighb[U] ) : neighb[U].append( W ) # do not append W if already in the list if ( V in neighb[V] ) : if not (U in neighb[V] ) : neighb[V].append( U ) # do not append U if already in the list if not (W in neighb[V] ) : neighb[V].append( W ) # do not append W if already in the list if ( W in neighb[W] ) : if not (U in neighb[W] ) : neighb[W].append( U ) # do not append U if already in the list if not (V in neighb[W] ) : neighb[W].append( V ) # do not append V if already in the list print ' OK' for i in range( nr_dat ) : neighb[i].remove( i ) # remove the point i itself neighb[i].sort() r_mi = 9.9e9 i_mi = i # search for the nearest neighbor of point i for j in neighb[i] : r = dist( i, j ) if r < r_mi : r_mi = r i_mi = j print 'NB %2d : r_mi=%5.3f, i_mi=%2d '% (i, r_mi, i_mi), neighb[i] if __name__ == '__main__' : X1 = [ 1.0, 2.0, 3.0, 4.0, 5.0, 4.0, 3.0 ] Y1 = [ 1.0, 6.0, 3.0, 3.0, 2.0, 6.0, 0.0 ] nr_dat = len( X1 ) nr_max = 2 * nr_dat + 1 I1 = [ 0 ] * nr_max I2 = [ 0 ] * nr_max I3 = [ 0 ] * nr_max neighb = [ ] # padding the 2 input-lists with 3 additional elements is required by dislin X1.append( 0 ) X1.append( 0 ) X1.append( 0 ) Y1.append( 0 ) Y1.append( 0 ) Y1.append( 0 ) # delaunay triangulation of the input lists # I1,I2,I3 are the indices of the triangular edge-points nr_tri = dislin.triang( X1,Y1, nr_dat, I1,I2,I3, nr_max ) print X1 print Y1 print nr_dat, nr_tri, nr_max print I1 print I2 print I3 find_nearest_neighb() #---- end ----