Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiled view #309

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 179 additions & 20 deletions src/main/java/net/imglib2/img/cell/CellGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@

import java.util.Arrays;

import java.util.Iterator;
import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.FlatIterationOrder;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.Point;
import net.imglib2.Positionable;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.util.IntervalIndexer;
import net.imglib2.util.Intervals;
import net.imglib2.util.Util;
import net.imglib2.view.RandomAccessibleIntervalCursor;

/**
* Defines {@link AbstractCellImg} geometry and translates between image, cell,
Expand All @@ -59,6 +70,13 @@ public class CellGrid

private final int hashcode;

/**
* @param dimensions
* the dimensions of the image (in pixels, not in cells).
* @param cellDimensions
* the dimensions of a standard cell (in pixels). Cells on the max border
* of the image may be cut off and have different dimensions.
*/
public CellGrid(
final long[] dimensions,
final int[] cellDimensions )
Expand All @@ -75,6 +93,8 @@ public CellGrid(
borderSize[ d ] = ( int ) ( dimensions[ d ] - ( numCells[ d ] - 1 ) * cellDimensions[ d ] );
}
hashcode = 31 * Arrays.hashCode( dimensions ) + Arrays.hashCode( cellDimensions );

cellIntervals = new CellIntervals();
}

public CellGrid( final CellGrid grid )
Expand All @@ -85,24 +105,35 @@ public CellGrid( final CellGrid grid )
numCells = grid.numCells.clone();
borderSize = grid.borderSize.clone();
hashcode = grid.hashcode;
cellIntervals = new CellIntervals();
}

public int numDimensions()
{
return n;
}

/**
* Get the number of cells in each dimension as a new long[].
*/
public long[] getGridDimensions()
{
return numCells.clone();
}

public void gridDimensions( final long[] s )
/**
* Write the number of cells in each dimension into the provided {@code
* dimensions} array.
*/
public void gridDimensions( final long[] dimensions )
{
for ( int i = 0; i < n; ++i )
s[ i ] = numCells[ i ];
dimensions[ i ] = numCells[ i ];
}

/**
* Get the number of cells in dimension {@code d}.
*/
public long gridDimension( final int d )
{
return numCells[ d ];
Expand All @@ -118,10 +149,9 @@ public long[] getImgDimensions()
}

/**
* Write the number of pixels in each dimension into long[]. Note, that this
* is the number of pixels in all cells combined, not the number of cells!
*
* @param dimensions
* Write the number of pixels in each dimension into the provided {@code
* dimensions} array. Note, that this is the number of pixels in all cells
* combined, not the number of cells!
*/
public void imgDimensions( final long[] dimensions )
{
Expand All @@ -130,22 +160,27 @@ public void imgDimensions( final long[] dimensions )
}

/**
* Get the number of pixels in a given dimension <em>d</em>. Note, that this
* is the number of pixels in all cells combined, not the number of cells!
*
* @param d
* Get the number of pixels in dimension {@code d}. Note, that this is the number
* of pixels in all cells combined, not the number of cells!
*/
public long imgDimension( final int d )
{
return dimensions[ d ];
}

/**
* Write the number of pixels in a standard cell in each dimension into
* long[]. Cells on the max border of the image may be cut off and have
* different dimensions.
*
* @param dimensions
* Get the number of pixels in a standard cell in each dimension as a new int[].
* Cells on the borders of the image may be cut off and have different dimensions.
*/
public int[] getCellDimensions()
{
return cellDimensions.clone();
}

/**
* Write the number of pixels in a standard cell in each dimension into the
* provided {@code dimensions} array. Cells on the max border of the image may be
* cut off and have different dimensions.
*/
public void cellDimensions( final int[] dimensions )
{
Expand All @@ -154,11 +189,8 @@ public void cellDimensions( final int[] dimensions )
}

/**
* Get the number of pixels in a standard cell in a given dimension
* <em>d</em>. Cells on the max border of the image may be cut off and have
* different dimensions.
*
* @param d
* Get the number of pixels in a standard cell in dimension {@code d}. Cells on the
* max border of the image may be cut off and have different dimensions.
*/
public int cellDimension( final int d )
{
Expand Down Expand Up @@ -219,6 +251,17 @@ public void getCellDimensions( final long[] cellGridPosition, final long[] cellM
}
}

public void getCellInterval( final long[] cellGridPosition, final long[] cellMin, final long[] cellMax )
{
for ( int d = 0; d < n; ++d )
{
final long gridPos = cellGridPosition[ d ];
final int cellDim = ( gridPos + 1 == numCells[ d ] ) ? borderSize[ d ] : cellDimensions[ d ];
cellMin[ d ] = gridPos * cellDimensions[ d ];
cellMax[ d ] = cellMin[ d ] + cellDim - 1;
}
}

/**
* From the position of a cell in the grid, compute the size of the cell in
* dimension {@code d}. The size will be the standard
Expand Down Expand Up @@ -319,4 +362,120 @@ public String toString()
+ "( dims = " + Util.printCoordinates( dimensions )
+ ", cellDims = " + Util.printCoordinates( cellDimensions ) + " )";
}

private class CellIntervalsRA extends Point implements RandomAccess< Interval >
{
private final long[] min = new long[ CellGrid.this.n ];

private final long[] max = new long[ CellGrid.this.n ];

private final Interval interval = FinalInterval.wrap( min, max );

@Override
public Interval get()
{
getCellInterval( position, min, max );
return interval;
}

CellIntervalsRA()
{
super( CellGrid.this.n );
}

CellIntervalsRA( CellIntervalsRA ra )
{
super( ra );
}

@Override
public RandomAccess< Interval > copyRandomAccess()
{
return copy();
}

@Override
public RandomAccess< Interval > copy()
{
return new CellIntervalsRA( this );
}
}

public class CellIntervals implements RandomAccessibleInterval< Interval >, IterableInterval< Interval >
{
private final long size = Intervals.numElements( numCells );

@Override
public int numDimensions()
{
return n;
}

@Override
public long min( final int d )
{
return 0;
}

@Override
public long max( final int d )
{
return numCells[ d ] - 1;
}

@Override
public RandomAccess< Interval > randomAccess()
{
return new CellIntervalsRA();
}

@Override
public RandomAccess< Interval > randomAccess( final Interval interval )
{
return randomAccess();
}

@Override
public Cursor< Interval > cursor()
{
return new RandomAccessibleIntervalCursor<>( this );
}

@Override
public Cursor< Interval > localizingCursor()
{
return cursor();
}

@Override
public long size()
{
return size;
}

@Override
public Interval firstElement()
{
return cursor().next();
}

@Override
public FlatIterationOrder iterationOrder()
{
return new FlatIterationOrder( this );
}

@Override
public Iterator< Interval > iterator()
{
return cursor();
}
}

private final CellIntervals cellIntervals;

public CellIntervals cellIntervals()
{
return cellIntervals;
}
}
Loading