Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
vtkXMLMaterial.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: vtkXMLMaterial.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 
32 #include "vtkXMLMaterial.h"
33 
34 #include "vtkObjectFactory.h"
35 #include "vtkSmartPointer.h"
36 #include "vtkXMLDataElement.h"
37 #include "vtkXMLMaterialParser.h"
38 #include "vtkXMLShader.h"
39 
40 #include <assert.h>
41 #include <vector>
42 
43 class vtkXMLMaterialInternals
44 {
45 public:
46  typedef std::vector<vtkXMLDataElement *> VectorOfElements;
47  typedef std::vector<vtkSmartPointer<vtkXMLShader>> VectorOfShaders;
48  VectorOfElements Properties;
49  VectorOfShaders VertexShaders;
50  VectorOfShaders FragmentShaders;
51  VectorOfShaders GeometryShaders;
52  VectorOfElements Textures;
53  void Initialize()
54  {
55  this->Properties.clear();
56  this->VertexShaders.clear();
57  this->FragmentShaders.clear();
58  this->GeometryShaders.clear();
59  this->Textures.clear();
60  }
61 };
62 
64 
65 //-----------------------------------------------------------------------------
67 {
68  this->RootElement = nullptr;
69  this->Internals = new vtkXMLMaterialInternals;
70 }
71 
72 //-----------------------------------------------------------------------------
74 {
75  this->SetRootElement(nullptr);
76  delete this->Internals;
77 }
78 
80 {
81  if (!name)
82  {
83  return nullptr;
84  }
85 
87  vtkXMLMaterial *material = vtkXMLMaterial::New();
88  parser->SetMaterial(material);
89 
90  // First, look for material library files.
91  // Then, look for Repository files.
92 
93  char *filename = vtkXMLShader::LocateFile(name);
94  if (filename)
95  {
96  parser->SetFileName(filename);
97  delete[] filename;
98  parser->Parse();
99  parser->Delete();
100  return material;
101  }
102 
103  parser->Delete();
104  material->Delete();
105  return nullptr;
106 }
107 
108 //-----------------------------------------------------------------------------
109 void vtkXMLMaterial::SetRootElement(vtkXMLDataElement *root)
110 {
111  this->Internals->Initialize();
112 
113  vtkSetObjectBodyMacro(RootElement, vtkXMLDataElement, root);
114  if (this->RootElement)
115  {
116  // Update the internal data structure to
117  // avoid repeated searches.
118  int numElems = this->RootElement->GetNumberOfNestedElements();
119  for (int i = 0; i < numElems; i++)
120  {
121  vtkXMLDataElement *elem = this->RootElement->GetNestedElement(i);
122  const char *name = elem->GetName();
123  if (!name)
124  {
125  continue;
126  }
127  if (strcmp(name, "Property") == 0)
128  {
129  this->Internals->Properties.push_back(elem);
130  }
131  else if (strcmp(name, "Shader") == 0)
132  {
133  vtkXMLShader *shader = vtkXMLShader::New();
134  shader->SetRootElement(elem);
135 
136  switch (shader->GetScope())
137  {
139  this->Internals->VertexShaders.push_back(shader);
140  break;
142  this->Internals->FragmentShaders.push_back(shader);
143  break;
145  this->Internals->GeometryShaders.push_back(shader);
146  break;
147  default:
148  vtkErrorMacro("Invalid scope for shader: " << shader->GetName());
149  }
150 
151  shader->Delete();
152  }
153  else if (strcmp(name, "Texture") == 0)
154  {
155  this->Internals->Textures.push_back(elem);
156  }
157  }
158  }
159 }
160 
161 //-----------------------------------------------------------------------------
163 {
164  return static_cast<int>(this->Internals->Properties.size());
165 }
166 
167 //-----------------------------------------------------------------------------
169 {
170  return static_cast<int>(this->Internals->Textures.size());
171 }
172 
173 //-----------------------------------------------------------------------------
175 {
176  return static_cast<int>(this->Internals->VertexShaders.size());
177 }
178 
179 //-----------------------------------------------------------------------------
181 {
182  return static_cast<int>(this->Internals->FragmentShaders.size());
183 }
184 
186 {
187  return static_cast<int>(this->Internals->GeometryShaders.size());
188 }
189 
190 //-----------------------------------------------------------------------------
191 vtkXMLDataElement *vtkXMLMaterial::GetProperty(int id)
192 {
193  if (id < this->GetNumberOfProperties())
194  {
195  return this->Internals->Properties[id];
196  }
197  return nullptr;
198 }
199 
200 //-----------------------------------------------------------------------------
201 vtkXMLDataElement *vtkXMLMaterial::GetTexture(int index)
202 {
203  if (index < this->GetNumberOfTextures())
204  {
205  return this->Internals->Textures[index];
206  }
207  return nullptr;
208 }
209 
210 //-----------------------------------------------------------------------------
212 {
213  if (id < this->GetNumberOfVertexShaders())
214  {
215  return this->Internals->VertexShaders[id].GetPointer();
216  }
217  return nullptr;
218 }
219 
220 //-----------------------------------------------------------------------------
222 {
223  if (id < this->GetNumberOfFragmentShaders())
224  {
225  return this->Internals->FragmentShaders[id].GetPointer();
226  }
227  return nullptr;
228 }
229 
231 {
232  if (id < this->GetNumberOfGeometryShaders())
233  {
234  return this->Internals->GeometryShaders[id].GetPointer();
235  }
236  return nullptr;
237 }
238 
239 //----------------------------------------------------------------------------
240 
241 // ----------------------------------------------------------------------------
242 // Description:
243 // Get the style the shaders.
244 // \post valid_result: result==1 || result==2
246 {
247  int result = 1;
248  int vStyle = 0;
249  if (this->GetVertexShader())
250  {
251  vStyle = this->GetVertexShader()->GetStyle();
252  }
253  int fStyle = 0;
254  if (this->GetFragmentShader())
255  {
256  fStyle = this->GetFragmentShader()->GetStyle();
257  }
258  int gStyle = 0;
259  if (this->GetGeometryShader())
260  {
261  gStyle = this->GetGeometryShader()->GetStyle();
262  }
263  if (vStyle != 0 && fStyle != 0 && !gStyle && vStyle != fStyle)
264  {
265  vtkErrorMacro(<< "vertex shader and fragment shader style differ.");
266  }
267  else
268  {
269  if (vStyle != 0)
270  {
271  result = vStyle;
272  }
273  else
274  {
275  result = fStyle;
276  }
277  }
278 
279  assert("post: valid_result" && (result == 1 || result == 2));
280  return result;
281 }
282 
283 //-----------------------------------------------------------------------------
284 void vtkXMLMaterial::PrintSelf(ostream &os, vtkIndent indent)
285 {
286  this->Superclass::PrintSelf(os, indent);
287  os << indent << "Number of Properties: " << this->GetNumberOfProperties() << endl;
288  os << indent << "Number of Vertex Shaders: " << this->GetNumberOfVertexShaders() << endl;
289  os << indent << "Number of Fragment Shaders: " << this->GetNumberOfFragmentShaders() << endl;
290  os << indent << "RootElement: ";
291  if (this->RootElement)
292  {
293  os << endl;
294  this->RootElement->PrintSelf(os, indent.GetNextIndent());
295  }
296  else
297  {
298  os << "(null)" << endl;
299  }
300 }
int GetNumberOfVertexShaders()
vtkXMLMaterialInternals * Internals
void SetRootElement(vtkXMLDataElement *)
vtkXMLDataElement * GetTexture(int id=0)
vtkXMLShader * GetVertexShader(int id=0)
void SetMaterial(vtkXMLMaterial *)
vtkXMLDataElement * GetProperty(int id=0)
static vtkXMLShader * New()
static vtkXMLMaterial * New()
vtkStandardNewMacro(vtkXMLMaterial)
vtkXMLShader * GetGeometryShader(int id=0)
void PrintSelf(ostream &os, vtkIndent indent) override
const char * GetName()
int GetNumberOfFragmentShaders()
static const std::string filename
static vtkXMLMaterial * CreateInstance(const char *name)
virtual int Parse() override
void SetRootElement(vtkXMLDataElement *)
vtkXMLShader * GetFragmentShader(int id=0)
static char * LocateFile(const char *filename)
static vtkXMLMaterialParser * New()
int GetNumberOfGeometryShaders()
vtkXMLDataElement * RootElement