Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
vtkXMLShader.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 /*=========================================================================
18 
19  Program: Visualization Toolkit
20  Module: vtkXMLShader.cxx
21 
22  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  All rights reserved.
24  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
25 
26  This software is distributed WITHOUT ANY WARRANTY; without even
27  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
28  PURPOSE. See the above copyright notice for more information.
29 
30 =========================================================================*/
31 #include "vtkXMLShader.h"
32 
33 #include "vtkObjectFactory.h"
34 #include "vtkXMLDataElement.h"
35 
36 #include <assert.h>
37 #include <vtksys/SystemTools.hxx>
38 
40 vtkCxxSetObjectMacro(vtkXMLShader, SourceLibraryElement, vtkXMLDataElement);
41 //-----------------------------------------------------------------------------
42 vtkXMLShader::vtkXMLShader() : Code(nullptr), RootElement(nullptr), SourceLibraryElement(nullptr), Args(nullptr)
43 {
44 }
45 
46 //-----------------------------------------------------------------------------
48 {
49  if (this->RootElement)
50  {
51  this->RootElement->UnRegister(this);
52  this->RootElement = nullptr;
53  }
54  this->SetSourceLibraryElement(nullptr);
55  this->SetCode(nullptr);
56  this->CleanupArgs();
57 }
58 
59 //-----------------------------------------------------------------------------
60 void vtkXMLShader::SetRootElement(vtkXMLDataElement *root)
61 {
62  vtkSetObjectBodyMacro(RootElement, vtkXMLDataElement, root);
63  this->SetCode(nullptr);
64  this->SetSourceLibraryElement(nullptr); // release the SourceLibrary element.
65 }
66 
67 //-----------------------------------------------------------------------------
68 // Note that this method allocates a new string which must be deleted by
69 // the caller.
71 {
72  if (!filename)
73  {
74  return nullptr;
75  }
76 
77  // if filename is absolute path, return the same.
78  if (vtksys::SystemTools::FileExists(filename))
79  {
80  return vtksys::SystemTools::DuplicateString(filename);
81  }
82 
83  // Fetch any runtime defined user paths for materials
84  std::vector<std::string> paths;
85  std::string userpaths;
86  vtksys::SystemTools::GetEnv("USER_MATERIALS_DIRS", userpaths);
87  if (userpaths.size() > 0)
88  {
89  vtksys::SystemTools::Split(userpaths.c_str(), paths, ';');
90  }
91 
92 #ifdef VTK_MATERIALS_DIRS
93  // search thru default paths to locate file.
94  vtksys::SystemTools::Split(VTK_MATERIALS_DIRS, paths, ';');
95 #endif
96  for (unsigned int i = 0; i < paths.size(); i++)
97  {
98  std::string path = paths[i];
99  if (path.size() == 0)
100  {
101  continue;
102  }
103  vtksys::SystemTools::ConvertToUnixSlashes(path);
104  if (path[path.size() - 1] != '/')
105  {
106  path += "/";
107  }
108  path += filename;
109  if (vtksys::SystemTools::FileExists(path.c_str()))
110  {
111  return vtksys::SystemTools::DuplicateString(path.c_str());
112  }
113  }
114  return nullptr;
115 }
116 
117 //-----------------------------------------------------------------------------
119 {
120  if (this->RootElement)
121  {
122  const char *scope = this->RootElement->GetAttribute("scope");
123  if (!scope)
124  {
125  vtkErrorMacro("Shader description missing \"scope\" attribute.");
126  }
127  else if (strcmp(scope, "Vertex") == 0)
128  {
130  }
131  else if (strcmp(scope, "Fragment") == 0)
132  {
134  }
135  else if (strcmp(scope, "Geometry") == 0)
136  {
138  }
139  }
141 }
142 
143 // ----------------------------------------------------------------------------
144 // \post valid_result: result==1 || result==2
146 {
147  int result = 1;
148  if (this->RootElement)
149  {
150  const char *loc = this->RootElement->GetAttribute("style");
151  if (loc == nullptr)
152  {
153  // fine. this attribute is optional.
154  }
155  else
156  {
157  if (strcmp(loc, "1") == 0)
158  {
159  // fine. default value.
160  }
161  else
162  {
163  if (strcmp(loc, "2") == 0)
164  {
165  result = 2; // new style
166  }
167  else
168  {
169  vtkErrorMacro(<< "style number not supported. Expect 1 or 2. We force it to be 1.");
170  }
171  }
172  }
173  }
174 
175  assert("post valid_result" && (result == 1 || result == 2));
176  return result;
177 }
178 
179 //-----------------------------------------------------------------------------
181 {
182  return (this->RootElement) ? this->RootElement->GetAttribute("name") : nullptr;
183 }
184 
185 //-----------------------------------------------------------------------------
187 {
188  return (this->RootElement) ? this->RootElement->GetAttribute("entry") : nullptr;
189 }
190 
191 //-----------------------------------------------------------------------------
192 const char **vtkXMLShader::GetArgs()
193 {
194  this->CleanupArgs();
195  if (!this->RootElement || !this->RootElement->GetAttribute("args"))
196  {
197  return nullptr;
198  }
199 
200  std::vector<std::string> args;
201  vtksys::SystemTools::Split(this->RootElement->GetAttribute("args"), args, ' ');
202 
203  int i;
204  int size = static_cast<int>(args.size());
205  if (size == 0)
206  {
207  return nullptr;
208  }
209  this->Args = new char *[size + 1];
210  for (i = 0; i < size; i++)
211  {
212  this->Args[i] = vtksys::SystemTools::DuplicateString(args[i].c_str());
213  }
214  this->Args[size] = nullptr;
215  return const_cast<const char **>(this->Args);
216 }
217 
218 //-----------------------------------------------------------------------------
220 {
221  return this->RootElement->GetCharacterData();
222 }
223 
224 //-----------------------------------------------------------------------------
226 {
227  if (this->Args)
228  {
229  char **a = this->Args;
230  while (*a)
231  {
232  delete[](*a);
233  a++;
234  }
235  delete[] this->Args;
236  this->Args = nullptr;
237  }
238 }
239 
240 //-----------------------------------------------------------------------------
241 void vtkXMLShader::PrintSelf(ostream &os, vtkIndent indent)
242 {
243  this->Superclass::PrintSelf(os, indent);
244  os << indent << "Name: " << (this->GetName() ? this->GetName() : "(none)") << endl;
245  os << indent << "Scope: ";
246  switch (this->GetScope())
247  {
248  case SCOPE_NONE:
249  os << "None";
250  break;
251  case SCOPE_MIXED:
252  os << "Mixed";
253  break;
254  case SCOPE_VERTEX:
255  os << "Vertex";
256  break;
257  case SCOPE_FRAGMENT:
258  os << "Fragment";
259  break;
260  case SCOPE_GEOMETRY:
261  os << "Geometry";
262  break;
263  }
264  os << endl;
265 
266  os << indent << "Entry: " << (this->GetEntry() ? this->GetEntry() : "(none)") << endl;
267  os << indent << "Args: ";
268  const char **args = this->GetArgs();
269  if (!args)
270  {
271  os << "(none)" << endl;
272  }
273  else
274  {
275  while (*args)
276  {
277  os << indent << *args << " ";
278  args++;
279  }
280  os << endl;
281  }
282 
283  os << indent << "RootElement: ";
284  if (this->RootElement)
285  {
286  os << endl;
287  this->RootElement->PrintSelf(os, indent.GetNextIndent());
288  }
289  else
290  {
291  os << "(none)" << endl;
292  }
293 }
void SetSourceLibraryElement(vtkXMLDataElement *)
const char * GetEntry()
void SetRootElement(vtkXMLDataElement *)
vtkXMLDataElement * RootElement
Definition: vtkXMLShader.h:117
void PrintSelf(ostream &os, vtkIndent indent) override
const char * GetCode()
char ** Args
Definition: vtkXMLShader.h:121
const char * GetName()
void CleanupArgs()
vtkStandardNewMacro(vtkXMLShader)
static const std::string filename
const char ** GetArgs()
static char * LocateFile(const char *filename)
vtkCxxSetObjectMacro(vtkXMLShader, SourceLibraryElement, vtkXMLDataElement)