Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added support for PySide coordinate system (#2537)

Kevin Cen1e437d658753
diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.py
index c84b1d727..cce012b76 100644
--- a/manim/renderer/opengl_renderer.py
+++ b/manim/renderer/opengl_renderer.py
@@ -568,7 +568,9 @@ class OpenGLRenderer:
return np_buf
# Returns offset from the bottom left corner in pixels.
- def pixel_coords_to_space_coords(self, px, py, relative=False):
+ # top_left flag should be set to True when using a GUI framework
+ # where the (0,0) is at the top left: e.g. PySide6
+ def pixel_coords_to_space_coords(self, px, py, relative=False, top_left=False):
pixel_shape = self.get_pixel_shape()
if pixel_shape is None:
return np.array([0, 0, 0])
@@ -580,7 +582,9 @@ class OpenGLRenderer:
else:
# Only scale wrt one axis
scale = fh / ph
- return fc + scale * np.array([(px - pw / 2), (py - ph / 2), 0])
+ return fc + scale * np.array(
+ [(px - pw / 2), (-1 if top_left else 1) * (py - ph / 2), 0]
+ )
@property
def background_color(self):
diff --git a/tests/test_scene_rendering/opengl/test_opengl_renderer.py b/tests/test_scene_rendering/opengl/test_opengl_renderer.py
index e3d69c053..188bbb58a 100644
--- a/tests/test_scene_rendering/opengl/test_opengl_renderer.py
+++ b/tests/test_scene_rendering/opengl/test_opengl_renderer.py
@@ -80,3 +80,29 @@ def test_get_frame_with_preview_enabled(use_opengl_renderer):
# height and width are flipped
assert renderer.get_pixel_shape()[0] == frame.shape[1]
assert renderer.get_pixel_shape()[1] == frame.shape[0]
+
+
[email protected]("enable_preview", [True])
+def test_pixel_coords_to_space_coords(use_opengl_renderer):
+ scene = SquareToCircle()
+ assert isinstance(scene.renderer, OpenGLRenderer)
+
+ renderer = scene.renderer
+ renderer.update_frame(scene)
+
+ px, py = 3, 2
+ pw, ph = renderer.get_pixel_shape()
+ _, fh = renderer.camera.get_shape()
+ fc = renderer.camera.get_center()
+
+ ex = fc[0] + (fh / ph) * (px - pw / 2)
+ ey = fc[1] + (fh / ph) * (py - ph / 2)
+ ez = fc[2]
+
+ assert (
+ renderer.pixel_coords_to_space_coords(px, py) == np.array([ex, ey, ez])
+ ).all()
+ assert (
+ renderer.pixel_coords_to_space_coords(px, py, top_left=True)
+ == np.array([ex, -ey, ez])
+ ).all()