Exemple #1
0
def test_cc_int():
    """Generates two random circles, computes the intersection,
       and verifies that the number of intersections and the 
       positions of the intersections are correct. 
       Returns True or False"""
    # gen two random circles p1,r2 and p2, r2
    p1 = vector.randvec(2, 0.0, 10.0, 1.0)
    r1 = random.uniform(0, 10.0)
    p2 = vector.randvec(2, 0.0, 10.0, 1.0)
    r2 = random.uniform(0, 10.0)
    # 33% change that r2=abs(r1-|p1-p2|)
    if random.random() < 0.33:
        r2 = abs(r1 - vector.norm(p1 - p2))
    # do interesection
    diag_print(
        "problem:" + str(p1) + "," + str(r1) + "," + str(p2) + "," + str(r2),
        "test_cc_int")
    sols = cc_int(p1, r1, p2, r2)
    diag_print("solutions:" + str(map(str, sols)), "test_cc_int")
    # test number of intersections
    if len(sols) == 0:
        if not tol_gt(vector.norm(p2 - p1), r1 + r2) and not tol_lt(
                vector.norm(p2 - p1), abs(r1 - r2)) and not tol_eq(
                    vector.norm(p1 - p2), 0):
            diag_print("number of solutions 0 is wrong", "test_cc_int")
            return False
    elif len(sols) == 1:
        if not tol_eq(vector.norm(p2 - p1), r1 + r2) and not tol_eq(
                vector.norm(p2 - p1), abs(r1 - r2)):
            diag_print("number of solutions 1 is wrong", "test_cc_int")
            return False
    elif len(sols) == 2:
        if not tol_lt(vector.norm(p2 - p1), r1 + r2) and not tol_gt(
                vector.norm(p2 - p1), abs(r1 - r2)):
            diag_prin("number of solutions 2 is wrong")
            return False
    else:
        diag_print("number of solutions > 2 is wrong", "test_cc_int")
        return False

    # test intersection coords
    for p3 in sols:
        if not tol_eq(vector.norm(p3 - p1), r1):
            diag_print("solution not on circle 1", "test_cc_int")
            return False
        if not tol_eq(vector.norm(p3 - p2), r2):
            diag_print("solution not on circle 2", "test_cc_int")
            return False

    diag_print("OK", "test_cc_int")
    return True
Exemple #2
0
def test_cc_int():
    """Generates two random circles, computes the intersection,
       and verifies that the number of intersections and the 
       positions of the intersections are correct. 
       Returns True or False"""
    # gen two random circles p1,r2 and p2, r2
    p1 = vector.randvec(2, 0.0, 10.0,1.0)
    r1 = random.uniform(0, 10.0)
    p2 = vector.randvec(2, 0.0, 10.0,1.0)
    r2 = random.uniform(0, 10.0)
    # 33% change that r2=abs(r1-|p1-p2|)
    if random.random() < 0.33:
        r2 = abs(r1-vector.norm(p1-p2))
    # do interesection 
    diag_print("problem:"+str(p1)+","+str(r1)+","+str(p2)+","+str(r2),"test_cc_int")
    sols = cc_int(p1, r1, p2, r2)
    diag_print("solutions:"+str(map(str, sols)),"test_cc_int")
    # test number of intersections
    if len(sols) == 0:
        if not tol_gt(vector.norm(p2-p1),r1 + r2) and not tol_lt(vector.norm(p2-p1),abs(r1 - r2)) and not tol_eq(vector.norm(p1-p2),0):
            diag_print("number of solutions 0 is wrong","test_cc_int")
            return False
    elif len(sols) == 1: 
        if not tol_eq(vector.norm(p2-p1),r1 + r2) and not tol_eq(vector.norm(p2-p1),abs(r1-r2)):
            diag_print("number of solutions 1 is wrong","test_cc_int")
            return False
    elif len(sols) == 2:
        if not tol_lt(vector.norm(p2-p1),r1 + r2) and not tol_gt(vector.norm(p2-p1),abs(r1 - r2)):
            diag_prin("number of solutions 2 is wrong")
            return False
    else:
        diag_print("number of solutions > 2 is wrong","test_cc_int")
        return False

    # test intersection coords
    for p3 in sols:
        if not tol_eq(vector.norm(p3-p1), r1):
            diag_print("solution not on circle 1","test_cc_int")
            return False
        if not tol_eq(vector.norm(p3-p2), r2):
            diag_print("solution not on circle 2","test_cc_int")
            return False

    diag_print("OK","test_cc_int")
    return True
Exemple #3
0
def test_perp_3d():
    for i in range(10):
        u = normalised(vector.randvec(3))
        v, w = perp_3d(u)
        print u, vector.norm(u)
        print v, vector.norm(v)
        print w, vector.norm(w)
        print tol_eq(vector.dot(u, v), 0.0)
        print tol_eq(vector.dot(v, w), 0.0)
        print tol_eq(vector.dot(w, u), 0.0)
