def test_vgroup_init(using_opengl_renderer): """Test the VGroup instantiation.""" VGroup() VGroup(OpenGLVMobject()) VGroup(OpenGLVMobject(), OpenGLVMobject()) with pytest.raises(TypeError): VGroup(OpenGLMobject()) with pytest.raises(TypeError): VGroup(OpenGLMobject(), OpenGLMobject())
def test_vgroup_supports_item_assigment(using_opengl_renderer): """Test VGroup supports array-like assignment for OpenGLVMObjects""" a = OpenGLVMobject() b = OpenGLVMobject() vgroup = VGroup(a) assert vgroup[0] == a vgroup[0] = b assert vgroup[0] == b assert len(vgroup) == 1
def test_vgroup_item_assignment_at_correct_position(using_opengl_renderer): """Test VGroup item-assignment adds to correct position for OpenGLVMObjects""" n_items = 10 vgroup = VGroup() for _i in range(n_items): vgroup.add(OpenGLVMobject()) new_obj = OpenGLVMobject() vgroup[6] = new_obj assert vgroup[6] == new_obj assert len(vgroup) == n_items
def test_vgroup_remove(using_opengl_renderer): """Test the VGroup remove method.""" a = OpenGLVMobject() c = OpenGLVMobject() b = VGroup(c) obj = VGroup(a, b) assert len(obj.submobjects) == 2 assert len(b.submobjects) == 1 obj.remove(a) b.remove(c) assert len(obj.submobjects) == 1 assert len(b.submobjects) == 0 obj.remove(b) assert len(obj.submobjects) == 0
def test_vgroup_remove_dunder(using_opengl_renderer): """Test the VGroup __sub__ magic method.""" a = OpenGLVMobject() c = OpenGLVMobject() b = VGroup(c) obj = VGroup(a, b) assert len(obj.submobjects) == 2 assert len(b.submobjects) == 1 assert len(obj - a) == 1 assert len(obj.submobjects) == 2 obj -= a b -= c assert len(obj.submobjects) == 1 assert len(b.submobjects) == 0 obj -= b assert len(obj.submobjects) == 0
def test_set_stroke_handles_lists_of_color_objects(using_opengl_renderer): m = OpenGLVMobject() assert m.stroke_color.hex == "#fff" m.set_stroke([Color(PURE_BLUE), Color(PURE_GREEN), Color(PURE_RED)]) assert m.get_stroke_colors()[0].hex == "#00f" assert m.get_stroke_colors()[1].hex == "#0f0" assert m.get_stroke_colors()[2].hex == "#f00"
def test_set_color(using_opengl_renderer): m = OpenGLMobject() assert m.color.hex == "#fff" np.alltrue(m.rgbas == np.array((0.0, 0.0, 0.0, 1.0))) m.set_color(BLACK) assert m.color.hex == "#000" np.alltrue(m.rgbas == np.array((1.0, 1.0, 1.0, 1.0))) m.set_color(PURE_GREEN, opacity=0.5) assert m.color.hex == "#0f0" np.alltrue(m.rgbas == np.array((0.0, 1.0, 0.0, 0.5))) m = OpenGLVMobject() assert m.color.hex == "#fff" np.alltrue(m.fill_rgba == np.array((0.0, 0.0, 0.0, 1.0))) np.alltrue(m.stroke_rgba == np.array((0.0, 0.0, 0.0, 1.0))) m.set_color(BLACK) assert m.color.hex == "#000" np.alltrue(m.fill_rgba == np.array((1.0, 1.0, 1.0, 1.0))) np.alltrue(m.stroke_rgba == np.array((1.0, 1.0, 1.0, 1.0))) m.set_color(PURE_GREEN, opacity=0.5) assert m.color.hex == "#0f0" np.alltrue(m.fill_rgba == np.array((0.0, 1.0, 0.0, 0.5))) np.alltrue(m.stroke_rgba == np.array((0.0, 1.0, 0.0, 0.5)))
def test_vmob_add_to_back(using_opengl_renderer): """Test the OpenGLMobject add_to_back method.""" a = OpenGLVMobject() b = Line() c = "text" with pytest.raises(ValueError): # OpenGLMobject cannot contain self a.add_to_back(a) with pytest.raises(TypeError): # All submobjects must be of type OpenGLMobject a.add_to_back(c) # No submobject gets added twice a.add_to_back(b) a.add_to_back(b, b) assert len(a.submobjects) == 1 a.submobjects.clear() a.add_to_back(b, b, b) a.add_to_back(b, b) assert len(a.submobjects) == 1 a.submobjects.clear() # Make sure the ordering has not changed o1, o2, o3 = Square(), Line(), Circle() a.add_to_back(o1, o2, o3) assert a.submobjects.pop() == o3 assert a.submobjects.pop() == o2 assert a.submobjects.pop() == o1
def test_set_stroke_handles_lists_of_strs(using_opengl_renderer): m = OpenGLVMobject() assert m.stroke_color.hex == "#fff" m.set_stroke([BLACK, BLUE, GREEN]) assert m.get_stroke_colors()[0] == Color(BLACK) assert m.get_stroke_colors()[1] == Color(BLUE) assert m.get_stroke_colors()[2] == Color(GREEN)
def test_vgroup_add(using_opengl_renderer): """Test the VGroup add method.""" obj = VGroup() assert len(obj.submobjects) == 0 obj.add(OpenGLVMobject()) assert len(obj.submobjects) == 1 with pytest.raises(TypeError): obj.add(OpenGLMobject()) assert len(obj.submobjects) == 1 with pytest.raises(TypeError): # If only one of the added object is not an instance of OpenGLVMobject, none of them should be added obj.add(OpenGLVMobject(), OpenGLMobject()) assert len(obj.submobjects) == 1 with pytest.raises(ValueError): # a OpenGLMobject cannot contain itself obj.add(obj)
def test_vdict_remove(using_opengl_renderer): """Test the VDict remove method.""" obj = VDict([("a", OpenGLVMobject())]) assert len(obj.submob_dict) == 1 obj.remove("a") assert len(obj.submob_dict) == 0 with pytest.raises(KeyError): obj.remove("a")
def test_vdict_add(using_opengl_renderer): """Test the VDict add method.""" obj = VDict() assert len(obj.submob_dict) == 0 obj.add([("a", OpenGLVMobject())]) assert len(obj.submob_dict) == 1 with pytest.raises(TypeError): obj.add([("b", OpenGLMobject())])
def test_set_fill(using_opengl_renderer): m = OpenGLMobject() assert m.color.hex == "#fff" m.set_color(BLACK) assert m.color.hex == "#000" m = OpenGLVMobject() assert m.color.hex == "#fff" m.set_color(BLACK) assert m.color.hex == "#000"
def init_points(self): uv_surface = self.uv_surface full_nu, full_nv = uv_surface.resolution part_nu, part_nv = self.resolution u_indices = np.linspace(0, full_nu, part_nu).astype(int) v_indices = np.linspace(0, full_nv, part_nv).astype(int) points, du_points, dv_points = uv_surface.get_surface_points_and_nudged_points( ) normals = uv_surface.get_unit_normals() nudged_points = points + self.normal_nudge * normals for ui in u_indices: path = OpenGLVMobject() full_ui = full_nv * ui path.set_points_smoothly(nudged_points[full_ui:full_ui + full_nv]) self.add(path) for vi in v_indices: path = OpenGLVMobject() path.set_points_smoothly(nudged_points[vi::full_nv]) self.add(path)
def test_vdict_init(using_opengl_renderer): """Test the VDict instantiation.""" # Test empty VDict VDict() # Test VDict made from list of pairs VDict([("a", OpenGLVMobject()), ("b", OpenGLVMobject()), ("c", OpenGLVMobject())]) # Test VDict made from a python dict VDict({ "a": OpenGLVMobject(), "b": OpenGLVMobject(), "c": OpenGLVMobject() }) # Test VDict made using zip VDict( zip(["a", "b", "c"], [OpenGLVMobject(), OpenGLVMobject(), OpenGLVMobject()])) # If the value is of type OpenGLMobject, must raise a TypeError with pytest.raises(TypeError): VDict({"a": OpenGLMobject()})
def test_opengl_vmobject_point_from_propotion(using_opengl_renderer): obj = OpenGLVMobject() # One long line, one short line obj.set_points_as_corners([ np.array([0, 0, 0]), np.array([4, 0, 0]), np.array([4, 2, 0]), ], ) # Total length of 6, so halfway along the object # would be at length 3, which lands in the first, long line. assert np.all(obj.point_from_proportion(0.5) == np.array([3, 0, 0])) with pytest.raises(ValueError, match="between 0 and 1"): obj.point_from_proportion(2) obj.clear_points() with pytest.raises(Exception, match="with no points"): obj.point_from_proportion(0)
def test_stroke_props_in_ctor(using_opengl_renderer): m = OpenGLVMobject(stroke_color=C.ORANGE, stroke_width=10) assert m.stroke_color == Color(C.ORANGE) assert m.stroke_width == 10
def test_set_stroke(using_opengl_renderer): m = OpenGLVMobject() m.set_stroke(color=C.ORANGE, width=2, opacity=0.8) assert m.stroke_width == 2 assert m.stroke_opacity == 0.8 assert m.stroke_color == Color(C.ORANGE)
def __init__( self, func: Callable[[np.ndarray], np.ndarray], color: Color | None = None, color_scheme: Callable[[np.ndarray], float] | None = None, min_color_scheme_value: float = 0, max_color_scheme_value: float = 2, colors: Sequence[Color] = DEFAULT_SCALAR_FIELD_COLORS, # Determining stream line starting positions: x_range: Sequence[float] = None, y_range: Sequence[float] = None, z_range: Sequence[float] = None, three_dimensions: bool = False, noise_factor: float | None = None, n_repeats=1, # Determining how lines are drawn dt=0.05, virtual_time=3, max_anchors_per_line=100, padding=3, # Determining stream line appearance: stroke_width=1, opacity=1, **kwargs, ): self.x_range = x_range or [ floor(-config["frame_width"] / 2), ceil(config["frame_width"] / 2), ] self.y_range = y_range or [ floor(-config["frame_height"] / 2), ceil(config["frame_height"] / 2), ] self.ranges = [self.x_range, self.y_range] if three_dimensions or z_range: self.z_range = z_range or self.y_range.copy() self.ranges += [self.z_range] else: self.ranges += [[0, 0]] for i in range(len(self.ranges)): if len(self.ranges[i]) == 2: self.ranges[i] += [0.5] self.ranges[i][1] += self.ranges[i][2] self.x_range, self.y_range, self.z_range = self.ranges super().__init__( func, color, color_scheme, min_color_scheme_value, max_color_scheme_value, colors, **kwargs, ) self.noise_factor = (noise_factor if noise_factor is not None else self.y_range[2] / 2) self.n_repeats = n_repeats self.virtual_time = virtual_time self.max_anchors_per_line = max_anchors_per_line self.padding = padding self.stroke_width = stroke_width half_noise = self.noise_factor / 2 np.random.seed(0) start_points = np.array( [(x - half_noise) * RIGHT + (y - half_noise) * UP + (z - half_noise) * OUT + self.noise_factor * np.random.random(3) for n in range(self.n_repeats) for x in np.arange(*self.x_range) for y in np.arange(*self.y_range) for z in np.arange(*self.z_range)], ) def outside_box(p): return (p[0] < self.x_range[0] - self.padding or p[0] > self.x_range[1] + self.padding - self.x_range[2] or p[1] < self.y_range[0] - self.padding or p[1] > self.y_range[1] + self.padding - self.y_range[2] or p[2] < self.z_range[0] - self.padding or p[2] > self.z_range[1] + self.padding - self.z_range[2]) max_steps = ceil(virtual_time / dt) + 1 if not self.single_color: self.background_img = self.get_colored_background_image() if config["renderer"] == "opengl": self.values_to_rgbas = self.get_vectorized_rgba_gradient_function( min_color_scheme_value, max_color_scheme_value, colors, ) for point in start_points: points = [point] for _ in range(max_steps): last_point = points[-1] new_point = last_point + dt * func(last_point) if outside_box(new_point): break points.append(new_point) step = max_steps if not step: continue if config["renderer"] == "opengl": line = OpenGLVMobject() else: line = VMobject() line.duration = step * dt step = max(1, int(len(points) / self.max_anchors_per_line)) line.set_points_smoothly(points[::step]) if self.single_color: line.set_stroke(self.color) else: if config["renderer"] == "opengl": # scaled for compatibility with cairo line.set_stroke(width=self.stroke_width / 4.0) norms = np.array([ np.linalg.norm(self.func(point)) for point in line.points ], ) line.set_rgba_array_direct( self.values_to_rgbas(norms, opacity), name="stroke_rgba", ) else: if np.any(self.z_range != np.array([0, 0.5, 0.5])): line.set_stroke( [self.pos_to_color(p) for p in line.get_anchors()], ) else: line.color_using_background_image(self.background_img) line.set_stroke(width=self.stroke_width, opacity=opacity) self.add(line) self.stream_lines = [*self.submobjects]
def test_vgroup_item_assignment_only_allows_vmobjects(using_opengl_renderer): """Test VGroup item-assignment raises TypeError when invalid type is passed""" vgroup = VGroup(OpenGLVMobject()) with pytest.raises(TypeError, match="All submobjects must be of type VMobject"): vgroup[0] = "invalid object"