Creating graphics/visualisations from C++ (Qt) and passing them to R - c++

I have a C++/Qt library that is producing some specialised visualisations; the visualisations are currently produced using Qt's QPainter system and primitives.
I would like to extend my library to produce these visualisations for R.
I already have a bridge between my C++ library and R using Rcpp, that works for exchanging basic data structures e.g. exposing data from the library as data-frames etc.
However I am a bit lost on what the next step should be in order to output R graphics from C++.
If it is possible to use the Qt infrastructure, it would be great as it would improve code reuse. However, if it is not at all possible, I would be willing to rewrite the visualisation code using the primitives of another system. Note that Qt can output SVG.
Do you have any advice on how to use Rcpp (or something else) to achieve this goal. Could you perhaps point me to other C++ libraries that produce visualisations for R? (Note: this does not include graphics libraries written in R for R).

Related

Use Matlab data structures in C++?

I am currently working on a project in C++, and I am actually interested in using Matlab data structures, instead of having to create my own data types (such as matrices, arrays, etc.)
Is there a way to seamlessly use Matlab objects in C++? I don't mind having to run Matlab in the background while my program runs.
EDIT: A starting point is this: http://www.mathworks.co.uk/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html. I will continue reading this.
You can use instead Armadillo C++ maths library; used by NASA, Boeing, Siemens, Deutsche Bank, MIT, CMU, Stanford, etc.
They have good documentation and examples if you are more familiar with MATLAB/OCTAVE
http://arma.sourceforge.net/docs.html#syntax
I would prefer using native C++ library of some sort and not Matlab. This is likely to be faster for both development and execution.
From writing C++ extensions for Matlab I learned one thing: Using Matlab objects in C++ is likely to give you considerable headache.
Matlab data structures are not exposed as C++ classes. Instead, you get pointers that you can manipulate with C-like API functions.
I recommend to use a native C++ library such as Eigen3.
The functionality you are looking at is not really intended to be used as seamless objects. In the past when I have used it I found it much simpler to do the C parts using either native arrays or a third party matrix library and then convert it into a Matlab matrix to return.
Mixing Matlab and C++ is typically done in one of two ways:
Having a C++ program call Matlab to do some specialist processing. This is chiefly useful for rapid development of complex matrix algorithms. You can do this either by calling the full Matlab engine, or by packaging you snippet of Matlab code into a shared library for distribution. (The distributed version packages a distributable copy of the Matlab runtime which is called with your scripts).
Having a Matlab script call a C++ function to do some specialist processing. This is often used to embed C++ implementations of algorithms (such as machine learning models) or to handle specific optimizations.
Both of these use cases have some overhead transferring the data to/from Matlab.
If you are simply looking for some matrix code to use in C++ you would be better off looking into the various C++ matrix libraries, such as the one implemented in Boost.
You can do mixed programming with C++ and Matlab. There are two possible ways:
Call MATLAB Engine directly: Refer to this post for more info. Matlab will run in the background.
Distribute MATLAB into independent shared library: check out here on how to do this (with detail steps and example).

C or C++ for a Robot?

Greetings,
I am trying to decide between C and C++ for my robot. I am a 5+ year veteran of Visual Basic.NET, however I'm going with Linux (Ubuntu) on this robot, and there is a compatibility problem between Linux and the .NET Framework. I want to stick with whichever language I choose for all of my projects, so I want to make sure that I choose the most appropriate one for the task.
For reference, I will describe my current robot in progress and what I am going to do with it. I am in the process of building a full-sized R4 Astromech (yep, I'm one of those guys). I have incorporated a PC motherboard with an Intel Core 2 2.1 GHz processor, 1 GB RAM. I will be using a scratch-built parallel interface card to control the drive motors, head motor, as well as a secondary parallel interface card (going to a second parallel port) which all of the sensors (IR, Ultrasonic Ranging, Visual Recognition via webcam, etc.) will be going to. Commands will be given using speech recognition (currently have a VB.NET scratch-built recognition program that I will be adapting to the new language).
Given the specifications and desired goals listed above, would I be better off with C or C++? I greatly appreciate any suggestions that you might have.
Thanks!
Thaskalas
What do you mean by a compatibility problem? Have you looked at Mono? It's an open-source implementation of the .NET libraries. It's geared toward C# not VB.NET but if you're more comfortable in a .NET environment use that. Speed isn't really an issue here as a Core2Duo is plenty fast for what you need to do.
If Mono won't work for you, I'd recommend C++. There are a lot more libraries out there for C++ (or at least, I am familiar with more, e.g. Boost), which can use most C libraries too. There's no real speed penalty for using C++. While using C wouldn't be bad per-se, C++ has some benefits and no drawbacks, so it's probably the better choice.
I would recommend using ROS. It will let you get started with a sophisticated Inter-Process Communications manager, as well as a large library of sophisticated robotics code, including multiple implementations of SLAM and other critical robotics algorithms. ROS also lets you program in multiple languages, including C, C++, and Python, so you aren't stuck with one language or another down the road.
I would also recommend C++ and ROS. In our company we're migrating to it, because there's so many people working on it, expanding it, and adding lots of cool features.
With this, you can forget about implementing most of the basic low-level stuff and start working on what you intend to research.
It's really easy to set up and start developing.
Since you're running Linux on it, I'd recommend a split approach, where you do the lower-level (device interface, where you may need fast performance) stuff in C (or C++), and the higher level stuff in a modern language like C# (using Mono) or Java, or even Python.
Python especially is hugely expressive, has a large set of libraries, and has a pretty straightforward C interface.
Writing your high-level control stuff in a low-level language like C/C++ will get old fast (IMHO). Robots should be fun!
Have you considered D? It's a fairly new language, is compiled to native code and can link directly to C. (The entire C standard library is even available from D, and bindings to the POSIX API are included in the standard library.) Basically all you need to do to use any C library from D is compile it with a C compiler and translate the function prototypes, constant declarations, etc. in the header file.
D is low-level enough that an experimental kernel is written in it, but has modern features like garbage collection (though manual memory management is still permitted), builtin strings and arrays, and more advanced/easier to use template metaprogramming facilities than C++. The biggest disadvantage is lack of a mature toolchain and libraries for enterprise-y things, but for your purposes that probably doesn't matter. BTW, if you need to do a bunch of matrix manipulation and stuff, there's the SciD project, which provides nice templated wrappers over LAPACK and BLAS.
Use C++. You have the space. You can use it "as a better C" to start with.
C++ is a bigger tool bag; why would you not want that!? You need not use all the tools, but with C you'd have no choice. Most importantly with C++ you have the choice of using both C and C++ third-party libraries.

Any OCaml bindings for GtkGlExt? If not, how can I write them?

I currently have two separate programs: (1) a GTK GUI; (2) a Glut application with openGL 3D graphics. However, I'd like to have the openGL part as some widget embedded in the GUI I have.
I have noticed GtkGlExt is exactly what I need, but unfortunately there are no OCaml bindings mentioned on its web pages.
My questions are:
(1) Do you know if there are any such bindings lost somewhere in cyberspace? (I have found a similar discussion from 2008 so I would hope after 3 years something would have happened.)
(2) If there aren't any bindings, how difficult would it be to produce these bindings and where would it be a good place to start? How much of the original C libraries would I have to undestand? (I haven't done any C calls from OCaml so far.)
(3) Alternatively, do you know of any other out-of-the-box package I could use? (I have attempted to install gtkglarea on OSX but I had errors which I couldn't solve and gave up. I've also tried to install lablgtk2-2.14.0+dfsg but got the errors described here.)
The standard solution is to use the LablGTK's OpenGL support. I know of no bindings to other solutions such as gtkglext. If you want to write your own bindings, in general it isn't overly difficult, does take some work and thought. You'll at least need to understand enough of the underlying C library to safely bridge with OCaml, which varies greatly with the library's interface. Since GtkGLExt is Glib-based, so you'll have the blessing and curse of being able to build on existing experience in the community with binding GLib/GTK libraries, and needing to understand enough of how Lablgtk2 works to make your bindings integrate with it. This is possible, but not exactly trivial and I don't recall seeing any good documentation on binding new GTK libraries to help you get started.
Your error in the other question looks like you're trying to hand-compile - have you tried building the GODI package for lablgtk with OpenGL enabled? It may have patches and/or build options to make it work. Alternatively, does MacPorts, which it looks like you're using from the /opt/local path, have OCaml and lablgtk packages?
Another option: There's another option I commonly use when I need access to facilities from an unbound library. Rather than writing bindings for the library, I'll write the logic needing that library in C and expose a minimal interface back to my OCaml code. In the GtkGLExt case, it's probably just a matter of creating initialization, teardown, and start/stop context calls that do the underlying work with the C API and return the OpenGL context or whatever is necessary. You'll still need to do some work to figure out how to get the right GTK object from the OCaml object, and how to pass back whatever is needed for LablGL to work, but it's typically easier than creating real bindings.

Matrices/Vectors in C++ with the R math standalone library?

All,
I have been playing with the R math standalone library in C++. I quite like being able to generate random numbers and use distribution functions that I am comfortable with from R. My question is: is it possible to use the matrix operations (multiplication, transpose, inverse, Chol, etc) that are available in R in a standalone library? I don't see them in Rmath.h.
If matrix operations are available to use in standalone C++ code, it seems that the R API becomes the perfect open source computational engine. Are people using it in this fashion?
Thanks so much for your guidance and suggestions!
Most, if not all, of the things you mention are provided by LAPACK or BLAS Fortran code that R links to, not something R provides new code for itself.
If you are interested in using C++ with R, look at the Rcpp package by Dirk Eddelbuettel and Romain Francois. Dirk has also written the RcppArmadillo package as an interface to the Armadillo C++ linear algebra library that can do the matrix operations you mention.
Whether this is of use will depend on whether you are wanting to write separate C++ code that is accessed outside R, or interfacing C++ code that you access within R. Rcpp facilitates (greatly) the latter. Take a look at Armadillo directly if it is the former situation.
I think the original questions starts at the wrong end. There is no C++ in R, and no C++ API in R itself.
So if you want to access R matrix functions, you have to go through the R API -- which is a C layer and very much not C++.
Gavin in his answer (and comments) and Josh are spot-on: You probably want something like Armadillo for high-level matrix algebra. Armadillo (just like related libraries) eventually goes to BLAS and LAPACK --- as does R. [ Doug Bates usually reminds us that there is one important difference related to pivoted decompositions; this is touched upon in the fastLm() implementation and example in RcppArmadillo. ]
Lastly, Rcpp can help with both cases:
whether you want to extend R by calling C++ code you wrote, where Rcpp makes it easy to pass objects back and forth, or
whether you want to embed R inside your C++ application using RInside as Rcpp once again provides the glue
The rcpp-devel list is a good place for more in-depth discussions and examples.

Plotting data and functions in C++

I am a beginner to programming and I have to generate and plot certain recursions such as x(n+1)=a*x(n)+b*x(n)^2.
I am supposed to use only C/C++ and not mathematical software such as MATLAB, GNU Octave, etc. I am however allowed to use Gnuplot.
Can you tell me what is the best way in which I can arrive at the plots for the above recursions and how I can implement it in my code? Is generating the values, storing it in a file using fstream, and then plotting it later a good idea?
Use the Boost Visualizer Library
http://www.boost.org/doc/libs/1_42_0/libs/graph/doc/index.html
Or go for OGDF using GML output, fairly straightforward
http://www.ogdf.net/ogdf.php?id=
These are all C++ Libraries, not 3rd party software like MATLAB.