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