def test_ray_polyline_intersections(self): # /.\ # . # . # (4)---------+----(3) # /________:_____:_________ # \ . | # (1)--+----(2) # | . # | . # (0) . polygon = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 2.0), (-1.0, 2.0)] p0 = (0.5, 0.0) ray0 = (0.0, 1.0) intersections0 = GeometryTools.rayPolygonIntersections(p0, ray0, polygon) self.assertEqual(intersections0, [(1, (0.5, 1.0)), (3, (0.5, 2.0))]) p1 = (1.5, 1.5) ray1 = (-1.0, 0.0) intersections1 = GeometryTools.rayPolygonIntersections(p1, ray1, polygon) self.assertEqual(intersections1, [(2, (1, 1.5))])
def test_point_in_polygon_with_3_element_points(self): p1 = (0.5, 0.51, 0.2) p2 = (0.5, 0.49, 0.1) poly = [(0,0,9), (0,1,9), (0.6,0.5), (0.4,0.5,9), (1,1), (1,0,9)] self.assertFalse(GeometryTools.pointInPolygon(p1, poly)) self.assertTrue(GeometryTools.pointInPolygon(p2, poly))
def test_point_in_strange_polygon(self): p1 = (0.5, 0.51) p2 = (0.5, 0.49) poly = [(0,0), (0, 1), (0.6, 0.5), (0.4, 0.5), (1, 1), (1, 0)] self.assertFalse(GeometryTools.pointInPolygon(p1, poly)) self.assertTrue(GeometryTools.pointInPolygon(p2, poly))
def test_convex_hull(self): points = [(0, 0), (0, 1), (1, 1), (1, 0), (1, 1), (0.5, 0.5), (0.25, 0.25), (0.5, 1.25), (0.5, 0.75)] result = GeometryTools.convexHull(points) self.assertEqual(result, [(0, 0), (0, 1), (0.5, 1.25), (1, 1), (1, 0)]) points = [(0, -0.5), (0, 0.5), (-0.5, 0), (0.5, 0), (0, 0), (0.5, 0.5)] result = GeometryTools.convexHull(points) self.assertEqual(result, [(-0.5, 0), (0, 0.5), (0.5, 0.5), (0.5, 0), (0, -0.5)])
def test_ccw(self): p1 = (0, 0) p2 = (1, 0) p3 = (0, 1) p4 = (0, 2) self.assertTrue(GeometryTools.ccw(p1, p2, p3) > 0) # Counter-clockwise self.assertTrue(GeometryTools.ccw(p1, p3, p2) < 0) # Clockwise self.assertTrue(GeometryTools.ccw(p1, p3, p4) == 0) # Colinear
def test_line_to_ray(self): p0 = (0.0, 0.0) p1 = (1.0, 1.0) p2 = (1.0, 0.0) ray = GeometryTools.lineToRay(p0, p1) self.assertEqual(ray, (1.0 / sqrt(2.0), 1.0 / sqrt(2.0))) ray = GeometryTools.lineToRay(p1, p0) self.assertEqual(ray, (-1.0 / sqrt(2.0), -1.0 / sqrt(2.0))) ray = GeometryTools.lineToRay(p0, p2) self.assertEqual(ray, (1.0, 0.0))
def test_point_in_polygon(self): p1 = (0.5, 0.5) p2 = (2, 2) p3 = (1, 0.5) # on the edge poly1 = [(0, 0), (1, 0), (1, 1), (0, 1)] # Not explicitly closed poly2 = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)] # explicitly closed self.assertTrue(GeometryTools.pointInPolygon(p1, poly1)) self.assertTrue(GeometryTools.pointInPolygon(p1, poly2)) self.assertFalse(GeometryTools.pointInPolygon(p2, poly1)) self.assertFalse(GeometryTools.pointInPolygon(p2, poly2)) self.assertTrue(GeometryTools.pointInPolygon(p3, poly1))
def test_point_in_polyline(self): p1 = (0.5, 0.5) p2 = (2, 2) poly = Polyline() poly.addPoint(0, 0) poly.addPoint(1, 0) poly.addPoint(1, 1) poly.addPoint(0, 1) poly.addPoint(0, 0) self.assertTrue(GeometryTools.pointInPolygon(p1, poly)) self.assertTrue(GeometryTools.pointInPolygon(p1, poly)) self.assertFalse(GeometryTools.pointInPolygon(p2, poly)) self.assertFalse(GeometryTools.pointInPolygon(p2, poly))
def test_slicing_internal_hull(self): polygon = [(2,2),(2,1),(1,1),(1,5),(5,5),(5,4),(4,4)] edge = [(0,0) , (10,0) , (10,10), (0,10) , (0,0)] sliced = GeometryTools.slicePolygon(edge , polygon) expected = [(2,2),(2,1),(1,1),(1,5),(5,5),(5,4),(4,4),(2.0,4.0),(2,2)] self.assertEqual(sliced, expected)
def test_ray_polyline_intersections(self): p0 = (0.5, 0.0) ray = (0.0, 1.0) polygon = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 2.0), (-1.0, 2.0)] intersections = GeometryTools.rayPolygonIntersections(p0, ray, polygon) self.assertEqual(intersections, [(1, (0.5, 1.0)), (3, (0.5, 2.0))])
def test_slicing_bendy_line_segments(self): p0 = (0.0, 0.0) p1 = (0.0, 1.0) p2 = (1.0, 1.0) p3 = (1.0, 0.0) polygon = [p0, p1, p2, p3, p0] lp0 = (0.2, 0.5) lp1 = (0.4, 0.5) lp2 = (0.4, 0.3) line = [lp0, lp1, lp2] expected = [(0.0, 0.5), p1, p2, p3, (0.4, 0.0), lp2, lp1, lp0, (0.0, 0.5)] result = GeometryTools.slicePolygon(polygon, line) self.assertEqual(result, expected) line = [lp2, lp1, lp0] result = GeometryTools.slicePolygon(polygon, line) self.assertEqual(result, expected)
def test_slicing_same_segment(self): p0 = (0.0, 0.0) p1 = (0.0, 1.0) p2 = (1.0, 1.0) p3 = (1.0, 0.0) polygon = [p0, p1, p2, p3, p0] lp0 = (0.2, 0.5) lp1 = (0.4, 0.5) lp2 = (0.4, 0.3) lp3 = (0.2, 0.3) line = [lp0, lp1, lp2, lp3] result = GeometryTools.slicePolygon(polygon, line) expected = [(0.0, 0.5), (0.0, 0.3), lp3, lp2, lp1, lp0, (0.0, 0.5)] self.assertEqual(result, expected) line = [lp3, lp2, lp1, lp0] result = GeometryTools.slicePolygon(polygon, line) expected = [(0.0, 0.3), (0.0, 0.5), lp0, lp1, lp2, lp3, (0.0, 0.3)] self.assertEqual(result, expected)
def test_ray_line_intersection(self): p0 = (0.0, 0.0) p1 = (0.0, 1.0) p2 = (1.0, 1.0) p3 = (1.0, 0.0) p5 = (2.0, 1.0) ray1 = GeometryTools.lineToRay(p0, p2) ray2 = GeometryTools.lineToRay(p2, p0) ray3 = GeometryTools.lineToRay(p0, p5) self.assertEqual((0.5, 0.5), GeometryTools.rayLineIntersection(p0, ray1, p1, p3)) self.assertIsNone(GeometryTools.rayLineIntersection(p0, ray2, p1, p3)) #ray2 is ray1 reversed (no backwards intersections) self.assertEqual((1.0, 0.5), GeometryTools.rayLineIntersection(p0, ray3, p2, p3)) self.assertIsNone(GeometryTools.rayLineIntersection(p0, ray3, p1, p2))
def test_ray_line_intersection(self): p0 = (0.0, 0.0) p1 = (0.0, 1.0) p2 = (1.0, 1.0) p3 = (1.0, 0.0) p5 = (2.0, 1.0) ray1 = GeometryTools.lineToRay(p0, p2) ray2 = GeometryTools.lineToRay(p2, p0) ray3 = GeometryTools.lineToRay(p0, p5) self.assertEqual((0.5, 0.5), GeometryTools.rayLineIntersection(p0, ray1, p1, p3)) self.assertIsNone(GeometryTools.rayLineIntersection( p0, ray2, p1, p3)) #ray2 is ray1 reversed (no backwards intersections) self.assertEqual((1.0, 0.5), GeometryTools.rayLineIntersection(p0, ray3, p2, p3)) self.assertIsNone(GeometryTools.rayLineIntersection(p0, ray3, p1, p2))