Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkModelFitParameterValueExtraction.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 
14 #include "mitkModelFitException.h"
15 
17 #include <mitkPixelTypeMultiplex.h>
18 
19 
29 template<typename TPixel>
31  const ::itk::Index<3> index,
32  unsigned int timestep, mitk::ModelTraitsInterface::ParameterValueType &value)
33 {
34  // Read voxel
35  if (image->GetDimension() == 2)
36  {
37  mitk::ImagePixelReadAccessor<TPixel, 2> readAccess(image, image->GetSliceData(0));
38  itk::Index<2> idx;
39  idx[0] = index[0];
40  idx[1] = index[1];
41  value = static_cast<double>(readAccess.GetPixelByIndex(idx));
42  }
43  else if (image->GetDimension() == 3)
44  {
45  mitk::ImagePixelReadAccessor<TPixel, 3> readAccess(image, image->GetVolumeData(0));
46  value = static_cast<double>(readAccess.GetPixelByIndex(index));
47  }
48  else if (image->GetDimension() == 4)
49  {
50  mitk::ImagePixelReadAccessor<TPixel, 3> readAccess(image, image->GetVolumeData(timestep));
51  value = static_cast<double>(readAccess.GetPixelByIndex(index));
52  }
53 
54  MITK_DEBUG << "Voxel [" << index[0] << ", " << index[1] << ", " << index[2] << ", " << timestep
55  << "]: " << value;
56 }
57 
58 
61  const mitk::Point3D& position, unsigned int timestep, bool noThrow)
62 {
63  // Calculate index
64  ::itk::Index<3> index;
66 
67  // check for invalid time step
68  if (geometry.IsNull())
69  {
70  geometry = image->GetTimeGeometry()->GetGeometryForTimeStep(0);
71  }
72 
73  geometry->WorldToIndex(position, index);
74 
75  return ReadVoxel(image, index, timestep, noThrow);
76 }
77 
80 const ::itk::Index<3> &index, unsigned int timestep, bool noThrow)
81 {
82  double result = 0.0;
83 
84  // check for invalid time step
85  if (timestep >= image->GetTimeSteps())
86  {
87  timestep = 0;
88  }
89 
90  try
91  {
92  mitkPixelTypeMultiplex4(ReadVoxelInternal, image->GetPixelType(), image, index, timestep, result);
93  }
94  catch (const std::exception& e)
95  {
96  if (noThrow)
97  {
98  MITK_ERROR << "Exception in ReadVoxel (index: " << index << "; timestep: " << timestep << "). Reason: " << e.what();
99  }
100  else throw;
101  }
102  catch (...)
103  {
104  if (noThrow)
105  {
106  MITK_ERROR << "Unknown exception in ReadVoxel (index: " << index << "; timestep: " << timestep << ").";
107  }
108  else throw;
109  }
110 
111  return result;
112 }
113 
116  const mitk::Point3D& position)
117 {
118  if (!fitInfo)
119  {
120  mitkThrow() << "Cannot extract parameter value map out of model fit instance. Passed model fit instance is a null ptr.";
121  }
122 
123  ParameterValueMapType parameterMap;
124 
125  for (mitk::modelFit::ModelFitInfo::ConstIterType paramIter = fitInfo->GetParameters().begin();
126  paramIter != fitInfo->GetParameters().end();
127  ++paramIter)
128  {
129  const mitk::modelFit::Parameter* param = *paramIter;
130 
132  {
133  parameterMap[param->name] = mitk::ReadVoxel(param->image, position);
134  }
135  }
136 
137  return parameterMap;
138 };
139 
142 const ::itk::Index<3> &index)
143 {
144  if (!fitInfo)
145  {
146  mitkThrow() << "Cannot extract parameter value map out of model fit instance. Passed model fit instance is a null ptr.";
147  }
148 
149  ParameterValueMapType parameterMap;
150 
151  for (mitk::modelFit::ModelFitInfo::ConstIterType paramIter = fitInfo->GetParameters().begin();
152  paramIter != fitInfo->GetParameters().end();
153  ++paramIter)
154  {
155  const mitk::modelFit::Parameter* param = *paramIter;
156 
158  {
159  parameterMap[param->name] = mitk::ReadVoxel(param->image, index);
160  }
161  }
162 
163  return parameterMap;
164 };
165 
168 {
169  if (!pTraitInterface)
170  {
171  mitkThrow() << "Cannot convert parameter values. Passed traits interface is a null ptr.";
172  }
173 
174  const mitk::ModelTraitsInterface::ParameterNamesType paramNameVector =
175  pTraitInterface->GetParameterNames();
176 
177  mitk::ModelTraitsInterface::ParametersType paramArray(paramNameVector.size());
179 
180  for (const auto& name : paramNameVector)
181  {
182  ParameterValueMapType::const_iterator iter = valueMap.find(name);
183 
184  if (iter != valueMap.end())
185  {
186  paramArray[paramIndex] = iter->second;
187  ++paramIndex;
188  }
189  else
190  {
191  mitkThrow() << "Cannot convert parameter values. Parameter name ('" << name << "') is missing in value map.";
192  }
193  }
194 
195  return paramArray;
196 }
MITKMODELFIT_EXPORT ModelTraitsInterface::ParameterValueType ReadVoxel(const mitk::Image *image, const mitk::Point3D &position, unsigned int timestep=0, bool noThrow=true)
Gives locked and index-based read access for a particular image part. The class provides several set-...
virtual ImageDataItemPointer GetSliceData(int s=0, int t=0, int n=0, void *data=nullptr, ImportMemoryManagementType importMemoryManagement=CopyMemory) const
Definition: mitkImage.cpp:243
MITKMODELFIT_EXPORT ModelTraitsInterface::ParametersType ConvertParameterMapToParameterVector(const ParameterValueMapType &valueMap, const ModelTraitsInterface *pTraitInterface)
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:101
#define MITK_ERROR
Definition: mitkLogMacros.h:20
ParametersType::SizeValueType ParametersSizeType
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
void ReadVoxelInternal(const mitk::PixelType, const mitk::Image *image, const ::itk::Index< 3 > index, unsigned int timestep, mitk::ModelTraitsInterface::ParameterValueType &value)
Reads the intensity with the given pixel type from the given image at the given position and time ste...
std::map< ModelTraitsInterface::ParameterNameType, double > ParameterValueMapType
itk::Array< ParameterValueType > ParametersType
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
std::vector< ParameterNameType > ParameterNamesType
Data class that stores all information about a modelfit parameter. Such a parameter is going to be fi...
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:106
virtual ParameterNamesType GetParameterNames() const =0
#define mitkThrow()
virtual ImageDataItemPointer GetVolumeData(int t=0, int n=0, void *data=nullptr, ImportMemoryManagementType importMemoryManagement=CopyMemory) const
Definition: mitkImage.cpp:326
Image class for storing images.
Definition: mitkImage.h:72
#define mitkPixelTypeMultiplex4(function, ptype, param1, param2, param3, param4)
mitk::Image::Pointer image
Data class that stores all information about a modelfit that is relevant to the visualization and sto...
MITKMODELFIT_EXPORT ParameterValueMapType ExtractParameterValueMapFromModelFit(const mitk::modelFit::ModelFitInfo *fitInfo, const mitk::Point3D &position)
virtual BaseGeometry::Pointer GetGeometryForTimeStep(TimeStepType timeStep) const =0
Returns the geometry which corresponds to the given time step.
ParamListType::const_iterator ConstIterType
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
Definition: mitkBaseData.h:355
const ParamListType & GetParameters() const
mitk::Image::ConstPointer image
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51