|
11 | 11 | * %% |
12 | 12 | * Redistribution and use in source and binary forms, with or without |
13 | 13 | * modification, are permitted provided that the following conditions are met: |
14 | | - * |
| 14 | + * |
15 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
16 | 16 | * this list of conditions and the following disclaimer. |
17 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
18 | 18 | * this list of conditions and the following disclaimer in the documentation |
19 | 19 | * and/or other materials provided with the distribution. |
20 | | - * |
| 20 | + * |
21 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
22 | 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
34 | 34 |
|
35 | 35 | package net.imglib2.algorithm.fill; |
36 | 36 |
|
| 37 | +import org.junit.Assert; |
| 38 | +import org.junit.Test; |
| 39 | + |
37 | 40 | import net.imglib2.Cursor; |
38 | 41 | import net.imglib2.Point; |
39 | 42 | import net.imglib2.algorithm.neighborhood.DiamondShape; |
|
45 | 48 | import net.imglib2.util.Pair; |
46 | 49 | import net.imglib2.view.ExtendedRandomAccessibleInterval; |
47 | 50 | import net.imglib2.view.Views; |
48 | | -import org.junit.Assert; |
49 | | -import org.junit.Test; |
50 | 51 |
|
51 | 52 | /** |
52 | 53 | * @author Philipp Hanslovsky <hanslovskyp@janelia.hhmi.org> |
53 | 54 | * @author Stephan Saalfeld <saalfelds@janelia.hhmi.org> |
54 | 55 | */ |
55 | | -public class FloodFillTest { |
56 | | - |
57 | | - private static final long START_LABEL = 1; |
58 | | - private static final long FILL_LABEL = 2; |
59 | | - private static final int[] N_DIMS = { 1, 2, 3, 4 }; |
60 | | - private static final int SIZE_OF_EACH_DIM = 60; |
61 | | - |
62 | | - |
63 | | - private static < T extends IntegerType < T > > void runTest( |
64 | | - int nDim, |
65 | | - int sizeOfEachDim, |
66 | | - ImgFactory< T > imageFactory, |
67 | | - T t ) |
68 | | - { |
69 | | - long[] dim = new long[nDim]; |
70 | | - long[] c = new long[nDim]; |
71 | | - long r = sizeOfEachDim / 4; |
72 | | - for ( int d = 0; d < nDim; ++d ) { |
73 | | - dim[d] = sizeOfEachDim; |
74 | | - c[d] = sizeOfEachDim / 3; |
75 | | - } |
76 | | - |
77 | | - long divisionLine = r / 3; |
78 | | - |
79 | | - Img<T> img = imageFactory.create(dim, t.copy() ); |
80 | | - Img<T> refImg = imageFactory.create(dim, t.copy()); |
81 | | - |
82 | | - for (Cursor<T> i = img.cursor(), ref = refImg.cursor(); i.hasNext(); ) |
83 | | - { |
84 | | - i.fwd(); |
85 | | - ref.fwd(); |
86 | | - long diffSum = 0; |
87 | | - for ( int d = 0; d < nDim; ++d ) |
88 | | - { |
89 | | - long diff = i.getLongPosition( d ) - c[d]; |
90 | | - diffSum += diff * diff; |
91 | | - |
92 | | - } |
93 | | - |
94 | | - if ( ( diffSum < r * r ) ) { |
95 | | - if ((i.getLongPosition(0) - c[0] < divisionLine)) { |
96 | | - i.get().setInteger(START_LABEL); |
97 | | - ref.get().setInteger( FILL_LABEL ); |
98 | | - } else if (i.getLongPosition(0) - c[0] > divisionLine) { |
99 | | - i.get().setInteger( START_LABEL ); |
100 | | - ref.get().setInteger( START_LABEL ); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - } |
105 | | - |
106 | | - T fillLabel = t.createVariable(); |
107 | | - fillLabel.setInteger( FILL_LABEL ); |
108 | | - |
109 | | - ExtendedRandomAccessibleInterval<T, Img<T>> extendedImg = Views.extendValue(img, fillLabel); |
110 | | - |
111 | | - Filter<Pair<T, T>, Pair<T, T>> filter = new Filter< Pair< T, T >, Pair< T, T > >() { |
112 | | - @Override |
113 | | - public boolean accept(Pair< T, T > p1, Pair< T, T > p2) { |
114 | | - return ( p1.getB().getIntegerLong() != p2.getB().getIntegerLong() ) && |
115 | | - ( p1.getA().getIntegerLong() == p2.getA().getIntegerLong() ); |
116 | | - } |
117 | | - }; |
118 | | - |
119 | | - FloodFill.fill( extendedImg, extendedImg, new Point( c ), fillLabel, new DiamondShape( 1 ), filter ); |
120 | | - |
121 | | - for ( Cursor< T > imgCursor = img.cursor(), refCursor = refImg.cursor(); imgCursor.hasNext(); ) |
122 | | - { |
123 | | - Assert.assertEquals( refCursor.next(), imgCursor.next() ); |
124 | | - } |
125 | | - |
126 | | - |
127 | | - } |
128 | | - |
129 | | - @Test |
130 | | - public void runTests() |
131 | | - { |
132 | | - for ( int nDim : N_DIMS ) |
133 | | - { |
134 | | - runTest( |
135 | | - nDim, |
136 | | - SIZE_OF_EACH_DIM, |
137 | | - new ArrayImgFactory<LongType>(), |
138 | | - new LongType() |
139 | | - ); |
140 | | - } |
141 | | - } |
| 56 | +public class FloodFillTest |
| 57 | +{ |
| 58 | + private static final long START_LABEL = 1; |
| 59 | + |
| 60 | + private static final long FILL_LABEL = 2; |
| 61 | + |
| 62 | + private static final int[] N_DIMS = { 1, 2, 3, 4 }; |
| 63 | + |
| 64 | + private static final int SIZE_OF_EACH_DIM = 60; |
| 65 | + |
| 66 | + private static < T extends IntegerType< T > > void runTest( final int nDim, final int sizeOfEachDim, final ImgFactory< T > imageFactory, final T t ) |
| 67 | + { |
| 68 | + final long[] dim = new long[ nDim ]; |
| 69 | + final long[] c = new long[ nDim ]; |
| 70 | + final long r = sizeOfEachDim / 4; |
| 71 | + for ( int d = 0; d < nDim; ++d ) |
| 72 | + { |
| 73 | + dim[ d ] = sizeOfEachDim; |
| 74 | + c[ d ] = sizeOfEachDim / 3; |
| 75 | + } |
| 76 | + |
| 77 | + final long divisionLine = r / 3; |
| 78 | + |
| 79 | + final Img< T > img = imageFactory.create( dim, t.copy() ); |
| 80 | + final Img< T > refImg = imageFactory.create( dim, t.copy() ); |
| 81 | + |
| 82 | + for ( Cursor< T > i = img.cursor(), ref = refImg.cursor(); i.hasNext(); ) |
| 83 | + { |
| 84 | + i.fwd(); |
| 85 | + ref.fwd(); |
| 86 | + long diffSum = 0; |
| 87 | + for ( int d = 0; d < nDim; ++d ) |
| 88 | + { |
| 89 | + final long diff = i.getLongPosition( d ) - c[ d ]; |
| 90 | + diffSum += diff * diff; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + if ( ( diffSum < r * r ) ) |
| 95 | + { |
| 96 | + if ( ( i.getLongPosition( 0 ) - c[ 0 ] < divisionLine ) ) |
| 97 | + { |
| 98 | + i.get().setInteger( START_LABEL ); |
| 99 | + ref.get().setInteger( FILL_LABEL ); |
| 100 | + } |
| 101 | + else if ( i.getLongPosition( 0 ) - c[ 0 ] > divisionLine ) |
| 102 | + { |
| 103 | + i.get().setInteger( START_LABEL ); |
| 104 | + ref.get().setInteger( START_LABEL ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + final T fillLabel = t.createVariable(); |
| 111 | + fillLabel.setInteger( FILL_LABEL ); |
| 112 | + |
| 113 | + final ExtendedRandomAccessibleInterval< T, Img< T > > extendedImg = Views.extendValue( img, fillLabel ); |
| 114 | + |
| 115 | + final Filter< Pair< T, T >, Pair< T, T > > filter = new Filter< Pair< T, T >, Pair< T, T > >() |
| 116 | + { |
| 117 | + @Override |
| 118 | + public boolean accept( final Pair< T, T > p1, final Pair< T, T > p2 ) |
| 119 | + { |
| 120 | + return ( p1.getB().getIntegerLong() != p2.getB().getIntegerLong() ) && ( p1.getA().getIntegerLong() == p2.getA().getIntegerLong() ); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + FloodFill.fill( extendedImg, extendedImg, new Point( c ), fillLabel, new DiamondShape( 1 ), filter ); |
| 125 | + |
| 126 | + for ( Cursor< T > imgCursor = img.cursor(), refCursor = refImg.cursor(); imgCursor.hasNext(); ) |
| 127 | + { |
| 128 | + Assert.assertEquals( refCursor.next(), imgCursor.next() ); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void runTests() |
| 135 | + { |
| 136 | + for ( final int nDim : N_DIMS ) |
| 137 | + { |
| 138 | + runTest( nDim, SIZE_OF_EACH_DIM, new ArrayImgFactory< LongType >(), new LongType() ); |
| 139 | + } |
| 140 | + } |
142 | 141 |
|
143 | 142 | } |
0 commit comments