class Object

Public Instance Methods

addTextWrapper(text, location, color, persistent) click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_widget.rb, line 57
def addTextWrapper(text, location, color, persistent)
    locationMap = {:topleft => 0,
        :topright => 1,
        :bottomleft => 2,
        :bottomright => 3}
    addText(text, locationMap[location], color, persistent)
end
config(value,options) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 198
def config(value,options)
    @log_replay = if value.respond_to? :task
                      if value.task.respond_to? :log_replay
                          value.task.log_replay
                      end
                  end
    @options = options.merge(@options)
    if value.type_name == "/base/samples/SonarBeam"
        if !@graphs.empty?
            puts "Cannot plot SonarBeam because plot is already used!"
            return :do_not_connect
        else
            @options[:multi_use_menu] = false
            getXAxis.setLabel "Bin Number"
        end
    elsif value.type_name =~ /\/std\/vector</ || value.type_name == "/base/samples/LaserScan"
        if !@graphs.empty?
            puts "Cannot plot std::vector because plot is already used!"
            return :do_not_connect
        else
            @options[:multi_use_menu] = false
            getXAxis.setLabel "Index"
        end
    end
    subfield = Array(options[:subfield]).join(".")
    subfield = "." + subfield if !subfield.empty?
    graph2(value.full_name+subfield) if value.respond_to? :full_name
end
corba_error() click to toggle source
# File bin/rock-display, line 57
def corba_error
    Vizkit.error "Corba error. Maybe Orocos is not initialized"
    Vizkit.error "or the corba name service is answering on a wrong network interface."
    Vizkit.error "Try to reset the nameserver and to disable all virtual network interfaces."
end
default_options() click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 10
def default_options()
    options = Hash.new
    options[:time_overlay] = true
    options[:fps_overlay] = true
    options[:display_first] = true
    options[:openGL] = true
    options[:use_typelib_adapter] = true
    options
end
display(frame,port_name) click to toggle source

display is called each time new data are available on the orocos output port this functions translates the orocos data struct to the widget specific format

# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 72
def display(frame,port_name)
    init

    if @options[:time_overlay] and  @isMinimized == false
        if frame.time.instance_of?(Time)
            time = frame.time
        else
            time = Time.at(frame.time.seconds,frame.time.microseconds)
        end
        @time_overlay_object.setText(time.strftime("%b %d %Y %H:%M:%S"))
    end
    if @options[:fps_overlay] and @isMinimized == false
        stat = ''
        stat_valid = ''
        stat_invalid = ''
        frame.attributes.each do |x|
            stat =x.data_.to_s if x.name_ == 'StatFps'
            stat_valid =x.data_.to_s if x.name_ == 'StatValidFps'
            stat_invalid =x.data_.to_s if x.name_ == 'StatInValidFps'
        end
        @fps_overlay_object.setText(" stat fps: #{stat},  valid #{stat_valid}, invalid #{stat_invalid}")
    end
    if @options[:use_typelib_adapter]
      @typelib_adapter ||= Vizkit::TypelibQtAdapter.new(self)
      if !@typelib_adapter.call_qt_method("addFrame",frame)
          Vizkit.warn "Cannot reach method addFrame. Disabling TypelibQtAdapter for widget #{self} and falling back to field access."
          @options[:use_typelib_adapter] = false
      end
    else
      addRawImage(frame.frame_mode.to_s,frame.pixel_size,frame.size.width,frame.size.height,frame.image.to_byte_array[8..-1],frame.image.size)
    end
    update2
end
display2(frame_pair,port_name) click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 30
def display2(frame_pair,port_name)
  init
  frame = @options[:display_first] == true ? frame_pair.first : frame_pair.second
  display(frame,port_name)
