module Vizkit::VizkitPluginLoaderExtension

Attributes

connected_transformation_producers[R]

The set of transformation producers connected to this widget so far

This is a mapping from (task_name, port_name) pairs to a boolean. The boolean is false if the port ever sent wrong data (i.e. of an unexpected frame transform), and true otherwise

port_frame_associations[R]

An association between (task_name, port_name) pairs to the frame in which the data produced by the port is expressed

Public Instance Methods

apply_transformer_configuration(conf, apply_examples: true) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 357
def apply_transformer_configuration(conf, apply_examples: true)
    conf.each_static_transform do |trsf|
        Vizkit.debug "pushing static transformation #{trsf.from} => #{trsf.to}"
        # target and source are exchanged because the transformer defines its transformations as Source_In_Target
        setTransformation(trsf.to.dup,trsf.from.dup,trsf.translation.to_qt,trsf.rotation.to_qt)
    end
    conf.each_dynamic_transform do |trsf|
        listen_to_transformation_producer(trsf)
    end
    if apply_examples
        conf.each_example_transform do |trsf|
            setTransformation(trsf.to.dup, trsf.from.dup, trsf.translation.to_qt, trsf.rotation.to_qt)
        end
    end
end
createPlugin(lib_name, plugin_name = nil, plugin_spec = PluginSpec.new(plugin_name)) click to toggle source

Creates a vizkit plugin object

Plugins, whose list is returned by custom_plugins, are created with

createPlugin(lib_name, plugin_name)

Where lib_name is the name of the plugin library without the “lib” and “-viz.so” parts. For instance, a package that installs a library called libvfh_star-viz.so will do

createPlugin("vfh_star", "VFHTree")

Moreover, if the library only provides one plugin, the plugin name can be omitted

createPlugin("vfh_star")
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 236
def createPlugin(lib_name, plugin_name = nil, plugin_spec = PluginSpec.new(plugin_name))
    path = findPluginPath(lib_name)
    if !path
        Kernel.raise "cannot find lib#{lib_name}-viz.so in VIZKIT_PLUGIN_RUBY_PATH."
    end
    plugin = load_plugin(path)
    if !plugin_name
        if plugin.getAvailablePlugins.size > 1
            Kernel.raise "#{lib_name} either defines multiple plugins (and you must select one explicitely)"
        else
            plugin_name = plugin.getAvailablePlugins.first
        end
    end

    if !plugin.getAvailablePlugins.include?(plugin_name)
        if plugin.getAvailablePlugins.include?("#{plugin_name}Visualization")
            plugin_name = "#{plugin_name}Visualization"
        else
            Kernel.raise "library #{lib_name} does not have any vizkit plugin called #{plugin_name}, available plugins are: #{plugin.getAvailablePlugins.join(", ")}"
        end
    end
    plugin = plugin.createPlugin(plugin_name)
    addPlugin(plugin)

    plugin.extend VizkitPluginExtension
    plugin.load_adapters(plugin_spec)

    plugin.extend(QtTypelibExtension)
    extendUpdateMethods(plugin,plugin_spec)
    plugin
end
custom_plugins() click to toggle source

For backward compatibility only

# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 215
def custom_plugins
    plugins
end
extendUpdateMethods(plugin,plugin_spec) click to toggle source
Calls superclass method
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 388
def extendUpdateMethods(plugin,plugin_spec)
    fcts = plugin_spec.find_all_callbacks(:callback_type => :display).find_all{|spec|spec.respond_to?(:to_sym)}
    if !fcts
        Vizkit.info "no callback functions registered for Vizkit3D plugin #{plugin_spec.plugin_name} (c++ name: #{plugin_spec.cplusplus_name}) from #{plugin_spec.lib_name}"
        return
    end
    fcts.each do |fct|
        # define the code block for the new method
        block = lambda do |*args|
            if args.size < 1 || args.size > 2
                Vizkit.error "#{fct.to_s}: wrong parameters"
                puts "usage: #{fct.to_s}(data [, port_name]), the port_name is optional but necessary to receive transformations."
                return
            end
            data = args[0] if args[0]
            port_name = args[1] if args[1]

            #inform widget about the frame for the plugin
            widget = Vizkit.vizkit3d_widget
            if frame_name = widget.port_frame_associations[port_name]
                Vizkit.debug "#{port_name}: associated to the #{frame_name} frame for plugin #{plugin}"
                widget.setPluginDataFrame(frame_name, plugin)
            else
                Vizkit.debug "no known frame for #{port_name}, displayed by widget #{plugin_spec.plugin_name} (plugin #{plugin})"
            end
            super data
        end

        plugin.class.instance_eval do 
            define_method(fct.to_sym, block)
        end
    end
end
findPluginPath(plugin_name) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 165
def findPluginPath(plugin_name)
    path = if !ENV['VIZKIT_PLUGIN_RUBY_PATH']
               "/usr/local/lib:/usr/lib:/usr/local/lib"
           else
               ENV['VIZKIT_PLUGIN_RUBY_PATH']
           end
    path.split(':').each do |path|
        p = File.join(path, "lib#{plugin_name}-viz.so")
        return p if File.file?(p)
        p = File.join(path, "lib#{plugin_name}-viz.bundle")
        return p if File.file?(p)
        p = File.join(path, "lib#{plugin_name}-viz.dylib")
        return p if File.file?(p)
    end
    nil
end
grid() click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 139
def grid
    @grid
