def test_prim_container(self): disk_primitive = draw.Disks(positions=[[0, 0], [1, 2], [2, 4]], colors=[[1, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]], radii=[1, 2, 3], outline=0.5) assert len(disk_primitive) == 3 disk1 = disk_primitive[1] disk_reversed = disk_primitive[::-1] disk_masked = disk_primitive[[True, False, True]] assert len(disk1) == 1 assert len(disk_reversed) == 3 assert len(disk_masked) == 2 npt.assert_almost_equal(disk1.positions, [[1, 2]]) npt.assert_almost_equal(disk_reversed.colors, [[0, 0, 0, 1], [0, 0, 1, 1], [1, 1, 0, 1]]) npt.assert_almost_equal(disk_masked.radii, [1, 3]) # Ensure that length corresponds to the size of the # shortest per-particle property array disk_primitive = draw.Disks(positions=[[0, 0], [1, 2], [2, 4]], colors=[[1, 1, 0, 1], [0, 0, 1, 1]], radii=[1, 2, 3], outline=0.5) assert len(disk_primitive) == 2
def simple_2d(seed=13, num_particles=2): np.random.seed(seed) positions = np.random.uniform(0, 3, (num_particles, 2)) colors = np.random.rand(num_particles, 4) orientations = np.random.rand(num_particles, 4) orientations[:, 1:3] = 0 orientations /= np.linalg.norm(orientations, axis=-1, keepdims=True) thetas = np.linspace(0, 2*np.pi, 5, endpoint=False) vertices = np.array([np.cos(thetas), np.sin(thetas)]).T prim1 = draw.Arrows2D(positions=positions, colors=colors*.5, orientations=orientations, magnitudes=[.25, .5]) prim1.vertices = prim1.vertices - (-.5, 0) prim2 = draw.Disks(outline=.05, positions=positions, colors=colors, diameters=np.ones((num_particles,))) prim3 = draw.Polygons(positions=-positions, colors=colors, vertices=vertices, outline=.05, orientations=orientations) prim4 = draw.Spheropolygons(positions=-positions, colors=colors, vertices=vertices, outline=.05, orientations=orientations) prim4.radius = .1 prim4.positions = prim3.positions + (3, 2) scene = draw.Scene([prim2, prim3, prim4, prim1], zoom=4, features=dict(pan=True)) return scene
def voronoi_with_disks(seed=13, num_points=32): np.random.seed(seed) positions = np.random.uniform(-3, 3, (num_points, 2)) colors = np.random.rand(num_points, 4) colors[:, 3] = 1 invbox = np.linalg.inv([[6, 0], [0, 6]])*2 prim = draw.Voronoi(positions=positions, colors=colors, clip_extent=invbox) prim2 = draw.Disks(positions=positions, colors=colors, diameters=np.ones((num_points,))*.5, outline=.125) scene = draw.Scene([prim, prim2], zoom=4, features=dict(pan=True)) return scene
def disks_and_lines(): thetas = np.linspace(0, 2*np.pi, 6, endpoint=False) extra_positions = np.array([np.cos(thetas), np.sin(thetas)]).T*1.1 positions = np.array([[0, 0]] + extra_positions.tolist()) colors = np.tile([[.25, .25, .8, 1]], (len(positions), 1)) diameters = np.ones((len(positions),)) prim1 = draw.Disks(positions=positions, colors=colors, diameters=diameters) positions_3d = np.pad(positions, ((0, 0), (0, 1)), 'constant') colors = np.tile([[.1, .1, .1, 1]], (6, 1)) prim2 = draw.Lines(start_points=np.tile(positions_3d[:1], (6, 1)), end_points=positions_3d[1:], widths=np.ones((6,))*.25, colors=colors) return draw.Scene([prim2, prim1], zoom=10)
def sunflower_2d(seed=13): center_disk = draw.Disks(outline=.05, positions=np.zeros((1, 2)), colors=np.array([0.225, 0.1, 0., 1]), diameters=np.sqrt(3)) thetas = np.linspace(0, 2*np.pi, 6, endpoint=False) vertices = np.array([np.cos(thetas), np.sin(thetas)]).T orientations = np.array([[np.cos(np.pi/12), 0, 0, np.sin(np.pi/12)]] * 6) petals = draw.Polygons(positions=np.sqrt(3)*vertices, colors=np.array([[0.9, 0.7, 0, 1]]*6), vertices=vertices, outline=.05, orientations=orientations) """ prim2 = draw.Disks(outline=.05, positions=positions, colors=colors, diameters=np.ones((num_particles,))) prim3 = draw.Polygons(positions=-positions, colors=colors, vertices=vertices, outline=.05, orientations=orientations) """ scene = draw.Scene([center_disk, petals], zoom=4, features=dict(pan=True)) return scene
def test_add_remove_prims(self): prim1 = draw.Disks() prim2 = draw.Spheres() scene = draw.Scene(prim1) self.assertIn(prim1, scene) self.assertNotIn(prim2, scene) scene.add_primitive(prim2) self.assertIn(prim2, scene) scene.remove_primitive(prim2) self.assertNotIn(prim2, scene) with self.assertRaises(ValueError): scene.remove_primitive(prim2) # shouldn't encounter exception with strict mode disabled scene.remove_primitive(prim2, strict=False) # prim1 should still be there self.assertIn(prim1, scene)