Skip to content

Commit fa18945

Browse files
committed
fix/test: DistanceTransform array out of bounds exception
see #108
1 parent d6bb3ae commit fa18945

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/main/java/net/imglib2/algorithm/morphology/distance/DistanceTransform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ private static < T extends RealType< T >, U extends RealType< U > > void transfo
11871187
final double sourceAtPosition = source.get( position ).getRealDouble();
11881188
double s = d.intersect( envelopeIndexAtK, source.get( envelopeIndexAtK ).getRealDouble(), position, sourceAtPosition, dim );
11891189

1190-
for ( double envelopeValueAtK = envelopeIntersectLocation.get( k ).get(); s <= envelopeValueAtK; envelopeValueAtK = envelopeIntersectLocation.get( k ).get() )
1190+
for ( double envelopeValueAtK = envelopeIntersectLocation.get( k ).get(); s <= envelopeValueAtK && k >= 1; envelopeValueAtK = envelopeIntersectLocation.get( k ).get() )
11911191
{
11921192
--k;
11931193
envelopeIndexAtK = lowerBoundDistanceIndex.get( k ).get();
@@ -1785,7 +1785,7 @@ private static < T extends RealType< T >, U extends RealType< U >, L extends Int
17851785
final double sourceAtPosition = source.get( position ).getRealDouble();
17861786
double s = d.intersect( envelopeIndexAtK, source.get( envelopeIndexAtK ).getRealDouble(), position, sourceAtPosition, dim );
17871787

1788-
for ( double envelopeValueAtK = envelopeIntersectLocation.get( k ).get(); s <= envelopeValueAtK; envelopeValueAtK = envelopeIntersectLocation.get( k ).get() )
1788+
for ( double envelopeValueAtK = envelopeIntersectLocation.get( k ).get(); s <= envelopeValueAtK && k >= 1; envelopeValueAtK = envelopeIntersectLocation.get( k ).get() )
17891789
{
17901790
--k;
17911791
envelopeIndexAtK = lowerBoundDistanceIndex.get( k ).get();

src/test/java/net/imglib2/algorithm/morphology/distance/DistanceTransformTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
package net.imglib2.algorithm.morphology.distance;
3636

37+
import static org.junit.Assert.assertArrayEquals;
3738
import static org.junit.Assert.assertTrue;
3839
import static org.junit.Assert.fail;
3940

@@ -501,6 +502,68 @@ public void testLabelPropagation()
501502

502503
}
503504

505+
@Test
506+
public void testWeights()
507+
{
508+
final double tolerance = 1e-9;
509+
final double M = Double.MAX_VALUE;
510+
511+
double[] data = new double[] {
512+
0, M, M, M,
513+
M, 0, M, M
514+
};
515+
ArrayImg<DoubleType, DoubleArray> dists = ArrayImgs.doubles(data, 4, 2);
516+
DistanceTransform.transform(dists, new EuclidianDistanceAnisotropic(0.1, 1.0));
517+
518+
final double[] expected = new double[] {0.0, 0.1, 0.4, 0.9, 0.1, 0.0, 0.1, 0.4 };
519+
assertArrayEquals( expected, dists.getAccessType().getCurrentStorageArray(), tolerance );
520+
}
521+
522+
@Test
523+
public void testWeightsL1()
524+
{
525+
final double tolerance = 1e-9;
526+
final double M = Double.MAX_VALUE;
527+
528+
double[] data = new double[] {
529+
0, M, M, M,
530+
M, 0, M, M
531+
};
532+
ArrayImg<DoubleType, DoubleArray> dists = ArrayImgs.doubles(data, 4, 2);
533+
DistanceTransform.transform(dists, DISTANCE_TYPE.L1, 0.1, 1.0 );
534+
535+
final double[] expected = new double[] {0.0, 0.1, 0.2, 0.3, 0.1, 0.0, 0.1, 0.2 };
536+
assertArrayEquals( expected, dists.getAccessType().getCurrentStorageArray(), tolerance );
537+
}
538+
539+
@Test
540+
public void testLabelPropagationWeights()
541+
{
542+
final long[] labelData = new long[]{
543+
1, 0, 0, 0,
544+
0, 2, 0, 0 };
545+
546+
final long[] expectedYClose = new long[] {
547+
1, 2, 2, 2,
548+
1, 2, 2, 2};
549+
550+
final long[] expectedXClose = new long[] {
551+
1, 1, 1, 1,
552+
2, 2, 2, 2};
553+
554+
double rx = 99.0;
555+
double ry = 0.01;
556+
ArrayImg<LongType, LongArray> labels = ArrayImgs.longs(Arrays.copyOf(labelData, 8), 4, 2);
557+
DistanceTransform.voronoiDistanceTransform(labels, 0, rx, ry);
558+
assertArrayEquals( expectedYClose,labels.getAccessType().getCurrentStorageArray());
559+
560+
rx = 0.01;
561+
ry = 99.0;
562+
ArrayImg<LongType, LongArray> labels2 = ArrayImgs.longs(Arrays.copyOf(labelData, 8), 4, 2);
563+
DistanceTransform.voronoiDistanceTransform(labels2, 0, rx, ry);
564+
assertArrayEquals( expectedXClose, labels2.getAccessType().getCurrentStorageArray());
565+
}
566+
504567
/**
505568
* Creates an label and distances images with the requested number of dimensions (ndims),
506569
* and places nLabels points with non-zero label. Checks that the propagated labels correctly

0 commit comments

Comments
 (0)