def extend_polyline_onto(self, polyline, k): if self.intersectsPolyline(polyline, k): return None if len(polyline) > 1: fault_polyline = self.getPolyline(k) ext1 = self.__ray_intersect(polyline[-2], polyline[-1], fault_polyline) ext2 = self.__ray_intersect(polyline[0], polyline[1], fault_polyline) if ext1 and ext2: d1 = GeometryTools.distance(ext1[0], ext1[1]) d2 = GeometryTools.distance(ext2[0], ext2[1]) if d1 < d2: return ext1 else: return ext2 if ext1: return ext1 else: return ext2 else: raise ValueError("Polyline must have length >= 2")
def test_ray_line_intersection(self): p = GeometryTools.rayLineIntersection((0,0) , (1,0) , (5,-1),(5,1)) self.assertEqual( p , (5,0)) self.assertIsNone( GeometryTools.rayLineIntersection((0,0) , (-1,0) , (5,-1),(5,1)) ) self.assertIsNone( GeometryTools.rayLineIntersection((0,0) , (0,1) , (5,-1),(5,1)) ) self.assertIsNone( GeometryTools.rayLineIntersection((0,0) , (0,-1) , (5,-1),(5,1)) ) p = GeometryTools.rayLineIntersection((0,0) , (1,1) , (5,-6),(5,6)) self.assertEqual( p , (5,5))
def containsPolyline(self, polyline): """ Will return true if at least one point from the polyline is inside the block. """ edge_polyline = self.getEdgePolygon() for p in polyline: if GeometryTools.pointInPolygon(p, edge_polyline): return True else: edge_polyline.assertClosed() return GeometryTools.polylinesIntersect(edge_polyline, polyline)
def __rayIntersect(p0, p1, polyline): ray_dir = GeometryTools.lineToRay(p0, p1) intersections = GeometryTools.rayPolygonIntersections(p1, ray_dir, polyline) if intersections: if len(intersections) > 1: d_list = [ GeometryTools.distance(p1, p[1]) for p in intersections ] index = d_list.index(min(d_list)) else: index = 0 p2 = intersections[index][1] return [p1, p2] else: return None
def test_intersection(self): p1 = (0.0, 0.0) p2 = (10.0, 0.0) p3 = (5.0, -5.0) p4 = (5.0, 5.0) self.assertEqual(GeometryTools.lineIntersection(p1, p2, p3, p4), (5.0, 0.0)) p5 = (0.0, 5.0) self.assertEqual(GeometryTools.lineIntersection(p1, p2, p3, p5), (2.5, 0)) self.assertEqual(GeometryTools.lineIntersection((0.0, 0.0), (1.0, 1.0), (0.0, 1.0), (1.0, 0.0)), (0.5, 0.5))
def test_intersection(self): p1 = Polyline(init_points=[(0, 0), (1, 0)]) p2 = Polyline(init_points=[(0.5, 0.5), (0.5, -0.5)]) p3 = Polyline(init_points=[(0, 1), (1, 1)]) self.assertTrue(GeometryTools.polylinesIntersect(p1, p2)) self.assertFalse(GeometryTools.polylinesIntersect(p2, p3)) self.assertFalse(GeometryTools.polylinesIntersect(p1, p3)) self.assertTrue(p1.intersects(p2)) self.assertTrue(p2.intersects(p1)) self.assertTrue(not p1.intersects(p3)) self.assertTrue(not p3.intersects(p1))
def test_intersection_outside_segments(self): p1 = (0.0, 0.0) p2 = (10.0, 0.0) p3 = (-1.0, -1.0) p4 = (-1.0, 1.0) self.assertIsNone(GeometryTools.lineIntersection(p1, p2, p3, p4))
def test_parallel(self): p1 = (0.0, 0.0) p2 = (10.0, 0.0) p3 = (0.0, 1.0) p4 = (10.0, 1.0) self.assertIsNone(GeometryTools.lineIntersection(p1, p2, p3, p4))
def end_join(self, other, k): fault_polyline = self.getPolyline(k) if isinstance(other, Fault): other_polyline = other.getPolyline(k) else: other_polyline = other return GeometryTools.joinPolylines(fault_polyline, other_polyline)
def countInside(self, polygon): """ Will count the number of points in block which are inside polygon. """ inside = 0 for p in self: if GeometryTools.pointInPolygon((p.x, p.y), polygon): inside += 1 return inside
def test_extend_to_edge(self): bound = Polyline(init_points=[(0, 0), (1, 0), (1, 1), (0, 1)]) l1 = Polyline(init_points=[(-1, 0.5), (0.5, 0.5)]) l2 = Polyline(init_points=[(0.25, 0.25), (0.75, 0.75)]) # Bound is not closed with self.assertRaises(AssertionError): GeometryTools.extendToEdge(bound, l1) bound.assertClosed() # l1 is not fully contained in bound with self.assertRaises(ValueError): GeometryTools.extendToEdge(bound, l1) l3 = GeometryTools.extendToEdge(bound, l2) self.assertEqual(l3[0], (0.00, 0.00)) self.assertEqual(l3[1], (0.25, 0.25)) self.assertEqual(l3[2], (0.75, 0.75)) self.assertEqual(l3[3], (1.00, 1.00)) self.assertEqual(len(l3), 4)
def extendToBBox(self, bbox, k, start=True): fault_polyline = self.getPolyline(k) if start: p0 = fault_polyline[1] p1 = fault_polyline[0] else: p0 = fault_polyline[-2] p1 = fault_polyline[-1] ray_dir = GeometryTools.lineToRay(p0,p1) intersections = GeometryTools.rayPolygonIntersections(p1, ray_dir, bbox) if intersections: p2 = intersections[0][1] if self.getName(): name = "Extend:%s" % self.getName() else: name = None return CPolyline(name=name, init_points=[(p1[0], p1[1]), p2]) else: raise Exception("Logical error - must intersect with bounding box")
def test_distance(self): p1 = (1, 1) p2 = (1, 2, 3) with self.assertRaises(ValueError): GeometryTools.distance(p1, p2) with self.assertRaises(TypeError): GeometryTools.distance(1, p2) p2 = (2, 2) self.assertEqual(GeometryTools.distance(p1, p2), math.sqrt(2)) p1 = (1, 1, 1) p2 = (2, 2, 2) self.assertEqual(GeometryTools.distance(p1, p2), math.sqrt(3))
def test_join__polylines(self): l1 = Polyline(init_points=[(0, 1), (1, 1)]) l2 = CPolyline(init_points=[(2, -1), (2, 0)]) l3 = CPolyline(init_points=[(2, 2), (2, 3)]) l4 = Polyline() l5 = CPolyline(init_points=[(0.5, 0), (0.5, 2)]) with self.assertRaises(ValueError): GeometryTools.joinPolylines(l1, l4) with self.assertRaises(ValueError): GeometryTools.joinPolylines(l4, l1) self.assertIsNone(GeometryTools.joinPolylines(l1, l5)) self.assertEqual(GeometryTools.joinPolylines(l1, l2), [(1, 1), (2, 0)])
def test_join_extend_polylines_onto(self): l1 = Polyline( init_points = [(0,1) , (1,1)]) l2 = CPolyline( init_points = [(2,0) , (2,2)]) l3 = CPolyline( init_points = [(0.5 , 0) , (0.5 , 2)]) l4 = Polyline( init_points = [(0,5) , (1,5)]) l5 = Polyline( init_points = [(0,5)]) self.assertIsNone( GeometryTools.connectPolylines( l1 , l3 )) with self.assertRaises( ValueError ): GeometryTools.connectPolylines( l1 , l5 ) with self.assertRaises( ValueError ): GeometryTools.connectPolylines( l1 , l4 ) self.assertEqual( GeometryTools.connectPolylines( l1 , l2 ) , [(1,1) , (2,1)])
def test_coincident(self): p1 = (0.0, 0.0) p2 = (10.0, 10.0) self.assertIsNone(GeometryTools.lineIntersection(p1, p2, p1, p2))
def test_nearest_point(self): l1 = Polyline(init_points=[(0, 0), (10, 0)]) p = GeometryTools.nearestPointOnPolyline((5, 5), l1) self.assertEqual(p, (5, 0))