Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkAnatomicalStructureColorPresets.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 
14 #include <vtkObjectFactory.h>
15 
16 #include "usGetModuleContext.h"
17 #include "usModuleContext.h"
18 #include "usModule.h"
19 #include "usModuleResource.h"
20 #include "usModuleResourceStream.h"
21 
22 namespace mitk {
23 
24  const std::string AnatomicalStructureColorPresets::PRESET = "preset";
25  const std::string AnatomicalStructureColorPresets::CATEGORY = "category";
26  const std::string AnatomicalStructureColorPresets::TYPE = "type";
27  const std::string AnatomicalStructureColorPresets::MODIFIER = "modifier";
28  const std::string AnatomicalStructureColorPresets::COLOR = "color";
29 
30  const std::string AnatomicalStructureColorPresets::CODE_VALUE = "code_value";
31  const std::string AnatomicalStructureColorPresets::CODE_SCHEME = "coding_scheme";
32  const std::string AnatomicalStructureColorPresets::CODE_NAME = "code_name";
33 
34  const std::string AnatomicalStructureColorPresets::COLOR_R = "rgb_r";
35  const std::string AnatomicalStructureColorPresets::COLOR_G = "rgb_g";
36  const std::string AnatomicalStructureColorPresets::COLOR_B = "rgb_b";
37 
38 
39  vtkStandardNewMacro(AnatomicalStructureColorPresets);
40 
42  {
43  us::ModuleResource presetResource = us::GetModuleContext()->GetModule()->GetResource("mitkAnatomicalStructureColorPresets.xml");
44  if (!presetResource) return false;
45 
46  us::ModuleResourceStream presetStream(presetResource);
47  vtkXMLParser::SetStream(&presetStream);
48  if (!vtkXMLParser::Parse())
49  {
50 #ifdef INTERDEBUG
51  MITK_INFO<<"AnatomicalStructureColorPresets::LoadPreset xml file cannot parse!"<<std::endl;
52 #endif
53  return false;
54  }
55  return true;
56  }
57 
58  bool AnatomicalStructureColorPresets::LoadPreset(const std::string& fileName)
59  {
60  if (fileName.empty())
61  return false;
62 
63  vtkXMLParser::SetFileName(fileName.c_str());
64 
65  if (!vtkXMLParser::Parse())
66  {
67 #ifdef INTERDEBUG
68  MITK_INFO<<"AnatomicalStructureColorPresets::LoadPreset xml file cannot parse!"<<std::endl;
69 #endif
70  }
71 
72  return true;
73 
74  }
75 
76  void AnatomicalStructureColorPresets::StartElement(const char *elementName, const char **atts)
77  {
78  std::string elementNameString = elementName;
79  if (elementNameString == PRESET)
80  {
81  m_presetName = ReadXMLStringAttribute("name", atts);
82  }
83  else if (elementNameString == CATEGORY)
84  {
85  std::string value = ReadXMLStringAttribute(CODE_VALUE, atts);
86  std::string scheme = ReadXMLStringAttribute(CODE_SCHEME, atts);
87  std::string name = ReadXMLStringAttribute(CODE_NAME, atts);
88  m_Category[m_presetName] = Category(value, scheme, name);
89  }
90  else if (elementNameString == TYPE)
91  {
92  std::string value = ReadXMLStringAttribute(CODE_VALUE, atts);
93  std::string scheme = ReadXMLStringAttribute(CODE_SCHEME, atts);
94  std::string name = ReadXMLStringAttribute(CODE_NAME, atts);
95  m_Type[m_presetName] = Type(value, scheme, name);
96  }
97  else if (elementNameString == MODIFIER)
98  {
99  std::string value = ReadXMLStringAttribute(CODE_VALUE, atts);
100  std::string scheme = ReadXMLStringAttribute(CODE_SCHEME, atts);
101  std::string name = ReadXMLStringAttribute(CODE_NAME, atts);
102  m_Type[m_presetName].modifier = Type::Modifier(value, scheme, name);
103  }
104  else if (elementNameString == COLOR)
105  {
106  std::string red = ReadXMLStringAttribute(COLOR_R, atts);
107  float r = atof(red.c_str());
108  std::string green = ReadXMLStringAttribute(COLOR_G, atts);
109  float g = atof(green.c_str());
110  std::string blue = ReadXMLStringAttribute(COLOR_B, atts);
111  float b = atof(blue.c_str());
112  Color color;
113  color.SetRed(r); color.SetGreen(g); color.SetBlue(b);
114  m_Color[m_presetName] = color;
115  }
116  }
117 
118  std::string AnatomicalStructureColorPresets::ReadXMLStringAttribute(const std::string& name, const char **atts)
119  {
120  if (atts)
121  {
122  const char** attsIter = atts;
123 
124  while (*attsIter != nullptr)
125  {
126  if (name == *attsIter)
127  {
128  return *(++attsIter);
129  }
130  attsIter += 2;
131  }
132  }
133 
134  return std::string();
135  }
136 
138  {
139  return m_Category[name];
140  }
141 
143  {
144  return m_Type[name];
145  }
146 
148  {
149  return m_Color[name];
150  }
151 
152  std::map<std::string, AnatomicalStructureColorPresets::Category> const AnatomicalStructureColorPresets::GetCategoryPresets()
153  {
154  return m_Category;
155  }
156 
157  std::map<std::string, AnatomicalStructureColorPresets::Type> const AnatomicalStructureColorPresets::GetTypePresets()
158  {
159  return m_Type;
160  }
161 
162  std::map<std::string, Color> const AnatomicalStructureColorPresets::GetColorPresets()
163  {
164  return m_Color;
165  }
166 
167  void AnatomicalStructureColorPresets::Save()
168  {
169  //Not yet implemented
170  }
171 
172  void AnatomicalStructureColorPresets::NewPresets(std::map<std::string, AnatomicalStructureColorPresets::Category>& newCategory, std::map<std::string, AnatomicalStructureColorPresets::Type>& newType, std::map<std::string, Color>& newColor)
173  {
174  m_Category = newCategory;
175  m_Type = newType;
176  m_Color = newColor;
177  this->Save();
178  }
179 
180 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
std::map< std::string, Color > const GetColorPresets()
DataCollection - Class to facilitate loading/accessing structured data.
void NewPresets(std::map< std::string, Category > &newCategory, std::map< std::string, Type > &newType, std::map< std::string, Color > &newColor)
std::map< std::string, Type > const GetTypePresets()
Module * GetModule() const
vtkStandardNewMacro(AnatomicalStructureColorPresets)
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
std::map< std::string, Category > const GetCategoryPresets()
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.