From cba1535a4f34d4757ab53eed74375eb31354c134 Mon Sep 17 00:00:00 2001 From: Neil Thistlethwaite Date: Thu, 27 Jul 2017 16:28:51 -0400 Subject: [PATCH 1/2] Relax NumericType restriction on AbstractSource and RandomAccessibleSources for non-numeric types, only NearestNeighborInterpolatorFactory is used --- src/main/java/bdv/util/AbstractSource.java | 21 ++++++++++++------- .../java/bdv/util/DefaultInterpolators.java | 11 ++-------- .../java/bdv/util/InterpolationFunction.java | 19 +++++++++++++++++ .../util/RandomAccessibleIntervalSource.java | 8 +++---- .../RandomAccessibleIntervalSource4D.java | 8 +++---- .../java/bdv/util/RandomAccessibleSource.java | 4 ++-- .../bdv/util/RandomAccessibleSource4D.java | 6 ++---- 7 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 src/main/java/bdv/util/InterpolationFunction.java diff --git a/src/main/java/bdv/util/AbstractSource.java b/src/main/java/bdv/util/AbstractSource.java index 119763c6..281bbb91 100644 --- a/src/main/java/bdv/util/AbstractSource.java +++ b/src/main/java/bdv/util/AbstractSource.java @@ -34,22 +34,27 @@ import bdv.viewer.Source; import mpicbg.spim.data.sequence.VoxelDimensions; import net.imglib2.RealRandomAccessible; +import net.imglib2.interpolation.randomaccess.NearestNeighborInterpolatorFactory; +import net.imglib2.type.Type; import net.imglib2.type.numeric.NumericType; import net.imglib2.view.Views; -public abstract class AbstractSource< T extends NumericType< T > > implements Source< T > +public abstract class AbstractSource< T extends Type< T > > implements Source< T > { - protected final T type; + protected final T extension; protected final String name; - protected final DefaultInterpolators< T > interpolators; + protected final InterpolationFunction interpolators; - public AbstractSource( final T type, final String name ) + public AbstractSource( final T extension, final String name ) { - this.type = type; + this.extension = extension; this.name = name; - interpolators = new DefaultInterpolators<>(); + if(extension instanceof NumericType) + interpolators = new DefaultInterpolators(); + else + interpolators = (interpolation -> new NearestNeighborInterpolatorFactory<>()); } public AbstractSource( final Supplier< T > typeSupplier, final String name ) @@ -66,13 +71,13 @@ public boolean isPresent( final int t ) @Override public T getType() { - return type; + return extension; } @Override public RealRandomAccessible< T > getInterpolatedSource( final int t, final int level, final Interpolation method ) { - return Views.interpolate( Views.extendZero( getSource( t, level ) ), interpolators.get( method ) ); + return Views.interpolate( Views.extendValue( getSource( t, level ), extension ), interpolators.apply( method ) ); } @Override diff --git a/src/main/java/bdv/util/DefaultInterpolators.java b/src/main/java/bdv/util/DefaultInterpolators.java index 190f0066..62ae282d 100644 --- a/src/main/java/bdv/util/DefaultInterpolators.java +++ b/src/main/java/bdv/util/DefaultInterpolators.java @@ -1,7 +1,5 @@ package bdv.util; -import java.util.function.Function; - import bdv.viewer.Interpolation; import net.imglib2.RandomAccessible; import net.imglib2.interpolation.InterpolatorFactory; @@ -9,7 +7,7 @@ import net.imglib2.interpolation.randomaccess.NearestNeighborInterpolatorFactory; import net.imglib2.type.numeric.NumericType; -public class DefaultInterpolators< T extends NumericType< T > > implements Function< Interpolation, InterpolatorFactory< T, RandomAccessible< T > > > +public class DefaultInterpolators< T extends NumericType< T > > implements InterpolationFunction< T > { private final InterpolatorFactory< T, RandomAccessible< T > >[] factories; @@ -21,6 +19,7 @@ public DefaultInterpolators() factories[ Interpolation.NLINEAR.ordinal() ] = new ClampingNLinearInterpolatorFactory<>(); } + @Override public InterpolatorFactory< T, RandomAccessible< T > > get( final Interpolation method ) { return factories[ method.ordinal() ]; @@ -30,10 +29,4 @@ public int size() { return factories.length; } - - @Override - public InterpolatorFactory< T, RandomAccessible< T > > apply( final Interpolation t ) - { - return get( t ); - } } diff --git a/src/main/java/bdv/util/InterpolationFunction.java b/src/main/java/bdv/util/InterpolationFunction.java new file mode 100644 index 00000000..efecc054 --- /dev/null +++ b/src/main/java/bdv/util/InterpolationFunction.java @@ -0,0 +1,19 @@ +package bdv.util; + +import java.util.function.Function; + +import bdv.viewer.Interpolation; +import net.imglib2.RandomAccessible; +import net.imglib2.interpolation.InterpolatorFactory; +import net.imglib2.type.Type; + +public interface InterpolationFunction< T extends Type< T > > extends Function< Interpolation, InterpolatorFactory< T, RandomAccessible< T > > > +{ + public abstract InterpolatorFactory< T, RandomAccessible< T > > get( final Interpolation method ); + + @Override + public default InterpolatorFactory< T, RandomAccessible< T > > apply( final Interpolation t ) + { + return get( t ); + } +} diff --git a/src/main/java/bdv/util/RandomAccessibleIntervalSource.java b/src/main/java/bdv/util/RandomAccessibleIntervalSource.java index 7be25bd0..b0151e40 100644 --- a/src/main/java/bdv/util/RandomAccessibleIntervalSource.java +++ b/src/main/java/bdv/util/RandomAccessibleIntervalSource.java @@ -32,10 +32,10 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.RealRandomAccessible; import net.imglib2.realtransform.AffineTransform3D; -import net.imglib2.type.numeric.NumericType; +import net.imglib2.type.Type; import net.imglib2.view.Views; -public class RandomAccessibleIntervalSource< T extends NumericType< T > > extends AbstractSource< T > +public class RandomAccessibleIntervalSource< T extends Type< T > > extends AbstractSource< T > { private final RandomAccessibleInterval< T > source; @@ -61,10 +61,8 @@ public RandomAccessibleIntervalSource( this.source = img; this.sourceTransform = sourceTransform; interpolatedSources = new RealRandomAccessible[ Interpolation.values().length ]; - final T zero = getType().createVariable(); - zero.setZero(); for ( final Interpolation method : Interpolation.values() ) - interpolatedSources[ method.ordinal() ] = Views.interpolate( Views.extendValue( source, zero ), interpolators.get( method ) ); + interpolatedSources[ method.ordinal() ] = Views.interpolate( Views.extendValue( source, extension.copy() ), interpolators.get( method ) ); } @Override diff --git a/src/main/java/bdv/util/RandomAccessibleIntervalSource4D.java b/src/main/java/bdv/util/RandomAccessibleIntervalSource4D.java index c81116bc..6347d6e7 100644 --- a/src/main/java/bdv/util/RandomAccessibleIntervalSource4D.java +++ b/src/main/java/bdv/util/RandomAccessibleIntervalSource4D.java @@ -34,10 +34,10 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.RealRandomAccessible; import net.imglib2.realtransform.AffineTransform3D; -import net.imglib2.type.numeric.NumericType; +import net.imglib2.type.Type; import net.imglib2.view.Views; -public class RandomAccessibleIntervalSource4D< T extends NumericType< T > > extends AbstractSource< T > +public class RandomAccessibleIntervalSource4D< T extends Type< T > > extends AbstractSource< T > { private final RandomAccessibleInterval< T > source; @@ -75,11 +75,9 @@ private void loadTimepoint( final int timepointIndex ) currentTimePointIndex = timepointIndex; if ( isPresent( timepointIndex ) ) { - final T zero = getType().createVariable(); - zero.setZero(); currentSource = Views.hyperSlice( source, 3, timepointIndex ); for ( final Interpolation method : Interpolation.values() ) - currentInterpolatedSources[ method.ordinal() ] = Views.interpolate( Views.extendValue( currentSource, zero ), interpolators.get( method ) ); + currentInterpolatedSources[ method.ordinal() ] = Views.interpolate( Views.extendValue( currentSource, extension.copy() ), interpolators.get( method ) ); } else { diff --git a/src/main/java/bdv/util/RandomAccessibleSource.java b/src/main/java/bdv/util/RandomAccessibleSource.java index d58af18b..f9578676 100644 --- a/src/main/java/bdv/util/RandomAccessibleSource.java +++ b/src/main/java/bdv/util/RandomAccessibleSource.java @@ -34,10 +34,10 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.RealRandomAccessible; import net.imglib2.realtransform.AffineTransform3D; -import net.imglib2.type.numeric.NumericType; +import net.imglib2.type.Type; import net.imglib2.view.Views; -public class RandomAccessibleSource< T extends NumericType< T > > extends AbstractSource< T > +public class RandomAccessibleSource< T extends Type< T > > extends AbstractSource< T > { private final RandomAccessible< T > source; diff --git a/src/main/java/bdv/util/RandomAccessibleSource4D.java b/src/main/java/bdv/util/RandomAccessibleSource4D.java index 7d5fa22f..2e0e692b 100644 --- a/src/main/java/bdv/util/RandomAccessibleSource4D.java +++ b/src/main/java/bdv/util/RandomAccessibleSource4D.java @@ -36,11 +36,11 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.RealRandomAccessible; import net.imglib2.realtransform.AffineTransform3D; -import net.imglib2.type.numeric.NumericType; +import net.imglib2.type.Type; import net.imglib2.util.Intervals; import net.imglib2.view.Views; -public class RandomAccessibleSource4D< T extends NumericType< T > > extends AbstractSource< T > +public class RandomAccessibleSource4D< T extends Type< T > > extends AbstractSource< T > { private final RandomAccessible< T > source; @@ -88,8 +88,6 @@ private void loadTimepoint( final int timepointIndex ) currentTimePointIndex = timepointIndex; if ( isPresent( timepointIndex ) ) { - final T zero = getType().createVariable(); - zero.setZero(); final RandomAccessible< T > slice = Views.hyperSlice( source, 3, timepointIndex ); currentSource = Views.interval( slice, timeSliceInterval ); for ( final Interpolation method : Interpolation.values() ) From 4c3dae69442bcb67099b1a006c3b51bb7d72d2cc Mon Sep 17 00:00:00 2001 From: Neil Thistlethwaite Date: Fri, 28 Jul 2017 16:45:45 -0400 Subject: [PATCH 2/2] Relax NumericType to Type on RAIMipMapSource --- .../java/bdv/util/RandomAccessibleIntervalMipmapSource.java | 3 ++- .../util/VolatileRandomAccessibleIntervalMipmapSource.java | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/bdv/util/RandomAccessibleIntervalMipmapSource.java b/src/main/java/bdv/util/RandomAccessibleIntervalMipmapSource.java index ad603e76..fe34b740 100644 --- a/src/main/java/bdv/util/RandomAccessibleIntervalMipmapSource.java +++ b/src/main/java/bdv/util/RandomAccessibleIntervalMipmapSource.java @@ -35,9 +35,10 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; import net.imglib2.realtransform.AffineTransform3D; +import net.imglib2.type.Type; import net.imglib2.type.numeric.NumericType; -public class RandomAccessibleIntervalMipmapSource< T extends NumericType< T > > extends AbstractSource< T > +public class RandomAccessibleIntervalMipmapSource< T extends Type< T > > extends AbstractSource< T > { protected final RandomAccessibleInterval< T >[] mipmapSources; diff --git a/src/main/java/bdv/util/VolatileRandomAccessibleIntervalMipmapSource.java b/src/main/java/bdv/util/VolatileRandomAccessibleIntervalMipmapSource.java index b501269d..3789d038 100644 --- a/src/main/java/bdv/util/VolatileRandomAccessibleIntervalMipmapSource.java +++ b/src/main/java/bdv/util/VolatileRandomAccessibleIntervalMipmapSource.java @@ -36,9 +36,9 @@ import net.imglib2.RandomAccessibleInterval; import net.imglib2.Volatile; import net.imglib2.realtransform.AffineTransform3D; -import net.imglib2.type.numeric.NumericType; +import net.imglib2.type.Type; -public class VolatileRandomAccessibleIntervalMipmapSource< T extends NumericType< T >, V extends Volatile< T > & NumericType< V > > extends AbstractSource< V > +public class VolatileRandomAccessibleIntervalMipmapSource< T extends Type< T >, V extends Volatile< T > & Type< V > > extends AbstractSource< V > { private final RandomAccessibleIntervalMipmapSource< T > source;