Skip to content

Commit 214c806

Browse files
committed
ImgMath: enhanced brevity of formulation by statically accessing operations
via static methods, with the operation classes now hidden as private.
1 parent dc26af0 commit 214c806

File tree

2 files changed

+117
-73
lines changed

2 files changed

+117
-73
lines changed

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

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,87 @@ static public boolean compatibleIterationOrder( final LinkedList< RandomAccessib
216216
return same_iteration_order;
217217
}
218218

219+
static public final Add add( final Object o1, final Object o2 )
220+
{
221+
return new Add( o1, o2 );
222+
}
223+
224+
static public final Add add( final Object... obs )
225+
{
226+
return new Add( obs );
227+
}
228+
229+
static public final Sub sub( final Object o1, final Object o2 )
230+
{
231+
return new Sub( o1, o2 );
232+
}
233+
234+
static public final Sub sub( final Object... obs )
235+
{
236+
return new Sub( obs );
237+
}
238+
239+
static public final Mul mul( final Object o1, final Object o2 )
240+
{
241+
return new Mul( o1, o2 );
242+
}
243+
244+
static public final Mul mul( final Object... obs )
245+
{
246+
return new Mul( obs );
247+
}
248+
249+
static public final Div div( final Object o1, final Object o2 )
250+
{
251+
return new Div( o1, o2 );
252+
}
253+
254+
static public final Div div( final Object... obs )
255+
{
256+
return new Div( obs );
257+
}
258+
259+
static public final Max max( final Object o1, final Object o2 )
260+
{
261+
return new Max( o1, o2 );
262+
}
263+
264+
static public final Max max( final Object... obs )
265+
{
266+
return new Max( obs );
267+
}
268+
269+
static public final Min min( final Object o1, final Object o2 )
270+
{
271+
return new Min( o1, o2 );
272+
}
273+
274+
static public final Min min( final Object... obs )
275+
{
276+
return new Min( obs );
277+
}
278+
279+
static public final Let let( final String varName, final Object varValue, final Object body )
280+
{
281+
return new Let( varName, varValue, body );
282+
}
283+
284+
static public final Let let( final Object[] pairs, final Object body )
285+
{
286+
return new Let( pairs, body );
287+
}
288+
289+
static public final Let let( final Object... obs )
290+
{
291+
return new Let( obs );
292+
}
293+
294+
static public final Var var( final String name )
295+
{
296+
return new Var( name );
297+
}
298+
299+
219300
static public interface IFunction
220301
{
221302
public void eval( RealType< ? > output );
@@ -237,7 +318,7 @@ static public interface IBinaryFunction extends IUnaryFunction
237318
public IFunction getSecond();
238319
}
239320

240-
static protected final class IterableImgSource< I extends RealType< I > > implements IFunction
321+
static private final class IterableImgSource< I extends RealType< I > > implements IFunction
241322
{
242323
private final RandomAccessibleInterval< I > rai;
243324
private final Iterator< I > it;
@@ -276,7 +357,7 @@ public void setConverter( final Converter< RealType< ? >, RealType< ? > > conver
276357
}
277358
}
278359

279-
static protected final class NumberSource implements IFunction
360+
static private final class NumberSource implements IFunction
280361
{
281362
private final double number;
282363

@@ -410,7 +491,7 @@ public void setScrap( final RealType< ? > output )
410491
}
411492
}
412493

