Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkContourModelSet.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 #include <algorithm>
13 #include <mitkContourModelSet.h>
14 #include <vtkMath.h>
15 
16 mitk::ContourModelSet::ContourModelSet() : m_Contours(), m_UpdateBoundingBox(true)
17 {
18  this->InitializeEmpty();
19 }
20 
22  : mitk::BaseData(other), m_Contours(other.m_Contours)
23 {
24  this->InitializeTimeGeometry(1);
25 }
26 
28 {
29  this->m_Contours.clear();
30 }
31 
33 {
34  this->InitializeTimeGeometry(1);
35  m_Contours.resize(0);
36 }
37 
39 {
40  this->m_Contours.push_back(&contourModel);
41  m_UpdateBoundingBox = true;
42 }
43 
45 {
46  this->m_Contours.push_back(contourModel);
47  m_UpdateBoundingBox = true;
48 }
49 
51 {
52  if (index >= 0 && static_cast<ContourModelListType::size_type>(index) < this->m_Contours.size())
53  {
54  return this->m_Contours.at(index).GetPointer();
55  }
56  else
57  {
58  return nullptr;
59  }
60 }
61 
63 {
64  return this->m_Contours.empty();
65 }
66 
68 {
69  return &(this->m_Contours);
70 }
71 
73 {
74  auto it = this->m_Contours.begin();
75 
76  auto end = this->m_Contours.end();
77 
78  // search for ContourModel and remove it if exists
79  while (it != end)
80  {
81  if ((*it) == contourModel)
82  {
83  this->m_Contours.erase(it);
84  m_UpdateBoundingBox = true;
85  return true;
86  }
87 
88  it++;
89  }
90 
91  return false;
92 }
93 
95 {
96  if (index >= 0 && static_cast<ContourModelListType::size_type>(index) < this->m_Contours.size())
97  {
98  this->m_Contours.erase(this->m_Contours.begin() + index);
99  m_UpdateBoundingBox = true;
100  return true;
101  }
102  else
103  {
104  return false;
105  }
106 }
107 
109 {
110  this->m_Contours.clear();
111  m_UpdateBoundingBox = true;
112 }
113 
115 {
116  if (this->GetSource())
117  {
118  this->GetSource()->UpdateOutputInformation();
119  }
120 
121  if (this->m_UpdateBoundingBox)
122  {
123  // update the bounds of the geometry according to the stored vertices
124  mitk::ScalarType mitkBounds[6];
125 
126  // calculate the boundingbox at each timestep
127  typedef itk::BoundingBox<unsigned long, 3, ScalarType> BoundingBoxType;
128  typedef BoundingBoxType::PointsContainer PointsContainer;
129 
130  int timesteps = this->GetTimeSteps();
131 
132  // iterate over the timesteps
133  for (int currenTimeStep = 0; currenTimeStep < timesteps; currenTimeStep++)
134  {
135  // only update bounds if the contour was modified
136  if (this->GetMTime() > this->GetGeometry(currenTimeStep)->GetBoundingBox()->GetMTime())
137  {
138  mitkBounds[0] = 0.0;
139  mitkBounds[1] = 0.0;
140  mitkBounds[2] = 0.0;
141  mitkBounds[3] = 0.0;
142  mitkBounds[4] = 0.0;
143  mitkBounds[5] = 0.0;
144 
145  BoundingBoxType::Pointer boundingBox = BoundingBoxType::New();
146 
147  PointsContainer::Pointer points = PointsContainer::New();
148 
149  auto contoursIt = this->Begin();
150  auto contoursEnd = this->End();
151 
152  while (contoursIt != contoursEnd)
153  {
154  auto it = contoursIt->GetPointer()->Begin(currenTimeStep);
155  auto end = contoursIt->GetPointer()->End(currenTimeStep);
156 
157  // fill the boundingbox with the points
158  while (it != end)
159  {
160  Point3D currentP = (*it)->Coordinates;
161  BoundingBoxType::PointType p;
162  p.CastFrom(currentP);
163  points->InsertElement(points->Size(), p);
164 
165  it++;
166  }
167 
168  ++contoursIt;
169  }
170 
171  // construct the new boundingBox
172  boundingBox->SetPoints(points);
173  boundingBox->ComputeBoundingBox();
174  BoundingBoxType::BoundsArrayType tmp = boundingBox->GetBounds();
175  mitkBounds[0] = tmp[0];
176  mitkBounds[1] = tmp[1];
177  mitkBounds[2] = tmp[2];
178  mitkBounds[3] = tmp[3];
179  mitkBounds[4] = tmp[4];
180  mitkBounds[5] = tmp[5];
181 
182  // set boundingBox at current timestep
183  BaseGeometry *geometry3d = this->GetGeometry(currenTimeStep);
184  geometry3d->SetBounds(mitkBounds);
185  }
186  }
187 
188  this->m_UpdateBoundingBox = false;
189  }
190  GetTimeGeometry()->Update();
191 }
itk::SmartPointer< mitk::BaseDataSource > GetSource() const
Get the process object that generated this data object.
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
Base of all data objects.
Definition: mitkBaseData.h:37
void UpdateOutputInformation() override
Update the OutputInformation of a ContourModel object.
double ScalarType
virtual bool RemoveContourModel(mitk::ContourModel *contourModel)
Remove the given ContourModel from the container if exists.
std::deque< mitk::ContourModel::Pointer > ContourModelListType
DataCollection - Class to facilitate loading/accessing structured data.
void InitializeEmpty() override
Pure virtual; Must be used in subclasses to get a data object to a valid state. Should at least creat...
virtual ContourModelSetIterator Begin()
Return an iterator a the front.
ContourModelListType m_Contours
virtual ContourModelSetIterator End()
Return an iterator a the front.
virtual void AddContourModel(mitk::ContourModel &contourModel)
Add a ContourModel to the container.
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
virtual bool RemoveContourModelAt(int index)
Remove a ContourModel at given index within the container if exists.
ContourModelListType * GetContourModelList()
Returns the container of the contours.
itk::BoundingBox< unsigned long, 3, mitk::ScalarType > BoundingBoxType
virtual mitk::ContourModel * GetContourModelAt(int index) const
Returns the ContourModel a given index.
unsigned long GetMTime() const override
Get the modified time of the last change of the contents this data object or its geometry.
void SetBounds(const BoundsArrayType &bounds)
Set the bounding box (in index/unit coordinates)
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
bool IsEmpty() const override
Returns a bool whether the container is empty or not.
void Update()
Updates the geometry.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:138
void Clear() override
Clear the storage container.
virtual void InitializeTimeGeometry(unsigned int timeSteps=1)
Initialize the TimeGeometry for a number of time steps. The TimeGeometry is initialized empty and eve...
BaseGeometry Describes the geometry of a data object.
BoundingBoxType::BoundsArrayType BoundsArrayType