Skip to content

Refactor/put start and end on#4658

Open
GoThrones wants to merge 3 commits intoManimCommunity:mainfrom
GoThrones:refactor/put_start_and_end_on
Open

Refactor/put start and end on#4658
GoThrones wants to merge 3 commits intoManimCommunity:mainfrom
GoThrones:refactor/put_start_and_end_on

Conversation

@GoThrones
Copy link
Copy Markdown
Contributor

Overview: What does this pull request change?

This PR refactors the method "put_start_and_end_on" in mobject.py and opengl_mobject.py:

Replaces self.points = np.array(start) with self.shift(np.asarray(start) - current_start) in the case of a mobject whose start and end point are the same, in both mobject.py and opengl_mobject.py

Adds a warning that warns the user that the mobject which is calling this method is a loop, and it will be shifted.

Some variable names have been changed for better reading.

Motivation and Explanation: Why and how do your changes improve the library?

The put_start_and_end_on method in both mobject.py and opengl_mobject.py previously used self.points = np.array(start) when the mobject had its start and end as the same point. This collapsed all of the mobject's points into a single Point3D, destroying its geometry entirely. For example, calling this method on a Circle or Square would reduce it to a single point, making it cease to exist visually.
This PR fixes the issue by replacing that line with self.shift(np.asarray(start) - current_start), which simply repositions the mobject to the start point while fully preserving its geometry.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Copy link
Copy Markdown
Member

@chopan050 chopan050 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Shifting the Mobject in this case makes sense, and I appreciate both expanding the variable names and making the behavior consistent in Mobject and OpenGLMobject.

I left some change requests:

Comment on lines -1947 to -1949
# TODO: this looks broken. It makes self.points a Point3D instead
# of a Point3D_Array. However, modifying this breaks some tests
# where this is currently expected.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this old TODO mentioned, tests break when modifying this. In particular, there is a failing test in tests/module/mobject/graphing/test_number_line.py which must be fixed in order to approve and merge this PR:

def test_start_and_end_at_same_point():
    line = DashedLine(np.zeros(3), np.zeros(3))
    line.put_start_and_end_on(np.zeros(3), np.array([0, 0, 0]))

    np.testing.assert_array_equal(np.round(np.zeros(3), 4), np.round(line.points, 4))

Modifying the last assertion to

    np.testing.assert_array_equal(np.round(line.points, 4), np.round(np.zeros((4, 3)), 4))

should do the trick. Notice that:

  • I flipped the argument order, because the 1st one should be the actual value and the 2nd one should be the desired value.
  • The desired array is now np.zeros((4, 3)) instead of np.zeros(3).

Comment on lines +1947 to +1949
# Previously self.points = np.array(start) was used here, but it would have collapsed
# all points to a single Point3D instead of shifting the mobject.
# Fixed by using shift instead.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that it is not necessary to leave a comment on how the old TODO was fixed:

Suggested change
# Previously self.points = np.array(start) was used here, but it would have collapsed
# all points to a single Point3D instead of shifting the mobject.
# Fixed by using shift instead.

Comment on lines +2140 to +2142
# Previously self.points = np.array(start) was used here, but it would have collapsed
# all points to a single Point3D instead of shifting the mobject.
# Fixed by using shift instead.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Previously self.points = np.array(start) was used here, but it would have collapsed
# all points to a single Point3D instead of shifting the mobject.
# Fixed by using shift instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants