-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New mobject: ImplicitSurface for implicit 3D surface rendering #4429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OeGiaretta
wants to merge
12
commits into
ManimCommunity:main
Choose a base branch
from
OeGiaretta:feature/implicit-surface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ca3f6aa
Example codes
OeGiaretta 9eb1ea5
.
OeGiaretta e2d6a72
Ajustes e testes.
OeGiaretta 19cfe31
Adjustment, testing, and implementation of the Implicit_Surface.
OeGiaretta a0984bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dd6f564
Add scikit-image as dependency for ImplicitSurface
OeGiaretta 8601a2b
Merge branch 'feature/implicit-surface' of https://github.com/OeGiare…
OeGiaretta f5baaed
Add ImplicitSurface Mobject and fix typing issues
OeGiaretta dba6e83
fix(docs): ensure alias_container is always initialized in
OeGiaretta 3d1a999
Delete docs/source/conduct.md
OeGiaretta 8f89152
Update file_ops.py
OeGiaretta 6ead5b1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
from typing import Any, Callable | ||
|
||
import numpy as np | ||
from skimage import measure | ||
|
||
from manim import * | ||
Check noticeCode scanning / CodeQL 'import *' may pollute namespace Note
Import pollutes the enclosing namespace, as the imported module
manim Error loading related location Loading |
||
from manim.mobject.three_d.three_dimensions import ThreeDVMobject | ||
from manim.utils.color import ManimColor | ||
|
||
|
||
class ImplicitSurface(ThreeDVMobject): | ||
"""Render an implicit isosurface using the Marching Cubes algorithm. | ||
|
||
Parameters | ||
---------- | ||
func : Callable[[float | np.ndarray, float | np.ndarray, float | np.ndarray], float | np.ndarray] | ||
Implicit function f(x, y, z). The surface is defined where f(x, y, z) = isolevel. | ||
resolution : int, default=25 | ||
Number of divisions per axis. | ||
isolevel : float, default=0.0 | ||
The isosurface level. | ||
x_range, y_range, z_range : Sequence[float], default=(-2, 2) | ||
Sampling ranges. | ||
color : Color, default=BLUE | ||
Color of the surface. | ||
**kwargs | ||
Additional arguments passed to ThreeDVMobject. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
func: Callable[ | ||
[float | np.ndarray, float | np.ndarray, float | np.ndarray], | ||
float | np.ndarray, | ||
], | ||
resolution: int = 25, | ||
isolevel: float = 0.0, | ||
x_range: Sequence[float] = (-2.0, 2.0), | ||
y_range: Sequence[float] = (-2.0, 2.0), | ||
z_range: Sequence[float] = (-2.0, 2.0), | ||
color: ManimColor = BLUE, | ||
**kwargs: Any, | ||
) -> None: | ||
super().__init__(**kwargs) | ||
|
||
# Generates the 3D grid: | ||
x = np.linspace(x_range[0], x_range[1], resolution) | ||
y = np.linspace(y_range[0], y_range[1], resolution) | ||
z = np.linspace(z_range[0], z_range[1], resolution) | ||
X, Y, Z = np.meshgrid(x, y, z, indexing="ij") | ||
values = func(X, Y, Z) | ||
|
||
# Extracts the mesh: | ||
verts, faces, _, _ = measure.marching_cubes(values, level=isolevel) | ||
|
||
# Normalizes to the real domain: | ||
scale_x = (x_range[1] - x_range[0]) / resolution | ||
scale_y = (y_range[1] - y_range[0]) / resolution | ||
scale_z = (z_range[1] - z_range[0]) / resolution | ||
verts = np.array( | ||
[ | ||
[ | ||
x_range[0] + v[0] * scale_x, | ||
y_range[0] + v[1] * scale_y, | ||
z_range[0] + v[2] * scale_z, | ||
] | ||
for v in verts | ||
] | ||
) | ||
|
||
# Builds the polygons | ||
for face in faces: | ||
tri = [verts[i] for i in face] | ||
self.add(Polygon(*tri, color=color, fill_opacity=1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from manim import * | ||
from manim.mobject.three_d.implicit_surface import ImplicitSurface | ||
|
||
|
||
class TestSurfaceScene(ThreeDScene): | ||
def construct(self): | ||
self.set_camera_orientation(phi=70 * DEGREES, theta=45 * DEGREES) | ||
surface = ImplicitSurface( | ||
lambda x, y, z, r=0.7: x**2 + y**2 - r**2, resolution=20, color=WHITE | ||
) | ||
self.add(surface) | ||
self.wait() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from manim import * | ||
from manim.mobject.three_d.implicit_surface import ImplicitSurface | ||
|
||
|
||
class TestSurface360(ThreeDScene): | ||
def construct(self): | ||
self.set_camera_orientation(phi=70 * DEGREES, theta=0) | ||
|
||
surface = ImplicitSurface( | ||
lambda x, y, z: x**2 + y**2 + z**2 - 1, resolution=20, color=BLUE | ||
) | ||
self.add(surface) | ||
|
||
self.begin_ambient_camera_rotation(rate=PI / 4) | ||
self.wait(8) | ||
self.stop_ambient_camera_rotation() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.