Skip to content

Commit a1165a6

Browse files
authored
Merge pull request #76 from imglib/imgmath-fixes
ImgMath: math operations can now be viewed without having to copy the result into an image
2 parents 8811f37 + b634bbe commit a1165a6

36 files changed

+829
-83
lines changed

src/main/java/net/imglib2/algorithm/math/Compute.java

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
import java.util.LinkedList;
77

88
import net.imglib2.Cursor;
9+
import net.imglib2.RandomAccess;
910
import net.imglib2.RandomAccessibleInterval;
1011
import net.imglib2.algorithm.math.abstractions.IBinaryFunction;
1112
import net.imglib2.algorithm.math.abstractions.IFunction;
1213
import net.imglib2.algorithm.math.abstractions.ITrinaryFunction;
1314
import net.imglib2.algorithm.math.abstractions.IUnaryFunction;
1415
import net.imglib2.algorithm.math.abstractions.OFunction;
1516
import net.imglib2.algorithm.math.abstractions.Util;
17+
import net.imglib2.algorithm.math.execution.FunctionCursor;
18+
import net.imglib2.algorithm.math.execution.FunctionCursorIncompatibleOrder;
19+
import net.imglib2.algorithm.math.execution.FunctionRandomAccess;
1620
import net.imglib2.converter.Converter;
1721
import net.imglib2.type.numeric.RealType;
1822
import net.imglib2.view.Views;
@@ -21,7 +25,7 @@ public class Compute
2125
{
2226
private final IFunction operation;
2327
private final boolean compatible_iteration_order;
24-
28+
2529
/**
2630
* Validate the {code operation}.
2731
*
@@ -71,14 +75,7 @@ public < O extends RealType< O > > RandomAccessibleInterval< O > into(
7175
)
7276
{
7377
if ( null == converter )
74-
converter = new Converter< RealType< ? >, O >()
75-
{
76-
@Override
77-
public final void convert( final RealType< ? > input, final O output)
78-
{
79-
output.setReal( input.getRealDouble() );
80-
}
81-
};
78+
converter = Util.genericRealTypeConverter();
8279

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

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

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

128-
IFunction parent = null;
129-
130-
// Iterate into the nested operations
126+
// Iterate into the nested operations, depth-first
131127
while ( ! ops.isEmpty() )
132128
{
133129
final IFunction op = ops.removeFirst();
134-
cp.put( op, parent );
135-
parent = op;
136130

137131
if ( op instanceof ImgSource )
138132
{
139-
images.addLast( ( ( ImgSource< ? > )op ).getRandomAccessibleInterval() );
133+
images.addFirst( ( ( ImgSource< ? > )op ).getRandomAccessibleInterval() );
140134
}
141135
else if ( op instanceof IUnaryFunction )
142136
{
143-
ops.addLast( ( ( IUnaryFunction )op ).getFirst() );
137+
final IFunction first = ( ( IUnaryFunction )op ).getFirst();
138+
ops.addFirst( first );
139+
cp.put( first, op );
144140

145141
if ( op instanceof IBinaryFunction )
146142
{
147-
ops.addLast( ( ( IBinaryFunction )op ).getSecond() );
143+
final IFunction second = ( ( IBinaryFunction )op ).getSecond();
144+
ops.add( 1, second );
145+
cp.put( second, op );
148146

149147
if ( op instanceof Let )
150148
{
@@ -153,7 +151,9 @@ else if ( op instanceof IUnaryFunction )
153151

154152
if ( op instanceof ITrinaryFunction )
155153
{
156-
ops.addLast( ( ( ITrinaryFunction )op ).getThird() );
154+
final IFunction third = ( ( ITrinaryFunction )op ).getThird();
155+
ops.add( 2, third );
156+
cp.put( third, op );
157157
}
158158
}
159159
}
@@ -168,16 +168,16 @@ else if ( op instanceof Var )
168168
final HashSet< Let > used = new HashSet<>();
169169
all: for ( final Var var : vars )
170170
{
171-
parent = var;
171+
IFunction parent = var;
172172
while ( null != ( parent = cp.get( parent ) ) )
173173
{
174174
if ( parent instanceof Let )
175175
{
176-
Let let = ( Let )parent;
176+
final Let let = ( Let )parent;
177177
if ( let.getVarName() != var.getName() )
178178
continue;
179179
// Else, found: Var is in use
180-
used.add( let );
180+
used.add( let ); // might already be in used
181181
continue all;
182182
}
183183
}
@@ -200,4 +200,43 @@ else if ( op instanceof Var )
200200

201201
return Util.compatibleIterationOrder( images );
202202
}
203+
204+
public < O extends RealType< O > > RandomAccess< O > randomAccess( final O outputType, final Converter< RealType< ? >, O > converter )
205+
{
206+
return new FunctionRandomAccess< O >( this.operation, outputType, converter );
207+
}
208+
209+
public < O extends RealType< O > > RandomAccess< O > randomAccess( final O outputType )
210+
{
211+
return new FunctionRandomAccess< O >( this.operation, outputType, Util.genericRealTypeConverter() );
212+
}
213+
214+
/** Returns a {@link RandomAccess} with the same type as the first input image found. */
215+
public < O extends RealType< O > > RandomAccess< O > randomAccess()
216+
{
217+
@SuppressWarnings("unchecked")
218+
final RandomAccessibleInterval< O > img = ( RandomAccessibleInterval< O > )Util.findFirstImg( operation );
219+
final O outputType = img.randomAccess().get().createVariable();
220+
return new FunctionRandomAccess< O >( this.operation, outputType, Util.genericRealTypeConverter() );
221+
}
222+
223+
public < O extends RealType< O > > Cursor< O > cursor( final O outputType, final Converter< RealType< ? >, O > converter )
224+
{
225+
if ( this.compatible_iteration_order )
226+
return new FunctionCursor< O >( this.operation, outputType, converter );
227+
return new FunctionCursorIncompatibleOrder< O >( this.operation, outputType, converter );
228+
}
229+
230+
public < O extends RealType< O > > Cursor< O > cursor( final O outputType )
231+
{
232+
return this.cursor( outputType, Util.genericRealTypeConverter() );
233+
}
234+
235+
/** Returns a {@link Cursor} with the same type as the first input image found. */
236+
public < O extends RealType< O > > Cursor< O > cursor()
237+
{
238+
@SuppressWarnings("unchecked")
239+
final RandomAccessibleInterval< O > img = ( RandomAccessibleInterval< O > )Util.findFirstImg( operation );
240+
return this.cursor( img.randomAccess().get().createVariable() );
241+
}
203242
}

