Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
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 (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 "mitkLabelSet.h"
15 
16 #include <itkCommand.h>
17 
18 mitk::LabelSet::LabelSet() : m_ActiveLabelValue(0), m_Layer(0)
19 {
22 }
23 
25 {
26  m_LabelContainer.clear();
27 }
28 
30  : itk::Object(),
31  m_LookupTable(other.GetLookupTable()->Clone()),
32  m_ActiveLabelValue(other.GetActiveLabel()->GetValue()),
33  m_Layer(other.GetLayer())
34 {
35  // clone Labels
36  auto otherIt = other.IteratorConstBegin();
37  for (; otherIt != other.IteratorConstEnd(); ++otherIt)
38  {
39  m_LabelContainer[otherIt->first] = otherIt->second->Clone();
40 
41  itk::SimpleMemberCommand<LabelSet>::Pointer command = itk::SimpleMemberCommand<LabelSet>::New();
42  command->SetCallbackFunction(this, &LabelSet::OnLabelModified);
43  m_LabelContainer[otherIt->first]->AddObserver(itk::ModifiedEvent(), command);
44  }
45 }
46 
48 {
50  Superclass::Modified();
51 }
52 
54 {
55  return m_LabelContainer.end();
56 }
57 
59 {
60  return m_LabelContainer.begin();
61 }
62 
64 {
65  return m_LabelContainer.end();
66 }
67 
69 {
70  return m_LabelContainer.begin();
71 }
72 
74 {
75  return m_LabelContainer.size();
76 }
77 
78 void mitk::LabelSet::SetLayer(unsigned int layer)
79 {
80  m_Layer = layer;
81  Modified();
82 }
83 
85 {
86  m_ActiveLabelValue = pixelValue;
87  ActiveLabelEvent.Send(pixelValue);
88  Modified();
89 }
90 
92 {
93  return m_LabelContainer.count(pixelValue) > 0 ? true : false;
94 }
95 
96 // TODO Parameter as Smartpointer
98 {
99  unsigned int max_size = mitk::Label::MAX_LABEL_VALUE + 1;
100  if (m_LabelContainer.size() >= max_size)
101  return;
102 
103  mitk::Label::Pointer newLabel(label->Clone());
104 
105  // TODO use layer of label parameter
106  newLabel->SetLayer(m_Layer);
107 
108  PixelType pixelValue;
109  if (m_LabelContainer.empty())
110  {
111  pixelValue = newLabel->GetValue();
112  }
113  else
114  {
115  pixelValue = m_LabelContainer.rbegin()->first;
116 
117  if (pixelValue >= newLabel->GetValue() && m_LabelContainer.find(newLabel->GetValue()) != m_LabelContainer.end())
118  {
119  ++pixelValue;
120  newLabel->SetValue(pixelValue);
121  }
122  else
123  {
124  pixelValue = newLabel->GetValue();
125  }
126  }
127 
128  // new map entry
129  m_LabelContainer[pixelValue] = newLabel;
130  UpdateLookupTable(pixelValue);
131 
132  // add DICOM information of the label
134 
135  itk::SimpleMemberCommand<LabelSet>::Pointer command = itk::SimpleMemberCommand<LabelSet>::New();
136  command->SetCallbackFunction(this, &LabelSet::OnLabelModified);
137  newLabel->AddObserver(itk::ModifiedEvent(), command);
138  // newLabel->AddObserver(itk::ModifiedEvent(),command);
139 
140  SetActiveLabel(newLabel->GetValue());
142  Modified();
143 }
144 
145 void mitk::LabelSet::AddLabel(const std::string &name, const mitk::Color &color)
146 {
147  if (m_LabelContainer.size() > 255)
148  return;
149 
151  newLabel->SetName(name);
152  newLabel->SetColor(color);
153  AddLabel(newLabel);
154 }
155 
156 void mitk::LabelSet::RenameLabel(PixelType pixelValue, const std::string &name, const mitk::Color &color)
157 {
158  mitk::Label *label = GetLabel(pixelValue);
159  label->SetName(name);
160  label->SetColor(color);
161 
162  // change DICOM information of the label
164 }
165 
167 {
168  m_LookupTable = lut;
169  Modified();
170 }
171 
172 void mitk::LabelSet::PrintSelf(std::ostream & /*os*/, itk::Indent /*indent*/) const
173 {
174 }
175 
177 {
178  auto it = m_LabelContainer.rbegin();
179  PixelType nextActivePixelValue = it->first;
180 
181  for (; it != m_LabelContainer.rend(); ++it)
182  {
183  if (it->first == pixelValue)
184  {
185  it->second->RemoveAllObservers();
186  m_LabelContainer.erase(pixelValue);
187  break;
188  }
189  nextActivePixelValue = it->first;
190  }
191 
192  if (m_ActiveLabelValue == pixelValue)
193  {
194  if (ExistLabel(nextActivePixelValue))
195  SetActiveLabel(nextActivePixelValue);
196  else
197  SetActiveLabel(m_LabelContainer.rbegin()->first);
198  }
199 
201 
202  Modified();
203 }
204 
206 {
207  auto _it = IteratorBegin();
208  for (; _it != IteratorConstEnd();)
209  {
211  m_LabelContainer.erase(_it++);
212  }
214 }
215 
217 {
218  auto it = m_LabelContainer.begin();
219 
220  for (; it != m_LabelContainer.end(); ++it)
221  {
222  if (it->first == m_ActiveLabelValue)
223  {
224  // go to next label
225  ++it;
226  if (it == m_LabelContainer.end())
227  {
228  // end of container; next label is first label
229  it = m_LabelContainer.begin();
230  }
231  break; // found the active label; finish loop
232  }
233  }
234 
235  SetActiveLabel(it->first);
236 }
237 
239 {
240  auto _end = m_LabelContainer.end();
241  auto _it = m_LabelContainer.begin();
242  for (; _it != _end; ++_it)
243  _it->second->SetLocked(value);
245  Modified();
246 }
247 
249 {
250  auto _end = m_LabelContainer.end();
251  auto _it = m_LabelContainer.begin();
252  for (; _it != _end; ++_it)
253  {
254  _it->second->SetVisible(value);
255  UpdateLookupTable(_it->first);
256  }
258  Modified();
259 }
260 
262 {
263  const mitk::Color &color = GetLabel(pixelValue)->GetColor();
264 
265  double rgba[4];
266  m_LookupTable->GetTableValue(static_cast<int>(pixelValue), rgba);
267  rgba[0] = color.GetRed();
268  rgba[1] = color.GetGreen();
269  rgba[2] = color.GetBlue();
270  if (GetLabel(pixelValue)->GetVisible())
271  rgba[3] = GetLabel(pixelValue)->GetOpacity();
272  else
273  rgba[3] = 0.0;
274  m_LookupTable->SetTableValue(static_cast<int>(pixelValue), rgba);
275 }
276 
278 {
279  if (m_LabelContainer.find(pixelValue) == m_LabelContainer.end())
280  return nullptr;
281  return m_LabelContainer[pixelValue];
282 }
283 
285 {
286  auto it = m_LabelContainer.find(pixelValue);
287  if (it == m_LabelContainer.end())
288  return nullptr;
289  return it->second.GetPointer();
290 }
291 
292 bool mitk::Equal(const mitk::LabelSet &leftHandSide, const mitk::LabelSet &rightHandSide, ScalarType eps, bool verbose)
293 {
294  bool returnValue = true;
295  // LabelSetmembers
296 
297  MITK_INFO(verbose) << "--- LabelSet Equal ---";
298 
299  // m_LookupTable;
300  const mitk::LookupTable *lhsLUT = leftHandSide.GetLookupTable();
301  const mitk::LookupTable *rhsLUT = rightHandSide.GetLookupTable();
302 
303  returnValue = *lhsLUT == *rhsLUT;
304  if (!returnValue)
305  {
306  MITK_INFO(verbose) << "Lookup tabels not equal.";
307  return returnValue;
308  ;
309  }
310 
311  // m_ActiveLabel;
312  returnValue = mitk::Equal(*leftHandSide.GetActiveLabel(), *rightHandSide.GetActiveLabel(), eps, verbose);
313  if (!returnValue)
314  {
315  MITK_INFO(verbose) << "Active label not equal.";
316  return returnValue;
317  ;
318  }
319 
320  // m_Layer;
321  returnValue = leftHandSide.GetLayer() == rightHandSide.GetLayer();
322  if (!returnValue)
323  {
324  MITK_INFO(verbose) << "Layer index not equal.";
325  return returnValue;
326  ;
327  }
328 
329  // container size;
330  returnValue = leftHandSide.GetNumberOfLabels() == rightHandSide.GetNumberOfLabels();
331  if (!returnValue)
332  {
333  MITK_INFO(verbose) << "Number of labels not equal.";
334  return returnValue;
335  ;
336  }
337 
338  // Label container (map)
339 
340  // m_LabelContainer;
341  auto lhsit = leftHandSide.IteratorConstBegin();
342  auto rhsit = rightHandSide.IteratorConstBegin();
343  for (; lhsit != leftHandSide.IteratorConstEnd(); ++lhsit, ++rhsit)
344  {
345  returnValue = rhsit->first == lhsit->first;
346  if (!returnValue)
347  {
348  MITK_INFO(verbose) << "Label in label container not equal.";
349  return returnValue;
350  ;
351  }
352 
353  returnValue = mitk::Equal(*(rhsit->second), *(lhsit->second), eps, verbose);
354  if (!returnValue)
355  {
356  MITK_INFO(verbose) << "Label in label container not equal.";
357  return returnValue;
358  ;
359  }
360  }
361 
362  return returnValue;
363 }
Message AllLabelsModifiedEvent
AllLabelsModifiedEvent is emitted whenever a new label has been removed from the LabelSet.
Definition: mitkLabelSet.h:101
LabelContainerConstIteratorType IteratorConstBegin() const
Returns a const iterator poiting to the begining of the container.
LabelContainerIteratorType IteratorEnd()
Returns a iterator pointing to the end of the container.
LabelContainerConstIteratorType IteratorConstEnd() const
Returns a const iterator pointing to the end of the container.
#define MITK_INFO
Definition: mitkLogMacros.h:18
Label * GetActiveLabel()
Definition: mitkLabelSet.h:172
double ScalarType
void UpdateLookupTable(PixelType pixelValue)
LookupTable::Pointer m_LookupTable
Definition: mitkLabelSet.h:210
void OnLabelModified()
Recall itk::Object::Modified event from a label and send a ModifyLabelEvent.
void Send(T t)
Definition: mitkMessage.h:602
float GetOpacity() const
Definition: mitkLabel.cpp:139
void SetAllLabelsLocked(bool)
A data structure describing a label.
Definition: mitkLabel.h:31
unsigned int GetNumberOfLabels() const
LabelContainerType::iterator LabelContainerIteratorType
Definition: mitkLabelSet.h:43
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:203
PixelType m_ActiveLabelValue
Definition: mitkLabelSet.h:212
unsigned int m_Layer
Definition: mitkLabelSet.h:214
LabelContainerType m_LabelContainer
Definition: mitkLabelSet.h:208
void RemoveAllLabels()
Message AddLabelEvent
AddLabelEvent is emitted whenever a new label has been added to the LabelSet.
Definition: mitkLabelSet.h:56
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:41
bool verbose(false)
void SetNextActiveLabel()
~LabelSet() override
const mitk::Color & GetColor() const
Definition: mitkLabel.cpp:197
LabelContainerType::const_iterator LabelContainerConstIteratorType
Definition: mitkLabelSet.h:42
bool ExistLabel(PixelType)
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
Message ModifyLabelEvent
ModifyLabelEvent is emitted whenever a label has been modified from the LabelSet. ...
Definition: mitkLabelSet.h:82
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.
Message RemoveLabelEvent
RemoveLabelEvent is emitted whenever a new label has been removed from the LabelSet.
Definition: mitkLabelSet.h:69
void SetActiveLabel(PixelType)
MITKCORE_EXPORT const ScalarType eps
virtual int GetLayer()
mitk::Label::PixelType PixelType
Definition: mitkLabelSet.h:37
LabelContainerIteratorType IteratorBegin()
Returns a iterator poiting to the begining of the container.
void SetLayer(unsigned int)
void RenameLabel(PixelType, const std::string &, const Color &)
Message1< PixelType > ActiveLabelEvent
ActiveLabelEvent is emitted whenever a label has been set as active in the LabelSet.
Definition: mitkLabelSet.h:87
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
Label * GetLabel(PixelType pixelValue)
Pointer Clone() const
void RemoveLabel(PixelType)
void SetLookupTable(LookupTable *lut)
void SetName(const std::string &name)
Definition: mitkLabel.cpp:146
The LookupTable class mitk wrapper for a vtkLookupTableThis class can be used to color images with a ...
void SetAllLabelsVisible(bool)