-
Notifications
You must be signed in to change notification settings - Fork 7
/
PositionFieldTransform.java
376 lines (351 loc) · 13.6 KB
/
PositionFieldTransform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2020 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imglib2.realtransform;
import java.util.function.Supplier;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.Localizable;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealLocalizable;
import net.imglib2.RealPositionable;
import net.imglib2.RealRandomAccess;
import net.imglib2.RealRandomAccessible;
import net.imglib2.converter.Converters;
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Localizables;
import net.imglib2.view.Views;
import net.imglib2.view.composite.CompositeIntervalView;
import net.imglib2.view.composite.RealComposite;
/**
* A {@link RealTransform} by continuous coordinate lookup.
*
* @author Stephan Saalfeld <saalfelds@janelia.hhmi.org>
* @author Caleb Hulbert <hulbertc@janelia.hhmi.org>
* @author John Bogovic <bogovicj@janelia.hhmi.org>
*/
public class PositionFieldTransform implements RealTransform
{
protected final RealRandomAccess< ? extends RealLocalizable > access;
protected final int numTargetDimensions;
public PositionFieldTransform( final RealRandomAccess< ? extends RealLocalizable > positionAccesses )
{
access = positionAccesses;
numTargetDimensions = access.get().numDimensions();
}
public PositionFieldTransform( final RealRandomAccessible< ? extends RealLocalizable > positions )
{
access = positions.realRandomAccess();
numTargetDimensions = access.get().numDimensions();
}
/**
*
* @param <T>
* type of the positions
* @param positions
* interleaved target coordinate, this means that the components
* of the target coordinates are in the 0th dimension
*/
public < T extends RealType< T > > PositionFieldTransform( final RandomAccessibleInterval< T > positions )
{
this( convertToComposite( positions ) );
}
/**
*
* @param <T>
* @param positions
* interleaved target coordinate, this means that the components
* of the target coordinates are in the 0th dimension
* @param pixelToPhysical
* a transformation from pixel coordinates to physical
* coordinates
*/
public < T extends RealType< T > > PositionFieldTransform( final RandomAccessibleInterval< T > positions, final AffineGet pixelToPhysical )
{
this( RealViews.affine( convertToComposite( positions ), pixelToPhysical ) );
}
/**
*
* @param <T>
* type of the positions
* @param positions
* interleaved target coordinate, this means that the components
* of the target coordinates are in the 0th dimension
* @param spacing
* the pixel spacing
*/
public < T extends RealType< T > > PositionFieldTransform( final RandomAccessibleInterval< T > positions, final double... spacing )
{
this( RealViews.affine(
convertToComposite( positions ),
spacing.length == 2 ? new Scale2D( spacing ) : spacing.length == 3 ? new Scale3D( spacing ) : new Scale( spacing ) ) );
}
/**
*
* @param <T>
* type of the positions
* @param positions
* interleaved target coordinate, this means that the components
* of the target coordinates are in the 0th dimension
* @param spacing
* the pixel spacing
* @param offset
* the pixel offset
*/
public < T extends RealType< T > > PositionFieldTransform( final RandomAccessibleInterval< T > positions, final double[] spacing, final double[] offset )
{
this( RealViews.affine( convertToComposite( positions ), new ScaleAndTranslation( spacing, offset ) ) );
}
@Override
public int numSourceDimensions()
{
return access.numDimensions();
}
@Override
public int numTargetDimensions()
{
return numTargetDimensions;
}
@Override
public void apply( final double[] source, final double[] target )
{
access.setPosition( source );
access.get().localize( target );
}
@Override
public void apply( final float[] source, final float[] target )
{
access.setPosition( source );
access.get().localize( target );
}
@Override
public void apply( final RealLocalizable source, final RealPositionable target )
{
access.setPosition( source );
access.get().localize( target );
}
@Override
public RealTransform copy()
{
return new PositionFieldTransform( access.copy() );
}
private static < T extends RealType< T > > RealRandomAccessible< ? extends RealLocalizable > convertToComposite(
final RandomAccessibleInterval< T > position )
{
final CompositeIntervalView< T, RealComposite< T > > collapsedFirst =
Views.collapseReal(
Views.moveAxis( position, 0, position.numDimensions() - 1 ) );
return Views.interpolate(
Views.extendBorder( collapsedFirst ),
new NLinearInterpolatorFactory<>() );
}
/**
* Creates a {@link RandomAccessibleInterval} of {@link DoubleType}
* containing the positions of a {@link PositionFieldTransform} for a given
* {@link RealTransform}. This can be useful for saving a transformation as
* a displacement field, but generally should be not used to create a
* {@link PositionFieldTransform}.
* <p>
* Components of the positions are in the 0th dimension, the extents of the
* field are given by the given {@link Interval}. The output interval will
* therefore be of size: <br>
* [ transform.numTargetDimensions(), interval.dimension(0), ...,
* interval.dimension( N-1 )]
* <p>
* The spacing parameter specifies how the discrete coordinates of the
* output position field map to the input source coordinates of the
* transform.
*
* @param transform
* the transform to be converted
* @param interval
* interval
* @param spacing
* the spacing of the grid
* @return the position field
*/
public static RandomAccessibleInterval< DoubleType > createPositionField(
final RealTransform transform,
final Interval interval,
final double[] spacing )
{
return createPositionField( transform, interval, new Scale( spacing ), () -> DoubleType.createVector( transform.numTargetDimensions() ) );
}
/**
* Creates a {@link RandomAccessibleInterval} of {@link DoubleType}
* containing the positions of a {@link PositionFieldTransform} for a given
* {@link RealTransform}. This can be useful for saving a transformation as
* a displacement field, but generally should be not used to create a
* {@link PositionFieldTransform}.
* <p>
* Components of the positions are in the 0th dimension, the extents of the
* field are given by the given {@link Interval}. The output interval will
* therefore be of size: <br>
* [ transform.numTargetDimensions(), interval.dimension(0), ...,
* interval.dimension( N-1 )]
* <p>
* The spacing and offset parameters specify how the discrete coordinates of
* the output position field map to the input source coordinates of the
* transform.
*
* @param transform
* the transform to be converted
* @param interval
* interval
* @param spacing
* the spacing of the grid
* @param offset
* the offset of the output in physical units
* @return the position field
*/
public static RandomAccessibleInterval< DoubleType > createPositionField(
final RealTransform transform,
final Interval interval,
final double[] spacing,
final double[] offset )
{
return createPositionField( transform, interval, spacing, offset, () -> DoubleType.createVector( transform.numTargetDimensions() ) );
}
/**
* Creates a {@link RandomAccessibleInterval} containing the positions of a
* {@link PositionFieldTransform} for a given {@link RealTransform}. This
* can be useful for saving a transformation as a displacement field, but
* generally should be not used to create a {@link PositionFieldTransform}.
* <p>
* Components of the positions are in the 0th dimension, the extents of the
* field are given by the given {@link Interval}. The output interval will
* therefore be of size: <br>
* [ transform.numTargetDimensions(), interval.dimension(0), ...,
* interval.dimension( N-1 )]
* <p>
* The spacing and offset parameters specify how the discrete coordinates of
* the output position field map to the input source coordinates of the
* transform.
* <p>
* The given supplier determines the output type and must provide
* {@link RealComposite}s of size greater than or equal to the transforms
* target dimension. For example,
*
* <pre>
* {@code () -> DoubleType.createVector(transform.numTargetDimensions())}
* </pre>
*
* @param <T>
* the type of the output
* @param transform
* the transform to be converted
* @param interval
* interval
* @param spacing
* the spacing of the grid
* @param offset
* the offset of the output in physical units
* @param supplier
* supplier for intermediate {@link RealComposite} type
* @return the position field
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > createPositionField(
final RealTransform transform,
final Interval interval,
final double[] spacing,
final double[] offset,
final Supplier< RealComposite< T > > supplier )
{
return createPositionField( transform, interval, new ScaleAndTranslation( spacing, offset ), supplier );
}
/**
* Creates a {@link RandomAccessibleInterval} containing the positions of a
* {@link PositionFieldTransform} for a given {@link RealTransform}. This
* can be useful for saving a transformation as a displacement field, but
* generally should be not used to create a {@link PositionFieldTransform}.
* <p>
* Components of the positions are in the 0th dimension, the extents of the
* field are given by the given {@link Interval}. The output interval will
* therefore be of size: <br>
* [ transform.numTargetDimensions(), interval.dimension(0), ...,
* interval.dimension( N-1 )]
* <p>
* The {@link RealTransform} specifies how the discrete coordinates of the
* output position field map to the input source coordinates of the
* transform, i.e. it enables setting the spacing and offset of the
* displacement field grid.
* <p>
* The given supplier determines the output type and must provide
* {@link RealComposite}s of size greater than or equal to the transforms
* target dimension. For example,
*
* <pre>
* {@code () -> DoubleType.createVector(transform.numTargetDimensions())}
* </pre>
*
* @param <T>
* the type of the output
* @param transform
* the transform to be converted
* @param interval
* interval
* @param gridTransform
* transformation from the discrete grid to the transform's
* source coordinates
* @param supplier
* supplier for intermediate {@link RealComposite} type
* @return the position field
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > createPositionField(
final RealTransform transform,
final Interval interval,
final RealTransform gridTransform,
final Supplier< RealComposite< T > > supplier )
{
final RandomAccessibleInterval< Localizable > pixelCoordinates = Localizables.randomAccessibleInterval( interval );
final RandomAccessible< RealComposite< T > > positions = Converters.convert2(
pixelCoordinates,
() -> {
final RealTransform gridCopy = gridTransform.copy();
final RealTransform copy = transform.copy();
return ( x, y ) -> {
gridCopy.apply( x, y );
copy.apply( y, y );
};
},
supplier );
final long[] pfieldDims = new long[ interval.numDimensions() + 1 ];
pfieldDims[ 0 ] = interval.numDimensions();
for ( int i = 0; i < interval.numDimensions(); ++i )
pfieldDims[ i + 1 ] = interval.dimension( i );
return Views.interval( Views.interleave( positions ), new FinalInterval( pfieldDims ) );
}
}