end
initialize_vizkit_extension() click to toggle source
Calls superclass method
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 116
def initialize_vizkit_extension
    super
    @use_transformer_broadcaster = true
    @load_transformer_config_from_broadcaster = true
    Vizkit.ensure_orocos_initialized
    if !@connected_to_broadcaster
        @port_frame_associations ||= Hash.new
        @connected_transformation_producers ||= Hash.new
        task_name, port_name = Vizkit.vizkit3d_transformer_broadcaster_name
        begin
            Orocos.load_typekit 'transformer'
            if task_name
                Vizkit.connect_port_to task_name, port_name, self
                @connected_to_broadcaster = true
            end
        rescue Orocos::NotFound => e
            Vizkit.warn e
        end
    end
    @grid = createPlugin("vizkit3d","GridVisualization")
    @grid.setPluginName("Grid")
end
isGridEnabled() click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 147
def isGridEnabled
    @grid.enabled
end
listen_to_transformation_producer(trsf) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 326
def listen_to_transformation_producer(trsf)
    return if @connected_transformation_producers.has_key?(trsf.producer)

    task, *port = trsf.producer.split('.')
    port = port.join(".")
    Vizkit.debug "connecting producer task #{task}, port #{port} for #{trsf.from} => #{trsf.to}"
    producer_name = task.gsub(/.*\//, '')
    Vizkit.connect_port_to producer_name, port do |data, port_name|
        if data.sourceFrame != trsf.from || data.targetFrame != trsf.to
            if @connected_transformation_producers[trsf.producer]
                Vizkit.warn "#{task}.#{port} produced a transformation for"
                Vizkit.warn "    #{data.sourceFrame} => #{data.targetFrame},"
                Vizkit.warn "    but I was expecting #{trsf.from} => #{trsf.to}"
                Vizkit.warn "  I am ignoring this transformation. You will get this message only once,"
                Vizkit.warn "  but get a notification if the right transformation is received later."
                @connected_transformation_producers[trsf.producer] = false
            end
        else
            if !@connected_transformation_producers[trsf.producer]
                Vizkit.warn "received the expected transformation from #{task}.#{port}"
                @connected_transformation_producers[trsf.producer] = true
            end
            Vizkit.debug "pushing dynamic transformation #{data.sourceFrame} => #{data.targetFrame}"
            # target and source are exchanged because the transformer defines its transformations as Source_In_Target
            setTransformation(data.targetFrame.dup,data.sourceFrame.dup,data.position.to_qt,data.orientation.to_qt)
        end
        data
    end
    @connected_transformation_producers[trsf.producer] = true
end
load_plugin(path) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 151
def load_plugin(path)
    loader = Qt::PluginLoader.new(path)
    loader.load
    if !loader.isLoaded
        Vizkit.error "Cannot load Vizkit3D pluign #{path}. Library seems to be incompatible to the qt loader. Have you created a plugin factory?"
        Kernel.raise "Cannot load #{path}. Last error is: #{loader.errorString}"
    end
    plugin_instance = loader.instance
    if plugin_instance == nil
        Kernel.raise "Could not load plugin #{loader.fileName}. Last error is #{loader.errorString}"
    end
    plugin_instance
end
load_transformer_configuration(path) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 373
def load_transformer_configuration(path)
    conf = Transformer::Configuration.new
    conf.load(path)
    apply_transformer_configuration(conf)
end
plugins() click to toggle source

Returns the list of plugins that are available

The returned value is an array of pairs [lib_name, plugin_name]

# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 185
def plugins
    libs = Array.new
    path = if !ENV['VIZKIT_PLUGIN_RUBY_PATH']
               "/usr/local/lib:/usr/lib"
           else
               ENV['VIZKIT_PLUGIN_RUBY_PATH']
           end
    path.split(':').each do |path|
        next unless File::directory? path
        Dir::foreach(path) do |lib|
            if lib =~ /^lib(.*)-viz.so$/
                qt_plugin =
                    begin load_plugin(File.join(path, lib))
                    rescue Exception => e
                        STDERR.puts "WARN: cannot load vizkit plugin library #{File.join(path, lib)}: #{e.message}"
                        next
                    end

                libname = $1
                adapters = qt_plugin.getAvailablePlugins
                adapters.each do |name|
                    libs << [libname, name]
                end
            end
        end
    end
    libs
end
pushTransformerConfiguration(data) click to toggle source

@deprecated renamed to #push_transformer_configuration

# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 280
def pushTransformerConfiguration(data)
    push_transformer_configuration(data)
end
push_transformer_configuration(data) click to toggle source

Updates the connections and internal configuration of the Vizkit3D widget to use the transformer configuration information in data

data is supposed to be a transformer/ConfigurationState value

# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 303
def push_transformer_configuration(data)
    if load_transformer_config_from_broadcaster?
        # Convert the broadcaster's configuration to a transformer configuration
        # object
        conf = Transformer::Configuration.new
        # Push the data to the underlying transformer
        data.static_transformations.each do |trsf|
            conf.static_transform trsf.position, trsf.orientation,
                trsf.sourceFrame => trsf.targetFrame
        end
        data.port_transformation_associations.each do |producer|
            conf.dynamic_transform "#{producer.task}.#{producer.port}",
                producer.from_frame => producer.to_frame
        end
        apply_transformer_configuration(conf)
    end

    self.port_frame_associations.clear
    data.port_frame_associations.each do |data_frame|
        port_frame_associations["#{data_frame.task}.#{data_frame.port}"] = data_frame.frame
    end
end
setGrid(val) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 143
def setGrid(val)
    @grid.enabled = val
end
update(data, port_name) click to toggle source
# File lib/vizkit/cplusplus_extensions/vizkit_widget.rb, line 379
def update(data, port_name)
    if @connected_to_broadcaster
        if use_transformer_broadcaster? && data.class == Types.transformer.ConfigurationState
            pushTransformerConfiguration(data)
            return
        end
    end
end