def __init__(self, sidelength, point): half_sl = sidelength / 2 self.tl = Point2D(point.x - half_sl, point.y - half_sl) self.tr = Point2D(point.x + half_sl, point.y - half_sl) self.bl = Point2D(point.x - half_sl, point.y + half_sl) self.br = Point2D(point.x + half_sl, point.y + half_sl) self.sidelength = sidelength
def test_edge_property(self): """ Small experimental test to verify whether the Frechet Distance between any two edges is defined my the maximum distance between both pairs of endpoints. """ spacing = 0.5 fixed = Edge2D(Point2D(0.0, 0.0), Point2D(0.0, 1.0)).get_steiner_edge(spacing) u, v = fixed.get_spine() def almost_equal(estimate, real): return real + 1 >= estimate >= real - 1 for i in range(0, 10000): p1 = Point2D(float(randint(-20, 20)), float(randint(-20, 20))) p2 = Point2D(float(randint(-20, 20)), float(randint(-20, 20))) # Ensure points aren't the same if p1 == p2: p2 = Point2D(p2.x + 0.1, p2.y) rand_edge = Edge2D(p1, p2).get_steiner_edge(spacing) x, y = rand_edge.get_spine() r = min( max(np.linalg.norm(x.v - u.v), np.linalg.norm(y.v - v.v)), max(np.linalg.norm(y.v - u.v), np.linalg.norm(x.v - v.v)) ) frechet_estimate = discrete_frechet(fixed, rand_edge) assert almost_equal(r, frechet_estimate)
def test_approximation(self): u = Point2D(0.0, 0.0) grid = ExponentialGrid2D(u, self.error, 1.0, 20.0) p = Point2D(0.0, 15.0) p_prime = grid.approximate_point(p) # Test for error property assert np.linalg.norm(p.v - p_prime.v) <= \ (self.error / 2) * np.linalg.norm(p.v - u.v)
def test_bottleneck_weight_easy_dag(self): dag = DirectedAcyclicGraph() p1 = Point2D(0, 0) p2 = Point2D(1, 0) p3 = Point2D(2, 0) dag.add_edge(p1, p2, 1) dag.add_edge(p2, p3, 2) assert dag.bottleneck_path_weight(p1, p3) == 2
def __init__(self, sidelength, tl_point): self.tl = tl_point self.tr = Point2D(tl_point.x + sidelength, tl_point.y) self.bl = Point2D(tl_point.x, tl_point.y + sidelength) self.br = Point2D(tl_point.x + sidelength, tl_point.y + sidelength) self.points = [ self.tl, self.tr, self.bl, self.br ]
def test_edge(self): curve = Edge2D(Point2D(-5.0, 1.0), Point2D(-4.0, 4.0)) e = Edge2D(Point2D(-3.0, 1.0), Point2D(-3.0, 3.0)) grid = FrechetGrid2D(curve, self.error) real = discrete_frechet(e.get_steiner_curve(STEINER_SPACING), curve.get_steiner_curve(STEINER_SPACING)) estimate = grid.approximate_frechet(e) # Test for (1 + epsilon) property of grid estimate assert estimate <= real or \ real <= (1 + self.error) * estimate
def test_symmetric_curves(self): c1 = PolygonalCurve2D([ Point2D(0.0, 1.0), Point2D(3.0, 2.0), Point2D(5.0, 2.0), Point2D(7.0, 1.0) ]) c2 = PolygonalCurve2D([ Point2D(0.0, 0.0), Point2D(3.0, 1.0), Point2D(5.0, 1.0), Point2D(7.0, 0.0) ]) self.perform_test(c1, c2, 1.0)
def create_children(children, parent): prev = None first = None for child in children: new = Tree.Node(Point2D(child['x'], child['y']), parent=parent) new.left_child = create_children(child['children'], new) if prev: prev.right_sibling = new if not first: first = new prev = new return first
def sub_divide(self, d_t): assert d_t > 0, "Distance for line partition must be greater than 0." pi = list() pi.append(self.p1) curr_t = t = d_t / self.d if self.d != 0 else 1 while curr_t < 1: pi.append(Point2D( (1 - curr_t) * self.p1.x + (curr_t * self.p2.x), (1 - curr_t) * self.p1.y + (curr_t * self.p2.y) )) curr_t += t pi.append(self.p2) return pi
def test_asymmetric_curves(self): c1 = PolygonalCurve2D([ Point2D(-5.0, 1.0), Point2D(-4.0, 4.0), Point2D(-2.0, -1.0) ]) c2 = PolygonalCurve2D([ Point2D(-6.0, 0.0), Point2D(-3.0, -2.0), Point2D(-2.0, 1.0) ]) self.perform_test(c1, c2, 6.08)
def create_tree(json): root = Tree.Node(Point2D(json['root']['x'], json['root']['x'])) def create_children(children, parent): prev = None first = None for child in children: new = Tree.Node(Point2D(child['x'], child['y']), parent=parent) new.left_child = create_children(child['children'], new) if prev: prev.right_sibling = new if not first: first = new prev = new return first root.left_child = create_children(json['root']['children'], root) return Tree(root=root)
def test_bottleneck_weight_hard_dag(self): dag = DirectedAcyclicGraph() p1 = Point2D(0, 0) p2 = Point2D(1, 0) p3 = Point2D(2, 0) p4 = Point2D(3, 0) p5 = Point2D(1, -1) p6 = Point2D(2, -1) dag.add_edge(p1, p2, 1) dag.add_edge(p2, p3, 2) dag.add_edge(p3, p4, 1) dag.add_edge(p2, p6, 3) dag.add_edge(p1, p5, 2) dag.add_edge(p5, p6, 5) dag.add_edge(p6, p4, 6) assert dag.bottleneck_path_weight(p1, p4) == 2
def test_small_float_values(self): tree = CurveRangeTree2D( PolygonalCurve2D([ Point2D(0.0, 0.0), Point2D(1.0, 0.0), Point2D(0.0, -0.01), Point2D(1.0, -0.01), Point2D(0.0, -0.02), Point2D(1.0, -0.02) ]), self.error, 0.55) # Create query parameters q_edge = Edge2D(Point2D(0.0, 0.0), Point2D(1.0, -0.02)) x = Point2D(0.0, 0.0) x_edge = Edge2D(Point2D(0.0, 0.0), Point2D(1.0, 0.0)) y = Point2D(1.0, -0.02) y_edge = Edge2D(Point2D(0.0, -0.02), Point2D(1.0, -0.02)) assert tree.is_approximate(q_edge, x, y, x_edge, y_edge) q_edge = Edge2D(Point2D(2.2, 0.0), Point2D(2.2, -0.02)) assert not tree.is_approximate(q_edge, x, y, x_edge, y_edge)
def test_query_square_curve(self): tree = CurveRangeTree2D( PolygonalCurve2D([ Point2D(0.0, 0.0), Point2D(5.0, 0.0), Point2D(5.0, 5.0), Point2D(1.0, 5.0), Point2D(1.0, 1.0), Point2D(4.0, 1.0), Point2D(4.0, 4.0), Point2D(2.0, 4.0), Point2D(2.0, 2.0), Point2D(3.0, 2.0), Point2D(3.0, 3.0) ]), self.error, self.delta) # Create query parameters x = Point2D(2.5, 0.0) x_edge = Edge2D(Point2D(0.0, 0.0), Point2D(5.0, 0.0)) y = Point2D(3.0, 2.5) y_edge = Edge2D(Point2D(3.0, 2.0), Point2D(3.0, 3.0)) # Query various edges against this tree q_edge = Edge2D(Point2D(2.5, -2.0), Point2D(5.5, -0.5)) assert tree.is_approximate(q_edge, x, y, x_edge, y_edge) q_edge = Edge2D(Point2D(-1.1, 5.0), Point2D(-1.1, 1)) assert not tree.is_approximate(q_edge, x, y, x_edge, y_edge) q_edge = Edge2D(Point2D(1.0, 2.5), Point2D(5.0, 2.5)) assert not tree.is_approximate(q_edge, x, y, x_edge, y_edge) q_edge = Edge2D(Point2D(0.0, 0.0), Point2D(5.0, 5.0)) assert not tree.is_approximate(q_edge, x, y, x_edge, y_edge)
def test_query_trivial_curve(self): tree = CurveRangeTree2D( PolygonalCurve2D( [Point2D(0.0, 0.0), Point2D(3.0, 0.0), Point2D(3.0, 3.0)]), self.error, self.delta) # Create query parameters q_edge = Edge2D(Point2D(0.0, -1.0), Point2D(3.0, -1.0)) x = Point2D(0.25, 0.0) x_edge = Edge2D(Point2D(0.0, 0.0), Point2D(3.0, 0.0)) y = Point2D(3.0, 2.5) y_edge = Edge2D(Point2D(3.0, 0.0), Point2D(3.0, 3.0)) assert tree.is_approximate(q_edge, x, y, x_edge, y_edge)