statanim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- statanim/__init__.py +0 -0
- statanim/animations/__init__.py +0 -0
- statanim/animations/clt_demo.py +1852 -0
- statanim/animations/flip_roll.py +1851 -0
- statanim/animations/sampling.py +2291 -0
- statanim/animations/transitions.py +1857 -0
- statanim/axes/__init__.py +0 -0
- statanim/axes/axes3d.py +1989 -0
- statanim/axes/grid3d.py +1127 -0
- statanim/axes/number_plane3d.py +1526 -0
- statanim/charts/__init__.py +0 -0
- statanim/charts/bar_chart3d.py +1572 -0
- statanim/charts/box_plot3d.py +1977 -0
- statanim/charts/heat_map3d.py +1639 -0
- statanim/charts/histogram3d.py +1019 -0
- statanim/charts/line_plot3d.py +1891 -0
- statanim/charts/scatter_plot3d.py +1469 -0
- statanim/charts/violin_plot3d.py +1567 -0
- statanim/core/__init__.py +0 -0
- statanim/core/base.py +1585 -0
- statanim/core/colors.py +1373 -0
- statanim/core/math_utils.py +2081 -0
- statanim/core/tex_utils.py +2111 -0
- statanim/distributions/__init__.py +0 -0
- statanim/distributions/base_dist.py +1936 -0
- statanim/distributions/cdf_viz.py +1773 -0
- statanim/distributions/continuous_dists.py +1769 -0
- statanim/distributions/discrete_dists.py +1734 -0
- statanim/distributions/normal3d.py +1877 -0
- statanim/distributions/pdf_viz.py +2247 -0
- statanim/distributions/pmf_viz.py +1936 -0
- statanim/inference/__init__.py +0 -0
- statanim/inference/confidence_interval.py +1693 -0
- statanim/inference/error_types.py +1825 -0
- statanim/inference/hypothesis.py +1821 -0
- statanim/inference/sampling_dist.py +1933 -0
- statanim/probability/__init__.py +0 -0
- statanim/probability/bayes.py +1682 -0
- statanim/probability/prob_tree.py +1478 -0
- statanim/probability/sample_space.py +1642 -0
- statanim/probability/venn3d.py +1857 -0
- statanim/props/__init__.py +0 -0
- statanim/props/card.py +2259 -0
- statanim/props/coin.py +1011 -0
- statanim/props/die.py +1391 -0
- statanim/props/spinner.py +1591 -0
- statanim/props/urn.py +1531 -0
- statanim/regression/__init__.py +0 -0
- statanim/regression/correlation.py +2805 -0
- statanim/regression/regression_plane.py +2286 -0
- statanim/regression/residuals.py +2599 -0
- statanim/scenes/__init__.py +0 -0
- statanim/scenes/demo_bayes.py +2056 -0
- statanim/scenes/demo_clt.py +1756 -0
- statanim/scenes/demo_distributions.py +1905 -0
- statanim/scenes/demo_hypothesis.py +1791 -0
- statanim/ui/__init__.py +0 -0
- statanim/ui/labels.py +1844 -0
- statanim/ui/panels.py +1937 -0
- statanim/ui/table3d.py +1745 -0
- statanim/ui/ticker.py +2007 -0
- statanim-0.1.0.dist-info/METADATA +463 -0
- statanim-0.1.0.dist-info/RECORD +66 -0
- statanim-0.1.0.dist-info/WHEEL +5 -0
- statanim-0.1.0.dist-info/licenses/LICENSE +21 -0
- statanim-0.1.0.dist-info/top_level.txt +1 -0
statanim/axes/grid3d.py
ADDED
|
@@ -0,0 +1,1127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
statanim/axes/grid3d.py
|
|
3
|
+
==========================
|
|
4
|
+
Production-quality 3D grid system for statistical visualizations.
|
|
5
|
+
|
|
6
|
+
Classes
|
|
7
|
+
-------
|
|
8
|
+
GridConfig
|
|
9
|
+
Dataclass holding all styling parameters for a grid plane.
|
|
10
|
+
|
|
11
|
+
GridPlane3D
|
|
12
|
+
A single richly-styled grid plane (XY, XZ, or YZ) with:
|
|
13
|
+
- Major/minor line subdivision
|
|
14
|
+
- Depth-fade along grid edges
|
|
15
|
+
- Zero-line emphasis
|
|
16
|
+
- Subtle fill behind the plane
|
|
17
|
+
- Animated sequential build-in
|
|
18
|
+
|
|
19
|
+
GridBoundingBox3D
|
|
20
|
+
A full wireframe bounding box around a 3D data region,
|
|
21
|
+
with optional corner tick marks and face-diagonal guides.
|
|
22
|
+
|
|
23
|
+
FloatingGrid3D
|
|
24
|
+
A detached grid backdrop not anchored to any axis system.
|
|
25
|
+
Useful as a chart background or floor plane.
|
|
26
|
+
|
|
27
|
+
FullGrid3D
|
|
28
|
+
Composite of all three axis-aligned GridPlane3D objects,
|
|
29
|
+
with a unified colour theme and synchronized animations.
|
|
30
|
+
|
|
31
|
+
BillboardLabel3D
|
|
32
|
+
A text label that always rotates to face the active camera,
|
|
33
|
+
used for axis tick labels in 3D.
|
|
34
|
+
|
|
35
|
+
GridSnapHelper
|
|
36
|
+
Utility (non-rendered) that maps data values to snapped
|
|
37
|
+
grid positions, ensuring plotted objects land exactly on
|
|
38
|
+
grid intersections.
|
|
39
|
+
|
|
40
|
+
Usage example
|
|
41
|
+
-------------
|
|
42
|
+
from manim import *
|
|
43
|
+
from statanim.axes.grid3d import FullGrid3D, GridConfig
|
|
44
|
+
|
|
45
|
+
class Demo(ThreeDScene):
|
|
46
|
+
def construct(self):
|
|
47
|
+
self.set_camera_orientation(phi=65*DEGREES, theta=-45*DEGREES)
|
|
48
|
+
|
|
49
|
+
grid = FullGrid3D(
|
|
50
|
+
x_range=[-4, 4, 1],
|
|
51
|
+
y_range=[-4, 4, 1],
|
|
52
|
+
z_range=[0, 6, 1],
|
|
53
|
+
minor_subdivisions=4,
|
|
54
|
+
)
|
|
55
|
+
self.play(grid.animate_build(lag=0.03))
|
|
56
|
+
self.wait()
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
from __future__ import annotations
|
|
60
|
+
|
|
61
|
+
from dataclasses import dataclass, field
|
|
62
|
+
from typing import Sequence, Tuple, Optional, Callable
|
|
63
|
+
import numpy as np
|
|
64
|
+
|
|
65
|
+
from manim import (
|
|
66
|
+
VGroup, VMobject, Line, DashedLine, Rectangle, Polygon,
|
|
67
|
+
Text, MathTex, DecimalNumber,
|
|
68
|
+
ThreeDScene, always_rotate,
|
|
69
|
+
DEGREES, PI, TAU,
|
|
70
|
+
RIGHT, LEFT, UP, DOWN, OUT, IN,
|
|
71
|
+
WHITE, BLACK, GRAY, BLUE, GREEN, RED, YELLOW,
|
|
72
|
+
ORIGIN, X_AXIS, Y_AXIS, Z_AXIS,
|
|
73
|
+
interpolate_color, color_to_rgba, rgba_to_color,
|
|
74
|
+
ManimColor, color_to_rgb,
|
|
75
|
+
AnimationGroup, LaggedStart, Create, FadeIn, DrawBorderThenFill,
|
|
76
|
+
Scene,
|
|
77
|
+
config,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# ---------------------------------------------------------------------------
|
|
81
|
+
# Internal colour helpers
|
|
82
|
+
# ---------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
def _with_opacity(color: ManimColor, opacity: float) -> ManimColor:
|
|
85
|
+
"""Return *color* with its opacity replaced by *opacity* (0–1)."""
|
|
86
|
+
r, g, b = color_to_rgb(color)
|
|
87
|
+
return rgba_to_color([r, g, b, opacity])
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _fade_color(
|
|
91
|
+
color: ManimColor,
|
|
92
|
+
base_opacity: float,
|
|
93
|
+
fade_factor: float,
|
|
94
|
+
) -> ManimColor:
|
|
95
|
+
"""Multiply a colour's opacity by *fade_factor* (0–1)."""
|
|
96
|
+
return _with_opacity(color, base_opacity * fade_factor)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# ---------------------------------------------------------------------------
|
|
100
|
+
# GridConfig
|
|
101
|
+
# ---------------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
@dataclass
|
|
104
|
+
class GridConfig:
|
|
105
|
+
"""All visual parameters for a single grid plane.
|
|
106
|
+
|
|
107
|
+
Attributes
|
|
108
|
+
----------
|
|
109
|
+
major_color : ManimColor
|
|
110
|
+
Colour of major grid lines (multiples of the step).
|
|
111
|
+
minor_color : ManimColor
|
|
112
|
+
Colour of minor (subdivision) grid lines.
|
|
113
|
+
zero_color : ManimColor
|
|
114
|
+
Colour of the zero-crossing lines on this plane.
|
|
115
|
+
fill_color : ManimColor
|
|
116
|
+
Background fill of the plane rectangle.
|
|
117
|
+
major_stroke_width : float
|
|
118
|
+
Stroke width for major grid lines.
|
|
119
|
+
minor_stroke_width : float
|
|
120
|
+
Stroke width for minor (subdivision) grid lines.
|
|
121
|
+
zero_stroke_width : float
|
|
122
|
+
Stroke width for the zero-crossing lines.
|
|
123
|
+
major_opacity : float
|
|
124
|
+
Opacity for major grid lines (before depth-fade is applied).
|
|
125
|
+
minor_opacity : float
|
|
126
|
+
Opacity for minor grid lines.
|
|
127
|
+
fill_opacity : float
|
|
128
|
+
Opacity of the background fill rectangle.
|
|
129
|
+
edge_fade : bool
|
|
130
|
+
If True, grid lines fade to transparent near the plane edges.
|
|
131
|
+
edge_fade_fraction : float
|
|
132
|
+
Fraction of the plane width/height over which the edge fade
|
|
133
|
+
is applied (0 = no fade zone, 0.2 = fade over 20% near edges).
|
|
134
|
+
dashed_minor : bool
|
|
135
|
+
If True, minor grid lines are drawn as dashed lines.
|
|
136
|
+
dash_length : float
|
|
137
|
+
Length of each dash segment for dashed minor lines.
|
|
138
|
+
dash_ratio : float
|
|
139
|
+
Ratio of dash to gap for dashed minor lines (0–1).
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
major_color: ManimColor = BLUE
|
|
143
|
+
minor_color: ManimColor = BLUE
|
|
144
|
+
zero_color: ManimColor = WHITE
|
|
145
|
+
fill_color: ManimColor = BLUE
|
|
146
|
+
|
|
147
|
+
major_stroke_width: float = 1.0
|
|
148
|
+
minor_stroke_width: float = 0.4
|
|
149
|
+
zero_stroke_width: float = 2.0
|
|
150
|
+
|
|
151
|
+
major_opacity: float = 0.45
|
|
152
|
+
minor_opacity: float = 0.18
|
|
153
|
+
zero_opacity: float = 0.70
|
|
154
|
+
fill_opacity: float = 0.04
|
|
155
|
+
|
|
156
|
+
edge_fade: bool = True
|
|
157
|
+
edge_fade_fraction: float = 0.12
|
|
158
|
+
|
|
159
|
+
dashed_minor: bool = False
|
|
160
|
+
dash_length: float = 0.05
|
|
161
|
+
dash_ratio: float = 0.5
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# Default theme presets
|
|
165
|
+
|
|
166
|
+
DARK_GRID = GridConfig(
|
|
167
|
+
major_color=ManimColor("#4A90D9"),
|
|
168
|
+
minor_color=ManimColor("#3A70A9"),
|
|
169
|
+
zero_color=ManimColor("#88BBFF"),
|
|
170
|
+
fill_color=ManimColor("#1A2A4A"),
|
|
171
|
+
major_opacity=0.50,
|
|
172
|
+
minor_opacity=0.20,
|
|
173
|
+
zero_opacity=0.80,
|
|
174
|
+
fill_opacity=0.06,
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
LIGHT_GRID = GridConfig(
|
|
178
|
+
major_color=ManimColor("#607080"),
|
|
179
|
+
minor_color=ManimColor("#9AABB8"),
|
|
180
|
+
zero_color=ManimColor("#2244AA"),
|
|
181
|
+
fill_color=ManimColor("#D0DCE8"),
|
|
182
|
+
major_opacity=0.40,
|
|
183
|
+
minor_opacity=0.18,
|
|
184
|
+
zero_opacity=0.65,
|
|
185
|
+
fill_opacity=0.08,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
STATS_GRID = GridConfig(
|
|
189
|
+
major_color=ManimColor("#5B7FA6"),
|
|
190
|
+
minor_color=ManimColor("#3D5A78"),
|
|
191
|
+
zero_color=ManimColor("#E0AA40"),
|
|
192
|
+
fill_color=ManimColor("#0E1D2E"),
|
|
193
|
+
major_stroke_width=1.2,
|
|
194
|
+
minor_stroke_width=0.5,
|
|
195
|
+
zero_stroke_width=2.4,
|
|
196
|
+
major_opacity=0.55,
|
|
197
|
+
minor_opacity=0.22,
|
|
198
|
+
zero_opacity=0.85,
|
|
199
|
+
fill_opacity=0.07,
|
|
200
|
+
edge_fade=True,
|
|
201
|
+
edge_fade_fraction=0.15,
|
|
202
|
+
dashed_minor=True,
|
|
203
|
+
dash_length=0.06,
|
|
204
|
+
dash_ratio=0.45,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
# ---------------------------------------------------------------------------
|
|
209
|
+
# GridPlane3D
|
|
210
|
+
# ---------------------------------------------------------------------------
|
|
211
|
+
|
|
212
|
+
class GridPlane3D(VGroup):
|
|
213
|
+
"""A richly styled single grid plane for 3D scenes.
|
|
214
|
+
|
|
215
|
+
Parameters
|
|
216
|
+
----------
|
|
217
|
+
axis_u : np.ndarray
|
|
218
|
+
Direction of the first (horizontal) axis on the plane.
|
|
219
|
+
Typically RIGHT, UP, or OUT.
|
|
220
|
+
axis_v : np.ndarray
|
|
221
|
+
Direction of the second (vertical) axis on the plane.
|
|
222
|
+
u_range : (float, float, float)
|
|
223
|
+
(min, max, step) for the u-axis grid lines.
|
|
224
|
+
v_range : (float, float, float)
|
|
225
|
+
(min, max, step) for the v-axis grid lines.
|
|
226
|
+
minor_subdivisions : int
|
|
227
|
+
Number of minor divisions per major step (0 = no minors).
|
|
228
|
+
origin : np.ndarray
|
|
229
|
+
3D point where u=0, v=0 maps.
|
|
230
|
+
config : GridConfig
|
|
231
|
+
Visual styling config. If *None*, uses ``STATS_GRID``.
|
|
232
|
+
label_u_values : bool
|
|
233
|
+
Whether to add tick-value labels along the u-axis edge.
|
|
234
|
+
label_v_values : bool
|
|
235
|
+
Whether to add tick-value labels along the v-axis edge.
|
|
236
|
+
label_font_size : int
|
|
237
|
+
Font size for tick labels.
|
|
238
|
+
num_decimal_places : int
|
|
239
|
+
Decimal places shown in tick labels.
|
|
240
|
+
|
|
241
|
+
Notes
|
|
242
|
+
-----
|
|
243
|
+
All grid lines are built as individual ``Line`` / ``DashedLine``
|
|
244
|
+
objects so that they can be animated independently (e.g. drawn
|
|
245
|
+
one at a time with ``LaggedStart``).
|
|
246
|
+
"""
|
|
247
|
+
|
|
248
|
+
def __init__(
|
|
249
|
+
self,
|
|
250
|
+
axis_u: np.ndarray = RIGHT,
|
|
251
|
+
axis_v: np.ndarray = UP,
|
|
252
|
+
u_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
253
|
+
v_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
254
|
+
minor_subdivisions: int = 4,
|
|
255
|
+
origin: np.ndarray = ORIGIN,
|
|
256
|
+
config: Optional[GridConfig] = None,
|
|
257
|
+
label_u_values: bool = False,
|
|
258
|
+
label_v_values: bool = False,
|
|
259
|
+
label_font_size: int = 18,
|
|
260
|
+
num_decimal_places: int = 0,
|
|
261
|
+
**kwargs,
|
|
262
|
+
):
|
|
263
|
+
super().__init__(**kwargs)
|
|
264
|
+
|
|
265
|
+
self.cfg = config if config is not None else STATS_GRID
|
|
266
|
+
self.axis_u = np.array(axis_u, dtype=float)
|
|
267
|
+
self.axis_v = np.array(axis_v, dtype=float)
|
|
268
|
+
self.u_range = u_range
|
|
269
|
+
self.v_range = v_range
|
|
270
|
+
self.minor_subdivisions = minor_subdivisions
|
|
271
|
+
self.origin = np.array(origin, dtype=float)
|
|
272
|
+
|
|
273
|
+
# Sub-groups (public so callers can animate selectively)
|
|
274
|
+
self.fill_plane = VGroup()
|
|
275
|
+
self.zero_lines = VGroup()
|
|
276
|
+
self.major_lines = VGroup()
|
|
277
|
+
self.minor_lines = VGroup()
|
|
278
|
+
self.tick_labels = VGroup()
|
|
279
|
+
|
|
280
|
+
self._build_fill()
|
|
281
|
+
self._build_grid_lines()
|
|
282
|
+
if label_u_values or label_v_values:
|
|
283
|
+
self._build_tick_labels(
|
|
284
|
+
label_u=label_u_values,
|
|
285
|
+
label_v=label_v_values,
|
|
286
|
+
font_size=label_font_size,
|
|
287
|
+
decimals=num_decimal_places,
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
self.add(
|
|
291
|
+
self.fill_plane,
|
|
292
|
+
self.minor_lines,
|
|
293
|
+
self.major_lines,
|
|
294
|
+
self.zero_lines,
|
|
295
|
+
self.tick_labels,
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
# ------------------------------------------------------------------
|
|
299
|
+
# Private helpers
|
|
300
|
+
# ------------------------------------------------------------------
|
|
301
|
+
|
|
302
|
+
def _uv_to_3d(self, u: float, v: float) -> np.ndarray:
|
|
303
|
+
return self.origin + u * self.axis_u + v * self.axis_v
|
|
304
|
+
|
|
305
|
+
def _edge_fade_opacity(
|
|
306
|
+
self,
|
|
307
|
+
t: float,
|
|
308
|
+
t_min: float,
|
|
309
|
+
t_max: float,
|
|
310
|
+
base_opacity: float,
|
|
311
|
+
) -> float:
|
|
312
|
+
"""Compute fade-adjusted opacity for a grid line at parameter *t*.
|
|
313
|
+
|
|
314
|
+
Lines near t_min or t_max fade toward zero. Lines in the
|
|
315
|
+
interior keep *base_opacity*.
|
|
316
|
+
"""
|
|
317
|
+
if not self.cfg.edge_fade:
|
|
318
|
+
return base_opacity
|
|
319
|
+
frac = self.cfg.edge_fade_fraction
|
|
320
|
+
t_range = t_max - t_min
|
|
321
|
+
if t_range == 0:
|
|
322
|
+
return base_opacity
|
|
323
|
+
rel = (t - t_min) / t_range # 0 … 1
|
|
324
|
+
# Smooth step that is 0 at edges, 1 in centre
|
|
325
|
+
lo_fade = np.clip(rel / frac, 0, 1)
|
|
326
|
+
hi_fade = np.clip((1 - rel) / frac, 0, 1)
|
|
327
|
+
fade = min(lo_fade, hi_fade)
|
|
328
|
+
# Smooth: 3t²-2t³
|
|
329
|
+
smooth = fade * fade * (3 - 2 * fade)
|
|
330
|
+
return base_opacity * smooth
|
|
331
|
+
|
|
332
|
+
def _make_line(
|
|
333
|
+
self,
|
|
334
|
+
start: np.ndarray,
|
|
335
|
+
end: np.ndarray,
|
|
336
|
+
color: ManimColor,
|
|
337
|
+
opacity: float,
|
|
338
|
+
stroke_width: float,
|
|
339
|
+
dashed: bool = False,
|
|
340
|
+
) -> VMobject:
|
|
341
|
+
if dashed:
|
|
342
|
+
ln = DashedLine(
|
|
343
|
+
start, end,
|
|
344
|
+
dash_length=self.cfg.dash_length,
|
|
345
|
+
dashed_ratio=self.cfg.dash_ratio,
|
|
346
|
+
color=_with_opacity(color, opacity),
|
|
347
|
+
stroke_width=stroke_width,
|
|
348
|
+
)
|
|
349
|
+
else:
|
|
350
|
+
ln = Line(
|
|
351
|
+
start, end,
|
|
352
|
+
color=_with_opacity(color, opacity),
|
|
353
|
+
stroke_width=stroke_width,
|
|
354
|
+
)
|
|
355
|
+
return ln
|
|
356
|
+
|
|
357
|
+
def _build_fill(self) -> None:
|
|
358
|
+
u_min, u_max, _ = self.u_range
|
|
359
|
+
v_min, v_max, _ = self.v_range
|
|
360
|
+
corners = [
|
|
361
|
+
self._uv_to_3d(u_min, v_min),
|
|
362
|
+
self._uv_to_3d(u_max, v_min),
|
|
363
|
+
self._uv_to_3d(u_max, v_max),
|
|
364
|
+
self._uv_to_3d(u_min, v_max),
|
|
365
|
+
]
|
|
366
|
+
rect = Polygon(
|
|
367
|
+
*corners,
|
|
368
|
+
fill_color=_with_opacity(self.cfg.fill_color, self.cfg.fill_opacity),
|
|
369
|
+
fill_opacity=1.0, # opacity already baked into colour
|
|
370
|
+
stroke_width=0,
|
|
371
|
+
)
|
|
372
|
+
self.fill_plane.add(rect)
|
|
373
|
+
|
|
374
|
+
def _build_grid_lines(self) -> None:
|
|
375
|
+
u_min, u_max, u_step = self.u_range
|
|
376
|
+
v_min, v_max, v_step = self.v_range
|
|
377
|
+
cfg = self.cfg
|
|
378
|
+
|
|
379
|
+
# --- lines parallel to axis_v (varying u) ---
|
|
380
|
+
self._add_lines_along_axis(
|
|
381
|
+
fixed_axis="u",
|
|
382
|
+
fixed_min=u_min,
|
|
383
|
+
fixed_max=u_max,
|
|
384
|
+
fixed_step=u_step,
|
|
385
|
+
other_min=v_min,
|
|
386
|
+
other_max=v_max,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
# --- lines parallel to axis_u (varying v) ---
|
|
390
|
+
self._add_lines_along_axis(
|
|
391
|
+
fixed_axis="v",
|
|
392
|
+
fixed_min=v_min,
|
|
393
|
+
fixed_max=v_max,
|
|
394
|
+
fixed_step=v_step,
|
|
395
|
+
other_min=u_min,
|
|
396
|
+
other_max=u_max,
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
def _add_lines_along_axis(
|
|
400
|
+
self,
|
|
401
|
+
fixed_axis: str, # "u" or "v"
|
|
402
|
+
fixed_min: float,
|
|
403
|
+
fixed_max: float,
|
|
404
|
+
fixed_step: float,
|
|
405
|
+
other_min: float,
|
|
406
|
+
other_max: float,
|
|
407
|
+
) -> None:
|
|
408
|
+
"""Add all major, minor, and zero lines sweeping across one axis."""
|
|
409
|
+
cfg = self.cfg
|
|
410
|
+
n_major = round((fixed_max - fixed_min) / fixed_step)
|
|
411
|
+
|
|
412
|
+
for i in range(n_major + 1):
|
|
413
|
+
t_major = fixed_min + i * fixed_step
|
|
414
|
+
is_zero = abs(t_major) < 1e-9
|
|
415
|
+
|
|
416
|
+
fade_op = self._edge_fade_opacity(
|
|
417
|
+
t_major, fixed_min, fixed_max,
|
|
418
|
+
cfg.zero_opacity if is_zero else cfg.major_opacity,
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
if fixed_axis == "u":
|
|
422
|
+
start = self._uv_to_3d(t_major, other_min)
|
|
423
|
+
end = self._uv_to_3d(t_major, other_max)
|
|
424
|
+
else:
|
|
425
|
+
start = self._uv_to_3d(other_min, t_major)
|
|
426
|
+
end = self._uv_to_3d(other_max, t_major)
|
|
427
|
+
|
|
428
|
+
if is_zero:
|
|
429
|
+
ln = self._make_line(
|
|
430
|
+
start, end,
|
|
431
|
+
cfg.zero_color, fade_op,
|
|
432
|
+
cfg.zero_stroke_width, dashed=False,
|
|
433
|
+
)
|
|
434
|
+
self.zero_lines.add(ln)
|
|
435
|
+
else:
|
|
436
|
+
ln = self._make_line(
|
|
437
|
+
start, end,
|
|
438
|
+
cfg.major_color, fade_op,
|
|
439
|
+
cfg.major_stroke_width, dashed=False,
|
|
440
|
+
)
|
|
441
|
+
self.major_lines.add(ln)
|
|
442
|
+
|
|
443
|
+
# Minor lines between this major tick and the next
|
|
444
|
+
if self.minor_subdivisions > 0 and i < n_major:
|
|
445
|
+
minor_step = fixed_step / self.minor_subdivisions
|
|
446
|
+
for j in range(1, self.minor_subdivisions):
|
|
447
|
+
t_minor = t_major + j * minor_step
|
|
448
|
+
fade_minor = self._edge_fade_opacity(
|
|
449
|
+
t_minor, fixed_min, fixed_max,
|
|
450
|
+
cfg.minor_opacity,
|
|
451
|
+
)
|
|
452
|
+
if fixed_axis == "u":
|
|
453
|
+
ms = self._uv_to_3d(t_minor, other_min)
|
|
454
|
+
me = self._uv_to_3d(t_minor, other_max)
|
|
455
|
+
else:
|
|
456
|
+
ms = self._uv_to_3d(other_min, t_minor)
|
|
457
|
+
me = self._uv_to_3d(other_max, t_minor)
|
|
458
|
+
|
|
459
|
+
mln = self._make_line(
|
|
460
|
+
ms, me,
|
|
461
|
+
cfg.minor_color, fade_minor,
|
|
462
|
+
cfg.minor_stroke_width, dashed=cfg.dashed_minor,
|
|
463
|
+
)
|
|
464
|
+
self.minor_lines.add(mln)
|
|
465
|
+
|
|
466
|
+
def _build_tick_labels(
|
|
467
|
+
self,
|
|
468
|
+
label_u: bool,
|
|
469
|
+
label_v: bool,
|
|
470
|
+
font_size: int,
|
|
471
|
+
decimals: int,
|
|
472
|
+
) -> None:
|
|
473
|
+
"""Add numeric tick labels along the outer edge of the plane."""
|
|
474
|
+
u_min, u_max, u_step = self.u_range
|
|
475
|
+
v_min, v_max, v_step = self.v_range
|
|
476
|
+
fmt = f"{{:.{decimals}f}}"
|
|
477
|
+
|
|
478
|
+
label_offset_scale = 0.25 # how far outside the plane edge
|
|
479
|
+
|
|
480
|
+
if label_u:
|
|
481
|
+
n = round((u_max - u_min) / u_step)
|
|
482
|
+
for i in range(n + 1):
|
|
483
|
+
val = u_min + i * u_step
|
|
484
|
+
pos = (
|
|
485
|
+
self._uv_to_3d(val, v_min)
|
|
486
|
+
- label_offset_scale * self.axis_v
|
|
487
|
+
)
|
|
488
|
+
lbl = Text(
|
|
489
|
+
fmt.format(val),
|
|
490
|
+
font_size=font_size,
|
|
491
|
+
color=_with_opacity(self.cfg.major_color, 0.70),
|
|
492
|
+
)
|
|
493
|
+
lbl.move_to(pos)
|
|
494
|
+
self.tick_labels.add(lbl)
|
|
495
|
+
|
|
496
|
+
if label_v:
|
|
497
|
+
n = round((v_max - v_min) / v_step)
|
|
498
|
+
for i in range(n + 1):
|
|
499
|
+
val = v_min + i * v_step
|
|
500
|
+
pos = (
|
|
501
|
+
self._uv_to_3d(u_min, val)
|
|
502
|
+
- label_offset_scale * self.axis_u
|
|
503
|
+
)
|
|
504
|
+
lbl = Text(
|
|
505
|
+
fmt.format(val),
|
|
506
|
+
font_size=font_size,
|
|
507
|
+
color=_with_opacity(self.cfg.major_color, 0.70),
|
|
508
|
+
)
|
|
509
|
+
lbl.move_to(pos)
|
|
510
|
+
self.tick_labels.add(lbl)
|
|
511
|
+
|
|
512
|
+
# ------------------------------------------------------------------
|
|
513
|
+
# Public API
|
|
514
|
+
# ------------------------------------------------------------------
|
|
515
|
+
|
|
516
|
+
def animate_build(
|
|
517
|
+
self,
|
|
518
|
+
lag: float = 0.02,
|
|
519
|
+
run_time_per_line: float = 0.4,
|
|
520
|
+
) -> AnimationGroup:
|
|
521
|
+
"""Return an animation that draws the plane line-by-line.
|
|
522
|
+
|
|
523
|
+
The build order is: fill → minor lines → major lines → zero lines.
|
|
524
|
+
Tick labels fade in at the end.
|
|
525
|
+
|
|
526
|
+
Parameters
|
|
527
|
+
----------
|
|
528
|
+
lag : float
|
|
529
|
+
Seconds between the start of consecutive line animations.
|
|
530
|
+
run_time_per_line : float
|
|
531
|
+
Duration of each individual line's ``Create`` animation.
|
|
532
|
+
"""
|
|
533
|
+
anims = []
|
|
534
|
+
|
|
535
|
+
anims.append(FadeIn(self.fill_plane, run_time=0.3))
|
|
536
|
+
|
|
537
|
+
line_groups = [self.minor_lines, self.major_lines, self.zero_lines]
|
|
538
|
+
for group in line_groups:
|
|
539
|
+
for ln in group:
|
|
540
|
+
anims.append(Create(ln, run_time=run_time_per_line))
|
|
541
|
+
|
|
542
|
+
if len(self.tick_labels) > 0:
|
|
543
|
+
anims.append(FadeIn(self.tick_labels, run_time=0.5))
|
|
544
|
+
|
|
545
|
+
return LaggedStart(*anims, lag_ratio=lag)
|
|
546
|
+
|
|
547
|
+
def set_opacity(self, opacity: float) -> "GridPlane3D":
|
|
548
|
+
"""Scale all line opacities by *opacity* (0–1). Returns self."""
|
|
549
|
+
for group in (self.major_lines, self.minor_lines, self.zero_lines):
|
|
550
|
+
for ln in group:
|
|
551
|
+
ln.set_stroke(opacity=opacity)
|
|
552
|
+
self.fill_plane.set_fill(opacity=self.cfg.fill_opacity * opacity)
|
|
553
|
+
return self
|
|
554
|
+
|
|
555
|
+
def highlight_line(
|
|
556
|
+
self,
|
|
557
|
+
axis: str,
|
|
558
|
+
value: float,
|
|
559
|
+
color: ManimColor = YELLOW,
|
|
560
|
+
stroke_width: float = 3.0,
|
|
561
|
+
opacity: float = 1.0,
|
|
562
|
+
) -> Optional[VMobject]:
|
|
563
|
+
"""Find the major grid line nearest to *value* on *axis* and
|
|
564
|
+
temporarily highlight it. Returns the line (caller can animate).
|
|
565
|
+
|
|
566
|
+
Parameters
|
|
567
|
+
----------
|
|
568
|
+
axis : str
|
|
569
|
+
``"u"`` or ``"v"`` – which axis family to search.
|
|
570
|
+
value : float
|
|
571
|
+
The data value whose nearest grid line to highlight.
|
|
572
|
+
"""
|
|
573
|
+
u_min, u_max, u_step = self.u_range
|
|
574
|
+
v_min, v_max, v_step = self.v_range
|
|
575
|
+
|
|
576
|
+
if axis == "u":
|
|
577
|
+
snap = round((value - u_min) / u_step) * u_step + u_min
|
|
578
|
+
start = self._uv_to_3d(snap, v_min)
|
|
579
|
+
end = self._uv_to_3d(snap, v_max)
|
|
580
|
+
else:
|
|
581
|
+
snap = round((value - v_min) / v_step) * v_step + v_min
|
|
582
|
+
start = self._uv_to_3d(u_min, snap)
|
|
583
|
+
end = self._uv_to_3d(u_max, snap)
|
|
584
|
+
|
|
585
|
+
ln = Line(
|
|
586
|
+
start, end,
|
|
587
|
+
color=_with_opacity(color, opacity),
|
|
588
|
+
stroke_width=stroke_width,
|
|
589
|
+
)
|
|
590
|
+
self.add(ln)
|
|
591
|
+
return ln
|
|
592
|
+
|
|
593
|
+
def get_snap_position(self, u_val: float, v_val: float) -> np.ndarray:
|
|
594
|
+
"""Return the 3D position of the grid point nearest to (u_val, v_val)."""
|
|
595
|
+
u_min, _, u_step = self.u_range
|
|
596
|
+
v_min, _, v_step = self.v_range
|
|
597
|
+
u_snapped = round((u_val - u_min) / u_step) * u_step + u_min
|
|
598
|
+
v_snapped = round((v_val - v_min) / v_step) * v_step + v_min
|
|
599
|
+
return self._uv_to_3d(u_snapped, v_snapped)
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
# ---------------------------------------------------------------------------
|
|
603
|
+
# GridBoundingBox3D
|
|
604
|
+
# ---------------------------------------------------------------------------
|
|
605
|
+
|
|
606
|
+
class GridBoundingBox3D(VGroup):
|
|
607
|
+
"""A wireframe bounding box around a 3D data region.
|
|
608
|
+
|
|
609
|
+
Draws the 12 edges of a rectangular prism, with optional corner
|
|
610
|
+
tick marks and face-diagonal guides (useful for showing axes).
|
|
611
|
+
|
|
612
|
+
Parameters
|
|
613
|
+
----------
|
|
614
|
+
x_range, y_range, z_range : (float, float)
|
|
615
|
+
(min, max) extents along each axis.
|
|
616
|
+
color : ManimColor
|
|
617
|
+
Edge colour.
|
|
618
|
+
stroke_width : float
|
|
619
|
+
Edge thickness.
|
|
620
|
+
opacity : float
|
|
621
|
+
Edge opacity.
|
|
622
|
+
corner_ticks : bool
|
|
623
|
+
Whether to draw small tick marks at each of the 8 corners.
|
|
624
|
+
tick_length : float
|
|
625
|
+
Length of each corner tick.
|
|
626
|
+
face_diagonals : bool
|
|
627
|
+
If True, draw light diagonal guides on each face.
|
|
628
|
+
diagonal_opacity : float
|
|
629
|
+
Opacity of the face diagonals.
|
|
630
|
+
"""
|
|
631
|
+
|
|
632
|
+
def __init__(
|
|
633
|
+
self,
|
|
634
|
+
x_range: Tuple[float, float] = (-3, 3),
|
|
635
|
+
y_range: Tuple[float, float] = (-3, 3),
|
|
636
|
+
z_range: Tuple[float, float] = (0, 4),
|
|
637
|
+
color: ManimColor = ManimColor("#5B7FA6"),
|
|
638
|
+
stroke_width: float = 1.2,
|
|
639
|
+
opacity: float = 0.60,
|
|
640
|
+
corner_ticks: bool = True,
|
|
641
|
+
tick_length: float = 0.12,
|
|
642
|
+
face_diagonals: bool = False,
|
|
643
|
+
diagonal_opacity: float = 0.15,
|
|
644
|
+
**kwargs,
|
|
645
|
+
):
|
|
646
|
+
super().__init__(**kwargs)
|
|
647
|
+
|
|
648
|
+
x0, x1 = x_range
|
|
649
|
+
y0, y1 = y_range
|
|
650
|
+
z0, z1 = z_range
|
|
651
|
+
|
|
652
|
+
# 8 corners of the bounding box
|
|
653
|
+
corners = np.array([
|
|
654
|
+
[x0, y0, z0], [x1, y0, z0],
|
|
655
|
+
[x1, y1, z0], [x0, y1, z0],
|
|
656
|
+
[x0, y0, z1], [x1, y0, z1],
|
|
657
|
+
[x1, y1, z1], [x0, y1, z1],
|
|
658
|
+
])
|
|
659
|
+
|
|
660
|
+
# 12 edges (pairs of corner indices)
|
|
661
|
+
edges = [
|
|
662
|
+
(0, 1), (1, 2), (2, 3), (3, 0), # bottom face
|
|
663
|
+
(4, 5), (5, 6), (6, 7), (7, 4), # top face
|
|
664
|
+
(0, 4), (1, 5), (2, 6), (3, 7), # vertical pillars
|
|
665
|
+
]
|
|
666
|
+
|
|
667
|
+
edge_color = _with_opacity(color, opacity)
|
|
668
|
+
self.edges = VGroup()
|
|
669
|
+
for a, b in edges:
|
|
670
|
+
ln = Line(
|
|
671
|
+
corners[a], corners[b],
|
|
672
|
+
color=edge_color,
|
|
673
|
+
stroke_width=stroke_width,
|
|
674
|
+
)
|
|
675
|
+
self.edges.add(ln)
|
|
676
|
+
self.add(self.edges)
|
|
677
|
+
|
|
678
|
+
# Corner tick marks
|
|
679
|
+
if corner_ticks:
|
|
680
|
+
self.ticks = VGroup()
|
|
681
|
+
tick_dirs = [
|
|
682
|
+
np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1]),
|
|
683
|
+
]
|
|
684
|
+
tick_color = _with_opacity(color, min(opacity * 1.3, 1.0))
|
|
685
|
+
for c in corners:
|
|
686
|
+
for d in tick_dirs:
|
|
687
|
+
t = Line(
|
|
688
|
+
c - d * tick_length / 2,
|
|
689
|
+
c + d * tick_length / 2,
|
|
690
|
+
color=tick_color,
|
|
691
|
+
stroke_width=stroke_width * 1.5,
|
|
692
|
+
)
|
|
693
|
+
self.ticks.add(t)
|
|
694
|
+
self.add(self.ticks)
|
|
695
|
+
|
|
696
|
+
# Face diagonal guides
|
|
697
|
+
if face_diagonals:
|
|
698
|
+
self.diagonals = VGroup()
|
|
699
|
+
diag_color = _with_opacity(color, diagonal_opacity)
|
|
700
|
+
face_pairs = [
|
|
701
|
+
(0, 6), (1, 7), # bottom-to-top diagonals across faces
|
|
702
|
+
(2, 4), (3, 5),
|
|
703
|
+
]
|
|
704
|
+
for a, b in face_pairs:
|
|
705
|
+
d = DashedLine(
|
|
706
|
+
corners[a], corners[b],
|
|
707
|
+
dash_length=0.08,
|
|
708
|
+
dashed_ratio=0.5,
|
|
709
|
+
color=diag_color,
|
|
710
|
+
stroke_width=0.5,
|
|
711
|
+
)
|
|
712
|
+
self.diagonals.add(d)
|
|
713
|
+
self.add(self.diagonals)
|
|
714
|
+
|
|
715
|
+
def animate_build(self, run_time: float = 1.5) -> AnimationGroup:
|
|
716
|
+
"""Draw all edges, then ticks, then diagonals."""
|
|
717
|
+
anims = [LaggedStart(
|
|
718
|
+
*[Create(e) for e in self.edges],
|
|
719
|
+
lag_ratio=0.06,
|
|
720
|
+
run_time=run_time,
|
|
721
|
+
)]
|
|
722
|
+
if hasattr(self, "ticks"):
|
|
723
|
+
anims.append(FadeIn(self.ticks, run_time=0.4))
|
|
724
|
+
if hasattr(self, "diagonals"):
|
|
725
|
+
anims.append(LaggedStart(
|
|
726
|
+
*[Create(d) for d in self.diagonals],
|
|
727
|
+
lag_ratio=0.08,
|
|
728
|
+
run_time=0.6,
|
|
729
|
+
))
|
|
730
|
+
return AnimationGroup(*anims, lag_ratio=0.15)
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
# ---------------------------------------------------------------------------
|
|
734
|
+
# FloatingGrid3D
|
|
735
|
+
# ---------------------------------------------------------------------------
|
|
736
|
+
|
|
737
|
+
class FloatingGrid3D(GridPlane3D):
|
|
738
|
+
"""A grid plane that lives anywhere in 3D space, not necessarily
|
|
739
|
+
through the origin.
|
|
740
|
+
|
|
741
|
+
This is a thin convenience wrapper over :class:`GridPlane3D` that
|
|
742
|
+
accepts a ``center`` keyword argument and pre-translates the origin.
|
|
743
|
+
|
|
744
|
+
Parameters
|
|
745
|
+
----------
|
|
746
|
+
center : np.ndarray
|
|
747
|
+
3D centre of the grid.
|
|
748
|
+
width : float
|
|
749
|
+
Full width of the grid (axis_u direction).
|
|
750
|
+
height : float
|
|
751
|
+
Full height of the grid (axis_v direction).
|
|
752
|
+
step : float
|
|
753
|
+
Major grid step in both directions.
|
|
754
|
+
minor_subdivisions : int
|
|
755
|
+
Minor subdivision count per major step.
|
|
756
|
+
axis_u, axis_v : np.ndarray
|
|
757
|
+
Plane basis vectors.
|
|
758
|
+
config : GridConfig
|
|
759
|
+
Visual config. Defaults to ``STATS_GRID``.
|
|
760
|
+
"""
|
|
761
|
+
|
|
762
|
+
def __init__(
|
|
763
|
+
self,
|
|
764
|
+
center: np.ndarray = ORIGIN,
|
|
765
|
+
width: float = 8.0,
|
|
766
|
+
height: float = 8.0,
|
|
767
|
+
step: float = 1.0,
|
|
768
|
+
minor_subdivisions: int = 4,
|
|
769
|
+
axis_u: np.ndarray = RIGHT,
|
|
770
|
+
axis_v: np.ndarray = UP,
|
|
771
|
+
config: Optional[GridConfig] = None,
|
|
772
|
+
**kwargs,
|
|
773
|
+
):
|
|
774
|
+
hw = width / 2
|
|
775
|
+
hh = height / 2
|
|
776
|
+
super().__init__(
|
|
777
|
+
axis_u=axis_u,
|
|
778
|
+
axis_v=axis_v,
|
|
779
|
+
u_range=(-hw, hw, step),
|
|
780
|
+
v_range=(-hh, hh, step),
|
|
781
|
+
minor_subdivisions=minor_subdivisions,
|
|
782
|
+
origin=np.array(center, dtype=float),
|
|
783
|
+
config=config,
|
|
784
|
+
**kwargs,
|
|
785
|
+
)
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
# ---------------------------------------------------------------------------
|
|
789
|
+
# FullGrid3D
|
|
790
|
+
# ---------------------------------------------------------------------------
|
|
791
|
+
|
|
792
|
+
class FullGrid3D(VGroup):
|
|
793
|
+
"""Three axis-aligned grid planes (XY, XZ, YZ) forming a full 3D grid.
|
|
794
|
+
|
|
795
|
+
Parameters
|
|
796
|
+
----------
|
|
797
|
+
x_range, y_range, z_range : (float, float, float)
|
|
798
|
+
(min, max, step) for each axis.
|
|
799
|
+
minor_subdivisions : int
|
|
800
|
+
Minor subdivisions per major step (applies to all planes).
|
|
801
|
+
config : GridConfig
|
|
802
|
+
Shared visual config. If *None*, uses ``STATS_GRID``.
|
|
803
|
+
show_xy : bool
|
|
804
|
+
Whether to include the XY (floor) plane.
|
|
805
|
+
show_xz : bool
|
|
806
|
+
Whether to include the XZ (back wall) plane.
|
|
807
|
+
show_yz : bool
|
|
808
|
+
Whether to include the YZ (side wall) plane.
|
|
809
|
+
label_axes : bool
|
|
810
|
+
Whether to add tick labels on each plane.
|
|
811
|
+
|
|
812
|
+
Attributes
|
|
813
|
+
----------
|
|
814
|
+
xy_plane : GridPlane3D
|
|
815
|
+
xz_plane : GridPlane3D
|
|
816
|
+
yz_plane : GridPlane3D
|
|
817
|
+
bounding_box : GridBoundingBox3D
|
|
818
|
+
"""
|
|
819
|
+
|
|
820
|
+
def __init__(
|
|
821
|
+
self,
|
|
822
|
+
x_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
823
|
+
y_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
824
|
+
z_range: Tuple[float, float, float] = (0, 5, 1),
|
|
825
|
+
minor_subdivisions: int = 4,
|
|
826
|
+
config: Optional[GridConfig] = None,
|
|
827
|
+
show_xy: bool = True,
|
|
828
|
+
show_xz: bool = True,
|
|
829
|
+
show_yz: bool = True,
|
|
830
|
+
show_bounding_box: bool = False,
|
|
831
|
+
label_axes: bool = False,
|
|
832
|
+
label_font_size: int = 18,
|
|
833
|
+
**kwargs,
|
|
834
|
+
):
|
|
835
|
+
super().__init__(**kwargs)
|
|
836
|
+
|
|
837
|
+
cfg = config if config is not None else STATS_GRID
|
|
838
|
+
x0, x1, xs = x_range
|
|
839
|
+
y0, y1, ys = y_range
|
|
840
|
+
z0, z1, zs = z_range
|
|
841
|
+
|
|
842
|
+
# XY plane (floor) — normal is Z
|
|
843
|
+
if show_xy:
|
|
844
|
+
xy_cfg = GridConfig(**cfg.__dict__) # copy
|
|
845
|
+
self.xy_plane = GridPlane3D(
|
|
846
|
+
axis_u=RIGHT,
|
|
847
|
+
axis_v=OUT, # Manim's OUT = +y direction (in 3D scene)
|
|
848
|
+
u_range=x_range,
|
|
849
|
+
v_range=y_range,
|
|
850
|
+
minor_subdivisions=minor_subdivisions,
|
|
851
|
+
origin=np.array([0, 0, z0]),
|
|
852
|
+
config=xy_cfg,
|
|
853
|
+
label_u_values=label_axes,
|
|
854
|
+
label_v_values=label_axes,
|
|
855
|
+
label_font_size=label_font_size,
|
|
856
|
+
)
|
|
857
|
+
self.add(self.xy_plane)
|
|
858
|
+
|
|
859
|
+
# XZ plane (back wall) — normal is Y
|
|
860
|
+
if show_xz:
|
|
861
|
+
xz_cfg = GridConfig(**cfg.__dict__)
|
|
862
|
+
# Make the xz plane slightly more transparent to not overwhelm
|
|
863
|
+
xz_cfg.major_opacity = cfg.major_opacity * 0.75
|
|
864
|
+
xz_cfg.minor_opacity = cfg.minor_opacity * 0.75
|
|
865
|
+
xz_cfg.fill_opacity = cfg.fill_opacity * 0.5
|
|
866
|
+
self.xz_plane = GridPlane3D(
|
|
867
|
+
axis_u=RIGHT,
|
|
868
|
+
axis_v=UP,
|
|
869
|
+
u_range=x_range,
|
|
870
|
+
v_range=z_range,
|
|
871
|
+
minor_subdivisions=minor_subdivisions,
|
|
872
|
+
origin=np.array([0, y1, 0]),
|
|
873
|
+
config=xz_cfg,
|
|
874
|
+
label_u_values=label_axes,
|
|
875
|
+
label_v_values=label_axes,
|
|
876
|
+
label_font_size=label_font_size,
|
|
877
|
+
)
|
|
878
|
+
self.add(self.xz_plane)
|
|
879
|
+
|
|
880
|
+
# YZ plane (side wall) — normal is X
|
|
881
|
+
if show_yz:
|
|
882
|
+
yz_cfg = GridConfig(**cfg.__dict__)
|
|
883
|
+
yz_cfg.major_opacity = cfg.major_opacity * 0.65
|
|
884
|
+
yz_cfg.minor_opacity = cfg.minor_opacity * 0.65
|
|
885
|
+
yz_cfg.fill_opacity = cfg.fill_opacity * 0.40
|
|
886
|
+
self.yz_plane = GridPlane3D(
|
|
887
|
+
axis_u=OUT, # Manim's OUT = +y
|
|
888
|
+
axis_v=UP,
|
|
889
|
+
u_range=y_range,
|
|
890
|
+
v_range=z_range,
|
|
891
|
+
minor_subdivisions=minor_subdivisions,
|
|
892
|
+
origin=np.array([x0, 0, 0]),
|
|
893
|
+
config=yz_cfg,
|
|
894
|
+
label_u_values=label_axes,
|
|
895
|
+
label_v_values=label_axes,
|
|
896
|
+
label_font_size=label_font_size,
|
|
897
|
+
)
|
|
898
|
+
self.add(self.yz_plane)
|
|
899
|
+
|
|
900
|
+
# Optional bounding box
|
|
901
|
+
if show_bounding_box:
|
|
902
|
+
self.bounding_box = GridBoundingBox3D(
|
|
903
|
+
x_range=(x0, x1),
|
|
904
|
+
y_range=(y0, y1),
|
|
905
|
+
z_range=(z0, z1),
|
|
906
|
+
color=cfg.major_color,
|
|
907
|
+
opacity=0.50,
|
|
908
|
+
)
|
|
909
|
+
self.add(self.bounding_box)
|
|
910
|
+
|
|
911
|
+
# ------------------------------------------------------------------
|
|
912
|
+
|
|
913
|
+
def animate_build(
|
|
914
|
+
self,
|
|
915
|
+
order: Sequence[str] = ("xy", "xz", "yz", "bounding_box"),
|
|
916
|
+
lag_between_planes: float = 0.2,
|
|
917
|
+
line_lag: float = 0.015,
|
|
918
|
+
) -> AnimationGroup:
|
|
919
|
+
"""Animate each plane building in sequence.
|
|
920
|
+
|
|
921
|
+
Parameters
|
|
922
|
+
----------
|
|
923
|
+
order : sequence of str
|
|
924
|
+
Which planes to animate and in what order.
|
|
925
|
+
Valid values: ``"xy"``, ``"xz"``, ``"yz"``, ``"bounding_box"``.
|
|
926
|
+
lag_between_planes : float
|
|
927
|
+
Seconds between the start of each plane's animation.
|
|
928
|
+
line_lag : float
|
|
929
|
+
Seconds between individual lines within a plane.
|
|
930
|
+
"""
|
|
931
|
+
anims = []
|
|
932
|
+
for name in order:
|
|
933
|
+
plane = getattr(self, f"{name}_plane" if name != "bounding_box" else "bounding_box", None)
|
|
934
|
+
if plane is None:
|
|
935
|
+
continue
|
|
936
|
+
if hasattr(plane, "animate_build"):
|
|
937
|
+
anims.append(plane.animate_build(lag=line_lag))
|
|
938
|
+
|
|
939
|
+
return LaggedStart(*anims, lag_ratio=lag_between_planes)
|
|
940
|
+
|
|
941
|
+
def set_floor_opacity(self, opacity: float) -> "FullGrid3D":
|
|
942
|
+
"""Convenience: set XY plane opacity."""
|
|
943
|
+
if hasattr(self, "xy_plane"):
|
|
944
|
+
self.xy_plane.set_opacity(opacity)
|
|
945
|
+
return self
|
|
946
|
+
|
|
947
|
+
def highlight_axes(
|
|
948
|
+
self,
|
|
949
|
+
color: ManimColor = YELLOW,
|
|
950
|
+
stroke_width: float = 2.5,
|
|
951
|
+
) -> "FullGrid3D":
|
|
952
|
+
"""Draw thick zero-crossing lines on all planes to emphasise axes."""
|
|
953
|
+
for attr in ("xy_plane", "xz_plane", "yz_plane"):
|
|
954
|
+
plane: Optional[GridPlane3D] = getattr(self, attr, None)
|
|
955
|
+
if plane is None:
|
|
956
|
+
continue
|
|
957
|
+
# Re-style existing zero lines
|
|
958
|
+
for ln in plane.zero_lines:
|
|
959
|
+
ln.set_stroke(color=_with_opacity(color, 0.85), width=stroke_width)
|
|
960
|
+
return self
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
# ---------------------------------------------------------------------------
|
|
964
|
+
# BillboardLabel3D
|
|
965
|
+
# ---------------------------------------------------------------------------
|
|
966
|
+
|
|
967
|
+
class BillboardLabel3D(VGroup):
|
|
968
|
+
"""A 3D text label that is always rotated to face the camera.
|
|
969
|
+
|
|
970
|
+
Manim's ``ThreeDScene`` can auto-rotate labels via
|
|
971
|
+
``self.add_fixed_orientation_mobjects()``, but this class packages
|
|
972
|
+
the label + offset together so it can be positioned relative to a
|
|
973
|
+
grid tick.
|
|
974
|
+
|
|
975
|
+
Parameters
|
|
976
|
+
----------
|
|
977
|
+
text : str
|
|
978
|
+
Label text. Rendered via :class:`~manim.Text`.
|
|
979
|
+
position : np.ndarray
|
|
980
|
+
3D world position of the label's centre.
|
|
981
|
+
font_size : int
|
|
982
|
+
Font size.
|
|
983
|
+
color : ManimColor
|
|
984
|
+
Label colour.
|
|
985
|
+
scene : ThreeDScene
|
|
986
|
+
The scene in which this label lives. Needed to call
|
|
987
|
+
``add_fixed_orientation_mobjects``.
|
|
988
|
+
"""
|
|
989
|
+
|
|
990
|
+
def __init__(
|
|
991
|
+
self,
|
|
992
|
+
text: str,
|
|
993
|
+
position: np.ndarray = ORIGIN,
|
|
994
|
+
font_size: int = 20,
|
|
995
|
+
color: ManimColor = WHITE,
|
|
996
|
+
scene: Optional[ThreeDScene] = None,
|
|
997
|
+
**kwargs,
|
|
998
|
+
):
|
|
999
|
+
super().__init__(**kwargs)
|
|
1000
|
+
self.label = Text(text, font_size=font_size, color=color)
|
|
1001
|
+
self.label.move_to(np.array(position, dtype=float))
|
|
1002
|
+
self.add(self.label)
|
|
1003
|
+
if scene is not None:
|
|
1004
|
+
scene.add_fixed_orientation_mobjects(self.label)
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
# ---------------------------------------------------------------------------
|
|
1008
|
+
# GridSnapHelper
|
|
1009
|
+
# ---------------------------------------------------------------------------
|
|
1010
|
+
|
|
1011
|
+
class GridSnapHelper:
|
|
1012
|
+
"""Utility class (no Manim rendering) that maps data values to
|
|
1013
|
+
snapped grid positions in 3D space.
|
|
1014
|
+
|
|
1015
|
+
Useful when placing chart objects (bars, spheres, labels) exactly
|
|
1016
|
+
on grid intersections rather than at raw data coordinates.
|
|
1017
|
+
|
|
1018
|
+
Parameters
|
|
1019
|
+
----------
|
|
1020
|
+
x_range, y_range, z_range : (float, float, float)
|
|
1021
|
+
(min, max, step) for each axis — must match your ``FullGrid3D``.
|
|
1022
|
+
origin : np.ndarray
|
|
1023
|
+
3D origin of the grid coordinate system.
|
|
1024
|
+
"""
|
|
1025
|
+
|
|
1026
|
+
def __init__(
|
|
1027
|
+
self,
|
|
1028
|
+
x_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
1029
|
+
y_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
1030
|
+
z_range: Tuple[float, float, float] = (0, 5, 1),
|
|
1031
|
+
origin: np.ndarray = ORIGIN,
|
|
1032
|
+
):
|
|
1033
|
+
self.x_range = x_range
|
|
1034
|
+
self.y_range = y_range
|
|
1035
|
+
self.z_range = z_range
|
|
1036
|
+
self.origin = np.array(origin, dtype=float)
|
|
1037
|
+
|
|
1038
|
+
def _snap(self, val: float, rng: Tuple[float, float, float]) -> float:
|
|
1039
|
+
mn, mx, step = rng
|
|
1040
|
+
snapped = round((val - mn) / step) * step + mn
|
|
1041
|
+
return float(np.clip(snapped, mn, mx))
|
|
1042
|
+
|
|
1043
|
+
def snap(
|
|
1044
|
+
self,
|
|
1045
|
+
x: float = 0.0,
|
|
1046
|
+
y: float = 0.0,
|
|
1047
|
+
z: float = 0.0,
|
|
1048
|
+
) -> np.ndarray:
|
|
1049
|
+
"""Return the snapped 3D position for data coordinates (x, y, z)."""
|
|
1050
|
+
sx = self._snap(x, self.x_range)
|
|
1051
|
+
sy = self._snap(y, self.y_range)
|
|
1052
|
+
sz = self._snap(z, self.z_range)
|
|
1053
|
+
return self.origin + np.array([sx, sy, sz])
|
|
1054
|
+
|
|
1055
|
+
def data_to_grid(
|
|
1056
|
+
self,
|
|
1057
|
+
points: np.ndarray,
|
|
1058
|
+
) -> np.ndarray:
|
|
1059
|
+
"""Batch-snap an (N, 3) array of data points.
|
|
1060
|
+
|
|
1061
|
+
Parameters
|
|
1062
|
+
----------
|
|
1063
|
+
points : np.ndarray, shape (N, 3)
|
|
1064
|
+
Rows of (x, y, z) data coordinates.
|
|
1065
|
+
|
|
1066
|
+
Returns
|
|
1067
|
+
-------
|
|
1068
|
+
np.ndarray, shape (N, 3)
|
|
1069
|
+
Snapped grid positions.
|
|
1070
|
+
"""
|
|
1071
|
+
out = np.zeros_like(points)
|
|
1072
|
+
for i, (x, y, z) in enumerate(points):
|
|
1073
|
+
out[i] = self.snap(x, y, z)
|
|
1074
|
+
return out
|
|
1075
|
+
|
|
1076
|
+
def axes_ticks(self, axis: str = "x") -> np.ndarray:
|
|
1077
|
+
"""Return a 1-D array of major tick values along *axis*."""
|
|
1078
|
+
rng = {"x": self.x_range, "y": self.y_range, "z": self.z_range}[axis]
|
|
1079
|
+
mn, mx, step = rng
|
|
1080
|
+
return np.arange(mn, mx + step * 0.5, step)
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
# ---------------------------------------------------------------------------
|
|
1084
|
+
# Convenience factory
|
|
1085
|
+
# ---------------------------------------------------------------------------
|
|
1086
|
+
|
|
1087
|
+
def make_stats_grid(
|
|
1088
|
+
x_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
1089
|
+
y_range: Tuple[float, float, float] = (-4, 4, 1),
|
|
1090
|
+
z_range: Tuple[float, float, float] = (0, 6, 1),
|
|
1091
|
+
theme: str = "stats",
|
|
1092
|
+
minor: int = 4,
|
|
1093
|
+
label_axes: bool = False,
|
|
1094
|
+
show_bounding_box: bool = False,
|
|
1095
|
+
) -> FullGrid3D:
|
|
1096
|
+
"""Factory that wires ``FullGrid3D`` + ``GridSnapHelper`` together.
|
|
1097
|
+
|
|
1098
|
+
Parameters
|
|
1099
|
+
----------
|
|
1100
|
+
theme : str
|
|
1101
|
+
``"stats"`` (default), ``"dark"``, or ``"light"``.
|
|
1102
|
+
|
|
1103
|
+
Returns
|
|
1104
|
+
-------
|
|
1105
|
+
FullGrid3D
|
|
1106
|
+
Ready to add to a scene.
|
|
1107
|
+
|
|
1108
|
+
Example
|
|
1109
|
+
-------
|
|
1110
|
+
::
|
|
1111
|
+
|
|
1112
|
+
grid = make_stats_grid(x_range=(-3, 3, 1), z_range=(0, 8, 1))
|
|
1113
|
+
snap = GridSnapHelper(x_range=(-3, 3, 1), z_range=(0, 8, 1))
|
|
1114
|
+
self.play(grid.animate_build())
|
|
1115
|
+
bar_pos = snap.snap(x=1.0, y=0.0, z=3.5)
|
|
1116
|
+
"""
|
|
1117
|
+
themes = {"stats": STATS_GRID, "dark": DARK_GRID, "light": LIGHT_GRID}
|
|
1118
|
+
cfg = themes.get(theme, STATS_GRID)
|
|
1119
|
+
return FullGrid3D(
|
|
1120
|
+
x_range=x_range,
|
|
1121
|
+
y_range=y_range,
|
|
1122
|
+
z_range=z_range,
|
|
1123
|
+
minor_subdivisions=minor,
|
|
1124
|
+
config=cfg,
|
|
1125
|
+
show_bounding_box=show_bounding_box,
|
|
1126
|
+
label_axes=label_axes,
|
|
1127
|
+
)
|