Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkTimeGridHelper.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 "mitkTimeGridHelper.h"
14 
15 #include "itkExceptionObject.h"
16 
18 {
19  const auto beginPos = timeGrid.begin();
20  const auto endPos = timeGrid.end();
21  for(mitk::ModelBase::TimeGridType::const_iterator posTime = beginPos; posTime != endPos; ++posTime)
22  {
23  if (posTime != beginPos && *(posTime-1)<*posTime) return false;
24  }
25 
26  return true;
27 };
28 
30 {
31  mitk::ModelBase::ModelResultType result(outputGrid.GetSize());
32  if (! inputSignal.GetSize())
33  {
34  return result;
35  }
36 
37  if (inputSignal.GetSize() != inputGrid.GetSize())
38  {
39  itkGenericExceptionMacro("Input signal and input time grid have not the same size.");
40  }
41 
42  mitk::ModelBase::ModelResultType::ValueType lastValue = inputSignal[0];
43  mitk::ModelBase::TimeGridType::ValueType lastTime = itk::NumericTraits<mitk::ModelBase::TimeGridType::ValueType>::NonpositiveMin();
44 
45  mitk::ModelBase::TimeGridType::const_iterator posITime = inputGrid.begin();
46  mitk::ModelBase::ModelResultType::const_iterator posValue = inputSignal.begin();
47  mitk::ModelBase::ModelResultType::iterator posResult = result.begin();
48 
49  for(mitk::ModelBase::TimeGridType::const_iterator posOTime = outputGrid.begin(); posOTime != outputGrid.end(); ++posResult, ++posOTime)
50  {
51  while(posITime!=inputGrid.end() && *posOTime > *posITime)
52  { //forward in the input grid until the current output point
53  //is between last and the current input point.
54  lastValue = *posValue;
55  lastTime = *posITime;
56  ++posValue;
57  ++posITime;
58  }
59 
60  double weightLast = 1 - (*posOTime - lastTime)/(*posITime - lastTime);
61  double weightNext = 1 - (*posITime - *posOTime)/(*posITime - lastTime);
62 
63  *posResult = weightLast * lastValue + weightNext * (*posValue);
64  }
65 
66  return result;
67 };
68 
70 mitk::GenerateSupersampledTimeGrid(const mitk::ModelBase::TimeGridType& grid, const unsigned int samplingRate)
71 {
72  unsigned int origGridSize = grid.size();
73 
74  mitk::ModelBase::TimeGridType interpolatedTimeGrid(((origGridSize - 1) * samplingRate) + 1);
75 
76  for (unsigned int t = 0; t < origGridSize - 1; ++t)
77  {
78  double delta = (grid[t + 1] - grid[t]) / samplingRate;
79 
80  for (unsigned int i = 0; i < samplingRate; ++i)
81  {
82  interpolatedTimeGrid[(t * samplingRate) + i] = grid[t] + i * delta;
83  }
84  }
85 
86  interpolatedTimeGrid[interpolatedTimeGrid.size() - 1] = grid[grid.size() - 1];
87 
88  return interpolatedTimeGrid;
89 };
MITKMODELFIT_EXPORT bool TimeGridIsMonotonIncreasing(const ModelBase::TimeGridType timeGrid)
itk::Array< double > TimeGridType
Definition: mitkModelBase.h:62
ValueType
Type of the value held by a Value object.
Definition: jsoncpp.h:345
ModelTraitsInterface::ModelResultType ModelResultType
Definition: mitkModelBase.h:55
MITKMODELFIT_EXPORT ModelBase::ModelResultType InterpolateSignalToNewTimeGrid(const ModelBase::ModelResultType &inputSignal, const ModelBase::TimeGridType &inputGrid, const ModelBase::TimeGridType &outputGrid)
MITKMODELFIT_EXPORT ModelBase::TimeGridType GenerateSupersampledTimeGrid(const mitk::ModelBase::TimeGridType &grid, const unsigned int samplingRate)