Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTubeGraphProperty.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 "mitkTubeGraphProperty.h"
18 
19 #include <mitkRenderingManager.h>
20 
22 /*: m_LabelGroups(), m_ActiveTubes(), m_TubeToLabelsMap()*/
23 {
24 }
25 
27  : BaseProperty(other),
28  m_ActiveTubes(other.m_ActiveTubes),
29  m_LabelGroups(other.m_LabelGroups),
30  m_TubeToLabelsMap(other.m_TubeToLabelsMap),
31  m_Annotations(other.m_Annotations)
32 {
33 }
34 
36 {
37  m_ActiveTubes.clear();
38  m_TubeToLabelsMap.clear();
39 
40  for (auto it = m_LabelGroups.begin(); it != m_LabelGroups.end(); it++)
41  delete *it;
42  m_LabelGroups.clear();
43 }
44 
46 {
47  // search for any label settings for the tube
48  if (m_LabelGroups.size() > 0)
49  {
50  for (auto it = m_TubeToLabelsMap.begin(); it != m_TubeToLabelsMap.end(); it++)
51  {
52  if (this->TubeDescriptorsCompare(tube, (*it).first.first))
53  {
54  // At the moment only the first entry is considered
55  LabelGroup *lg = this->GetLabelGroupByName((*it).first.second);
56  LabelGroup::Label *label = this->GetLabelByName(lg, (*it).second);
57  return label->isVisible;
58  }
59  }
60  // If nothing is found, look if the first labelgroup is visible for "undefined" label
61  LabelGroup::Label *label = this->GetLabelByName((*m_LabelGroups.begin()), "Undefined");
62  return label->isVisible;
63  }
64  else
65  return true;
66 }
67 
68 void mitk::TubeGraphProperty::SetTubeActive(const TubeDescriptorType &tube, const bool &active)
69 {
70  // set active
71  if (active)
72  {
73  for (std::vector<TubeDescriptorType>::const_iterator it = m_ActiveTubes.begin(); it != m_ActiveTubes.end(); it++)
74  {
75  if (this->TubeDescriptorsCompare(tube, (*it)))
76  {
77  return;
78  }
79  }
80  // if not found, add it
81  m_ActiveTubes.push_back(tube);
82  this->Modified();
83  }
84  // set deactive
85  else
86  {
87  for (auto it = m_ActiveTubes.begin(); it != m_ActiveTubes.end(); it++)
88  {
89  if (this->TubeDescriptorsCompare(tube, (*it)))
90  { // if found, delete it
91  m_ActiveTubes.erase(it);
92  this->Modified();
93  return;
94  }
95  }
96  }
98  // RenderingManager::GetInstance()->RequestUpdateAll();
99 }
100 
101 void mitk::TubeGraphProperty::SetTubesActive(std::vector<TubeDescriptorType> &tubes)
102 {
103  for (auto it = tubes.begin(); it != tubes.end(); it++)
104  this->SetTubeActive(*it, true);
105 }
106 
108 {
109  for (std::vector<TubeDescriptorType>::const_iterator it = m_ActiveTubes.begin(); it != m_ActiveTubes.end(); it++)
110  {
111  if (this->TubeDescriptorsCompare((*it), tube))
112  return true;
113  }
114  return false;
115 }
116 
117 std::vector<mitk::TubeGraphProperty::TubeDescriptorType> mitk::TubeGraphProperty::GetActiveTubes()
118 {
119  return m_ActiveTubes;
120 }
121 
123 {
124  // if (!m_ActiveTubes.empty())
125  m_ActiveTubes.clear();
126  this->Modified();
127 }
128 
130 {
131  for (auto it = m_Annotations.begin(); it != m_Annotations.end(); it++)
132  {
133  if ((*it)->name == annotation->name)
134  {
135  MITK_INFO << "An Annotation with this name already exists.";
136  return;
137  }
138  }
139  m_Annotations.push_back(annotation);
140 }
142 {
143  for (auto it = m_Annotations.begin(); it != m_Annotations.end(); it++)
144  {
145  if ((*it)->name == annotation)
146  return *it;
147  }
148  return nullptr;
149 }
150 std::vector<mitk::TubeGraphProperty::Annotation *> mitk::TubeGraphProperty::GetAnnotations()
151 {
152  return m_Annotations;
153 }
155 {
156  for (auto it = m_Annotations.begin(); it != m_Annotations.end(); it++)
157  {
158  if ((*it)->name == annotation->name)
159  {
160  m_Annotations.erase(it);
161  return;
162  }
163  }
164 }
165 
167 {
168  Color color;
169  // grey as default color
170  color[0] = 150;
171  color[1] = 150;
172  color[2] = 150;
173 
174  bool isActive(false);
175  bool hasLabel(false);
176  // search the vector with active tubes first. If the tube is active it can not have another color.
177  for (std::vector<TubeDescriptorType>::const_iterator it = m_ActiveTubes.begin(); it != m_ActiveTubes.end(); it++)
178  {
179  if (this->TubeDescriptorsCompare(tube, (*it)))
180  {
181  color[0] = 255;
182  color[1] = 255;
183  color[2] = 0;
184  isActive = true;
185  break;
186  }
187  }
188 
189  if (!isActive)
190  {
191  // So let see which label is activ on this tube and which color is set.
192  if (m_LabelGroups.size() > 0)
193  {
194  for (auto it = m_TubeToLabelsMap.begin(); it != m_TubeToLabelsMap.end(); it++)
195  {
196  if (this->TubeDescriptorsCompare(tube, (*it).first.first))
197  { // At the moment only the first entry is considered
198  LabelGroup *lg = this->GetLabelGroupByName((*it).first.second);
199  LabelGroup::Label *label = this->GetLabelByName(lg, (*it).second);
200  color = label->labelColor;
201  hasLabel = true;
202  }
203  }
204  // If nothing is found, look if the first labelgroup is visible for "undefined" label
205  if (!hasLabel)
206  {
207  LabelGroup::Label *label = this->GetLabelByName(*(m_LabelGroups.begin()), "Undefined");
208  color = label->labelColor;
209  }
210  }
211  }
212 
213  return color;
214 }
215 
217 {
218  bool isUndefined(label->labelName.compare("Undefined") == 0);
219  for (unsigned int i = 0; i < m_ActiveTubes.size(); i++)
220  {
221  bool isInList(false);
222  for (auto it = m_TubeToLabelsMap.begin(); it != m_TubeToLabelsMap.end(); it++)
223  {
224  if ((this->TubeDescriptorsCompare(it->first.first, m_ActiveTubes[i])) &&
225  (labelGroup->labelGroupName.compare(it->first.second) == 0))
226  {
227  // If tube has a label fromlabel group, deleted if "undefined" is clicked.
228  if (isUndefined)
229  {
230  m_TubeToLabelsMap.erase(it);
231  break;
232  }
233  else
234  {
235  it->second = label->labelName;
236  isInList = true;
237  }
238  }
239  }
240  if (!isInList && !isUndefined)
241  {
242  TubeToLabelGroupType tubeToLabelGroup(m_ActiveTubes[i], labelGroup->labelGroupName);
243  m_TubeToLabelsMap.insert(std::pair<TubeToLabelGroupType, std::string>(tubeToLabelGroup, label->labelName));
244  }
245  }
246  this->Modified();
247  m_ActiveTubes.clear();
248 }
249 
250 void mitk::TubeGraphProperty::SetTubesToLabels(std::map<TubeToLabelGroupType, std::string> tubeToLabelMap)
251 {
252  m_TubeToLabelsMap = tubeToLabelMap;
253 }
254 
255 std::map<mitk::TubeGraphProperty::TubeToLabelGroupType, std::string> mitk::TubeGraphProperty::GetTubesToLabels()
256 {
257  return m_TubeToLabelsMap;
258 }
259 
260 void mitk::TubeGraphProperty::AddLabelGroup(LabelGroup *labelGroup, unsigned int position)
261 {
262  // Check if the name is unique
263  for (auto it = m_LabelGroups.begin(); it != m_LabelGroups.end(); it++)
264  {
265  if (labelGroup->labelGroupName.compare((*it)->labelGroupName) == 0)
266  {
267  MITK_ERROR << "The label group must be unique! This name already exists!";
268  return;
269  }
270  }
271 
272  // Add the label group at position, if you can, otherwise put it at the end of the vector
273  if (position < m_LabelGroups.size() - 1)
274  {
275  auto it = m_LabelGroups.begin() + position;
276  m_LabelGroups.insert(it, labelGroup);
277  }
278  else
279  {
280  m_LabelGroups.push_back(labelGroup);
281 
282  // when given position is larger then vector size, the label group will be added at the last position
283  if (position >= m_LabelGroups.size())
284  MITK_INFO << "Position is out of space. So the label group was added on last position: "
285  << m_LabelGroups.size() - 1;
286  }
287 }
288 
290 {
291  // find labelGroup in vector
292  auto foundElement = std::find(m_LabelGroups.begin(), m_LabelGroups.end(), labelGroup);
293  unsigned int pos = foundElement - m_LabelGroups.begin();
294 
295  // found it? delete it!
296  if (pos < m_LabelGroups.size())
297  {
298  // delete every assignment to a tube
299  for (auto it = m_TubeToLabelsMap.begin(); it != m_TubeToLabelsMap.end(); it++)
300  {
301  if (labelGroup->labelGroupName.compare((*it).first.second) == 0)
302  m_TubeToLabelsMap.erase(it);
303  }
304 
305  m_LabelGroups.erase(foundElement);
306  }
307  else
308  {
309  MITK_ERROR << "Could not find the label group!";
310  return;
311  }
312 }
313 
315 {
316  // LabelGroup? Check if it is a label in property class
317  if (label)
318  {
319  label->labelColor = color;
320  this->Modified();
321  }
322 }
323 
325 {
326  // LabelGroup? Check if it is a label in property class
327  if (label)
328  {
329  label->isVisible = isVisible;
330  this->Modified();
331  }
332 }
333 
334 void mitk::TubeGraphProperty::RenameLabel(LabelGroup *labelGroup, LabelGroup::Label *label, std::string newName)
335 {
336  // LabelGroup? Check if it is a label in property class
337  if (label)
338  {
339  // rename the label in the assignement vector for tubes
340  for (auto it = m_TubeToLabelsMap.begin(); it != m_TubeToLabelsMap.end(); it++)
341  {
342  // Label group fit?
343  if (labelGroup->labelGroupName.compare((*it).first.second) == 0)
344  // label fit?
345  if (label->labelName.compare((*it).second) == 0)
346  // rename it
347  (*it).second = newName;
348  }
349 
350  // rename the label in label group
351  label->labelName = newName;
352  }
353 }
354 
356 {
357  return m_LabelGroups;
358 }
359 
361 {
362  return m_LabelGroups.size();
363 }
364 
366 {
367  unsigned int pos = std::find(m_LabelGroups.begin(), m_LabelGroups.end(), labelGroup) - m_LabelGroups.begin();
368 
369  if (pos < m_LabelGroups.size())
370  return pos;
371  // label group is not in property class
372  else
373  {
374  MITK_ERROR << "Could not find such a label group!";
375  return m_LabelGroups.size();
376  }
377 }
378 
380 {
381  for (auto it = m_LabelGroups.begin(); it != m_LabelGroups.end(); it++)
382  {
383  if (labelGroup.compare((*it)->labelGroupName) == 0)
384  return (*it);
385  }
386 
387  MITK_ERROR << "Could not find such a label group!";
388  return nullptr;
389 }
390 
392  std::string labelName)
393 {
394  for (auto it = labelGroup->labels.begin(); it != labelGroup->labels.end(); it++)
395  {
396  if (labelName.compare((*it)->labelName) == 0)
397  return (*it);
398  }
399 
400  MITK_ERROR << "Could not find such a label!";
401  return nullptr;
402 }
403 
404 bool mitk::TubeGraphProperty::TubeDescriptorsCompare(const TubeDescriptorType &tube1, const TubeDescriptorType &tube2)
405 {
406  if (((tube1.first == tube2.first) && (tube1.second == tube2.second)) ||
407  ((tube1.first == tube2.second) && (tube1.second == tube2.first)))
408  return true;
409  else
410  return false;
411 }
412 
413 bool mitk::TubeGraphProperty::IsEqual(const BaseProperty &property) const
414 { // TODO see ResectionProposalDescriptorProperty
415  // const TubeGraphProperty& other =
416  // static_cast<const TubeGraphProperty&>(property);
417 
418  return true
419  // same number of elements, same name and corresponding elements are equal (set datatype stores them sorted!)
420  /*(m_LDs.size() == other.GetLabelDesriptorSet().size())
421  && (m_ResectionProposalName == other.GetResectionProposalName())
422  && std::equal(m_LDs.begin(), m_LDs.end(), other.GetLabelDesriptorSet().begin())*/;
423 }
424 
425 bool mitk::TubeGraphProperty::Assign(const BaseProperty &property)
426 { // TODO see ResectionProposalDescriptorProperty
427  // const TubeGraphProperty& other =
428  // static_cast<const TubeGraphProperty&>(property);
429  /* this->m_LDs = other.m_LDs;
430  this->m_ResectionProposalName = other.m_ResectionProposalName;*/
431  return true;
432 }
433 
435 { // TODO
436  std::stringstream result;
437  /* result << "Tube graph: " << this->GetResectionProposalName();
438  result << "\n Number of labels: " << this->GetNumberOfLabels() << "\n";
439  for (LabelDesriptorSet::const_iterator it = m_LDs.begin(); it != m_LDs.end(); it++)
440  result << "Label " << it->m_Name<< ": "
441  << "Label = " << it->m_Label << ", ResectionType = " << it->m_ResectionType
442  << ", Volume = " << it->m_Volume << "\n";*/
443  return result.str();
444 }
445 
446 itk::LightObject::Pointer mitk::TubeGraphProperty::InternalClone() const
447 {
448  itk::LightObject::Pointer result(new Self(*this));
449  return result;
450 }
TubeGraph::TubeDescriptorType TubeDescriptorType
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void SetTubesToLabels(std::map< TubeToLabelGroupType, std::string > tubeToLabelMap)
void SetLabelForActivatedTubes(LabelGroup *labelGroup, LabelGroup::Label *label)
void RemoveAnnotation(Annotation *annotation)
void SetLabelColor(LabelGroup::Label *label, Color color)
LabelGroup * GetLabelGroupByName(std::string labelGroup)
void AddAnnotation(Annotation *annotation)
Color GetColorOfTube(const TubeDescriptorType &tube)
void AddLabelGroup(LabelGroup *labelGroup, unsigned int position)
Abstract base class for properties.
LabelGroupSetType GetLabelGroups()
std::pair< TubeDescriptorType, std::string > TubeToLabelGroupType
std::vector< TubeDescriptorType > GetActiveTubes()
Property for tube graphs.
std::vector< Annotation * > GetAnnotations()
void RemoveLabelGroup(LabelGroup *labelGroup)
void SetTubeActive(const TubeDescriptorType &tube, const bool &active)
std::map< TubeToLabelGroupType, std::string > GetTubesToLabels()
LabelGroup::Label * GetLabelByName(LabelGroup *labelGroup, std::string labelName)
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
void SetLabelVisibility(LabelGroup::Label *label, bool isVisible)
void SetTubesActive(std::vector< TubeDescriptorType > &tubes)
void RenameLabel(LabelGroup *labelGroup, LabelGroup::Label *label, std::string newName)
unsigned int GetIndexOfLabelGroup(LabelGroup *labelGroup)
std::vector< TubeGraphProperty::LabelGroup * > LabelGroupSetType
Annotation * GetAnnotationByName(std::string annotation)
bool IsTubeVisible(const TubeDescriptorType &tube)
virtual std::string GetValueAsString() const override
bool IsTubeActive(const TubeDescriptorType &tube)