Ejemplo n.º 1
0
    def deselectBlock(self,
                      voxel,
                      boxSize,
                      axes=(0, 1, 2),
                      bias=None,
                      combine=False):
        """De-selects the block (sets all voxels to 0) specified by the given
        voxel and box size. See the :func:`.routines.voxelBlock` function
        for details on the arguments.

        :arg combine:   Combine this change with the previous stored change
                        (see :meth:`__storeChange`).
        """

        block, offset = glroutines.voxelBlock(voxel,
                                              self.__selection.shape,
                                              boxSize,
                                              bias=bias,
                                              axes=axes)

        self.removeFromSelection(block, offset, combine)
Ejemplo n.º 2
0
def selectLine(shape, dims, from_, to, boxSize, axes=(0, 1, 2), bias=None):
    """Selects a continuous "line" in an array of the given ``shape``,
    between the points ``from_`` and ``to``.

    :arg shape:   Shape of the image in which the selection is taking place.

    :arg dims:    Size of one voxel along each axis (the pixdims).

    :arg from_:   Start point of the line

    :arg to:      End point of the line

    See the :func:`.routines.voxelBlock` function for details on the other
    arguments.

    :returns: A tuple containing:

               - A 3D boolean ``numpy`` array containing the selected line.
               - An offset of this array according to the ``shape`` of the
                 full image. If the
    """

    from_ = np.array(from_)
    to = np.array(to)

    # The boxSize is specified in scaled
    # voxels, so we need to calculate the
    # distance between the two voxels,
    # and the required number of points,
    # in scaled voxels as well.
    length = np.sqrt(np.sum((from_ * dims - to * dims)**2))

    # box size can either be
    # a scalar or a sequence.
    # We add 2 to the number
    # of points for the from_
    # and to locations.
    if isinstance(boxSize, collections.Sequence):
        npoints = int(np.ceil(length / min(boxSize))) + 2
    else:
        npoints = int(np.ceil(length / boxSize)) + 2

    # Create a bunch of interpolated
    # points between from_ and to
    xs = np.linspace(from_[0], to[0], npoints).round()
    ys = np.linspace(from_[1], to[1], npoints).round()
    zs = np.linspace(from_[2], to[2], npoints).round()

    # We have the minimums and maximums
    # of the coordinates to be selected
    xmin = xs.min()
    ymin = ys.min()
    zmin = zs.min()
    xmax = xs.max()
    ymax = ys.max()
    zmax = zs.max()

    # But before we can figure out how big
    # the cuboid region which encompasses
    # the line is, we need to take into
    # account the pen size. So we create
    # blocks at the start and end points.
    minBox = glroutines.voxelBox([xmin, ymin, zmin], shape, dims, boxSize,
                                 axes, bias)
    maxBox = glroutines.voxelBox([xmax, ymax, zmax], shape, dims, boxSize,
                                 axes, bias)

    # And then adjust our overall
    # block offset and size by
    # these start/end blocks
    offset = [
        int(min((xmin, minBox[:, 0].min()))),
        int(min((ymin, minBox[:, 1].min()))),
        int(min((zmin, minBox[:, 2].min())))
    ]
    size = [
        int(max((xmax, maxBox[:, 0].max())) - offset[0]),
        int(max((ymax, maxBox[:, 1].max())) - offset[1]),
        int(max((zmax, maxBox[:, 2].max())) - offset[2])
    ]

    # Allocate a selection block
    # which will contain the line
    block = np.zeros(size, dtype=np.bool)

    # Generate a voxel block
    # at each point
    for i in range(npoints):

        point = (xs[i], ys[i], zs[i])
        pointBlock, pointOff = glroutines.voxelBlock(voxel=point,
                                                     shape=shape,
                                                     dims=dims,
                                                     boxSize=boxSize,
                                                     axes=axes,
                                                     bias=bias)

        pointOff = [
            pointOff[0] - offset[0], pointOff[1] - offset[1],
            pointOff[2] - offset[2]
        ]

        # And fill in our line block
        sz = pointBlock.shape
        block[pointOff[0]:pointOff[0] + sz[0], pointOff[1]:pointOff[1] + sz[1],
              pointOff[2]:pointOff[2] + sz[2]] = pointBlock

    return block, offset