|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import numpy as np |
3 | 4 | import pytest |
4 | 5 |
|
5 | | -from manim import Mobject |
| 6 | +from manim import DL, UR, Circle, Mobject, Rectangle, Square, VGroup |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def test_mobject_add(): |
@@ -49,3 +50,110 @@ def test_mobject_remove(): |
49 | 50 | assert len(obj.submobjects) == 10 |
50 | 51 |
|
51 | 52 | assert obj.remove(Mobject()) is obj |
| 53 | + |
| 54 | + |
| 55 | +def test_mobject_dimensions_single_mobject(): |
| 56 | + # A Mobject with no points and no submobjects has no dimensions |
| 57 | + empty = Mobject() |
| 58 | + assert empty.width == 0 |
| 59 | + assert empty.height == 0 |
| 60 | + assert empty.depth == 0 |
| 61 | + |
| 62 | + has_points = Mobject() |
| 63 | + has_points.points = np.array([[-1, -2, -3], [1, 3, 5]]) |
| 64 | + assert has_points.width == 2 |
| 65 | + assert has_points.height == 5 |
| 66 | + assert has_points.depth == 8 |
| 67 | + |
| 68 | + rect = Rectangle(width=3, height=5) |
| 69 | + |
| 70 | + assert rect.width == 3 |
| 71 | + assert rect.height == 5 |
| 72 | + assert rect.depth == 0 |
| 73 | + |
| 74 | + # Dimensions should be recalculated after scaling |
| 75 | + rect.scale(2.0) |
| 76 | + assert rect.width == 6 |
| 77 | + assert rect.height == 10 |
| 78 | + assert rect.depth == 0 |
| 79 | + |
| 80 | + # Dimensions should not be dependent on location |
| 81 | + rect.move_to([-3, -4, -5]) |
| 82 | + assert rect.width == 6 |
| 83 | + assert rect.height == 10 |
| 84 | + assert rect.depth == 0 |
| 85 | + |
| 86 | + circ = Circle(radius=2) |
| 87 | + |
| 88 | + assert circ.width == 4 |
| 89 | + assert circ.height == 4 |
| 90 | + assert circ.depth == 0 |
| 91 | + |
| 92 | + |
| 93 | +def is_close(x, y): |
| 94 | + return abs(x - y) < 0.00001 |
| 95 | + |
| 96 | + |
| 97 | +def test_mobject_dimensions_nested_mobjects(): |
| 98 | + vg = VGroup() |
| 99 | + |
| 100 | + for x in range(-5, 8, 1): |
| 101 | + row = VGroup() |
| 102 | + vg += row |
| 103 | + for y in range(-17, 2, 1): |
| 104 | + for z in range(0, 10, 1): |
| 105 | + s = Square().move_to([x, y, z / 10]) |
| 106 | + row += s |
| 107 | + |
| 108 | + assert vg.width == 14.0, vg.width |
| 109 | + assert vg.height == 20.0, vg.height |
| 110 | + assert is_close(vg.depth, 0.9), vg.depth |
| 111 | + |
| 112 | + # Dimensions should be recalculated after scaling |
| 113 | + vg.scale(0.5) |
| 114 | + assert vg.width == 7.0, vg.width |
| 115 | + assert vg.height == 10.0, vg.height |
| 116 | + assert is_close(vg.depth, 0.45), vg.depth |
| 117 | + |
| 118 | + # Adding a mobject changes the bounds/dimensions |
| 119 | + rect = Rectangle(width=3, height=5) |
| 120 | + rect.move_to([9, 3, 1]) |
| 121 | + vg += rect |
| 122 | + assert vg.width == 13.0, vg.width |
| 123 | + assert is_close(vg.height, 18.5), vg.height |
| 124 | + assert is_close(vg.depth, 0.775), vg.depth |
| 125 | + |
| 126 | + |
| 127 | +def test_mobject_dimensions_mobjects_with_no_points_are_at_origin(): |
| 128 | + rect = Rectangle(width=2, height=3) |
| 129 | + rect.move_to([-4, -5, 0]) |
| 130 | + outer_group = VGroup(rect) |
| 131 | + |
| 132 | + # This is as one would expect |
| 133 | + assert outer_group.width == 2 |
| 134 | + assert outer_group.height == 3 |
| 135 | + |
| 136 | + # Adding a mobject with no points has a quirk of adding a "point" |
| 137 | + # to [0, 0, 0] (the origin). This changes the size of the outer |
| 138 | + # group because now the bottom left corner is at [-5, -6.5, 0] |
| 139 | + # but the upper right corner is [0, 0, 0] instead of [-3, -3.5, 0] |
| 140 | + outer_group.add(VGroup()) |
| 141 | + assert outer_group.width == 5 |
| 142 | + assert outer_group.height == 6.5 |
| 143 | + |
| 144 | + |
| 145 | +def test_mobject_dimensions_has_points_and_children(): |
| 146 | + outer_rect = Rectangle(width=3, height=6) |
| 147 | + inner_rect = Rectangle(width=2, height=1) |
| 148 | + inner_rect.align_to(outer_rect.get_corner(UR), DL) |
| 149 | + outer_rect.add(inner_rect) |
| 150 | + |
| 151 | + # The width of a mobject should depend both on its points and |
| 152 | + # the points of all children mobjects. |
| 153 | + assert outer_rect.width == 5 # 3 from outer_rect, 2 from inner_rect |
| 154 | + assert outer_rect.height == 7 # 6 from outer_rect, 1 from inner_rect |
| 155 | + assert outer_rect.depth == 0 |
| 156 | + |
| 157 | + assert inner_rect.width == 2 |
| 158 | + assert inner_rect.height == 1 |
| 159 | + assert inner_rect.depth == 0 |
0 commit comments