Skip to content
81 changes: 60 additions & 21 deletions src/main/java/net/imglib2/algorithm/math/Compute.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import java.util.LinkedList;

import net.imglib2.Cursor;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.math.abstractions.IBinaryFunction;
import net.imglib2.algorithm.math.abstractions.IFunction;
import net.imglib2.algorithm.math.abstractions.ITrinaryFunction;
import net.imglib2.algorithm.math.abstractions.IUnaryFunction;
import net.imglib2.algorithm.math.abstractions.OFunction;
import net.imglib2.algorithm.math.abstractions.Util;
import net.imglib2.algorithm.math.execution.FunctionCursor;
import net.imglib2.algorithm.math.execution.FunctionCursorIncompatibleOrder;
import net.imglib2.algorithm.math.execution.FunctionRandomAccess;
import net.imglib2.converter.Converter;
import net.imglib2.type.numeric.RealType;
import net.imglib2.view.Views;
Expand All @@ -21,7 +25,7 @@ public class Compute
{
private final IFunction operation;
private final boolean compatible_iteration_order;

/**
* Validate the {code operation}.
*
Expand Down Expand Up @@ -71,14 +75,7 @@ public < O extends RealType< O > > RandomAccessibleInterval< O > into(
)
{
if ( null == converter )
converter = new Converter< RealType< ? >, O >()
{
@Override
public final void convert( final RealType< ? > input, final O output)
{
output.setReal( input.getRealDouble() );
}
};
converter = Util.genericRealTypeConverter();

// Recursive copy: initializes interval iterators and sets temporary computation holder
final OFunction< O > f = this.operation.reInit(
Expand Down Expand Up @@ -115,6 +112,7 @@ private boolean validate( final IFunction f )

// child-parent map
final HashMap< IFunction, IFunction > cp = new HashMap<>();
cp.put( f, null );

// Collect images to later check their iteration order
final LinkedList< RandomAccessibleInterval< ? > > images = new LinkedList<>();
Expand All @@ -125,26 +123,26 @@ private boolean validate( final IFunction f )
// Collect Let instances to check that their declared variables are used
final HashSet< Let > lets = new HashSet<>();

IFunction parent = null;

// Iterate into the nested operations
// Iterate into the nested operations, depth-first
while ( ! ops.isEmpty() )
{
final IFunction op = ops.removeFirst();
cp.put( op, parent );
parent = op;

if ( op instanceof ImgSource )
{
images.addLast( ( ( ImgSource< ? > )op ).getRandomAccessibleInterval() );
images.addFirst( ( ( ImgSource< ? > )op ).getRandomAccessibleInterval() );
}
else if ( op instanceof IUnaryFunction )
{
ops.addLast( ( ( IUnaryFunction )op ).getFirst() );
final IFunction first = ( ( IUnaryFunction )op ).getFirst();
ops.addFirst( first );
cp.put( first, op );

if ( op instanceof IBinaryFunction )
{
ops.addLast( ( ( IBinaryFunction )op ).getSecond() );
final IFunction second = ( ( IBinaryFunction )op ).getSecond();
ops.add( 1, second );
cp.put( second, op );

if ( op instanceof Let )
{
Expand All @@ -153,7 +151,9 @@ else if ( op instanceof IUnaryFunction )

if ( op instanceof ITrinaryFunction )
{
ops.addLast( ( ( ITrinaryFunction )op ).getThird() );
final IFunction third = ( ( ITrinaryFunction )op ).getThird();
ops.add( 2, third );
cp.put( third, op );
}
}
}
Expand All @@ -168,16 +168,16 @@ else if ( op instanceof Var )
final HashSet< Let > used = new HashSet<>();
all: for ( final Var var : vars )
{
parent = var;
IFunction parent = var;
while ( null != ( parent = cp.get( parent ) ) )
{
if ( parent instanceof Let )
{
Let let = ( Let )parent;
final Let let = ( Let )parent;
if ( let.getVarName() != var.getName() )
continue;
// Else, found: Var is in use
used.add( let );
used.add( let ); // might already be in used
continue all;
}
}
Expand All @@ -200,4 +200,43 @@ else if ( op instanceof Var )

return Util.compatibleIterationOrder( images );
}

public < O extends RealType< O > > RandomAccess< O > randomAccess( final O outputType, final Converter< RealType< ? >, O > converter )
{
return new FunctionRandomAccess< O >( this.operation, outputType, converter );
}

public < O extends RealType< O > > RandomAccess< O > randomAccess( final O outputType )
{
return new FunctionRandomAccess< O >( this.operation, outputType, Util.genericRealTypeConverter() );
}

/** Returns a {@link RandomAccess} with the same type as the first input image found. */
public < O extends RealType< O > > RandomAccess< O > randomAccess()
{
@SuppressWarnings("unchecked")
final RandomAccessibleInterval< O > img = ( RandomAccessibleInterval< O > )Util.findFirstImg( operation );
final O outputType = img.randomAccess().get().createVariable();
return new FunctionRandomAccess< O >( this.operation, outputType, Util.genericRealTypeConverter() );
}

public < O extends RealType< O > > Cursor< O > cursor( final O outputType, final Converter< RealType< ? >, O > converter )
{
if ( this.compatible_iteration_order )
return new FunctionCursor< O >( this.operation, outputType, converter );
return new FunctionCursorIncompatibleOrder< O >( this.operation, outputType, converter );
}

public < O extends RealType< O > > Cursor< O > cursor( final O outputType )
{
return this.cursor( outputType, Util.genericRealTypeConverter() );
}

/** Returns a {@link Cursor} with the same type as the first input image found. */
public < O extends RealType< O > > Cursor< O > cursor()
{
@SuppressWarnings("unchecked")
final RandomAccessibleInterval< O > img = ( RandomAccessibleInterval< O > )Util.findFirstImg( operation );
return this.cursor( img.randomAccess().get().createVariable() );
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/imglib2/algorithm/math/If.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import net.imglib2.algorithm.math.abstractions.ATrinaryFunction;
import net.imglib2.algorithm.math.abstractions.Compare;
import net.imglib2.algorithm.math.abstractions.OFunction;
import net.imglib2.algorithm.math.execution.Comparison;
import net.imglib2.algorithm.math.execution.IfStatement;
Expand Down Expand Up @@ -41,7 +42,7 @@ public < O extends RealType< O > > OFunction< O > reInit(
// and then having to read it out and compare it to zero to make a boolean,
// instead returning a boolean directly.
final OFunction< O > instance;
if ( this.a instanceof Comparison )
if ( this.a instanceof Compare )
instance = new IfStatementBoolean< O >( ( Comparison< O > ) this.a.reInit( tmp, bindings, converter, imgS ),
this.b.reInit( tmp, bindings, converter, imgS ), this.c.reInit( tmp, bindings, converter, imgS ) );
else
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/imglib2/algorithm/math/ImgSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.math.abstractions.IFunction;
import net.imglib2.algorithm.math.abstractions.OFunction;
import net.imglib2.algorithm.math.abstractions.ViewableFunction;
import net.imglib2.algorithm.math.execution.ImgSourceIterable;
import net.imglib2.algorithm.math.execution.ImgSourceIterableDirect;
import net.imglib2.algorithm.math.execution.Variable;
import net.imglib2.converter.Converter;
import net.imglib2.type.numeric.RealType;

public class ImgSource< I extends RealType< I > > implements IFunction
public class ImgSource< I extends RealType< I > > extends ViewableFunction implements IFunction
{
private final RandomAccessibleInterval< I > rai;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/imglib2/algorithm/math/Let.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import net.imglib2.algorithm.math.abstractions.IFunction;
import net.imglib2.algorithm.math.abstractions.OFunction;
import net.imglib2.algorithm.math.abstractions.Util;
import net.imglib2.algorithm.math.abstractions.ViewableFunction;
import net.imglib2.algorithm.math.execution.LetBinding;
import net.imglib2.algorithm.math.execution.Variable;
import net.imglib2.converter.Converter;
import net.imglib2.type.numeric.RealType;

public final class Let implements IFunction, IBinaryFunction
public final class Let extends ViewableFunction implements IFunction, IBinaryFunction
{
private final String varName;
private final IFunction varValue;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/imglib2/algorithm/math/NumberSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import net.imglib2.algorithm.math.abstractions.IFunction;
import net.imglib2.algorithm.math.abstractions.OFunction;
import net.imglib2.algorithm.math.execution.IterableRandomAccessibleFunction;
import net.imglib2.algorithm.math.execution.NumericSource;
import net.imglib2.algorithm.math.execution.Variable;
import net.imglib2.converter.Converter;
Expand All @@ -26,4 +27,22 @@ public < O extends RealType< O > > NumericSource< O > reInit(
{
return new NumericSource< O >( tmp.copy(), this.number );
}

@Override
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view()
{
throw new UnsupportedOperationException();
}

@Override
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType )
{
throw new UnsupportedOperationException();
}

@Override
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType, final Converter< RealType< ? >, O > converter )
{
throw new UnsupportedOperationException();
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/imglib2/algorithm/math/Var.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import net.imglib2.algorithm.math.abstractions.IVar;
import net.imglib2.algorithm.math.abstractions.OFunction;
import net.imglib2.algorithm.math.execution.IterableRandomAccessibleFunction;
import net.imglib2.algorithm.math.execution.Variable;
import net.imglib2.converter.Converter;
import net.imglib2.type.numeric.RealType;
Expand Down Expand Up @@ -32,4 +33,22 @@ public < O extends RealType< O > > Variable< O > reInit(
{
return new Variable< O >( this.name, bindings.get( this.name ) );
}

@Override
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view()
{
throw new UnsupportedOperationException();
}

@Override
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType )
{
throw new UnsupportedOperationException();
}

@Override
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType, final Converter< RealType< ? >, O > converter )
{
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.imglib2.algorithm.math.abstractions;

abstract public class ABinaryFunction extends VarargsFunction implements IBinaryFunction
abstract public class ABinaryFunction extends ViewableFunction implements IBinaryFunction
{
protected final IFunction a, b;

Expand All @@ -12,7 +12,7 @@ public ABinaryFunction( final Object o1, final Object o2 )

public ABinaryFunction( final Object... obs )
{
final IFunction[] p = this.wrapMap( obs );
final IFunction[] p = Util.wrapMap( this, obs );
this.a = p[ 0 ];
this.b = p[ 1 ];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
package net.imglib2.algorithm.math.abstractions;

import net.imglib2.type.numeric.RealType;

abstract public class ATrinaryFunction extends VarargsFunction implements ITrinaryFunction
abstract public class ATrinaryFunction extends ViewableFunction implements ITrinaryFunction
{
protected final IFunction a, b ,c;

protected final RealType< ? > scrap;

public ATrinaryFunction( final Object o1, final Object o2, final Object o3 )
{
this.a = Util.wrap( o1 );
this.b = Util.wrap( o2 );
this.c = Util.wrap( o3 );
this.scrap = null;
}

protected ATrinaryFunction( final RealType< ? > scrap, final IFunction f1, final IFunction f2, final IFunction f3 )
{
this.scrap = scrap;
this.a = f1;
this.b = f2;
this.c = f3;
}

public final IFunction getFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.imglib2.type.numeric.RealType;

abstract public class AUnaryFunction implements IUnaryFunction
abstract public class AUnaryFunction extends ViewableFunction implements IUnaryFunction
{
protected final IFunction a;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import net.imglib2.algorithm.math.execution.IterableRandomAccessibleFunction;
import net.imglib2.algorithm.math.execution.Variable;
import net.imglib2.converter.Converter;
import net.imglib2.type.numeric.RealType;
Expand All @@ -14,4 +15,10 @@ public < O extends RealType< O > > OFunction< O > reInit(
final Converter< RealType< ? >, O > converter,
final Map< Variable< O >, OFunction< O > > imgSources
);

public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view();

public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType );

public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType, final Converter< RealType< ? >, O > converter );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.imglib2.algorithm.math.abstractions;

import net.imglib2.Localizable;

public interface IImgSourceIterable
{
public boolean hasNext();

public Localizable localizable();

public void localize( final long[] position );
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.imglib2.algorithm.math.abstractions;

import net.imglib2.type.numeric.RealType;

public interface IVar extends IFunction
{
public String getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.imglib2.algorithm.math.abstractions;

import java.util.List;

import net.imglib2.Localizable;
import net.imglib2.type.numeric.RealType;

Expand All @@ -8,4 +10,6 @@ public interface OFunction< O extends RealType< O > >
public O eval();

public O eval( final Localizable loc );

public List< OFunction< O > > children();
}
Loading