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