Skip to content

Commit b913404

Browse files
committed
ImgMath: now is type-safe.
1 parent f485f88 commit b913404

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1026
-815
lines changed

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

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import java.util.Map;
44

5-
import net.imglib2.Localizable;
65
import net.imglib2.algorithm.math.abstractions.ABinaryFunction;
7-
import net.imglib2.algorithm.math.abstractions.IFunction;
8-
import net.imglib2.algorithm.math.abstractions.IVar;
6+
import net.imglib2.algorithm.math.abstractions.OFunction;
7+
import net.imglib2.algorithm.math.execution.Addition;
8+
import net.imglib2.algorithm.math.execution.Variable;
99
import net.imglib2.converter.Converter;
1010
import net.imglib2.type.numeric.RealType;
1111

@@ -20,37 +20,14 @@ public Add( final Object... obs )
2020
{
2121
super( obs );
2222
}
23-
24-
private Add( final RealType< ? > scrap, final IFunction f1, final IFunction f2 )
25-
{
26-
super( scrap, f1, f2 );
27-
}
28-
29-
@SuppressWarnings({ "unchecked" })
30-
@Override
31-
public final RealType< ? > eval()
32-
{
33-
this.scrap.set( this.a.eval() );
34-
this.scrap.add( this.b.eval() );
35-
return this.scrap;
36-
}
37-
38-
@SuppressWarnings({ "unchecked" })
39-
@Override
40-
public final RealType< ? > eval( final Localizable loc )
41-
{
42-
this.scrap.set( this.a.eval( loc ) );
43-
this.scrap.add( this.b.eval( loc ) );
44-
return this.scrap;
45-
}
4623

4724
@Override
48-
public Add reInit(
49-
final RealType< ? > tmp,
50-
final Map< String, RealType< ? > > bindings,
51-
final Converter<RealType<?>, RealType<?>> converter,
52-
Map< IVar, IFunction > imgSources )
25+
public < O extends RealType< O > > Addition< O > reInit(
26+
final O tmp,
27+
final Map< String, O > bindings,
28+
final Converter< RealType< ? >, O > converter,
29+
Map< Variable< O >, OFunction< O > > imgSources )
5330
{
54-
return new Add( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
31+
return new Addition< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
5532
}
5633
}

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

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import net.imglib2.algorithm.math.abstractions.IFunction;
1212
import net.imglib2.algorithm.math.abstractions.ITrinaryFunction;
1313
import net.imglib2.algorithm.math.abstractions.IUnaryFunction;
14-
import net.imglib2.algorithm.math.abstractions.ImgSource;
14+
import net.imglib2.algorithm.math.abstractions.OFunction;
1515
import net.imglib2.algorithm.math.abstractions.Util;
1616
import net.imglib2.converter.Converter;
1717
import net.imglib2.type.numeric.RealType;
@@ -20,82 +20,95 @@
2020
public class Compute
2121
{
2222
private final IFunction operation;
23-
private final Converter< RealType< ? >, RealType< ? > > converter;
23+
private final boolean compatible_iteration_order;
2424

2525
/**
26-
* Prepare the operation for computation with a default converter.
27-
* The converter will be used only if necessary, that is, only if
28-
* the input image type is not the same or a sublclass of the output
29-
* image type as specified in the {@link Compute#into(RandomAccessibleInterval)}.
26+
* Validate the {@param operation}.
3027
*
3128
* @param operation
3229
*/
3330
public Compute( final IFunction operation )
3431
{
35-
this( operation, new Converter< RealType< ? >, RealType< ? > >()
36-
{
37-
@Override
38-
public final void convert( final RealType<?> input, RealType<?> output)
39-
{
40-
output.setReal( input.getRealDouble() );
41-
}
42-
});
32+
this.operation = operation;
33+
34+
// Throw RuntimeException as needed to indicate incorrect construction
35+
this.compatible_iteration_order = this.validate( this.operation );
4336
}
44-
37+
4538
/**
46-
* Prepare the operation for computation.
47-
* The converter will be used only if necessary, that is, only if
48-
* the input image type is not the same or a sublclass of the output
49-
* image type as specified in the {@link Compute#into(RandomAccessibleInterval)}.
39+
* Execute the computation and store the result into the {@code target}.
40+
* The computation is done using {@code Type}-based math, with the {@code Type}
41+
* of the {@code target} defining the specific math implementation and numerical
42+
* precision that will be used.
5043
*
51-
* @param operation
52-
* @param converter
44+
* @param target The {@code {@link RandomAccessibleInterval} into which to store the computation;
45+
* note its {@code Type} determines the precision of the computation and the specific
46+
* implementation of the mathematical operations.
47+
* @return The {@code target}.
5348
*/
54-
public Compute(
55-
final IFunction operation,
56-
final Converter< RealType< ? >, RealType< ? > > converter
57-
)
49+
public < O extends RealType< O > > RandomAccessibleInterval< O > into( final RandomAccessibleInterval< O > target )
5850
{
59-
this.operation = operation;
60-
this.converter = converter;
51+
return this.into( target, null );
6152
}
6253

63-
@SuppressWarnings({ "unchecked", "rawtypes" })
64-
public RandomAccessibleInterval< ? extends RealType< ? > > into( final RandomAccessibleInterval< ? extends RealType< ? > > target )
54+
/**
55+
* Execute the mathematical operations and store the result into the given {@code RandomAccessibleInterval}.
56+
*
57+
* @param target The {@code {@link RandomAccessibleInterval} into which to store the computation;
58+
* note its {@code Type} determines the precision of the computation and the specific
59+
* implementation of the mathematical operations.
60+
*
61+
* @param converter The {@code Converter} that transfers all input {@code Type} to the {@code Type}
62+
* of the {@code target}; when null, will create one that uses double floating-point
63+
* precision; but note that if the {@code Type} of an input {@code RandomAccessibleInterval}
64+
* is the same as that of the {@code target}, the converter will not be used.
65+
*
66+
* @return The {@code target}.
67+
*/
68+
public < O extends RealType< O > > RandomAccessibleInterval< O > into(
69+
final RandomAccessibleInterval< O > target,
70+
Converter< RealType< ? >, O > converter
71+
)
6572
{
73+
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+
};
82+
6683
// Recursive copy: initializes interval iterators and sets temporary computation holder
67-
final IFunction f = this.operation.reInit(
84+
final OFunction< O > f = this.operation.reInit(
6885
target.randomAccess().get().createVariable(),
69-
new HashMap< String, RealType< ? > >(),
70-
this.converter, null );
71-
72-
final boolean compatible_iteration_order = this.setup( f );
86+
new HashMap< String, O >(),
87+
converter, null );
7388

7489
// Check compatible iteration order and dimensions
7590
if ( compatible_iteration_order )
7691
{
7792
// Evaluate function for every pixel
78-
for ( final RealType output : Views.iterable( target ) )
93+
for ( final O output : Views.iterable( target ) )
7994
output.set( f.eval() );
8095
}
8196
else
8297
{
8398
// Incompatible iteration order
84-
final Cursor< ? extends RealType< ? > > cursor = Views.iterable( target ).cursor();
99+
final Cursor< O > cursor = Views.iterable( target ).cursor();
85100

86101
while ( cursor.hasNext() )
87102
{
88103
cursor.fwd();
89-
final RealType output = cursor.get();
90-
output.set( f.eval( cursor ) );
104+
cursor.get().set( f.eval( cursor ) );
91105
}
92106
}
93107

94108
return target;
95109
}
96110

