Medical Imaging Interaction Toolkit  2018.4.99-c4b6bb11
Medical Imaging Interaction Toolkit
mitkLabel.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 "mitkLabel.h"
14 
15 #include "itkProcessObject.h"
16 #include "tinyxml.h"
17 #include <itkCommand.h>
18 #include <mitkProperties.h>
20 #include <mitkStringProperty.h>
21 
23 
25 {
26  if (GetProperty("locked") == nullptr)
27  SetLocked(true);
28  if (GetProperty("visible") == nullptr)
29  SetVisible(true);
30  if (GetProperty("opacity") == nullptr)
31  SetOpacity(0.6);
32  if (GetProperty("center.coordinates") == nullptr)
33  {
34  mitk::Point3D pnt;
35  pnt.SetElement(0, 0);
36  pnt.SetElement(1, 0);
37  pnt.SetElement(2, 0);
39  }
40  if (GetProperty("center.index") == nullptr)
41  {
42  mitk::Point3D pnt;
43  pnt.SetElement(0, 0);
44  pnt.SetElement(1, 0);
45  pnt.SetElement(2, 0);
47  }
48  if (GetProperty("color") == nullptr)
49  {
50  mitk::Color col;
51  col.Set(0, 0, 0);
52  SetColor(col);
53  }
54  if (GetProperty("name") == nullptr)
55  SetName("noName!");
56  if (GetProperty("value") == nullptr)
57  SetValue(0);
58  if (GetProperty("layer") == nullptr)
59  SetLayer(0);
60 
62 }
63 
64 mitk::Label::Label(const Label &other) : PropertyList(other)
65 // copyconstructer of property List handles the coping action
66 {
67  auto *map = this->GetMap();
68  auto it = map->begin();
69  auto end = map->end();
70 
71  for (; it != end; ++it)
72  {
73  itk::SimpleMemberCommand<Label>::Pointer command = itk::SimpleMemberCommand<Label>::New();
74  command->SetCallbackFunction(this, &Label::Modified);
75  it->second->AddObserver(itk::ModifiedEvent(), command);
76  }
77 }
78 
80 {
81 }
82 
83 void mitk::Label::SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName, bool fallBackOnDefaultContext)
84 {
85  itk::SimpleMemberCommand<Label>::Pointer command = itk::SimpleMemberCommand<Label>::New();
86  command->SetCallbackFunction(this, &Label::Modified);
87  property->AddObserver(itk::ModifiedEvent(), command);
88 
89  Superclass::SetProperty(propertyKey, property, contextName, fallBackOnDefaultContext);
90 }
91 
92 void mitk::Label::SetLocked(bool locked)
93 {
94  mitk::BoolProperty *property = dynamic_cast<mitk::BoolProperty *>(GetProperty("locked"));
95  if (property != nullptr)
96  // Update Property
97  property->SetValue(locked);
98  else
99  // Create new Property
100  SetBoolProperty("locked", locked);
101 }
102 
104 {
105  bool locked;
106  GetBoolProperty("locked", locked);
107  return locked;
108 }
109 
110 void mitk::Label::SetVisible(bool visible)
111 {
112  mitk::BoolProperty *property = dynamic_cast<mitk::BoolProperty *>(GetProperty("visible"));
113  if (property != nullptr)
114  // Update Property
115  property->SetValue(visible);
116  else
117  // Create new Property
118  SetBoolProperty("visible", visible);
119 }
120 
122 {
123  bool visible;
124  GetBoolProperty("visible", visible);
125  return visible;
126 }
127 
128 void mitk::Label::SetOpacity(float opacity)
129 {
130  mitk::FloatProperty *property = dynamic_cast<mitk::FloatProperty *>(GetProperty("opacity"));
131  if (property != nullptr)
132  // Update Property
133  property->SetValue(opacity);
134  else
135  // Create new Property
136  SetFloatProperty("opacity", opacity);
137 }
138 
140 {
141  float opacity;
142  GetFloatProperty("opacity", opacity);
143  return opacity;
144 }
145 
146 void mitk::Label::SetName(const std::string &name)
147 {
148  SetStringProperty("name", name.c_str());
149 }
150 
151 std::string mitk::Label::GetName() const
152 {
153  std::string name;
154  GetStringProperty("name", name);
155  return name;
156 }
157 
159 {
160  mitk::UShortProperty *property = dynamic_cast<mitk::UShortProperty *>(GetProperty("value"));
161  if (property != nullptr)
162  // Update Property
163  property->SetValue(pixelValue);
164  else
165  // Create new Property
166  SetProperty("value", mitk::UShortProperty::New(pixelValue));
167 }
168 
170 {
171  PixelType pixelValue;
172  mitk::UShortProperty *property = dynamic_cast<UShortProperty *>(GetProperty("value"));
173  assert(property);
174  pixelValue = property->GetValue();
175  return pixelValue;
176 }
177 
178 void mitk::Label::SetLayer(unsigned int layer)
179 {
180  mitk::UIntProperty *property = dynamic_cast<mitk::UIntProperty *>(GetProperty("layer"));
181  if (property != nullptr)
182  // Update Property
183  property->SetValue(layer);
184  else
185  // Create new Property
186  SetProperty("layer", mitk::UIntProperty::New(layer));
187 }
188 
189 unsigned int mitk::Label::GetLayer() const
190 {
191  unsigned int layer;
192  mitk::UIntProperty *prop = dynamic_cast<mitk::UIntProperty *>(GetProperty("layer"));
193  layer = prop->GetValue();
194  return layer;
195 }
196 
198 {
199  mitk::ColorProperty *colorProp = dynamic_cast<mitk::ColorProperty *>(GetProperty("color"));
200  return colorProp->GetColor();
201 }
202 
204 {
205  mitk::ColorProperty *colorProp = dynamic_cast<mitk::ColorProperty *>(GetProperty("color"));
206  if (colorProp != nullptr)
207  // Update Property
208  colorProp->SetColor(_color);
209  else
210  // Create new Property
211  SetProperty("color", mitk::ColorProperty::New(_color));
212 }
213 
215 {
216  mitk::Point3dProperty *property = dynamic_cast<mitk::Point3dProperty *>(GetProperty("center.index"));
217  if (property != nullptr)
218  // Update Property
219  property->SetValue(center);
220  else
221  // Create new Property
222  SetProperty("center.index", mitk::Point3dProperty::New(center));
223 }
224 
226 {
227  mitk::Point3dProperty *property = dynamic_cast<mitk::Point3dProperty *>(GetProperty("center.index"));
228  return property->GetValue();
229 }
230 
232 {
233  mitk::Point3dProperty *property = dynamic_cast<mitk::Point3dProperty *>(GetProperty("center.coordinates"));
234  if (property != nullptr)
235  // Update Property
236  property->SetValue(center);
237  else
238  // Create new Property
239  SetProperty("center.coordinates", mitk::Point3dProperty::New(center));
240 }
241 
243 {
244  mitk::Point3dProperty *property = dynamic_cast<mitk::Point3dProperty *>(GetProperty("center.coordinates"));
245  return property->GetValue();
246 }
247 
248 itk::LightObject::Pointer mitk::Label::InternalClone() const
249 {
250  itk::LightObject::Pointer result(new Self(*this));
251  result->UnRegister();
252  return result;
253 }
254 
255 void mitk::Label::PrintSelf(std::ostream & /*os*/, itk::Indent /*indent*/) const
256 {
257  // todo
258 }
259 
260 bool mitk::Equal(const mitk::Label &leftHandSide, const mitk::Label &rightHandSide, ScalarType /*eps*/, bool verbose)
261 {
262  MITK_INFO(verbose) << "--- Label Equal ---";
263 
264  bool returnValue = true;
265  // have to be replaced until a PropertyList Equal was implemented :
266  // returnValue = mitk::Equal((const mitk::PropertyList &)leftHandSide,(const mitk::PropertyList
267  // &)rightHandSide,eps,verbose);
268 
269  const mitk::PropertyList::PropertyMap *lhsmap = leftHandSide.GetMap();
270  const mitk::PropertyList::PropertyMap *rhsmap = rightHandSide.GetMap();
271 
272  returnValue = lhsmap->size() == rhsmap->size();
273 
274  if (!returnValue)
275  {
276  MITK_INFO(verbose) << "Labels in label container are not equal.";
277  return returnValue;
278  }
279 
280  auto lhsmapIt = lhsmap->begin();
281  auto lhsmapItEnd = lhsmap->end();
282 
283  for (; lhsmapIt != lhsmapItEnd; ++lhsmapIt)
284  {
285  if (rhsmap->find(lhsmapIt->first) == rhsmap->end())
286  {
287  returnValue = false;
288  break;
289  }
290  }
291 
292  if (!returnValue)
293  {
294  MITK_INFO(verbose) << "Labels in label container are not equal.";
295  return returnValue;
296  }
297 
298  return returnValue;
299 }
bool GetFloatProperty(const char *propertyKey, float &floatValue) const
Convenience method to access the value of a FloatProperty.
mitk::Point3D GetCenterOfMassCoordinates() const
Definition: mitkLabel.cpp:242
~Label() override
Definition: mitkLabel.cpp:79
void SetBoolProperty(const char *propertyKey, bool boolValue)
Convenience method to set the value of a BoolProperty.
void SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName="", bool fallBackOnDefaultContext=false) override
Add new or change existent property.
Definition: mitkLabel.cpp:83
#define MITK_INFO
Definition: mitkLogMacros.h:18
void SetColor(const mitk::Color &color)
double ScalarType
static Pointer New()
float GetOpacity() const
Definition: mitkLabel.cpp:139
void SetLocked(bool locked)
Definition: mitkLabel.cpp:92
void SetLayer(unsigned int layer)
Definition: mitkLabel.cpp:178
void SetValue(PixelType pixelValue)
Definition: mitkLabel.cpp:158
mitk::Point3D GetCenterOfMassIndex() const
Definition: mitkLabel.cpp:225
A data structure describing a label.
Definition: mitkLabel.h:31
Key-value list holding instances of BaseProperty.
static Pointer New()
void Modified()
Definition: mitkLabel.h:73
void SetOpacity(float opacity)
Definition: mitkLabel.cpp:128
bool GetVisible() const
Definition: mitkLabel.cpp:121
virtual T GetValue() const
The ColorProperty class RGB color property.
void SetColor(const mitk::Color &)
Definition: mitkLabel.cpp:203
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
bool GetStringProperty(const char *propertyKey, std::string &stringValue) const
Convenience method to access the value of a StringProperty.
void SetVisible(bool visible)
Definition: mitkLabel.cpp:110
static const PixelType MAX_LABEL_VALUE
The maximum value a label can get: Since the value is of type unsigned short MAX_LABEL_VALUE = 65535...
Definition: mitkLabel.h:41
bool verbose(false)
bool GetLocked() const
Definition: mitkLabel.cpp:103
const mitk::Color & GetColor() const
const mitk::Color & GetColor() const
Definition: mitkLabel.cpp:197
void SetCenterOfMassCoordinates(const mitk::Point3D &center)
Definition: mitkLabel.cpp:231
static T max(T x, T y)
Definition: svm.cpp:56
PixelType GetValue() const
Definition: mitkLabel.cpp:169
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
void SetCenterOfMassIndex(const mitk::Point3D &center)
Definition: mitkLabel.cpp:214
unsigned int GetLayer() const
Definition: mitkLabel.cpp:189
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static Pointer New()
mitk::BaseProperty * GetProperty(const std::string &propertyKey) const
Get a property by its name.
unsigned short PixelType
Definition: mitkLabel.h:36
void SetStringProperty(const char *propertyKey, const char *stringValue)
Convenience method to set the value of a StringProperty.
void SetFloatProperty(const char *propertyKey, float floatValue)
Convenience method to set the value of a FloatProperty.
static Pointer New()
bool GetBoolProperty(const char *propertyKey, bool &boolValue) const
Convenience method to access the value of a BoolProperty.
virtual void SetValue(T _arg)
void SetName(const std::string &name)
Definition: mitkLabel.cpp:146
const PropertyMap * GetMap() const
std::string GetName() const
Definition: mitkLabel.cpp:151
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: mitkLabel.cpp:255