def test_polyline_intersection(self): grid = EclGrid.createRectangular((100, 100, 10), (0.25, 0.25, 1)) # Fault1 Fault4 # | | # | | # | | # | ------- Fault2 | # | | # | | # (5 , 2.50) # -------- Fault3 # fault1 = Fault(grid, "Fault1") fault2 = Fault(grid, "Fault2") fault3 = Fault(grid, "Fault3") fault4 = Fault(grid, "Fault4") fault1.addRecord(1, 1, 10, grid.getNY() - 1, 0, 0, "X") fault2.addRecord(5, 10, 15, 15, 0, 0, "Y") fault3.addRecord(5, 10, 5, 5, 0, 0, "Y") fault4.addRecord(20, 20, 10, grid.getNY() - 1, 0, 0, "X") polyline = Polyline(init_points=[(4, 4), (8, 4)]) self.assertTrue(fault4.intersectsPolyline(polyline, 0)) cpolyline = CPolyline(init_points=[(4, 4), (8, 4)]) self.assertTrue(fault4.intersectsPolyline(cpolyline, 0)) polyline = Polyline(init_points=[(8, 4), (16, 4)]) self.assertFalse(fault4.intersectsPolyline(polyline, 0)) cpolyline = CPolyline(init_points=[(8, 4), (16, 4)]) self.assertFalse(fault4.intersectsPolyline(cpolyline, 0))
def test_construction_default(self): with self.assertRaises(TypeError): pl = Polyline(init_points=1) with self.assertRaises(TypeError): pl = Polyline(init_points=[1.23]) pl = Polyline(init_points=[(1, 0), (1, 1), (1, 2)]) self.assertEqual(len(pl), 3)
def test_save(self): with TestAreaContext("polyline/fwrite") as work_area: p1 = Polyline(init_points=[(1, 0), (1, 1), (1, 2)]) p2 = Polyline(init_points=[(1, 0), (1, 1), (1, 2)]) self.assertTrue(p1 == p2) XYZIo.saveXYFile(p1, "poly.xy") p2 = XYZIo.readXYFile("poly.xy") self.assertTrue(p1 == p2)
def test_closed(self): pl = Polyline(init_points=[(1, 0), (1, 1), (0, 2)]) self.assertFalse(pl.isClosed()) pl.addPoint(1, 0) self.assertEqual(4, len(pl)) self.assertTrue(pl.isClosed()) pl = Polyline(init_points=[(1, 0), (1, 1), (0, 2)]) self.assertFalse(pl.isClosed()) pl.assertClosed() self.assertEqual(4, len(pl)) self.assertTrue(pl.isClosed())
def test_add(self): l1 = Polyline(init_points=[(-1, 0.5), (0.5, 0.5)]) l2 = Polyline(init_points=[(-1, 0.5), (0.5, 0.5)]) l3 = l1 + l2 self.assertEqual(len(l3), 4) self.assertEqual(l1[0], l3[0]) self.assertEqual(l1[1], l3[1]) self.assertEqual(l1[0], l3[2]) self.assertEqual(l1[1], l3[3]) l4 = l1 l4 += l2 self.assertEqual(l3, l4)
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_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_construction(self): polyline = Polyline(name="test line") with self.assertRaises(IndexError): polyline.isClosed() self.assertEqual(polyline.name(), "test line") self.assertEqual(len(polyline), 0) polyline.addPoint(0, 0, 0) self.assertEqual(len(polyline), 1) polyline.addPoint(1, 1, 0) self.assertEqual(len(polyline), 2) polyline.addPoint(1, 1.5) self.assertEqual(len(polyline), 3) self.assertEqual(polyline[0], (0, 0, 0)) self.assertEqual(polyline[1], (1, 1, 0)) self.assertEqual(polyline[2], (1, 1.5)) polyline.addPoint(0, 1, 0) self.assertFalse(polyline.isClosed()) polyline.addPoint(0, 0, 0) self.assertTrue(polyline.isClosed())
def test_add_polyline_barrier2(self): grid = EclGrid.createRectangular((10, 10, 1), (1, 1, 1)) layer = FaultBlockLayer(self.grid, 0) polyline = Polyline(init_points=[(0.1, 0.9), (8.9, 0.9), (8.9, 8.9)]) points = [ ((0, 0), (0, 1)), ((2, 0), (2, 1)), ((4, 0), (4, 1)), ((6, 0), (6, 1)), ((8, 0), (8, 1)), # ((8, 1), (9, 1)), ((8, 3), (9, 3)), ((8, 5), (9, 5)), ((8, 7), (9, 7)) ] geo_layer = layer.getGeoLayer() for p1, p2 in points: self.assertTrue(geo_layer.cellContact(p1, p2)) layer.addPolylineBarrier(polyline) for p1, p2 in points: print(p1, p2) self.assertFalse(geo_layer.cellContact(p1, p2))
def getEdgePolygon(self): x_list = DoubleVector() y_list = DoubleVector() self.cNamespace().trace_edge( self , x_list , y_list ) p = Polyline() for (x,y) in zip(x_list , y_list): p.addPoint(x,y) return p
def test_unzip(self): p2 = Polyline(init_points=[(1, 0), (1, 1), (1, 2)]) p3 = Polyline(init_points=[(1, 0, 1), (1, 1, 2), (1, 2, 3)]) (x, y) = p2.unzip() self.assertEqual(x, [1, 1, 1]) self.assertEqual(y, [0, 1, 2]) (x, y, z) = p3.unzip() self.assertEqual(x, [1, 1, 1]) self.assertEqual(y, [0, 1, 2]) self.assertEqual(z, [1, 2, 3]) with self.assertRaises(ValueError): (x, y, z) = p2.unzip() with self.assertRaises(ValueError): (x, y) = p3.unzip()
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_intersects(self): polyline1 = CPolyline(init_points=[(0, 0), (1, 0), (1, 1)]) polyline2 = CPolyline(init_points=[(0.50, 0.50), (1.50, 0.50)]) polyline3 = Polyline(init_points=[(0.50, 0.50), (1.50, 0.50)]) polyline4 = CPolyline(init_points=[(0.50, 1.50), (1.50, 1.50)]) self.assertTrue(polyline1.intersects(polyline2)) self.assertTrue(polyline1.intersects(polyline3)) self.assertFalse(polyline1.intersects(polyline4))
def test_iteration(self): values = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1)] polyline = Polyline(name="iteration line") for p in values: polyline.addPoint(*p) for index, point in enumerate(polyline): self.assertEqual(point, values[index])
def getEdgePolygon(self): x_list = DoubleVector() y_list = DoubleVector() cell_list = IntVector() self._trace_edge(x_list, y_list, cell_list) p = Polyline() for (x, y) in zip(x_list, y_list): p.addPoint(x, y) return p
def __initPolyline(self): pl = Polyline() for segment in self: (x, y, z) = self.__grid.getLayerXYZ(segment.getC1(), self.__k) pl.addPoint(x, y, z) segment = self[-1] (x, y, z) = self.__grid.getLayerXYZ(segment.getC2(), self.__k) pl.addPoint(x, y, z) self.__polyline = pl
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 setUp(self): self.grid = EclGrid.create_rectangular((16, 16, 1), (1, 1, 1)) self.poly1 = Polyline(init_points=[(0, 0), (0, 4), (5, 5), (0, 5)]) self.poly2 = Polyline(init_points=[(11, 11), (16, 12), (16, 16), (12, 16)]) self.fault_block_kw = EclKW.create("FAULTBLK", self.grid.getGlobalSize(), EclTypeEnum.ECL_INT_TYPE) self.fault_block_kw.assign(0) self.faults = FaultCollection(self.grid) for j in range(4): for i in range(4): g1 = i + j * self.grid.getNX() g2 = i + 12 + (j + 12) * self.grid.getNX() self.fault_block_kw[g1] = 1 self.fault_block_kw[g2] = 2 self.fault_blocks = [] for k in range(self.grid.getNZ()): block = FaultBlockLayer(self.grid, k) block.scanKeyword(self.fault_block_kw) self.fault_blocks.append(block)
def test_add_polyline_barrier1(self): grid = EclGrid.create_rectangular( (4,1,1) , (1,1,1) ) layer = FaultBlockLayer( self.grid , 0 ) polyline = Polyline( init_points = [ (1.99 , 0.001) , (2.01 , 0.99)]) points = [((1,0) , (2,0))] geo_layer = layer.getGeoLayer() for p1,p2 in points: self.assertTrue(geo_layer.cellContact( p1 , p2 )) layer.addPolylineBarrier( polyline ) for p1,p2 in points: print(p1,p2) self.assertFalse(geo_layer.cellContact( p1 , p2 ))
def splitFaultBlocks(self , grid , fault_blocks ): boundingPolygon = Polyline(init_points = grid.getBoundingBox2D()) boundingPolygon.assertClosed() if self.hasPolygon(): if len(self.edges) == 1: return self.splitFaultBlockClosedPolygon( grid , fault_blocks , self.edges[0] ) else: current_fault_block_layer = fault_blocks k = fault_blocks.getK() for edge in self.edges: if isinstance(edge , Polyline): # Start on a brand new fault block layer. next_fault_block_layer = FaultBlockLayer( grid , k ) for block in current_fault_block_layer: if block.containsPolyline(edge): print "Block %d is split due to edge:%s" % (block.getBlockID() , edge.name()) sliced = GeometryTools.slicePolygon( boundingPolygon , edge ) inside_list = [] outside_list = [] for p in block: if GeometryTools.pointInPolygon( (p.x , p.y) , sliced ): inside_list.append( p ) else: outside_list.append( p ) if len(inside_list) * len(outside_list) == 0: new_block = next_fault_block_layer.addBlock( ) for p in inside_list: new_block.addCell(p.i , p.j) for p in outside_list: new_block.addCell(p.i , p.j) else: layer = Layer( grid.getNX() , grid.getNY() ) for p in inside_list: layer[p.i , p.j] = 1 for p in outside_list: layer[p.i , p.j] = 2 next_fault_block_layer.scanLayer( layer ) else: next_fault_block_layer.insertBlockContent( block ) current_fault_block_layer = next_fault_block_layer return current_fault_block_layer else: return fault_blocks
def test_nearest_point(self): l1 = Polyline(init_points=[(0, 0), (10, 0)]) p = GeometryTools.nearestPointOnPolyline((5, 5), l1) self.assertEqual(p, (5, 0))