Skip to content

Commit 77cbc08

Browse files
committed
ImgMath: tests for KDtreeSource "gen"
1 parent 9ac8b78 commit 77cbc08

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import static net.imglib2.algorithm.math.ImgMath.OR;
2424
import static net.imglib2.algorithm.math.ImgMath.XOR;
2525
import static net.imglib2.algorithm.math.ImgMath.NOT;
26+
import static net.imglib2.algorithm.math.ImgMath.block;
27+
import static net.imglib2.algorithm.math.ImgMath.gen;
2628

2729

2830
import static org.junit.Assert.assertTrue;
@@ -36,17 +38,22 @@
3638
import java.nio.ByteBuffer;
3739
import java.nio.channels.Channels;
3840
import java.nio.channels.ReadableByteChannel;
41+
import java.util.ArrayList;
3942

4043
import javax.imageio.ImageIO;
4144

4245
import org.junit.Test;
4346

4447
import net.imglib2.Cursor;
4548
import net.imglib2.IterableInterval;
49+
import net.imglib2.KDTree;
50+
import net.imglib2.Point;
4651
import net.imglib2.RandomAccess;
4752
import net.imglib2.RandomAccessibleInterval;
53+
import net.imglib2.algorithm.integral.IntegralImg;
4854
import net.imglib2.algorithm.math.abstractions.IFunction;
4955
import net.imglib2.algorithm.math.abstractions.Util;
56+
import net.imglib2.converter.Converter;
5057
import net.imglib2.converter.Converters;
5158
import net.imglib2.img.Img;
5259
import net.imglib2.img.array.ArrayImg;
@@ -59,6 +66,7 @@
5966
import net.imglib2.type.numeric.RealType;
6067
import net.imglib2.type.numeric.integer.LongType;
6168
import net.imglib2.type.numeric.integer.UnsignedByteType;
69+
import net.imglib2.type.numeric.integer.UnsignedLongType;
6270
import net.imglib2.type.numeric.integer.UnsignedShortType;
6371
import net.imglib2.type.numeric.real.DoubleType;
6472
import net.imglib2.type.numeric.real.FloatType;
@@ -803,6 +811,7 @@ static private final < T extends RealType< T > > boolean same( final Img< T > im
803811
return true;
804812
}
805813

814+
@SuppressWarnings("unchecked")
806815
@Test
807816
public void test10Logical() {
808817
System.out.println("test10Logical");
@@ -833,16 +842,16 @@ public void test10Logical() {
833842
img4 = into4x4Img( pixels4 );
834843

835844
// AND
836-
assertTrue( same( img1, (Img< UnsignedByteType >) compute(AND(img1, img2)).intoArrayImg() ) );
845+
assertTrue( same( img1, ( Img< UnsignedByteType > )compute(AND(img1, img2)).intoArrayImg() ) );
837846
// OR
838-
assertTrue( same( img2, (Img< UnsignedByteType >) compute(OR(img1, img2)).intoArrayImg() ) );
847+
assertTrue( same( img2, ( Img< UnsignedByteType > )compute(OR(img1, img2)).intoArrayImg() ) );
839848
// XOR
840-
assertTrue( same( img3, (Img< UnsignedByteType >) compute(XOR(img1, img2)).intoArrayImg() ) );
849+
assertTrue( same( img3, ( Img< UnsignedByteType > )compute(XOR(img1, img2)).intoArrayImg() ) );
841850
// NOT
842-
assertTrue( same( img4, (Img< UnsignedByteType >) compute(NOT(img1)).intoArrayImg() ) );
851+
assertTrue( same( img4, ( Img< UnsignedByteType > )compute(NOT(img1)).intoArrayImg() ) );
843852

844853
// Test LogicalAndBoolean
845-
final Img< UnsignedByteType> imgIFAND = (Img< UnsignedByteType>) compute(IF(AND(LT(img1, 1), LT(img2, 1)),
854+
final Img< UnsignedByteType> imgIFAND = ( Img< UnsignedByteType> )compute(IF(AND(LT(img1, 1), LT(img2, 1)),
846855
THEN(1),
847856
ELSE(0))).intoArrayImg();
848857

@@ -853,6 +862,49 @@ public void test10Logical() {
853862
assertTrue( same(imgTEST, imgIFAND ) );
854863
}
855864

865+
@Test
866+
public void test11IntegralBlockReading() {
867+
System.out.println("test11IntegralBlockReading");
868+
final Img< UnsignedByteType > img = ArrayImgs.unsignedBytes( 3, 3 );
869+
for ( final UnsignedByteType t : img )
870+
t.setOne();
871+
final IntegralImg< UnsignedByteType, UnsignedLongType > ii = new IntegralImg< UnsignedByteType, UnsignedLongType >( img, new UnsignedLongType(),
872+
new Converter< UnsignedByteType, UnsignedLongType >() {
873+
@Override
874+
public void convert( final UnsignedByteType input, final UnsignedLongType output) {
875+
output.setInteger( input.getIntegerLong() );
876+
}
877+
});
878+
ii.process();
879+
final Img< UnsignedLongType > integralImg = ii.getResult();
880+
final RandomAccessibleInterval< UnsignedLongType > view = block( Views.extendBorder( integralImg ), 3 ).view( new UnsignedLongType() );
881+
final RandomAccess< UnsignedLongType > ra = view.randomAccess();
882+
ra.setPosition( new long[]{ 1, 1 } );
883+
assertTrue( 9 == ra.get().get() );
884+
}
885+
886+
@Test
887+
public void test12KDTreeGen()
888+
{
889+
System.out.println("test12KDTreeGen");
890+
final ArrayList< UnsignedByteType > values = new ArrayList<>();
891+
values.add( new UnsignedByteType( 1 ) );
892+
values.add( new UnsignedByteType( 2 ) );
893+
final ArrayList< Point > positions = new ArrayList<>();
894+
positions.add( Point.wrap( new long[]{ 25, 25 } ) );
895+
positions.add( Point.wrap( new long[]{ 200, 400 } ) );
896+
final KDTree< UnsignedByteType > kdtree = new KDTree<>( values, positions );
897+
final RandomAccess< UnsignedByteType > ra = gen( kdtree, 5 ).view( new UnsignedByteType() ).randomAccess();
898+
ra.setPosition( new long[]{ 20, 20 } );
899+
assertTrue( 0 == ra.get().get() );
900+
ra.setPosition( new long[]{ 23, 29 } );
901+
assertTrue( 1 == ra.get().get() );
902+
ra.setPosition( new long[]{ 200, 400 } );
903+
assertTrue( 2 == ra.get().get() );
904+
ra.setPosition( new long[]{ -200, -400 } );
905+
assertTrue( 0 == ra.get().get() );
906+
}
907+
856908
static public void main(String[] args) {
857909
new ImgMathTest().test1();
858910
}

0 commit comments

Comments
 (0)