|
| 1 | +__all__ = ["OpenGLPMobject", "OpenGLPGroup", "OpenGLPMPoint"] |
| 2 | + |
| 3 | +import moderngl |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from ...constants import * |
| 7 | +from ...mobject.opengl_mobject import OpenGLMobject |
| 8 | +from ...utils.bezier import interpolate |
| 9 | +from ...utils.color import BLACK, WHITE, YELLOW, color_gradient, color_to_rgba |
| 10 | +from ...utils.config_ops import _Uniforms |
| 11 | +from ...utils.iterables import resize_with_interpolation |
| 12 | + |
| 13 | + |
| 14 | +class OpenGLPMobject(OpenGLMobject): |
| 15 | + shader_folder = "true_dot" |
| 16 | + # Scale for consistency with cairo units |
| 17 | + OPENGL_POINT_RADIUS_SCALE_FACTOR = 0.01 |
| 18 | + shader_dtype = [ |
| 19 | + ("point", np.float32, (3,)), |
| 20 | + ("color", np.float32, (4,)), |
| 21 | + ] |
| 22 | + |
| 23 | + point_radius = _Uniforms() |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, stroke_width=2.0, color=YELLOW, render_primitive=moderngl.POINTS, **kwargs |
| 27 | + ): |
| 28 | + self.stroke_width = stroke_width |
| 29 | + super().__init__(color=color, render_primitive=render_primitive, **kwargs) |
| 30 | + self.point_radius = ( |
| 31 | + self.stroke_width * OpenGLPMobject.OPENGL_POINT_RADIUS_SCALE_FACTOR |
| 32 | + ) |
| 33 | + |
| 34 | + def reset_points(self): |
| 35 | + self.rgbas = np.zeros((1, 4)) |
| 36 | + self.points = np.zeros((0, 3)) |
| 37 | + return self |
| 38 | + |
| 39 | + def get_array_attrs(self): |
| 40 | + return ["points", "rgbas"] |
| 41 | + |
| 42 | + def add_points(self, points, rgbas=None, color=None, opacity=None): |
| 43 | + """ |
| 44 | + points must be a Nx3 numpy array, as must rgbas if it is not None |
| 45 | + """ |
| 46 | + if rgbas is None and color is None: |
| 47 | + color = YELLOW |
| 48 | + self.append_points(points) |
| 49 | + # rgbas array will have been resized with points |
| 50 | + if color is not None: |
| 51 | + if opacity is None: |
| 52 | + opacity = self.rgbas[-1, 3] |
| 53 | + new_rgbas = np.repeat([color_to_rgba(color, opacity)], len(points), axis=0) |
| 54 | + elif rgbas is not None: |
| 55 | + new_rgbas = rgbas |
| 56 | + elif len(rgbas) != len(points): |
| 57 | + raise ValueError("points and rgbas must have same shape") |
| 58 | + self.rgbas = np.append(self.rgbas, new_rgbas, axis=0) |
| 59 | + return self |
| 60 | + |
| 61 | + def thin_out(self, factor=5): |
| 62 | + """ |
| 63 | + Removes all but every nth point for n = factor |
| 64 | + """ |
| 65 | + for mob in self.family_members_with_points(): |
| 66 | + num_points = mob.get_num_points() |
| 67 | + |
| 68 | + def thin_func(): |
| 69 | + return np.arange(0, num_points, factor) |
| 70 | + |
| 71 | + if len(mob.points) == len(mob.rgbas): |
| 72 | + mob.set_rgba_array_direct(mob.rgbas[thin_func()]) |
| 73 | + mob.set_points(mob.points[thin_func()]) |
| 74 | + |
| 75 | + return self |
| 76 | + |
| 77 | + def set_color_by_gradient(self, *colors): |
| 78 | + self.rgbas = np.array( |
| 79 | + list(map(color_to_rgba, color_gradient(*colors, self.get_num_points()))) |
| 80 | + ) |
| 81 | + return self |
| 82 | + |
| 83 | + def set_colors_by_radial_gradient( |
| 84 | + self, center=None, radius=1, inner_color=WHITE, outer_color=BLACK |
| 85 | + ): |
| 86 | + start_rgba, end_rgba = list(map(color_to_rgba, [inner_color, outer_color])) |
| 87 | + if center is None: |
| 88 | + center = self.get_center() |
| 89 | + for mob in self.family_members_with_points(): |
| 90 | + distances = np.abs(self.points - center) |
| 91 | + alphas = np.linalg.norm(distances, axis=1) / radius |
| 92 | + |
| 93 | + mob.rgbas = np.array( |
| 94 | + np.array([interpolate(start_rgba, end_rgba, alpha) for alpha in alphas]) |
| 95 | + ) |
| 96 | + return self |
| 97 | + |
| 98 | + def match_colors(self, pmobject): |
| 99 | + self.rgbas[:] = resize_with_interpolation(pmobject.rgbas, self.get_num_points()) |
| 100 | + return self |
| 101 | + |
| 102 | + def fade_to(self, color, alpha, family=True): |
| 103 | + rgbas = interpolate(self.rgbas, color_to_rgba(color), alpha) |
| 104 | + for mob in self.submobjects: |
| 105 | + mob.fade_to(color, alpha, family) |
| 106 | + self.set_rgba_array_direct(rgbas) |
| 107 | + return self |
| 108 | + |
| 109 | + def filter_out(self, condition): |
| 110 | + for mob in self.family_members_with_points(): |
| 111 | + to_keep = ~np.apply_along_axis(condition, 1, mob.get_points()) |
| 112 | + for key in mob.data: |
| 113 | + mob.data[key] = mob.data[key][to_keep] |
| 114 | + return self |
| 115 | + |
| 116 | + def sort_points(self, function=lambda p: p[0]): |
| 117 | + """ |
| 118 | + function is any map from R^3 to R |
| 119 | + """ |
| 120 | + for mob in self.family_members_with_points(): |
| 121 | + indices = np.argsort(np.apply_along_axis(function, 1, mob.get_points())) |
| 122 | + for key in mob.data: |
| 123 | + mob.data[key] = mob.data[key][indices] |
| 124 | + return self |
| 125 | + |
| 126 | + def ingest_submobjects(self): |
| 127 | + for key in self.data: |
| 128 | + self.data[key] = np.vstack([sm.data[key] for sm in self.get_family()]) |
| 129 | + return self |
| 130 | + |
| 131 | + def point_from_proportion(self, alpha): |
| 132 | + index = alpha * (self.get_num_points() - 1) |
| 133 | + return self.get_points()[int(index)] |
| 134 | + |
| 135 | + def pointwise_become_partial(self, pmobject, a, b): |
| 136 | + lower_index = int(a * pmobject.get_num_points()) |
| 137 | + upper_index = int(b * pmobject.get_num_points()) |
| 138 | + for key in self.data: |
| 139 | + self.data[key] = pmobject.data[key][lower_index:upper_index] |
| 140 | + return self |
| 141 | + |
| 142 | + def get_shader_data(self): |
| 143 | + shader_data = np.zeros(len(self.get_points()), dtype=self.shader_dtype) |
| 144 | + self.read_data_to_shader(shader_data, "point", "points") |
| 145 | + self.read_data_to_shader(shader_data, "color", "rgbas") |
| 146 | + return shader_data |
| 147 | + |
| 148 | + |
| 149 | +class OpenGLPGroup(OpenGLPMobject): |
| 150 | + def __init__(self, *pmobs, **kwargs): |
| 151 | + if not all([isinstance(m, OpenGLPMobject) for m in pmobs]): |
| 152 | + raise Exception("All submobjects must be of type OpenglPMObject") |
| 153 | + super().__init__(**kwargs) |
| 154 | + self.add(*pmobs) |
| 155 | + |
| 156 | + def fade_to(self, color, alpha, family=True): |
| 157 | + if family: |
| 158 | + for mob in self.submobjects: |
| 159 | + mob.fade_to(color, alpha, family) |
| 160 | + |
| 161 | + |
| 162 | +class OpenGLPMPoint(OpenGLPMobject): |
| 163 | + def __init__(self, location=ORIGIN, stroke_width=4.0, color=BLACK, **kwargs): |
| 164 | + self.location = location |
| 165 | + super().__init__(stroke_width=stroke_width, **kwargs) |
| 166 | + |
| 167 | + def init_points(self): |
| 168 | + self.points = np.array([self.location], dtype=np.float32) |
0 commit comments