src/main/java/net/imglib2/algorithm/math/If.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55

66
import net.imglib2.algorithm.math.abstractions.ATrinaryFunction;
7+
import net.imglib2.algorithm.math.abstractions.Compare;
78
import net.imglib2.algorithm.math.abstractions.OFunction;
89
import net.imglib2.algorithm.math.execution.Comparison;
910
import net.imglib2.algorithm.math.execution.IfStatement;
@@ -41,7 +42,7 @@ public < O extends RealType< O > > OFunction< O > reInit(
4142
// and then having to read it out and compare it to zero to make a boolean,
4243
// instead returning a boolean directly.
4344
final OFunction< O > instance;
44-
if ( this.a instanceof Comparison )
45+
if ( this.a instanceof Compare )
4546
instance = new IfStatementBoolean< O >( ( Comparison< O > ) this.a.reInit( tmp, bindings, converter, imgS ),
4647
this.b.reInit( tmp, bindings, converter, imgS ), this.c.reInit( tmp, bindings, converter, imgS ) );
4748
else

src/main/java/net/imglib2/algorithm/math/ImgSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import net.imglib2.RandomAccessibleInterval;
66
import net.imglib2.algorithm.math.abstractions.IFunction;
77
import net.imglib2.algorithm.math.abstractions.OFunction;
8+
import net.imglib2.algorithm.math.abstractions.ViewableFunction;
89
import net.imglib2.algorithm.math.execution.ImgSourceIterable;
910
import net.imglib2.algorithm.math.execution.ImgSourceIterableDirect;
1011
import net.imglib2.algorithm.math.execution.Variable;
1112
import net.imglib2.converter.Converter;
1213
import net.imglib2.type.numeric.RealType;
1314

14-
public class ImgSource< I extends RealType< I > > implements IFunction
15+
public class ImgSource< I extends RealType< I > > extends ViewableFunction implements IFunction
1516
{
1617
private final RandomAccessibleInterval< I > rai;
1718

src/main/java/net/imglib2/algorithm/math/Let.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import net.imglib2.algorithm.math.abstractions.IFunction;
88
import net.imglib2.algorithm.math.abstractions.OFunction;
99
import net.imglib2.algorithm.math.abstractions.Util;
10+
import net.imglib2.algorithm.math.abstractions.ViewableFunction;
1011
import net.imglib2.algorithm.math.execution.LetBinding;
1112
import net.imglib2.algorithm.math.execution.Variable;
1213
import net.imglib2.converter.Converter;
1314
import net.imglib2.type.numeric.RealType;
1415

15-
public final class Let implements IFunction, IBinaryFunction
16+
public final class Let extends ViewableFunction implements IFunction, IBinaryFunction
1617
{
1718
private final String varName;
1819
private final IFunction varValue;

src/main/java/net/imglib2/algorithm/math/NumberSource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import net.imglib2.algorithm.math.abstractions.IFunction;
66
import net.imglib2.algorithm.math.abstractions.OFunction;
7+
import net.imglib2.algorithm.math.execution.IterableRandomAccessibleFunction;
78
import net.imglib2.algorithm.math.execution.NumericSource;
89
import net.imglib2.algorithm.math.execution.Variable;
910
import net.imglib2.converter.Converter;
@@ -26,4 +27,22 @@ public < O extends RealType< O > > NumericSource< O > reInit(
2627
{
2728
return new NumericSource< O >( tmp.copy(), this.number );
2829
}
30+
31+
@Override
32+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view()
33+
{
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
@Override
38+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType )
39+
{
40+
throw new UnsupportedOperationException();
41+
}
42+
43+
@Override
44+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType, final Converter< RealType< ? >, O > converter )
45+
{
46+
throw new UnsupportedOperationException();
47+
}
2948
}

src/main/java/net/imglib2/algorithm/math/Var.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import net.imglib2.algorithm.math.abstractions.IVar;
66
import net.imglib2.algorithm.math.abstractions.OFunction;
7+
import net.imglib2.algorithm.math.execution.IterableRandomAccessibleFunction;
78
import net.imglib2.algorithm.math.execution.Variable;
89
import net.imglib2.converter.Converter;
910
import net.imglib2.type.numeric.RealType;
@@ -32,4 +33,22 @@ public < O extends RealType< O > > Variable< O > reInit(
3233
{
3334
return new Variable< O >( this.name, bindings.get( this.name ) );
3435
}
36+
37+
@Override
38+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view()
39+
{
40+
throw new UnsupportedOperationException();
41+
}
42+
43+
@Override
44+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType )
45+
{
46+
throw new UnsupportedOperationException();
47+
}
48+
49+
@Override
50+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType, final Converter< RealType< ? >, O > converter )
51+
{
52+
throw new UnsupportedOperationException();
53+
}
3554
}

