Medical Imaging Interaction Toolkit  2018.4.99-e1268d66
Medical Imaging Interaction Toolkit
mitkBaseData.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 "mitkBaseData.h"
14 
15 #include <itkObjectFactoryBase.h>
16 #include <mitkException.h>
17 #include <mitkGeometry3D.h>
19 #include <mitkStringProperty.h>
20 
22  : m_SourceOutputIndexDuplicate(0),
23  m_Initialized(true),
24  m_PropertyList(PropertyList::New()),
25  m_TimeGeometry(ProportionalTimeGeometry::New())
26 {
27 }
28 
30  : itk::DataObject(),
32  Identifiable(),
35  m_PropertyList(other.m_PropertyList->Clone()),
36  m_TimeGeometry(other.m_TimeGeometry->Clone())
37 {
38 }
39 
41 {
42 }
43 
44 void mitk::BaseData::InitializeTimeGeometry(unsigned int timeSteps)
45 {
47  mitk::BaseGeometry::Pointer baseGeo = dynamic_cast<BaseGeometry *>(geo3D.GetPointer());
48  baseGeo->Initialize();
49 
50  // The geometry is propagated automatically to the other items,
51  // if EvenlyTimed is true...
52  // Old timeGeometry->InitializeEvenlyTimed( g3d.GetPointer(), timeSteps );
53 
54  TimeGeometry::Pointer timeGeometry = this->GetTimeGeometry();
55  timeGeometry->Initialize();
56  timeGeometry->Expand(timeSteps);
57  for (TimeStepType step = 0; step < timeSteps; ++step)
58  {
59  timeGeometry->SetTimeStepGeometry(baseGeo.GetPointer(), step);
60  }
61 }
62 
64 {
65  if (this->GetSource())
66  {
67  this->GetSource()->UpdateOutputInformation();
68  }
69  if (m_TimeGeometry.IsNotNull())
70  {
71  m_TimeGeometry->UpdateBoundingBox();
72  }
73 }
74 
76 {
78 
80 
81  return GetTimeGeometry();
82 }
83 
84 void mitk::BaseData::Expand(unsigned int timeSteps)
85 {
86  if (m_TimeGeometry.IsNotNull())
87  {
88  m_TimeGeometry->Expand(timeSteps);
89  }
90  else
91  {
92  this->InitializeTimeGeometry(timeSteps);
93  }
94 }
95 
97 {
99 
101 
102  return GetGeometry(t);
103 }
104 
106 {
108  if (geometry != nullptr)
109  {
110  timeGeometry->Initialize(geometry, 1);
111  }
112  SetTimeGeometry(timeGeometry);
113  return;
114 }
115 
117 {
118  m_TimeGeometry = geometry;
119  this->Modified();
120 }
121 
123 {
124  SetGeometry(static_cast<mitk::BaseGeometry *>(aGeometry3D->Clone().GetPointer()));
125 }
126 
128 {
129  TimeGeometry::Pointer clonedGeometry = geometry->Clone();
130  SetTimeGeometry(clonedGeometry.GetPointer());
131 }
132 
133 void mitk::BaseData::SetClonedGeometry(const BaseGeometry *aGeometry3D, unsigned int time)
134 {
135  if (m_TimeGeometry)
136  {
137  m_TimeGeometry->SetTimeStepGeometry(static_cast<mitk::BaseGeometry *>(aGeometry3D->Clone().GetPointer()), time);
138  }
139 }
140 
141 bool mitk::BaseData::IsEmptyTimeStep(unsigned int) const
142 {
143  return IsInitialized() == false;
144 }
145 
147 {
148  if (IsInitialized() == false)
149  return true;
150  const TimeGeometry *timeGeometry = const_cast<BaseData *>(this)->GetUpdatedTimeGeometry();
151  if (timeGeometry == nullptr)
152  return true;
153  unsigned int timeSteps = timeGeometry->CountTimeSteps();
154  for (unsigned int t = 0; t < timeSteps; ++t)
155  {
156  if (IsEmptyTimeStep(t) == false)
157  return false;
158  }
159  return true;
160 }
161 
163 {
164  return static_cast<mitk::BaseDataSource *>(Superclass::GetSource().GetPointer());
165 }
166 
168 {
169  return m_PropertyList;
170 }
171 
173 {
174  return m_PropertyList->GetProperty(propertyKey);
175 }
176 
177 void mitk::BaseData::SetProperty(const char *propertyKey, BaseProperty *propertyValue)
178 {
179  m_PropertyList->SetProperty(propertyKey, propertyValue);
180 }
181 
183 {
184  m_PropertyList = pList;
185 }
186 
188 {
189  TimeGeometry *timeGeom = GetTimeGeometry();
190 
191  assert(timeGeom != nullptr);
192 
193  TimeStepType steps = timeGeom->CountTimeSteps();
194  for (TimeStepType timestep = 0; timestep < steps; ++timestep)
195  {
196  auto geometry = GetGeometry(timestep);
197  if (geometry != nullptr)
198  {
199  geometry->SetOrigin(origin);
200  }
201  }
202 }
203 
204 unsigned long mitk::BaseData::GetMTime() const
205 {
206  unsigned long time = Superclass::GetMTime();
207  if (m_TimeGeometry.IsNotNull())
208  {
209  if ((time < m_TimeGeometry->GetMTime()))
210  {
211  return m_TimeGeometry->GetMTime();
212  }
213  }
214  return time;
215 }
216 
217 void mitk::BaseData::Graft(const itk::DataObject *)
218 {
219  itkExceptionMacro(<< "Graft not implemented for mitk::BaseData subclass " << this->GetNameOfClass())
220 }
221 
222 void mitk::BaseData::CopyInformation(const itk::DataObject *data)
223 {
224  const auto *bd = dynamic_cast<const Self *>(data);
225  if (bd != nullptr)
226  {
227  m_PropertyList = bd->GetPropertyList()->Clone();
228  if (bd->GetTimeGeometry() != nullptr)
229  {
230  m_TimeGeometry = bd->GetTimeGeometry()->Clone();
231  }
232  }
233  else
234  {
235  // pointer could not be cast back down; this can be the case if your filters input
236  // and output objects differ in type; then you have to write your own GenerateOutputInformation method
237  itkExceptionMacro(<< "mitk::BaseData::CopyInformation() cannot cast " << typeid(data).name() << " to "
238  << typeid(Self *).name());
239  }
240 }
241 
243 {
244  return m_Initialized;
245 }
246 
248 {
249  this->ClearData();
250  this->InitializeEmpty();
251 }
252 
254 {
255  if (m_Initialized)
256  {
257  ReleaseData();
258  m_Initialized = false;
259  }
260 }
261 
263 {
264  // empty by default. override if needed!
265 }
266 
267 void mitk::BaseData::PrintSelf(std::ostream &os, itk::Indent indent) const
268 {
269  os << std::endl;
270  os << indent << " TimeGeometry: ";
271  if (GetTimeGeometry() == nullptr)
272  os << "nullptr" << std::endl;
273  else
274  GetTimeGeometry()->Print(os, indent);
275  // print out all properties
276  PropertyList::Pointer propertyList = this->GetPropertyList();
277  if (propertyList.IsNotNull() && !propertyList->IsEmpty())
278  {
279  // general headline
280  os << "Properties of BaseData:" << std::endl;
281 
282  const PropertyList::PropertyMap *map = propertyList->GetMap();
283  for (auto iter = map->begin(); iter != map->end(); ++iter)
284  {
285  os << " " << (*iter).first << " " << (*iter).second->GetValueAsString() << std::endl;
286  }
287  }
288 }
289 
290 mitk::BaseProperty::ConstPointer mitk::BaseData::GetConstProperty(const std::string &propertyKey, const std::string &contextName, bool fallBackOnDefaultContext) const
291 {
292  if (propertyKey.empty())
293  return nullptr;
294 
295  if (contextName.empty() || fallBackOnDefaultContext)
296  return m_PropertyList->GetProperty(propertyKey);
297 
298  return nullptr;
299 }
300 
301 mitk::BaseProperty * mitk::BaseData::GetNonConstProperty(const std::string &propertyKey, const std::string &contextName, bool fallBackOnDefaultContext)
302 {
303  if (propertyKey.empty())
304  return nullptr;
305 
306  if (contextName.empty() || fallBackOnDefaultContext)
307  return m_PropertyList->GetProperty(propertyKey);
308 
309  return nullptr;
310 }
311 
312 void mitk::BaseData::SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName, bool fallBackOnDefaultContext)
313 {
314  if (propertyKey.empty())
315  mitkThrow() << "Property key is empty.";
316 
317  if (contextName.empty() || fallBackOnDefaultContext)
318  {
319  m_PropertyList->SetProperty(propertyKey, property);
320  return;
321  }
322 
323  mitkThrow() << "Unknown or unsupported non-default property context.";
324 }
325 
326 void mitk::BaseData::RemoveProperty(const std::string &propertyKey, const std::string &contextName, bool fallBackOnDefaultContext)
327 {
328  if (propertyKey.empty())
329  mitkThrow() << "Property key is empty.";
330 
331  if (contextName.empty() || fallBackOnDefaultContext)
332  {
333  m_PropertyList->RemoveProperty(propertyKey);
334  return;
335  }
336 
337  mitkThrow() << "Unknown or unsupported non-default property context.";
338 }
339 
340 std::vector<std::string> mitk::BaseData::GetPropertyKeys(const std::string &/*contextName*/, bool /*includeDefaultContext*/) const
341 {
342  return m_PropertyList->GetPropertyKeys();
343 }
344 
345 std::vector<std::string> mitk::BaseData::GetPropertyContextNames() const
346 {
347  return std::vector<std::string>();
348 }
virtual void SetOrigin(const Point3D &origin)
Convenience method for setting the origin of the BaseGeometry instances of all time steps...
itk::SmartPointer< mitk::BaseDataSource > GetSource() const
Get the process object that generated this data object.
virtual void ClearData()
reset to non-initialized state, release memory
virtual void SetClonedTimeGeometry(const TimeGeometry *geometry)
Set a clone of the provided TimeGeometry as TimeGeometry of the data.
virtual bool IsEmpty() const
Check whether object contains data (at least at one point in time), e.g., a set of points may be empt...
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Base of all data objects.
Definition: mitkBaseData.h:37
std::vector< std::string > GetPropertyKeys(const std::string &contextName="", bool includeDefaultContext=false) const override
Query keys of existing properties.
Base class of all Operation-classes.
Definition: mitkOperation.h:29
std::vector< std::string > GetPropertyContextNames() const override
Query names of existing contexts.
BaseProperty::ConstPointer GetConstProperty(const std::string &propertyKey, const std::string &contextName="", bool fallBackOnDefaultContext=true) const override
Get property by its key.
virtual void SetGeometry(BaseGeometry *aGeometry3D)
Set the BaseGeometry of the data, which will be referenced (not copied!). Assumes the data object has...
virtual void SetTimeGeometry(TimeGeometry *geometry)
Set the TimeGeometry of the data, which will be referenced (not copied!).
Superclass of all classes generating some kind of mitk::BaseData.
Pointer Clone() const
BaseProperty * GetNonConstProperty(const std::string &propertyKey, const std::string &contextName="", bool fallBackOnDefaultContext=true) override
Get property by its key.
Key-value list holding instances of BaseProperty.
static Pointer New()
void SetPropertyList(PropertyList *propertyList)
Set the data&#39;s property list.
abstract class, that can be used by Undo to undo an operation.
virtual bool IsEmptyTimeStep(unsigned int t) const
Check whether object contains data (at a specified time), e.g., a set of points may be empty...
void SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName="", bool fallBackOnDefaultContext=false) override
Add new or change existent property.
void ExecuteOperation(Operation *operation) override
overwrite if the Data can be called by an Interactor (StateMachine).
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
void Graft(const DataObject *) override
void SetRequestedRegionToLargestPossibleRegion() override=0
Set the RequestedRegion to the LargestPossibleRegion.
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
~BaseData() override
#define mitkThrow()
std::vcl_size_t TimeStepType
unsigned long GetMTime() const override
Get the modified time of the last change of the contents this data object or its geometry.
void UpdateOutputInformation() override
Update the information for this BaseData (the geometry in particular) so that it can be used as an ou...
mitk::PropertyList::Pointer GetPropertyList() const
Get the data&#39;s property list.
const mitk::BaseGeometry * GetUpdatedGeometry(int t=0)
Return the BaseGeometry of the data at time t.
mitk::BaseProperty::Pointer GetProperty(const char *propertyKey) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList, and set it to this, respectively;.
virtual void Clear()
Calls ClearData() and InitializeEmpty();.
const mitk::TimeGeometry * GetUpdatedTimeGeometry()
Return the TimeGeometry of the data.
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
virtual void InitializeEmpty()
Pure virtual; Must be used in subclasses to get a data object to a valid state. Should at least creat...
Definition: mitkBaseData.h:397
virtual void SetClonedGeometry(const BaseGeometry *aGeometry3D)
Set a clone of the provided Geometry as Geometry of the data. Assumes the data object has only 1 time...
Pointer Clone() const
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
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
virtual void Expand(unsigned int timeSteps)
Expands the TimeGeometry to a number of TimeSteps.
Base class of identifiable objects.
virtual void InitializeTimeGeometry(unsigned int timeSteps=1)
Initialize the TimeGeometry for a number of time steps. The TimeGeometry is initialized empty and eve...
unsigned int m_SourceOutputIndexDuplicate
Definition: mitkBaseData.h:402
BaseGeometry Describes the geometry of a data object.
void RemoveProperty(const std::string &propertyKey, const std::string &contextName="", bool fallBackOnDefaultContext=false) override
Removes a property. If the property does not exist, nothing will be done.
void CopyInformation(const itk::DataObject *data) override
Copy information from the specified data set.