413-
static public class Mul extends BinaryFunction
494+
static private final class Mul extends BinaryFunction
414495
{
415496
public Mul( final Object o1, final Object o2 )
416497
{
@@ -446,7 +527,7 @@ public Mul copy() {
446527
}
447528
}
448529

449-
static public class Div extends BinaryFunction implements IFunction
530+
static private final class Div extends BinaryFunction implements IFunction
450531
{
451532

452533
public Div( final Object o1, final Object o2 )
@@ -483,7 +564,7 @@ public Div copy() {
483564
}
484565
}
485566

486-
static public class Max extends BinaryFunction implements IFunction
567+
static private final class Max extends BinaryFunction implements IFunction
487568
{
488569

489570
public Max( final Object o1, final Object o2 )
@@ -522,7 +603,7 @@ public Max copy() {
522603
}
523604
}
524605

525-
static public class Min extends BinaryFunction
606+
static private final class Min extends BinaryFunction
526607
{
527608

528609
public Min( final Object o1, final Object o2 )
@@ -561,7 +642,7 @@ public Min copy() {
561642
}
562643
}
563644

564-
static public class Add extends BinaryFunction
645+
static private final class Add extends BinaryFunction
565646
{
566647

567648
public Add( final Object o1, final Object o2 )
@@ -598,7 +679,7 @@ public Add copy() {
598679
}
599680
}
600681

601-
static public class Sub extends BinaryFunction
682+
static private final class Sub extends BinaryFunction
602683
{
603684

604685
public Sub( final Object o1, final Object o2 )
@@ -635,14 +716,6 @@ public Sub copy() {
635716
}
636717
}
637718

638-
static public class Neg extends Sub
639-
{
640-
public Neg( final Object o )
641-
{
642-
super( 0, o );
643-
}
644-
}
645-
646719
static public final class Let implements IFunction, IBinaryFunction
647720
{
648721
private final String varName;

src/test/java/net/imglib2/algorithm/math/ImgMathTest.java

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package net.imglib2.algorithm.math;
22

3-
import static net.imglib2.algorithm.math.ImgMath.Add;
4-
import static net.imglib2.algorithm.math.ImgMath.Sub;
5-
import static net.imglib2.algorithm.math.ImgMath.Mul;
6-
import static net.imglib2.algorithm.math.ImgMath.Div;
7-
import static net.imglib2.algorithm.math.ImgMath.Max;
8-
import static net.imglib2.algorithm.math.ImgMath.Min;
9-
import static net.imglib2.algorithm.math.ImgMath.Neg;
10-
import static net.imglib2.algorithm.math.ImgMath.Let;
11-
import static net.imglib2.algorithm.math.ImgMath.Var;
3+
import static net.imglib2.algorithm.math.ImgMath.add;
4+
import static net.imglib2.algorithm.math.ImgMath.sub;
5+
import static net.imglib2.algorithm.math.ImgMath.mul;
6+
import static net.imglib2.algorithm.math.ImgMath.div;
7+
import static net.imglib2.algorithm.math.ImgMath.max;
8+
import static net.imglib2.algorithm.math.ImgMath.min;
9+
import static net.imglib2.algorithm.math.ImgMath.let;
10+
import static net.imglib2.algorithm.math.ImgMath.var;
1211
import static org.junit.Assert.assertTrue;
1312

1413
import org.junit.Test;
@@ -51,7 +50,7 @@ protected static boolean testImgMath1( )
5150
final ArrayImg< FloatType, ? > brightness = new ArrayImgFactory< FloatType >( new FloatType() ).create( dims );
5251

5352
try {
54-
new ImgMath( new Div( new Max( red, new Max( green, blue ) ), 3.0 ) ).into( brightness );
53+
new ImgMath( div( max( red, max( green, blue ) ), 3.0 ) ).into( brightness );
5554
} catch (Exception e) {
5655
e.printStackTrace();
5756
}
@@ -87,7 +86,7 @@ protected static boolean testIterationOrder() {
8786
// Divide pixels from each other (better than subtract: zero sum would be the default in case of error)
8887
final ArrayImg< LongType, ? > img3 = new ArrayImgFactory<>( new LongType() ).create( img1 );
8988
try {
90-
new ImgMath( new Div( img1, img2 ) ).into( img3 );
89+
new ImgMath( div( img1, img2 ) ).into( img3 );
9190
} catch (Exception e) {
9291
e.printStackTrace();
9392
}
@@ -123,7 +122,7 @@ protected static boolean comparePerformance( final int n_iterations ) {
123122
for ( int i=0; i < n_iterations; ++i ) {
124123
final long t0 = System.nanoTime();
125124
try {
126-
new ImgMath( new Div( new Max( red, new Max( green, blue ) ), 3.0 ) ).into( brightness );
125+
new ImgMath( div( max( red, max( green, blue ) ), 3.0 ) ).into( brightness );
127126
} catch (Exception e) {
128127
e.printStackTrace();
129128
}
@@ -186,7 +185,7 @@ protected static boolean testVarags() {
186185
final ArrayImg< FloatType, ? > brightness = new ArrayImgFactory< FloatType >( new FloatType() ).create( dims );
187186

188187
try {
189-
new ImgMath( new Div( new Max( red, green, blue ), 3.0 ) ).into( brightness );
188+
new ImgMath( div( max( red, green, blue ), 3.0 ) ).into( brightness );
190189
} catch (Exception e) {
191190
e.printStackTrace();
192191
}
@@ -201,36 +200,13 @@ protected static boolean testVarags() {
201200
return 100 * 100 * 100 * 10 == sum;
202201
}
203202

204-
static protected boolean testNeg() {
205-
final ArrayImg< FloatType, ? > in = new ArrayImgFactory< FloatType >( new FloatType() ).create( new long[]{ 10, 10 } );
206-
for ( final FloatType t : in )
207-
t.setOne();
208-
final ArrayImg< FloatType, ? > out = new ArrayImgFactory< FloatType >( new FloatType() ).create( new long[]{ 10, 10 } );
209-
210-
try {
211-
new ImgMath( new Neg( in ) ).into( out );
212-
} catch (Exception e) {
213-
e.printStackTrace();
214-
}
215-
216-
double sum = 0;
217-
218-
for ( final FloatType t: out )
219-
sum += t.getRealDouble();
220-
221-
System.out.println( "Sum Neg: " + sum );
222-
223-
224-
return out.dimension( 0 ) * out.dimension( 1 ) == -sum;
225-
}
226-
227203
protected boolean testLetOneLevel() {
228204

229205
final ArrayImg< FloatType, ? > img = new ArrayImgFactory< FloatType >( new FloatType() ).create( new long[]{ 10, 10 } );
230206
final ArrayImg< FloatType, ? > target = new ArrayImgFactory< FloatType >(new FloatType() ).create( img );
231207

232208
try {
233-
new ImgMath( new Let( "one", 1, new Add( img, new Var( "one" ) ) ) ).into( target );
209+
new ImgMath( let( "one", 1, add( img, var( "one" ) ) ) ).into( target );
234210
} catch (Exception e) {
235211
e.printStackTrace();
236212
}
@@ -250,10 +226,10 @@ protected boolean testLetTwoLevels() {
250226
final ArrayImg< FloatType, ? > target = new ArrayImgFactory< FloatType >(new FloatType() ).create( img );
251227

252228
try {
253-
new ImgMath( new Add( new Let( "one", 1,
254-
new Add( img, new Var( "one" ) ) ),
255-
new Let( "two", 2,
256-
new Add( new Var( "two" ), 0 ) ) ) ).into( target );
229+
new ImgMath( add( let( "one", 1,
230+
add( img, var( "one" ) ) ),
231+
let( "two", 2,
232+
add( var( "two" ), 0 ) ) ) ).into( target );
257233
} catch (Exception e) {
258234
e.printStackTrace();
259235
}
@@ -276,9 +252,9 @@ protected boolean testMultiLet() {
276252
t.setReal( 100.0d );
277253

278254
try {
279-
new ImgMath( new Let( "pixel", img,
280-
"constant", 10.0d,
281-
new Add( new Var( "pixel"), new Var( "constant" ) ) ) ).into( target );
255+
new ImgMath( let( "pixel", img,
256+
"constant", 10.0d,
257+
add( var( "pixel"), var( "constant" ) ) ) ).into( target );
282258
} catch (Exception e) {
283259
e.printStackTrace();
284260
}
@@ -301,10 +277,10 @@ protected boolean testNestedMultiLet() {
301277
t.setReal( 100.0d );
302278

303279
try {
304-
new ImgMath( new Let( "pixel", img,
305-
"constant", 10.0d,
306-
new Add( new Var( "pixel"), new Let( "pixel2", img,
307-
new Sub( new Var( "pixel2" ), new Var( "constant" ) ) ) ) ) ).into( target );
280+
new ImgMath( let( "pixel", img,
281+
"constant", 10.0d,
282+
add( var( "pixel"), let( "pixel2", img,
283+
sub( var( "pixel2" ), var( "constant" ) ) ) ) ) ).into( target );
308284
} catch (Exception e) {
309285
e.printStackTrace();
310286
}
@@ -319,31 +295,26 @@ protected boolean testNestedMultiLet() {
319295
}
320296

321297

322-
//@Test
298+
@Test
323299
public void test1() {
324300
assertTrue( testImgMath1() );
325301
}
326302

327-
//@Test
303+
@Test
328304
public void test2() {
329305
assertTrue( testIterationOrder() );
330306
}
331307

332-
//@Test
308+
@Test
333309
public void test3() {
334310
assertTrue ( comparePerformance( 30 ) );
335311
}
336312

337-
//@Test
313+
@Test
338314
public void test4() {
339315
assertTrue( testVarags() );
340316
}
341317

342-
//@Test
343-
public void test5() {
344-
assertTrue( testNeg() );
345-
}
346-
347318
@Test
348319
public void testLet1Simple() {
349320
assertTrue( testLetOneLevel() );

0 commit comments

Comments
 (0)