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