mirror of
https://github.com/3b1b/manim.git
synced 2025-04-13 09:47:07 +00:00
8.5 KiB
8.5 KiB
Utils Module Documentation
Overview
The utils module provides essential utility functions and classes that support the core functionality of Manim. It includes mathematical operations, color management, file handling, caching mechanisms, and system utilities.
Directory Structure
manimlib/utils/
├── bezier.py # Bezier curve operations
├── cache.py # Caching mechanisms
├── color.py # Color manipulation
├── config_ops.py # Configuration management
├── customization.py # User customization
├── debug.py # Debugging utilities
├── dict_ops.py # Dictionary operations
├── directories.py # Directory management
├── family_ops.py # Mobject family operations
├── file_ops.py # File operations
├── images.py # Image handling
├── iterables.py # Iterable utilities
├── paths.py # Path manipulation
├── rate_functions.py # Animation rate functions
├── shaders.py # Shader utilities
├── simple_functions.py # Basic utility functions
├── sounds.py # Sound handling
├── space_ops.py # Spatial operations
├── strings.py # String utilities
├── tex.py # TeX utilities
├── tex_file_writing.py # TeX file generation
└── tex_to_symbol_count.py # TeX symbol counting
Core Components
1. Mathematical Utilities
Bezier Operations (bezier.py
)
- Purpose: Handle Bezier curve calculations
- Key Functions:
interpolate_bezier
: Interpolate points along curveget_bezier_points
: Generate curve pointspartial_bezier_points
: Get partial curveget_quadratic_approximation
: Approximate curves
- Usage:
points = get_bezier_points(start, control1, control2, end)
Space Operations (space_ops.py
)
- Purpose: Handle spatial transformations and vector operations
- Key Functions:
rotation_matrix
: Generate rotation matricesangle_between_vectors
: Calculate anglesget_norm
: Vector normalizationcross
: Cross product calculationfind_intersection
: Line intersectionget_closest_point_on_line
: Point-line distance
- Usage:
angle = angle_between_vectors(vector1, vector2) intersection = find_intersection(p0, v0, p1, v1)
Rate Functions (rate_functions.py
)
- Purpose: Animation timing functions
- Key Functions:
linear
: Linear interpolationsmooth
: Smooth interpolationthere_and_back
: Reversing motiondouble_smooth
: Double smooth transitionexponential_decay
: Exponential decaylingering
: Lingering effect
- Usage:
progress = smooth(alpha) decay = exponential_decay(t, half_life=0.1)
2. Color Management (color.py
)
- Purpose: Color operations and conversions
- Key Features:
- Color space conversions (RGB, hex)
- Color interpolation
- Gradient generation
- Color map creation
- Average color calculation
- Usage:
rgb = color_to_rgb(hex_color) interpolated = interpolate_color(color1, color2, alpha) avg_color = average_color(*colors) colormap = get_colormap_from_colors(colors)
3. File and Resource Management
File Operations (file_ops.py
)
- Purpose: File system operations
- Key Functions:
find_file
: Locate files with extensionsguarantee_existence
: Directory creationdownload_url
: URL resource downloading
- Usage:
file_path = find_file(name, directories, extensions) dir_path = guarantee_existence(path)
Directory Management (directories.py
)
- Purpose: Directory structure handling
- Key Functions:
get_cache_dir
: Cache directory accessget_downloads_dir
: Downloads locationget_shader_dir
: Shader file locationget_sound_dir
: Sound file location
- Usage:
cache_dir = get_cache_dir() shader_dir = get_shader_dir()
Image Operations (images.py
)
- Purpose: Image processing and management
- Key Features:
- Image loading and path resolution
- Format conversion
- Image inversion
- Raster and vector image handling
- Usage:
image_path = get_full_raster_image_path(filename) inverted = invert_image(image)
Sound Operations (sounds.py
)
- Purpose: Sound file handling
- Key Features:
- Sound file path resolution
- Audio format support (.wav, .mp3)
- Platform-specific playback
- Usage:
sound_path = get_full_sound_file_path(filename)
4. TeX and Mathematical Typesetting
TeX System (tex.py
, tex_file_writing.py
, tex_to_symbol_count.py
)
- Purpose: LaTeX integration and processing
- Key Features:
- TeX template management
- SVG conversion
- Symbol counting
- Template configuration
- Error handling
- Usage:
svg = latex_to_svg(latex_expression) symbol_count = num_tex_symbols(tex_string)
5. Performance and Optimization
Caching System (cache.py
)
- Purpose: Performance optimization through caching
- Key Features:
- Disk-based caching
- Function result caching
- Cache size management
- Cache invalidation
- Usage:
@cache_on_disk def expensive_operation(): # Function implementation
Shader Management (shaders.py
)
- Purpose: GPU shader handling
- Key Features:
- Shader code loading
- Texture management
- Uniform handling
- Program compilation
- Usage:
shader_code = get_shader_code_from_file(filename) texture = image_path_to_texture(path, ctx)
6. Utility Functions
Simple Functions (simple_functions.py
)
- Purpose: Basic utility operations
- Key Functions:
sigmoid
: Sigmoid functionclip
: Value clampingfdiv
: Safe divisionget_parameters
: Function parameter inspection
- Usage:
result = sigmoid(x) clipped = clip(value, min_val, max_val)
Dictionary Operations (dict_ops.py
)
- Purpose: Dictionary manipulation utilities
- Key Features:
- Deep dictionary updates
- Nested access
- Dictionary merging
- Usage:
merged = merge_dicts(dict1, dict2)
Family Operations (family_ops.py
)
- Purpose: Mobject family relationship handling
- Key Features:
- Family member tracking
- Relationship management
- Hierarchy operations
- Usage:
family = extract_mobject_family_members(mobject)
Best Practices
1. Mathematical Operations
- Use vectorized operations when possible
- Cache complex calculations
- Handle edge cases and numerical stability
- Maintain precision in transformations
2. Resource Management
- Use context managers for file operations
- Handle permissions and file existence
- Clean up resources properly
- Use appropriate caching strategies
3. Performance Optimization
- Profile critical paths
- Cache frequent operations
- Optimize memory usage
- Use appropriate data structures
- Leverage GPU acceleration when possible
Integration Examples
1. Complex Animation
from manimlib.utils.rate_functions import smooth
from manimlib.utils.space_ops import rotation_matrix
from manimlib.utils.bezier import interpolate_bezier
class ComplexAnimation(Scene):
def construct(self):
# Create a smooth path with bezier curves
points = get_bezier_points(start, control1, control2, end)
# Apply rotation with smooth timing
matrix = rotation_matrix(PI, axis=OUT)
self.play(
MoveAlongPath(object, points),
ApplyMatrix(matrix, object),
rate_func=smooth
)
2. Resource Management
from manimlib.utils.file_ops import find_file
from manimlib.utils.images import get_full_raster_image_path
from manimlib.utils.sounds import get_full_sound_file_path
class ResourceExample(Scene):
def construct(self):
# Load and display an image
image_path = get_full_raster_image_path("example.png")
image = ImageMobject(image_path)
# Add sound
sound_path = get_full_sound_file_path("effect.wav")
self.add_sound(sound_path)