Skip to content

Feature Request: Add ImplicitSurface for visualizing f(x, y, z) = 0 #4428

@lktn

Description

@lktn

Description of the problem

ManimCE supports Surface and ParametricSurface for explicit and parametric surfaces.
However, there is no built-in way to visualize implicit surfaces defined by an equation of the form:

$$f(x,y,z) = 0$$

I have an idea:

from manim import *
import numpy as np
from skimage import measure

def ImplicitSurface(f, x_range, y_range, z_range):
    X, Y, Z = np.mgrid[
        x_range[0]:x_range[1]:x_range[2]*1j,
        y_range[0]:y_range[1]:y_range[2]*1j,
        z_range[0]:z_range[1]:z_range[2]*1j
    ]

    dx = (x_range[1] - x_range[0]) / (x_range[2] - 1)
    dy = (y_range[1] - y_range[0]) / (y_range[2] - 1)
    dz = (z_range[1] - z_range[0]) / (z_range[2] - 1)

    verts, faces, _, _ = measure.marching_cubes(f(X, Y, Z), level=0, spacing=(dx, dy, dz))
    verts[:,0] += x_range[0]
    verts[:,1] += y_range[0]
    verts[:,2] += z_range[0]

    return VGroup(*[Polygon(*verts[i], stroke_width=0.5, fill_opacity=1) for i in faces]).set_shade_in_3d(True)

class MyScene(ThreeDScene):
    def construct(self):
        self.set_camera_orientation(phi=60*DEGREES, theta=-60*DEGREES)
        surface = ImplicitSurface(
            f=lambda x, y, z: x**4 + y**4 + z**4 -16*(x**2 + y**2 + z**2) + 120,
            x_range=[-4.1, 4.1, 50],
            y_range=[-4.1, 4.1, 50],
            z_range=[-4.1, 4.1, 50],
        ).set_color_by_gradient(RED, PINK)
        
        self.add(surface.scale(0.6))

Metadata

Metadata

Assignees

No one assigned

    Labels

    new featureEnhancement specifically adding a new feature (feature request should be used for issues instead)

    Type

    No type

    Projects

    Status

    🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions