Ruby API for GR Framework!!

·FOSS, GSoC, GSoC2018, Ruby

First published on my old blog, The Silent Monk’s Retreat.

GR Framework is a universal framework for cross-platform visualization applications. It offers developers a compact, portable and consistent graphics library for their programs. Applications range from publication quality 2D graphs to the representation of complex 3D scenes.

The above paragraph is directly picked up from GR Framework documentation and encapsulates the spirit of GR. You can read more about GR Framework here.

So far GR Framework is available for C, Python, and Julia.
For my GSoC 2018 project, I extended its support to Ruby using C Extensions.

Before we begin using it we first need to install it (duh).

the easiest way to install GR framework is to simply select your system configuration hereand following the process mentioned.
but instead of installing python-gr replace it with just gr.
(unless you also want GR Framework for Python)

This will install GR Framework in /usr . So we will create a symbolic link for the same by:

sudo ln -s /usr/gr /usr/local/gr 

You can also install using it via pre-built binaries but that does not install fonts for text and installing it from source leads to an X11 error. (Trust me I tried)

Now that you have GR Framework installed. You need to install the C extensions to use it with Ruby.

To do the same, you first need to clone my repo using:

git clone https://github.com/pgtgrly/GRruby-extension
cd GRruby-extension

Install the gems using:

gem install rspec ZenTest rspec-autotest hoe rake-compiler

Make sure that Your Rspec version is 3.x.
And compile it using:

rake compile

Congratulations you can now used GR Framework with Ruby!
Let’s take it for a test drive!
Whip out your terminal and make sure your working directory in GRruby-extensions.
Then write your Ruby code in a file (say hello_world.rb)

require "./lib/grruby.rb"
GR.setviewport(0.1, 0.95, 0.1, 0.95)
GR.setwindow(-10, 10, -10, 10)
GR.setspace(-0.5, 0.5, 0, 90)
GR.setmarkersize(1.0)
GR.setmarkertype(-1)
GR.setcharheight(0.012)
GR.settextalign(2, 0)
GR.settextfontprec(103, 0)
GR.setfillintstyle(1)
j=1;
while j<50;
  i=1
  x=[0]
  y=[0.25*(-1)**j]
  GR.axes(1, 1, 0.0, 0.0, 1, 1, 0.01)
  GR.updatews()
  while (i<10) do 
    x.insert(-1,x[-1]+1*(-1)**j)
    y.insert(-1,y[-1]*1.5)
    GR.setfillcolorind(10*j+i) 
    if x.size >2 and y.size >2
      GR.fillarea(x,y)
    end
    GR.polyline(x,y)
    GR.polymarker(x,y)
    sleep(0.5)
    GR.updatews()
    i+=1    
  end
  sleep(1)
  GR.clearws()
  j+=1
end
puts("done")
hold=gets

Save it and run it using Ruby interpreter.

ruby hello_world.rb

Your output would look something like:

GR.gif

Now Rejoice!!
I have tried to follow the Python GR API as an example and the same can be referenced.
There are more examples that can be found in the examples folder (I will be updating it with more examples soon, as well as change the API to reflect the dynamic Arrays that are not present in C).

Post Script: I have tested the above in a fresh Ubuntu-install and it is working fine, please let me know about any compilation issues in any other OS.