Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkLabelSet.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 "mitkLabelSet.h"
18 #include <itkCommand.h>
19 
20 mitk::LabelSet::LabelSet() : m_ActiveLabelValue(0), m_Layer(0)
21 {
24 }
25 
26 mitk::LabelSet::~LabelSet()
27 {
28  m_LabelContainer.clear();
29 }
30 
32  : itk::Object(),
33  m_LookupTable(other.GetLookupTable()->Clone()),
34  m_ActiveLabelValue(other.GetActiveLabel()->GetValue()),
35  m_Layer(other.GetLayer())
36 {
37  // clone Labels
38  auto otherIt = other.IteratorConstBegin();
39  for (; otherIt != other.IteratorConstEnd(); ++otherIt)
40  {
41  m_LabelContainer[otherIt->first] = otherIt->second->Clone();
42 
44  command->SetCallbackFunction(this, &LabelSet::OnLabelModified);
45  m_LabelContainer[otherIt->first]->AddObserver(itk::ModifiedEvent(), command);
46  }
47 }
48 
50 {
51  ModifyLabelEvent.Send();
52  Superclass::Modified();
53 }
54 
56 {
57  return m_LabelContainer.end();
58 }
59 
61 {
62  return m_LabelContainer.begin();
63 }
64 
66 {
67  return m_LabelContainer.end();
68 }
69 
71 {
72  return m_LabelContainer.begin();
73 }
74 
76 {
77  return m_LabelContainer.size();
78 }
79 
80 void mitk::LabelSet::SetLayer(unsigned int layer)
81 {
82  m_Layer = layer;
83  Modified();
84 }
85 
87 {
88  m_ActiveLabelValue = pixelValue;
89  ActiveLabelEvent.Send(pixelValue);
90  Modified();
91 }
92 
94 {
95  return m_LabelContainer.count(pixelValue) > 0 ? true : false;
96 }
97 
98 // TODO Parameter as Smartpointer
100 {
101  unsigned int max_size = mitk::Label::MAX_LABEL_VALUE + 1;
102  if (m_LabelContainer.size() >= max_size)
103  return;
104 
105  mitk::Label::Pointer newLabel(label->Clone());
106 
107  // TODO use layer of label parameter
108  newLabel->SetLayer(m_Layer);
109 
110  PixelType pixelValue;
111  if (m_LabelContainer.empty())
112  {
113  pixelValue = newLabel->GetValue();
114  }
115  else
116  {
117  pixelValue = m_LabelContainer.rbegin()->first;
118 
119  if (pixelValue >= newLabel->GetValue() && m_LabelContainer.find(newLabel->GetValue()) != m_LabelContainer.end())
120  {
121  ++pixelValue;
122  newLabel->SetValue(pixelValue);
123  }
124  else
125  {
126  pixelValue = newLabel->GetValue();
127  }
128  }
129 
130  // new map entry
131  m_LabelContainer[pixelValue] = newLabel;
132  UpdateLookupTable(pixelValue);
133 
135  command->SetCallbackFunction(this, &LabelSet::OnLabelModified);
136  newLabel->AddObserver(itk::ModifiedEvent(), command);
137  // newLabel->AddObserver(itk::ModifiedEvent(),command);
138 
139  SetActiveLabel(newLabel->GetValue());
140  AddLabelEvent.Send();
141  Modified();
142 }
143 
144 void mitk::LabelSet::AddLabel(const std::string &name, const mitk::Color &color)
145 {
146  if (m_LabelContainer.size() > 255)
147  return;
148 
150  newLabel->SetName(name);
151  newLabel->SetColor(color);
152  AddLabel(newLabel);
153 }
154 
155 void mitk::LabelSet::RenameLabel(PixelType pixelValue, const std::string &name, const mitk::Color &color)
156 {
157  mitk::Label *label = GetLabel(pixelValue);
158  label->SetName(name);
159  label->SetColor(color);
160 }
161 
162 void mitk::LabelSet::SetLookupTable(mitk::LookupTable *lut)
163 {
164  m_LookupTable = lut;
165  Modified();
166 }
167 
168 void mitk::LabelSet::PrintSelf(std::ostream & /*os*/, itk::Indent /*indent*/) const
169 {
170 }
171 
173 {
174  auto it = m_LabelContainer.rbegin();
175  PixelType nextActivePixelValue = it->first;
176 
177  for (; it != m_LabelContainer.rend(); it++)
178  {
179  if (it->first == pixelValue)
180  {
181  it->second->RemoveAllObservers();
182  m_LabelContainer.erase(pixelValue);
183  break;
184  }
185  nextActivePixelValue = it->first;
186  }
187 
188  if (m_ActiveLabelValue == pixelValue)
189  {
190  if (ExistLabel(nextActivePixelValue))
191  SetActiveLabel(nextActivePixelValue);
192  else
193  SetActiveLabel(m_LabelContainer.rbegin()->first);
194  }
195 
196  RemoveLabelEvent.Send();
197 
198  Modified();
199 }
200 
202 {
203  auto _it = IteratorBegin();
204  for (; _it != IteratorConstEnd();)
205  {
206  RemoveLabelEvent.Send();
207  m_LabelContainer.erase(_it++);
208  }
209  AllLabelsModifiedEvent.Send();
210 }
211 
213 {
214  auto _end = m_LabelContainer.end();
215  auto _it = m_LabelContainer.begin();
216  for (; _it != _end; ++_it)
217  _it->second->SetLocked(value);
218  AllLabelsModifiedEvent.Send();
219  Modified();
220 }
221 
223 {
224  auto _end = m_LabelContainer.end();
225  auto _it = m_LabelContainer.begin();
226  for (; _it != _end; ++_it)
227  {
228  _it->second->SetVisible(value);
229  UpdateLookupTable(_it->first);
230  }
231  AllLabelsModifiedEvent.Send();
232  Modified();
233 }
234 
236 {
237  const mitk::Color &color = GetLabel(pixelValue)->GetColor();
238 
239  double rgba[4];
240  m_LookupTable->GetTableValue(static_cast<int>(pixelValue), rgba);
241  rgba[0] = color.GetRed();
242  rgba[1] = color.GetGreen();
243  rgba[2] = color.GetBlue();
244  if (GetLabel(pixelValue)->GetVisible())
245  rgba[3] = GetLabel(pixelValue)->GetOpacity();
246  else
247  rgba[3] = 0.0;
248  m_LookupTable->SetTableValue(static_cast<int>(pixelValue), rgba);
249 }
250 
252 {
253  if (m_LabelContainer.find(pixelValue) == m_LabelContainer.end())
254  return nullptr;
255  return m_LabelContainer[pixelValue];
256 }
257 
259 {
260  auto it = m_LabelContainer.find(pixelValue);
261  if (it == m_LabelContainer.end())
262  return nullptr;
263  return it->second.GetPointer();
264 }
265 
266 bool mitk::Equal(const mitk::LabelSet &leftHandSide, const mitk::LabelSet &rightHandSide, ScalarType eps, bool verbose)
267 {
268  bool returnValue = true;
269  // LabelSetmembers
270 
271  MITK_INFO(verbose) << "--- LabelSet Equal ---";
272 
273  // m_LookupTable;
274  const mitk::LookupTable *lhsLUT = leftHandSide.GetLookupTable();
275  const mitk::LookupTable *rhsLUT = rightHandSide.GetLookupTable();
276 
277  returnValue = *lhsLUT == *rhsLUT;
278  if (!returnValue)
279  {
280  MITK_INFO(verbose) << "Lookup tabels not equal.";
281  return returnValue;
282  ;
283  }
284 
285  // m_ActiveLabel;
286  returnValue = mitk::Equal(*leftHandSide.GetActiveLabel(), *rightHandSide.GetActiveLabel(), eps, verbose);
287  if (!returnValue)
288  {
289  MITK_INFO(verbose) << "Active label not equal.";
290  return returnValue;
291  ;
292  }
293 
294  // m_Layer;
295  returnValue = leftHandSide.GetLayer() == rightHandSide.GetLayer();
296  if (!returnValue)
297  {
298  MITK_INFO(verbose) << "Layer index not equal.";
299  return returnValue;
300  ;
301  }
302 
303  // container size;
304  returnValue = leftHandSide.GetNumberOfLabels() == rightHandSide.GetNumberOfLabels();
305  if (!returnValue)
306  {
307  MITK_INFO(verbose) << "Number of labels not equal.";
308  return returnValue;
309  ;
310  }
311 
312  // Label container (map)
313 
314  // m_LabelContainer;
315  auto lhsit = leftHandSide.IteratorConstBegin();
316  auto rhsit = rightHandSide.IteratorConstBegin();
317  for (; lhsit != leftHandSide.IteratorConstEnd(); ++lhsit, ++rhsit)
318  {
319  returnValue = rhsit->first == lhsit->first;
320  if (!returnValue)
321  {
322  MITK_INFO(verbose) << "Label in label container not equal.";
323  return returnValue;
324  ;
325  }
326 
327  returnValue = mitk::Equal(*(rhsit->second), *(lhsit->second), eps, verbose);
328  if (!returnValue)
329  {
330  MITK_INFO(verbose) << "Label in label container not equal.";
331  return returnValue;
332  ;
333  }
334  }
335 
336  return returnValue;
337 }
LabelContainerIteratorType IteratorEnd()
Returns a iterator pointing to the end of the container.
itk::SmartPointer< Self > Pointer
Pointer Clone() const
Pointer Clone() const
#define MITK_INFO
Definition: mitkLogMacros.h:22
unsigned int GetNumberOfLabels() const
Label * GetActiveLabel()
Definition: mitkLabelSet.h:174
double ScalarType
void UpdateLookupTable(PixelType pixelValue)
LookupTable::Pointer m_LookupTable
Definition: mitkLabelSet.h:208
void OnLabelModified()
Recall itk::Object::Modified event from a label and send a ModifyLabelEvent.
void SetAllLabelsLocked(bool)
A data structure describing a label.
Definition: mitkLabel.h:35
LabelContainerConstIteratorType IteratorConstEnd() const
Returns a const iterator pointing to the end of the container.
LabelContainerType::iterator LabelContainerIteratorType
Definition: mitkLabelSet.h:47
void AddLabel(mitk::Label *label)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
static Pointer New()
void SetColor(const mitk::Color &)
Definition: mitkLabel.cpp:205
LabelContainerType m_LabelContainer
Definition: mitkLabelSet.h:206
void RemoveAllLabels()
static Pointer New()
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
LabelContainerType::const_iterator LabelContainerConstIteratorType
Definition: mitkLabelSet.h:46
bool ExistLabel(PixelType)
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
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.
void SetActiveLabel(PixelType)
MITKCORE_EXPORT const ScalarType eps
virtual int GetLayer()
LabelContainerConstIteratorType IteratorConstBegin() const
Returns a const iterator poiting to the begining of the container.
mitk::Label::PixelType PixelType
Definition: mitkLabelSet.h:41
LabelContainerIteratorType IteratorBegin()
Returns a iterator poiting to the begining of the container.
void SetLayer(unsigned int)
void RenameLabel(PixelType, const std::string &, const Color &)
Label * GetLabel(PixelType pixelValue)
void RemoveLabel(PixelType)
void SetName(const std::string &name)
Definition: mitkLabel.cpp:148
The LookupTable class mitk wrapper for a vtkLookupTableThis class can be used to color images with a ...
void SetAllLabelsVisible(bool)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.