Exemple #4
0
def test_perp_3d():
    for i in range(10):
        u = normalised(vector.randvec(3))
        v,w = perp_3d(u)
        print u, vector.norm(u)
        print v, vector.norm(v)
        print w, vector.norm(w)
        print tol_eq(vector.dot(u,v),0.0) 
        print tol_eq(vector.dot(v,w),0.0) 
        print tol_eq(vector.dot(w,u),0.0) 
Exemple #5
0
def test_rr_int():
    """test random ray-ray intersection. returns True iff succesful"""
    # generate tree points A,B,C an two rays AC, BC.
    # then calculate the intersection of the two rays
    # and check that it equals C
    p_a = vector.randvec(2, 0.0, 10.0, 1.0)
    p_b = vector.randvec(2, 0.0, 10.0, 1.0)
    p_c = vector.randvec(2, 0.0, 10.0, 1.0)
    # print p_a, p_b, p_c
    if tol_eq(vector.norm(p_c - p_a), 0) or tol_eq(vector.norm(p_c - p_b), 0):
        return True  # ignore this case
    v_ac = (p_c - p_a) / vector.norm(p_c - p_a)
    v_bc = (p_c - p_b) / vector.norm(p_c - p_b)
    s = rr_int(p_a, v_ac, p_b, v_bc)
    if tol_eq(math.fabs(vector.dot(v_ac, v_bc)), 1.0):
        return len(s) == 0
    else:
        if len(s) > 0:
            p_s = s[0]
            return tol_eq(p_s[0], p_c[0]) and tol_eq(p_s[1], p_c[1])
        else:
            return False
Exemple #6
0
def test_rr_int():
    """test random ray-ray intersection. returns True iff succesful"""
    # generate tree points A,B,C an two rays AC, BC. 
    # then calculate the intersection of the two rays
    # and check that it equals C
    p_a = vector.randvec(2, 0.0, 10.0,1.0)
    p_b = vector.randvec(2, 0.0, 10.0,1.0)
    p_c = vector.randvec(2, 0.0, 10.0,1.0)
    # print p_a, p_b, p_c
    if tol_eq(vector.norm(p_c - p_a),0) or tol_eq(vector.norm(p_c - p_b),0): 
        return True # ignore this case
    v_ac = (p_c - p_a) / vector.norm(p_c - p_a)
    v_bc = (p_c - p_b) / vector.norm(p_c - p_b)
    s = rr_int(p_a, v_ac, p_b, v_bc)
    if tol_eq(math.fabs(vector.dot(v_ac, v_bc)),1.0): 
        return len(s) == 0
    else:
        if len(s) > 0:
            p_s = s[0]
            return tol_eq(p_s[0],p_c[0]) and tol_eq(p_s[1],p_c[1])
        else:
            return False
Exemple #7
0
def test_sss_int():
    p1 = vector.randvec(3, 0.0, 10.0, 1.0)
    p2 = vector.randvec(3, 0.0, 10.0, 1.0)
    p3 = vector.randvec(3, 0.0, 10.0, 1.0)
    p4 = vector.randvec(3, 0.0, 10.0, 1.0)
    #p1 = vector.vector([0.0,0.0,0.0])
    #p2 = vector.vector([1.0,0.0,0.0])
    #p3 = vector.vector([0.0,1.0,0.0])
    #p4 = vector.vector([1.0,1.0,1.0])
    d14 = vector.norm(p4 - p1)
    d24 = vector.norm(p4 - p2)
    d34 = vector.norm(p4 - p3)
    sols = sss_int(p1, d14, p2, d24, p3, d34)
    sat = True
    for sol in sols:
        # print sol
        d1s = vector.norm(sol - p1)
        d2s = vector.norm(sol - p2)
        d3s = vector.norm(sol - p3)
        sat = sat and tol_eq(d1s, d14)
        sat = sat and tol_eq(d2s, d24)
        sat = sat and tol_eq(d3s, d34)
        # print sat
    return sat
Exemple #8
0
def test_sss_int():
    p1 = vector.randvec(3, 0.0, 10.0,1.0)
    p2 = vector.randvec(3, 0.0, 10.0,1.0)
    p3 = vector.randvec(3, 0.0, 10.0,1.0)
    p4 = vector.randvec(3, 0.0, 10.0,1.0)
    #p1 = vector.vector([0.0,0.0,0.0])
    #p2 = vector.vector([1.0,0.0,0.0])
    #p3 = vector.vector([0.0,1.0,0.0])
    #p4 = vector.vector([1.0,1.0,1.0])
    d14 = vector.norm(p4-p1)
    d24 = vector.norm(p4-p2)
    d34 = vector.norm(p4-p3)
    sols = sss_int(p1,d14,p2,d24,p3,d34)
    sat = True
    for sol in sols:
        # print sol
        d1s = vector.norm(sol-p1)
        d2s = vector.norm(sol-p2)
        d3s = vector.norm(sol-p3)
        sat = sat and tol_eq(d1s,d14)
        sat = sat and tol_eq(d2s,d24)
        sat = sat and tol_eq(d3s,d34)
        # print sat
    return sat
