Turning a coordinate or object to a coordinate (i.e. make _pointify publicly available)
Description of proposed feature
In some libraries I don’t want to consider whether the user gives a coordinate or a mobject as input. For instance, to compute the barycenter between two points or objects (it’s also weird to see that no such native function exists in Manim.
I’d therefore love to have:
- a function to convert a point/object to coordinate, like:
def to_point(mob_or_point, direction=None):
if isinstance(mob_or_point, (Mobject, OpenGLMobject)):
mob = mob_or_point
if direction is None:
return mob.get_center()
else:
return mob.get_boundary_point(direction)
return np.array(mob_or_point)
- a function to compute the barycenter between two objects, like:
def barycenter(obj1, obj2, alpha=1/2): ## Ideally: also work for a set of points, where alpha is a list of size n-1
return (1-alpha)*to_point(obj1) + alpha*to_point(obj2)
It could for instance improve the midpoint function (https://docs.manim.community/en/stable/_modules/manim/utils/space_ops.html#midpoint) to work on arbitrary objects with a parameter alpha.
How can the new feature be used?
In many ways. For instance to define animations where we need to place points depending on other points.
or you just pass get_center() manually, i think this makes too many assumptions about what the user wants because what if you actually don't want the center to be used, then you have to add an extra parameter or whata if it doesn't account for mobjects that actually don't possess that function and so on. Then soon we would have many issues complaining about the function not doing what it should do even if it does and it would all end up in a mess.
So i would just manually pass the coordinates because functions should not randomly accept mobjects and assume they know what a user wants.
But maybe the other devs have different opinions on that.
Note to devs: If we add a center point to each Mobject this might actually be possible in terms of experimental.
I wouldn't really say it works for the direction part easily but in combination with #3238
it would be easier to combine the two maybe with some special syntax like mob @ direction and then it becomes a point ?
For the rotation/scale can't we simply add an invisible cube of size 1x1x1 (resetable at any time to "apply" the scale) in the mobject, to recover the applied transformations?
Can you elaborate on that?
Well, if we can somehow add a special invisible mobject/set of points (let's say a cube where one vertex O is initially located at (0,0,0), vertex A is at (1,0,0) vertex B at (0,1,0), C is at (0,0,1)) to any mobject that scales like any other point except that it stays invisible, then it becomes easy to recover the translation/scale/rotation applied to any mobject:
- the translation is the new position of O
- the scale on axis X is the distance between O and A (note that here the scale is relative to the "relative" coordinate system and not to the absolute xzy position)
- the scale on axis Y is the distance between O and B
- the scale on axis Z is the distance between O and C
- the average scale is the average of the X-Y-Z scales
- the rotation is computed similarly, like if we have only rotations in the X-Y plane the angle is the angle between (1,0,0) and A-O, for 3D rotations we can compute the matrix directly…
And to reset the translation/scale and/or rotation we just update the coordinates of the cube (but not of the other vertices) by, respectively:
- substracting O to A,B, and C
- defining A = O+(A-O)/|A-O|, same for other points
- A = O+(1,0,0)*|A-O| or alike To reset both translation/scale/rotation we just set O,A,B,C to their original value (0,0,0), (1,0,0)…
I meant more like, what would this solve for this issue? Sorry if i was not clear on that. Because using the mobjects as points is not related to reverting transforms? + It would be pretty difficult because we can actually do non linear transforms
So if you might help me understand how the reverting transforms is related to using mobjects as points that would be great!
Oh sorry, I got confused since my internet connection was bad I just answered the email without checking the actual issue, thinking that it was something else ^^ So it is indeed not directly related to the original question, but if you implement this functionality, I'd love to also have this more general case where we add these 4 points. Or, even better, also allow the user to add additional invisible points to implement anchors (which is not existing in Manim except for the basic center/left/right/… which is not available in Manim without faking them by creating new methods to the object).
The reason is that sometimes I want to be able to get the position/scale/rotation of an object, and do something based on that. For instance, we can implement this way an updater on a second object whose scale/rotation matches the scale/rotation of the tracked object…
One possibility is to manually keep a record of all rotations that we applied, but it might not be trivial to maintain… This becomes nearly impossible to do when considering complex combinations of animations with ease-in/ease-out functions, or stuff like MoveAlongPath that would update orientation as well based on the tangeant.