<title>forgeviz.md</title>
<p>Visualizing af::array with Forge</p>
<p>Arrayfire as a library aims to provide a robust and easy to use platform for high-performance, parallel and GPU computing.
The goal of <a href="https://github.com/arrayfire/forge">Forge</a>, an OpenGL visualization library, is to provide equally robust visualizations that are interoperable between Arrayfire data-structures and an OpenGL context.</p>
<p>Arrayfire provides wrapper functions that are designed to be a simple interface to visualize af::arrays. These functions perform various interoperability tasks. One in particular is that instead of wasting time copying and reformatting data from the GPU to the host and back to the GPU, we can draw directly from GPU-data to GPU-framebuffers, saving two memory copies!</p>
<p>Let’s see exactly what visuals we can illuminate with forge and how Arrayfire anneals the data between the two libraries.</p>
<h1><a id="Setup_17"></a>Setup</h1>
<p>Before we can call Forge functions, we need to set up the related “canvas” classes.
Forge functions are tied to the af::Window class. First let’s create a window:</p>
<pre><code class="language-{.cpp}">const static int width = 512, height = 512;
af::Window window(width, height, "2D plot example title");

do{

//drawing functions here

} while( !window.close() );</code></pre>
<p>We also added a drawing loop, so now we can use Forge’s drawing functions to
draw to the window.
The drawing functions present in Forge are listed below.</p>
<h1><a id="Rendering_Functions_35"></a>Rendering Functions</h1>
<p>Documentation for rendering functions can be found <a href="http://sandbox.arrayfire.com/docs/group__gfx__func__draw.htm">http://sandbox.arrayfire.com/docs/group__gfx__func__draw.htm</a> .</p>
<h2><a id="Image_38"></a>Image</h2>
<p>The af::Window::image() function can be used to plot grayscale or color images.
To plot a grayscale image a 2d array should be passed into the function.
Let’s see this on a static noise example:</p>
<pre><code class="language-{.cpp}">array img = constant(0, width, height); //make a black image
array random = randu(width, height);      //make random [0,1] distribution

window.image(img);</code></pre>
<p>&lt;img src=“gfx_docs_images/noise.png” alt=“Forge image plot of noise” width=“20%” /&gt;</p>
<p>Tweaking the previous example by giving our image a depth of 3 for the RGB values
allows us to generate colorful noise:</p>
<pre><code class="language-{.cpp}">array img = 255 * randu(width, height, 3);      //make random [0, 255] distribution
window.image( img.as(u8) );</code></pre>
<p>&lt;img src=“gfx_docs_images/color_noise.png” alt=“Forge image plot of color noise” width=“20%” /&gt;</p>
<p>Note that Forge automatically handles any af::array type passed from Arrayfire.
In the first example we passed in an image of floats in the range [0, 1].
In the last example we cast our array to an unsigned byte array with the range
[0, 255]. The type-handling properties are consistent for all Forge drawing functions.</p>
<h2><a id="Plot_63"></a>Plot</h2>
<p>The af::Window::plot() function visualizes an array as a 2d-line plot. Let’s see
a simple example:</p><pre><code class="language-{.cpp}">array X = seq(-af::Pi, af::Pi, 0.01);
array Y = sin(X);
window.plot(X, Y);</code></pre>
<p>&lt;img src=“gfx_docs_images/sin_plot.png” alt=“Forge 2d line plot of sin() function” width=“30%” /&gt;
The plot function has the signature:</p>
<blockquote>
<p><strong>void plot( const array &amp;X, const array &amp;Y, const char * const title = NULL );</strong></p>
</blockquote>
<p>Both the x and y coordinates of the points are required to plot. This allows for
non-uniform, or parametric plots:</p><pre><code class="language-{.cpp}">array t = seq(0, 100, 0.01);
array X = sin(t) * (exp(cos(t)) - 2 * cos(4 * t) - pow(sin(t / 12), 5));
array Y = cos(t) * (exp(cos(t)) - 2 * cos(4 * t) - pow(sin(t / 12), 5));
window.plot(X, Y);</code></pre>
<p>&lt;img src=“gfx_docs_images/butterfly_plot.png” alt=“Forge 2d line plot of butterfly function” width=“30%” /&gt;</p>
<h2><a id="Scatter_88"></a>Scatter</h2>
<p>The scatter() function visualizes an array as a 2d-scatter plot. Let’s see a example:</p><pre><code class="language-{.cpp}">array X = seq(-af::Pi, af::Pi, 0.01);
array noise = randn(X.dims(0)) /3.0f;
array Y = sin(X) + noise;
window.scatter(X, Y);</code></pre>
<p>&lt;img src=“forge_viz/sin_scatter.png” alt=“Forge 2d scatter plot” width=“40%” /&gt;</p>
<p>The scatter function has the signature:</p>
<blockquote>
<p><strong>void scatter( const array &amp;X, const array &amp;Y, af::markerType marker=AF_MARKER_POINT, const char * const title = NULL);</strong></p>
</blockquote>
<p>The <strong>af::markerType</strong> enum determines which marker will be drawn at each point.
It can take on the values of:</p><pre><code class="language-{.cpp}">enum af::markerType {
    AF_MARKER_POINT
    AF_MARKER_CIRCLE
    AF_MARKER_SQUARE
    AF_MARKER_TRIANGLE
    AF_MARKER_CROSS
    AF_MARKER_PLUS
    AF_MARKER_STAR
};</code></pre>
<p>&lt;img src=“forge_viz/marker_types.png” alt=“Different marker types in forge” width=“100%” /&gt;</p>
<h2><a id="Plot3_118"></a>Plot3</h2>
<p>The af::Window::plot3() function will plot a curve in 3d-space.
Its signature is:</p>
<blockquote>
<p><strong>void plot3 (const array &amp;in, const char * title = NULL);</strong></p>
</blockquote>
<p>The input array expects xyz-triplets in sequential order. The points can be in a
flattened one dimensional (<em>3n x 1</em>) array, or in one of the (<em>3 x n</em>), (<em>n x 3</em>) matrix forms.</p>
<pre><code class="language-{.cpp}">array Z = seq(0.1f, 10.f, 0.01);
array Y = sin(10 * Z) / Z;
array X = cos(10 * Z) / Z;

