Ejemplo n.º 1
0
    def from_atlas(cls, atlas, chart_id):
        # Bounding box variables of the chart
        low = Coord(atlas.rows - 1, atlas.cols - 1)
        high = Coord(0, 0)

        # Search the atlas for the chart and find the bounding
        for i in range(atlas.rows):
            for j in range(atlas.cols):
                tmp_id = atlas[i, j]
                if tmp_id == chart_id:
                    low.x = min(low.x, i)
                    low.y = min(low.y, j)
                    high.x = max(high.x, i)
                    high.y = max(high.y, j)

        # Calculate dimensions
        height = high.x - low.x + 1
        width = high.y - low.y + 1

        # Create array for the chart
        chart_arr = np.ndarray(shape=(height, width), dtype=Chart.DATA_TYPE)
        chart_arr.fill(Chart.EMPTY)

        # Copy the chart from the atlas to the array
        pixels = 0
        for i in range(height):
            for j in range(width):
                tmp_id = atlas[low.x + i, low.y + j]
                if tmp_id == chart_id:
                    chart_arr[i, j] = Chart.OCCUPIED
                    pixels += 1

        # Create a new Chart object and return it
        index = low
        chart = Chart.create(chart_arr,
                             height,
                             width,
                             chart_id=chart_id,
                             index=index,
                             pixels=pixels)

        return chart