Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkPointSetMappingHelper.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mapRegistration.h"
14 
16 #include "mitkRegistrationHelper.h"
17 
18 
19 #include "mapPointSetMappingTask.h"
20 
21 ::map::core::continuous::Elements<3>::InternalPointSetType::Pointer mitk::PointSetMappingHelper::ConvertPointSetMITKtoMAP(const mitk::PointSet::DataType* mitkSet)
22 {
23  if (! mitkSet) mapDefaultExceptionStaticMacro(<< "Error, cannot convert point set. Passed mitk point set is null.");
24 
25  ::map::core::continuous::Elements<3>::InternalPointSetType::Pointer mapSet = ::map::core::continuous::Elements<3>::InternalPointSetType::New();
26  ::map::core::continuous::Elements<3>::InternalPointSetType::PointsContainer::Pointer mapContainer = ::map::core::continuous::Elements<3>::InternalPointSetType::PointsContainer::New();
27  ::map::core::continuous::Elements<3>::InternalPointSetType::PointDataContainer::Pointer mapDataContainer = ::map::core::continuous::Elements<3>::InternalPointSetType::PointDataContainer::New();
28  mapSet->SetPoints(mapContainer);
29  mapSet->SetPointData(mapDataContainer);
30 
31  unsigned int pointCount = mitkSet->GetNumberOfPoints();
32 
33  for (unsigned int pointId = 0; pointId < pointCount; ++pointId)
34  {
35  mapSet->SetPoint(pointId, mitkSet->GetPoint(pointId));
36 
38  if (mitkSet->GetPointData(pointId,&data))
39  {
40  mapSet->SetPointData(pointId,data.id);
41  }
42  }
43 
44  return mapSet;
45 }
46 
48  mitk::PointSetMappingHelper::map(const ::mitk::PointSet* input, const mitk::PointSetMappingHelper::RegistrationType* registration, int timeStep,
49  bool throwOnMappingError, const ::mitk::PointSet::PointDataType& errorPointValue)
50 {
51  if (!registration)
52  {
53  mitkThrow() << "Cannot map point set. Passed registration wrapper pointer is nullptr.";
54  }
55  if (!input)
56  {
57  mitkThrow() << "Cannot map point set. Passed point set pointer is nullptr.";
58  }
59  if (static_cast<int>(input->GetTimeSteps())<=timeStep && timeStep>=0)
60  {
61  mitkThrow() << "Cannot set point set. Selected time step is larger then mitk point set. MITK time step count: "<<input->GetTimeSteps()<<"; selected time step: "<<timeStep;
62  }
63 
64  ::mitk::PointSet::Pointer result = input->Clone();
65 
66  typedef ::map::core::continuous::Elements<3>::InternalPointSetType MAPPointSetType;
67  typedef ::map::core::Registration<3,3> ConcreteRegistrationType;
68  const ConcreteRegistrationType* castedReg = dynamic_cast<const ConcreteRegistrationType*>(registration);
69  if (!castedReg)
70  {
71  mitkThrow() <<"Moving and/or fixed dimension of the registration is not 3. Cannot map point 3D set.";
72  }
73 
74  typedef ::map::core::PointSetMappingTask<ConcreteRegistrationType, MAPPointSetType, MAPPointSetType> MappingTaskType;
75  MappingTaskType::ErrorPointValueType internalErrorValue = itk::NumericTraits<MappingTaskType::ErrorPointValueType>::NonpositiveMin();
76  MappingTaskType::Pointer spTask = MappingTaskType::New();
77  spTask->setRegistration(castedReg);
78  spTask->setThrowOnMappingError(throwOnMappingError);
79  spTask->setErrorPointValue(internalErrorValue);
80 
81  unsigned int timePos = timeStep;
82  unsigned int timeEndPos = timeStep+1;
83  if (timeStep < 0)
84  {
85  timePos = 0;
86  timeEndPos = input->GetTimeSteps();
87  }
88 
89  while (timePos<timeEndPos)
90  {
91  MAPPointSetType::Pointer inputTempSet = ConvertPointSetMITKtoMAP(input->GetPointSet(timePos));
92  spTask->setInputPointSet(inputTempSet);
93  spTask->execute();
94 
95  MAPPointSetType::Pointer mappedSet = spTask->getResultPointSet();
96 
97  unsigned int pointCount = input->GetSize(timePos);
98 
99  for (unsigned int pointId = 0; pointId < pointCount; ++pointId)
100  {
101  result->SetPoint(pointId, mappedSet->GetPoint(pointId), timePos);
102  bool invalid = true;
103  MAPPointSetType::PixelType mappedData;
104  if (mappedSet->GetPointData(pointId,&mappedData))
105  {
106  invalid = mappedData == internalErrorValue;
107  }
108 
109  if (invalid)
110  {
111  result->GetPointSet(timePos)->GetPointData()->SetElement(pointId,errorPointValue);
112  }
113  else
114  {
115  result->GetPointSet(timePos)->GetPointData()->SetElement(pointId,input->GetPointSet(timePos)->GetPointData()->GetElement(pointId));
116  }
117  }
118 
119  ++timePos;
120  }
121 
122  return result;
123 }
124 
126  mitk::PointSetMappingHelper::map(const ::mitk::PointSet* input, const MITKRegistrationType* registration, int timeStep,
127  bool throwOnMappingError, const ::mitk::PointSet::PointDataType& errorPointValue)
128 {
129  if (!registration)
130  {
131  mitkThrow() << "Cannot map point set. Passed registration wrapper pointer is nullptr.";
132  }
133  if (!registration->GetRegistration())
134  {
135  mitkThrow() << "Cannot map point set. Passed registration wrapper containes no registration.";
136  }
137  if (!input)
138  {
139  mitkThrow() << "Cannot map point set. Passed point set pointer is nullptr.";
140  }
141 
142  ::mitk::PointSet::Pointer result = map(input, registration->GetRegistration(), timeStep, throwOnMappingError, errorPointValue);
143  return result;
144 }
MeshType DataType
Definition: mitkPointSet.h:131
struct for data of a point
Definition: mitkPointSet.h:93
MAPRegistrationWrapper Wrapper class to allow the handling of MatchPoint registration objects as mitk...
MITKMATCHPOINTREGISTRATION_EXPORT ::mitk::PointSet::Pointer map(const ::mitk::PointSet *input, const RegistrationType *registration, int timeStep=-1, bool throwOnMappingError=true, const ::mitk::PointSet::PointDataType &errorPointValue=::mitk::PointSet::PointDataType())
::map::core::RegistrationBase * GetRegistration()
MITKMATCHPOINTREGISTRATION_EXPORT ::map::core::continuous::Elements< 3 >::InternalPointSetType::Pointer ConvertPointSetMITKtoMAP(const mitk::PointSet::DataType *mitkSet)
#define mitkThrow()
::map::core::RegistrationBase RegistrationType
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=nullptr, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)