array Pts = join(1, X, Y, Z);
//Pts can be passed in as a matrix in the from n x 3, 3 x n
//or in the flattened xyz-triplet array with size 3n x 1
window.plot3(Pts);
//both of the following are equally valid
//window.plot3(transpose(Pts));
//window.plot3(flat(Pts));</code></pre>
<p>&lt;img src=“gfx_docs_images/spiral_plot3.png” alt=“Forge 3d line plot” width=“40%” /&gt;</p>
<h2><a id="Histogram_140"></a>Histogram</h2>
<p>The af::Window::hist() function renders an input array as a histogram.
In our example, the input array will be created with Arrayfire’s histogram()
function, which actually counts and bins each sample. The output from histogram()
can directly be fed into the af::Window::hist() rendering function.</p>
<pre><code class="language-{.cpp}">const int BINS = 128; SAMPLES = 9162;
array norm = randn(SAMPLES);
array hist_arr = histogram(norm, BINS);

win.hist(hist_arr, 0, BINS);</code></pre>
<p>In addition to the histogram array with the number of samples in each bin, the
af::Window::hist() function takes two additional parameters – the minimum and
maximum values of all datapoints in the histogram array. This effectively sets
the range of the binned data. The full signature of af::Window::hist() is:</p>
<blockquote>
<p><strong>void hist(const array &amp; X, const double minval, const double maxval, const char * const title = NULL);</strong></p>
</blockquote>
<p>&lt;img src=“gfx_docs_images/norm_histogram.png” alt=“Forge 3d scatter plot” width=“40%” /&gt;</p>
<h2><a id="Scatter3_162"></a>Scatter3</h2>
<p>The scatter3() function visualizes an array as a 3d-scatter plot.
Its signature is:</p>
<blockquote>
<p><strong>void scatter3 (const array &amp;in, af::markerType marker=AF_MARKER_POINT, const char * title = NULL);</strong></p>
</blockquote>
<p>The input array expects xyz-triplets in sequential order. The points can be in a
flattened one dimensional (<em>3n x 1</em>) array, or in one of the (<em>3 x n</em>), (<em>n x 3</em>) matrix forms.</p>
<p>&lt;img src=“forge_viz/norm_scatter3.png” alt=“Forge 3d scatter plot” width=“40%” /&gt;</p><pre><code class="language-{.cpp}">array X = randn(300);
array Y = randn(300);
array Z = randn(300);

array Pts = join(1, X, Y, Z);
window.scatter3(Pts);</code></pre>
<p>The <strong>af::markerType</strong> enum determines which marker will be drawn at each point.
It can take on the values of:</p>
<pre><code class="language-{.cpp}">enum af::markerType {
    AF_MARKER_POINT
    AF_MARKER_CIRCLE
    AF_MARKER_SQUARE
    AF_MARKER_TRIANGLE
    AF_MARKER_CROSS
    AF_MARKER_PLUS
    AF_MARKER_STAR
};</code></pre>
<p>&lt;img src=“forge_viz/marker_types.png” alt=“Different marker types in forge” width=“100%” /&gt;</p>
<h2><a id="Surface_195"></a>Surface</h2>
<p>The af::Window::surface() function will plot af::arrays as a 3d surface.</p>
<pre><code class="language-{.cpp}">array Z = randu(21, 21);
window.surface(Z, "Random Surface");    //equal to next function call
//window.surface( seq(-1, 1, 0.1), seq(-1, 1, 0.1), Z, "Random Surface");</code></pre>
<p>&lt;img src=“gfx_docs_images/rand_surface.png” alt=“Forge random surface plot” width=“30%” /&gt;
There are two overloads for the af::Window::surface() function:</p>
<blockquote>
<p><strong>void surface (const array &amp; S, const char * const title )</strong>
// Accepts a 2d matrix with the z values of the surface</p>
</blockquote>
<blockquote>
<p><strong>void surface (const array &amp;xVals, const array &amp;yVals, const array &amp;S, const char * const title)</strong>
// accepts additional vectors that define the x,y coordinates for the surface points.</p>
</blockquote>
<p>The second overload has two options for the x, y coordinate vectors. Assuming a surface grid of size <strong>m x n</strong>:</p>
<ol>
<li>Short vectors defining the spacing along each axis. Vectors will have sizes <strong>m x 1</strong> and <strong>n x 1</strong>.</li>
<li>Vectors containing the coordinates of each and every point.
Each of the vectors will have length <strong>mn x 1</strong>.
This can be used for completely non-uniform or parametric surfaces.</li>
</ol>
<h1><a id="Conclusion_216"></a>Conclusion</h1>
<p>There is a fairly comprehensive collection of methods to visualize data in Arrayfire.
Thanks to the high-performance gpu plotting library Forge, the provided Arrayfire
functions not only make visualizations as simple as possible, but keep them as
robust as the rest of the Arrayfire library.</p>

