2015-06-10 22:00:35 -07:00
|
|
|
from helpers import *
|
2017-08-24 19:05:30 -07:00
|
|
|
from mobject.vectorized_mobject import VGroup, VMobject, VectorizedPoint
|
|
|
|
from topics.geometry import Square, Line
|
2017-02-13 18:43:55 -08:00
|
|
|
from scene import Scene
|
|
|
|
from camera import Camera
|
2017-08-30 13:16:08 -07:00
|
|
|
from animation.continual_animation import AmbientMovement
|
2017-08-24 19:05:30 -07:00
|
|
|
from animation.transform import ApplyMethod
|
2018-03-05 18:53:13 -06:00
|
|
|
import numpy as np
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2017-03-02 11:53:04 -08:00
|
|
|
class CameraWithPerspective(Camera):
|
2018-03-05 18:53:13 -06:00
|
|
|
CONFIG = {'camera_distance': 20}
|
|
|
|
|
2017-03-02 11:53:04 -08:00
|
|
|
def points_to_pixel_coords(self, points):
|
2018-03-05 18:53:13 -06:00
|
|
|
distance_ratios = np.divide(self.camera_distance, self.camera_distance - points[:, 2])
|
2017-03-02 11:53:04 -08:00
|
|
|
scale_factors = interpolate(0, 1, distance_ratios)
|
|
|
|
adjusted_points = np.array(points)
|
2018-03-05 18:53:13 -06:00
|
|
|
for i in (0, 1):
|
|
|
|
adjusted_points[(:, i)] *= scale_factors
|
2017-03-02 11:53:04 -08:00
|
|
|
|
|
|
|
return Camera.points_to_pixel_coords(self, adjusted_points)
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
|
2017-03-02 11:53:04 -08:00
|
|
|
class ThreeDCamera(CameraWithPerspective):
|
2018-03-05 18:53:13 -06:00
|
|
|
CONFIG = {'sun_vect': 5 * UP + LEFT,
|
|
|
|
'shading_factor': 0.2,
|
|
|
|
'distance': 5.0,
|
|
|
|
'default_distance': 5.0,
|
|
|
|
'phi': 0,
|
|
|
|
'theta': -TAU / 4}
|
|
|
|
|
2017-02-13 18:43:55 -08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
Camera.__init__(self, *args, **kwargs)
|
2018-03-05 18:53:13 -06:00
|
|
|
self.unit_sun_vect = self.sun_vect / np.linalg.norm(self.sun_vect)
|
2017-08-30 13:16:08 -07:00
|
|
|
self.rotation_mobject = VectorizedPoint()
|
2018-02-02 10:34:08 +01:00
|
|
|
self.moving_center = VectorizedPoint(self.space_center)
|
2017-08-24 19:05:30 -07:00
|
|
|
self.set_position(self.phi, self.theta, self.distance)
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-02-10 21:43:55 -08:00
|
|
|
def modified_rgb(self, vmobject, rgb):
|
2017-08-24 23:12:06 -07:00
|
|
|
if should_shade_in_3d(vmobject):
|
2018-02-10 21:43:55 -08:00
|
|
|
return self.get_shaded_rgb(rgb, self.get_unit_normal_vect(vmobject))
|
2017-03-10 16:54:44 -08:00
|
|
|
else:
|
2018-02-13 15:39:31 -08:00
|
|
|
return rgb
|
2017-03-10 16:54:44 -08:00
|
|
|
|
2018-02-10 21:43:55 -08:00
|
|
|
def get_stroke_rgb(self, vmobject):
|
|
|
|
return self.modified_rgb(vmobject, vmobject.get_stroke_rgb())
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-02-10 21:43:55 -08:00
|
|
|
def get_fill_rgb(self, vmobject):
|
|
|
|
return self.modified_rgb(vmobject, vmobject.get_fill_rgb())
|
2017-02-13 18:43:55 -08:00
|
|
|
|
|
|
|
def get_shaded_rgb(self, rgb, normal_vect):
|
2018-03-05 18:53:13 -06:00
|
|
|
brightness = np.dot(normal_vect, self.unit_sun_vect) ** 2
|
2017-02-13 18:43:55 -08:00
|
|
|
if brightness > 0:
|
2018-03-05 18:53:13 -06:00
|
|
|
alpha = self.shading_factor * brightness
|
2017-02-13 18:43:55 -08:00
|
|
|
return interpolate(rgb, np.ones(3), alpha)
|
|
|
|
else:
|
2018-03-05 18:53:13 -06:00
|
|
|
alpha = -self.shading_factor * brightness
|
2017-02-13 18:43:55 -08:00
|
|
|
return interpolate(rgb, np.zeros(3), alpha)
|
|
|
|
|
|
|
|
def get_unit_normal_vect(self, vmobject):
|
|
|
|
anchors = vmobject.get_anchors()
|
|
|
|
if len(anchors) < 3:
|
|
|
|
return OUT
|
2018-03-05 18:53:13 -06:00
|
|
|
normal = np.cross(anchors[1] - anchors[0], anchors[2] - anchors[1])
|
2017-02-13 18:43:55 -08:00
|
|
|
if normal[2] < 0:
|
|
|
|
normal = -normal
|
|
|
|
length = np.linalg.norm(normal)
|
|
|
|
if length == 0:
|
|
|
|
return OUT
|
2018-03-05 18:53:13 -06:00
|
|
|
return normal / length
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2017-02-23 18:14:08 -08:00
|
|
|
def display_multiple_vectorized_mobjects(self, vmobjects):
|
2018-03-05 18:53:13 -06:00
|
|
|
camera_point = self.spherical_coords_to_point(*self.get_spherical_coords())
|
|
|
|
|
2017-02-23 18:14:08 -08:00
|
|
|
def z_cmp(*vmobs):
|
2018-02-06 12:23:33 -08:00
|
|
|
three_d_status = map(should_shade_in_3d, vmobs)
|
2018-03-05 18:53:13 -06:00
|
|
|
has_points = [ vm.get_num_points() > 0 for vm in vmobs ]
|
2018-02-06 12:23:33 -08:00
|
|
|
if all(three_d_status) and all(has_points):
|
|
|
|
cmp_vect = self.get_unit_normal_vect(vmobs[1])
|
2018-03-05 18:53:13 -06:00
|
|
|
return cmp(*[ np.dot(vm.get_center(), cmp_vect) for vm in vmobs ])
|
2018-02-06 12:23:33 -08:00
|
|
|
else:
|
|
|
|
return 0
|
2018-03-05 18:53:13 -06:00
|
|
|
|
|
|
|
Camera.display_multiple_vectorized_mobjects(self, sorted(vmobjects, cmp=z_cmp))
|
2017-02-23 18:14:08 -08:00
|
|
|
|
2017-08-30 13:16:08 -07:00
|
|
|
def get_spherical_coords(self, phi = None, theta = None, distance = None):
|
|
|
|
curr_phi, curr_theta, curr_d = self.rotation_mobject.points[0]
|
2018-03-05 18:53:13 -06:00
|
|
|
if phi is None:
|
|
|
|
phi = curr_phi
|
|
|
|
if theta is None:
|
|
|
|
theta = curr_theta
|
|
|
|
if distance is None:
|
|
|
|
distance = curr_d
|
2017-08-30 13:16:08 -07:00
|
|
|
return np.array([phi, theta, distance])
|
2017-08-24 19:05:30 -07:00
|
|
|
|
2018-02-06 11:14:20 +01:00
|
|
|
def get_cartesian_coords(self, phi = None, theta = None, distance = None):
|
2018-03-05 18:53:13 -06:00
|
|
|
spherical_coords_array = self.get_spherical_coords(phi, theta, distance)
|
2018-02-06 11:14:20 +01:00
|
|
|
phi2 = spherical_coords_array[0]
|
|
|
|
theta2 = spherical_coords_array[1]
|
|
|
|
d2 = spherical_coords_array[2]
|
2018-03-05 18:53:13 -06:00
|
|
|
return self.spherical_coords_to_point(phi2, theta2, d2)
|
2018-02-06 11:14:20 +01:00
|
|
|
|
2017-08-24 19:05:30 -07:00
|
|
|
def get_phi(self):
|
2017-08-30 13:16:08 -07:00
|
|
|
return self.get_spherical_coords()[0]
|
2017-08-24 19:05:30 -07:00
|
|
|
|
|
|
|
def get_theta(self):
|
2017-08-30 13:16:08 -07:00
|
|
|
return self.get_spherical_coords()[1]
|
2017-08-24 19:05:30 -07:00
|
|
|
|
|
|
|
def get_distance(self):
|
2017-08-30 13:16:08 -07:00
|
|
|
return self.get_spherical_coords()[2]
|
2017-08-24 19:05:30 -07:00
|
|
|
|
|
|
|
def spherical_coords_to_point(self, phi, theta, distance):
|
2018-03-05 18:53:13 -06:00
|
|
|
return distance * np.array([np.sin(phi) * np.cos(theta), np.sin(phi) * np.sin(theta), np.cos(phi)])
|
2017-08-24 19:05:30 -07:00
|
|
|
|
2018-02-02 10:34:08 +01:00
|
|
|
def get_center_of_rotation(self, x = None, y = None, z = None):
|
|
|
|
curr_x, curr_y, curr_z = self.moving_center.points[0]
|
|
|
|
if x is None:
|
|
|
|
x = curr_x
|
|
|
|
if y is None:
|
|
|
|
y = curr_y
|
|
|
|
if z is None:
|
|
|
|
z = curr_z
|
|
|
|
return np.array([x, y, z])
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def set_position(self, phi = None, theta = None, distance = None, center_x = None, center_y = None, center_z = None):
|
2017-08-30 13:16:08 -07:00
|
|
|
point = self.get_spherical_coords(phi, theta, distance)
|
|
|
|
self.rotation_mobject.move_to(point)
|
|
|
|
self.phi, self.theta, self.distance = point
|
2018-02-02 15:12:07 +01:00
|
|
|
center_of_rotation = self.get_center_of_rotation(center_x, center_y, center_z)
|
|
|
|
self.moving_center.move_to(center_of_rotation)
|
|
|
|
self.space_center = self.moving_center.points[0]
|
2017-08-24 19:05:30 -07:00
|
|
|
|
|
|
|
def get_view_transformation_matrix(self):
|
2018-03-05 18:53:13 -06:00
|
|
|
return self.default_distance / self.get_distance() * np.dot(rotation_matrix(self.get_phi(), LEFT), rotation_about_z(-self.get_theta() - np.pi / 2))
|
2017-08-24 19:05:30 -07:00
|
|
|
|
|
|
|
def points_to_pixel_coords(self, points):
|
|
|
|
matrix = self.get_view_transformation_matrix()
|
|
|
|
new_points = np.dot(points, matrix.T)
|
2018-02-02 10:34:08 +01:00
|
|
|
self.space_center = self.moving_center.points[0]
|
2017-08-24 19:05:30 -07:00
|
|
|
return Camera.points_to_pixel_coords(self, new_points)
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
|
2017-02-13 18:43:55 -08:00
|
|
|
class ThreeDScene(Scene):
|
2018-03-05 18:53:13 -06:00
|
|
|
CONFIG = {'camera_class': ThreeDCamera,
|
|
|
|
'ambient_camera_rotation': None}
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def set_camera_position(self, phi = None, theta = None, distance = None, center_x = None, center_y = None, center_z = None):
|
2018-02-02 15:12:07 +01:00
|
|
|
self.camera.set_position(phi, theta, distance, center_x, center_y, center_z)
|
2017-08-24 19:05:30 -07:00
|
|
|
|
2017-08-25 17:56:55 -07:00
|
|
|
def begin_ambient_camera_rotation(self, rate = 0.01):
|
2018-03-05 18:53:13 -06:00
|
|
|
self.ambient_camera_rotation = AmbientMovement(self.camera.rotation_mobject, direction=UP, rate=rate)
|
2017-08-24 19:05:30 -07:00
|
|
|
self.add(self.ambient_camera_rotation)
|
|
|
|
|
2017-08-29 00:04:25 -07:00
|
|
|
def stop_ambient_camera_rotation(self):
|
2017-08-31 00:16:08 -07:00
|
|
|
if self.ambient_camera_rotation is not None:
|
|
|
|
self.remove(self.ambient_camera_rotation)
|
2017-08-30 18:33:55 -07:00
|
|
|
self.ambient_camera_rotation = None
|
2017-08-29 00:04:25 -07:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def move_camera(self, phi = None, theta = None, distance = None, center_x = None, center_y = None, center_z = None, added_anims = [], **kwargs):
|
2017-08-30 13:16:08 -07:00
|
|
|
target_point = self.camera.get_spherical_coords(phi, theta, distance)
|
2018-03-05 18:53:13 -06:00
|
|
|
movement = ApplyMethod(self.camera.rotation_mobject.move_to, target_point, **kwargs)
|
2018-02-02 10:34:08 +01:00
|
|
|
target_center = self.camera.get_center_of_rotation(center_x, center_y, center_z)
|
2018-03-05 18:53:13 -06:00
|
|
|
movement_center = ApplyMethod(self.camera.moving_center.move_to, target_center, **kwargs)
|
2017-08-30 18:33:55 -07:00
|
|
|
is_camera_rotating = self.ambient_camera_rotation in self.continual_animations
|
|
|
|
if is_camera_rotating:
|
2017-08-24 19:05:30 -07:00
|
|
|
self.remove(self.ambient_camera_rotation)
|
2018-02-02 10:34:08 +01:00
|
|
|
self.play(movement, movement_center, *added_anims)
|
|
|
|
target_point = self.camera.get_spherical_coords(phi, theta, distance)
|
2017-08-30 18:33:55 -07:00
|
|
|
if is_camera_rotating:
|
2017-08-24 19:05:30 -07:00
|
|
|
self.add(self.ambient_camera_rotation)
|
|
|
|
|
2018-01-17 22:13:38 -08:00
|
|
|
def get_moving_mobjects(self, *animations):
|
2018-01-29 21:29:24 -08:00
|
|
|
moving_mobjects = Scene.get_moving_mobjects(self, *animations)
|
|
|
|
if self.camera.rotation_mobject in moving_mobjects:
|
|
|
|
return list_update(self.mobjects, moving_mobjects)
|
|
|
|
return moving_mobjects
|
2017-08-24 19:05:30 -07:00
|
|
|
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2017-08-24 23:12:06 -07:00
|
|
|
def should_shade_in_3d(mobject):
|
2018-03-05 18:53:13 -06:00
|
|
|
return hasattr(mobject, 'shade_in_3d') and mobject.shade_in_3d
|
|
|
|
|
2017-03-10 16:54:44 -08:00
|
|
|
|
2017-08-24 23:12:06 -07:00
|
|
|
def shade_in_3d(mobject):
|
2017-08-24 19:05:30 -07:00
|
|
|
for submob in mobject.submobject_family():
|
2017-08-24 23:12:06 -07:00
|
|
|
submob.shade_in_3d = True
|
2017-08-24 19:05:30 -07:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
|
2017-08-27 14:43:18 -07:00
|
|
|
def turn_off_3d_shading(mobject):
|
|
|
|
for submob in mobject.submobject_family():
|
|
|
|
submob.shade_in_3d = False
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
|
2017-02-23 18:14:08 -08:00
|
|
|
class ThreeDMobject(VMobject):
|
2018-03-05 18:53:13 -06:00
|
|
|
|
2017-04-17 10:19:30 -07:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
VMobject.__init__(self, *args, **kwargs)
|
2017-08-24 23:12:06 -07:00
|
|
|
shade_in_3d(self)
|
2017-02-23 18:14:08 -08:00
|
|
|
|
2017-02-18 14:02:46 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
class Cube(ThreeDMobject):
|
|
|
|
CONFIG = {'fill_opacity': 0.75,
|
|
|
|
'fill_color': BLUE,
|
|
|
|
'stroke_width': 0,
|
|
|
|
'propagate_style_to_family': True,
|
|
|
|
'side_length': 2}
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2017-03-09 15:50:40 -08:00
|
|
|
def generate_points(self):
|
2018-03-05 18:53:13 -06:00
|
|
|
for vect in (IN,
|
|
|
|
OUT,
|
|
|
|
LEFT,
|
|
|
|
RIGHT,
|
|
|
|
UP,
|
|
|
|
DOWN):
|
|
|
|
face = Square(side_length=self.side_length)
|
|
|
|
face.shift(self.side_length * OUT / 2.0)
|
|
|
|
face.apply_function(lambda p: np.dot(p, z_to_vector(vect).T))
|
|
|
|
self.add(face)
|
2017-02-13 18:43:55 -08:00
|
|
|
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
class SphereThreeD(ThreeDMobject):
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def __init__(self, r, eps):
|
|
|
|
self.r = r
|
|
|
|
self.eps = eps
|
|
|
|
ThreeDMobject.__init__(self)
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
CONFIG = {'fill_opacity': 0.75,
|
|
|
|
'fill_color': BLUE,
|
|
|
|
'stroke_width': 0,
|
|
|
|
'propagate_style_to_family': True,
|
|
|
|
'side_length': 2}
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def generate_points(self):
|
|
|
|
points = [ (self.r * (np.sin(phi) * np.cos(theta)), self.r * (np.sin(phi) * np.sin(theta)), self.r * np.cos(phi)) for phi in np.arange(0, 2 * np.pi, self.eps) for theta in np.arange(0, 2 * np.pi, self.eps) ]
|
|
|
|
for vect in points:
|
|
|
|
face = Square(side_length=self.eps)
|
|
|
|
face.apply_function(lambda p: np.dot(p, z_to_vector(vect).T))
|
|
|
|
self.add(face)
|
2017-02-13 18:43:55 -08:00
|
|
|
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
class Parametric3D(ThreeDMobject):
|
|
|
|
CONFIG = {'fill_opacity': 0.75,
|
|
|
|
'fill_color': BLUE,
|
|
|
|
'stroke_width': 0,
|
|
|
|
'propagate_style_to_family': True}
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def __init__(self, f, g, h, phi_min, phi_max, theta_min, theta_max, eps):
|
|
|
|
self.f = f
|
|
|
|
self.g = g
|
|
|
|
self.h = h
|
|
|
|
self.phi_min = phi_min
|
|
|
|
self.phi_max = phi_max
|
|
|
|
self.theta_min = theta_min
|
|
|
|
self.theta_max = theta_max
|
|
|
|
self.eps = eps
|
|
|
|
ThreeDMobject.__init__(self)
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def generate_points(self):
|
|
|
|
points = [ (self.f(phi, theta), self.g(phi, theta), self.h(phi, theta)) for phi in np.arange(self.phi_min, self.phi_max, self.eps) for theta in np.arange(self.theta_min, self.theta_max, self.eps) ]
|
|
|
|
for vect in points:
|
|
|
|
face = Square(side_length=self.eps)
|
|
|
|
scalefactor = np.linalg.norm(vect)
|
|
|
|
face.shift(scalefactor * OUT / 2.0)
|
|
|
|
face.apply_function(lambda p: np.dot(p, z_to_vector(vect).T))
|
|
|
|
self.add(face)
|
2017-02-13 18:43:55 -08:00
|
|
|
|
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
class Prism(Cube):
|
|
|
|
CONFIG = {'dimensions': [3, 2, 1]}
|
2017-02-13 18:43:55 -08:00
|
|
|
|
2018-03-05 18:53:13 -06:00
|
|
|
def generate_points(self):
|
|
|
|
Cube.generate_points(self)
|
|
|
|
for dim, value in enumerate(self.dimensions):
|
|
|
|
self.rescale_to_fit(value, dim, stretch=True)
|