Fun hacky solution to MoreFiltersMoreLight scene

This commit is contained in:
Grant Sanderson
2017-08-30 18:33:55 -07:00
parent ea8381de71
commit 231df4740f
3 changed files with 156 additions and 12 deletions

59
bell.py
View File

@ -315,11 +315,9 @@ class MoreFiltersMoreLight(FilterScene):
def construct(self):
self.remove(self.axes)
pfs = self.pol_filters
for pf in pfs:
pf.set_fill(WHITE, opacity = 0.25)
pf.arrow.set_fill(opacity = 1)
turn_off_3d_shading(pfs)
self.color_filters(pfs)
self.remove(pfs)
self.build_color_map(pfs)
self.add(pfs[4], pfs[2], pfs[0])
self.move_camera(
@ -340,6 +338,59 @@ class MoreFiltersMoreLight(FilterScene):
)
self.dither()
def color_filters(self, pfs):
colors = [RED, GREEN, BLUE, MAROON_B, PURPLE_C]
for pf, color in zip(pfs, colors):
pf.set_fill(color, 0.5)
pf.arrow.set_fill(WHITE, 1)
turn_off_3d_shading(pfs)
def build_color_map(self, pfs):
phi, theta = self.camera.get_phi(), self.camera.get_theta()
self.set_camera_position(np.pi/2, -np.pi)
self.original_rgbs = [(255, 255, 255)]
self.new_rgbs = [(255, 255, 255)]
for bool_array in it.product(*5*[[True, False]]):
pfs_to_use = VGroup(*[
pf
for pf, b in reversed(zip(pfs, bool_array))
if b
])
self.camera.capture_mobject(pfs_to_use)
frame = self.camera.get_image()
h, w, three = frame.shape
rgb = frame[3*h/8, 7*w/12]
self.original_rgbs.append(rgb)
angles = [pf.filter_angle for pf in pfs_to_use]
p = 0.5
for a1, a2 in zip(angles, angles[1:]):
p *= np.cos(a2 - a1)**2
new_rgb = (255*p*np.ones(3)).astype(int)
if not any(bool_array):
new_rgb = [0, 0, 0]
self.new_rgbs.append(new_rgb)
self.camera.reset()
self.set_camera_position(phi, theta)
def get_frame(self):
frame = FilterScene.get_frame(self)
bool_arrays = [
(frame[:,:,0] == r) & (frame[:,:,1] == g) & (frame[:,:,2] == b)
for (r, g, b) in self.original_rgbs
]
for ba, new_rgb in zip(bool_arrays, self.new_rgbs):
frame[ba] = new_rgb
covered = reduce(
lambda b1, b2 : b1 | b2,
bool_arrays
)
frame[~covered] = [127, 127, 127]
return frame
class ConfusedPiCreature(Scene):
def construct(self):
randy = Randolph()

View File

@ -97,9 +97,9 @@ class ThreeDCamera(CameraWithPerspective):
def get_spherical_coords(self, phi = None, theta = None, distance = None):
curr_phi, curr_theta, curr_d = self.rotation_mobject.points[0]
phi = phi or curr_phi
theta = theta or curr_theta
distance = distance or curr_d
if phi is None: phi = curr_phi
if theta is None: theta = curr_theta
if distance is None: distance = curr_d
return np.array([phi, theta, distance])
def get_phi(self):
@ -137,6 +137,7 @@ class ThreeDCamera(CameraWithPerspective):
class ThreeDScene(Scene):
CONFIG = {
"camera_class" : ThreeDCamera,
"ambient_camera_rotation" : None,
}
def set_camera_position(self, phi = None, theta = None, distance = None):
@ -152,6 +153,7 @@ class ThreeDScene(Scene):
def stop_ambient_camera_rotation(self):
self.remove(self.ambient_camera_rotation)
self.ambient_camera_rotation = None
def move_camera(
self,
@ -165,10 +167,11 @@ class ThreeDScene(Scene):
target_point,
**kwargs
)
if hasattr(self, "ambient_camera_rotation"):
is_camera_rotating = self.ambient_camera_rotation in self.continual_animations
if is_camera_rotating:
self.remove(self.ambient_camera_rotation)
self.play(movement, *added_anims)
if hasattr(self, "ambient_camera_rotation"):
if is_camera_rotating:
self.add(self.ambient_camera_rotation)
def separate_moving_and_static_mobjects(self, *animations):

View File

@ -801,9 +801,99 @@ class ListRelevantWaveIdeas(TeacherStudentsScene):
)
self.dither(5)
class DirectWaveOutOfScreen(IntroduceEMWave):
CONFIG = {
"EMWave_config" : {
"requires_start_up" : False,
"amplitude" : 2,
"start_point" : SPACE_WIDTH*LEFT,
"A_vect" : [0, 1, 0],
"start_up_time" : 0,
}
}
def setup(self):
IntroduceEMWave.setup(self)
self.remove(self.axes)
for ov in self.em_wave.continual_animations:
ov.vector.normal_vector = RIGHT
self.set_camera_position(0.9*np.pi/2, -0.3*np.pi)
def construct(self):
self.move_into_position()
def move_into_position(self):
self.dither(2)
self.continual_update()
faded_vectors = VGroup(*[
ov.vector
for ov in self.em_wave.continual_animations[:-2]
])
self.move_camera(
0.99*np.pi/2, -0.01,
run_time = 2,
added_anims = [faded_vectors.set_fill, None, 0.5]
)
self.stop_ambient_camera_rotation()
self.move_camera(
np.pi/2, 0,
added_anims = [faded_vectors.set_fill, None, 0.05],
run_time = 2,
)
self.play(
self.em_wave.M_vects.set_fill, None, 0
)
self.dither(2)
self.play(faded_vectors.set_fill, None, 0)
self.dither(4)
class ShowVectorEquation(Scene):
def construct(self):
self.force_skipping()
self.add_vector()
self.add_plane()
self.write_horizontally_polarized()
self.write_components()
self.show_graph()
self.add_phi()
self.add_amplitude()
self.add_kets()
self.switch_to_vertically_polarized_light()
def add_vector(self):
self.vector = Vector(2*RIGHT, color = E_COLOR)
self.oscillating_vector = OscillatingVector(
self.vector,
A_vect = [2, 0, 0],
frequency = 0.25,
)
self.add(self.oscillating_vector)
self.revert_to_original_skipping_status()
self.dither(3)
def add_plane(self):
pass
def write_horizontally_polarized(self):
pass
def write_components(self):
pass
def show_graph(self):
pass
def add_phi(self):
pass
def add_amplitude(self):
pass
def add_kets(self):
pass
def switch_to_vertically_polarized_light(self):
pass