97-
@SuppressWarnings({ "rawtypes" })
98-
private boolean setup( final IFunction f )
111+
private boolean validate( final IFunction f )
99112
{
100113
final LinkedList< IFunction > ops = new LinkedList<>();
101114
ops.add( f );
@@ -123,7 +136,7 @@ private boolean setup( final IFunction f )
123136

124137
if ( op instanceof ImgSource )
125138
{
126-
images.addLast( ( ( ImgSource )op ).getRandomAccessibleInterval() );
139+
images.addLast( ( ( ImgSource< ? > )op ).getRandomAccessibleInterval() );
127140
}
128141
else if ( op instanceof IUnaryFunction )
129142
{

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

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import java.util.Map;
44

5-
import net.imglib2.Localizable;
65
import net.imglib2.algorithm.math.abstractions.ABinaryFunction;
7-
import net.imglib2.algorithm.math.abstractions.IFunction;
8-
import net.imglib2.algorithm.math.abstractions.IVar;
6+
import net.imglib2.algorithm.math.abstractions.OFunction;
7+
import net.imglib2.algorithm.math.execution.Division;
8+
import net.imglib2.algorithm.math.execution.Variable;
99
import net.imglib2.converter.Converter;
1010
import net.imglib2.type.numeric.RealType;
1111

12-
public final class Div extends ABinaryFunction implements IFunction
12+
public final class Div extends ABinaryFunction
1313
{
1414

1515
public Div( final Object o1, final Object o2 )
@@ -21,37 +21,14 @@ public Div( final Object... obs )
2121
{
2222
super( obs );
2323
}
24-
25-
private Div( final RealType< ? > scrap, final IFunction f1, final IFunction f2 )
26-
{
27-
super( scrap, f1, f2 );
28-
}
29-
30-
@SuppressWarnings({ "unchecked" })
31-
@Override
32-
public final RealType< ? > eval()
33-
{
34-
this.scrap.set( this.a.eval() );
35-
this.scrap.div( this.b.eval() );
36-
return this.scrap;
37-
}
38-
39-
@SuppressWarnings({ "unchecked" })
40-
@Override
41-
public final RealType< ? > eval( final Localizable loc )
42-
{
43-
this.scrap.set( this.a.eval( loc ) );
44-
this.scrap.div( this.b.eval( loc ) );
45-
return this.scrap;
46-
}
4724

4825
@Override
49-
public Div reInit(
50-
final RealType<?> tmp,
51-
final Map<String, RealType<?>> bindings,
52-
final Converter<RealType<?>, RealType<?>> converter,
53-
Map< IVar, IFunction > imgSources )
26+
public < O extends RealType< O > > Division< O > reInit(
27+
final O tmp,
28+
final Map< String, O > bindings,
29+
final Converter< RealType< ? >, O > converter,
30+
Map< Variable< O >, OFunction< O > > imgSources )
5431
{
55-
return new Div( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
32+
return new Division< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
5633
}
5734
}

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,26 @@
33
import java.util.Map;
44

55
import net.imglib2.algorithm.math.abstractions.Compare;
6-
import net.imglib2.algorithm.math.abstractions.IFunction;
7-
import net.imglib2.algorithm.math.abstractions.IVar;
6+
import net.imglib2.algorithm.math.abstractions.OFunction;
7+
import net.imglib2.algorithm.math.execution.Equality;
8+
import net.imglib2.algorithm.math.execution.Variable;
89
import net.imglib2.converter.Converter;
910
import net.imglib2.type.numeric.RealType;
1011

1112
public final class Equal extends Compare
1213
{
13-
public Equal( final Object o1, final Object o2) {
14-
super( o1, o2 );
15-
}
16-
17-
private Equal( final RealType< ? > scrap, final IFunction f1, final IFunction f2 )
18-
{
19-
super( scrap, f1, f2 );
20-
}
21-
22-
@SuppressWarnings({ "rawtypes", "unchecked" })
23-
@Override
24-
public final boolean compare( final RealType t1, final RealType t2 )
14+
public Equal( final Object o1, final Object o2 )
2515
{
26-
return 0 == t1.compareTo( t2 );
16+
super( o1, o2 );
2717
}
2818

2919
@Override
30-
public Equal reInit(
31-
final RealType< ? > tmp,
32-
final Map< String, RealType< ? > > bindings,
33-
final Converter<RealType<?>, RealType<?>> converter,
34-
final Map< IVar, IFunction > imgSources )
20+
public < O extends RealType< O > > OFunction< O > reInit(
21+
final O tmp,
22+
final Map< String, O > bindings,
23+
final Converter< RealType< ? >, O > converter,
24+
final Map< Variable< O >, OFunction< O > > imgSources )
3525
{
36-
return new Equal( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
26+
return new Equality< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
3727
}
3828
}

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,26 @@
33
import java.util.Map;
44

55
import net.imglib2.algorithm.math.abstractions.Compare;
6-
import net.imglib2.algorithm.math.abstractions.IFunction;
7-
import net.imglib2.algorithm.math.abstractions.IVar;
6+
import net.imglib2.algorithm.math.abstractions.OFunction;
7+
import net.imglib2.algorithm.math.execution.IsGreaterThan;
8+
import net.imglib2.algorithm.math.execution.Variable;
89
import net.imglib2.converter.Converter;
910
import net.imglib2.type.numeric.RealType;
1011

1112
public final class GreaterThan extends Compare
1213
{
13-
public GreaterThan( final Object o1, final Object o2) {
14-
super(o1, o2);
15-
}
16-
17-
private GreaterThan( final RealType< ? > scrap, final IFunction f1, final IFunction f2 )
18-
{
19-
super( scrap, f1, f2 );
20-
}
21-
22-
@SuppressWarnings({ "rawtypes", "unchecked" })
23-
public final boolean compare( final RealType t1, final RealType t2 )
14+
public GreaterThan( final Object o1, final Object o2 )
2415
{
25-
return 1 == t1.compareTo( t2 );
16+
super( o1, o2 );
2617
}
2718

2819
@Override
29-
public GreaterThan reInit(
30-
final RealType<?> tmp,
31-
final Map<String, RealType<?>> bindings,
32-
final Converter<RealType<?>, RealType<?>> converter,
33-
Map< IVar, IFunction > imgSources )
20+
public < O extends RealType< O > > IsGreaterThan< O > reInit(
21+
final O tmp,
22+
final Map< String, O > bindings,
23+
final Converter< RealType< ? >, O > converter,
24+
Map< Variable< O >, OFunction< O > > imgSources )
3425
{
35-
return new GreaterThan( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
26+
return new IsGreaterThan< O >( tmp.copy(), this.a.reInit( tmp, bindings, converter, imgSources ), this.b.reInit( tmp, bindings, converter, imgSources ) );
3627
}
3728
}

0 commit comments

Comments
 (0)