def test_remove_duplicates(): ''' remove_duplicates() : normal case, latlons ''' from duplicate import remove_duplicates # normal case xyzs = [(0,0.5,0),(-0.5,0,0),(0.5,0,0),(1,1.2,0),(0.5,0,0)] ret = remove_duplicates(xyzs) equal(ret, [(0,0.5,0),(-0.5,0,0),(0.5,0,0),(1,1.2,0)]) xyzs = [(0,0.5,0),(-0.5,0,0),(0.5,0,0),(1,1.2,0),(0.5,0,0),(1.2,2.3,0),(-0.5,0,0)] ret = remove_duplicates(xyzs) equal(ret, [(0,0.5,0),(-0.5,0,0),(0.5,0,0),(1,1.2,0),(1.2,2.3,0)])
def test_sort_ccw_idxs(): ''' sort_ccw_idxs(): normal case, straight line, duplicated ''' from sphere import sort_ccw_idxs from duplicate import remove_duplicates from convert_coord.cart_ll import latlon2xyz # normal lls = [(0.79,0.79), (0.78,0.77), (0.78,0.79), (0.79,0.77), (0.80,0.78)] xyzs = [latlon2xyz(*ll) for ll in lls] ret = sort_ccw_idxs(xyzs) a_equal(ret, [0,4,3,1,2]) # straight line lls = [(0.79,0.79), (0.78,0.77), (0.78,0.79), \ (0.79,0.77), (0.80,0.78), (0.78,0.78)] xyzs = [latlon2xyz(*ll) for ll in lls] ret = sort_ccw_idxs(xyzs) a_equal(ret, [0,4,3,1,5,2]) #----------------------------------------------------- # duplicated #----------------------------------------------------- lls = [(-0.34784230590688509, 6.1959188445798699), (-0.3478423059068852, 0.08726646259971646), (-0.52194946399942688, 0.08726646259971646), (-0.52194946399942688, 6.1959188445798699), (-0.52194946399942688, 6.1959188445798699)] xyzs = [latlon2xyz(*ll) for ll in lls] unique_xyzs = remove_duplicates(xyzs) ret = sort_ccw_idxs(unique_xyzs) a_equal(ret, [0,3,2,1]) #----------------------------------------------------- lls = [(-1.3956102462281967, 0.43633231299858227), (-1.3956102462281967, 0.26179938779914985), (-1.5707963267948966, 0), (-1.5707963267948966, 0)] xyzs = [latlon2xyz(*ll) for ll in lls] unique_xyzs = remove_duplicates(xyzs) ret = sort_ccw_idxs(unique_xyzs) a_equal(ret, [0,1,2])
def intersect_two_polygons(polygon_xyzs1, polygon_xyzs2): ''' The points of polygons should be sorted in counter-clockwise ''' xyzs1 = polygon_xyzs1 xyzs2 = polygon_xyzs2 #----------------------------------------------------------- # Check in/out #----------------------------------------------------------- poly1_inout = list() for xyz in xyzs1: inout = pt_in_polygon(xyzs2, xyz) poly1_inout.append(inout) poly2_inout = list() for xyz in xyzs2: inout = pt_in_polygon(xyzs1, xyz) poly2_inout.append(inout) #----------------------------------------------------------- # Not overlap and inclusion #----------------------------------------------------------- if 'out' not in poly1_inout and 'in' not in poly2_inout: return xyzs1 elif 'in' not in poly1_inout and 'out' not in poly2_inout: return xyzs2 #----------------------------------------------------------- # Partially overlapped #----------------------------------------------------------- vertices = list() #--------------------------------- # inner and border points #--------------------------------- for seq, io1 in enumerate(poly1_inout): if io1 != 'out': vertices.append( xyzs1[seq] ) for seq, io2 in enumerate(poly2_inout): if io2 != 'out': vertices.append( xyzs2[seq] ) #--------------------------------- # intersection points #--------------------------------- for xyz1, xyz2 in zip(xyzs1, xyzs1[1:]+xyzs1[:1]): for xyz3, xyz4 in zip(xyzs2, xyzs2[1:]+xyzs2[:1]): ret = intersect_two_arcs(xyz1, xyz2, xyz3, xyz4) if ret != None and 'pt' not in ret: vertices.append(ret) #--------------------------------- # remove duplicates #--------------------------------- unique_xyzs = remove_duplicates(vertices) if len(vertices) < 3: return None #--------------------------------- # sorting in counter-clockwise #--------------------------------- sort_idxs = sort_ccw_idxs(unique_xyzs) sorted_xyzs = [unique_xyzs[idx] for idx in sort_idxs] return sorted_xyzs