|
3 | 3 | import numpy as np |
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from manim import DL, UR, Circle, Mobject, Rectangle, Square, VGroup |
| 6 | +from manim import DL, PI, UR, Circle, Mobject, Rectangle, Square, Triangle, VGroup |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def test_mobject_add(): |
@@ -168,3 +168,78 @@ def test_mobject_dimensions_has_points_and_children(): |
168 | 168 | assert inner_rect.width == 2 |
169 | 169 | assert inner_rect.height == 1 |
170 | 170 | assert inner_rect.depth == 0 |
| 171 | + |
| 172 | + |
| 173 | +def test_rotate_about_vertex_view(): |
| 174 | + """Test that rotating about a vertex obtained from get_vertices() works correctly. |
| 175 | +
|
| 176 | + This is a regression test for an issue where get_vertices() returns a view of the points array, |
| 177 | + and using it as about_point in rotate() would cause the view to be mutated. |
| 178 | + """ |
| 179 | + triangle = Triangle() |
| 180 | + original_vertices = triangle.get_vertices().copy() |
| 181 | + first_vertex = original_vertices[0].copy() |
| 182 | + |
| 183 | + # This should rotate about the first vertex without corrupting it |
| 184 | + triangle.rotate(PI / 2, about_point=triangle.get_vertices()[0]) |
| 185 | + |
| 186 | + # The first vertex should remain in the same position (within numerical precision) |
| 187 | + rotated_vertices = triangle.get_vertices() |
| 188 | + np.testing.assert_allclose(rotated_vertices[0], first_vertex, atol=1e-6) |
| 189 | + |
| 190 | + |
| 191 | +def test_scale_about_vertex_view(): |
| 192 | + """Test that scaling about a vertex obtained from get_vertices() works correctly. |
| 193 | +
|
| 194 | + This is a regression test for an issue where get_vertices() returns a view of the points array, |
| 195 | + and using it as about_point in scale() would cause the view to be mutated. |
| 196 | + """ |
| 197 | + triangle = Triangle() |
| 198 | + original_vertices = triangle.get_vertices().copy() |
| 199 | + first_vertex = original_vertices[0].copy() |
| 200 | + |
| 201 | + # This should scale about the first vertex without corrupting it |
| 202 | + triangle.scale(2, about_point=triangle.get_vertices()[0]) |
| 203 | + |
| 204 | + # The first vertex should remain in the same position (within numerical precision) |
| 205 | + scaled_vertices = triangle.get_vertices() |
| 206 | + np.testing.assert_allclose(scaled_vertices[0], first_vertex, atol=1e-6) |
| 207 | + |
| 208 | + |
| 209 | +def test_stretch_about_vertex_view(): |
| 210 | + """Test that stretching about a vertex obtained from get_vertices() works correctly. |
| 211 | +
|
| 212 | + This is a regression test for an issue where get_vertices() returns a view of the points array, |
| 213 | + and using it as about_point in stretch() would cause the view to be mutated. |
| 214 | + """ |
| 215 | + triangle = Triangle() |
| 216 | + original_vertices = triangle.get_vertices().copy() |
| 217 | + first_vertex = original_vertices[0].copy() |
| 218 | + |
| 219 | + # This should stretch about the first vertex without corrupting it |
| 220 | + triangle.stretch(2, 0, about_point=triangle.get_vertices()[0]) |
| 221 | + |
| 222 | + # The first vertex should remain in the same position (within numerical precision) |
| 223 | + stretched_vertices = triangle.get_vertices() |
| 224 | + np.testing.assert_allclose(stretched_vertices[0], first_vertex, atol=1e-6) |
| 225 | + |
| 226 | + |
| 227 | +def test_apply_matrix_about_vertex_view(): |
| 228 | + """Test that apply_matrix about a vertex obtained from get_vertices() works correctly. |
| 229 | +
|
| 230 | + This is a regression test for an issue where get_vertices() returns a view of the points array, |
| 231 | + and using it as about_point in apply_matrix() would cause the view to be mutated. |
| 232 | + """ |
| 233 | + triangle = Triangle() |
| 234 | + original_vertices = triangle.get_vertices().copy() |
| 235 | + first_vertex = original_vertices[0].copy() |
| 236 | + |
| 237 | + # Define a rotation matrix (90 degrees rotation around z-axis) |
| 238 | + rotation_matrix = np.array([[0, -1, 0], [1, 0, 0], [0, 0, 1]]) |
| 239 | + |
| 240 | + # This should apply the matrix about the first vertex without corrupting it |
| 241 | + triangle.apply_matrix(rotation_matrix, about_point=triangle.get_vertices()[0]) |
| 242 | + |
| 243 | + # The first vertex should remain in the same position (within numerical precision) |
| 244 | + transformed_vertices = triangle.get_vertices() |
| 245 | + np.testing.assert_allclose(transformed_vertices[0], first_vertex, atol=1e-6) |
0 commit comments