Skip to content

Commit bdc8f78

Browse files
committed
ImgMath: Util.findImg( IFunction ) to recursively find images, if any,
in an IFunction with nested operations and images.
1 parent 54577d9 commit bdc8f78

File tree

1 file changed

+34
-0
lines changed
  • src/main/java/net/imglib2/algorithm/math/abstractions

1 file changed

+34
-0
lines changed

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

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

3+
import java.util.HashSet;
34
import java.util.Iterator;
45
import java.util.LinkedList;
6+
import java.util.Set;
57

68
import net.imglib2.RandomAccessibleInterval;
79
import net.imglib2.algorithm.math.ImgSource;
@@ -76,4 +78,36 @@ else if ( o instanceof IFunction )
7678
// Make it fail
7779
return null;
7880
}
81+
82+
static public final Set< RandomAccessibleInterval< ? > > findImg ( final IFunction f )
83+
{
84+
final Set< RandomAccessibleInterval< ? > > images = new HashSet<>();
85+
final LinkedList< IFunction > ops = new LinkedList<>();
86+
ops.add( f );
87+
88+
// Iterate into the nested operations
89+
while ( ! ops.isEmpty() )
90+
{
91+
final IFunction op = ops.removeFirst();
92+
93+
if ( op instanceof ImgSource )
94+
images.add( ( ( ImgSource< ? > )op ).getRandomAccessibleInterval() );
95+
else if ( op instanceof IUnaryFunction )
96+
{
97+
ops.addLast( ( ( IUnaryFunction )op ).getFirst() );
98+
99+
if ( op instanceof IBinaryFunction )
100+
{
101+
ops.addLast( ( ( IBinaryFunction )op ).getSecond() );
102+
103+
if ( op instanceof ITrinaryFunction )
104+
{
105+
ops.addLast( ( ( ITrinaryFunction )op ).getThird() );
106+
}
107+
}
108+
}
109+
}
110+
111+
return images;
112+
}
79113
}

0 commit comments

Comments
 (0)