@Override public CellImg< T, A > copy() { @SuppressWarnings( "unchecked" ) final CellImg< T, A > copy = ( CellImg< T, A > ) factory().create( dimension ); copyDataTo( copy ); return copy; } }
@Test public void testSumWithCursor() { long sum = 0; final Cursor< IntType > cursor = intImg.cursor(); while ( cursor.hasNext() ) { sum += cursor.next().get(); } assertTrue( sum == intDataSum ); }
private < A extends ArrayDataAccess< A > > CellImg< T, A > create( final long[] dimensions, final T type, final NativeTypeFactory< T, A > typeFactory ) { verifyDimensions( dimensions ); final int n = dimensions.length; final Fraction entitiesPerPixel = type.getEntitiesPerPixel(); final int[] cellDimensions = getCellDimensions( defaultCellDimensions, n, entitiesPerPixel ); final CellGrid grid = new CellGrid( dimensions, cellDimensions ); final long[] gridDimensions = new long[ grid.numDimensions() ]; grid.gridDimensions( gridDimensions ); final Cell< A > cellType = new Cell<>( new int[] { 1 }, new long[] { 1 }, null ); final ListImg< Cell< A > > cells = new ListImg<>( gridDimensions, cellType ); final long[] cellGridPosition = new long[ n ]; final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; final ListLocalizingCursor< Cell< A > > cellCursor = cells.localizingCursor(); while ( cellCursor.hasNext() ) { cellCursor.fwd(); cellCursor.localize( cellGridPosition ); grid.getCellDimensions( cellGridPosition, cellMin, cellDims ); final A data = ArrayDataAccessFactory.get( typeFactory ).createArray( ( int ) entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ) ); cellCursor.set( new Cell<>( cellDims, cellMin, data ) ); } final CellImg< T, A > img = new CellImg<>( this, grid, cells, entitiesPerPixel ); img.setLinkedType( typeFactory.createLinkedType( img ) ); return img; }
/** * Fill intImg (a CellContainer with 40x40x40 cells) with data using flat * array iteration order. */ public void fillImage() { final int[] pos = new int[ dimensions.length ]; final RandomAccess< IntType > a = intImg.randomAccess(); final int[] idim = new int[ dimensions.length ]; for ( int d = 0; d < dimensions.length; ++d ) idim[ d ] = ( int ) dimensions[ d ]; for ( int i = 0; i < numValues; ++i ) { IntervalIndexer.indexToPosition( i, idim, pos ); a.setPosition( pos ); a.get().set( intData[ i ] ); } }
@Test public void testSumWithLocalizingCursor() { long sum = 0; final Cursor< IntType > cursor = intImg.localizingCursor(); while ( cursor.hasNext() ) { sum += cursor.next().get(); } assertTrue( sum == intDataSum ); }
private < T extends NativeType< T > > void testDefaultCellSize( final T type ) { final int defaultCellSize = 43; final int[] expectedCellDims = { 43, 43, 43, 43 }; final CellImgFactory< T > factory = new CellImgFactory<>( type, defaultCellSize ); final long[] dimension = { 100, 80, 4, 3 }; final CellImg< T, ? > img = factory.create( dimension ); final int[] cellDims = new int[ dimension.length ]; img.getCellGrid().cellDimensions( cellDims ); assertArrayEquals( expectedCellDims, cellDims ); }
@SuppressWarnings( "unchecked" ) final CellImg< UnsignedShortType, ShortArray > cellImg = ( CellImg< UnsignedShortType, ShortArray > ) factory.create( dimsLong ); final Cursor< Cell< ShortArray > > cursor = cellImg.getCells().cursor(); while ( cursor.hasNext() )
@Test public void testJmpWithCursor() { final int steps = 43; final Cursor< IntType > cursor1 = intImg.cursor(); for ( int i = 0; i < steps; ++i ) cursor1.fwd(); final int v1 = cursor1.next().get(); final long[] p1 = new long[ dimensions.length ]; cursor1.localize( p1 ); final Cursor< IntType > cursor2 = intImg.cursor(); cursor2.jumpFwd( steps ); final int v2 = cursor2.next().get(); final long[] p2 = new long[ dimensions.length ]; cursor2.localize( p2 ); assertTrue( v1 == v2 ); assertArrayEquals( p1, p2 ); }
private < A extends ArrayDataAccess< A > > CellImg< T, A > create( final long[] dimensions, final T type, final NativeTypeFactory< T, A > typeFactory ) { verifyDimensions( dimensions ); final int n = dimensions.length; final Fraction entitiesPerPixel = type.getEntitiesPerPixel(); final int[] cellDimensions = getCellDimensions( defaultCellDimensions, n, entitiesPerPixel ); final CellGrid grid = new CellGrid( dimensions, cellDimensions ); final long[] gridDimensions = new long[ grid.numDimensions() ]; grid.gridDimensions( gridDimensions ); final Cell< A > cellType = new Cell<>( new int[] { 1 }, new long[] { 1 }, null ); final ListImg< Cell< A > > cells = new ListImg<>( gridDimensions, cellType ); final long[] cellGridPosition = new long[ n ]; final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; final ListLocalizingCursor< Cell< A > > cellCursor = cells.localizingCursor(); while ( cellCursor.hasNext() ) { cellCursor.fwd(); cellCursor.localize( cellGridPosition ); grid.getCellDimensions( cellGridPosition, cellMin, cellDims ); final A data = ArrayDataAccessFactory.get( typeFactory ).createArray( ( int ) entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ) ); cellCursor.set( new Cell<>( cellDims, cellMin, data ) ); } final CellImg< T, A > img = new CellImg<>( this, grid, cells, entitiesPerPixel ); img.setLinkedType( typeFactory.createLinkedType( img ) ); return img; }
@Test public void testSumWithRandomAccess() { long sum = 0; final RandomAccess< IntType > access = intImg.randomAccess(); final long[] position = new long[ dimensions.length ]; for ( int d = 0; d < dimensions.length; ++d ) position[ d ] = 0; for ( int i = 0; i < numValues; ++i ) { access.setPosition( position ); sum += access.get().get(); for ( int d = 0; d < dimensions.length; ++d ) if ( ++position[ d ] >= dimensions[ d ] ) position[ d ] = 0; else break; } assertTrue( sum == intDataSum ); } }
@Test public void testJmpWithLocalizingCursor() { final int steps = 43; final Cursor< IntType > cursor1 = intImg.localizingCursor(); for ( int i = 0; i < steps; ++i ) cursor1.fwd(); final int v1 = cursor1.next().get(); final long[] p1 = new long[ dimensions.length ]; cursor1.localize( p1 ); final Cursor< IntType > cursor2 = intImg.localizingCursor(); cursor2.jumpFwd( steps ); final int v2 = cursor2.next().get(); final long[] p2 = new long[ dimensions.length ]; cursor2.localize( p2 ); assertTrue( v1 == v2 ); assertArrayEquals( p1, p2 ); }
private < T extends NativeType< T > > void testDefaultCellDimensions( final T type ) { final int[] defaultCellDims = { 6, 8, 5, 3 }; final int[] expectedCellDims = defaultCellDims.clone(); final CellImgFactory< T > factory = new CellImgFactory<>( type, defaultCellDims ); final long[] dimension = { 100, 80, 4, 3 }; final CellImg< T, ? > img = factory.create( dimension ); final int[] cellDims = new int[ dimension.length ]; img.getCellGrid().cellDimensions( cellDims ); assertArrayEquals( expectedCellDims, cellDims ); }
public CellImg<ByteType, ?> generateByteTestCellImg(final boolean fill, final long... dims) { final CellImg<ByteType, ?> img = new CellImgFactory<ByteType>().create(dims, new ByteType()); if (fill) { final Cursor<ByteType> c = img.cursor(); while (c.hasNext()) c.next().set((byte) pseudoRandom()); } return img; }
@Override public CellImg< T, A > copy() { @SuppressWarnings( "unchecked" ) final CellImg< T, A > copy = ( CellImg< T, A > ) factory().create( dimension ); copyDataTo( copy ); return copy; } }
@Before public void createSourceData() { dimensions = new long[] { 16, 37, 5, 13 }; numValues = 1; for ( int d = 0; d < dimensions.length; ++d ) numValues *= dimensions[ d ]; intData = new int[ numValues ]; intDataSum = 0; final Random random = new Random( 0 ); for ( int i = 0; i < numValues; ++i ) { intData[ i ] = random.nextInt(); intDataSum += intData[ i ]; } intImg = new CellImgFactory<>( new IntType(), 4 ).create( dimensions ); final long[] pos = new long[ dimensions.length ]; final RandomAccess< IntType > a = intImg.randomAccess(); for ( int i = 0; i < numValues; ++i ) { IntervalIndexer.indexToPosition( i, dimensions, pos ); a.setPosition( pos ); a.get().set( intData[ i ] ); } }
@Test public void testResetWithLocalizingCursor() { final Cursor< IntType > cursor = intImg.localizingCursor(); final int v1 = cursor.next().get(); final long[] p1 = new long[ dimensions.length ]; cursor.localize( p1 ); cursor.reset(); final int v2 = cursor.next().get(); final long[] p2 = new long[ dimensions.length ]; cursor.localize( p2 ); assertTrue( v1 == v2 ); assertArrayEquals( p1, p2 ); }
public CellImg<ByteType, ?> generateByteTestCellImg(final boolean fill, final int[] cellDims, final long... dims) { final CellImg<ByteType, ?> img = new CellImgFactory<ByteType>(cellDims) .create(dims, new ByteType()); if (fill) { final Cursor<ByteType> c = img.cursor(); while (c.hasNext()) c.next().set((byte) pseudoRandom()); } return img; }
@Before public void createSourceData() { dimensions = new long[] { 48, 17, 102 }; numValues = 1; for ( int d = 0; d < dimensions.length; ++d ) numValues *= dimensions[ d ]; intData = new int[ numValues ]; intDataSum = 0; final Random random = new Random( 0 ); for ( int i = 0; i < numValues; ++i ) { intData[ i ] = random.nextInt(); intDataSum += intData[ i ]; } intImg = new CellImgFactory<>( new IntType(), 10 ).create( dimensions ); final long[] pos = new long[ dimensions.length ]; final RandomAccess< IntType > a = intImg.randomAccess(); for ( int i = 0; i < numValues; ++i ) { IntervalIndexer.indexToPosition( i, dimensions, pos ); a.setPosition( pos ); a.get().set( intData[ i ] ); } }
@Test public void testResetWithCursor() { final Cursor< IntType > cursor = intImg.cursor(); final int v1 = cursor.next().get(); final long[] p1 = new long[ dimensions.length ]; cursor.localize( p1 ); cursor.reset(); final int v2 = cursor.next().get(); final long[] p2 = new long[ dimensions.length ]; cursor.localize( p2 ); assertTrue( v1 == v2 ); assertArrayEquals( p1, p2 ); }
for ( final Cursor< IntType > c = cellImage.cursor(); c.hasNext(); )