def test_only_looks_at_segments_that_intersect_with_candidates_bounding_box(self): distances = [0.0, 0.0, 0.0, 0.0] epsilon = 1.0 line_segs = [LineSegment.from_tuples((0, 1), (1, 0)), LineSegment.from_tuples((0, 2), (1, 1)), LineSegment.from_tuples((0, 4.5), (1, 3.5)), LineSegment.from_tuples((0, 5.5), (1, 4.5))] candidates = [] i = 0 for seg in line_segs: candidates.append(self.MockDistanceCandidate(distances, seg, i)) i += 1 index = RtreeTrajectoryLineSegmentCandidateIndex(candidates=candidates, epsilon=epsilon) expected_neighbors = [[candidates[1]], [candidates[0], candidates[2]], [candidates[1], candidates[3]], [candidates[2]]] i = 0 for cand in candidates: actual_neighbors = index.find_neighbors_of(cand) self.assertEquals(len(expected_neighbors[i]), len(actual_neighbors), "length test failed on index :" + str(i)) for neighbor in actual_neighbors: self.assertIn(neighbor, expected_neighbors[i], "membership test failed on index: " + str(i)) i += 1
def test_four_line_sets_should_result(self): lines = [LineSegment(Point(0, 0), Point(1, 0)), \ LineSegment(Point(0.5, 1), Point(1.5, 1))] traj_lines = [TrajectoryLineSegment(lines[0], 0), \ TrajectoryLineSegment(lines[1], 1)] res = get_representative_trajectory_average_inputs(trajectory_line_segments=traj_lines, \ min_prev_dist=0.5, min_lines=1) expected = 4 self.assertEquals(len(res), expected)
def test_two_short_line_endpoints_get_picked(self): lines = [LineSegment(Point(0, 0), Point(1, 0)), \ LineSegment(Point(0, 1), Point(1, 1))] traj_lines = [TrajectoryLineSegment(lines[0], 0), \ TrajectoryLineSegment(lines[1], 1)] res = get_representative_trajectory_average_inputs(trajectory_line_segments=traj_lines, \ min_prev_dist=1, min_lines=2) expected = 2 self.assertEquals(len(res), expected)
def test_line_seg_distances(self): seg = LineSegment(Point(0, 0), Point(0, 2)) other = LineSegment(Point(0, 0), Point(2, 2)) perp_dist = perpendicular_distance(seg, other) ang_dist = angular_distance(seg, other) self.assertAlmostEquals(math.sqrt(2.0), perp_dist, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) self.assertAlmostEquals(math.sqrt(2.0), ang_dist, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY)
def create_trajectory_line_seg(self, start, end, traj_id, original_position=None): return TrajectoryLineSegment(LineSegment(Point(start[0], start[1]), \ Point(end[0], end[1])), traj_id, original_position)
def test_right_angle_encoding_cost(self): points = [Point(0, 0), Point(0, 2), Point(2, 2)] line_segs = [ LineSegment(Point(0, 0), Point(0, 2)), LineSegment(Point(0, 2), Point(2, 2)) ] #line_segs = self.get_line_segs(points) actual = encoding_cost(trajectory_line_segs=line_segs, low=0, high=len(line_segs), partition_line=LineSegment( points[0], points[2]), angular_dist_func=angular_distance, perpendicular_dist_func=perpendicular_distance) self.assertAlmostEquals(math.log(2 * math.sqrt(2.0), 2) + math.log(2 * math.sqrt(2.0), 2), actual, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY)
def create_line(self, horizontal_start, horizontal_end, line_set_ids, trajectory_id, orig_pos=None): if orig_pos == None: orig_pos = random.randint(0, 1000) return {'trajectory_line_segment': \ TrajectoryLineSegment(LineSegment(Point(horizontal_start, random.randint(0, 1000)), \ Point(horizontal_end, random.randint(0, 1000))), \ trajectory_id, orig_pos), \ 'line_set_ids': line_set_ids}
def funtmp(seg): start = Point(x=seg['start']['x'], y=seg['start']['y'], C=seg['start']['c'], V=seg['start']['v'], TIME=seg['start']['time']) end = Point(x=seg['end']['x'], y=seg['end']['y'], C=seg['end']['c'], V=seg['end']['v'], TIME=seg['end']['time']) lineseg = LineSegment(start, end) return lineseg
def add_line(self, test_object, start_a, end_a, start_b, end_b): line = LineSegment.from_tuples((start_a, end_a), (start_b, end_b)) test_object['lines'].append(line)
def test_creation(self): self.assertRaises((Exception, ArgumentError), TrajectoryLineSegment, \ LineSegment(Point(0, 0), Point(1, 1)), -1) self.assertRaises((Exception, ArgumentError), TrajectoryLineSegment, None, 1) self.assertRaises((Exception, ArgumentError), TrajectoryLineSegment, \ LineSegment(Point(0, 0), Point(1, 1)), None)
def run_test_case(point, start, end, expected): line_seg = LineSegment.from_tuples(start, end) point = Point(point[0], point[1]) self.assertAlmostEquals(expected, point.distance_to_projection_on(line_seg), \ delta=DECIMAL_MAX_DIFF_FOR_EQUALITY)
def create_simple_line_seg(self, start, end): return LineSegment(Point(start[0], start[1]), Point(end[0], end[1]))
def get_line_segs(self, points): return map(lambda i: LineSegment(points[i], points[i + 1]), range(0, len(points) - 1))
def create_line_segment(self, start, end): return LineSegment.from_tuples(start, end)
class GeometryTest(unittest.TestCase): horizontal_line = LineSegment.from_tuples((0, 0), (2, 0)) vertical_line = LineSegment.from_tuples((0, 0), (0, 2)) line_at_45_degrees = LineSegment.from_tuples((0, 0), (1, 1)) line_at_30_degrees = LineSegment.from_tuples((2, 2), (2 + math.sqrt(3.0), 3)) line_at_60_degrees = LineSegment.from_tuples((0, 0), (1, math.sqrt(3.0))) line_at_135_degrees = LineSegment.from_tuples((3, 4), (2, 5)) line_at_225_degrees = LineSegment.from_tuples((3, 4), (1, 2)) line_at_315_degrees = LineSegment.from_tuples((-1, -1), (0, -2)) def test_all_sine_coefficient_computing(self): actual = GeometryTest.horizontal_line.sine_of_angle_with( GeometryTest.line_at_45_degrees) self.assertEquals(1.0 / math.sqrt(2.0), actual) actual = GeometryTest.line_at_30_degrees.sine_of_angle_with( GeometryTest.line_at_60_degrees) self.assertEquals(0.5, actual) actual = GeometryTest.line_at_60_degrees.sine_of_angle_with( GeometryTest.line_at_30_degrees) self.assertEquals(-0.5, actual) actual = GeometryTest.line_at_135_degrees.sine_of_angle_with( GeometryTest.line_at_45_degrees) self.assertAlmostEquals(-1, actual, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) actual = GeometryTest.line_at_225_degrees.sine_of_angle_with( GeometryTest.line_at_45_degrees) self.assertEquals(0, actual) actual = GeometryTest.line_at_225_degrees.sine_of_angle_with( GeometryTest.line_at_135_degrees) self.assertAlmostEquals(-1, actual, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) actual = GeometryTest.line_at_315_degrees.sine_of_angle_with( GeometryTest.line_at_45_degrees) self.assertAlmostEquals(1, actual, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) def test_distance_to_projection_computing(self): def run_test_case(point, start, end, expected): line_seg = LineSegment.from_tuples(start, end) point = Point(point[0], point[1]) self.assertAlmostEquals(expected, point.distance_to_projection_on(line_seg), \ delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) run_test_case((3.5, 4.5), (3, 3), (5, 5), math.sqrt(2.0) / 2.0) run_test_case((4.5, 3.5), (3, 3), (5, 5), math.sqrt(2.0) / 2.0) run_test_case((-4, 5), (10, 10), (11, 10), 5) run_test_case((-4, 5), (10, 10), (10, 11), 14) run_test_case((-5, -5), (-7, -4), (-6, -2), math.sqrt(1.0 + 4.0)) run_test_case((0, 0), (4, 2), (4, 6), 4.0) run_test_case((0, 2), (4, 2), (4, 6), 4.0) def test_rotation(self): def run_test_case(vector, angle, expected): actual = vector.rotated(angle) self.assertAlmostEquals(expected.x, actual.x, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) self.assertAlmostEquals(expected.y, actual.y, delta=DECIMAL_MAX_DIFF_FOR_EQUALITY) run_test_case(Vec2(0, 1), -90, Vec2(1, 0)) run_test_case(Vec2(math.sqrt(2.0), 0), 45, Vec2(1, 1))