示例#1
0
    def get_graph(
        self,
        func,
        color=None,
        x_min=None,
        x_max=None,
    ):
        if color is None:
            color = self.default_graph_colors_cycle.next()
        if x_min is None:
            x_min = self.x_min
        if x_max is None:
            x_max = self.x_max

        def parameterized_function(alpha):
            x = interpolate(x_min, x_max, alpha)
            y = func(x)
            if not np.isfinite(y):
                y = self.y_max
            return self.coords_to_point(x, y)

        graph = ParametricFunction(
            parameterized_function,
            color=color,
            num_anchor_points=self.num_graph_anchor_points,
        )
        graph.underlying_function = func
        return graph
示例#2
0
    def __init__(self, mob, iterations=5, radius=10, alpha=30, camera=None):
        if camera is None:
            from camera.camera import Camera
            camera = Camera()
        arr = mob.get_binary_array()
        arr = ndimage.binary_dilation(
            arr,
            structure=circular_binary_structure(radius),
            iterations=iterations,
        )
        pixel_list = np.column_stack(np.where(arr == 1)).astype("float64")

        concave_hull = list(
            alpha_shape(pixel_list, alpha=alpha, only_outer=True))

        # sort edges
        for i, first in enumerate(concave_hull):
            loop = True
            for j, second in enumerate(concave_hull[i + 1:]):
                j += i + 1
                if first[1] == second[0]:
                    loop = False
                    concave_hull[i + 1], concave_hull[j] = \
                        concave_hull[j], concave_hull[i + 1]
            if loop and i != len(concave_hull) - 1:
                warnings.warn(
                    "the alpha shape in split into different parts. This can "
                    "be fixed by increasing alpha.")
                print(i, len(concave_hull))
                # breakpoint(context=9)
                pass

        temp = np.zeros((len(concave_hull) + 1, 2))
        for i, pair in enumerate(concave_hull):
            temp[i] = pixel_list[pair[0]]
        temp[-1] = pixel_list[concave_hull[0][0]]
        pixel_list = temp

        point_list = np.zeros((pixel_list.shape[0], pixel_list.shape[1] + 1))
        point_list[:,
                   0] = pixel_list[:,
                                   0] * camera.frame_height / camera.pixel_height
        point_list[:,
                   1] = -pixel_list[:,
                                    1] * camera.frame_width / camera.pixel_width

        # TODO: figure out optimal num_anchor_points
        ParametricFunction.__init__(
            self,
            lambda t, point_list=point_list: point_list[int(t)],
            t_min=0,
            t_max=len(point_list) - 1,
            scale_handle_to_anchor_distances_after_applying_functions=False,
        )
        self.move_to(mob.get_center())
示例#3
0
 def get_graph(self,
               function,
               num_graph_points=None,
               x_min=None,
               x_max=None,
               **kwargs):
     kwargs["fill_opacity"] = kwargs.get("fill_opacity", 0)
     kwargs["num_anchor_points"] = \
         num_graph_points or self.default_num_graph_points
     x_min = x_min or self.x_min
     x_max = x_max or self.x_max
     graph = ParametricFunction(
         lambda t: self.coords_to_point(t, function(t)),
         t_min=x_min,
         t_max=x_max,
         **kwargs)
     graph.underlying_function = function
     return graph
 def get_graph(
     self, function, num_graph_points=None,
     x_min=None,
     x_max=None,
     **kwargs
 ):
     kwargs["fill_opacity"] = kwargs.get("fill_opacity", 0)
     kwargs["num_anchor_points"] = \
         num_graph_points or self.default_num_graph_points
     x_min = x_min or self.x_min
     x_max = x_max or self.x_max
     graph = ParametricFunction(
         lambda t: self.coords_to_point(t, function(t)),
         t_min=x_min,
         t_max=x_max,
         **kwargs
     )
     graph.underlying_function = function
     return graph