NVIDIA Iray API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mdl_definition_wrapper.h
Go to the documentation of this file.
1 //*****************************************************************************
2 // Copyright 1986, 2014 NVIDIA Corporation. All rights reserved.
3 //*****************************************************************************
6 //*****************************************************************************
7 
8 #ifndef MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H
9 #define MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H
10 
11 #include <mi/base/handle.h>
12 #include <mi/neuraylib/assert.h>
20 #include <mi/neuraylib/set_get.h>
21 
22 #include <string>
23 
24 namespace mi {
25 
30 class Mdl_definition_wrapper
41 {
42 public:
43 
45 
46 
51  Mdl_definition_wrapper( neuraylib::ITransaction* transaction, const char* name);
52 
58  bool is_valid() const;
59 
66  Element_type get_type() const;
67 
69  const char* get_name() const;
70 
72  const char* get_module() const;
73 
76 
81  const char* get_parameter_name( Uint32 index) const;
82 
87  const char* get_parameter_type( const char* parameter_name) const;
88 
94  bool is_parameter_type_uniform( const char* parameter_name) const;
95 
100  const char* get_argument_type( const char* argument_name) const;
101 
105  const char* get_return_type() const;
106 
110  bool is_return_type_varying() const;
111 
113 
115 
126  template <class T>
127  Sint32 get_default( const char* parameter_name, T& value) const;
128 
130 
132 
134  const IAttribute_container* get_annotations() const;
135 
137  const IAttribute_container* get_parameter_annotations( const char* parameter_name) const;
138 
143  const IAttribute_container* get_return_annotations() const;
144 
146 
148 
168  IScene_element* create_instance(
169  const IAttribute_container* arguments = 0, Sint32* errors = 0) const;
170 
192  template <class T>
193  T* create_instance( const IAttribute_container* arguments = 0, Sint32* errors = 0) const
194  {
195  IScene_element* ptr_iscene_element = create_instance( arguments, errors);
196  if ( !ptr_iscene_element)
197  return 0;
198  T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
199  ptr_iscene_element->release();
200  return ptr_T;
201  }
202 
204 
205 private:
206 
209  Element_type m_type;
210  MISTD::string m_name;
211 };
212  // end group mi_neuray_materials
214 
216  neuraylib::ITransaction* transaction, const char* name)
217 {
218  mi_neuray_assert( transaction);
219  mi_neuray_assert( name);
220 
221  m_transaction = make_handle_dup( transaction);
222  m_name = name;
223  m_access = transaction->access<IScene_element>( name);
224  m_type = m_access.is_valid_interface()
225  ? m_access->get_element_type() : static_cast<Element_type>( 0);
226 }
227 
229 {
230  return m_access.is_valid_interface()
233 }
234 
236 {
237  return m_type;
238 }
239 
240 inline const char* Mdl_definition_wrapper::get_name() const
241 {
242  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
243 
245  m_access->get_interface<IMdl_material_definition>());
246  return md->get_name();
247 
248  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
249 
251  m_access->get_interface<IMdl_function_definition>());
252  return fd->get_name();
253 
254  } else
255  return 0;
256 }
257 
258 inline const char* Mdl_definition_wrapper::get_module() const
259 {
260  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
261 
263  m_access->get_interface<IMdl_material_definition>());
264  return md->get_module();
265 
266  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
267 
269  m_access->get_interface<IMdl_function_definition>());
270  return fd->get_module();
271 
272  } else
273  return 0;
274 }
275 
277 {
278  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
279 
281  m_access->get_interface<IMdl_material_definition>());
282  return md->get_parameter_count();
283 
284  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
285 
287  m_access->get_interface<IMdl_function_definition>());
288  return fd->get_parameter_count();
289 
290  } else
291  return 0;
292 }
293 
294 inline const char* Mdl_definition_wrapper::get_parameter_name( Uint32 index) const
295 {
296  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
297 
299  m_access->get_interface<IMdl_material_definition>());
300  return md->get_parameter_name( index);
301 
302  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
303 
305  m_access->get_interface<IMdl_function_definition>());
306  return fd->get_parameter_name( index);
307 
308  } else
309  return 0;
310 }
311 
312 inline const char* Mdl_definition_wrapper::get_parameter_type( const char* parameter_name) const
313 {
314  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
315 
317  m_access->get_interface<IMdl_material_definition>());
318  return md->get_parameter_type( parameter_name);
319 
320  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
321 
323  m_access->get_interface<IMdl_function_definition>());
324  return fd->get_parameter_type( parameter_name);
325 
326  } else
327  return 0;
328 }
329 
330 inline bool Mdl_definition_wrapper::is_parameter_type_uniform( const char* parameter_name) const
331 {
332  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
333 
335  m_access->get_interface<IMdl_material_definition>());
336  return md->is_parameter_type_uniform( parameter_name);
337 
338  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
339 
341  m_access->get_interface<IMdl_function_definition>());
342  return fd->is_parameter_type_uniform( parameter_name);
343 
344  } else
345  return false;
346 }
347 
348 inline const char* Mdl_definition_wrapper::get_argument_type( const char* argument_name) const
349 {
350  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
351 
353  m_access->get_interface<IMdl_material_definition>());
354  return md->get_argument_type( argument_name);
355 
356  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
357 
359  m_access->get_interface<IMdl_function_definition>());
360  return fd->get_argument_type( argument_name);
361 
362  } else
363  return 0;
364 }
365 
366 inline const char* Mdl_definition_wrapper::get_return_type() const
367 {
368  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
369 
370  return 0;
371 
372  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
373 
375  m_access->get_interface<IMdl_function_definition>());
376  return fd->get_return_type();
377 
378  } else
379  return 0;
380 }
381 
382 
384 {
385  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
386 
387  return false;
388 
389  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
390 
392  m_access->get_interface<IMdl_function_definition>());
393  return fd->is_return_type_varying();
394 
395  } else
396  return false;
397 }
398 
399 template <class T>
400 Sint32 Mdl_definition_wrapper::get_default( const char* parameter_name, T& value) const
401 {
402  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
403 
405  m_access->get_interface<IMdl_material_definition>());
406  base::Handle<const IAttachable> argument( md->get_default( parameter_name));
407  if( !argument.is_valid_interface())
408  return -2;
409  Sint32 result = mi::get_value( argument.get(), value);
410  return result == 0 ? 0 : -3;
411 
412  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
413 
415  m_access->get_interface<IMdl_function_definition>());
416  base::Handle<const IAttachable> argument( fd->get_default( parameter_name));
417  if( !argument.is_valid_interface())
418  return -2;
419  Sint32 result = mi::get_value( argument.get(), value);
420  return result == 0 ? 0 : -3;
421 
422  } else
423  return -1;
424 }
425 
427 {
428  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
429 
431  m_access->get_interface<IMdl_material_definition>());
432  return md->get_annotations();
433 
434  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
435 
437  m_access->get_interface<IMdl_function_definition>());
438  return fd->get_annotations();
439 
440  } else
441  return 0;
442 }
443 
445  const char* parameter_name) const
446 {
447  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
448 
450  m_access->get_interface<IMdl_material_definition>());
451  return md->get_parameter_annotations( parameter_name);
452 
453  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
454 
456  m_access->get_interface<IMdl_function_definition>());
457  return fd->get_parameter_annotations( parameter_name);
458 
459  } else
460  return 0;
461 }
462 
464 {
465  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
466 
467  return 0;
468 
469  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
470 
472  m_access->get_interface<IMdl_function_definition>());
473  return fd->get_return_annotations();
474 
475  } else
476  return 0;
477 }
478 
480  const IAttribute_container* arguments, Sint32* errors) const
481 {
482  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
483 
485  m_access->get_interface<IMdl_material_definition>());
486  if( arguments)
487  return md->create_material_instance( arguments, errors);
488 
489  base::Handle<IAttribute_container> local_arguments(
490  m_transaction->create<IAttribute_container>( "Attribute_container"));
491  Uint32 count = md->get_parameter_count();
492  for( Uint32 i = 0; i < count; ++i) {
493  base::Handle<const IData> default_( md->get_default( i));
494  if( !default_) {
495  const char* name = md->get_parameter_name( i);
496  const char* type = md->get_argument_type( i);
497  base::Handle<IAttachable> argument(
498  local_arguments->create_attribute<IAttachable>( name, type));
499  }
500  }
501  return md->create_material_instance( local_arguments.get(), errors);
502 
503  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
504 
506  m_access->get_interface<IMdl_function_definition>());
507  if( arguments)
508  return fd->create_function_call( arguments, errors);
509 
510  base::Handle<IAttribute_container> local_arguments(
511  m_transaction->create<IAttribute_container>( "Attribute_container"));
512  Uint32 count = fd->get_parameter_count();
513  for( Uint32 i = 0; i < count; ++i) {
514  base::Handle<const IData> default_( fd->get_default( i));
515  if( !default_) {
516  const char* name = fd->get_parameter_name( i);
517  const char* type = fd->get_argument_type( i);
518  base::Handle<IAttachable> argument(
519  local_arguments->create_attribute<IAttachable>( name, type));
520  }
521  }
522  return fd->create_function_call( local_arguments.get(), errors);
523 
524  } else
525  return 0;
526 
527 }
528 
529 } // namespace mi
530 
531 #endif // MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H