src/main/java/net/imglib2/algorithm/math/abstractions/ABinaryFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.imglib2.algorithm.math.abstractions;
22

3-
abstract public class ABinaryFunction extends VarargsFunction implements IBinaryFunction
3+
abstract public class ABinaryFunction extends ViewableFunction implements IBinaryFunction
44
{
55
protected final IFunction a, b;
66

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

1313
public ABinaryFunction( final Object... obs )
1414
{
15-
final IFunction[] p = this.wrapMap( obs );
15+
final IFunction[] p = Util.wrapMap( this, obs );
1616
this.a = p[ 0 ];
1717
this.b = p[ 1 ];
1818
}

src/main/java/net/imglib2/algorithm/math/abstractions/ATrinaryFunction.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
package net.imglib2.algorithm.math.abstractions;
22

3-
import net.imglib2.type.numeric.RealType;
4-
5-
abstract public class ATrinaryFunction extends VarargsFunction implements ITrinaryFunction
3+
abstract public class ATrinaryFunction extends ViewableFunction implements ITrinaryFunction
64
{
75
protected final IFunction a, b ,c;
8-
9-
protected final RealType< ? > scrap;
106

117
public ATrinaryFunction( final Object o1, final Object o2, final Object o3 )
128
{
139
this.a = Util.wrap( o1 );
1410
this.b = Util.wrap( o2 );
1511
this.c = Util.wrap( o3 );
16-
this.scrap = null;
17-
}
18-
19-
protected ATrinaryFunction( final RealType< ? > scrap, final IFunction f1, final IFunction f2, final IFunction f3 )
20-
{
21-
this.scrap = scrap;
22-
this.a = f1;
23-
this.b = f2;
24-
this.c = f3;
2512
}
2613

2714
public final IFunction getFirst()

src/main/java/net/imglib2/algorithm/math/abstractions/AUnaryFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.imglib2.type.numeric.RealType;
44

5-
abstract public class AUnaryFunction implements IUnaryFunction
5+
abstract public class AUnaryFunction extends ViewableFunction implements IUnaryFunction
66
{
77
protected final IFunction a;
88

src/main/java/net/imglib2/algorithm/math/abstractions/IFunction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import net.imglib2.algorithm.math.execution.IterableRandomAccessibleFunction;
56
import net.imglib2.algorithm.math.execution.Variable;
67
import net.imglib2.converter.Converter;
78
import net.imglib2.type.numeric.RealType;
@@ -14,4 +15,10 @@ public < O extends RealType< O > > OFunction< O > reInit(
1415
final Converter< RealType< ? >, O > converter,
1516
final Map< Variable< O >, OFunction< O > > imgSources
1617
);
18+
19+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view();
20+
21+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType );
22+
23+
public < O extends RealType< O > > IterableRandomAccessibleFunction< O > view( final O outputType, final Converter< RealType< ? >, O > converter );
1724
}

0 commit comments

Comments
 (0)