def test_draw(self): p = _MockSTPlotter() ribbon = geom.Ribbon(geom.Line((0, 1), (2, 3)), geom.Line((0, 1), (0, 0)), tag='test', draw_options={ 'facecolor': 'red', 'label': 'test2' }) ribbon.draw(p, tlim=(-2, 3), xlim=(0, 1)) self.assertEqual(len(p.points), 0) self.assertEqual(len(p.segments), 2) self.assertEqual(len(p.polygons), 1) p.polygons_equal(self, p.polygons[0], ([(0, 0), (0, 1), (2, 1), (2, 0)], 'test', { 'facecolor': 'red', 'label': 'test2' })) p.segments_equal(self, p.segments[0], ((2, 0), (2, 1), None, { 'color': geom.geomrc['ribbon.default_edgecolor'], 'zorder': 1 })) p.segments_equal(self, p.segments[1], ((0, 0), (0, 1), None, { 'color': geom.geomrc['ribbon.default_edgecolor'], 'zorder': 1 })) self.assertEqual(p.tlim, (-2, 3)) self.assertEqual(p.xlim, (0, 1))
def test_draw_out_of_bounds(self): p = _MockSTPlotter() ribbon = geom.Ribbon(geom.Line((0, 1), (2, 3)), geom.Line((0, 1), (0, 0))) ribbon.draw(p, tlim=(4, 5), xlim=(0, 1)) self.assertEqual(len(p.segments), 0) self.assertEqual(len(p.polygons), 0)
def test_lorentz_transform(self): v = 3 / 5 group_transformed = geom.lorentz_transformed( geom.PointGroup([(0, 1), (2, 3)]), v) line_transformed = geom.lorentz_transformed(geom.Line((0, 1), (2, 3)), v) ribbon_transformed = geom.lorentz_transformed( geom.Ribbon(geom.Line((0, 1), (2, 3)), geom.Line((0, 1), (0, 0))), v) self.collection.lorentz_transform(v) self.assertAlmostEqual(self.collection[0][0], group_transformed[0]) self.assertAlmostEqual(self.collection[0][1], group_transformed[1]) self.assertAlmostEqual(self.collection[1].direction(), line_transformed.direction()) self.assertAlmostEqual(self.collection[1].point(), line_transformed.point()) self.assertAlmostEqual(self.collection[2][0].direction(), ribbon_transformed[0].direction()) self.assertAlmostEqual(self.collection[2][0].point(), ribbon_transformed[0].point()) self.assertAlmostEqual(self.collection[2][1].direction(), ribbon_transformed[1].direction()) self.assertAlmostEqual(self.collection[2][1].point(), ribbon_transformed[1].point())
def test_get_vertices_both_lines_out_of_bounds(self): ribbon = geom.Ribbon( geom.Line((1, 1), (3, 0)), geom.Line((1, 1), (0, 3)), ) self.assertEqual(ribbon._get_vertices((0, 2), (0, 2)), [(0, 0), (0, 2), (2, 2), (2, 0)])
def test_get_vertices_line_on_corner(self): ribbon = geom.Ribbon( geom.Line((1, 1), (1, 0)), geom.Line((1, 1), (0, 2)), ) self.assertEqual(ribbon._get_vertices((0, 2), (0, 2)), [(0, 0), (0, 2), (2, 2), (2, 1), (1, 0)])
def test_get_vertices_exact_boundary(self): ribbon = geom.Ribbon( geom.Line((0, 1), (1, 0)), geom.Line((0, 1), (3, 0)), ) self.assertEqual(ribbon._get_vertices((0, 3), (0, 3)), [(1, 0), (1, 3), (3, 3), (3, 0)])
def test_get_vertices_flat_lines(self): ribbon = geom.Ribbon( geom.Line((0, 1), (1, 0)), geom.Line((0, 1), (2, 0)), ) self.assertEqual(ribbon._get_vertices((0, 3), (0, 3)), [(1, 0), (1, 3), (2, 3), (2, 0)])
def test_draw_no_edges(self): p = _MockSTPlotter() ribbon = geom.Ribbon(geom.Line((0, 1), (2, 3)), geom.Line((0, 1), (0, 0)), draw_options={'edgecolor': 'None'}) ribbon.draw(p, tlim=(-2, 3), xlim=(0, 1)) self.assertEqual(len(p.segments), 0) self.assertEqual(len(p.polygons), 1)
def lateral_gradient_ribbon(direction, point1, point2, color1, color2, divisions=100, draw_options=geom.geomrc['draw_options']): """A `specrel.geom.Ribbon`-esque object with a lateral color gradient (across the finite direction). Args: direction (specrel.geom.STVector or iterable): Direction vector for the gradient. point1 (specrel.geom.STVector or iterable): A point that the first edge of the ribbon passes through. point2 (specrel.geom.STVector or iterable): A point that the second edge of the ribbon passes through. color1 (color): A Matplotlib color for the gradient starting color. color2 (color): A Matplotlib color for the gradient ending color. divisions (int, optional): The number of line segment divisions in the gradient. More divisions means a smoother gradient. draw_options (TYPE, optional): See `specrel.geom.LorentzTransformable`. Returns: specrel.geom.Collection: Collection containing the `specrel.geom.Ribbon` objects of slowly changing color from `point1` to `point2`. """ # Copy draw_options and remove color, facecolor, and edgecolor if they're # there draw_options = dict(draw_options) draw_options.pop('color', None) draw_options.pop('facecolor', None) draw_options.pop('edgecolor', None) # Color gradient calculator for the given points and colors def this_colorgrad(x): return _calc_colorgrad(x, point1, point2, color1, color2) grad = geom.Collection() # Ribbons comprising the color gradient, as we move from point1 to point2 for i in range(divisions): start_point, _ = this_colorgrad(i / divisions) # Overlap bands by half a division, except the last one end_point, _ = this_colorgrad(min((i + 1 + 1 / 2) / divisions, 1)) _, grad_color = this_colorgrad((i + 1 / 2) / divisions) # Explicitly turn off edge coloring grad.append( geom.Ribbon(geom.Line(direction, start_point), geom.Line(direction, end_point), draw_options={ 'facecolor': grad_color, 'edgecolor': 'None', **draw_options })) return grad
def test_init(self): ribbon = geom.Ribbon(geom.Line((0, 1), (2, 3)), geom.Line((0, 1), (0, 0)), tag='test', draw_options={'color': 'red'}) self.assertEqual(ribbon[0].direction().t, 0) self.assertEqual(ribbon[0].direction().x, 1) self.assertEqual(ribbon[0].point().t, 2) self.assertEqual(ribbon[0].point().x, 3) self.assertEqual(ribbon[1].direction().t, 0) self.assertEqual(ribbon[1].direction().x, 1) self.assertEqual(ribbon[1].point().t, 0) self.assertEqual(ribbon[1].point().x, 0) self.assertEqual(ribbon.tag, 'test') self.assertEqual(ribbon.draw_options, {'color': 'red'})
def test_point_on_boundary(self): ribbon = geom.Ribbon(geom.Line((1, 1), (0, 2)), geom.Line((1, 1), (0, 0))) self.assertTrue(ribbon._point_inside((0, 2)))
def test_point_not_inside(self): ribbon = geom.Ribbon(geom.Line((1, 1), (0, 2)), geom.Line((1, 1), (0, 0))) self.assertFalse(ribbon._point_inside((0, 3)))
def setUp(self): group = geom.PointGroup([(0, 1), (2, 3)]) line = geom.Line((0, 1), (2, 3)) ribbon = geom.Ribbon(geom.Line((0, 1), (2, 3)), geom.Line((0, 1), (0, 0))) self.collection = geom.Collection([group, line, ribbon])