29
29
30
30
package net .imagej .legacy .convert .roi .point ;
31
31
32
- import static org .junit .Assert .assertEquals ;
33
- import static org .junit .Assert .assertFalse ;
34
- import static org .junit .Assert .assertTrue ;
35
-
36
32
import ij .IJ ;
37
33
import ij .ImagePlus ;
38
34
39
35
import java .util .ArrayList ;
36
+ import java .util .Collections ;
40
37
import java .util .Iterator ;
41
38
import java .util .List ;
42
39
43
40
import net .imglib2 .RealLocalizable ;
44
41
import net .imglib2 .RealPoint ;
42
+ import net .imglib2 .RealRandomAccess ;
43
+ import net .imglib2 .RealRandomAccessible ;
44
+ import net .imglib2 .img .array .ArrayImgs ;
45
+ import net .imglib2 .interpolation .randomaccess .NearestNeighborInterpolatorFactory ;
45
46
import net .imglib2 .roi .geom .real .DefaultWritableRealPointCollection ;
46
47
import net .imglib2 .roi .geom .real .WritableRealPointCollection ;
47
- import net .imglib2 .roi .util .RealLocalizableRealPositionable ;
48
- import net .imglib2 .roi .util .RealLocalizableRealPositionableWrapper ;
49
48
49
+ import net .imglib2 .type .numeric .integer .UnsignedByteType ;
50
+ import net .imglib2 .view .Views ;
50
51
import org .junit .Before ;
51
52
import org .junit .Test ;
52
53
54
+ import static org .junit .Assert .*;
55
+ import static org .junit .Assert .assertArrayEquals ;
56
+
53
57
/**
54
58
* Tests {@link RealPointCollectionWrapper}
55
59
*
56
60
* @author Alison Walter
61
+ * @author Gabriel Selzer
57
62
*/
58
63
public class RealPointCollectionWrapperTest {
59
64
60
- private WritableRealPointCollection <RealLocalizableRealPositionable > rpc ;
61
- private RealPointCollectionWrapper wrap ;
65
+ private WritableRealPointCollection <RealPoint > rpc ;
66
+ private RealPointCollectionWrapper < RealPoint > wrap ;
62
67
63
68
@ Before
64
69
public void setup () {
65
- final List <RealLocalizableRealPositionable > pts = new ArrayList <>(3 );
66
- pts .add (new RealLocalizableRealPositionableWrapper <>(new RealPoint (
67
- new double [] { 12 , 3 })));
68
- pts .add (new RealLocalizableRealPositionableWrapper <>(new RealPoint (
69
- new double [] { 0.25 , 6.5 })));
70
- pts .add (new RealLocalizableRealPositionableWrapper <>(new RealPoint (
71
- new double [] { -107 , 33 })));
70
+ final List <RealPoint > pts = new ArrayList <>(3 );
71
+ pts .add (new RealPoint (12 , 3 ));
72
+ pts .add (new RealPoint (0.25 , 6.5 ));
73
+ pts .add (new RealPoint (-107 , 33 ));
72
74
73
75
rpc = new DefaultWritableRealPointCollection <>(pts );
74
- wrap = new RealPointCollectionWrapper (rpc );
76
+ wrap = new RealPointCollectionWrapper <> (rpc , () -> new RealPoint ( 2 ) );
75
77
76
78
// NB: can't remove points without associated image
77
79
final ImagePlus i = IJ .createImage ("Ramp" , "8-bit ramp" , 128 , 128 , 1 );
@@ -138,6 +140,39 @@ public void testRemovePoint() {
138
140
assertTrue (pointsEqual ());
139
141
}
140
142
143
+ /**
144
+ * Ensures that {@link WritableRealPointCollection}s can wrap
145
+ * {@link RealRandomAccess}es (i.e. something that is not a {@link RealPoint})
146
+ */
147
+ @ Test
148
+ public void testRPCOfRandomAccesses () {
149
+ RealRandomAccessible <UnsignedByteType > testImg = Views .interpolate (ArrayImgs
150
+ .unsignedBytes (10 , 10 ), new NearestNeighborInterpolatorFactory <>());
151
+ RealRandomAccess <UnsignedByteType > ra1 = testImg .realRandomAccess ();
152
+ ra1 .setPosition (new int [] { 1 , 1 });
153
+ WritableRealPointCollection <RealRandomAccess <UnsignedByteType >> rpc =
154
+ new DefaultWritableRealPointCollection <>(Collections .singletonList (ra1 ));
155
+
156
+ RealPointCollectionWrapper <RealRandomAccess <UnsignedByteType >> wrapped =
157
+ new RealPointCollectionWrapper <>(rpc , testImg ::realRandomAccess );
158
+
159
+ // add a new point
160
+ wrapped .addPoint (2.0 , 3.0 );
161
+ wrapped .synchronize ();
162
+
163
+ // assert there are now two RRAs
164
+ assertEquals (2 , rpc .size ());
165
+ Iterator <RealRandomAccess <UnsignedByteType >> iw = rpc .points ().iterator ();
166
+ RealRandomAccess <UnsignedByteType > ra = iw .next ();
167
+ assertArrayEquals (new double [] { 1.0 , 1.0 }, ra .positionAsDoubleArray (),
168
+ 1e-6 );
169
+ ra = iw .next ();
170
+ assertArrayEquals (new double [] { 2.0 , 3.0 }, ra .positionAsDoubleArray (),
171
+ 1e-6 );
172
+ assertFalse (iw .hasNext ());
173
+
174
+ }
175
+
141
176
// -- Helper methods --
142
177
143
178
private boolean pointsEqual () {
@@ -146,8 +181,7 @@ private boolean pointsEqual() {
146
181
final float [] yp = wrap .getFloatPolygon ().ypoints ;
147
182
148
183
int count = 0 ;
149
- final Iterator <RealLocalizableRealPositionable > itr = rpc .points ()
150
- .iterator ();
184
+ final Iterator <RealPoint > itr = rpc .points ().iterator ();
151
185
while (itr .hasNext ()) {
152
186
final RealLocalizable pt = itr .next ();
153
187
boolean match = false ;
0 commit comments