Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Add 3D surfaces and solids

apoogdk08586be1e923
diff --git a/rust/bridge/crates/manimgx-core/src/geometry/three_d/bezier.rs b/rust/bridge/crates/manimgx-core/src/geometry/three_d/bezier.rs
new file mode 100644
index 00000000..1499ca5d
--- /dev/null
+++ b/rust/bridge/crates/manimgx-core/src/geometry/three_d/bezier.rs
@@ -0,0 +1,80 @@
+use crate::vertex::Vertex;
+use glam::Vec3;
+
+pub struct BezierCurve;
+
+impl BezierCurve {
+ /// De Casteljau evaluation of a cubic Bezier at parameter t
+ fn evaluate(points: &[Vec3; 4], t: f32) -> Vec3 {
+ let p01 = points[0].lerp(points[1], t);
+ let p12 = points[1].lerp(points[2], t);
+ let p23 = points[2].lerp(points[3], t);
+ let p012 = p01.lerp(p12, t);
+ let p123 = p12.lerp(p23, t);
+ p012.lerp(p123, t)
+ }
+
+ /// Tangent of a cubic Bezier at parameter t
+ fn tangent(points: &[Vec3; 4], t: f32) -> Vec3 {
+ let p01 = points[0].lerp(points[1], t);
+ let p12 = points[1].lerp(points[2], t);
+ let p23 = points[2].lerp(points[3], t);
+ let p012 = p01.lerp(p12, t);
+ let p123 = p12.lerp(p23, t);
+ (p123 - p012).normalize_or_zero()
+ }
+
+ /// Tessellate a cubic Bezier into a tube mesh
+ pub fn generate_tube(
+ control_points: &[[f32; 3]; 4],
+ color: [f32; 4],
+ width: f32,
+ segments: u32,
+ radial_segments: u32,
+ ) -> (Vec<Vertex>, Vec<u32>) {
+ let points = [
+ Vec3::from_array(control_points[0]),
+ Vec3::from_array(control_points[1]),
+ Vec3::from_array(control_points[2]),
+ Vec3::from_array(control_points[3]),
+ ];
+
+ let radius = width / 2.0;
+ let mut vertices = Vec::new();
+ let mut indices = Vec::new();
+
+ for i in 0..=segments {
+ let t = i as f32 / segments as f32;
+ let center = Self::evaluate(&points, t);
+ let forward = Self::tangent(&points, t);
+
+ // Build a frame around the curve
+ let up = if forward.dot(Vec3::Y).abs() > 0.99 {
+ Vec3::Z
+ } else {
+ Vec3::Y
+ };
+ let right = forward.cross(up).normalize();
+ let actual_up = right.cross(forward).normalize();
+
+ for j in 0..=radial_segments {
+ let angle = j as f32 * 2.0 * std::f32::consts::PI / radial_segments as f32;
+ let normal = right * angle.cos() + actual_up * angle.sin();
+ let pos = center + normal * radius;
+ vertices.push(Vertex::new(pos.to_array(), normal.to_array(), color));
+ }
+ }
+
+ for i in 0..segments {
+ for j in 0..radial_segments {
+ let current = i * (radial_segments + 1) + j;
+ let next = current + radial_segments + 1;
+ indices.extend_from_slice(&[current, next, current + 1]);
+ indices.extend_from_slice(&[current + 1, next, next + 1]);
+ }
+ }
+
+ (vertices, indices)
+ }
+
+}
diff --git a/rust/bridge/crates/manimgx-core/src/geometry/three_d/mesh.rs b/rust/bridge/crates/manimgx-core/src/geometry/three_d/mesh.rs
new file mode 100644
index 00000000..43595eed
--- /dev/null
+++ b/rust/bridge/crates/manimgx-core/src/geometry/three_d/mesh.rs
@@ -0,0 +1,56 @@
+use crate::vertex::Vertex;
+
+pub struct SurfaceMesh;
+
+impl SurfaceMesh {
+ /// Generate a mesh from a function f(x, z) -> y
+ /// over the domain [x_min, x_max] x [z_min, z_max]
+ pub fn from_function<F>(
+ f: F,
+ x_range: [f32; 2],
+ z_range: [f32; 2],
+ resolution: u32,
+ color: [f32; 4],
+ ) -> (Vec<Vertex>, Vec<u32>)
+ where
+ F: Fn(f32, f32) -> f32,
+ {
+ let n_verts = ((resolution + 1) * (resolution + 1)) as usize;
+ let n_indices = (resolution * resolution * 6) as usize;
+ let mut vertices = Vec::with_capacity(n_verts);
+ let mut indices = Vec::with_capacity(n_indices);
+
+ let dx = (x_range[1] - x_range[0]) / resolution as f32;
+ let dz = (z_range[1] - z_range[0]) / resolution as f32;
+
+ for zi in 0..=resolution {
+ for xi in 0..=resolution {
+ let x = x_range[0] + xi as f32 * dx;
+ let z = z_range[0] + zi as f32 * dz;
+ let y = f(x, z);
+
+ // Approximate normal via central differences
+ let eps = 0.001;
+ let dydx = (f(x + eps, z) - f(x - eps, z)) / (2.0 * eps);
+ let dydz = (f(x, z + eps) - f(x, z - eps)) / (2.0 * eps);
+ let normal = [-dydx, 1.0, -dydz];
+ let len =
+ (normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]).sqrt();
+ let normal = [normal[0] / len, normal[1] / len, normal[2] / len];
+
+ vertices.push(Vertex::new([x, y, z], normal, color));
+ }
+ }
+
+ for zi in 0..resolution {
+ for xi in 0..resolution {
+ let current = zi * (resolution + 1) + xi;
+ let next_row = current + resolution + 1;
+ indices.extend_from_slice(&[current, next_row, current + 1]);
+ indices.extend_from_slice(&[current + 1, next_row, next_row + 1]);
+ }
+ }
+
+ (vertices, indices)
+ }
+}
diff --git a/rust/bridge/crates/manimgx-core/src/geometry/three_d/mod.rs b/rust/bridge/crates/manimgx-core/src/geometry/three_d/mod.rs
new file mode 100644
index 00000000..e8ba253f
--- /dev/null
+++ b/rust/bridge/crates/manimgx-core/src/geometry/three_d/mod.rs
@@ -0,0 +1,3 @@
+pub mod bezier;
+pub mod mesh;
+pub mod primitives;
diff --git a/rust/bridge/crates/manimgx-core/src/geometry/three_d/primitives.rs b/rust/bridge/crates/manimgx-core/src/geometry/three_d/primitives.rs
new file mode 100644
index 00000000..2adcd45f
--- /dev/null
+++ b/rust/bridge/crates/manimgx-core/src/geometry/three_d/primitives.rs
@@ -0,0 +1,273 @@
+use crate::vertex::Vertex;
+use std::f32::consts::PI;
+
+pub struct Cube;
+pub struct Box3D;
+pub struct Sphere;
+pub struct Cylinder;
+pub struct Plane;
+pub struct ParametricSurface;
+
+impl Cube {
+ pub fn generate(size: f32, color: [f32; 4]) -> (Vec<Vertex>, Vec<u32>) {
+ let h = size / 2.0;
+
+ #[rustfmt::skip]
+ let face_data: &[([f32; 3], [[f32; 3]; 4])] = &[
+ // normal, 4 corner positions
+ ([0.0, 0.0, 1.0], [[-h, -h, h], [h, -h, h], [h, h, h], [-h, h, h]]), // front
+ ([0.0, 0.0, -1.0], [[h, -h, -h], [-h, -h, -h], [-h, h, -h], [h, h, -h]]), // back
+ ([0.0, 1.0, 0.0], [[-h, h, h], [h, h, h], [h, h, -h], [-h, h, -h]]), // top
+ ([0.0, -1.0, 0.0], [[-h, -h, -h], [h, -h, -h], [h, -h, h], [-h, -h, h]]), // bottom
+ ([1.0, 0.0, 0.0], [[h, -h, h], [h, -h, -h], [h, h, -h], [h, h, h]]), // right
+ ([-1.0, 0.0, 0.0], [[-h, -h, -h], [-h, -h, h], [-h, h, h], [-h, h, -h]]), // left
+ ];
+
+ let mut vertices = Vec::with_capacity(24);
+ let mut indices = Vec::with_capacity(36);
+
+ for (normal, positions) in face_data {
+ let base = vertices.len() as u32;
+ for pos in positions {
+ vertices.push(Vertex::new(*pos, *normal, color));
+ }
+ indices.extend_from_slice(&[base, base + 1, base + 2, base, base + 2, base + 3]);
+ }
+
+ (vertices, indices)
+ }
+}
+
+impl Box3D {
+ /// Generate a rectangular prism (box) with independent width, height, depth.
+ /// Width = X axis, Height = Y axis, Depth = Z axis.
+ pub fn generate(width: f32, height: f32, depth: f32, color: [f32; 4]) -> (Vec<Vertex>, Vec<u32>) {
+ let hw = width / 2.0;
+ let hh = height / 2.0;
+ let hd = depth / 2.0;
+
+ #[rustfmt::skip]
+ let face_data: &[([f32; 3], [[f32; 3]; 4])] = &[
+ ([0.0, 0.0, 1.0], [[-hw, -hh, hd], [hw, -hh, hd], [hw, hh, hd], [-hw, hh, hd]]), // front
+ ([0.0, 0.0, -1.0], [[hw, -hh, -hd], [-hw, -hh, -hd], [-hw, hh, -hd], [hw, hh, -hd]]), // back
+ ([0.0, 1.0, 0.0], [[-hw, hh, hd], [hw, hh, hd], [hw, hh, -hd], [-hw, hh, -hd]]), // top
+ ([0.0, -1.0, 0.0], [[-hw, -hh, -hd], [hw, -hh, -hd], [hw, -hh, hd], [-hw, -hh, hd]]), // bottom
+ ([1.0, 0.0, 0.0], [[hw, -hh, hd], [hw, -hh, -hd], [hw, hh, -hd], [hw, hh, hd]]), // right
+ ([-1.0, 0.0, 0.0], [[-hw, -hh, -hd], [-hw, -hh, hd], [-hw, hh, hd], [-hw, hh, -hd]]), // left
+ ];
+
+ let mut vertices = Vec::with_capacity(24);
+ let mut indices = Vec::with_capacity(36);
+
+ for (normal, positions) in face_data {
+ let base = vertices.len() as u32;
+ for pos in positions {
+ vertices.push(Vertex::new(*pos, *normal, color));
+ }
+ indices.extend_from_slice(&[base, base + 1, base + 2, base, base + 2, base + 3]);
+ }
+
+ (vertices, indices)
+ }
+}
+
+impl Sphere {
+ pub fn generate(
+ radius: f32,
+ color: [f32; 4],
+ segments: u32,
+ rings: u32,
+ ) -> (Vec<Vertex>, Vec<u32>) {
+ let mut vertices = Vec::new();
+ let mut indices = Vec::new();
+
+ for ring in 0..=rings {
+ let theta = ring as f32 * PI / rings as f32;
+ let sin_theta = theta.sin();
+ let cos_theta = theta.cos();
+
+ for seg in 0..=segments {
+ let phi = seg as f32 * 2.0 * PI / segments as f32;
+ let sin_phi = phi.sin();
+ let cos_phi = phi.cos();
+
+ let x = sin_theta * cos_phi;
+ let y = cos_theta;
+ let z = sin_theta * sin_phi;
+
+ vertices.push(Vertex::new(
+ [x * radius, y * radius, z * radius],
+ [x, y, z],
+ color,
+ ));
+ }
+ }
+
+ for ring in 0..rings {
+ for seg in 0..segments {
+ let current = ring * (segments + 1) + seg;
+ let next = current + segments + 1;
+
+ indices.extend_from_slice(&[current, next, current + 1]);
+ indices.extend_from_slice(&[current + 1, next, next + 1]);
+ }
+ }
+
+ (vertices, indices)
+ }
+}
+
+impl Cylinder {
+ pub fn generate(
+ radius: f32,
+ height: f32,
+ color: [f32; 4],
+ segments: u32,
+ ) -> (Vec<Vertex>, Vec<u32>) {
+ let mut vertices = Vec::new();
+ let mut indices = Vec::new();
+ let half_h = height / 2.0;
+
+ // Side vertices
+ for i in 0..=segments {
+ let angle = i as f32 * 2.0 * PI / segments as f32;
+ let x = angle.cos() * radius;
+ let z = angle.sin() * radius;
+ let nx = angle.cos();
+ let nz = angle.sin();
+
+ vertices.push(Vertex::new([x, -half_h, z], [nx, 0.0, nz], color));
+ vertices.push(Vertex::new([x, half_h, z], [nx, 0.0, nz], color));
+ }
+
+ // Side indices
+ for i in 0..segments {
+ let base = i * 2;
+ indices.extend_from_slice(&[base, base + 1, base + 2]);
+ indices.extend_from_slice(&[base + 2, base + 1, base + 3]);
+ }
+
+ // Top cap
+ let top_center = vertices.len() as u32;
+ vertices.push(Vertex::new([0.0, half_h, 0.0], [0.0, 1.0, 0.0], color));
+ for i in 0..=segments {
+ let angle = i as f32 * 2.0 * PI / segments as f32;
+ let x = angle.cos() * radius;
+ let z = angle.sin() * radius;
+ vertices.push(Vertex::new([x, half_h, z], [0.0, 1.0, 0.0], color));
+ }
+ for i in 0..segments {
+ indices.extend_from_slice(&[top_center, top_center + 1 + i, top_center + 2 + i]);
+ }
+
+ // Bottom cap
+ let bot_center = vertices.len() as u32;
+ vertices.push(Vertex::new([0.0, -half_h, 0.0], [0.0, -1.0, 0.0], color));
+ for i in 0..=segments {
+ let angle = i as f32 * 2.0 * PI / segments as f32;
+ let x = angle.cos() * radius;
+ let z = angle.sin() * radius;
+ vertices.push(Vertex::new([x, -half_h, z], [0.0, -1.0, 0.0], color));
+ }
+ for i in 0..segments {
+ indices.extend_from_slice(&[bot_center, bot_center + 2 + i, bot_center + 1 + i]);
+ }
+
+ (vertices, indices)
+ }
+}
+
+impl Plane {
+ pub fn generate(width: f32, depth: f32, color: [f32; 4]) -> (Vec<Vertex>, Vec<u32>) {
+ let hw = width / 2.0;
+ let hd = depth / 2.0;
+ let up = [0.0, 1.0, 0.0];
+
+ let vertices = vec![
+ Vertex::new([-hw, 0.0, -hd], up, color),
+ Vertex::new([hw, 0.0, -hd], up, color),
+ Vertex::new([hw, 0.0, hd], up, color),
+ Vertex::new([-hw, 0.0, hd], up, color),
+ ];
+ let indices = vec![0, 2, 1, 0, 3, 2];
+
+ (vertices, indices)
+ }
+}
+
+impl ParametricSurface {
+ /// Generate a parametric surface from pre-computed point grid.
+ /// `points` is a row-major grid of [x, y, z] positions, with dimensions `u_steps × v_steps`.
+ /// Normals are computed from cross products of partial derivatives.
+ pub fn generate(
+ points: &[[f32; 3]],
+ u_steps: u32,
+ v_steps: u32,
+ color: [f32; 4],
+ ) -> (Vec<Vertex>, Vec<u32>) {
+ let mut vertices = Vec::with_capacity((u_steps * v_steps) as usize);
+ let mut indices = Vec::new();
+
+ // Build vertices with computed normals
+ for vi in 0..v_steps {
+ for ui in 0..u_steps {
+ let idx = (vi * u_steps + ui) as usize;
+ let p = points[idx];
+
+ // Compute normal via finite differences
+ let du = if ui + 1 < u_steps {
+ let next = points[idx + 1];
+ [next[0] - p[0], next[1] - p[1], next[2] - p[2]]
+ } else if ui > 0 {
+ let prev = points[idx - 1];
+ [p[0] - prev[0], p[1] - prev[1], p[2] - prev[2]]
+ } else {
+ [1.0, 0.0, 0.0]
+ };
+
+ let dv = if vi + 1 < v_steps {
+ let next = points[(idx + u_steps as usize).min(points.len() - 1)];
+ [next[0] - p[0], next[1] - p[1], next[2] - p[2]]
+ } else if vi > 0 {
+ let prev = points[idx.saturating_sub(u_steps as usize)];
+ [p[0] - prev[0], p[1] - prev[1], p[2] - prev[2]]
+ } else {
+ [0.0, 0.0, 1.0]
+ };
+
+ // Cross product du × dv
+ let normal = [
+ du[1] * dv[2] - du[2] * dv[1],
+ du[2] * dv[0] - du[0] * dv[2],
+ du[0] * dv[1] - du[1] * dv[0],
+ ];
+ let len = (normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2])
+ .sqrt()
+ .max(1e-6);
+ let normal = [normal[0] / len, normal[1] / len, normal[2] / len];
+
+ vertices.push(Vertex::new(p, normal, color));
+ }
+ }
+
+ // Build triangle indices
+ for vi in 0..(v_steps - 1) {
+ for ui in 0..(u_steps - 1) {
+ let tl = vi * u_steps + ui;
+ let tr = tl + 1;
+ let bl = tl + u_steps;
+ let br = bl + 1;
+
+ indices.push(tl);
+ indices.push(bl);
+ indices.push(tr);
+
+ indices.push(tr);
+ indices.push(bl);
+ indices.push(br);
+ }
+ }
+
+ (vertices, indices)
+ }
+}
diff --git a/src/manimgx/mobjects/three_d.py b/src/manimgx/mobjects/three_d.py
new file mode 100644
index 00000000..de2571c0
--- /dev/null
+++ b/src/manimgx/mobjects/three_d.py
@@ -0,0 +1,1657 @@
+# SPDX-FileCopyrightText: 2026 Academa, Inc.
+# SPDX-FileCopyrightText: 2024 the Manim Community Developers
+# SPDX-FileCopyrightText: 2018 3Blue1Brown LLC
+# SPDX-License-Identifier: MIT
+
+"""Ported from Manim CE 0.21 (MIT)."""
+
+from __future__ import annotations
+
+import functools
+import warnings
+
+__all__ = [
+ "Arrow3D",
+ "Cone",
+ "ConvexHull3D",
+ "Cube",
+ "Cylinder",
+ "Dodecahedron",
+ "Dot3D",
+ "Icosahedron",
+ "Line3D",
+ "Octahedron",
+ "Polyhedron",
+ "Prism",
+ "Sphere",
+ "Surface",
+ "Tetrahedron",
+ "ThreeDVMobject",
+ "Torus",
+]
+from collections.abc import Callable, Hashable, Iterable, Sequence
+from typing import TYPE_CHECKING, ClassVar, Literal, Self, TypedDict, Unpack, cast
+
+import numpy as np
+
+from manimgx.caches import forgets
+from manimgx.constants import (
+ DEFAULT_DOT_RADIUS,
+ DOWN,
+ IN,
+ LEFT,
+ ORIGIN,
+ OUT,
+ PI,
+ RIGHT,
+ TAU,
+ UP,
+ Y_AXIS,
+ Z_AXIS,
+ LineJointType,
+)
+from manimgx.drawing.geometry import (
+ QuickHull,
+ normalize,
+ perpendicular_bisector,
+ rotation_about_z,
+ rotation_matrix,
+ sample,
+ z_to_vector,
+)
+from manimgx.drawing.paint import (
+ BLUE,
+ BLUE_D,
+ BLUE_E,
+ LIGHT_GREY,
+ WHITE,
+ Colorscale,
+ ManimColor,
+ ParsableManimColor,
+ Style,
+ rgbas_by_value,
+)
+from manimgx.mobject import MeshMobject, Mobject, VectorizedPoint, VGroup, VMobject
+from manimgx.mobjects.graph import Graph, GraphOptions
+from manimgx.mobjects.shapes import Circle, Polygon, Square
+
+if TYPE_CHECKING:
+ from manimgx.mobjects.plotting import ThreeDAxes
+ from manimgx.typing import (
+ Point3D,
+ Point3DLike,
+ Point3DLike_Array,
+ Vector3D,
+ Vector3DLike,
+ )
+
+
+class ThreeDVMobject(VMobject):
+ """A path shaded by a three-dimensional scene's light: a
+ [VMobject][manimgx.VMobject] with `shade_in_3d` on.
+
+ It takes a VMobject's arguments. Give it points in three dimensions (with
+ `set_points_as_corners`, `set_points_smoothly`, …) to draw a curve through space
+ that the light shades as it does the other three-dimensional objects.
+ """
+
+ # u_index … v2: a surface face's place in its (u, v) grid; nothing sets them (a
+ # Surface is one mesh, not faces of this class)
+ defaults: ClassVar[Style] = {"shade_in_3d": True}
+ u_index: int
+ v_index: int
+ u1: float
+ u2: float
+ v1: float
+ v2: float
+
+
+class SurfaceLook(Style, total=False):
+ """A [Surface][manimgx.Surface]'s keywords but its function, ranges and resolution,
+ for the shapes that set those (spheres, cylinders, …): its checkerboard, with the
+ style keywords."""
+
+ surface_piece_config: Style | None
+ """Accepted for Manim compatibility; ignored."""
+ checkerboard_colors: Iterable[ParsableManimColor] | Literal[False]
+ """The colors the faces are checkered with, in turn along both u and v (default
+ `BLUE_D` and `BLUE_E`, a cone's False; False: the fill color alone)."""
+ should_make_jagged: bool
+ """Accepted for Manim compatibility; ignored."""
+ pre_function_handle_to_anchor_scale_factor: float
+ """Accepted for Manim compatibility; ignored."""
+
+
+class SurfaceOptions(SurfaceLook, total=False):
+ """A [Surface][manimgx.Surface]'s keywords but its function and ranges, for the
+ shapes that pass them on ([Cone][manimgx.Cone]): its resolution and checkerboard,
+ with the style keywords."""
+
+ resolution: int | Sequence[int]
+ """How many faces the surface has along u and along v: one positive integer for
+ both, or a pair of positive integers `(u, v)` (default 32)."""
+
+
+_COLORMAP_TEXELS = 256 # a colorscale's samples along the value, as a texture
+
+
+@forgets
+def _checkerboard(
+ palette: tuple[tuple[float, float, float, float], ...],
+ u_count: int,
+ v_count: int,
+ opacity: float,
+) -> np.ndarray:
+ """A checkerboard's rows — one color per cell: a value of its palette,
+ grid and opacity."""
+ k = np.arange(u_count * v_count)
+ rows = np.array(palette)[(k // v_count + k % v_count) % len(palette)]
+ rows[:, 3] = opacity
+ rows.flags.writeable = False
+ return rows
+
+
+class Surface(MeshMobject):
+ """A parametric surface: the points `func(u, v)` for u and v over their ranges,
+ drawn as the smooth surface through them; checkered in two blues, with thin light
+ grey edges, and shaded by a three-dimensional scene's light, unless styled.
+
+ The ranges are divided into `resolution` steps, along u and along v, and the
+ function is sampled at the corners of the grid's cells, the surface's faces. What is
+ drawn is the smooth surface through those samples, not flat faces: `resolution` sets
+ how closely it follows the function, and the light shades it smoothly, across the
+ seam too where the surface closes on itself (a sphere, a torus, a cylinder). Each
+ face keeps its own colors, so a checkerboard keeps sharp cells, and the stroke draws
+ the faces' edges, curved with the surface. The faces are one mesh (a
+ [MeshMobject][manimgx.MeshMobject]): it morphs into other meshes (drawn as flat
+ faces while it morphs into one of another grid), and [Create][manimgx.Create] draws
+ it in face by face.
+ [set_fill_by_checkerboard][manimgx.Surface.set_fill_by_checkerboard] and
+ [set_fill_by_value][manimgx.Surface.set_fill_by_value] color it.
+
+ The checkerboard palette supplies the initial paint; use
+ [set_fill_by_checkerboard][manimgx.Surface.set_fill_by_checkerboard] to recolor
+ the surface later. The initial palette and ignored compatibility options are
+ not retained as instance attributes.
+
+ Args:
+ func: The function, from (u, v) to a point in scene coordinates (on axes, their
+ [c2p][manimgx.Axes.coords_to_point] of coordinates).
+ u_range: The range of u, `(u_min, u_max)`.
+ v_range: The range of v, `(v_min, v_max)`.
+ resolution: How many faces the surface has along u and along v: one positive
+ integer for both, or a pair of positive integers `(u, v)`.
+ surface_piece_config: Accepted for Manim compatibility; ignored.
+ checkerboard_colors: The initial checkerboard colors, in turn; False for the
+ fill color alone.
+ should_make_jagged: Accepted for Manim compatibility; ignored.
+ pre_function_handle_to_anchor_scale_factor: Accepted for Manim compatibility;
+ ignored.
+
+ Examples:
+ ```python
+ import numpy as np
+
+ import manimgx as m
+
+
+ class SurfaceExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=60 * m.DEGREES, theta=-60 * m.DEGREES)
+
+ def mobius(u: float, v: float) -> np.ndarray:
+ r = 2.5 + v * np.cos(u / 2)
+ return np.array([r * np.cos(u), r * np.sin(u), v * np.sin(u / 2)])
+
+ strip = m.Surface(
+ mobius, u_range=(0, m.TAU), v_range=(-1, 1), resolution=(48, 6)
+ )
+ self.add(strip)
+ ```
+ """
+
+ # one mesh, func(u, v) sampled over a grid of faces, drawn as the C² cubic spline
+ # through its samples (see `feed.refine_surface`: periodic along a direction whose
+ # ends meet, not-a-knot otherwise). Paint belongs to lattice cells; the renderer
+ # derives their independently colored blocks and edge loops. Unequal lattices
+ # align as explicit triangle soup.
+ defaults: ClassVar[Style] = {
+ "fill_color": BLUE_D,
+ "fill_opacity": 1.0,
+ "stroke_color": LIGHT_GREY,
+ "stroke_width": 0.5,
+ "stroke_opacity": 1.0,
+ "shade_in_3d": True,
+ }
+
+ def __init__(
+ self,
+ func: Callable[[float, float], np.ndarray],
+ u_range: Sequence[float] = (0, 1),
+ v_range: Sequence[float] = (0, 1),
+ resolution: int | Sequence[int] = 32,
+ surface_piece_config: Style | None = None,
+ checkerboard_colors: Iterable[ParsableManimColor] | Literal[False] = (
+ BLUE_D,
+ BLUE_E,
+ ),
+ should_make_jagged: bool = False,
+ pre_function_handle_to_anchor_scale_factor: float = 1e-05,
+ **kwargs: Unpack[Style],
+ ) -> None:
+ self.u_range = u_range
+ self.v_range = v_range
+ self.resolution = resolution
+ colors: list[ManimColor] | Literal[False] = (
+ False
+ if checkerboard_colors is False
+ else [ManimColor(c) for c in checkerboard_colors]
+ )
+ self._func = func
+ super().__init__(**kwargs)
+ if colors:
+ self.set_fill_by_checkerboard(*colors)
+
+ def func(self, u: float, v: float) -> np.ndarray:
+ """The surface's point at (u, v): its function's value.
+
+ Args:
+ u: The first parameter.
+ v: The second parameter.
+
+ Returns:
+ The point, in scene coordinates, as the surface was made (before it was
+ moved).
+ """
+ return self._func(u, v)
+
+ def generate_points(self) -> Self:
+ u_res, v_res = (
+ (self.resolution, self.resolution)
+ if isinstance(self.resolution, int)
+ else self.resolution
+ )
+ if u_res <= 0 or v_res <= 0:
+ raise ValueError("Surface resolution must be positive in both directions")
+ u = np.linspace(self.u_range[0], self.u_range[1], u_res + 1)
+ v = np.linspace(self.v_range[0], self.v_range[1], v_res + 1)
+ grid = sample(self.func, *np.meshgrid(u, v, indexing="ij"))
+ self.points = grid.reshape(-1, 3)
+ self._topology = (len(u) - 1, len(v) - 1)
+ self.uvs = None
+ return self
+
+ def face_indices(self) -> tuple[np.ndarray, np.ndarray]:
+ """The place of every face in the surface's grid: its index along u and its
+ index along v, in the mesh's order.
+
+ Returns:
+ Two arrays, the faces' indices along u and along v.
+ """
+ grid = self.grid
+ if grid is None:
+ raise ValueError("Checkerboard cells require lattice topology")
+ u, v = grid
+ k = np.arange(u * v)
+ return k // v, k % v
+
+ def _paint_faces(self, colors: np.ndarray, opacity: float | None) -> Self:
+ """One color per cell — (cells, 4) RGBA; opacity as given, else
+ as it was."""
+ rows = colors.copy()
+ rows[:, 3] = self._opacity(opacity, len(rows))
+ return self._fill_rows(rows)
+
+ def _fill_rows(self, rows: np.ndarray) -> Self:
+ """Colors as the surface's fill, in place of a colormap."""
+ self.paint = self.paint.but(fill=rows, texture=None, colormap=False)
+ return self
+
+ def _opacity(self, opacity: float | None, count: int) -> float | np.ndarray:
+ """The opacity faces are painted with: as given, else as it was (per cell, or one)."""
+ current = self.paint.fill
+ if opacity is not None:
+ return opacity
+ return current[:, 3] if len(current) == count else float(current[0, 3])
+
+ def set_fill_by_checkerboard(
+ self, *colors: ParsableManimColor, opacity: float | None = None
+ ) -> Self:
+ """Color the faces like a checkerboard: the colors in turn, along u and along v.
+
+ The face at place (i, j) of the grid takes the color i + j places along
+ `colors`, cycling: two colors make a checkerboard, more make diagonal stripes.
+ It replaces the colors [set_fill_by_value][manimgx.Surface.set_fill_by_value]
+ gave.
+
+ Args:
+ *colors: The colors, in turn.
+ opacity: The faces' opacity, from 0 to 1; None to keep theirs.
+ """
+ palette = tuple(tuple(ManimColor(c).to_rgba()) for c in colors)
+ grid = self.grid
+ if grid is None:
+ raise ValueError("Checkerboard cells require lattice topology")
+ alpha = self._opacity(opacity, grid[0] * grid[1])
+ # one opacity: the checkerboard is a value, made once
+ if isinstance(alpha, float):
+ return self._fill_rows(_checkerboard(palette, *grid, alpha))
+ i, j = self.face_indices()
+ return self._paint_faces(np.array(palette)[(i + j) % len(palette)], opacity)
+
+ def set_fill_by_value(
+ self,
+ axes: ThreeDAxes,
+ colorscale: Colorscale | None = None,
+ axis: int = 2,
+ colors: Colorscale | None = None,
+ ) -> Self:
+ """Color the surface point by point by a coordinate on axes: its height, z, by
+ default, on a colorscale.
+
+ Each sample of the surface has its coordinate along `axis`, and the values are
+ blended across the surface before the colorscale maps them: the colors follow
+ the surface smoothly, and a face whose corners straddle a color of the scale
+ shows it. The colors are the fill's, lit as a fill color is, at the fill's
+ opacity: a fill color set later ([set_fill][manimgx.Mobject.set_fill],
+ [set_color][manimgx.Mobject.set_color]) replaces them, as
+ [set_fill_by_checkerboard][manimgx.Surface.set_fill_by_checkerboard] does.
+ Without a colorscale, it warns and leaves the colors as they are.
+
+ Args:
+ axes: The axes whose coordinates color the surface.
+ colorscale: The colors: spread evenly over the axes' range along `axis`, or
+ `(color, value)` pairs, which span their own values; blended between,
+ and held beyond the ends.
+ axis: The coordinate: 0 for x, 1 for y, 2 for z.
+ colors: The colorscale by its older name, accepted for Manim compatibility:
+ used when `colorscale` is None.
+
+ Examples:
+ ```python
+ import numpy as np
+
+ import manimgx as m
+
+
+ class SurfaceSetFillByValueExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(
+ phi=65 * m.DEGREES, theta=-130 * m.DEGREES
+ )
+ axes = m.ThreeDAxes(
+ x_range=(0, 5),
+ y_range=(0, 5),
+ z_range=(-1, 1, 0.5),
+ x_length=7,
+ y_length=7,
+ z_length=3,
+ )
+ surface = m.Surface(
+ lambda u, v: axes.c2p(u, v, np.sin(u) * np.cos(v)),
+ u_range=(0, 5),
+ v_range=(0, 5),
+ resolution=8,
+ )
+ surface.set_fill_by_value(
+ axes, colorscale=[(m.RED, -0.5), (m.YELLOW, 0), (m.GREEN, 0.5)]
+ )
+ self.add(axes, surface)
+ ```
+ """
+ # the colorscale is a texture along the value (a colormap) and each sample's
+ # value its coordinate: values are interpolated over the surface before they are
+ # mapped
+ colorscale = colorscale if colorscale is not None else colors
+ if colorscale is None:
+ warnings.warn(
+ "The value passed to the colorscale keyword argument was None, the"
+ " surface fill color has not been changed",
+ stacklevel=2,
+ )
+ return self
+ low, high = (axes.x_range, axes.y_range, axes.z_range or (0, 0))[axis][:2]
+ if all(isinstance(c, (tuple, list)) and len(c) == 2 for c in colorscale):
+ pivots = [
+ float(cast("tuple[ParsableManimColor, float]", c)[1])
+ for c in colorscale
+ ]
+ low, high = min(pivots), max(pivots)
+ values = np.asarray(axes.point_to_coords(self.points), dtype=float)[:, axis]
+ span = high - low if high > low else 1.0
+ n = _COLORMAP_TEXELS
+ ramp = rgbas_by_value(colorscale, np.linspace(low, high, n), low, high)
+ colormap = (ramp[None] * 255).round().astype(np.uint8)
+ colormap[..., 3] = 255
+ colormap.flags.writeable = False
+ # texel centers: the ends hold beyond them
+ u = (0.5 + np.clip((values - low) / span, 0.0, 1.0) * (n - 1)) / n
+ self.uvs = np.stack([u, np.full_like(u, 0.5)], axis=1)
+ grid = self.grid
+ count = len(self.points) if grid is None else grid[0] * grid[1]
+ alpha = self._opacity(None, count)
+ fill = np.ones((1 if isinstance(alpha, float) else count, 4))
+ fill[:, 3] = alpha
+ self.paint = self.paint.but(fill=fill, texture=colormap, colormap=True)
+ return self
+
+
+def _revolve(
+ radius: float | np.ndarray,
+ height: float | np.ndarray,
+ angle: float | np.ndarray,
+) -> np.ndarray:
+ """Turn a radial profile about z, at one angle or a grid of angles."""
+ return np.array([radius * np.cos(angle), radius * np.sin(angle), height])
+
+
+class Sphere(Surface):
+ """A sphere: checkered in two blues and shaded by a three-dimensional scene's light,
+ unless styled.
+
+ It is a [Surface][manimgx.Surface] of u, the longitude, and v, the angle from the
+ south pole (the bottom, along z): their ranges may cut out a part of it.
+
+ Args:
+ center: Where its center goes.
+ radius: Its radius, in scene units.
+ resolution: How many faces it has around and from pole to pole: one number for
+ both, or `(u, v)`; None for (24, 12).
+ u_range: The range of the longitude, in radians: (0, τ) goes all the way
+ around.
+ v_range: The range of the angle from the south pole, in radians: (0, π) goes
+ from pole to pole.
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook]:
+ `checkerboard_colors`, and the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class SphereExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=-90 * m.DEGREES)
+ self.add(
+ m.Sphere(center=(-4, 0, 0), radius=1.5),
+ m.Sphere(radius=1.5, resolution=(12, 6)).set_color(m.GREEN),
+ m.Sphere(
+ center=(4, 0, 0),
+ radius=1.5,
+ v_range=(0, m.PI / 2),
+ checkerboard_colors=[m.RED, m.YELLOW],
+ ),
+ )
+ ```
+ """
+
+ def __init__(
+ self,
+ center: Point3DLike = ORIGIN,
+ radius: float = 1,
+ resolution: int | Sequence[int] | None = None,
+ u_range: Sequence[float] = (0, TAU),
+ v_range: Sequence[float] = (0, PI),
+ **kwargs: Unpack[SurfaceLook],
+ ) -> None:
+ self.radius = radius
+ super().__init__(
+ self.func,
+ resolution=resolution if resolution is not None else (24, 12),
+ u_range=u_range,
+ v_range=v_range,
+ **kwargs,
+ )
+ self.shift(center)
+
+ def func(self, u: float, v: float) -> Point3D:
+ """The sphere's point at a longitude and an angle from its south pole, as it is
+ made around the origin.
+
+ Args:
+ u: The longitude, in radians, counterclockwise from the x-axis.
+ v: The angle from the south pole, in radians: 0 at the bottom, π at the
+ top.
+
+ Returns:
+ The point, relative to the sphere's center.
+ """
+ return self.radius * _revolve(np.sin(v), -np.cos(v), u)
+
+
+class Dot3D(Sphere):
+ """A dot in three dimensions: a small sphere, white unless given a color.
+
+ Args:
+ point: Where its center goes.
+ radius: Its radius, in scene units.
+ resolution: How many faces it has around and from pole to pole: one number for
+ both, or `(u, v)`; None for (24, 12).
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook]:
+ `color` paints it all in one color; the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class Dot3DExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=-45 * m.DEGREES)
+ axes = m.ThreeDAxes(x_range=(-3, 3), y_range=(-3, 3), z_range=(-2, 2))
+ self.add(
+ axes,
+ m.Dot3D(axes.c2p(0, 0, 1.5), radius=0.2, color=m.RED),
+ m.Dot3D(axes.c2p(2, 0, 0), radius=0.2, color=m.BLUE),
+ m.Dot3D(axes.c2p(0, 2, 0), radius=0.2, color=m.YELLOW),
+ )
+ ```
+ """
+
+ def __init__(
+ self,
+ point: Point3DLike = ORIGIN,
+ radius: float = DEFAULT_DOT_RADIUS,
+ resolution: int | tuple[int, int] | None = (8, 8),
+ **kwargs: Unpack[SurfaceLook],
+ ) -> None:
+ color = kwargs.pop("color", None) or WHITE # one color, over the checkerboard
+ super().__init__(center=point, radius=radius, resolution=resolution, **kwargs)
+ self.set_color(color)
+
+
+class Cube(VGroup):
+ """A cube: six square faces, centered at the origin with its edges along the axes;
+ blue, three-quarters opaque, without edges and shaded by a three-dimensional
+ scene's light, unless styled.
+
+ Args:
+ side_length: The length of its edges, in scene units.
+ **kwargs: [Style keywords][manimgx.drawing.paint.Style] for its faces.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class CubeExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=-45 * m.DEGREES)
+ cubes = m.Group(
+ m.Cube(),
+ m.Cube(side_length=3, fill_color=m.TEAL, fill_opacity=1),
+ m.Cube(fill_opacity=0.3, stroke_width=2, stroke_color=m.YELLOW),
+ ).arrange(buff=1.5)
+ self.add(cubes)
+ ```
+ """
+
+ defaults: ClassVar[Style] = {
+ "fill_opacity": 0.75,
+ "fill_color": BLUE,
+ "stroke_width": 0,
+ }
+
+ def __init__(self, side_length: float = 2, **kwargs: Unpack[Style]) -> None:
+ self.side_length = side_length
+ super().__init__(**kwargs)
+
+ def generate_points(self) -> Self:
+ for vect in (IN, OUT, LEFT, RIGHT, UP, DOWN):
+ face = Square(
+ side_length=self.side_length,
+ shade_in_3d=True,
+ joint_type=LineJointType.BEVEL,
+ )
+ face.flip()
+ face.shift(self.side_length * OUT / 2.0)
+ face.apply_matrix(z_to_vector(vect))
+ self.add(face)
+ return self
+
+
+class Prism(Cube):
+ """A box: a [Cube][manimgx.Cube] stretched to its width, height and depth; blue,
+ three-quarters opaque, without edges and shaded by a three-dimensional scene's
+ light, unless styled.
+
+ Args:
+ dimensions: Its size along x, y and z, in scene units; None for (3, 2, 1).
+ **kwargs: [Style keywords][manimgx.drawing.paint.Style] for its faces.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class PrismExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=60 * m.DEGREES, theta=-60 * m.DEGREES)
+ prisms = m.Group(
+ m.Prism(),
+ m.Prism(dimensions=[1, 2, 3], fill_color=m.GREEN),
+ ).arrange(buff=1.5)
+ self.add(prisms)
+ ```
+ """
+
+ def __init__(
+ self, dimensions: Vector3DLike | None = None, **kwargs: Unpack[Style]
+ ) -> None:
+ self.dimensions = (
+ [3.0, 2.0, 1.0] if dimensions is None else [float(d) for d in dimensions]
+ )
+ super().__init__(**kwargs)
+
+ def generate_points(self) -> Self:
+ super().generate_points()
+ for dim, value in enumerate(self.dimensions):
+ self.rescale_to_fit(value, dim, stretch=True)
+ return self
+
+
+class _DirectedSurface(Surface):
+ direction: Vector3D
+ _current_theta: float
+ _current_phi: float
+
+ def set_direction(self, direction: Vector3DLike) -> Self:
+ """Turn the surface's axis to a direction, undoing its previous direction.
+
+ Both turns are about the origin, preserving other changes to the surface.
+
+ Args:
+ direction: The direction of the axis, from a cone's base to its apex.
+ """
+ self.direction = np.array(direction, dtype=float)
+ x, y, z = self.direction
+ r = np.sqrt(x**2 + y**2 + z**2)
+ theta = float(np.arccos(z / r)) if r > 0 else 0.0
+ if x == 0:
+ phi = 0.0 if y == 0 else float(np.arctan(np.inf)) + (PI if y < 0 else 0)
+ else:
+ phi = float(np.arctan(y / x))
+ if x < 0:
+ phi += PI
+ turn = (
+ rotation_about_z(phi)
+ @ rotation_matrix(theta - self._current_theta, Y_AXIS)
+ @ rotation_about_z(-self._current_phi)
+ )
+ self._rotate_direction_state(turn)
+ self._apply_linear(turn, ORIGIN)
+ self._current_theta, self._current_phi = theta, phi
+ return self
+
+
+class Cone(_DirectedSurface):
+ """A cone, its apex at the origin, pointing along `direction` from its base; blue,
+ shaded by a three-dimensional scene's light, and open at the base, unless styled.
+
+ It is a [Surface][manimgx.Surface] of u, the distance from the apex along its side,
+ and v, the angle around its axis: `v_range` may cut out a slice, and `u_min` its
+ tip. It is one color, not checkered, unless given `checkerboard_colors`.
+
+ Args:
+ base_radius: The radius of its base, in scene units.
+ height: Its height, from its base to its apex, in scene units.
+ direction: The direction it points in, from its base to its apex.
+ show_base: Whether a disc closes its base.
+ v_range: The range of the angle around its axis, in radians: (0, τ) goes all
+ the way around.
+ u_min: Where its side begins, as a distance from the apex along it, in scene
+ units: above 0, the tip is cut off.
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceOptions]:
+ `resolution`, `checkerboard_colors`, and the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class ConeExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=-60 * m.DEGREES)
+ frustum = m.Cone(base_radius=1.5, height=3, u_min=1.5, fill_color=m.RED)
+ self.add(
+ m.Cone(base_radius=1.5, height=3).shift(4 * m.LEFT + 1.5 * m.OUT),
+ m.Cone(direction=m.X_AXIS + m.Z_AXIS, show_base=True),
+ frustum.shift(4 * m.RIGHT + 1.5 * m.OUT),
+ )
+ ```
+ """
+
+ def __init__(
+ self,
+ base_radius: float = 1,
+ height: float = 1,
+ direction: Vector3DLike = Z_AXIS,
+ show_base: bool = False,
+ v_range: Sequence[float] = (0, TAU),
+ u_min: float = 0,
+ **kwargs: Unpack[SurfaceOptions],
+ ) -> None:
+ self.direction = np.array(direction, dtype=float)
+ self.theta = PI - np.arctan(base_radius / height)
+ kwargs.setdefault("checkerboard_colors", False)
+ super().__init__(
+ self.func,
+ v_range=v_range,
+ u_range=(u_min, np.sqrt(base_radius**2 + height**2)),
+ **kwargs,
+ )
+ self.new_height = height
+ self._current_theta = 0.0
+ self._current_phi = 0.0
+ self.base_circle = Circle(
+ radius=base_radius,
+ color=self.fill_color,
+ fill_opacity=self.fill_opacity,
+ stroke_width=0,
+ )
+ """The disc of its base, in its fill color: part of the cone with
+ `show_base`."""
+ self.base_circle.shift(height * IN)
+ self._set_start_and_end_attributes(self.direction)
+ if show_base:
+ self.add(self.base_circle)
+ self.set_direction(direction)
+
+ def func(self, u: float, v: float) -> Point3D:
+ """The cone's point, as it is made pointing up (along z) with its apex at the
+ origin, at a distance from the apex along its side and an angle around its axis.
+
+ Args:
+ u: The distance from the apex, along the side, in scene units.
+ v: The angle around the axis, in radians.
+
+ Returns:
+ The point, in scene coordinates.
+ """
+ return _revolve(u * np.sin(self.theta), u * np.cos(self.theta), v)
+
+ def _set_start_and_end_attributes(self, direction: Vector3D) -> None:
+ normalized_direction = direction * np.linalg.norm(direction)
+ start = self.base_circle.get_center()
+ end = start + normalized_direction * self.new_height
+ self.start_point = VectorizedPoint(start)
+ self.end_point = VectorizedPoint(end)
+ self.add(self.start_point, self.end_point)
+
+
+class Cylinder(_DirectedSurface):
+ """A cylinder, centered at the origin, its axis along `direction`: checkered in two
+ blues, closed at both ends by discs, and shaded by a three-dimensional scene's
+ light, unless styled.
+
+ It is a [Surface][manimgx.Surface] of u, the height along its axis, and v, the
+ angle around it: `v_range` may cut out a slice.
+
+ Args:
+ radius: Its radius, in scene units.
+ height: Its height, along its axis, in scene units.
+ direction: The direction of its axis.
+ v_range: The range of the angle around its axis, in radians: (0, τ) goes all
+ the way around.
+ show_ends: Whether discs close its ends (see
+ [add_bases][manimgx.Cylinder.add_bases]).
+ resolution: How many faces it has along its axis and around it: one number for
+ both, or `(u, v)`.
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook]:
+ `checkerboard_colors`, and the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class CylinderExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=-60 * m.DEGREES)
+ self.add(
+ m.Cylinder(radius=1.5, height=3).shift(4.5 * m.LEFT),
+ m.Cylinder(radius=0.5, height=4, direction=m.X_AXIS + m.Z_AXIS),
+ m.Cylinder(show_ends=False, v_range=(0, m.PI)).shift(4 * m.RIGHT),
+ )
+ ```
+ """
+
+ def __init__(
+ self,
+ radius: float = 1,
+ height: float = 2,
+ direction: Vector3DLike = Z_AXIS,
+ v_range: Sequence[float] = (0, TAU),
+ show_ends: bool = True,
+ resolution: int | Sequence[int] = (24, 24),
+ **kwargs: Unpack[SurfaceLook],
+ ) -> None:
+ self._height = height
+ self.radius = radius
+ super().__init__(
+ self.func,
+ resolution=resolution,
+ u_range=(-self._height / 2, self._height / 2),
+ v_range=v_range,
+ **kwargs,
+ )
+ if show_ends:
+ self.add_bases()
+ self._current_phi = 0.0
+ self._current_theta = 0.0
+ self.set_direction(direction)
+
+ def func(self, u: float, v: float) -> np.ndarray:
+ """The cylinder's point, as it is made along z and centered on the origin, at a
+ height along its axis and an angle around it.
+
+ Args:
+ u: The height along the axis, in scene units, from the middle.
+ v: The angle around the axis, in radians.
+
+ Returns:
+ The point, in scene coordinates.
+ """
+ return _revolve(self.radius, u, v)
+
+ def add_bases(self) -> Self:
+ """Add the discs that close the cylinder's ends: `base_top` and `base_bottom`,
+ in its fill color.
+
+ They are placed at the ends of a cylinder along z, centered on the origin, as
+ it is made: the constructor adds them (with `show_ends`) before turning the
+ cylinder to its direction.
+ """
+ bases = [
+ Circle(
+ radius=self.radius,
+ color=self.fill_color,
+ fill_opacity=self.fill_opacity,
+ shade_in_3d=True,
+ stroke_width=0,
+ )
+ for _ in range(2)
+ ]
+ self.base_top, self.base_bottom = bases
+ self.base_top.shift(self.u_range[1] * IN)
+ self.base_bottom.shift(self.u_range[0] * IN)
+ self.add(self.base_top, self.base_bottom)
+ return self
+
+
+class Line3DOptions(SurfaceLook, total=False):
+ """A [Line3D][manimgx.Line3D]'s keywords but its ends, for the methods that pass
+ them on ([parallel_to][manimgx.Line3D.parallel_to],
+ [perpendicular_to][manimgx.Line3D.perpendicular_to]): its thickness and resolution,
+ with the surface keywords."""
+
+ thickness: float
+ """The line's radius, in scene units (default 0.02)."""
+ resolution: int | tuple[int, int]
+ """How many faces the line has around it (2 along it), or `(along, around)`
+ (default 24)."""
+
+
+class Line3D(Cylinder):
+ """A line in three dimensions: a thin cylinder from one point to another; checkered
+ in two blues, and shaded by a three-dimensional scene's light, unless styled.
+
+ An end may be a mobject: the line then starts (or ends) at its point farthest
+ toward the other end (see [get_boundary_point][manimgx.Mobject.get_boundary_point]).
+
+ Args:
+ start: Where the line starts: a point, or a mobject.
+ end: Where it ends: a point, or a mobject.
+ thickness: Its radius, in scene units.
+ resolution: How many faces it has around it (2 along it), or
+ `(along, around)`.
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook]:
+ `color` paints it all in one color; the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class Line3DExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=65 * m.DEGREES, theta=-45 * m.DEGREES)
+ axes = m.ThreeDAxes(x_range=(-3, 3), y_range=(-3, 3), z_range=(-2, 2))
+ line = m.Line3D(axes.c2p(-2, 0, 0), axes.c2p(1, 2, 1.5), thickness=0.05)
+ self.add(axes, line.set_color(m.YELLOW))
+ ```
+ """
+
+ def __init__(
+ self,
+ start: Point3DLike | Mobject = LEFT,
+ end: Point3DLike | Mobject = RIGHT,
+ thickness: float = 0.02,
+ resolution: int | Sequence[int] = 24,
+ **kwargs: Unpack[SurfaceLook],
+ ):
+ color = kwargs.pop("color", None)
+ self.thickness = thickness
+ self.resolution = (2, resolution) if isinstance(resolution, int) else resolution
+ self.set_start_and_end_attrs(start, end, **kwargs)
+ if color is not None:
+ self.set_color(color)
+
+ def set_start_and_end_attrs(
+ self,
+ start: Point3DLike | Mobject,
+ end: Point3DLike | Mobject,
+ **kwargs: Unpack[SurfaceLook],
+ ) -> Self:
+ """Make the line run from one point (or mobject) to another: the constructor
+ calls it.
+
+ It makes the line anew, as a cylinder of the line's thickness and resolution
+ from `start` to `end`, styled by the keywords.
+
+ Args:
+ start: Where the line starts: a point, or a mobject.
+ end: Where it ends: a point, or a mobject.
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook].
+ """
+ rough_start = self.pointify(start)
+ rough_end = self.pointify(end)
+ self.vect = rough_end - rough_start
+ self.length = float(np.linalg.norm(self.vect))
+ self.direction: Vector3D = normalize(self.vect)
+ self.start = self.pointify(start, self.direction)
+ self.end = self.pointify(end, -self.direction)
+ super().__init__(
+ height=self.length,
+ radius=self.thickness,
+ direction=self.direction,
+ resolution=self.resolution,
+ **kwargs,
+ )
+ self.shift((self.start + self.end) / 2)
+ return self
+
+ def pointify(
+ self, mob_or_point: Mobject | Point3DLike, direction: Vector3DLike | None = None
+ ) -> Point3D:
+ """Turn a point or a mobject into a point: a point as it is; a mobject's center,
+ or its boundary point in a direction.
+
+ Args:
+ mob_or_point: The point, or the mobject.
+ direction: The direction of the mobject's boundary point (see
+ [get_boundary_point][manimgx.Mobject.get_boundary_point]); None for its
+ center.
+
+ Returns:
+ The point, in scene coordinates.
+ """
+ if isinstance(mob_or_point, Mobject):
+ return (
+ mob_or_point.get_center()
+ if direction is None
+ else mob_or_point.get_boundary_point(direction)
+ )
+ return np.array(mob_or_point, dtype=float)
+
+ @classmethod
+ def parallel_to(
+ cls,
+ line: Line3D,
+ point: Point3DLike = ORIGIN,
+ length: float = 5,
+ **kwargs: Unpack[Line3DOptions],
+ ) -> Line3D:
+ """Make a line parallel to another, through a point: `length` long, centered on
+ the point.
+
+ Args:
+ line: The line to be parallel to.
+ point: The point the new line is centered on.
+ length: The new line's length, in scene units.
+ **kwargs: [Line keywords][manimgx.mobjects.three_d.Line3DOptions]:
+ `thickness`, `color`, ….
+
+ Returns:
+ A new line.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class Line3DParallelToExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(
+ phi=60 * m.DEGREES, theta=-45 * m.DEGREES
+ )
+ line = m.Line3D(2 * m.RIGHT, m.UP + m.OUT, color=m.RED)
+ parallel = m.Line3D.parallel_to(line, color=m.YELLOW)
+ self.add(m.ThreeDAxes(), line, parallel)
+ ```
+ """
+ np_point = np.asarray(point, dtype=float)
+ vect = normalize(line.vect)
+ return cls(np_point + vect * length / 2, np_point - vect * length / 2, **kwargs)
+
+ @classmethod
+ def perpendicular_to(
+ cls,
+ line: Line3D,
+ point: Point3DLike = ORIGIN,
+ length: float = 5,
+ **kwargs: Unpack[Line3DOptions],
+ ) -> Line3D:
+ """Make a line perpendicular to another, through a point: in the plane of the
+ line and the point, `length` long, centered on the point.
+
+ A point on the line (which makes no plane with it) raises a ValueError.
+
+ Args:
+ line: The line to be perpendicular to.
+ point: The point the new line is centered on, off the line.
+ length: The new line's length, in scene units.
+ **kwargs: [Line keywords][manimgx.mobjects.three_d.Line3DOptions]:
+ `thickness`, `color`, ….
+
+ Returns:
+ A new line.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class Line3DPerpendicularToExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(
+ phi=60 * m.DEGREES, theta=-45 * m.DEGREES
+ )
+ line = m.Line3D(2 * m.RIGHT, m.UP + m.OUT, color=m.RED)
+ perpendicular = m.Line3D.perpendicular_to(line, color=m.BLUE)
+ self.add(m.ThreeDAxes(), line, perpendicular)
+ ```
+ """
+ np_point = np.asarray(point, dtype=float)
+ norm = np.cross(line.vect, np_point - line.start)
+ if np.linalg.norm(norm) == 0:
+ raise ValueError("Could not find the perpendicular.")
+ start, end = perpendicular_bisector([line.start, line.end], norm)
+ vect = normalize(end - start)
+ return cls(np_point + vect * length / 2, np_point - vect * length / 2, **kwargs)
+
+
+class Arrow3D(Line3D):
+ """An arrow in three dimensions: a thin cylinder from `start`, ending in a cone
+ whose apex is at `end`; white unless given a color, shaded by a three-dimensional
+ scene's light.
+
+ Args:
+ start: Where the arrow starts.
+ end: Where its tip's apex is.
+ thickness: The radius of its shaft, in scene units.
+ height: The length of its tip, a [Cone][manimgx.Cone], in scene units.
+ base_radius: The radius of its tip's base, in scene units.
+ resolution: How many faces its shaft has around it (2 along it), or
+ `(along, around)`.
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook]
+ for the shaft and the tip: `color`, and the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class Arrow3DExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=65 * m.DEGREES, theta=-45 * m.DEGREES)
+ axes = m.ThreeDAxes(x_range=(-3, 3), y_range=(-3, 3), z_range=(-2, 2))
+ arrows = [
+ m.Arrow3D(axes.c2p(0, 0, 0), axes.c2p(*end), color=color)
+ for end, color in (
+ ((2, 0, 0), m.RED),
+ ((0, 2, 0), m.GREEN),
+ ((0, 0, 2), m.BLUE),
+ ((1.5, 1.5, 1.5), m.YELLOW),
+ )
+ ]
+ self.add(axes, *arrows)
+ ```
+ """
+
+ def __init__(
+ self,
+ start: Point3DLike = LEFT,
+ end: Point3DLike = RIGHT,
+ thickness: float = 0.02,
+ height: float = 0.3,
+ base_radius: float = 0.08,
+ resolution: int | tuple[int, int] = 24,
+ **kwargs: Unpack[SurfaceLook],
+ ) -> None:
+ color = kwargs.pop("color", None) or WHITE
+ start, end = np.asarray(start, dtype=float), np.asarray(end, dtype=float)
+ super().__init__(
+ start=start,
+ end=end - height * normalize(end - start),
+ thickness=thickness,
+ resolution=resolution,
+ **kwargs,
+ )
+ self.cone = Cone(
+ direction=self.direction, base_radius=base_radius, height=height, **kwargs
+ )
+ """Its tip: a [Cone][manimgx.Cone], its apex at the arrow's end."""
+ self.cone.shift(end)
+ self.end_point = VectorizedPoint(end)
+ self.add(self.end_point, self.cone)
+ self.set_color(color)
+
+
+class Torus(Surface):
+ """A torus: a tube around a circle in the xy-plane, centered at the origin;
+ checkered in two blues and shaded by a three-dimensional scene's light, unless
+ styled.
+
+ It is a [Surface][manimgx.Surface] of u, the angle around its axis (z), and v, the
+ angle around its tube: their ranges may cut out a part of it.
+
+ Args:
+ major_radius: The radius of the circle the tube runs around: from the torus's
+ center to the middle of its tube, in scene units.
+ minor_radius: The tube's radius, in scene units.
+ u_range: The range of the angle around its axis, in radians: (0, τ) goes all
+ the way around.
+ v_range: The range of the angle around its tube, in radians.
+ resolution: How many faces it has around its axis and around its tube: one
+ number for both, or `(u, v)`; None for (24, 24).
+ **kwargs: [Surface keywords][manimgx.mobjects.three_d.SurfaceLook]:
+ `checkerboard_colors`, and the style keywords.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class TorusExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=65 * m.DEGREES, theta=-60 * m.DEGREES)
+ self.add(
+ m.Torus(major_radius=2, minor_radius=0.7).shift(3 * m.LEFT),
+ m.Torus(
+ major_radius=2,
+ minor_radius=0.7,
+ u_range=(0, 1.5 * m.PI),
+ checkerboard_colors=[m.ORANGE, m.YELLOW],
+ ).shift(3 * m.RIGHT),
+ )
+ ```
+ """
+
+ def __init__(
+ self,
+ major_radius: float = 3,
+ minor_radius: float = 1,
+ u_range: Sequence[float] = (0, TAU),
+ v_range: Sequence[float] = (0, TAU),
+ resolution: int | tuple[int, int] | None = None,
+ **kwargs: Unpack[SurfaceLook],
+ ) -> None:
+ self.R = major_radius
+ self.r = minor_radius
+ super().__init__(
+ self.func,
+ u_range=u_range,
+ v_range=v_range,
+ resolution=resolution if resolution is not None else (24, 24),
+ **kwargs,
+ )
+
+ def func(self, u: float, v: float) -> Point3D:
+ """The torus's point, as it is made around the origin, at an angle around its
+ axis and an angle around its tube.
+
+ Args:
+ u: The angle around the axis, in radians, counterclockwise from the x-axis.
+ v: The angle around the tube, in radians: 0 on its inner side.
+
+ Returns:
+ The point, in scene coordinates.
+ """
+ return _revolve(self.R - self.r * np.cos(v), -self.r * np.sin(v), u)
+
+
+class PolyhedronOptions(TypedDict, total=False):
+ """How a [Polyhedron][manimgx.Polyhedron]'s faces and its graph of vertices and
+ edges look, for the solids that set their own vertices and faces
+ ([Tetrahedron][manimgx.Tetrahedron], [ConvexHull3D][manimgx.ConvexHull3D], …)."""
+
+ faces_config: Style | None
+ """[Style keywords][manimgx.drawing.paint.Style] for the faces, over their defaults:
+ half opaque, shaded by a three-dimensional scene's light (default None: none)."""
+ graph_config: GraphOptions | None
+ """[Graph keywords][manimgx.mobjects.graph.GraphOptions] for the vertices and edges,
+ over their defaults: [Dot3D][manimgx.Dot3D] vertices, invisible edges (default None:
+ none)."""
+
+
+class Polyhedron(VGroup):
+ """A polyhedron: its faces, polygons through its vertices, and the graph of its
+ vertices and edges; blue half-opaque faces with white dots at the vertices, shaded
+ by a three-dimensional scene's light, unless styled.
+
+ The faces are its [faces][manimgx.Polyhedron.faces], a group of
+ [Polygon][manimgx.Polygon]s, whose outlines draw the edges; its
+ [graph][manimgx.Polyhedron.graph] is a [Graph][manimgx.Graph] whose vertices are
+ [Dot3D][manimgx.Dot3D]s, `graph[i]` the vertex of index i, and whose own edges are
+ invisible unless configured. The faces follow the vertices: move a vertex, and an
+ updater keeps the faces through it. Initial coordinates and graph options are
+ construction inputs; later positions belong to the graph, and `faces_list`
+ defines the faces.
+
+ Args:
+ vertex_coords: The vertices' points, in scene coordinates.
+ faces_list: The faces, each a list of the indices of its vertices, in order
+ around it.
+ faces_config: [Style keywords][manimgx.drawing.paint.Style] for the faces, over
+ their defaults (half opaque, shaded in 3D); None for none.
+ graph_config: [Graph keywords][manimgx.mobjects.graph.GraphOptions] for the
+ vertices and edges, over their defaults ([Dot3D][manimgx.Dot3D] vertices,
+ invisible edges); None for none.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class PolyhedronExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=30 * m.DEGREES)
+ corners = [[1, 1, 0], [1, -1, 0], [-1, -1, 0], [-1, 1, 0], [0, 0, 2]]
+ faces = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [0, 1, 2, 3]]
+ pyramid = m.Polyhedron(corners, faces).scale(1.5).shift(m.IN)
+ self.add(pyramid)
+ self.play(pyramid.graph[4].animate.move_to([1, 1, 2]), run_time=2)
+ ```
+ """
+
+ def __init__(
+ self,
+ vertex_coords: Point3DLike_Array,
+ faces_list: list[list[int]],
+ faces_config: Style | None = None,
+ graph_config: GraphOptions | None = None,
+ ):
+ super().__init__()
+ self.faces_config = Style(fill_opacity=0.5, shade_in_3d=True) | (
+ faces_config or Style()
+ )
+ graph_config = GraphOptions(
+ vertex_type=Dot3D, edge_config={"stroke_opacity": 0}
+ ) | (graph_config or GraphOptions())
+ vertex_indices = list(range(len(vertex_coords)))
+ layout: dict[Hashable, Point3D] = {
+ i: np.asarray(p, dtype=float) for i, p in enumerate(vertex_coords)
+ }
+ self.faces_list = faces_list
+ """The faces, each a list of the indices of its vertices."""
+ face_coords = [[layout[j] for j in i] for i in faces_list]
+ self.edges = self.get_edges(self.faces_list)
+ """The edges, each a pair of vertex indices."""
+ self.faces = self.create_faces(face_coords)
+ """The faces: a group of [Polygon][manimgx.Polygon]s, in the order of
+ `faces_list`."""
+ self.graph = Graph(
+ vertex_indices,
+ self.edges,
+ **(graph_config | GraphOptions(layout=layout)),
+ )
+ """The vertices and edges: a [Graph][manimgx.Graph], `graph[i]` the vertex of
+ index i."""
+ self.add(self.faces, self.graph)
+ self.add_updater(self.update_faces)
+
+ def get_edges(self, faces_list: list[list[int]]) -> list[tuple[int, int]]:
+ """Find the edges of faces: each two vertices next to each other around a face,
+ each edge once.
+
+ Args:
+ faces_list: The faces, each a list of the indices of its vertices, in order
+ around it.
+
+ Returns:
+ The edges, each a pair of vertex indices, in the order the faces first give
+ them.
+ """
+ # each edge once (CE listed it once per face, doubling a translucent edge)
+ edges: dict[frozenset[int], tuple[int, int]] = {}
+ for face in faces_list:
+ for edge in zip(face, face[1:] + face[:1], strict=True):
+ edges.setdefault(frozenset(edge), edge)
+ return list(edges.values())
+
+ def create_faces(
+ self, face_coords: Sequence[Sequence[Point3DLike]]
+ ) -> VGroup[Polygon]:
+ """Make faces: a polygon through each list of points, styled by the
+ polyhedron's `faces_config`.
+
+ Args:
+ face_coords: The faces' corners, a list of points for each face.
+
+ Returns:
+ A new group of the polygons.
+ """
+ return VGroup[Polygon](
+ *(Polygon(*face, **self.faces_config) for face in face_coords)
+ )
+
+ def update_faces(self, m: Mobject) -> Self:
+ """Put the faces back through the vertices, where they are now: the updater the
+ polyhedron runs every frame.
+
+ Args:
+ m: The mobject the updater runs for (the polyhedron); unused.
+ """
+ for face, coords in zip(self.faces, self.extract_face_coords(), strict=True):
+ face.set_points_as_corners([*coords, coords[0]])
+ return self
+
+ def extract_face_coords(self) -> list[list[Point3D]]:
+ """The faces' corners where the vertices are now: the centers of the graph's
+ vertices.
+
+ Returns:
+ A list of points for each face, in the order of `faces_list`.
+ """
+ vertices = [vertex.get_center() for vertex in self.graph.vertices.values()]
+ return [[vertices[i] for i in face] for face in self.faces_list]
+
+
+class Tetrahedron(Polyhedron):
+ """A regular tetrahedron: four triangular faces, centered at the origin; a
+ [Polyhedron][manimgx.Polyhedron], blue half-opaque faces with white dots at the
+ vertices unless styled.
+
+ Args:
+ edge_length: The length of its edges, in scene units.
+ **kwargs: [Polyhedron keywords][manimgx.mobjects.three_d.PolyhedronOptions]:
+ `faces_config`, `graph_config`.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class TetrahedronExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=30 * m.DEGREES)
+ self.add(m.Tetrahedron(edge_length=4))
+ ```
+ """
+
+ def __init__(self, edge_length: float = 1, **kwargs: Unpack[PolyhedronOptions]):
+ unit = edge_length * np.sqrt(2) / 4
+ super().__init__(
+ vertex_coords=[
+ np.array([unit, unit, unit]),
+ np.array([unit, -unit, -unit]),
+ np.array([-unit, unit, -unit]),
+ np.array([-unit, -unit, unit]),
+ ],
+ faces_list=[[0, 1, 2], [3, 0, 2], [0, 1, 3], [3, 1, 2]],
+ **kwargs,
+ )
+
+
+class Octahedron(Polyhedron):
+ """A regular octahedron: eight triangular faces, its vertices on the axes, centered
+ at the origin; a [Polyhedron][manimgx.Polyhedron], blue half-opaque faces with
+ white dots at the vertices unless styled.
+
+ Args:
+ edge_length: The length of its edges, in scene units.
+ **kwargs: [Polyhedron keywords][manimgx.mobjects.three_d.PolyhedronOptions]:
+ `faces_config`, `graph_config`.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class OctahedronExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=30 * m.DEGREES)
+ octahedron = m.Octahedron(edge_length=3)
+ octahedron.faces[2].set_color(m.YELLOW)
+ octahedron.graph[0].set_color(m.RED)
+ self.add(octahedron)
+ ```
+ """
+
+ def __init__(self, edge_length: float = 1, **kwargs: Unpack[PolyhedronOptions]):
+ unit = edge_length * np.sqrt(2) / 2
+ super().__init__(
+ vertex_coords=[
+ np.array([unit, 0, 0]),
+ np.array([-unit, 0, 0]),
+ np.array([0, unit, 0]),
+ np.array([0, -unit, 0]),
+ np.array([0, 0, unit]),
+ np.array([0, 0, -unit]),
+ ],
+ faces_list=[
+ [2, 4, 1],
+ [0, 4, 2],
+ [4, 3, 0],
+ [1, 3, 4],
+ [3, 5, 0],
+ [1, 5, 3],
+ [2, 5, 1],
+ [0, 5, 2],
+ ],
+ **kwargs,
+ )
+
+
+class Icosahedron(Polyhedron):
+ """A regular icosahedron: twenty triangular faces, centered at the origin; a
+ [Polyhedron][manimgx.Polyhedron], blue half-opaque faces with white dots at the
+ vertices unless styled.
+
+ Args:
+ edge_length: The length of its edges, in scene units.
+ **kwargs: [Polyhedron keywords][manimgx.mobjects.three_d.PolyhedronOptions]:
+ `faces_config`, `graph_config`.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class IcosahedronExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=30 * m.DEGREES)
+ self.add(
+ m.Icosahedron(edge_length=2, faces_config={"color": m.TEAL})
+ )
+ ```
+ """
+
+ def __init__(self, edge_length: float = 1, **kwargs: Unpack[PolyhedronOptions]):
+ unit_a = edge_length * ((1 + np.sqrt(5)) / 4)
+ unit_b = edge_length * (1 / 2)
+ super().__init__(
+ vertex_coords=[
+ np.array([0, unit_b, unit_a]),
+ np.array([0, -unit_b, unit_a]),
+ np.array([0, unit_b, -unit_a]),
+ np.array([0, -unit_b, -unit_a]),
+ np.array([unit_b, unit_a, 0]),
+ np.array([unit_b, -unit_a, 0]),
+ np.array([-unit_b, unit_a, 0]),
+ np.array([-unit_b, -unit_a, 0]),
+ np.array([unit_a, 0, unit_b]),
+ np.array([unit_a, 0, -unit_b]),
+ np.array([-unit_a, 0, unit_b]),
+ np.array([-unit_a, 0, -unit_b]),
+ ],
+ faces_list=[
+ [1, 8, 0],
+ [1, 5, 7],
+ [8, 5, 1],
+ [7, 3, 5],
+ [5, 9, 3],
+ [8, 9, 5],
+ [3, 2, 9],
+ [9, 4, 2],
+ [8, 4, 9],
+ [0, 4, 8],
+ [6, 4, 0],
+ [6, 2, 4],
+ [11, 2, 6],
+ [3, 11, 2],
+ [0, 6, 10],
+ [10, 1, 0],
+ [10, 7, 1],
+ [11, 7, 3],
+ [10, 11, 7],
+ [10, 11, 6],
+ ],
+ **kwargs,
+ )
+
+
+class Dodecahedron(Polyhedron):
+ """A regular dodecahedron: twelve pentagonal faces, centered at the origin; a
+ [Polyhedron][manimgx.Polyhedron], blue half-opaque faces with white dots at the
+ vertices unless styled.
+
+ Args:
+ edge_length: The length of its edges, in scene units.
+ **kwargs: [Polyhedron keywords][manimgx.mobjects.three_d.PolyhedronOptions]:
+ `faces_config`, `graph_config`.
+
+ Examples:
+ ```python
+ import manimgx as m
+
+
+ class DodecahedronExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=30 * m.DEGREES)
+ self.add(
+ m.Dodecahedron(
+ edge_length=1.5, faces_config={"color": m.PURPLE}
+ )
+ )
+ ```
+ """
+
+ def __init__(self, edge_length: float = 1, **kwargs: Unpack[PolyhedronOptions]):
+ unit_a = edge_length * ((1 + np.sqrt(5)) / 4)
+ unit_b = edge_length * ((3 + np.sqrt(5)) / 4)
+ unit_c = edge_length * (1 / 2)
+ super().__init__(
+ vertex_coords=[
+ np.array([unit_a, unit_a, unit_a]),
+ np.array([unit_a, unit_a, -unit_a]),
+ np.array([unit_a, -unit_a, unit_a]),
+ np.array([unit_a, -unit_a, -unit_a]),
+ np.array([-unit_a, unit_a, unit_a]),
+ np.array([-unit_a, unit_a, -unit_a]),
+ np.array([-unit_a, -unit_a, unit_a]),
+ np.array([-unit_a, -unit_a, -unit_a]),
+ np.array([0, unit_c, unit_b]),
+ np.array([0, unit_c, -unit_b]),
+ np.array([0, -unit_c, -unit_b]),
+ np.array([0, -unit_c, unit_b]),
+ np.array([unit_c, unit_b, 0]),
+ np.array([-unit_c, unit_b, 0]),
+ np.array([unit_c, -unit_b, 0]),
+ np.array([-unit_c, -unit_b, 0]),
+ np.array([unit_b, 0, unit_c]),
+ np.array([-unit_b, 0, unit_c]),
+ np.array([unit_b, 0, -unit_c]),
+ np.array([-unit_b, 0, -unit_c]),
+ ],
+ faces_list=[
+ [18, 16, 0, 12, 1],
+ [3, 18, 16, 2, 14],
+ [3, 10, 9, 1, 18],
+ [1, 9, 5, 13, 12],
+ [0, 8, 4, 13, 12],
+ [2, 16, 0, 8, 11],
+ [4, 17, 6, 11, 8],
+ [17, 19, 5, 13, 4],
+ [19, 7, 15, 6, 17],
+ [6, 15, 14, 2, 11],
+ [19, 5, 9, 10, 7],
+ [7, 10, 3, 14, 15],
+ ],
+ **kwargs,
+ )
+
+
+class ConvexHull3D(Polyhedron):
+ """The convex hull of points in three dimensions: the smallest convex polyhedron
+ around them, its faces triangles; a [Polyhedron][manimgx.Polyhedron], blue
+ half-opaque faces with white dots at the vertices unless styled.
+
+ Its vertices are the points on the hull (those inside are left out), and its faces
+ are wound counterclockwise, seen from outside. Fewer than four points raise a
+ ValueError.
+
+ Args:
+ *points: The points, in scene coordinates.
+ tolerance: How far outside a face a point must be to count as beyond it, in
+ scene units.
+ **kwargs: [Polyhedron keywords][manimgx.mobjects.three_d.PolyhedronOptions]:
+ `faces_config`, `graph_config`.
+
+ Examples:
+ ```python
+ import numpy as np
+
+ import manimgx as m
+
+
+ class ConvexHull3DExample(m.ThreeDScene):
+ def construct(self) -> None:
+ self.set_camera_orientation(phi=70 * m.DEGREES, theta=30 * m.DEGREES)
+ points = np.random.default_rng(1).uniform(-2, 2, size=(30, 3))
+ hull = m.ConvexHull3D(*points, faces_config={"color": m.GREEN})
+ self.add(hull, *(m.Dot3D(p, color=m.YELLOW) for p in points))
+ ```
+ """
+
+ def __init__(
+ self,
+ *points: Point3DLike,
+ tolerance: float = 1e-05,
+ **kwargs: Unpack[PolyhedronOptions],
+ ):
+ array = np.array(points, dtype=float)
+ hull = QuickHull(tolerance)
+ hull.build(array)
+ # In the hull's own order: CE iterated hash sets of point bytes, whose order changes
+ # from process to process (salted hashing), and so did faces, vertex ids and draw order.
+ index: dict[bytes, int] = {}
+ vertices: list[Point3D] = []
+ faces: list[list[int]] = []
+ for facet in dict.fromkeys(hull.facets):
+ if facet in hull.removed:
+ continue
+ a, b, c = facet.coordinates
+ corners = (
+ (a, b, c)
+ if np.dot(np.cross(b - a, c - a), facet.normal) > 0
+ else (a, c, b)
+ )
+ for point in corners: # wound counterclockwise seen from outside
+ if point.tobytes() not in index:
+ index[point.tobytes()] = len(vertices)
+ vertices.append(point)
+ faces.append([index[point.tobytes()] for point in corners])
+ super().__init__(vertex_coords=vertices, faces_list=faces, **kwargs)
diff --git a/src/manimgx/mobjects/vector_field.py b/src/manimgx/mobjects/vector_field.py
index 92551463..8d819ca3 100644
--- a/src/manimgx/mobjects/vector_field.py
+++ b/src/manimgx/mobjects/vector_field.py
@@ -7,6 +7,8 @@
from __future__ import annotations
+from warnings import deprecated
+
__all__ = ["ArrowVectorField", "StreamLines", "VectorField"]
import itertools as it
import random
@@ -139,6 +141,7 @@ class VectorField(VGroup):
None
)
+ @deprecated("use pos_to_color", category=None)
def pos_to_rgb(self, pos: Point3D) -> FloatRGB:
"""The field's RGB color at a point, using its current function and palette."""
if self.single_color:
@@ -368,7 +371,7 @@ class ArrowVectorField(VectorField):
values.
length_func: The arrows' length, as a function of their vectors' length.
opacity: The arrows' opacity, from 0 to 1.
- vector_config: [Arrow keywords][manimgx.mobjects.shapes.ArrowTips] for the
+ vector_config: [Arrow keywords][manimgx.Arrow] for the
arrows: their tips, `stroke_width`, …; None for none.
**kwargs: [Style keywords][manimgx.drawing.paint.Style] of the field's group
itself: as it has no points, they do not restyle its arrows.
diff --git a/tests/unit/mobjects/test_three_d.py b/tests/unit/mobjects/test_three_d.py
new file mode 100644
index 00000000..f0be986e
--- /dev/null
+++ b/tests/unit/mobjects/test_three_d.py
@@ -0,0 +1,168 @@
+"""Spatial shapes are where their parameters put them.
+
+- A spatial arrow's shaft meets its tip, whose apex is the requested end, in any direction.
+- A cone's or cylinder's direction replaces the one before, carries its edits, parts and sheen
+ about the origin, and comes back to where it was; a spatial line rebuilt between new ends
+ starts from a fresh frame.
+- A torus is its tube revolved about the axis, starting from the inner side and turning down.
+- A polyhedron's faces follow its vertices, the same faces in the same paint.
+"""
+
+import numpy as np
+import pytest
+from hypothesis import assume, example, given
+from hypothesis import strategies as st
+from tests.strategies import arrays
+
+import manimgx as m
+
+
+@given(ends=arrays((2, 3), 5), height=st.floats(0.05, 0.7))
+@example(ends=np.array([[0.0, 0.0, 0.0], [0.0, 2.0**-24, 3.0]]), height=0.5)
+@example(ends=np.array([[2.0**-23, 2.0**-23, 0.0], [0.0, 2.0**-23, 4.0]]), height=0.5)
+def test_an_arrow_joins_its_shaft_to_its_tip_in_any_direction(
+ ends: np.ndarray,
+ height: float,
+) -> None:
+ start, end = ends
+ length = np.linalg.norm(end - start)
+ assume(length > 2 * height)
+ direction = (end - start) / length
+ # a direction a hair off the axis turns by its tiny cross product with it: to about the
+ # square root of the float precision, of the coordinates
+ tolerance = np.sqrt(np.finfo(float).eps) * (1 + np.abs(ends).max())
+ arrow = m.Arrow3D(
+ start,
+ end,
+ height=height,
+ resolution=6,
+ color=m.RED,
+ fill_opacity=0.4,
+ checkerboard_colors=[m.BLUE, m.YELLOW],
+ )
+ np.testing.assert_allclose(arrow.base_top.get_center(), start, atol=tolerance)
+ np.testing.assert_allclose(
+ arrow.base_bottom.get_center(),
+ end - height * direction,
+ atol=tolerance,
+ )
+ np.testing.assert_allclose(arrow.cone.points[0], end, atol=tolerance)
+ np.testing.assert_allclose(arrow.end_point.get_center(), end, atol=tolerance)
+ for surface in [arrow, arrow.cone]:
+ assert np.allclose(surface.paint.fill[:, :3], m.RED.to_rgb())
+ assert np.allclose(surface.paint.fill[:, 3], 0.4)
+
+
[email protected]("kind", [m.Cone, m.Cylinder])
+@given(directions=arrays((3, 3), 5))
+def test_successive_directions_replace_each_other_and_round_trip(
+ kind: type[m.Cone] | type[m.Cylinder], directions: np.ndarray
+) -> None:
+ surface = (
+ kind(show_base=True, resolution=4)
+ if kind is m.Cone
+ else m.Cylinder(resolution=4)
+ )
+ original = [part.points.copy() for part in surface.get_family()]
+ sheen = [part.paint.sheen_direction.copy() for part in surface.get_family()]
+ for direction in [*directions, m.ORIGIN, m.OUT]:
+ assert surface.set_direction(direction) is surface
+ axis = (
+ surface.points[0] - surface.base_circle.get_center()
+ if isinstance(surface, m.Cone)
+ else surface.base_bottom.get_center() - surface.base_top.get_center()
+ )
+ wanted = direction if np.linalg.norm(direction) else m.OUT
+ np.testing.assert_allclose(
+ axis / np.linalg.norm(axis), wanted / np.linalg.norm(wanted), atol=1e-7
+ )
+ for part, points, expected_sheen in zip(
+ surface.get_family(), original, sheen, strict=True
+ ):
+ np.testing.assert_allclose(part.points, points, atol=1e-12)
+ np.testing.assert_allclose(
+ part.paint.sheen_direction, expected_sheen, atol=1e-12
+ )
+
+
[email protected]("kind", [m.Cone, m.Cylinder])
+def test_directions_carry_edits_children_and_sheen_about_the_origin(
+ kind: type[m.Cone] | type[m.Cylinder],
+) -> None:
+ surface = kind(resolution=4)
+ surface.add(m.Line(m.ORIGIN, m.RIGHT))
+ surface.shift((1, 2, 3)).stretch(0.7, 0).set_sheen(0.5, m.UP).rotate(m.PI / 4)
+ points = [part.points.copy() for part in surface.get_family()]
+ sheen = [part.paint.sheen_direction.copy() for part in surface.get_family()]
+ for direction, indices, signs in [
+ (m.UP, [1, 2, 0], [-1, 1, -1]),
+ (m.RIGHT, [2, 1, 0], [1, 1, -1]),
+ ]:
+ surface.set_direction(direction)
+ for part, original, light in zip(
+ surface.get_family(), points, sheen, strict=True
+ ):
+ np.testing.assert_allclose(
+ part.points, original[:, indices] * signs, atol=1e-12
+ )
+ np.testing.assert_allclose(
+ part.paint.sheen_direction, light[indices] * signs, atol=1e-12
+ )
+
+
+def test_rebuilding_a_spatial_line_resets_its_direction_frame() -> None:
+ line = m.Line3D((-1, 2, 3), (4, -2, -1), resolution=4)
+ line.set_direction(m.UP)
+ start, end = np.array([2, -3, 1]), np.array([-3, 2, -1])
+ line.set_start_and_end_attrs(start, end)
+ np.testing.assert_allclose(line.base_top.get_center(), start, atol=1e-12)
+ np.testing.assert_allclose(line.base_bottom.get_center(), end, atol=1e-12)
+
+
+@given(radius=st.floats(0.3, 5), tube_fraction=st.floats(0.05, 0.9))
+def test_a_torus_revolves_its_tube_from_the_inner_side_downward(
+ radius: float, tube_fraction: float
+) -> None:
+ tube = radius * tube_fraction
+ torus = m.Torus(major_radius=radius, minor_radius=tube, resolution=(5, 7))
+ x, y, z = torus.points.T
+ np.testing.assert_allclose(
+ (np.hypot(x, y) - radius) ** 2 + z**2, tube**2, atol=1e-12
+ )
+ for u, v, point in [
+ (0, 0, (radius - tube, 0, 0)),
+ (0, m.PI / 2, (radius, 0, -tube)),
+ (0, m.PI, (radius + tube, 0, 0)),
+ (m.PI / 2, 0, (0, radius - tube, 0)),
+ ]:
+ np.testing.assert_allclose(torus.func(u, v), point, atol=1e-12)
+
+
+@given(vertices=arrays((5, 3), 5))
+def test_faces_follow_vertices_without_replacing_their_identity_or_style(
+ vertices: np.ndarray,
+) -> None:
+ faces = [[0, 1, 2, 3], [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]
+ poly = m.Polyhedron(
+ [[-1, -1, 0], [1, -1, 0], [1, 1, 0], [-1, 1, 0], [0, 0, 1]],
+ faces,
+ graph_config={"vertex_type": m.VectorizedPoint},
+ )
+ original_faces = list(poly.faces)
+ for i, face in enumerate(poly.faces):
+ face.set_color(m.RED if i % 2 else m.BLUE).set_opacity((i + 1) / 5)
+ paints = [face.paint for face in poly.faces]
+ for i, vertex in enumerate(vertices):
+ poly.graph[i].move_to(vertex)
+ assert poly.update_faces(poly) is poly
+ for face, original, paint, indices in zip(
+ poly.faces, original_faces, paints, faces, strict=True
+ ):
+ assert face is original
+ assert face.paint is paint
+ np.testing.assert_allclose(
+ face.get_start_anchors(), vertices[indices], atol=1e-9
+ )
+ np.testing.assert_allclose(
+ face.get_end_anchors()[-1], vertices[indices[0]], atol=1e-9
+ )