def test_constructor(self): v1 = Vertex((0,0)) v2 = Vertex((1,1)) v3 = Vertex((2,2)) half_edge_1 = HalfEdge(v1, v2) half_edge_2 = HalfEdge(v2, v3) # Should complain about tuple input self.assertRaises(Exception, HalfEdge, *[v1,(1,1)]) # Should complain about incompatible twin self.assertRaises(Exception, HalfEdge, \ *(v2, v3), **{'twin':half_edge_1}) # Should complain about incompatible previous self.assertRaises(Exception, HalfEdge, \ *(v3, v2), **{'previous':half_edge_1}) # Should complain about incompatible nxt self.assertRaises(Exception, HalfEdge, *(v3, v2), \ **{'next':half_edge_1}) # Should complain about nxt of incompatible type self.assertRaises(Exception, HalfEdge, *(v3, v2), \ **{'next':2}) # Should complain about incompatible parent self.assertRaises(Exception, HalfEdge, *(v3, v2), \ **{'parent':half_edge_2})
def test_get_neighbor(self): """ TODO: Test flagged neighbor """ # # Make interval # v1 = Vertex(0) v2 = Vertex(1) I = Interval(v1, v2) # # Left interval # v0 = Vertex(-1) I0 = Interval(v0, v1) # # Right interval # v3 = Vertex(2) I1 = Interval(v2, v3) I0.assign_next(I) self.assertEqual(I0.next(), I) I.assign_next(I1) self.assertEqual(I.get_neighbor(1), I1) self.assertEqual(I.get_neighbor(0), I0)
def test_assign_next(self): # # Make interval # v1 = Vertex(0) v2 = Vertex(1) I = Interval(v1, v2) # # Left interval # v0 = Vertex(-1) I0 = Interval(v0, v1) # # Right interval # v3 = Vertex(2) I1 = Interval(v2, v3) I0.assign_next(I) self.assertEqual(I0.next(), I) self.assertEqual(I.previous(), I0) I.assign_next(I1) self.assertEqual(I.next(), I1) self.assertEqual(I1.previous(), I)
def test_intersects_line_segment(self): vertices = [Vertex((0,0)), Vertex((3,1)), Vertex((2,3)), Vertex((-1,1))] h_edges = [] for i in range(4): h_edges.append(HalfEdge(vertices[i], vertices[(i+1)%4])) cell = Cell(h_edges) # # Line beginning in cell and ending outside # line_1 = [(1,1),(3,0)] self.assertTrue(cell.intersects_line_segment(line_1),\ 'Cell should intersect line segment.') # # Line inside cell # line_2 = [(1,1),(1.1,1.1)] self.assertTrue(cell.intersects_line_segment(line_2),\ 'Cell contains line segment.') # # Line outside cell # line_3 = [(3,0),(5,6)] self.assertFalse(cell.intersects_line_segment(line_3),\ 'Cell does not intersect line segment.')
def test_intersects_line_segment(self): # # Define a HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((0,1)) h_edge = HalfEdge(v1,v2) # # Line 1 intersects h_edge in the middle # line_1 = [(-0.5,0.5),(0.5,0.5)] self.assertTrue(h_edge.intersects_line_segment(line_1),\ 'HalfEdge should intersect line_1.') # # Line 2 intersects h_edge at the vertex # line_2 = [(-1,1),(0,1)] self.assertTrue(h_edge.intersects_line_segment(line_2),\ 'HalfEdge should intersect line_2.') # # Line 3 is on top of h_edge # line_3 = [(0,0),(0,1)] self.assertTrue(h_edge.intersects_line_segment(line_3),\ 'HalfEdge should intersect line_3.') # # Line 4 does not intersect h_edge # line_4 = [(1,2), (3,3)] self.assertFalse(h_edge.intersects_line_segment(line_4),\ 'HalfEdge should intersect line_4.')
def test_bin_points(self): # # Cell vertices # v1 = Vertex((0, 0)) v2 = Vertex((1, 0)) v3 = Vertex((1, 1)) v4 = Vertex((0, 1)) # Cell HalfEdges h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h34 = HalfEdge(v3, v4) h41 = HalfEdge(v4, v1) # Cell cell = QuadCell([h12, h23, h34, h41]) # Split cell twice cell.split() cell.get_child(1).split() # # Error: point not in cell # x = np.array([[-1, -1]]) self.assertRaises(Exception, cell.bin_points, *(x, )) # # Corner points # x = convert_to_array([v1, v2, v3, v4]) bins = cell.bin_points(x) # There should be four bins self.assertEqual(len(bins), 4) # No binning cells should have children for c, dummy in bins: self.assertFalse(c.has_children()) # # Center point # x = np.array([[0.5, 0.5]]) bins = cell.bin_points(x) self.assertEqual(len(bins), 1) # # Mark # sf = '1' for child in cell.get_children(): child.mark(sf) x = np.array([[0.75, 0.25]]) bins = cell.bin_points(x, subforest_flag=sf) for c, dummy in bins: self.assertTrue(c.is_marked(sf)) self.assertFalse(c.has_children(flag=sf))
def test_head(self): # # Retrieve head # v1 = Vertex((0,0)) v2 = Vertex((1,1)) half_edge = HalfEdge(v1,v2) self.assertEqual(half_edge.head(), v2, 'Incorrect head.')
def test_base(self): # # Retrieve base # v1 = Vertex((0,0)) v2 = Vertex((1,1)) half_edge = HalfEdge(v1,v2) self.assertEqual(half_edge.base(), v1, 'Incorrect base.')
def test_mark(self): v = Vertex(0) v.mark() self.assertTrue(v.is_marked()) v = Vertex(0) v.mark('sflkj') self.assertTrue(v.is_marked())
def test_twin(self): v1 = Vertex((0,0)) v2 = Vertex((1,1)) half_edge_1 = HalfEdge(v1,v2) half_edge_2 = HalfEdge(v2,v1, twin=half_edge_1) self.assertEqual(half_edge_2.twin(), half_edge_1,\ 'Twin incorrectly specified.') self.assertIsNone(half_edge_1.twin(), \ 'half_edge_2 has no twin yet.')
def test_get_vertices(self): # # Make interval # v1 = Vertex(0) v2 = Vertex(1) I = Interval(v1, v2) self.assertEqual(I.get_vertices(), [v1, v2])
def test_subcell_position(self): """ Locate sub-cell within a skew quadcell """ # # Define ROOT Cell # # Define vertices v1 = Vertex((1, 1)) v2 = Vertex((2, 3)) v3 = Vertex((1.5, 5)) v4 = Vertex((0, 2)) vertices = [v1, v2, v3, v4] # Define HalfEdges h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h34 = HalfEdge(v3, v4) h41 = HalfEdge(v4, v1) halfedges = [h12, h23, h34, h41] # Define QuadCell cell = QuadCell(halfedges) # Check that cell is not a rectangle self.assertFalse(cell.is_rectangle()) # # Refine ROOT cell twice and pick a grandchild # cell.split() child = cell.get_child(2) child.split() grandchild = child.get_child(1) # # Determine relative position of the grandchild within cell # pos, width = cell.subcell_position(grandchild) # # Map the associated region the reference cell to physical cell # x_ref = np.array([ pos, pos + np.array([width, 0]), pos + np.array([width, width]), pos + np.array([0, width]) ]) x_vertices = cell.reference_map(x_ref) # # Check that points match up # for v, vh in zip(grandchild.get_vertices(), x_vertices): self.assertTrue(np.allclose(np.array(v.coordinates()), vh))
def test_contains_points(self): # # Triangle # v1 = Vertex((0,0)) v2 = Vertex((1,0)) v3 = Vertex((0,1)) h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h31 = HalfEdge(v3, v1) cell = Cell([h12, h23, h31]) # Vertices in_cell = cell.contains_points([v1,v2,v3]) in_cell_ref = np.ones(3, dtype=np.bool) for i in range(3): self.assertEqual(in_cell_ref[i], in_cell[i]) # Random points points = np.random.rand(100,2) in_cell_ref = (points[:,1]<1-points[:,0]) in_cell = cell.contains_points(points) for i in range(100): self.assertEqual(in_cell_ref[i], in_cell[i]) # # Square # v4 = Vertex((1,1)) h24 = HalfEdge(v2,v4) h43 = HalfEdge(v4,v3) square_half_edges = [h12, h24, h43, h31] cell = Cell(square_half_edges) points = [(2,0), (-1,0), (0.5,0.5)] in_cell_ref = np.array([0,0,1], dtype=np.bool) in_cell = cell.contains_points(points) for i in range(3): self.assertEqual(in_cell_ref[i], in_cell[i]) # # Single points # # Vertex point = Vertex((3,3)) self.assertFalse(cell.contains_points(point)) # Tuple point = (1,0) self.assertTrue(cell.contains_points(point)) # Array point = np.array([1,0]) self.assertTrue(cell.contains_points(point))
def test_reference_map(self): v1 = Vertex((1,1)) v2 = Vertex((2,3)) h = HalfEdge(v1,v2) x = np.linspace(0, 1, 5) points = h.reference_map(x) y = h.reference_map(points, mapsto='reference') for (yi,xi) in zip(x,y): self.assertAlmostEqual(xi,yi)
def test_assign_cell(self): # Make a cell points = [(0,0),(1,0),(1,1),(0,1)] vertices = [Vertex(point) for point in points] half_edges = [HalfEdge(vertices[i], vertices[(i+1)%4]) for i in range(4)] cell = Cell(half_edges) # Make a (different) half_edge he = HalfEdge(Vertex((0,0)), Vertex((1,1))) he.assign_cell(cell) self.assertEqual(he.cell(),cell)
def test_make_twin(self): # # New HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((2,2)) h12 = HalfEdge(v1,v2) h21 = h12.make_twin() self.assertIsNotNone(h12.twin()) self.assertIsNotNone(h21.twin()) self.assertEqual(h12.base(),h21.head()) self.assertEqual(h21.base(),h12.head())
def test_next(self): # # New HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((2,2)) v3 = Vertex((3,4)) next_half_edge = HalfEdge(v1, v2) half_edge = HalfEdge(v3, v1, nxt=next_half_edge) # Check self.assertEqual(half_edge.next(),next_half_edge,\ 'Next HalfEdge incorrect.')
def test_previous(self): # # New HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((2,2)) v3 = Vertex((3,4)) previous_half_edge = HalfEdge(v1, v2) half_edge = HalfEdge(v2, v3, previous=previous_half_edge) # Check self.assertEqual(half_edge.previous(),previous_half_edge,\ 'Previous HalfEdge incorrect.')
def test_locate_point(self): v_sw = Vertex((0, 0)) v_se = Vertex((3, 1)) v_ne = Vertex((2, 3)) v_nw = Vertex((-1, 1)) h12 = HalfEdge(v_sw, v_se) h23 = HalfEdge(v_se, v_ne) h34 = HalfEdge(v_ne, v_nw) h41 = HalfEdge(v_nw, v_sw) cell = QuadCell([h12, h23, h34, h41]) points = np.random.rand(5, 2)
def test_constructor(self): # # Proper Interval # v1 = Vertex(0) v2 = Vertex(1) I = Interval(v1, v2) self.assertTrue(isinstance(I, HalfEdge)) # # Interval using 2D Vertices # w1 = Vertex((0, 0)) w2 = Vertex((1, 1)) self.assertRaises(Exception, Interval, *(w1, w2))
def test_get_parent(self): # # New HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((2,2)) half_edge = HalfEdge(v1, v2) # Split half_edge.split() # Check if half_edge is children's parent for child in half_edge.get_children(): self.assertEqual(half_edge, child.get_parent(),\ 'HalfEdge should be child"s parent')
def test_constructor(self): # Check the right number of halfedges self.assertRaises(Exception, QuadCell, *([1, 2, 2, 2, 2])) # Rectangle v1 = Vertex((0, 0)) v2 = Vertex((1, 0)) v3 = Vertex((1, 1)) v4 = Vertex((0, 1)) h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h34 = HalfEdge(v3, v4) h41 = HalfEdge(v4, v1) cell = QuadCell([h12, h23, h34, h41]) self.assertTrue(cell.is_rectangle())
def test_get_half_edges(self): # # Construct Cell # v1 = Vertex((0,0)) v2 = Vertex((1,0)) v3 = Vertex((0,1)) h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h31 = HalfEdge(v3, v1) # Check whether you get the right he's back hes = [h12, h23, h31] cell = Cell(hes) self.assertEqual(cell.get_half_edges(), hes)
def test_cell(self): points = [(0,0),(1,0),(1,1),(0,1)] vertices = [Vertex(point) for point in points] half_edges = [HalfEdge(vertices[i], vertices[(i+1)%4]) for i in range(4)] cell = Cell(half_edges) for he in half_edges: self.assertEqual(he.cell(), cell)
def test_split(self): # New (regular) Interval I = Interval(Vertex(0), Vertex(1), n_children=3) I.split() for i in range(2): self.assertEqual(I.get_child(i).next(), I.get_child(i + 1)) for i in np.arange(1, 3): self.assertEqual(I.get_child(i).previous(), I.get_child(i - 1)) # # Split the middle child # middle_child = I.get_child(1) # Check that you can't split the middle child into 2 because the # tree is not regular. self.assertRaises(Exception, middle_child.split, **{'n_children': 2}) # Split using the default number of children middle_child.split() # Check that middle child has 3 children self.assertEqual(middle_child.n_children(), 3) # Check that middle child's right child has no next interval self.assertEqual( middle_child.get_child(2).get_neighbor(1), I.get_child(2)) # # Split the right child # right_child = I.get_child(2) right_child.split() # Check that the right child has the correct number of children self.assertEqual(right_child.n_children(), 3) # Now the middle child's right child has a right neighbor self.assertEqual(middle_child.get_child(2).get_neighbor(1),\ right_child.get_child(0)) # # Irregular Interval # I = Interval(Vertex(0), Vertex(1), regular=False, n_children=3) I.split(n_children=2) for child in I.get_children(): self.assertEqual(child.n_children(), 2)
def test_get_vertex(self): # # Construct Cell # v1 = Vertex((0,0)) v2 = Vertex((1,0)) v3 = Vertex((0,1)) h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h31 = HalfEdge(v3, v1) # Check whether you get the right he's back vs = [v1, v2, v3] cell = Cell([h12,h23,h31]) for i in range(3): self.assertEqual(cell.get_vertex(i), vs[i])
def test_has_children(self): # # New HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((2,2)) half_edge = HalfEdge(v1, v2) # Shouldn't have children self.assertFalse(half_edge.has_children(),\ 'Half Edge should not have children') # Split edge half_edge.split() # Should have children self.assertTrue(half_edge.has_children(),\ 'HalfEdge should have children.')
def test_unmark(self): # # Define a HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((0,1)) h_edge = HalfEdge(v1,v2) # # Mark it with a specific flag # h_edge.mark(1) self.assertTrue(h_edge.is_marked(1),'HalfEdge should be marked.') # # Unmark it # h_edge.unmark(1) self.assertFalse(h_edge.is_marked(),'HalfEdge should be marked.') self.assertFalse(h_edge.is_marked(1),'HalfEdge should be marked.')
def test_has_parent(self): # # New HalfEdge # v1 = Vertex((0,0)) v2 = Vertex((2,2)) half_edge = HalfEdge(v1, v2) self.assertFalse(half_edge.has_parent(), \ 'Half Edge should not have a parent.') # Split edge half_edge.split() # Children should have parent for child in half_edge.get_children(): self.assertTrue(child.has_parent(), \ 'HalfEdge children should have a parent.')
def test_constructor(self): """ Constructor """ # # Triangle # v1 = Vertex((0,0)) v2 = Vertex((1,0)) v3 = Vertex((0,1)) h12 = HalfEdge(v1, v2) h23 = HalfEdge(v2, v3) h31 = HalfEdge(v3, v1) # Vertices not in order bad_list_1 = [h12, h31, h23] self.assertRaises(Exception, Cell, *[bad_list_1]) # Not a closed loop bad_list_2 = [h12, h23] self.assertRaises(Exception, Cell, *[bad_list_2]) triangle_half_edges = [h12, h23, h31] cell = Cell(triangle_half_edges) self.assertAlmostEqual(cell.area(),0.5) self.assertEqual(cell.n_vertices(),3) self.assertEqual(cell.n_half_edges(),3) half_edge = cell.get_half_edge(0) for i in range(3): self.assertEqual(half_edge.next(), triangle_half_edges[(i+1)%3]) half_edge = half_edge.next() # # Square # v4 = Vertex((1,1)) h24 = HalfEdge(v2,v4) h43 = HalfEdge(v4,v3) square_half_edges = [h12, h24, h43, h31] cell = Cell(square_half_edges) self.assertAlmostEqual(cell.area(),1) self.assertEqual(cell.n_vertices(),4) self.assertEqual(cell.n_half_edges(),4)