Skip to content

Commit 73f87bd

Browse files
icedcoffeeeepre-commit-ci[bot]WampyCakes
authored
Implementing a Zoom parameter to :class:~.ThreeDScene (#1929)
* add a zoom parameter * adding zoom trackers * implementing zoom in three_d_scene * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated typing error * added test * updated test Co-authored-by: Iced-Tea3 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: KingWampy <[email protected]>
1 parent f63b967 commit 73f87bd

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

manim/camera/three_d_camera.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(
3333
phi=0,
3434
theta=-90 * DEGREES,
3535
gamma=0,
36+
zoom=1,
3637
**kwargs
3738
):
3839
"""Initializes the ThreeDCamera
@@ -50,6 +51,7 @@ def __init__(
5051
self.phi = phi
5152
self.theta = theta
5253
self.gamma = gamma
54+
self.zoom = zoom
5355
self.shading_factor = shading_factor
5456
self.default_distance = default_distance
5557
self.light_source_start_point = light_source_start_point
@@ -61,6 +63,7 @@ def __init__(
6163
self.theta_tracker = ValueTracker(self.theta)
6264
self.distance_tracker = ValueTracker(self.distance)
6365
self.gamma_tracker = ValueTracker(self.gamma)
66+
self.zoom_tracker = ValueTracker(self.zoom)
6467
self.fixed_orientation_mobjects = {}
6568
self.fixed_in_frame_mobjects = set()
6669
self.reset_rotation_matrix()
@@ -90,6 +93,7 @@ def get_value_trackers(self):
9093
self.theta_tracker,
9194
self.distance_tracker,
9295
self.gamma_tracker,
96+
self.zoom_tracker,
9397
]
9498

9599
def modified_rgbas(self, vmobject, rgbas):
@@ -178,6 +182,16 @@ def get_gamma(self):
178182
"""
179183
return self.gamma_tracker.get_value()
180184

185+
def get_zoom(self):
186+
"""Returns the zoom amount of the camera.
187+
188+
Returns
189+
-------
190+
float
191+
The zoom amount of the camera.
192+
"""
193+
return self.zoom_tracker.get_value()
194+
181195
def set_phi(self, value):
182196
"""Sets the polar angle i.e the angle between Z_AXIS and Camera through ORIGIN in radians.
183197
@@ -218,6 +232,16 @@ def set_gamma(self, value):
218232
"""
219233
self.gamma_tracker.set_value(value)
220234

235+
def set_zoom(self, value):
236+
"""Sets the zoom amount of the camera.
237+
238+
Parameters
239+
----------
240+
value : int, float
241+
The zoom amount of the camera.
242+
"""
243+
self.zoom_tracker.set_value(value)
244+
221245
def reset_rotation_matrix(self):
222246
"""Sets the value of self.rotation_matrix to
223247
the matrix corresponding to the current position of the camera
@@ -271,6 +295,7 @@ def project_points(self, points):
271295
"""
272296
frame_center = self.frame_center
273297
distance = self.get_distance()
298+
zoom = self.get_zoom()
274299
rot_matrix = self.get_rotation_matrix()
275300

276301
points = points - frame_center
@@ -288,7 +313,7 @@ def project_points(self, points):
288313
else:
289314
factor = distance / (distance - zs)
290315
factor[(distance - zs) < 0] = 10 ** 6
291-
points[:, i] *= factor
316+
points[:, i] *= factor * zoom
292317
return points
293318

294319
def project_point(self, point):

manim/scene/three_d_scene.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def set_camera_orientation(
5151
phi: Optional[float] = None,
5252
theta: Optional[float] = None,
5353
gamma: Optional[float] = None,
54+
zoom: Optional[float] = None,
5455
distance: Optional[float] = None,
5556
frame_center: Optional[Union["Mobject", Sequence[float]]] = None,
5657
):
@@ -71,6 +72,9 @@ def set_camera_orientation(
7172
gamma : int or float, optional
7273
The rotation of the camera about the vector from the ORIGIN to the Camera.
7374
75+
zoom : float, optional
76+
The zoom factor of the scene.
77+
7478
frame_center : list, tuple or np.array, optional
7579
The new center of the camera frame in cartesian coordinates.
7680
@@ -83,6 +87,8 @@ def set_camera_orientation(
8387
self.renderer.camera.set_distance(distance)
8488
if gamma is not None:
8589
self.renderer.camera.set_gamma(gamma)
90+
if zoom is not None:
91+
self.renderer.camera.set_zoom(zoom)
8692
if frame_center is not None:
8793
self.renderer.camera._frame_center.move_to(frame_center)
8894

@@ -166,6 +172,7 @@ def move_camera(
166172
phi: Optional[float] = None,
167173
theta: Optional[float] = None,
168174
gamma: Optional[float] = None,
175+
zoom: Optional[float] = None,
169176
distance: Optional[float] = None,
170177
frame_center: Optional[Union["Mobject", Sequence[float]]] = None,
171178
added_anims: Iterable["Animation"] = [],
@@ -189,6 +196,9 @@ def move_camera(
189196
gamma : int or float, optional
190197
The rotation of the camera about the vector from the ORIGIN to the Camera.
191198
199+
zoom : int or float, optional
200+
The zoom factor of the camera.
201+
192202
frame_center : list, tuple or np.array, optional
193203
The new center of the camera frame in cartesian coordinates.
194204
@@ -202,6 +212,7 @@ def move_camera(
202212
(theta, self.renderer.camera.theta_tracker),
203213
(distance, self.renderer.camera.distance_tracker),
204214
(gamma, self.renderer.camera.gamma_tracker),
215+
(zoom, self.renderer.camera.zoom_tracker),
205216
]
206217
for value, tracker in value_tracker_pairs:
207218
if value is not None:
-3.74 KB
Binary file not shown.
Binary file not shown.

tests/test_graphical_units/test_threed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ def test_CameraMoveAxes(scene):
5555
axes = ThreeDAxes()
5656
scene.add(axes)
5757
scene.add(Dot([1, 2, 3]))
58-
scene.move_camera(phi=PI / 8, theta=-PI / 8, frame_center=[1, 2, 3])
58+
scene.move_camera(phi=PI / 8, theta=-PI / 8, frame_center=[1, 2, 3], zoom=2)
5959

6060

6161
@frames_comparison(base_scene=ThreeDScene)
6262
def test_CameraMove(scene):
6363
cube = Cube()
6464
scene.add(cube)
65-
scene.move_camera(phi=PI / 4, theta=PI / 4, frame_center=[0, 0, -1])
65+
scene.move_camera(phi=PI / 4, theta=PI / 4, frame_center=[0, 0, -1], zoom=0.5)
6666

6767

6868
@frames_comparison(base_scene=ThreeDScene)

0 commit comments

Comments
 (0)