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

Draft: streamify View API #348

Closed
wants to merge 8 commits into from
Closed
13 changes: 10 additions & 3 deletions src/main/java/net/imglib2/RandomAccessibleInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -34,6 +34,8 @@

package net.imglib2;

import net.imglib2.streamifiedview.RaiView;

/**
* <p>
* <em>f</em>:{x&isin;Z<sup><em>n</em></sup>|[min,max]&rarr;T}
Expand All @@ -60,4 +62,9 @@
* @author Stephan Saalfeld
*/
public interface RandomAccessibleInterval< T > extends RandomAccessible< T >, Interval
{}
{
default RaiView< T > view()
{
return RaiView.wrap( this );
}
}
10 changes: 5 additions & 5 deletions src/main/java/net/imglib2/interpolation/Interpolant.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand Down Expand Up @@ -53,7 +53,7 @@ final public class Interpolant< T, F > implements RealRandomAccessible< T >, Vie

protected final int n;

final InterpolatorFactory< T, F > factory;
final InterpolatorFactory< T, ? super F > factory;

/**
*
Expand All @@ -79,7 +79,7 @@ public Interpolant( final EuclideanSpace source, final InterpolatorFactory< T, F
* @param factory
* @param n
*/
public Interpolant( final F source, final InterpolatorFactory< T, F > factory, final int n )
public Interpolant( final F source, final InterpolatorFactory< T, ? super F > factory, final int n )
{
this.source = source;
this.factory = factory;
Expand Down Expand Up @@ -112,7 +112,7 @@ public F getSource()
/**
* @return {@link InterpolatorFactory} used for interpolation
*/
public InterpolatorFactory< T, F > getInterpolatorFactory()
public InterpolatorFactory< T, ? super F > getInterpolatorFactory()
{
return factory;
}
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/net/imglib2/stream/DebugHelpers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*-
* #%L
* Mastodon Collections
* %%
* Copyright (C) 2015 - 2022 Tobias Pietzsch, Jean-Yves Tinevez
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imglib2.stream;

import java.io.PrintStream;

public class DebugHelpers
{
public static void printStackTrace( String ... unlessContains )
{
printStackTrace( System.out, 3, -1, unlessContains );
}

public static void printStackTrace( int maxDepth, String ... unlessContains )
{
printStackTrace( System.out, 3, maxDepth, unlessContains );
}

public static void printStackTrace( PrintStream out, int maxDepth, String ... unlessContains )
{
printStackTrace( out, 3, maxDepth, unlessContains );
}

public static void printStackTrace( PrintStream out, int startDepth, int maxDepth, String ... unlessContains )
{
final StackTraceElement[] trace = Thread.currentThread().getStackTrace();

for ( StackTraceElement element : trace )
{
final String traceLine = element.toString();
for ( String template : unlessContains )
if ( traceLine.contains( template ) )
return;
}

final int len = ( maxDepth < 0 )
? trace.length
: Math.min( startDepth + maxDepth, trace.length );
for ( int i = startDepth; i < len; ++i )
{
final String prefix = ( i == startDepth ) ? "" : " at ";
out.println( prefix + trace[ i ].toString() );
}

out.println();
}
}
54 changes: 54 additions & 0 deletions src/main/java/net/imglib2/streamifiedview/RaView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.imglib2.streamifiedview;

import net.imglib2.Interval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessible;
import net.imglib2.interpolation.InterpolatorFactory;
import net.imglib2.view.Views;

/**
* First attempt at an interface which basically copies java's stream syntax to imglib2-views.
*
* @author Michael Innerberger
* @see Views
*/
public interface RaView< T > extends RandomAccessible< T >
{
RandomAccessible< T > delegate();

default RaiView< T > interval( Interval 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 )
{
return new RaWrapper<>( delegate );
}

// TODO: Delegate all methods of RandomAccessible, also those that
// have a default implementations ...

@Override
default int numDimensions()
{
return delegate().numDimensions();
}

@Override
default RandomAccess< T > randomAccess()
{
return delegate().randomAccess();
}

@Override
default RandomAccess< T > randomAccess( final Interval interval )
{
return delegate().randomAccess(interval);
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/imglib2/streamifiedview/RaWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.imglib2.streamifiedview;

import net.imglib2.RandomAccessible;

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

RaWrapper( final RandomAccessible< T > delegate )
{
this.delegate = delegate;
}

@Override
public RandomAccessible< T > delegate()
{
return delegate;
}
}
133 changes: 133 additions & 0 deletions src/main/java/net/imglib2/streamifiedview/RaiView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package net.imglib2.streamifiedview;

import net.imglib2.Cursor;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.NativeType;
import net.imglib2.view.Views;

/**
* First attempt at an interface which basically copies java's stream syntax to imglib2-views.
*
* @author Michael Innerberger
* @see net.imglib2.view.Views
*/
public interface RaiView< T > extends RaView< T >, RandomAccessibleInterval< T >, IterableInterval< T >
{
default RaiView< T > expandValue( final T value, long... border )
{
return wrap( Views.expandValue( delegate(), value, border ) );
}

default RaiView< T > permute( final int from, final int to )
{
return wrap( Views.permute( delegate(), from, to ) );
}

default RaiView< T > translate( long... translation )
{
return wrap( Views.translate( delegate(), translation ) );
}

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

@Override
RandomAccessibleInterval< T > delegate();

static < T > RaiView< T > wrap( final RandomAccessibleInterval< T > delegate )
{
return new RaiWrapper<>( delegate );
}

// TODO: Delegate all methods of RandomAccessibleInterval, also those that
// have a default implementations ...

@Override
default int numDimensions()
{
return delegate().numDimensions();
}

@Override
default long min( final int d )
{
return delegate().min( d );
}

@Override
default long max( final int d )
{
return delegate().max( d );
}

@Override
default RandomAccess< T > randomAccess()
{
return delegate().randomAccess();
}

@Override
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();
}

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

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

@Override
default Object iterationOrder()
{
return Views.iterable( delegate() ).iterationOrder();
}

/**
* @return an {@link ArrayImg} containing a persistent copy of the data in this {@link RaiView}.
* @throws ClassCastException if the type of this {@link RaiView} is not a {@link NativeType}.
*/
// the following method ensures that T is a NativeType
@SuppressWarnings({"unchecked", "rawtypes"})
default <S extends NativeType<S>> ArrayImg<S, ?> toArrayImg() {
final long[] dims = dimensionsAsLongArray();
final T type = firstElement();

if (type instanceof NativeType) {
final NativeType<S> nativeType = (NativeType<S>) type;
ArrayImgFactory<S> factory = new ArrayImgFactory(nativeType);
ArrayImg<S, ?> outputImg = factory.create(dims);
final RandomAccessibleInterval<S> inputRai = (RandomAccessibleInterval<S>) this;
LoopBuilder.setImages(inputRai, outputImg).forEachPixel((in, out) -> out.set(in));
return outputImg;
} else {
throw new ClassCastException("Cannot create ArrayImg from type " + type.getClass().getCanonicalName() +
". Need an implementation of NativeType.");
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/net/imglib2/streamifiedview/RaiWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.imglib2.streamifiedview;

import net.imglib2.Cursor;
import net.imglib2.RandomAccessibleInterval;

class RaiWrapper< T > implements RaiView< T >
{
private final RandomAccessibleInterval< T > delegate;

RaiWrapper( final RandomAccessibleInterval< T > delegate )
{
this.delegate = delegate;
}

@Override
public RandomAccessibleInterval< T > delegate()
{
return delegate;
}
}
Loading