Exemple #9
0
def test_cl_int():
    """Generates random circle and line, computes the intersection,
       and verifies that the number of intersections and the 
       positions of the intersections are correct. 
       Returns True or False"""
    # 3 random points
    p1 = vector.randvec(2, 0.0, 10.0, 1.0)
    p2 = vector.randvec(2, 0.0, 10.0, 1.0)
    p3 = vector.randvec(2, 0.0, 10.0, 1.0)
    # prevent div by zero / no line direction
    if tol_eq(vector.norm(p1 - p2), 0):
        p2 = p1 + p3 * 0.1
    # line (o,v): origin p1, direction p1-p2
    o = p1
    v = (p2 - p1) / vector.norm(p2 - p1)
    # cirlce (c, r): centered in p3, radius p3-p2 + rx
    c = p3
    r0 = vector.norm(p3 - p2)
    # cases rx = 0, rx > 0, rx < 0
    case = random.choice([1, 2, 3])
    if case == 1:
        r = r0  #should have one intersection (unles r0 = 0)
    elif case == 2:
        r = random.random() * r0  # should have no ints (unless r0=0)
    elif case == 3:
        r = r0 + random.random() * r0  # should have 2 ints (unless r0=0)
    # do interesection
    diag_print(
        "problem:" + str(c) + "," + str(r) + "," + str(o) + "," + str(v),
        "test_cl_int")
    sols = cl_int(c, r, o, v)
    diag_print("solutions:" + str(map(str, sols)), "test_cl_int")
    # distance from point on line closest to circle center
    l = vector.dot(c - o, v) / vector.norm(v)
    p = o + v * l / vector.norm(v)
    d = vector.norm(p - c)
    diag_print("distance center to line=" + str(d), "test_cl_int")
    # test number of intersections
    if len(sols) == 0:
        if not tol_gt(d, r):
            diag_print("wrong number of solutions: 0", "test_cl_int")
            return False
    elif len(sols) == 1:
        if not tol_eq(d, r):
            diag_print("wrong number of solutions: 1", "test_cl_int")
            return False
    elif len(sols) == 2:
        if not tol_lt(d, r):
            diag_print("wrong number of solutions: 2", "test_cl_int")
            return False
    else:
        diag_print("wrong number of solutions: >2", "test_cl_int")

    # test coordinates of intersection
    for s in sols:
        # s on line (o,v)
        if not is_colinear(s, o, o + v):
            diag_print("solution not on line", "test_cl_int")
            return False
        # s on circle c, r
        if not tol_eq(vector.norm(s - c), r):
            diag_print("solution not on circle", "test_cl_int")
            return False

    return True
Exemple #10
0
def test_cl_int():
    """Generates random circle and line, computes the intersection,
       and verifies that the number of intersections and the 
       positions of the intersections are correct. 
       Returns True or False"""
    # 3 random points
    p1 = vector.randvec(2, 0.0, 10.0,1.0)
    p2 = vector.randvec(2, 0.0, 10.0,1.0)
    p3 = vector.randvec(2, 0.0, 10.0,1.0)
    # prevent div by zero / no line direction
    if tol_eq(vector.norm(p1-p2),0):
        p2 = p1 + p3 * 0.1
    # line (o,v): origin p1, direction p1-p2
    o = p1
    v = (p2 - p1) / vector.norm(p2 - p1)
    # cirlce (c, r): centered in p3, radius p3-p2 + rx
    c = p3
    r0 = vector.norm(p3-p2)
    # cases rx = 0, rx > 0, rx < 0
    case = random.choice([1,2,3])
    if case==1:
        r = r0      #should have one intersection (unles r0 = 0)
    elif case==2:
        r = random.random() * r0   # should have no ints (unless r0=0)
    elif case==3:
        r = r0 + random.random() * r0 # should have 2 ints (unless r0=0) 
    # do interesection 
    diag_print("problem:"+str(c)+","+str(r)+","+str(o)+","+str(v),"test_cl_int")
    sols = cl_int(c,r,o,v)
    diag_print("solutions:"+str(map(str, sols)),"test_cl_int")
    # distance from point on line closest to circle center
    l = vector.dot(c-o, v) / vector.norm(v)
    p = o + v * l / vector.norm(v)  
    d = vector.norm(p-c)
    diag_print("distance center to line="+str(d),"test_cl_int")
    # test number of intersections 
    if len(sols) == 0:
        if not tol_gt(d, r):
            diag_print("wrong number of solutions: 0", "test_cl_int")
            return False
    elif len(sols) == 1:
        if not tol_eq(d, r):
            diag_print("wrong number of solutions: 1", "test_cl_int")
            return False
    elif len(sols) == 2:
        if not tol_lt(d, r):
            diag_print("wrong number of solutions: 2", "test_cl_int")
            return False
    else:
            diag_print("wrong number of solutions: >2", "test_cl_int")

    # test coordinates of intersection 
    for s in sols:
        # s on line (o,v)
        if not is_colinear(s, o, o+v):
            diag_print("solution not on line", "test_cl_int")
            return False
        # s on circle c, r
        if not tol_eq(vector.norm(s-c), r):
            diag_print("solution not on circle", "test_cl_int")
            return False

    return True