NVIDIA Iray API Home  Up
 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, 2016 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 #include <mi/neuraylib/version.h>
22 
23 #include <string>
24 
25 namespace mi {
26 
27 namespace neuraylib {
28 
33 class Mdl_definition_wrapper
44 {
45 public:
46 
48 
49 
54  Mdl_definition_wrapper( ITransaction* transaction, const char* name);
55 
61  bool is_valid() const;
62 
69  Element_type get_type() const;
70 
72  const char* get_name() const;
73 
75  const char* get_module() const;
76 
78  bool is_exported() const;
79 
82 
87  const char* get_parameter_name( Uint32 index) const;
88 
93  Sint32 get_parameter_index( const char* name) const;
94 
102  const char* get_parameter_type( Uint32 index) const;
103 
108  const char* get_parameter_type( const char* name) const;
109 
118  bool is_parameter_type_uniform( Uint32 index) const;
119 
125  bool is_parameter_type_uniform( const char* name) const;
126 
134  const char* get_argument_type( Uint32 index) const;
135 
140  const char* get_argument_type( const char* name) const;
141 
145  const char* get_return_type() const;
146 
150  bool is_return_type_varying() const;
151 
153 
155 
169  template<class T>
170  Sint32 get_default( Uint32 index, T& value) const;
171 
182  template <class T>
183  Sint32 get_default( const char* name, T& value) const;
184 
186 
188 
190  const IAttribute_container* get_annotations() const;
191 
199  const IAttribute_container* get_parameter_annotations( Uint32 index) const;
200 
205  const IAttribute_container* get_parameter_annotations( const char* name) const;
206 
211  const IAttribute_container* get_return_annotations() const;
212 
214 
216 
236  IScene_element* create_instance(
237  const IAttribute_container* arguments = 0, Sint32* errors = 0) const;
238 
261  template <class T>
262  T* create_instance( const IAttribute_container* arguments = 0, Sint32* errors = 0) const
263  {
264  IScene_element* ptr_iscene_element = create_instance( arguments, errors);
265  if ( !ptr_iscene_element)
266  return 0;
267  T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
268  ptr_iscene_element->release();
269  return ptr_T;
270  }
271 
273 
274 private:
275 
278  Element_type m_type;
279  MISTD::string m_name;
280 };
281  // end group mi_neuray_materials
283 
285  ITransaction* transaction, const char* name)
286 {
287  mi_neuray_assert( transaction);
288  mi_neuray_assert( name);
289 
290  m_transaction = make_handle_dup( transaction);
291  m_name = name;
292  m_access = transaction->access<IScene_element>( name);
293  m_type = m_access.is_valid_interface()
294  ? m_access->get_element_type() : static_cast<Element_type>( 0);
295 }
296 
298 {
299  return m_access.is_valid_interface()
302 }
303 
305 {
306  return m_type;
307 }
308 
309 inline const char* Mdl_definition_wrapper::get_name() const
310 {
311  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
312 
314  m_access->get_interface<IMdl_material_definition>());
315  return md->get_name();
316 
317  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
318 
320  m_access->get_interface<IMdl_function_definition>());
321  return fd->get_name();
322 
323  } else
324  return 0;
325 }
326 
327 inline const char* Mdl_definition_wrapper::get_module() const
328 {
329  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
330 
332  m_access->get_interface<IMdl_material_definition>());
333  return md->get_module();
334 
335  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
336 
338  m_access->get_interface<IMdl_function_definition>());
339  return fd->get_module();
340 
341  } else
342  return 0;
343 }
344 
346 {
347  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
348 
350  m_access->get_interface<IMdl_material_definition>());
351  return md->is_exported();
352 
353  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
354 
356  m_access->get_interface<IMdl_function_definition>());
357  return fd->is_exported();
358 
359  } else
360  return false;
361 }
362 
364 {
365  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
366 
368  m_access->get_interface<IMdl_material_definition>());
369  return md->get_parameter_count();
370 
371  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
372 
374  m_access->get_interface<IMdl_function_definition>());
375  return fd->get_parameter_count();
376 
377  } else
378  return 0;
379 }
380 
381 inline const char* Mdl_definition_wrapper::get_parameter_name( Uint32 index) const
382 {
383  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
384 
386  m_access->get_interface<IMdl_material_definition>());
387  return md->get_parameter_name( index);
388 
389  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
390 
392  m_access->get_interface<IMdl_function_definition>());
393  return fd->get_parameter_name( index);
394 
395  } else
396  return 0;
397 }
398 
399 inline Sint32 Mdl_definition_wrapper::get_parameter_index( const char* name) const
400 {
401  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
402 
404  m_access->get_interface<IMdl_material_definition>());
405  return md->get_parameter_index( name);
406 
407  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
408 
410  m_access->get_interface<IMdl_function_definition>());
411  return fd->get_parameter_index( name);
412 
413  } else
414  return 0;
415 }
416 
417 inline const char* Mdl_definition_wrapper::get_parameter_type( Uint32 index) const
418 {
419  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
420 
422  m_access->get_interface<IMdl_material_definition>());
423  return md->get_parameter_type( index);
424 
425  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
426 
428  m_access->get_interface<IMdl_function_definition>());
429  return fd->get_parameter_type( index);
430 
431  } else
432  return 0;
433 }
434 
435 inline const char* Mdl_definition_wrapper::get_parameter_type( const char* name) const
436 {
437  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
438 
440  m_access->get_interface<IMdl_material_definition>());
441  return md->get_parameter_type( name);
442 
443  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
444 
446  m_access->get_interface<IMdl_function_definition>());
447  return fd->get_parameter_type( name);
448 
449  } else
450  return 0;
451 }
452 
454 {
455  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
456 
458  m_access->get_interface<IMdl_material_definition>());
459  return md->is_parameter_type_uniform( index);
460 
461  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
462 
464  m_access->get_interface<IMdl_function_definition>());
465  return fd->is_parameter_type_uniform( index);
466 
467  } else
468  return false;
469 }
470 
471 inline bool Mdl_definition_wrapper::is_parameter_type_uniform( const char* name) const
472 {
473  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
474 
476  m_access->get_interface<IMdl_material_definition>());
477  return md->is_parameter_type_uniform( name);
478 
479  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
480 
482  m_access->get_interface<IMdl_function_definition>());
483  return fd->is_parameter_type_uniform( name);
484 
485  } else
486  return false;
487 }
488 
489 inline const char* Mdl_definition_wrapper::get_argument_type( Uint32 index) const
490 {
491  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
492 
494  m_access->get_interface<IMdl_material_definition>());
495  return md->get_argument_type( index);
496 
497  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
498 
500  m_access->get_interface<IMdl_function_definition>());
501  return fd->get_argument_type( index);
502 
503  } else
504  return 0;
505 }
506 
507 inline const char* Mdl_definition_wrapper::get_argument_type( const char* name) const
508 {
509  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
510 
512  m_access->get_interface<IMdl_material_definition>());
513  return md->get_argument_type( name);
514 
515  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
516 
518  m_access->get_interface<IMdl_function_definition>());
519  return fd->get_argument_type( name);
520 
521  } else
522  return 0;
523 }
524 
525 inline const char* Mdl_definition_wrapper::get_return_type() const
526 {
527  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
528 
529  return 0;
530 
531  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
532 
534  m_access->get_interface<IMdl_function_definition>());
535  return fd->get_return_type();
536 
537  } else
538  return 0;
539 }
540 
541 
543 {
544  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
545 
546  return false;
547 
548  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
549 
551  m_access->get_interface<IMdl_function_definition>());
552  return fd->is_return_type_varying();
553 
554  } else
555  return false;
556 }
557 
558 template <class T>
560 {
561  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
562 
564  m_access->get_interface<IMdl_material_definition>());
565  base::Handle<const IAttachable> argument( md->get_default( index));
566  if( !argument.is_valid_interface())
567  return -2;
568  Sint32 result = mi::get_value( argument.get(), value);
569  return result == 0 ? 0 : -3;
570 
571  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
572 
574  m_access->get_interface<IMdl_function_definition>());
575  base::Handle<const IAttachable> argument( fd->get_default( index));
576  if( !argument.is_valid_interface())
577  return -2;
578  Sint32 result = mi::get_value( argument.get(), value);
579  return result == 0 ? 0 : -3;
580 
581  } else
582  return -1;
583 }
584 
585 template <class T>
586 Sint32 Mdl_definition_wrapper::get_default( const char* name, T& value) const
587 {
588  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
589 
591  m_access->get_interface<IMdl_material_definition>());
592  base::Handle<const IAttachable> argument( md->get_default( name));
593  if( !argument.is_valid_interface())
594  return -2;
595  Sint32 result = mi::get_value( argument.get(), value);
596  return result == 0 ? 0 : -3;
597 
598  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
599 
601  m_access->get_interface<IMdl_function_definition>());
602  base::Handle<const IAttachable> argument( fd->get_default( name));
603  if( !argument.is_valid_interface())
604  return -2;
605  Sint32 result = mi::get_value( argument.get(), value);
606  return result == 0 ? 0 : -3;
607 
608  } else
609  return -1;
610 }
611 
613 {
614  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
615 
617  m_access->get_interface<IMdl_material_definition>());
618  return md->get_annotations();
619 
620  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
621 
623  m_access->get_interface<IMdl_function_definition>());
624  return fd->get_annotations();
625 
626  } else
627  return 0;
628 }
629 
631  Uint32 index) const
632 {
633  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
634 
636  m_access->get_interface<IMdl_material_definition>());
637  return md->get_parameter_annotations( index);
638 
639  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
640 
642  m_access->get_interface<IMdl_function_definition>());
643  return fd->get_parameter_annotations( index);
644 
645  } else
646  return 0;
647 }
648 
650  const char* name) const
651 {
652  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
653 
655  m_access->get_interface<IMdl_material_definition>());
656  return md->get_parameter_annotations( name);
657 
658  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
659 
661  m_access->get_interface<IMdl_function_definition>());
662  return fd->get_parameter_annotations( name);
663 
664  } else
665  return 0;
666 }
667 
669 {
670  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
671 
672  return 0;
673 
674  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
675 
677  m_access->get_interface<IMdl_function_definition>());
678  return fd->get_return_annotations();
679 
680  } else
681  return 0;
682 }
683 
685  const IAttribute_container* arguments, Sint32* errors) const
686 {
687  if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {
688 
690  m_access->get_interface<IMdl_material_definition>());
691  if( arguments)
692  return md->create_material_instance( arguments, errors);
693 
694  base::Handle<IAttribute_container> local_arguments(
695  m_transaction->create<IAttribute_container>( "Attribute_container"));
696  Uint32 count = md->get_parameter_count();
697  for( Uint32 i = 0; i < count; ++i) {
698  base::Handle<const IData> default_( md->get_default( i));
699  if( !default_) {
700  const char* name = md->get_parameter_name( i);
701  const char* type = md->get_argument_type( i);
702  base::Handle<IAttachable> argument(
703  local_arguments->create_attribute<IAttachable>( name, type));
704  }
705  }
706  return md->create_material_instance( local_arguments.get(), errors);
707 
708  } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {
709 
711  m_access->get_interface<IMdl_function_definition>());
712  if( arguments)
713  return fd->create_function_call( arguments, errors);
714 
715  base::Handle<IAttribute_container> local_arguments(
716  m_transaction->create<IAttribute_container>( "Attribute_container"));
717  Uint32 count = fd->get_parameter_count();
718  for( Uint32 i = 0; i < count; ++i) {
719  base::Handle<const IData> default_( fd->get_default( i));
720  if( !default_) {
721  const char* name = fd->get_parameter_name( i);
722  const char* type = fd->get_argument_type( i);
723  base::Handle<IAttachable> argument(
724  local_arguments->create_attribute<IAttachable>( name, type));
725  }
726  }
727  return fd->create_function_call( local_arguments.get(), errors);
728 
729  } else
730  return 0;
731 
732 }
733 
734 } // namespace neuraylib
735 
736 #ifdef MI_NEURAYLIB_DEPRECATED_NAMESPACE_MI_TRANSITION
745 #endif // MI_NEURAYLIB_DEPRECATED_NAMESPACE_MI_TRANSITION
746 
747 } // namespace mi
748 
749 #endif // MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H