Performance improved in set_color_by_rgba_func (#2316)

* removing 1 in neg axis if unit_tex is specified

* performance improved in `set_color_by_rgba_func`

* resolving imag axis number mob in ComplexPlane
This commit is contained in:
Varniex 2025-03-21 00:26:29 +05:30 committed by GitHub
parent 7a61a13691
commit dbfe7ac75d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 21 deletions

View file

@ -770,13 +770,6 @@ class ComplexPlane(NumberPlane):
axis = self.get_x_axis() axis = self.get_x_axis()
value = z.real value = z.real
number_mob = axis.get_number_mobject(value, font_size=font_size, **kwargs) number_mob = axis.get_number_mobject(value, font_size=font_size, **kwargs)
# For -i, remove the "1"
if z.imag == -1:
number_mob.remove(number_mob[1])
number_mob[0].next_to(
number_mob[1], LEFT,
buff=number_mob[0].get_width() / 4
)
self.coordinate_labels.add(number_mob) self.coordinate_labels.add(number_mob)
self.add(self.coordinate_labels) self.add(self.coordinate_labels)
return self return self

View file

@ -52,7 +52,7 @@ SubmobjectType = TypeVar('SubmobjectType', bound='Mobject')
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Callable, Iterator, Union, Tuple, Optional, Any from typing import Callable, Iterator, Union, Tuple, Optional, Any
import numpy.typing as npt import numpy.typing as npt
from manimlib.typing import ManimColor, Vect3, Vect4, Vect3Array, UniformDict, Self from manimlib.typing import ManimColor, Vect3, Vect4Array, Vect3Array, UniformDict, Self
from moderngl.context import Context from moderngl.context import Context
T = TypeVar('T') T = TypeVar('T')
@ -287,10 +287,7 @@ class Mobject(object):
about_point = self.get_bounding_box_point(about_edge) about_point = self.get_bounding_box_point(about_edge)
for mob in self.get_family(): for mob in self.get_family():
arrs = [] arrs = [mob.data[key] for key in mob.pointlike_data_keys if mob.has_points()]
if mob.has_points():
for key in mob.pointlike_data_keys:
arrs.append(mob.data[key])
if works_on_bounding_box: if works_on_bounding_box:
arrs.append(mob.get_bounding_box()) arrs.append(mob.get_bounding_box())
@ -1323,20 +1320,19 @@ class Mobject(object):
def set_color_by_rgba_func( def set_color_by_rgba_func(
self, self,
func: Callable[[Vect3], Vect4], func: Callable[[Vect3Array], Vect4Array],
recurse: bool = True recurse: bool = True
) -> Self: ) -> Self:
""" """
Func should take in a point in R3 and output an rgba value Func should take in a point in R3 and output an rgba value
""" """
for mob in self.get_family(recurse): for mob in self.get_family(recurse):
rgba_array = [func(point) for point in mob.get_points()] mob.set_rgba_array(func(mob.get_points()))
mob.set_rgba_array(rgba_array)
return self return self
def set_color_by_rgb_func( def set_color_by_rgb_func(
self, self,
func: Callable[[Vect3], Vect3], func: Callable[[Vect3Array], Vect3Array],
opacity: float = 1, opacity: float = 1,
recurse: bool = True recurse: bool = True
) -> Self: ) -> Self:
@ -1344,8 +1340,9 @@ class Mobject(object):
Func should take in a point in R3 and output an rgb value Func should take in a point in R3 and output an rgb value
""" """
for mob in self.get_family(recurse): for mob in self.get_family(recurse):
rgba_array = [[*func(point), opacity] for point in mob.get_points()] points = mob.get_points()
mob.set_rgba_array(rgba_array) opacity = np.ones((points.shape[0], 1)) * opacity
mob.set_rgba_array(np.hstack((func(points), opacity)))
return self return self
@affects_family_data @affects_family_data

View file

@ -182,9 +182,13 @@ class NumberLine(Line):
if x < 0 and direction[0] == 0: if x < 0 and direction[0] == 0:
# Align without the minus sign # Align without the minus sign
num_mob.shift(num_mob[0].get_width() * LEFT / 2) num_mob.shift(num_mob[0].get_width() * LEFT / 2)
if x == unit and unit_tex: if abs(x) == unit and unit_tex:
center = num_mob.get_center() center = num_mob.get_center()
num_mob.remove(num_mob[0]) if x > 0:
num_mob.remove(num_mob[0])
else:
num_mob.remove(num_mob[1])
num_mob[0].next_to(num_mob[1], LEFT, buff=num_mob[0].get_width() / 4)
num_mob.move_to(center) num_mob.move_to(center)
return num_mob return num_mob