-
Notifications
You must be signed in to change notification settings - Fork 20
/
itkSliceSeriesSpecialCoordinatesImage.h
596 lines (538 loc) · 21.4 KB
/
itkSliceSeriesSpecialCoordinatesImage.h
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkSliceSeriesSpecialCoordinatesImage_h
#define itkSliceSeriesSpecialCoordinatesImage_h
#include "itkSpecialCoordinatesImage.h"
#include "itkPoint.h"
#include "itkNeighborhoodAccessorFunctor.h"
#include "itkVectorContainer.h"
#include "itkMath.h"
namespace itk
{
/** \class SliceSeriesSpecialCoordinatesImage
*
* \brief An image composed of a series of adjacent slices that are not
* necessarily uniformly spaced.
*
* This is an itk::SpecialCoordinatesImage comprised of a series of N-1 dimension image
* slices. It is assumed that these slices are adjacent to each other without
* overlap or intersection.
*
* The physical location of a pixel Index is determined by first calling
* TransformIndexToPhysicalPoint for dimensions 0 to N-1, then by running the
* result through a Transform. The metadata for the image that defines its
* spatial domain are the SliceImage and the SliceTransform's. It is possible
* for the Slice image to be a specialized type like the
* CurvilinearArraySpecialCoordinatesImage.
*
* \sa SpecialCoordinatesImage
* \sa CurvilinearArraySpecialCoordinatesImage
*
* \ingroup Ultrasound
*
* \ingroup ImageObjects
*/
template <typename TSliceImage,
typename TTransform,
typename TPixel = typename TSliceImage::PixelType,
unsigned int VDimension = TSliceImage::ImageDimension + 1>
class ITK_TEMPLATE_EXPORT SliceSeriesSpecialCoordinatesImage : public SpecialCoordinatesImage<TPixel, VDimension>
{
public:
/** Standard class type alias */
using Self = SliceSeriesSpecialCoordinatesImage;
using Superclass = SpecialCoordinatesImage<TPixel, VDimension>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
using ConstWeakPointer = WeakPointer<const Self>;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(SliceSeriesSpecialCoordinatesImage, SpecialCoordinatesImage);
/** Pixel type alias support. Used to declare pixel type in filters
* or other operations. */
using PixelType = TPixel;
/** Typedef alias for PixelType */
using ValueType = TPixel;
/** Typedef of the image slice. This is usually one less dimension of the
* image.*/
using SliceImageType = TSliceImage;
/** Typedef of the transform used to transform each slice. This could also
* be a base class transform type. */
using TransformType = TTransform;
/** Typedef of the array of transform used to store the per-slice
* transforms. */
using SliceTransformsType = VectorContainer<IdentifierType, typename TransformType::Pointer>;
/** Internal Pixel representation. Used to maintain a uniform API
* with Image Adaptors and allow to keep a particular internal
* representation of data while showing a different external
* representation. */
using InternalPixelType = TPixel;
using IOPixelType = typename Superclass::IOPixelType;
/** Accessor type that convert data between internal and external
* representations. */
using AccessorType = DefaultPixelAccessor<PixelType>;
/** Accessor functor to choose between accessors: DefaultPixelAccessor for
* the Image, and DefaultVectorPixelAccessor for the vector image. The
* functor provides a generic API between the two accessors. */
using AccessorFunctorType = DefaultPixelAccessorFunctor<Self>;
/** Typedef for the functor used to access a neighborhood of pixel
* pointers. */
using NeighborhoodAccessorFunctorType = NeighborhoodAccessorFunctor<Self>;
/** Dimension of the image. This constant is used by functions that are
* templated over image type (as opposed to being templated over pixel type
* and dimension) when they need compile time access to the dimension of
* the image. */
itkStaticConstMacro(ImageDimension, unsigned int, VDimension);
/** Index type alias support. An index is used to access pixel values. */
using IndexType = typename Superclass::IndexType;
using IndexValueType = typename Superclass::IndexValueType;
/** Offset type alias support. An offset is used to access pixel values. */
using OffsetType = typename Superclass::OffsetType;
/** Size type alias support. A size is used to define region bounds. */
using SizeType = typename Superclass::SizeType;
using SizeValueType = typename Superclass::SizeValueType;
/** Container used to store pixels in the image. */
using PixelContainer = ImportImageContainer<SizeValueType, PixelType>;
/** Region type alias support. A region is used to specify a subset of
* an image.
*/
using RegionType = typename Superclass::RegionType;
/** Spacing type alias support. Spacing holds the "fake" size of a
* pixel, making each pixel look like a 1 unit hyper-cube to filters
* that were designed for normal images and that therefore use
* m_Spacing. The spacing is the geometric distance between image
* samples.
*/
using SpacingType = typename Superclass::SpacingType;
/** Origin type alias support. The origin is the "fake" geometric
* coordinates of the index (0,0). Also for use w/ filters designed
* for normal images.
*/
using PointType = typename Superclass::PointType;
/** A pointer to the pixel container. */
using PixelContainerPointer = typename PixelContainer::Pointer;
using PixelContainerConstPointer = typename PixelContainer::ConstPointer;
/** Set/Get the Slice Image. This image is used to find the relative pixel
* location locations within an image. Its size must be the same size as the
* the size in the corresponding dimenions of this image. The pixel values
* in this image are not used. */
itkSetObjectMacro(SliceImage, SliceImageType);
itkGetConstObjectMacro(SliceImage, SliceImageType);
void
SetSliceTransform(IndexValueType sliceIndex, TransformType * transform);
const TransformType *
GetSliceTransform(IndexValueType sliceIndex) const;
/** Graft the data and information from one image to another. This
* is a convenience method to setup a second image with all the meta
* information of another image and use the same pixel
* container. Note that this method is different than just using two
* SmartPointers to the same image since separate DataObjects are
* still maintained. This method is similar to
* ImageSource::GraftOutput(). The implementation in ImageBase
* simply calls CopyInformation() and copies the region ivars.
* The implementation here refers to the superclass' implementation
* and then copies over the pixel container. */
virtual void
Graft(const DataObject * data) override;
/** \brief Get the continuous index from a physical point
*
* Returns true if the resulting index is within the image, false otherwise.
* \sa Transform */
template <typename TCoordRep, typename TIndexRep>
bool
TransformPhysicalPointToContinuousIndex(const Point<TCoordRep, VDimension> & point,
ContinuousIndex<TIndexRep, VDimension> & index) const
{
const RegionType & region = this->GetLargestPossibleRegion();
const unsigned int sliceDimensionIndex = ImageDimension - 1;
IndexValueType lowerIndex = region.GetIndex(sliceDimensionIndex);
IndexValueType upperIndex = lowerIndex + region.GetSize(sliceDimensionIndex) - 1;
IndexValueType nextIndex = lowerIndex;
PointType lowerPoint;
const TransformType * transform = this->GetSliceInverseTransform(lowerIndex);
if (transform == nullptr)
{
itkExceptionMacro("Inverse slice transform not available for index: " << lowerIndex);
}
lowerPoint = transform->TransformPoint(point);
int lowerSign = Math::sgn(lowerPoint[sliceDimensionIndex]);
PointType upperPoint;
transform = this->GetSliceInverseTransform(upperIndex);
if (transform == nullptr)
{
itkExceptionMacro("Inverse slice transform not available for index: " << upperIndex);
}
upperPoint = transform->TransformPoint(point);
int upperSign = Math::sgn(upperPoint[sliceDimensionIndex]);
PointType nextPoint = lowerPoint;
int nextSign = 0;
if (lowerSign == 0)
{
nextSign = 0;
nextPoint = lowerPoint;
}
else if (upperSign == 0)
{
nextSign = 0;
nextPoint = upperPoint;
}
else
{
if (lowerSign == upperSign)
{
// outside the image
return false;
}
// Binary search for the transforms that bounds the slice
while (upperIndex - lowerIndex > 1)
{
nextIndex = lowerIndex + (upperIndex - lowerIndex) / 2;
transform = this->GetSliceInverseTransform(nextIndex);
nextPoint = transform->TransformPoint(point);
nextSign = Math::sgn(nextPoint[sliceDimensionIndex]);
if (nextSign == 0)
{
break;
}
else if (nextSign == lowerSign)
{
lowerIndex = nextIndex;
lowerPoint = nextPoint;
}
else
{
upperIndex = nextIndex;
upperPoint = nextPoint;
}
}
}
if (nextSign != 0)
{
const TCoordRep fraction =
-lowerPoint[sliceDimensionIndex] / (upperPoint[sliceDimensionIndex] - lowerPoint[sliceDimensionIndex]);
nextPoint[sliceDimensionIndex] = lowerIndex + fraction * (upperIndex - lowerIndex);
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
nextPoint[ii] = lowerPoint[ii] + fraction * (upperPoint[ii] - lowerPoint[ii]);
}
}
else
{
nextPoint[sliceDimensionIndex] = nextIndex;
}
typename SliceImageType::PointType slicePoint;
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
slicePoint[ii] = nextPoint[ii];
}
ContinuousIndex<TIndexRep, SliceImageType::ImageDimension> sliceIndex;
this->m_SliceImage->TransformPhysicalPointToContinuousIndex(slicePoint, sliceIndex);
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
index[ii] = sliceIndex[ii];
}
index[sliceDimensionIndex] = nextPoint[sliceDimensionIndex];
// Now, check to see if the index is within allowed bounds
const bool isInside = region.IsInside(index);
return isInside;
}
/** Get the index (discrete) from a physical point.
* Floating point index results are truncated to integers.
* Returns true if the resulting index is within the image, false otherwise
* \sa Transform */
template <typename TCoordRep>
bool
TransformPhysicalPointToIndex(const Point<TCoordRep, VDimension> & point, IndexType & index) const
{
const RegionType & region = this->GetLargestPossibleRegion();
const unsigned int sliceDimensionIndex = ImageDimension - 1;
IndexValueType lowerIndex = region.GetIndex(sliceDimensionIndex);
IndexValueType upperIndex = lowerIndex + region.GetSize(sliceDimensionIndex) - 1;
IndexValueType nextIndex = lowerIndex;
PointType lowerPoint;
const TransformType * transform = this->GetSliceInverseTransform(lowerIndex);
if (transform == nullptr)
{
itkExceptionMacro("Inverse slice transform not available for index: " << lowerIndex);
}
lowerPoint = transform->TransformPoint(point);
int lowerSign = Math::sgn(lowerPoint[sliceDimensionIndex]);
PointType upperPoint;
transform = this->GetSliceInverseTransform(upperIndex);
if (transform == nullptr)
{
itkExceptionMacro("Inverse slice transform not available for index: " << upperIndex);
}
upperPoint = transform->TransformPoint(point);
int upperSign = Math::sgn(upperPoint[sliceDimensionIndex]);
PointType nextPoint = lowerPoint;
int nextSign = 0;
if (lowerSign == 0)
{
nextSign = 0;
nextPoint = lowerPoint;
}
else if (upperSign == 0)
{
nextSign = 0;
nextPoint = upperPoint;
}
else
{
if (lowerSign == upperSign)
{
// outside the image
return false;
}
// Binary search for the transforms that bounds the slice
while (upperIndex - lowerIndex > 1)
{
nextIndex = lowerIndex + (upperIndex - lowerIndex) / 2;
transform = this->GetSliceInverseTransform(nextIndex);
nextPoint = transform->TransformPoint(point);
nextSign = Math::sgn(nextPoint[sliceDimensionIndex]);
if (nextSign == 0)
{
break;
}
else if (nextSign == lowerSign)
{
lowerIndex = nextIndex;
lowerPoint = nextPoint;
}
else
{
upperIndex = nextIndex;
upperPoint = nextPoint;
}
}
}
if (nextSign != 0)
{
const TCoordRep fraction =
-lowerPoint[sliceDimensionIndex] / (upperPoint[sliceDimensionIndex] - lowerPoint[sliceDimensionIndex]);
nextPoint[sliceDimensionIndex] = lowerIndex + fraction * (upperIndex - lowerIndex);
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
nextPoint[ii] = lowerPoint[ii] + fraction * (upperPoint[ii] - lowerPoint[ii]);
}
}
else
{
nextPoint[sliceDimensionIndex] = nextIndex;
}
typename SliceImageType::PointType slicePoint;
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
slicePoint[ii] = nextPoint[ii];
}
typename SliceImageType::IndexType sliceIndex;
this->m_SliceImage->TransformPhysicalPointToIndex(slicePoint, sliceIndex);
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
index[ii] = sliceIndex[ii];
}
index[sliceDimensionIndex] = Math::RoundHalfIntegerUp<IndexValueType>(nextPoint[sliceDimensionIndex]);
// Now, check to see if the index is within allowed bounds
const bool isInside = region.IsInside(index);
return isInside;
}
/** Get a physical point (in the space which
* the origin and spacing information comes from)
* from a continuous index (in the index space)
* \sa Transform */
template <typename TCoordRep, typename TIndexRep>
void
TransformContinuousIndexToPhysicalPoint(const ContinuousIndex<TIndexRep, VDimension> & index,
Point<TCoordRep, VDimension> & point) const
{
point.Fill(0.0);
ContinuousIndex<TIndexRep, SliceImageType::ImageDimension> sliceIndex;
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
sliceIndex[ii] = index[ii];
}
Point<TCoordRep, SliceImageType::ImageDimension> slicePoint;
this->m_SliceImage->TransformContinuousIndexToPhysicalPoint(sliceIndex, slicePoint);
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
point[ii] += slicePoint[ii];
}
using PointType = Point<TCoordRep, VDimension>;
PointType lowerPoint;
const IndexValueType floor = Math::Floor<IndexValueType, TIndexRep>(index[ImageDimension - 1]);
const IndexValueType ceil = Math::Ceil<IndexValueType, TIndexRep>(index[ImageDimension - 1]);
const TransformType * transform = this->GetSliceTransform(floor);
if (transform != nullptr)
{
lowerPoint = transform->TransformPoint(point);
}
else
{
const RegionType & largestRegion = this->GetLargestPossibleRegion();
const IndexType & largestIndex = largestRegion.GetIndex();
if (index[ImageDimension - 1] < largestIndex[ImageDimension - 1])
{
point[ImageDimension - 1] = index[ImageDimension - 1] - largestIndex[ImageDimension - 1];
point = transform->TransformPoint(point);
return;
}
const SizeType & largestSize = largestRegion.GetSize();
if (index[ImageDimension - 1] > largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1)
{
point[ImageDimension - 1] =
index[ImageDimension - 1] - largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1;
point = transform->TransformPoint(point);
}
return;
}
transform = this->GetSliceTransform(ceil);
PointType upperPoint;
if (transform != nullptr)
{
upperPoint = transform->TransformPoint(point);
}
else
{
const RegionType & largestRegion = this->GetLargestPossibleRegion();
const IndexType & largestIndex = largestRegion.GetIndex();
if (index[ImageDimension - 1] < largestIndex[ImageDimension - 1])
{
point[ImageDimension - 1] = index[ImageDimension - 1] - largestIndex[ImageDimension - 1];
point = transform->TransformPoint(point);
return;
}
const SizeType & largestSize = largestRegion.GetSize();
if (index[ImageDimension - 1] > largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1)
{
point[ImageDimension - 1] =
index[ImageDimension - 1] - largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1;
point = transform->TransformPoint(point);
}
return;
}
const TIndexRep fraction = index[ImageDimension - 1] - floor;
for (unsigned int ii = 0; ii < ImageDimension; ++ii)
{
point[ii] = lowerPoint[ii] + fraction * (upperPoint[ii] - lowerPoint[ii]);
}
}
/** Get a physical point (in the space which
* the origin and spacing information comes from)
* from a discrete index (in the index space)
*
* \sa Transform */
template <typename TCoordRep>
void
TransformIndexToPhysicalPoint(const IndexType & index, Point<TCoordRep, VDimension> & point) const
{
point.Fill(0.0);
typename SliceImageType::IndexType sliceIndex;
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
sliceIndex[ii] = index[ii];
}
Point<TCoordRep, SliceImageType::ImageDimension> slicePoint;
this->m_SliceImage->TransformIndexToPhysicalPoint(sliceIndex, slicePoint);
for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
{
point[ii] += slicePoint[ii];
}
const TransformType * transform = this->GetSliceTransform(index[ImageDimension - 1]);
if (transform != nullptr)
{
point = transform->TransformPoint(point);
return;
}
const RegionType & largestRegion = this->GetLargestPossibleRegion();
const IndexType & largestIndex = largestRegion.GetIndex();
if (index[ImageDimension - 1] < largestIndex[ImageDimension - 1])
{
point[ImageDimension - 1] = index[ImageDimension - 1] - largestIndex[ImageDimension - 1];
transform = this->GetSliceTransform(largestIndex[ImageDimension - 1]);
point = transform->TransformPoint(point);
return;
}
const SizeType & largestSize = largestRegion.GetSize();
if (index[ImageDimension - 1] >
static_cast<IndexValueType>(largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1))
{
point[ImageDimension - 1] =
index[ImageDimension - 1] - largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1;
transform = this->GetSliceTransform(largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1);
point = transform->TransformPoint(point);
}
}
template <typename TCoordRep>
void
TransformLocalVectorToPhysicalVector(FixedArray<TCoordRep, VDimension> &) const
{}
template <typename TCoordRep>
void
TransformPhysicalVectorToLocalVector(const FixedArray<TCoordRep, VDimension> &,
FixedArray<TCoordRep, VDimension> &) const
{}
/** Return the Pixel Accessor object */
AccessorType
GetPixelAccessor(void)
{
return AccessorType();
}
/** Return the Pixel Accesor object */
const AccessorType
GetPixelAccessor(void) const
{
return AccessorType();
}
/** Return the NeighborhoodAccessor functor */
NeighborhoodAccessorFunctorType
GetNeighborhoodAccessor()
{
return NeighborhoodAccessorFunctorType();
}
/** Return the NeighborhoodAccessor functor */
const NeighborhoodAccessorFunctorType
GetNeighborhoodAccessor() const
{
return NeighborhoodAccessorFunctorType();
}
virtual void
SetLargestPossibleRegion(const RegionType & region) override;
virtual void
CopyInformation(const DataObject * data) override;
protected:
SliceSeriesSpecialCoordinatesImage();
virtual ~SliceSeriesSpecialCoordinatesImage() {}
virtual void
PrintSelf(std::ostream & os, Indent indent) const override;
const TransformType *
GetSliceInverseTransform(IndexValueType sliceIndex) const;
private:
SliceSeriesSpecialCoordinatesImage(const Self &); // purposely not implemented
void
operator=(const Self &); // purposely not implemented
typename SliceImageType::Pointer m_SliceImage;
typename SliceTransformsType::Pointer m_SliceTransforms;
typename SliceTransformsType::Pointer m_SliceInverseTransforms;
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkSliceSeriesSpecialCoordinatesImage.hxx"
#endif
#endif