Skip to content

Commit

Permalink
Add recursive generic to RaView.
Browse files Browse the repository at this point in the history
This is necessary to make apply(Function) work correctly for both, RaView
and RaiView. Effectively, the function given to RaView.apply() should
take RaView (or any parent, e.g., RandomAccessible). The function given to
RaiView.app;y() should take RaiView (or any parent, e.g., RandomAccessibleInterval).

I can find no other way to make this work.
Hopefully this is not too inconvenient because RaView is not supposed to be
used as type of variables, etc. It is just a construct to make the view-
chaining API possible
chains
  • Loading branch information
tpietzsch committed Jun 12, 2024
1 parent fc75f35 commit 75f3026
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
25 changes: 22 additions & 3 deletions src/main/java/net/imglib2/streamifiedview/RaView.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package net.imglib2.streamifiedview;

import java.util.function.Function;
import java.util.function.Supplier;

import net.imglib2.Interval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessible;
import net.imglib2.converter.Converter;
import net.imglib2.converter.Converters;
import net.imglib2.converter.TypeIdentity;
import net.imglib2.converter.read.ConvertedRandomAccessible;
import net.imglib2.interpolation.InterpolatorFactory;
import net.imglib2.view.Views;

Expand All @@ -13,21 +20,33 @@
* @author Michael Innerberger
* @see Views
*/
public interface RaView< T > extends RandomAccessible< T >
public interface RaView< T, V extends RaView< T, V > > extends RandomAccessible< T >
{
RandomAccessible< T > delegate();

default RaiView< T > interval( Interval interval )
{
return RaiView.wrap( Views.interval(delegate(), interval) );
return RaiView.wrap( Views.interval( delegate(), interval ) );
}

default RraView< T > interpolate( final InterpolatorFactory< T, ? super RandomAccessible< T > > factory )
{
return RraView.wrap( Views.interpolate( delegate(), factory ) );
}

static < T > RaView< T > wrap( final RandomAccessible< T > delegate )
default < U > RaView< U, ? > convert(
final Converter< ? super T, ? super U > converter,
final Supplier< U > targetSupplier )
{
return wrap( Converters.convert2( delegate(), converter, targetSupplier ) );
}

default < U > U apply( Function< ? super V, U > function )
{
return function.apply( ( V ) this );
}

static < T > RaView< T, ? > wrap( final RandomAccessible< T > delegate )
{
return new RaWrapper<>( delegate );
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/imglib2/streamifiedview/RaWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.imglib2.RandomAccessible;

class RaWrapper< T > implements RaView< T >
class RaWrapper< T > implements RaView< T, RaWrapper< T > >
{
private final RandomAccessible< T > delegate;

Expand Down
47 changes: 37 additions & 10 deletions src/main/java/net/imglib2/streamifiedview/RaiView.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package net.imglib2.streamifiedview;

import java.util.function.Function;

import net.imglib2.Cursor;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.view.Views;
Expand All @@ -14,7 +15,7 @@
* @author Michael Innerberger
* @see Views
*/
public interface RaiView< T > extends RaView< T >, RandomAccessibleInterval< T >
public interface RaiView< T > extends RaView< T, RaiView< T > >, RandomAccessibleInterval< T >
{
default RaiView< T > expandValue( final T value, long... border )
{
Expand All @@ -31,11 +32,40 @@ default RaiView< T > translate( long... translation )
return wrap( Views.translate( delegate(), translation ) );
}

default RaView< T > extendBorder()
default RaiView< T > zeroMin()
{
return wrap( Views.zeroMin( delegate() ) );
}

default RaiView< T > rotate( int fromAxis, int toAxis )
{
return wrap( Views.rotate( delegate(), fromAxis, toAxis ) );
}

default RaiView< T > hyperSlice( int d, long pos )
{
return wrap( Views.hyperSlice( delegate(), d, pos ) );
}

default RaView< T, ? > extendBorder()
{
return RaView.wrap( Views.extendBorder( delegate() ) );
}

// TODO: here we have a problem. We would like to override this
// from
// default < U > U apply( Function< ? super RaView< T >, U > function )
// to
// default < U > U apply( Function< ? super RaiView< T >, U > function ).
// That is: broaden the allowed range of the Function argument to also allow RaiView (a subclass of RaView)
// Can recursive generics solve that?

// TODO: rename? transform()? apply()? map()?
// default < U > U apply( Function< ? super RaView< T >, U > function )
// {
// return function.apply( this );
// }

@Override
RandomAccessibleInterval< T > delegate();

Expand Down Expand Up @@ -83,30 +113,27 @@ default RandomAccess< T > randomAccess( final Interval interval )
return delegate().randomAccess(interval);
}

// TODO: Not sure about the following.
// It's not so nice to have to use Views.iterable() always.

@Override
default Cursor< T > cursor()
{
return Views.iterable( delegate() ).cursor();
return delegate().cursor();
}

@Override
default Cursor< T > localizingCursor()
{
return Views.iterable( delegate() ).localizingCursor();
return delegate().localizingCursor();
}

@Override
default long size()
{
return Views.iterable( delegate() ).size();
return delegate().size();
}

@Override
default Object iterationOrder()
{
return Views.iterable( delegate() ).iterationOrder();
return delegate().iterationOrder();
}
}
10 changes: 9 additions & 1 deletion src/main/java/net/imglib2/streamifiedview/RraView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.imglib2.streamifiedview;

import java.util.function.Function;

import net.imglib2.RealInterval;
import net.imglib2.RealRandomAccess;
import net.imglib2.RealRandomAccessible;
Expand All @@ -16,11 +18,17 @@ public interface RraView< T > extends RealRandomAccessible< T >
{
RealRandomAccessible< T > delegate();

default RaView< T > raster()
default RaView< T, ? > raster()
{
return RaView.wrap( Views.raster( delegate() ) );
}

// TODO: rename? transform()? apply()? map()?
default < U > U apply( Function< ? super RraView< T >, U > function )
{
return function.apply( this );
}

static < T > RraView< T > wrap( final RealRandomAccessible< T > delegate )
{
return new RraWrapper<>( delegate );
Expand Down

0 comments on commit 75f3026

Please sign in to comment.