def is_seg1_overlapping_with_seg2(seg1, seg2): '''Checks if a segment is in proximity of another one upstream''' # get the coordinates of seg2 (from the origin) s1 = coords(seg2[0]) s2 = coords(seg2[1]) # vector of the center of seg2 (from the origin) C = 0.5 * (s1 + s2) # endpoint of seg1 (from the origin) P = coords(seg1[1]) # vector from the center C of seg2 to the endpoint P of seg1 CP = P - C # vector of seg2 S1S2 = s2 - s1 # projection of CP upon seg2 prj = mm.vector_projection(CP, S1S2) # check if the distance of the orthogonal complement of CP projection on S1S2 # (vertical distance from P to seg2) is smaller than the sum of the radii. (overlap) # If not exit early, because there is no way that backtracking can feasible if not is_seg2_within_seg1_radius(np.linalg.norm(CP - prj), seg1, seg2): return False # projection lies within the length of the cylinder. Check if the distance between # the center C of seg2 and the projection of the end point of seg1, P is smaller than # half of the others length plus a 5% tolerance return np.linalg.norm(prj) < 0.55 * np.linalg.norm(S1S2)
def test_vector_projection_collinear(): v1 = np.array([1., 2., 3.]) v2 = np.array([4., 8., 12.]) res = mm.vector_projection(v1, v2) assert np.allclose(res, v1)
def test_vector_projection_perpendicular(): v1 = np.array([2., 0., 0.]) v2 = np.array([0., 3., 0.]) res = mm.vector_projection(v1, v2) assert np.allclose(res, (0., 0., 0.))
def test_vector_projection_perpendicular(): v1 = np.array([2., 0., 0.]) v2 = np.array([0., 3., 0.]) res = mm.vector_projection(v1, v2) nt.assert_true(np.allclose(res, (0.,0.,0.)))
def test_vector_projection_collinear(): v1 = np.array([1., 2., 3.]) v2 = np.array([4., 8., 12.]) res = mm.vector_projection(v1, v2) nt.assert_true(np.allclose(res, v1))
def test_vector_projection(): v1 = np.array([4., 1., 0.]) v2 = np.array([2., 3., 0.]) res = mm.vector_projection(v1, v2) nt.assert_true( np.allclose(res, (1.6923076923076923, 2.5384615384615383, 0.)))
def test_vector_projection(): v1 = np.array([4., 1., 0.]) v2 = np.array([2., 3., 0.]) res = mm.vector_projection(v1, v2) nt.assert_true(np.allclose(res, (1.6923076923076923, 2.5384615384615383, 0.)))