ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkSliceSeriesSpecialCoordinatesImage.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkSliceSeriesSpecialCoordinatesImage_h
19#define itkSliceSeriesSpecialCoordinatesImage_h
20
22#include "itkPoint.h"
24#include "itkVectorContainer.h"
25#include "itkMath.h"
26
27namespace itk
28{
52template <typename TSliceImage,
53 typename TTransform,
54 typename TPixel = typename TSliceImage::PixelType,
55 unsigned int VDimension = TSliceImage::ImageDimension + 1>
56class ITK_TEMPLATE_EXPORT SliceSeriesSpecialCoordinatesImage : public SpecialCoordinatesImage<TPixel, VDimension>
57{
58public:
65
67 itkNewMacro(Self);
68
70 itkOverrideGetNameOfClassMacro(SliceSeriesSpecialCoordinatesImage);
71
74 using PixelType = TPixel;
75
77 using ValueType = TPixel;
78
81 using SliceImageType = TSliceImage;
82
85 using TransformType = TTransform;
86
90
95 using InternalPixelType = TPixel;
96
98
102
107
111
116 static constexpr unsigned int ImageDimension = VDimension;
117
121
124
128
131
136
144
150
154
159 itkSetObjectMacro(SliceImage, SliceImageType);
160 itkGetConstObjectMacro(SliceImage, SliceImageType);
161
162 void
164 const TransformType *
166
177 void
178 Graft(const ImageBase<VDimension> * image) override;
179 using Superclass::Graft;
180
185 template <typename TCoordRep, typename TIndexRep>
186 bool
189 {
190 const RegionType & region = this->GetLargestPossibleRegion();
191 const unsigned int sliceDimensionIndex = ImageDimension - 1;
192 IndexValueType lowerIndex = region.GetIndex(sliceDimensionIndex);
193 IndexValueType upperIndex = lowerIndex + region.GetSize(sliceDimensionIndex) - 1;
194 IndexValueType nextIndex = lowerIndex;
195 PointType lowerPoint;
196 const TransformType * transform = this->GetSliceInverseTransform(lowerIndex);
197 if (transform == nullptr)
198 {
199 itkExceptionMacro("Inverse slice transform not available for index: " << lowerIndex);
200 }
201 lowerPoint = transform->TransformPoint(point);
202 int lowerSign = Math::sgn(lowerPoint[sliceDimensionIndex]);
203 PointType upperPoint;
204 transform = this->GetSliceInverseTransform(upperIndex);
205 if (transform == nullptr)
206 {
207 itkExceptionMacro("Inverse slice transform not available for index: " << upperIndex);
208 }
209 upperPoint = transform->TransformPoint(point);
210 int upperSign = Math::sgn(upperPoint[sliceDimensionIndex]);
211 PointType nextPoint = lowerPoint;
212 int nextSign = 0;
213 if (lowerSign == 0)
214 {
215 nextSign = 0;
216 nextPoint = lowerPoint;
217 }
218 else if (upperSign == 0)
219 {
220 nextSign = 0;
221 nextPoint = upperPoint;
222 }
223 else
224 {
225 if (lowerSign == upperSign)
226 {
227 // outside the image
228 return false;
229 }
230
231 // Binary search for the transforms that bounds the slice
232 while (upperIndex - lowerIndex > 1)
233 {
234 nextIndex = lowerIndex + (upperIndex - lowerIndex) / 2;
235 transform = this->GetSliceInverseTransform(nextIndex);
236 nextPoint = transform->TransformPoint(point);
237 nextSign = Math::sgn(nextPoint[sliceDimensionIndex]);
238 if (nextSign == 0)
239 {
240 break;
241 }
242 else if (nextSign == lowerSign)
243 {
244 lowerIndex = nextIndex;
245 lowerPoint = nextPoint;
246 }
247 else
248 {
249 upperIndex = nextIndex;
250 upperPoint = nextPoint;
251 }
252 }
253 }
254
255 if (nextSign != 0)
256 {
257 const TCoordRep fraction =
258 -lowerPoint[sliceDimensionIndex] / (upperPoint[sliceDimensionIndex] - lowerPoint[sliceDimensionIndex]);
259 nextPoint[sliceDimensionIndex] = lowerIndex + fraction * (upperIndex - lowerIndex);
260 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
261 {
262 nextPoint[ii] = lowerPoint[ii] + fraction * (upperPoint[ii] - lowerPoint[ii]);
263 }
264 }
265 else
266 {
267 nextPoint[sliceDimensionIndex] = nextIndex;
268 }
269
270 typename SliceImageType::PointType slicePoint;
271 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
272 {
273 slicePoint[ii] = nextPoint[ii];
274 }
277 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
278 {
279 index[ii] = sliceIndex[ii];
280 }
281 index[sliceDimensionIndex] = nextPoint[sliceDimensionIndex];
282 // Now, check to see if the index is within allowed bounds
283 const bool isInside = region.IsInside(index);
284
285 return isInside;
286 }
287
302 template <typename TIndexRep, typename TCoordRep>
310
315 template <typename TCoordRep>
316 bool
318 {
319 const RegionType & region = this->GetLargestPossibleRegion();
320 const unsigned int sliceDimensionIndex = ImageDimension - 1;
321 IndexValueType lowerIndex = region.GetIndex(sliceDimensionIndex);
322 IndexValueType upperIndex = lowerIndex + region.GetSize(sliceDimensionIndex) - 1;
323 IndexValueType nextIndex = lowerIndex;
324 PointType lowerPoint;
325 const TransformType * transform = this->GetSliceInverseTransform(lowerIndex);
326 if (transform == nullptr)
327 {
328 itkExceptionMacro("Inverse slice transform not available for index: " << lowerIndex);
329 }
330 lowerPoint = transform->TransformPoint(point);
331 int lowerSign = Math::sgn(lowerPoint[sliceDimensionIndex]);
332 PointType upperPoint;
333 transform = this->GetSliceInverseTransform(upperIndex);
334 if (transform == nullptr)
335 {
336 itkExceptionMacro("Inverse slice transform not available for index: " << upperIndex);
337 }
338 upperPoint = transform->TransformPoint(point);
339 int upperSign = Math::sgn(upperPoint[sliceDimensionIndex]);
340 PointType nextPoint = lowerPoint;
341 int nextSign = 0;
342 if (lowerSign == 0)
343 {
344 nextSign = 0;
345 nextPoint = lowerPoint;
346 }
347 else if (upperSign == 0)
348 {
349 nextSign = 0;
350 nextPoint = upperPoint;
351 }
352 else
353 {
354 if (lowerSign == upperSign)
355 {
356 // outside the image
357 return false;
358 }
359
360 // Binary search for the transforms that bounds the slice
361 while (upperIndex - lowerIndex > 1)
362 {
363 nextIndex = lowerIndex + (upperIndex - lowerIndex) / 2;
364 transform = this->GetSliceInverseTransform(nextIndex);
365 nextPoint = transform->TransformPoint(point);
366 nextSign = Math::sgn(nextPoint[sliceDimensionIndex]);
367 if (nextSign == 0)
368 {
369 break;
370 }
371 else if (nextSign == lowerSign)
372 {
373 lowerIndex = nextIndex;
374 lowerPoint = nextPoint;
375 }
376 else
377 {
378 upperIndex = nextIndex;
379 upperPoint = nextPoint;
380 }
381 }
382 }
383
384 if (nextSign != 0)
385 {
386 const TCoordRep fraction =
387 -lowerPoint[sliceDimensionIndex] / (upperPoint[sliceDimensionIndex] - lowerPoint[sliceDimensionIndex]);
388 nextPoint[sliceDimensionIndex] = lowerIndex + fraction * (upperIndex - lowerIndex);
389 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
390 {
391 nextPoint[ii] = lowerPoint[ii] + fraction * (upperPoint[ii] - lowerPoint[ii]);
392 }
393 }
394 else
395 {
396 nextPoint[sliceDimensionIndex] = nextIndex;
397 }
398
399 typename SliceImageType::PointType slicePoint;
400 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
401 {
402 slicePoint[ii] = nextPoint[ii];
403 }
404 typename SliceImageType::IndexType sliceIndex = this->m_SliceImage->TransformPhysicalPointToIndex(slicePoint);
405 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
406 {
407 index[ii] = sliceIndex[ii];
408 }
409 index[sliceDimensionIndex] = Math::RoundHalfIntegerUp<IndexValueType>(nextPoint[sliceDimensionIndex]);
410 // Now, check to see if the index is within allowed bounds
411 const bool isInside = region.IsInside(index);
412
413 return isInside;
414 }
415
431 template <typename TCoordRep>
432 IndexType
434 {
435 IndexType index;
436 TransformPhysicalPointToIndex(point, index);
437 return index;
438 }
439
444 template <typename TCoordRep, typename TIndexRep>
445 void
447 Point<TCoordRep, VDimension> & point) const
448 {
449 point.Fill(0.0);
451 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
452 {
453 sliceIndex[ii] = index[ii];
454 }
456 this->m_SliceImage->TransformContinuousIndexToPhysicalPoint(sliceIndex, slicePoint);
457 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
458 {
459 point[ii] += slicePoint[ii];
460 }
461 using CoordRepPointType = Point<TCoordRep, VDimension>;
462 CoordRepPointType lowerPoint;
465 const TransformType * transform = this->GetSliceTransform(floor);
466 if (transform != nullptr)
467 {
468 lowerPoint = transform->TransformPoint(point);
469 }
470 else
471 {
472 const RegionType & largestRegion = this->GetLargestPossibleRegion();
473 const IndexType & largestIndex = largestRegion.GetIndex();
474 if (index[ImageDimension - 1] < largestIndex[ImageDimension - 1])
475 {
476 point[ImageDimension - 1] = index[ImageDimension - 1] - largestIndex[ImageDimension - 1];
477 return;
478 }
479
480 const SizeType & largestSize = largestRegion.GetSize();
481 if (index[ImageDimension - 1] > largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1)
482 {
483 point[ImageDimension - 1] =
484 index[ImageDimension - 1] - largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1;
485 }
486 return;
487 }
488
489 transform = this->GetSliceTransform(ceil);
490 CoordRepPointType upperPoint;
491 if (transform != nullptr)
492 {
493 upperPoint = transform->TransformPoint(point);
494 }
495 else
496 {
497 const RegionType & largestRegion = this->GetLargestPossibleRegion();
498 const IndexType & largestIndex = largestRegion.GetIndex();
499 if (index[ImageDimension - 1] < largestIndex[ImageDimension - 1])
500 {
501 point[ImageDimension - 1] = index[ImageDimension - 1] - largestIndex[ImageDimension - 1];
502 return;
503 }
504
505 const SizeType & largestSize = largestRegion.GetSize();
506 if (index[ImageDimension - 1] > largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1)
507 {
508 point[ImageDimension - 1] =
509 index[ImageDimension - 1] - largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1;
510 }
511 return;
512 }
513
514 const TIndexRep fraction = index[ImageDimension - 1] - floor;
515 for (unsigned int ii = 0; ii < ImageDimension; ++ii)
516 {
517 point[ii] = lowerPoint[ii] + fraction * (upperPoint[ii] - lowerPoint[ii]);
518 }
519 }
520
525 template <typename TCoordRep, typename TIndexRep>
533
539 template <typename TCoordRep>
540 void
542 {
543 point.Fill(0.0);
544 typename SliceImageType::IndexType sliceIndex;
545 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
546 {
547 sliceIndex[ii] = index[ii];
548 }
550 this->m_SliceImage->TransformIndexToPhysicalPoint(sliceIndex, slicePoint);
551 for (unsigned int ii = 0; ii < SliceImageType::ImageDimension; ++ii)
552 {
553 point[ii] += slicePoint[ii];
554 }
555 const TransformType * transform = this->GetSliceTransform(index[ImageDimension - 1]);
556 if (transform != nullptr)
557 {
558 point = transform->TransformPoint(point);
559 return;
560 }
561 const RegionType & largestRegion = this->GetLargestPossibleRegion();
562 const IndexType & largestIndex = largestRegion.GetIndex();
563 if (index[ImageDimension - 1] < largestIndex[ImageDimension - 1])
564 {
565 point[ImageDimension - 1] = index[ImageDimension - 1] - largestIndex[ImageDimension - 1];
566 transform = this->GetSliceTransform(largestIndex[ImageDimension - 1]);
567 point = transform->TransformPoint(point);
568 return;
569 }
570
571 const SizeType & largestSize = largestRegion.GetSize();
572 if (index[ImageDimension - 1] >
573 static_cast<IndexValueType>(largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1))
574 {
575 point[ImageDimension - 1] =
576 index[ImageDimension - 1] - largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1;
577 transform = this->GetSliceTransform(largestIndex[ImageDimension - 1] + largestSize[ImageDimension - 1] - 1);
578 point = transform->TransformPoint(point);
579 }
580 }
581
587 template <typename TCoordRep>
590 {
592 TransformIndexToPhysicalPoint(index, point);
593 return point;
594 }
595
596 template <typename TCoordRep>
597 void
600
601 template <typename TCoordRep>
602 void
606
608 AccessorType
610 {
611 return AccessorType();
612 }
613
615 const AccessorType
617 {
618 return AccessorType();
619 }
620
622 NeighborhoodAccessorFunctorType
627
629 const NeighborhoodAccessorFunctorType
634
635 virtual void
636 SetLargestPossibleRegion(const RegionType & region) override;
637
638 virtual void
639 CopyInformation(const DataObject * data) override;
640
641protected:
644
645 virtual void
646 PrintSelf(std::ostream & os, Indent indent) const override;
647
648 const TransformType *
650
651private:
652 SliceSeriesSpecialCoordinatesImage(const Self &); // purposely not implemented
653 void
654 operator=(const Self &); // purposely not implemented
655
656 typename SliceImageType::Pointer m_SliceImage;
659};
660} // end namespace itk
661
662#ifndef ITK_MANUAL_INSTANTIATION
663# include "itkSliceSeriesSpecialCoordinatesImage.hxx"
664#endif
665
666#endif
A templated class holding a point in n-Dimensional image space.
Base class for all data objects in ITK.
virtual void Graft(const DataObject *)
Provides a common API for pixel accessors for Image and VectorImage.
Give access to partial aspects a type.
Simulate a standard C array with copy semantics.
void Fill(const ValueType &)
Base class for templated image classes.
typename IndexType::IndexValueType IndexValueType
virtual const RegionType & GetLargestPossibleRegion() const
typename SizeType::SizeValueType SizeValueType
Defines an itk::Image front-end to a standard C-array.
Control indentation during Print() invocation.
Definition itkIndent.h:53
Provides accessor interfaces to Get pixels and is meant to be used on pointers contained within Neigh...
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
void SetSliceTransform(IndexValueType sliceIndex, TransformType *transform)
void TransformIndexToPhysicalPoint(const IndexType &index, Point< TCoordRep, VDimension > &point) const
void TransformPhysicalVectorToLocalVector(const FixedArray< TCoordRep, VDimension > &, FixedArray< TCoordRep, VDimension > &) const
const TransformType * GetSliceInverseTransform(IndexValueType sliceIndex) const
bool TransformPhysicalPointToIndex(const Point< TCoordRep, VDimension > &point, IndexType &index) const
ImportImageContainer< SizeValueType, PixelType > PixelContainer
SpecialCoordinatesImage< TPixel, VDimension > Superclass
typename PixelContainer::ConstPointer PixelContainerConstPointer
virtual void SetLargestPossibleRegion(const RegionType &region) override
void Graft(const ImageBase< VDimension > *image) override
~SliceSeriesSpecialCoordinatesImage() override=default
NeighborhoodAccessorFunctor< Self > NeighborhoodAccessorFunctorType
void TransformLocalVectorToPhysicalVector(FixedArray< TCoordRep, VDimension > &) const
virtual void PrintSelf(std::ostream &os, Indent indent) const override
Point< TCoordRep, VDimension > TransformIndexToPhysicalPoint(const IndexType &index) const
IndexType TransformPhysicalPointToIndex(const Point< TCoordRep, VDimension > &point) const
bool TransformPhysicalPointToContinuousIndex(const Point< TCoordRep, VDimension > &point, ContinuousIndex< TIndexRep, VDimension > &index) const
Get the continuous index from a physical point.
const TransformType * GetSliceTransform(IndexValueType sliceIndex) const
Point< TCoordRep, VDimension > TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VDimension > &index) const
const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
virtual void CopyInformation(const DataObject *data) override
void TransformContinuousIndexToPhysicalPoint(const ContinuousIndex< TIndexRep, VDimension > &index, Point< TCoordRep, VDimension > &point) const
VectorContainer< IdentifierType, typename TransformType::Pointer > SliceTransformsType
ContinuousIndex< TIndexRep, VDimension > TransformPhysicalPointToContinuousIndex(const Point< TCoordRep, VDimension > &point) const
Returns the continuous index from a physical point.
Implements transparent reference counting.
Point< PointValueType, VImageDimension > PointType
Vector< SpacingValueType, VImageDimension > SpacingType
Implements a weak reference to an object.
TInput Ceil(TInput x) template< typename TReturn
TInput RoundHalfIntegerUp(TInput x) template< typename TReturn
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
constexpr int sgn(T x)
Sign of a value: -1, 0, or +1.
Definition itkMath.h:872
Floor(TInput x) template< TReturn
Round towards minus infinity.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
detail::VectorContainer< std::conditional_t< std::is_void_v< T2 >, SizeValueType, T1 >, std::conditional_t< std::is_void_v< T2 >, T1, T2 > > VectorContainer