-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDisplacementFieldTransform.java
383 lines (360 loc) · 14.5 KB
/
DisplacementFieldTransform.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
377
378
379
380
381
382
383
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2024 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.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.position.FunctionRandomAccessible;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.view.Views;
import net.imglib2.view.composite.RealComposite;
/**
* A {@link RealTransform} by continuous offset lookup.
*
* @author Stephan Saalfeld <[email protected]>
* @author Caleb Hulbert <[email protected]>
* @author John Bogovic <[email protected]>
*/
public class DisplacementFieldTransform extends PositionFieldTransform
{
protected final int numTargetDimensions;
public DisplacementFieldTransform( final RealRandomAccess< ? extends RealLocalizable > displacementsAccess )
{
super( displacementsAccess );
this.numTargetDimensions = access.get().numDimensions() < access.numDimensions() ? access.get().numDimensions() : access.numDimensions();
}
public DisplacementFieldTransform( final RealRandomAccessible< ? extends RealLocalizable > displacements )
{
super( displacements );
this.numTargetDimensions = access.get().numDimensions() < access.numDimensions() ? access.get().numDimensions() : access.numDimensions();
}
/**
*
* @param <T>
* type of the displacements
* @param displacements
* interleaved displacement vectors, this means that the
* components of the displacement vectors are in the 0th
* dimension
*/
public < T extends RealType< T > > DisplacementFieldTransform( final RandomAccessibleInterval< T > displacements )
{
super( displacements );
this.numTargetDimensions = access.get().numDimensions() < access.numDimensions() ? access.get().numDimensions() : access.numDimensions();
}
/**
*
* @param <T>
* type of the displacements
* @param displacements
* interleaved displacement vectors, this means that the
* components of the displacement vectors are in the 0th
* dimension
* @param pixelToPhysical
* a transformation from pixel coordinates to physical
* coordinates
*/
public < T extends RealType< T > > DisplacementFieldTransform( final RandomAccessibleInterval< T > displacements, final AffineGet pixelToPhysical )
{
super( displacements, pixelToPhysical );
this.numTargetDimensions = access.get().numDimensions() < access.numDimensions() ? access.get().numDimensions() : access.numDimensions();
}
/**
*
* @param <T>
* type of the displacements
* @param displacements
* interleaved displacement vectors, this means that the
* components of the displacement vectors are in the 0th
* dimension
* @param spacing
* the pixel spacing
*/
public < T extends RealType< T > > DisplacementFieldTransform( final RandomAccessibleInterval< T > displacements, final double... spacing )
{
super( displacements, spacing );
this.numTargetDimensions = access.get().numDimensions() < access.numDimensions() ? access.get().numDimensions() : access.numDimensions();
}
/**
*
* @param <T>
* type of the displacements
* @param displacements
* interleaved displacement vectors, this means that the
* components of the displacement vectors are in the 0th
* dimension
* @param spacing
* the pixel spacing
* @param offset
* the pixel offset
*/
public < T extends RealType< T > > DisplacementFieldTransform( final RandomAccessibleInterval< T > displacements, final double[] spacing, final double[] offset )
{
super( displacements, spacing, offset );
this.numTargetDimensions = access.get().numDimensions() < access.numDimensions() ? access.get().numDimensions() : access.numDimensions();
}
@Override
public int numTargetDimensions()
{
return numTargetDimensions;
}
@Override
public void apply( final double[] source, final double[] target )
{
// TODO replace with setPositionAndGet after new imglib2 release
// TODO N = min (target, numTargetDimsensions) ?
access.setPosition( source );
final RealLocalizable comp = access.get();
for ( int d = 0; d < numTargetDimensions(); d++ )
target[ d ] = comp.getDoublePosition( d ) + source[ d ];
}
@Override
public void apply( final float[] source, final float[] target )
{
// TODO replace with setPositionAndGet after new imglib2 release
access.setPosition( source );
final RealLocalizable comp = access.get();
for ( int d = 0; d < numTargetDimensions(); d++ )
target[ d ] = comp.getFloatPosition( d ) + source[ d ];
}
@Override
public void apply( final RealLocalizable source, final RealPositionable target )
{
// TODO replace with setPositionAndGet after new imglib2 release
access.setPosition( source );
final RealLocalizable comp = access.get();
for ( int d = 0; d < numTargetDimensions(); d++ )
target.setPosition( comp.getDoublePosition( d ) + source.getDoublePosition( d ), d );
}
@Override
public RealTransform copy()
{
return new DisplacementFieldTransform( access.copy() );
}
/**
* Creates a {@link RandomAccessibleInterval} of {@link DoubleType}
* containing the displacements of a {@link DisplacementFieldTransform} 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 DisplacementFieldTransform}.
* <p>
* Components of the displacements 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 displacement 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 displacement field
*/
public static RandomAccessibleInterval< DoubleType > createDisplacementField(
final RealTransform transform,
final Interval interval,
final double[] spacing )
{
return createDisplacementField( transform, interval, new Scale( spacing ), () -> DoubleType.createVector( transform.numTargetDimensions() ) );
}
/**
* Creates a {@link RandomAccessibleInterval} of {@link DoubleType}
* containing the displacements of a {@link DisplacementFieldTransform} 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 DisplacementFieldTransform}.
* <p>
* Components of the displacements 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 displacement 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 displacement field
*/
public static RandomAccessibleInterval< DoubleType > createDisplacementField(
final RealTransform transform,
final Interval interval,
final double[] spacing,
final double[] offset )
{
return createDisplacementField( transform, interval, new ScaleAndTranslation( spacing, offset ), () -> DoubleType.createVector( transform.numTargetDimensions() ) );
}
/**
* Creates a {@link RandomAccessibleInterval} containing the displacements
* of a {@link DisplacementFieldTransform} 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 DisplacementFieldTransform}.
* <p>
* Components of the displacements 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 displacement 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 displacement field
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > createDisplacementField(
final RealTransform transform,
final Interval interval,
final double[] spacing,
final double[] offset,
final Supplier< RealComposite< T > > supplier )
{
return createDisplacementField( transform, interval, new ScaleAndTranslation( spacing, offset ), supplier );
}
/**
* Creates a {@link RandomAccessibleInterval} containing the displacements
* of a {@link DisplacementFieldTransform} 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 DisplacementFieldTransform}.
* <p>
* Components of the displacements 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 displacement 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 displacement field
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > createDisplacementField(
final RealTransform transform,
final Interval interval,
final RealTransform gridTransform,
final Supplier< RealComposite< T > > supplier )
{
final int nd = transform.numTargetDimensions();
final RandomAccessible< RealComposite< T > > transformedGrid = new FunctionRandomAccessible<>(
nd,
() -> {
final RealTransform copy = gridTransform.copy();
return ( x, y ) -> {
copy.apply( x, y );
};
},
supplier );
final RandomAccessible< RealComposite< T > > displacements = Converters.convert2(
transformedGrid,
() -> {
final RealTransform copy = transform.copy();
return ( x, y ) -> {
copy.apply( x, y );
for ( int d = 0; d < nd; d++ )
y.move( -x.getDoublePosition( d ), d );
};
},
supplier );
final long[] dfieldDims = new long[ interval.numDimensions() + 1 ];
dfieldDims[ 0 ] = interval.numDimensions();
for ( int i = 0; i < interval.numDimensions(); ++i )
dfieldDims[ i + 1 ] = interval.dimension( i );
return Views.interval( Views.interleave( displacements ), new FinalInterval( dfieldDims ) );
}
}