neuray API Programmer's Manual

mdl_definition_wrapper.h File Reference

Description

Utility class for MDL material and function definitions.

Code Example

mdl_definition_wrapper.h

‎//*****************************************************************************
// Copyright 1986, 2016 NVIDIA Corporation. All rights reserved.
//*****************************************************************************
//*****************************************************************************

#ifndef MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H
#define MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H

#include <mi/base/handle.h>
#include <mi/neuraylib/assert.h>
#include <mi/neuraylib/iattachable.h>
#include <mi/neuraylib/iattribute_container.h>
#include <mi/neuraylib/imdl_function_call.h>
#include <mi/neuraylib/imdl_function_definition.h>
#include <mi/neuraylib/imdl_material_definition.h>
#include <mi/neuraylib/imdl_material_instance.h>
#include <mi/neuraylib/itransaction.h>
#include <mi/neuraylib/set_get.h>
#include <mi/neuraylib/version.h>

#include <string>

namespace mi {

namespace neuraylib {











class Mdl_definition_wrapper
{
public:



    Mdl_definition_wrapper( ITransaction* transaction, const char* name);

    bool is_valid() const;
    
    Element_type 
               get_type() const;

    const char* get_name() const;

    const char* get_module() const;

    bool is_exported() const;
    
    Uint32 
               get_parameter_count() const;

    const char* get_parameter_name( Uint32 index) const;

    Sint32 
               get_parameter_index( const char* name) const;

    const char* get_parameter_type( Uint32 index) const;

    const char* get_parameter_type( const char* name) const;

    bool is_parameter_type_uniform( Uint32 index) const;

    bool is_parameter_type_uniform( const char* name) const;

    const char* get_argument_type( Uint32 index) const;

    const char* get_argument_type( const char* name) const;

    const char* get_return_type() const;

    bool is_return_type_varying() const;




    template<class T>
    Sint32 
               get_default( Uint32 index, T& value) const;

    template <class T>
    Sint32 
               get_default( const char* name, T& value) const;



    
    const IAttribute_container* get_annotations() const;

    const IAttribute_container* get_parameter_annotations( Uint32 index) const;

    const IAttribute_container* get_parameter_annotations( const char* name) const;

    const IAttribute_container* get_return_annotations() const;




    IScene_element* create_instance(
        const IAttribute_container* arguments = 0, Sint32* errors = 0) const;

    template <class T>
    T* create_instance( const IAttribute_container* arguments = 0, Sint32* errors = 0) const
    {
        IScene_element* ptr_iscene_element = create_instance( arguments, errors);
        if ( !ptr_iscene_element)
            return 0;
        T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
        ptr_iscene_element->release();
        return ptr_T;
    }


private:

    base::Handle< neuraylib::ITransaction> m_transaction;
    base::Handle< const IScene_element> m_access;
    Element_type m_type;
    MISTD::string m_name;
};
 // end group mi_neuray_materials

inline Mdl_definition_wrapper::Mdl_definition_wrapper(
    ITransaction* transaction, const char* name)
{
    mi_neuray_assert( transaction);
    mi_neuray_assert( name);

    m_transaction = make_handle_dup( transaction);
    m_name = name;
    m_access = transaction->access<IScene_element>( name);
    m_type = m_access.is_valid_interface()
        ? m_access->get_element_type() : static_cast<Element_type>( 0);
}

inline bool Mdl_definition_wrapper::is_valid() const
{
    return m_access.is_valid_interface()
        && (m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION
        ||  m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION);
}

inline Element_type 
               Mdl_definition_wrapper::get_type() const
{
    return m_type;
}

inline const char* Mdl_definition_wrapper::get_name() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_name();

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_name();

    } else
        return 0;
}

inline const char* Mdl_definition_wrapper::get_module() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_module();

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_module();

    } else
        return 0;
}

inline bool Mdl_definition_wrapper::is_exported() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->is_exported();

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->is_exported();

    } else
        return false;
}

inline Uint32 
               Mdl_definition_wrapper::get_parameter_count() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_count();

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_count();

    } else
        return 0;
}

inline const char* Mdl_definition_wrapper::get_parameter_name( Uint32 index) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_name( index);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_name( index);

    } else
        return 0;
}

inline Sint32 
               Mdl_definition_wrapper::get_parameter_index( const char* name) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_index( name);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_index( name);

    } else
        return 0;
}

inline const char* Mdl_definition_wrapper::get_parameter_type( Uint32 index) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_type( index);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_type( index);

    } else
        return 0;
}

inline const char* Mdl_definition_wrapper::get_parameter_type( const char* name) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_type( name);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_type( name);

    } else
        return 0;
}

inline bool Mdl_definition_wrapper::is_parameter_type_uniform( Uint32 index) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->is_parameter_type_uniform( index);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->is_parameter_type_uniform( index);

    } else
        return false;
}

inline bool Mdl_definition_wrapper::is_parameter_type_uniform( const char* name) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->is_parameter_type_uniform( name);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->is_parameter_type_uniform( name);

    } else
        return false;
}

inline const char* Mdl_definition_wrapper::get_argument_type( Uint32 index) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_argument_type( index);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_argument_type( index);

    } else
        return 0;
}