end
from_mat(mat) click to toggle source
# File lib/type_specialize.rb, line 67
def from_mat(mat)
    if(mat.type == OpenCV::Cv::CV_8UC1)
        self.frame_mode = :MODE_GRAYSCALE
        self.pixel_size = 1
        self.row_size = mat.cols
        self.frame_status = :STATUS_VALID
        self.data_depth = 8
    elsif(mat.type == OpenCV::Cv::CV_8UC3)
        self.frame_mode = :MODE_RGB
        self.pixel_size = 3
        self.row_size = mat.cols*3
        self.frame_status = :STATUS_VALID
        self.data_depth = 8
    else
        raise "Unsupported mat type"
    end
    self.size.height = mat.rows
    self.size.width = mat.cols
    self.time = Time.now
    self.image.raw_memcpy(mat.data.address,mat.total*self.pixel_size)
end
graph2(name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 227
def graph2(name)        
    if(!@graphs.has_key?(name))
        axis = if @options[:use_y_axis2] then getYAxis2
               else getYAxis
               end

        axis.setLabel(name.split(".").last)
        graph = addGraph(getXAxis(),axis)
        graph.setName(name)
        graph_style(graph,@options[:plot_style])
        graph.addToLegend
        
        if color = @options[:colors][@color_next_idx]
            graph.setPen(Qt::Pen.new(Qt::Brush.new(color),1))
        end
        
        @color_next_idx = (@color_next_idx + 1) % @options[:colors].count            
        
        @graphs[name] = graph
    end

    @graphs[name]
end
graph_style(graph,style) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 178
def graph_style(graph,style)
    if style == :Dot
        graph.setLineStyle(Qt::CPGraph::LSNone)
        graph.setScatterStyle(Qt::CPGraph::SSDot)
    else
        graph.setLineStyle(Qt::CPGraph::LSLine)
        graph.setScatterStyle(Qt::CPGraph::SSNone)
    end
    @needs_update = true
end
init() click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 41
def init 
  if !defined? @init
    @options ||= default_options
    openGL(@options[:openGL])
    @time_overlay_object = addText(-150,-5,0,"")
    @time_overlay_object.setColor(Qt::Color.new(255,255,0))
    @time_overlay_object.setPosFactor(1,1);
    @time_overlay_object.setBackgroundColor(Qt::Color.new(0,0,0,40))
    @fps_overlay_object = addText(5,-5,0,"   ")
    @fps_overlay_object.setColor(Qt::Color.new(255,255,0))
    @fps_overlay_object.setBackgroundColor(Qt::Color.new(0,0,0,40))
    @fps_overlay_object.setPosFactor(0,1);
    @folder_path ||= nil
    @isMinimized = false
    connect(SIGNAL("activityChanged(bool)"),self,:setActive)
    @init = true
  end
end
initialize_vizkit_extension() click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 56
def initialize_vizkit_extension
    @options = default_options
    @graphs = Hash.new
    @time = nil 
    @timer = Qt::Timer.new
    @needs_update = false
    @timer.connect(SIGNAL"timeout()") do 
        replot if @needs_update
        @needs_update = false
    end
    @timer.start(1000*@options[:update_period])
    @color_next_idx = 0
    
    getLegend.setVisible(true)
    getXAxis.setLabel("Time in sec")
    setTitle("Rock-Plot2d")
    
    
    self.connect(SIGNAL('mousePressOnPlotArea(QMouseEvent*)')) do |event|
        if event.button() == Qt::RightButton 
            #show pop up menue 
            menu = Qt::Menu.new(self)
            action_scrolling = Qt::Action.new("AutoScrolling", self)
            action_scrolling.checkable = true
            action_scrolling.checked = @options[:auto_scrolling]
            menu.add_action(action_scrolling)
            if @options[:multi_use_menu]
                action_reuse = Qt::Action.new("Reuse Widget", self)
                action_reuse.checkable = true
                action_reuse.checked = @options[:reuse]
                menu.add_action(action_reuse)
                action_use_y2 = Qt::Action.new("Use 2. Y-Axis", self)
                action_use_y2.checkable = true
                action_use_y2.checked = @options[:use_y_axis2]
                menu.add_action(action_use_y2)
                action_plotdot = Qt::Action.new("'dot' style", self)
                action_plotdot.checkable = true
                action_plotdot.checked = @options[:plot_style] == :Dot
                menu.add_action(action_plotdot)
                action_plotline = Qt::Action.new("'line' style", self)
                action_plotline.checkable = true
                action_plotline.checked = @options[:plot_style] == :Line
                menu.add_action(action_plotline)
            end
            menu.addSeparator
            action_saving = Qt::Action.new("Save to File", self)
            menu.add_action(action_saving)
            if @options[:is_time_plot]
                menu.addSeparator
                action_timestamp = Qt::Action.new("Show timestamps", self)
                action_timestamp.checkable = true
                action_timestamp.checked = @options[:plot_timestamps]
                menu.add_action(action_timestamp)
                action_sample_period = Qt::Action.new("Show sample period", self)
                action_sample_period.checkable = true
                action_sample_period.checked = !@options[:plot_timestamps]
                menu.add_action(action_sample_period)
            end

            action = menu.exec(mapToGlobal(event.pos))
            if(action == action_scrolling)
                @options[:auto_scrolling] = !@options[:auto_scrolling]
                update_zoom_range_flag(!@options[:auto_scrolling], @options[:use_y_axis2])
            elsif(action == action_reuse)
                @options[:reuse] = !@options[:reuse]
            elsif(action == action_use_y2)
                update_zoom_range_flag(false, @options[:use_y_axis2])
                @options[:use_y_axis2] = !@options[:use_y_axis2]
                if @options[:use_y_axis2]
                    getYAxis2.setVisible(true)
                end
                update_zoom_range_flag(!@options[:auto_scrolling], @options[:use_y_axis2])
             elsif(action == action_plotdot)
                plot_style(:Dot)
            elsif(action == action_plotline)
                plot_style(:Line)
            elsif action == action_saving
                file_path = Qt::FileDialog::getSaveFileName(nil,"Save Plot to Pdf",File.expand_path("."),"Pdf (*.pdf)")
                savePdf(file_path,false,0,0) if file_path
            elsif ((action == action_timestamp) || (action == action_sample_period))
                @options[:plot_timestamps] =! @options[:plot_timestamps]
            end
        end
    end
    
    
    self.connect(SIGNAL('mousePressOnLegendItem(QMouseEvent*, QVariant)')) do |event, itemIdx|
        if event.button() == Qt::RightButton 
            #show pop up menue 
            menu = Qt::Menu.new(self)
            action_remove = Qt::Action.new("remove graph", self)
            menu.add_action(action_remove)
            
            action = menu.exec(mapToGlobal(event.pos))
            
            if(action == action_remove)                    
                # note: we assume all graphs have a corresponding 
                # legend item with the same index (true for this widget)
                graph = getGraph(itemIdx.to_i())
                
                unless graph == 0 || graph.nil?
                    
                    while true
                        cur_port = connection_manager().find_port_by_name(graph.name)
                        
                        if cur_port
                            connection_manager().disconnect(cur_port,  keep_port: false)
                        else
                            break
                        end 
                    end
                    
                    @graphs.delete graph.name
                    removeGraph(itemIdx.to_i())
                    @needs_update = true
                end
            end
        end
    end
    
end
multi_value?() click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 251
def multi_value?
    @options[:reuse] && @options[:multi_use_menu] 
end
options(hash = Hash.new) click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 36
def options(hash = Hash.new)
  @options ||= default_options
  @options.merge!(hash)
end
plot_style(style) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 189
def plot_style(style)
    if @options[:plot_style] != style 
        @options[:plot_style] = style
        @graphs.each_value do |graph|
            graph_style(graph,style)
        end
    end
end
rename_graph(old_name,new_name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 255
def rename_graph(old_name,new_name)
    graph = @graphs[old_name]
    if graph           
        graph.setName(new_name)
        @graphs[new_name] = @graphs[old_name]
        @graphs.delete old_name
    end
end
replay(log) click to toggle source
# File bin/rock-replay, line 94
def replay(log)
    unless log.replay?
        if @search_filter.empty?
            Vizkit.warn "There are no samples in the log file!"
        else
            Vizkit.warn "No ports were selected for replay (ports with no samples cannot be selected)!"
        end
        exit 0
    end
    puts "Aligning streams. This can take a long time"
    Vizkit.control log, :show_marker=>true, :marker_type=>"/logger/Marker"
    Vizkit.exec
end
result(a,b) click to toggle source
# File test/test_vizkit.rb, line 52
def result(a,b)
end
save(path) click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 20
def save(path)
      saveImage2(path)
end
save_frame(frame,path) click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 24
def save_frame(frame,path)
  format = File.extname(path).sub!(/\./,"").upcase!
  format = "PNG" unless format
  saveImage3(frame.frame_mode.to_s,frame.pixel_size,frame.size.width,frame.size.height,frame.image.to_byte_array[8..-1],path,format)
end
save_images_to(folder) click to toggle source

save all images which are displayed to the given folder

# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 6
def save_images_to(folder)
    @folder_path = folder
end
setActive(active) click to toggle source
# File lib/vizkit/cplusplus_extensions/image_view_old_widget.rb, line 60
def setActive(active)
    if active == true
        @isMinimized = false
    else
        @fps_overlay_object.setText("")
        @time_overlay_object.setText("")
        @isMinimized = true
    end
end
setXTitle(value) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 42
def setXTitle(value)
    getXAxis.setLabel(value.to_s)
end
setYTitle(value) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 46
def setYTitle(value)
    getYAxis.setLabel(value.to_s)
end
set_x_axis_scale(start,stop) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 328
def set_x_axis_scale(start,stop)
    getXAxis.setRange(start,stop)
end
set_y_axis_scale(start,stop) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 332
def set_y_axis_scale(start,stop)
    getYAxis.setRange(start,stop)
end
start_simple_cov(name) click to toggle source
# File test/test_helper.rb, line 10
def start_simple_cov(name)
    if defined? SimpleCov
        if !@simple_cov_started
            puts name
            SimpleCov.command_name name
            @simple_cov_started = true
            SimpleCov.root(File.join(File.dirname(__FILE__),".."))
            SimpleCov.start do 
                add_group "Vizkit.rb" do |src|
                    nil != (src.filename =~ /lib\/vizkit\/\w*.rb$/)
                end
                add_group "C++ Widget Extension","lib/vizkit/cplusplus_extensions"
                add_group "Buildin Ruby Widgets", "lib/vizkit/widgets"
            end
        end
    end
end
time() click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 32
def time 
    time = if @log_replay
               @log_replay.time
           else
               Time.now
           end
    @time ||= time 
    time
end
to_file(filename) click to toggle source
# File lib/type_specialize.rb, line 14
def to_file(filename)
    OpenCV::Cv::imwrite(self.to_mat,filename)
end
to_mat() click to toggle source
# File lib/type_specialize.rb, line 18
def to_mat
    channels = if self.frame_mode == :MODE_UNDEFINED
                   0
               elsif(self.frame_mode == :MODE_BAYER)
                   1
               elsif(self.frame_mode == :MODE_BAYER_RGGB)
                   1
               elsif(self.frame_mode == :MODE_BAYER_BGGR)
                   1
               elsif(self.frame_mode == :MODE_BAYER_GBRG)
                   1
               elsif(self.frame_mode == :MODE_BAYER_GRBG)
                   1
               elsif(self.frame_mode == :MODE_GRAYSCALE)
                   1
               elsif(self.frame_mode == :MODE_UYVY)
                   1
               elsif(self.frame_mode == :MODE_RGB)
                   3
               elsif(self.frame_mode == :MODE_BGR)
                   3
               elsif(self.frame_mode == :MODE_RGB32)
                   4
               else
                   raise "Unsupported frame_mode";
               end
    type = if(channels == 1)
               if(self.pixel_size == 1)
                   OpenCV::Cv::CV_8UC1;
               elsif(self.pixel_size == 2)
                   OpenCV::Cv::CV_16UC1;
               else
                   raise "Unsupported pixel_size"
               end
           elsif channels == 3
               if(self.pixel_size == 3)
                   OpenCV::Cv::CV_8UC3;
               elsif(self.pixel_size == 6)
                   OpenCV::Cv::CV_16UC3;
               else
                   raise "Unsupported pixel_size"
               end
           else
                raise "Unsupported number of channels"
           end
    p = FFI::Pointer.new(:char,self.image.contained_memory_id)
    OpenCV::Cv::Mat.new(self.size.height,self.size.width,type,p)
end
update(sample,port_name) click to toggle source
# File lib/vizkit/cplusplus_extensions/artificial_horizon.rb, line 4
def update(sample,port_name)
    if sample.respond_to?(:orientation) # pose and rigid body state
        sample = sample.orientation
    end

    if !sample.kind_of?(Eigen::Quaternion)
        # The base typelib plugin is not loaded, do the convertions by ourselves
        sample = Eigen::Quaternion.new(sample.re, *sample.im.to_a)
    end

    setPitchAngle(sample.pitch)
    setRollAngle(sample.roll)
end
update_angle(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 375
def update_angle(sample,name)
    update sample.rad,name
end
update_custom(name,values_x,values_y) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 336
def update_custom(name,values_x,values_y)
    graph = graph2(name)
    graph.addData(values_x,values_y)        
    if @options[:auto_scrolling] || @options[:auto_scrolling_x]
        getXAxis.setRange(values_x-@options[:xaxis_window],values_x+@options[:pre_xaxis_window])
        graph.rescaleValueAxis(true)                
    end       
    if @options[:auto_scrolling] || @options[:auto_scrolling_y]
        getYAxis.setRange(values_y-@options[:yaxis_window],values_y+@options[:pre_yaxis_window])
        graph.rescaleValueAxis(true)                
    end       
    @needs_update = true
end
update_laser_scan(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 372
def update_laser_scan(sample,name)
    update_vector sample.ranges,name
end
update_orientation(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 279
def update_orientation(sample,name)
    rename_graph(name,name+"_yaw")
    update((sample.yaw)   *(180.00/Math::PI),name+"_yaw")
    update((sample.pitch) *(180.00/Math::PI),name+"_pitch")
    update((sample.roll)   *(180.00/Math::PI),name+"_roll")
end
update_sonar_beam(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 369
def update_sonar_beam(sample,name)
    update_vector sample.beam,name
end
update_time(sample, name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 304
def update_time(sample, name)
    # So that the time related options of the right click menu are not shown for other types
    @options[:is_time_plot] = true
    if @options[:plot_timestamps]
        update(sample.to_f, name)
    else
        update_time_diff(sample, name)
    end
end
update_time_diff(sample, name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 314
def update_time_diff(sample, name)
    # For each data source an entry in the dictionary is created
    if @previous_time == nil
        @previous_time = {}
    end
    if @previous_time[name] == nil 
        @previous_time[name] = sample.to_f
    end
    difference = sample.to_f - @previous_time[name]
    @previous_time[name] = sample.to_f
    update(difference, name)
end
update_vector(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 350
def update_vector(sample,name)
    if sample.size() > @options[:max_points]
        Vizkit.logger.warn "Cannot plot #{name}. Vector is too big"
        return
    end
    graph = graph2(name)
    graph.clearData
    sample.to_a.each_with_index do |value,index|
        graph.addData(index,value)
    end
    if @options[:auto_scrolling] || @options[:auto_scrolling_x]
        graph.rescaleKeyAxis(false)
    end
    if @options[:auto_scrolling] || @options[:auto_scrolling_y]
        graph.rescaleValueAxis(false)
    end
    @needs_update = true
end
update_vector3d(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 286
def update_vector3d(sample,name)
    rename_graph(name,name+"_x")
    update(sample[0],name+"_x")
    update(sample[1],name+"_y")
    update(sample[2],name+"_z")
end
update_vectorXd(sample,name) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 293
def update_vectorXd(sample,name)
    if (sample.size() == 1)
        update(sample[0], name)
    else
        rename_graph(name,name+"[0]")
        for i in (0..sample.size()-1)
            update(sample[i], name+"["+i.to_s()+"]")
        end
    end
end
update_zoom_range_flag(flag, use_2nd_axis) click to toggle source
# File lib/vizkit/cplusplus_extensions/plot2d.rb, line 50
def update_zoom_range_flag(flag, use_2nd_axis)
    y_axis = Hash[true => 1, false => 0][use_2nd_axis]
    setZoomAble flag, y_axis
    setRangeAble flag, y_axis
end