Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Port remainder of geometry.py

Devin Nealbc643e9d7d4c
diff --git a/manim/mobject/opengl_geometry.py b/manim/mobject/opengl_geometry.py
index fb945089c..ffbb5f9ab 100644
--- a/manim/mobject/opengl_geometry.py
+++ b/manim/mobject/opengl_geometry.py
@@ -10,7 +10,7 @@ from ..utils.iterables import adjacent_n_tuples
from ..utils.iterables import adjacent_pairs
from ..utils.simple_functions import fdiv
-# from ..utils.simple_functions import clip
+from ..utils.simple_functions import clip
from ..utils.space_ops import angle_of_vector
from ..utils.space_ops import angle_between_vectors
from ..utils.space_ops import compass_directions
@@ -90,7 +90,7 @@ class OpenGLTipableVMobject(OpenGLVMobject):
config = dict()
config.update(self.tip_config)
config.update(kwargs)
- return ArrowTip(**config)
+ return OpenGLArrowTip(**config)
def position_tip(self, tip, at_start=False):
# Last two control points, defining both
@@ -591,164 +591,176 @@ class OpenGLDashedLine(OpenGLLine):
return self.submobjects[-1].get_points()[-2]
-# class TangentLine(Line):
-# CONFIG = {"length": 1, "d_alpha": 1e-6}
-#
-# def __init__(self, vmob, alpha, **kwargs):
-# digest_config(self, kwargs)
-# da = self.d_alpha
-# a1 = clip(alpha - da, 0, 1)
-# a2 = clip(alpha + da, 0, 1)
-# super().__init__(vmob.pfp(a1), vmob.pfp(a2), **kwargs)
-# self.scale(self.length / self.get_length())
-#
-#
-# class Elbow(VMobject):
-# CONFIG = {
-# "width": 0.2,
-# "angle": 0,
-# }
-#
-# def __init__(self, **kwargs):
-# super().__init__(self, **kwargs)
-# self.set_points_as_corners([UP, UP + RIGHT, RIGHT])
-# self.set_width(self.width, about_point=ORIGIN)
-# self.rotate(self.angle, about_point=ORIGIN)
-#
-#
-# class Arrow(Line):
-# CONFIG = {
-# "fill_color": GREY_A,
-# "fill_opacity": 1,
-# "stroke_width": 0,
-# "buff": MED_SMALL_BUFF,
-# "thickness": 0.05,
-# "tip_width_ratio": 5,
-# "tip_angle": PI / 3,
-# "max_tip_length_to_length_ratio": 0.5,
-# "max_width_to_length_ratio": 0.1,
-# }
-#
-# def set_points_by_ends(self, start, end, buff=0, path_arc=0):
-# # Find the right tip length and thickness
-# vect = end - start
-# length = max(get_norm(vect), 1e-8)
-# thickness = self.thickness
-# w_ratio = fdiv(self.max_width_to_length_ratio, fdiv(thickness, length))
-# if w_ratio < 1:
-# thickness *= w_ratio
-#
-# tip_width = self.tip_width_ratio * thickness
-# tip_length = tip_width / (2 * np.tan(self.tip_angle / 2))
-# t_ratio = fdiv(self.max_tip_length_to_length_ratio, fdiv(tip_length, length))
-# if t_ratio < 1:
-# tip_length *= t_ratio
-# tip_width *= t_ratio
-#
-# # Find points for the stem
-# if path_arc == 0:
-# points1 = (length - tip_length) * np.array([RIGHT, 0.5 * RIGHT, ORIGIN])
-# points1 += thickness * UP / 2
-# points2 = points1[::-1] + thickness * DOWN
-# else:
-# # Solve for radius so that the tip-to-tail length matches |end - start|
-# a = 2 * (1 - np.cos(path_arc))
-# b = -2 * tip_length * np.sin(path_arc)
-# c = tip_length ** 2 - length ** 2
-# R = (-b + np.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
-#
-# # Find arc points
-# points1 = Arc.create_quadratic_bezier_points(path_arc)
-# points2 = np.array(points1[::-1])
-# points1 *= R + thickness / 2
-# points2 *= R - thickness / 2
-# if path_arc < 0:
-# tip_length *= -1
-# rot_T = rotation_matrix_transpose(PI / 2 - path_arc, OUT)
-# for points in points1, points2:
-# points[:] = np.dot(points, rot_T)
-# points += R * DOWN
-#
-# self.set_points(points1)
-# # Tip
-# self.add_line_to(tip_width * UP / 2)
-# self.add_line_to(tip_length * LEFT)
-# self.tip_index = len(self.get_points()) - 1
-# self.add_line_to(tip_width * DOWN / 2)
-# self.add_line_to(points2[0])
-# # Close it out
-# self.append_points(points2)
-# self.add_line_to(points1[0])
-#
-# if length > 0:
-# # Final correction
-# super().scale(length / self.get_length())
-#
-# self.rotate(angle_of_vector(vect) - self.get_angle())
-# self.rotate(
-# PI / 2 - np.arccos(normalize(vect)[2]),
-# axis=rotate_vector(self.get_unit_vector(), -PI / 2),
-# )
-# self.shift(start - self.get_start())
-# self.refresh_triangulation()
-#
-# def reset_points_around_ends(self):
-# self.set_points_by_ends(
-# self.get_start(), self.get_end(), path_arc=self.path_arc
-# )
-# return self
-#
-# def get_start(self):
-# nppc = self.n_points_per_curve
-# points = self.get_points()
-# return (points[0] + points[-nppc]) / 2
-#
-# def get_end(self):
-# return self.get_points()[self.tip_index]
-#
-# def put_start_and_end_on(self, start, end):
-# self.set_points_by_ends(start, end, buff=0, path_arc=self.path_arc)
-# return self
-#
-# def scale(self, *args, **kwargs):
-# super().scale(*args, **kwargs)
-# self.reset_points_around_ends()
-# return self
-#
-# def set_thickness(self, thickness):
-# self.thickness = thickness
-# self.reset_points_around_ends()
-# return self
-#
-# def set_path_arc(self, path_arc):
-# self.path_arc = path_arc
-# self.reset_points_around_ends()
-# return self
-#
-#
-# class Vector(Arrow):
-# CONFIG = {
-# "buff": 0,
-# }
-#
-# def __init__(self, direction=RIGHT, **kwargs):
-# if len(direction) == 2:
-# direction = np.hstack([direction, 0])
-# super().__init__(ORIGIN, direction, **kwargs)
-#
-#
-# class DoubleArrow(Arrow):
-# def __init__(self, *args, **kwargs):
-# Arrow.__init__(self, *args, **kwargs)
-# self.add_tip(at_start=True)
-#
-#
-# class CubicBezier(VMobject):
-# def __init__(self, a0, h0, h1, a1, **kwargs):
-# VMobject.__init__(self, **kwargs)
-# self.add_cubic_bezier_curve(a0, h0, h1, a1)
-#
-#
+class OpenGLTangentLine(OpenGLLine):
+ def __init__(self, vmob, alpha, length=1, d_alpha=1e-6, **kwargs):
+ self.length = length
+ self.d_alpha = d_alpha
+ da = self.d_alpha
+ a1 = clip(alpha - da, 0, 1)
+ a2 = clip(alpha + da, 0, 1)
+ super().__init__(vmob.pfp(a1), vmob.pfp(a2), **kwargs)
+ self.scale(self.length / self.get_length())
+
+
+class OpenGLElbow(OpenGLVMobject):
+ def __init__(self, width=0.2, angle=0, **kwargs):
+ self.angle = angle
+ super().__init__(self, **kwargs)
+ self.set_points_as_corners([UP, UP + RIGHT, RIGHT])
+ self.set_width(width, about_point=ORIGIN)
+ self.rotate(self.angle, about_point=ORIGIN)
+
+
+class OpenGLArrow(OpenGLLine):
+ def __init__(
+ self,
+ start=LEFT,
+ end=RIGHT,
+ path_arc=0,
+ fill_color=GREY_A,
+ fill_opacity=1,
+ stroke_width=0,
+ buff=MED_SMALL_BUFF,
+ thickness=0.05,
+ tip_width_ratio=5,
+ tip_angle=PI / 3,
+ max_tip_length_to_length_ratio=0.5,
+ max_width_to_length_ratio=0.1,
+ **kwargs
+ ):
+ self.thickness = thickness
+ self.tip_width_ratio = tip_width_ratio
+ self.tip_angle = tip_angle
+ self.max_tip_length_to_length_ratio = max_tip_length_to_length_ratio
+ self.max_width_to_length_ratio = max_width_to_length_ratio
+ super().__init__(
+ start=start,
+ end=end,
+ buff=buff,
+ path_arc=path_arc,
+ fill_color=fill_color,
+ fill_opacity=fill_opacity,
+ stroke_width=stroke_width,
+ **kwargs
+ )
+
+ def set_points_by_ends(self, start, end, buff=0, path_arc=0):
+ # Find the right tip length and thickness
+ vect = end - start
+ length = max(get_norm(vect), 1e-8)
+ thickness = self.thickness
+ w_ratio = fdiv(self.max_width_to_length_ratio, fdiv(thickness, length))
+ if w_ratio < 1:
+ thickness *= w_ratio
+
+ tip_width = self.tip_width_ratio * thickness
+ tip_length = tip_width / (2 * np.tan(self.tip_angle / 2))
+ t_ratio = fdiv(self.max_tip_length_to_length_ratio, fdiv(tip_length, length))
+ if t_ratio < 1:
+ tip_length *= t_ratio
+ tip_width *= t_ratio
+
+ # Find points for the stem
+ if path_arc == 0:
+ points1 = (length - tip_length) * np.array([RIGHT, 0.5 * RIGHT, ORIGIN])
+ points1 += thickness * UP / 2
+ points2 = points1[::-1] + thickness * DOWN
+ else:
+ # Solve for radius so that the tip-to-tail length matches |end - start|
+ a = 2 * (1 - np.cos(path_arc))
+ b = -2 * tip_length * np.sin(path_arc)
+ c = tip_length ** 2 - length ** 2
+ R = (-b + np.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
+
+ # Find arc points
+ points1 = OpenGLArc.create_quadratic_bezier_points(path_arc)
+ points2 = np.array(points1[::-1])
+ points1 *= R + thickness / 2
+ points2 *= R - thickness / 2
+ if path_arc < 0:
+ tip_length *= -1
+ rot_T = rotation_matrix_transpose(PI / 2 - path_arc, OUT)
+ for points in points1, points2:
+ points[:] = np.dot(points, rot_T)
+ points += R * DOWN
+
+ self.set_points(points1)
+ # Tip
+ self.add_line_to(tip_width * UP / 2)
+ self.add_line_to(tip_length * LEFT)
+ self.tip_index = len(self.get_points()) - 1
+ self.add_line_to(tip_width * DOWN / 2)
+ self.add_line_to(points2[0])
+ # Close it out
+ self.append_points(points2)
+ self.add_line_to(points1[0])
+
+ if length > 0:
+ # Final correction
+ super().scale(length / self.get_length())
+
+ self.rotate(angle_of_vector(vect) - self.get_angle())
+ self.rotate(
+ PI / 2 - np.arccos(normalize(vect)[2]),
+ axis=rotate_vector(self.get_unit_vector(), -PI / 2),
+ )
+ self.shift(start - self.get_start())
+ self.refresh_triangulation()
+
+ def reset_points_around_ends(self):
+ self.set_points_by_ends(
+ self.get_start(), self.get_end(), path_arc=self.path_arc
+ )
+ return self
+
+ def get_start(self):
+ nppc = self.n_points_per_curve
+ points = self.get_points()
+ return (points[0] + points[-nppc]) / 2
+
+ def get_end(self):
+ return self.get_points()[self.tip_index]
+
+ def put_start_and_end_on(self, start, end):
+ self.set_points_by_ends(start, end, buff=0, path_arc=self.path_arc)
+ return self
+
+ def scale(self, *args, **kwargs):
+ super().scale(*args, **kwargs)
+ self.reset_points_around_ends()
+ return self
+
+ def set_thickness(self, thickness):
+ self.thickness = thickness
+ self.reset_points_around_ends()
+ return self
+
+ def set_path_arc(self, path_arc):
+ self.path_arc = path_arc
+ self.reset_points_around_ends()
+ return self
+
+
+class OpenGLVector(OpenGLArrow):
+ def __init__(self, direction=RIGHT, buff=0, **kwargs):
+ self.buff = buff
+ if len(direction) == 2:
+ direction = np.hstack([direction, 0])
+ super().__init__(ORIGIN, direction, buff=buff, **kwargs)
+
+
+class OpenGLDoubleArrow(OpenGLArrow):
+ def __init__(self, *args, **kwargs):
+ OpenGLArrow.__init__(self, *args, **kwargs)
+ self.add_tip(at_start=True)
+
+
+class OpenGLCubicBezier(OpenGLVMobject):
+ def __init__(self, a0, h0, h1, a1, **kwargs):
+ OpenGLVMobject.__init__(self, **kwargs)
+ self.add_cubic_bezier_curve(a0, h0, h1, a1)
+
+
class OpenGLPolygon(OpenGLVMobject):
def __init__(self, *vertices, **kwargs):
self.vertices = vertices
diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.py
index 921b600a5..80b53fb34 100644
--- a/manim/mobject/types/opengl_vectorized_mobject.py
+++ b/manim/mobject/types/opengl_vectorized_mobject.py
@@ -12,12 +12,14 @@ from ...utils.color import *
# from manimlib.mobject.mobject import Mobject
# from manimlib.mobject.mobject import Point
from ...utils.bezier import bezier
+
# from manimlib.utils.bezier import get_smooth_quadratic_bezier_handle_points
# from manimlib.utils.bezier import get_smooth_cubic_bezier_handle_points
-# from manimlib.utils.bezier import get_quadratic_approximation_of_cubic
+from ...utils.bezier import get_quadratic_approximation_of_cubic
from ...utils.bezier import interpolate
from ...utils.bezier import integer_interpolate
from ...utils.bezier import partial_quadratic_bezier_points
+
# from manimlib.utils.color import rgb_to_hex
from ...utils.iterables import make_even
@@ -32,10 +34,32 @@ from ...utils.space_ops import get_norm
from ...utils.space_ops import get_unit_normal
from ...utils.space_ops import z_to_vector
-# from manimlib.shader_wrapper import ShaderWrapper
+from ...renderer.shader_wrapper import ShaderWrapper
+
+JOINT_TYPE_MAP = {
+ "auto": 0,
+ "round": 1,
+ "bevel": 2,
+ "miter": 3,
+}
class OpenGLVMobject(OpenGLMobject):
+ fill_dtype = [
+ ("point", np.float32, (3,)),
+ ("unit_normal", np.float32, (3,)),
+ ("color", np.float32, (4,)),
+ ("vert_index", np.float32, (1,)),
+ ]
+ stroke_dtype = [
+ ("point", np.float32, (3,)),
+ ("prev_point", np.float32, (3,)),
+ ("next_point", np.float32, (3,)),
+ ("unit_normal", np.float32, (3,)),
+ ("stroke_width", np.float32, (1,)),
+ ("color", np.float32, (4,)),
+ ]
+
def __init__(
self,
fill_color=None,
@@ -916,17 +940,23 @@ class OpenGLVMobject(OpenGLMobject):
return self
def get_fill_shader_wrapper(self):
- self.fill_shader_wrapper.vert_data = self.get_fill_shader_data()
- self.fill_shader_wrapper.vert_indices = self.get_fill_shader_vert_indices()
- self.fill_shader_wrapper.uniforms = self.get_shader_uniforms()
- self.fill_shader_wrapper.depth_test = self.depth_test
- return self.fill_shader_wrapper
+ return ShaderWrapper(
+ vert_data=self.get_fill_shader_data(),
+ vert_indices=self.get_triangulation(),
+ shader_folder="quadratic_bezier_fill",
+ render_primitive=moderngl.TRIANGLES,
+ uniforms=self.get_fill_uniforms(),
+ depth_test=self.depth_test,
+ )
def get_stroke_shader_wrapper(self):
- self.stroke_shader_wrapper.vert_data = self.get_stroke_shader_data()
- self.stroke_shader_wrapper.uniforms = self.get_stroke_uniforms()
- self.stroke_shader_wrapper.depth_test = self.depth_test
- return self.stroke_shader_wrapper
+ return ShaderWrapper(
+ vert_data=self.get_stroke_shader_data(),
+ shader_folder="quadratic_bezier_stroke",
+ render_primitive=moderngl.TRIANGLES,
+ uniforms=self.get_stroke_uniforms(),
+ depth_test=self.depth_test,
+ )
def get_shader_wrapper_list(self):
# Build up data lists
@@ -963,36 +993,40 @@ class OpenGLVMobject(OpenGLMobject):
result["flat_stroke"] = float(self.flat_stroke)
return result
+ def get_fill_uniforms(self):
+ return dict(
+ is_fixed_in_frame=float(self.is_fixed_in_frame),
+ gloss=self.gloss,
+ shadow=self.shadow,
+ )
+
def get_stroke_shader_data(self):
- points = self.get_points()
- if len(self.stroke_data) != len(points):
- self.stroke_data = resize_array(self.stroke_data, len(points))
+ points = self.data["points"]
+ stroke_data = np.zeros(len(points), dtype=OpenGLVMobject.stroke_dtype)
- if "points" not in self.locked_data_keys:
- nppc = self.n_points_per_curve
- self.stroke_data["point"] = points
- self.stroke_data["prev_point"][:nppc] = points[-nppc:]
- self.stroke_data["prev_point"][nppc:] = points[:-nppc]
- self.stroke_data["next_point"][:-nppc] = points[nppc:]
- self.stroke_data["next_point"][-nppc:] = points[:nppc]
+ nppc = self.n_points_per_curve
+ stroke_data["point"] = points
+ stroke_data["prev_point"][:nppc] = points[-nppc:]
+ stroke_data["prev_point"][nppc:] = points[:-nppc]
+ stroke_data["next_point"][:-nppc] = points[nppc:]
+ stroke_data["next_point"][-nppc:] = points[:nppc]
- self.read_data_to_shader(self.stroke_data, "color", "stroke_rgba")
- self.read_data_to_shader(self.stroke_data, "stroke_width", "stroke_width")
- self.read_data_to_shader(self.stroke_data, "unit_normal", "unit_normal")
+ self.read_data_to_shader(stroke_data, "color", "stroke_rgba")
+ self.read_data_to_shader(stroke_data, "stroke_width", "stroke_width")
+ self.read_data_to_shader(stroke_data, "unit_normal", "unit_normal")
- return self.stroke_data
+ return stroke_data
def get_fill_shader_data(self):
- points = self.get_points()
- if len(self.fill_data) != len(points):
- self.fill_data = resize_array(self.fill_data, len(points))
- self.fill_data["vert_index"][:, 0] = range(len(points))
+ points = self.data["points"]
+ fill_data = np.zeros(len(points), dtype=OpenGLVMobject.fill_dtype)
+ fill_data["vert_index"][:, 0] = range(len(points))
- self.read_data_to_shader(self.fill_data, "point", "points")
- self.read_data_to_shader(self.fill_data, "color", "fill_rgba")
- self.read_data_to_shader(self.fill_data, "unit_normal", "unit_normal")
+ self.read_data_to_shader(fill_data, "point", "points")
+ self.read_data_to_shader(fill_data, "color", "fill_rgba")
+ self.read_data_to_shader(fill_data, "unit_normal", "unit_normal")
- return self.fill_data
+ return fill_data
def refresh_shader_data(self):
self.get_fill_shader_data()
@@ -1025,10 +1059,8 @@ class OpenGLVectorizedPoint(OpenGLPoint, OpenGLVMobject):
self.artificial_height = artificial_height
super().__init__(
- color=color,
- fill_opacity=fill_opacity,
- stroke_width=stroke_width,
- **kwargs)
+ color=color, fill_opacity=fill_opacity, stroke_width=stroke_width, **kwargs
+ )
self.set_points(np.array([location]))
diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.py
index 097a7d46e..adfd96992 100644
--- a/manim/renderer/opengl_renderer.py
+++ b/manim/renderer/opengl_renderer.py
@@ -55,7 +55,8 @@ class OpenGLRenderer:
self.context.disable(moderngl.DEPTH_TEST)
def render_mobject(self, mob):
- shader_wrapper_list = self.get_shader_wrapper_list(mob)
+ # shader_wrapper_list = self.get_shader_wrapper_list(mob)
+ shader_wrapper_list = mob.get_shader_wrapper_list()
render_group_list = map(
lambda x: self.get_render_group(self.context, x), shader_wrapper_list
)
diff --git a/manim/utils/bezier.py b/manim/utils/bezier.py
index 521a28e26..8a310c234 100644
--- a/manim/utils/bezier.py
+++ b/manim/utils/bezier.py
@@ -21,6 +21,8 @@ import numpy as np
from scipy import linalg
from ..utils.simple_functions import choose
+from ..utils.space_ops import cross2d
+from ..utils.space_ops import find_intersection
CLOSED_THRESHOLD: float = 0.001
@@ -235,5 +237,75 @@ def diag_to_matrix(l_and_u: typing.Tuple[int, int], diag: np.ndarray) -> np.ndar
return matrix
+# Given 4 control points for a cubic bezier curve (or arrays of such)
+# return control points for 2 quadratics (or 2n quadratics) approximating them.
+def get_quadratic_approximation_of_cubic(a0, h0, h1, a1):
+ a0 = np.array(a0, ndmin=2)
+ h0 = np.array(h0, ndmin=2)
+ h1 = np.array(h1, ndmin=2)
+ a1 = np.array(a1, ndmin=2)
+ # Tangent vectors at the start and end.
+ T0 = h0 - a0
+ T1 = a1 - h1
+
+ # Search for inflection points. If none are found, use the
+ # midpoint as a cut point.
+ # Based on http://www.caffeineowl.com/graphics/2d/vectorial/cubic-inflexion.html
+ has_infl = np.ones(len(a0), dtype=bool)
+
+ p = h0 - a0
+ q = h1 - 2 * h0 + a0
+ r = a1 - 3 * h1 + 3 * h0 - a0
+
+ a = cross2d(q, r)
+ b = cross2d(p, r)
+ c = cross2d(p, q)
+
+ disc = b * b - 4 * a * c
+ has_infl &= disc > 0
+ sqrt_disc = np.sqrt(np.abs(disc))
+ settings = np.seterr(all="ignore")
+ ti_bounds = []
+ for sgn in [-1, +1]:
+ ti = (-b + sgn * sqrt_disc) / (2 * a)
+ ti[a == 0] = (-c / b)[a == 0]
+ ti[(a == 0) & (b == 0)] = 0
+ ti_bounds.append(ti)
+ ti_min, ti_max = ti_bounds
+ np.seterr(**settings)
+ ti_min_in_range = has_infl & (0 < ti_min) & (ti_min < 1)
+ ti_max_in_range = has_infl & (0 < ti_max) & (ti_max < 1)
+
+ # Choose a value of t which starts at 0.5,
+ # but is updated to one of the inflection points
+ # if they lie between 0 and 1
+
+ t_mid = 0.5 * np.ones(len(a0))
+ t_mid[ti_min_in_range] = ti_min[ti_min_in_range]
+ t_mid[ti_max_in_range] = ti_max[ti_max_in_range]
+
+ m, n = a0.shape
+ t_mid = t_mid.repeat(n).reshape((m, n))
+
+ # Compute bezier point and tangent at the chosen value of t
+ mid = bezier([a0, h0, h1, a1])(t_mid)
+ Tm = bezier([h0 - a0, h1 - h0, a1 - h1])(t_mid)
+
+ # Intersection between tangent lines at end points
+ # and tangent in the middle
+ i0 = find_intersection(a0, T0, mid, Tm)
+ i1 = find_intersection(a1, T1, mid, Tm)
+
+ m, n = np.shape(a0)
+ result = np.zeros((6 * m, n))
+ result[0::6] = a0
+ result[1::6] = i0
+ result[2::6] = mid
+ result[3::6] = mid
+ result[4::6] = i1
+ result[5::6] = a1
+ return result
+
+
def is_closed(points: typing.Tuple[np.ndarray, np.ndarray]) -> bool:
return np.allclose(points[0], points[-1])
diff --git a/manim/utils/color.py b/manim/utils/color.py
index 6a999dcde..f1065ed77 100644
--- a/manim/utils/color.py
+++ b/manim/utils/color.py
@@ -77,6 +77,11 @@ __all__ = [
"DARK_GRAY",
"DARKER_GREY",
"DARKER_GRAY",
+ "GREY_E",
+ "GREY_D",
+ "GREY_C",
+ "GREY_B",
+ "GREY_A",
"GREY_BROWN",
"PINK",
"LIGHT_PINK",
@@ -183,6 +188,11 @@ class Colors(Enum):
dark_gray = "#444444"
darker_grey = "#222222"
darker_gray = "#222222"
+ grey_e = "#222222"
+ grey_d = "#444444"
+ grey_c = "#888888"
+ grey_b = "#BBBBBB"
+ grey_a = "#DDDDDD"
grey_brown = "#736357"
pink = "#D147BD"
light_pink = "#DC75CD"
@@ -252,6 +262,11 @@ DARK_GRAY = Colors.dark_gray.value
DARKER_GREY = Colors.darker_gray.value
DARKER_GRAY = Colors.darker_gray.value
GREY_BROWN = Colors.grey_brown.value
+GREY_E = Colors.grey_e.value
+GREY_D = Colors.grey_d.value
+GREY_C = Colors.grey_c.value
+GREY_B = Colors.grey_b.value
+GREY_A = Colors.grey_a.value
PINK = Colors.pink.value
LIGHT_PINK = Colors.light_pink.value
GREEN_SCREEN = Colors.green_screen.value
diff --git a/manim/utils/simple_functions.py b/manim/utils/simple_functions.py
index 0e9c68874..79a1f122b 100644
--- a/manim/utils/simple_functions.py
+++ b/manim/utils/simple_functions.py
@@ -60,6 +60,14 @@ def get_parameters(function):
# but for now, we just allow the option to handle indeterminate 0/0.
+def clip(a, min_a, max_a):
+ if a < min_a:
+ return min_a
+ elif a > max_a:
+ return max_a
+ return a
+
+
def clip_in_place(array, min_val=None, max_val=None):
if max_val is not None:
array[array > max_val] = max_val
diff --git a/manim/utils/space_ops.py b/manim/utils/space_ops.py
index 18f8306f9..315c2677f 100644
--- a/manim/utils/space_ops.py
+++ b/manim/utils/space_ops.py
@@ -42,6 +42,7 @@ from ..constants import OUT
from ..constants import PI
from ..constants import RIGHT
from ..constants import TAU
+from ..constants import DOWN
from ..utils.iterables import adjacent_pairs
from ..utils.simple_functions import fdiv
import itertools as it
@@ -60,17 +61,35 @@ def norm_squared(v):
# TODO, implement quaternion type
-def quaternion_mult(q1, q2):
- w1, x1, y1, z1 = q1
- w2, x2, y2, z2 = q2
- return np.array(
- [
- w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2,
- w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2,
- w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2,
- w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2,
- ]
- )
+def quaternion_mult(*quats):
+ if config["use_opengl_renderer"]:
+ if len(quats) == 0:
+ return [1, 0, 0, 0]
+ result = quats[0]
+ for next_quat in quats[1:]:
+ w1, x1, y1, z1 = result
+ w2, x2, y2, z2 = next_quat
+ result = [
+ w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2,
+ w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2,
+ w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2,
+ w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2,
+ ]
+ return result
+ else:
+ q1 = quats[0]
+ q2 = quats[1]
+
+ w1, x1, y1, z1 = q1
+ w2, x2, y2, z2 = q2
+ return np.array(
+ [
+ w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2,
+ w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2,
+ w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2,
+ w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2,
+ ]
+ )
def quaternion_from_angle_axis(angle, axis):
@@ -112,6 +131,22 @@ def thick_diagonal(dim, thickness=2):
return (np.abs(row_indices - col_indices) < thickness).astype("uint8")
+def rotation_matrix_transpose_from_quaternion(quat):
+ quat_inv = quaternion_conjugate(quat)
+ return [
+ quaternion_mult(quat, [0, *basis], quat_inv)[1:]
+ for basis in [
+ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1],
+ ]
+ ]
+
+
+def rotation_matrix_from_quaternion(quat):
+ return np.transpose(rotation_matrix_transpose_from_quaternion(quat))
+
+
def rotation_matrix_transpose(angle, axis):
if axis[0] == 0 and axis[1] == 0:
# axis = [0, 0, z] case is common enough it's worth