2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
2020-06-05 13:24:26 -07:00
|
|
|
from manimlib.mobject.mobject import Group
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.mobject.geometry import Square
|
2020-06-05 11:12:52 -07:00
|
|
|
from manimlib.mobject.types.surface import ParametricSurface
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
2020-06-05 13:24:26 -07:00
|
|
|
from manimlib.utils.config_ops import digest_config
|
|
|
|
from manimlib.utils.space_ops import get_norm
|
|
|
|
from manimlib.utils.space_ops import z_to_vector
|
2018-04-01 10:45:41 -07:00
|
|
|
|
|
|
|
|
2020-06-04 15:41:20 -07:00
|
|
|
# Sphere, cylinder, cube, prism
|
2018-08-15 16:23:29 -07:00
|
|
|
|
|
|
|
class Sphere(ParametricSurface):
|
|
|
|
CONFIG = {
|
2020-06-05 11:12:52 -07:00
|
|
|
"resolution": (100, 50),
|
2018-08-22 21:23:24 -07:00
|
|
|
"radius": 1,
|
2020-06-04 17:17:38 -07:00
|
|
|
"u_range": (0, TAU),
|
|
|
|
"v_range": (0, PI),
|
2018-08-15 16:23:29 -07:00
|
|
|
}
|
2018-04-01 10:45:41 -07:00
|
|
|
|
2020-06-05 13:24:26 -07:00
|
|
|
def uv_func(self, u, v):
|
2020-06-04 15:41:20 -07:00
|
|
|
return self.radius * np.array([
|
2020-06-04 17:17:38 -07:00
|
|
|
np.cos(u) * np.sin(v),
|
|
|
|
np.sin(u) * np.sin(v),
|
2020-06-05 11:12:52 -07:00
|
|
|
-np.cos(v)
|
2018-08-15 16:23:29 -07:00
|
|
|
])
|
2018-04-01 10:45:41 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-06-05 13:24:26 -07:00
|
|
|
class Cylinder(ParametricSurface):
|
2020-06-05 11:12:52 -07:00
|
|
|
CONFIG = {
|
2020-06-05 13:24:26 -07:00
|
|
|
"height": 2,
|
|
|
|
"radius": 1,
|
|
|
|
"axis": OUT,
|
|
|
|
"u_range": (0, TAU),
|
|
|
|
"v_range": (-1, 1),
|
|
|
|
"resolution": (100, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
def init_points(self):
|
|
|
|
super().init_points()
|
|
|
|
self.scale(self.radius)
|
|
|
|
self.set_depth(self.height, stretch=True)
|
|
|
|
self.apply_matrix(z_to_vector(self.axis))
|
|
|
|
return self
|
2020-06-05 11:12:52 -07:00
|
|
|
|
2020-06-05 13:24:26 -07:00
|
|
|
def uv_func(self, u, v):
|
|
|
|
return [np.cos(u), np.sin(u), v]
|
|
|
|
|
|
|
|
|
|
|
|
class Line3D(Cylinder):
|
|
|
|
CONFIG = {
|
|
|
|
"width": 0.05,
|
2020-06-05 11:12:52 -07:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:24:26 -07:00
|
|
|
def __init__(self, start, end, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
|
|
|
axis = end - start
|
|
|
|
super().__init__(
|
|
|
|
height=get_norm(axis),
|
|
|
|
radius=self.width / 2,
|
|
|
|
axis=axis
|
|
|
|
)
|
2020-06-05 11:12:52 -07:00
|
|
|
|
|
|
|
|
2020-06-05 13:24:26 -07:00
|
|
|
class Disk3D(ParametricSurface):
|
2018-04-01 10:45:41 -07:00
|
|
|
CONFIG = {
|
2020-06-05 13:24:26 -07:00
|
|
|
"radius": 1,
|
|
|
|
"u_range": (0, 1),
|
|
|
|
"v_range": (0, TAU),
|
|
|
|
"resolution": (1, 24),
|
|
|
|
}
|
|
|
|
|
|
|
|
def init_points(self):
|
|
|
|
super().init_points()
|
|
|
|
self.scale(self.radius)
|
|
|
|
|
|
|
|
def uv_func(self, u, v):
|
|
|
|
return [
|
|
|
|
u * np.cos(v),
|
|
|
|
u * np.sin(v),
|
|
|
|
0
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class Cube(Group):
|
|
|
|
CONFIG = {
|
|
|
|
# "fill_color": BLUE,
|
|
|
|
# "fill_opacity": 1,
|
|
|
|
# "stroke_width": 1,
|
|
|
|
# "stroke_color": BLACK,
|
|
|
|
"color": BLUE,
|
|
|
|
"opacity": 1,
|
|
|
|
"gloss": 0.5,
|
|
|
|
"square_resolution": (1, 1),
|
2018-04-06 13:58:59 -07:00
|
|
|
"side_length": 2,
|
2018-04-01 10:45:41 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2020-06-05 13:24:26 -07:00
|
|
|
square = ParametricSurface(
|
|
|
|
lambda u, v: [u, v, 0],
|
|
|
|
resolution=self.square_resolution,
|
|
|
|
u_range=(-1, 1),
|
|
|
|
v_range=(-1, 1),
|
|
|
|
)
|
|
|
|
square.set_color(self.color, self.opacity, self.gloss)
|
2018-04-01 10:45:41 -07:00
|
|
|
for vect in IN, OUT, LEFT, RIGHT, UP, DOWN:
|
2020-06-05 13:24:26 -07:00
|
|
|
# face = Square(side_length=self.side_length)
|
|
|
|
face = square.deepcopy()
|
|
|
|
face.shift(OUT)
|
2018-08-15 16:23:29 -07:00
|
|
|
face.apply_matrix(z_to_vector(vect))
|
2018-04-01 10:45:41 -07:00
|
|
|
self.add(face)
|
2020-06-05 13:24:26 -07:00
|
|
|
self.set_height(self.side_length)
|
2018-04-01 10:45:41 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-01 10:45:41 -07:00
|
|
|
class Prism(Cube):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"dimensions": [3, 2, 1]
|
2018-04-01 10:45:41 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
|
|
|
Cube.init_points(self)
|
2018-04-01 10:45:41 -07:00
|
|
|
for dim, value in enumerate(self.dimensions):
|
2018-04-06 13:58:59 -07:00
|
|
|
self.rescale_to_fit(value, dim, stretch=True)
|