inline const char* Mdl_definition_wrapper::get_argument_type( const char* name) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_argument_type( name);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_argument_type( name);

    } else
        return 0;
}

inline const char* Mdl_definition_wrapper::get_return_type() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        return 0;

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_return_type();

    } else
        return 0;
}


inline bool Mdl_definition_wrapper::is_return_type_varying() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        return false;

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->is_return_type_varying();

    } else
        return false;
}

template <class T>
Sint32 
               Mdl_definition_wrapper::get_default( Uint32 index, T& value) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        base::Handle< const IAttachable> argument( md->get_default( index));
        if( !argument.is_valid_interface())
            return -2;
        Sint32 result = mi::get_value( argument.get(), value);
        return result == 0 ? 0 : -3;

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        base::Handle< const IAttachable> argument( fd->get_default( index));
        if( !argument.is_valid_interface())
            return -2;
        Sint32 result = mi::get_value( argument.get(), value);
        return result == 0 ? 0 : -3;

    } else
        return -1;
}

template <class T>
Sint32 
               Mdl_definition_wrapper::get_default( const char* name, T& value) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        base::Handle< const IAttachable> argument( md->get_default( name));
        if( !argument.is_valid_interface())
            return -2;
        Sint32 result = mi::get_value( argument.get(), value);
        return result == 0 ? 0 : -3;

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        base::Handle< const IAttachable> argument( fd->get_default( name));
        if( !argument.is_valid_interface())
            return -2;
        Sint32 result = mi::get_value( argument.get(), value);
        return result == 0 ? 0 : -3;

    } else
        return -1;
}

inline const IAttribute_container* Mdl_definition_wrapper::get_annotations() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_annotations();

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_annotations();

    } else
        return 0;
}

inline const IAttribute_container* Mdl_definition_wrapper::get_parameter_annotations(
    Uint32 index) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_annotations( index);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_annotations( index);

    } else
        return 0;
}

inline const IAttribute_container* Mdl_definition_wrapper::get_parameter_annotations(
    const char* name) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        return md->get_parameter_annotations( name);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_parameter_annotations( name);

    } else
        return 0;
}

inline const IAttribute_container* Mdl_definition_wrapper::get_return_annotations() const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        return 0;

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        return fd->get_return_annotations();

    } else
        return 0;
}

inline IScene_element* Mdl_definition_wrapper::create_instance(
    const IAttribute_container* arguments, Sint32* errors) const
{
    if( m_type == ELEMENT_TYPE_MDL_MATERIAL_DEFINITION) {

        base::Handle< const IMdl_material_definition> md(
            m_access->get_interface<IMdl_material_definition>());
        if( arguments)
            return md->create_material_instance( arguments, errors);

        base::Handle< IAttribute_container> local_arguments(
            m_transaction->create<IAttribute_container>( "Attribute_container"));
        Uint32 count = md->get_parameter_count();
        for( Uint32 i = 0; i < count; ++i) {
            base::Handle< const IData> default_( md->get_default( i));
            if( !default_) {
                const char* name = md->get_parameter_name( i);
                const char* type = md->get_argument_type( i);
                base::Handle< IAttachable> argument(
                    local_arguments->create_attribute<IAttachable>( name, type));
            }
        }
        return md->create_material_instance( local_arguments.get(), errors);

    } else if( m_type == ELEMENT_TYPE_MDL_FUNCTION_DEFINITION) {

        base::Handle< const IMdl_function_definition> fd(
            m_access->get_interface<IMdl_function_definition>());
        if( arguments)
            return fd->create_function_call( arguments, errors);

        base::Handle< IAttribute_container> local_arguments(
            m_transaction->create<IAttribute_container>( "Attribute_container"));
        Uint32 count = fd->get_parameter_count();
        for( Uint32 i = 0; i < count; ++i) {
            base::Handle< const IData> default_( fd->get_default( i));
            if( !default_) {
                const char* name = fd->get_parameter_name( i);
                const char* type = fd->get_argument_type( i);
                base::Handle< IAttachable> argument(
                    local_arguments->create_attribute<IAttachable>( name, type));
            }
        }
        return fd->create_function_call( local_arguments.get(), errors);

    } else
        return 0;

}

} // namespace neuraylib

#ifdef MI_NEURAYLIB_DEPRECATED_NAMESPACE_MI_TRANSITION
using neuraylib::ELEMENT_TYPE_MDL_FUNCTION_DEFINITION;
using neuraylib::ELEMENT_TYPE_MDL_MATERIAL_DEFINITION;
using neuraylib::Element_type;
using neuraylib::IAttribute_container;
using neuraylib::IMdl_function_definition;
using neuraylib::IMdl_material_definition;
using neuraylib::IScene_element;
using neuraylib::Mdl_definition_wrapper;
#endif // MI_NEURAYLIB_DEPRECATED_NAMESPACE_MI_TRANSITION

} // namespace mi

#endif // MI_NEURAYLIB_MDL_DEFINITION_WRAPPER_H

Namespaces

namespace 
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH. More...
namespace 
Namespace for the neuray API. More...

Classes

class 
A wrapper around the interfaces for MDL material and function definitions. More...