Example #1
0
def get_difference(poly1, poly2, pixels=None):
    """attempt to get the difference poly1-poly2 as poly1^~poly2
        use a pixelation to attempt to find an outside point"""
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    m = Basemap(projection='moll', lon_0=0)
    if pixels is None:
        pixels = get_healpix_pixelation(4)
    bounding_xyz = list(poly2.points)
    #contained = np.zeros(pixels.shape[0],dtype==bool)
    #for itr in range(0,len(bounding_xyz)):
    #    ra,dec = sgv.vector_to_radec(
    #    contained = contained | contains_points(bounding_xyz[itr],pixels)
    contained = contains_points(pixels, poly2)
    poly2_complement = None
    #poly2_complement = poly2.invert_polygon()
    #contained_c = contains_points(pixels,poly2_complement)
    #assert not np.any(contained & contained_c)
    first_false = 100 + np.argmin(contained[100:])
    #print("orig 1",contains_points(pixels[first_false:first_false+1],poly1),poly1.area())
    #print("orig 2",contains_points(pixels[first_false:first_false+1],poly2),poly2.area())
    print(poly2)
    colors = ['red', 'green', 'blue']
    for itr in range(0, len(bounding_xyz)):
        first_false = 100 + itr + np.argmin(contained[100 + itr:])
        theta_in = pixels[first_false, 0]
        phi_in = pixels[first_false, 1]
        inside_xyz = np.asarray(
            sgv.radec_to_vector(phi_in, theta_in - np.pi / 2., degrees=False))
        loc_poly = SphericalPolygon(bounding_xyz[itr].copy(),
                                    inside_xyz.copy())
        loc_poly.draw(m, color=colors[itr])
        cont = contains_points(pixels[first_false:first_false + 1], loc_poly)
        print("loc contains: ", cont)
        if poly2_complement is None:
            poly2_complement = deepcopy(loc_poly)
        else:
            poly2_complement = deepcopy(
                poly2_complement.intersection(loc_poly))
        cont_comp = contains_points(pixels[first_false:first_false + 1],
                                    poly2_complement)
        print("comp contains: ", cont_comp)
        print("test: ",
              np.all(contains_points(pixels[contained], poly2_complement)))
        print("test: ",
              np.any(contains_points(pixels[~contained], poly2_complement)))
        print("insp: ", list(poly2_complement.inside))
        #print("comp ",itr,contains_points(pixels[first_false:first_false+1],poly2_complement),poly2_complement.area(),poly2_complement.is_clockwise())
        #print("inside c ",list(poly2_complement.inside))
        #print("vert c ",list(poly2_complement.points))
        #for itr2 in range(0,len(list(poly2_complement.inside))):
        #    print("loc cont inside c ",loc_poly.contains_point(list(poly2_complement.inside)[itr2]))
    #print(poly1.area(),poly2.area(),poly2_complement.area())
    plt.show()
    return poly1.intersection(poly2_complement)