1 Visualizing af::array with Forge {#forge_visualization}
4 Arrayfire as a library aims to provide a robust and easy to use platform for
5 high-performance, parallel and GPU computing.
9 The goal of [Forge](https://github.com/arrayfire/forge), an OpenGL visualization
10 library, is to provide equally robust visualizations that are interoperable
11 between Arrayfire data-structures and an OpenGL context.
13 Arrayfire provides wrapper functions that are designed to be a simple interface
14 to visualize af::arrays. These functions perform various interop tasks. One in
15 particular is that instead of wasting time copying and reformatting data from
16 the GPU to the host and back to the GPU, we can draw directly from GPU-data to
17 GPU-framebuffers! This saves 2 memory copies.
19 Visualizations can be manipulated with a mouse. The following actions are available:
20 - zoom (Alt + Mouse Left Click, move up & down)
21 - pan (Just left click and drag)
22 - rotation (Mouse right click - track ball rotation).
24 Let's see exactly what visuals we can illuminate with forge and how Arrayfire
25 anneals the data between the two libraries.
28 Before we can call Forge functions, we need to set up the related "canvas" classes.
29 Forge functions are tied to the af::Window class. First let's create a window:
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
31 const static int width = 512, height = 512;
32 af::Window window(width, height, "2D plot example title");
36 //drawing functions here
38 } while( !window.close() );
39 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 We also added a drawing loop, so now we can use Forge's drawing functions to
43 The drawing functions present in Forge are listed below.
45 # Rendering Functions {#render_func}
47 Documentation for rendering functions can be found [here](\ref gfx_func_draw).
50 The af::Window::image() function can be used to plot grayscale or color images.
51 To plot a grayscale image a 2d array should be passed into the function.
52 Let's see this on a static noise example:
53 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
54 array img = constant(0, width, height); //make a black image
55 array random = randu(width, height); //make random [0,1] distribution
56 img(random > 0.5) = 1; //set all pixels where distribution > 0.5 to white
59 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 <img src="gfx_docs_images/noise.png" alt="Forge image plot of noise" width="20%" />
61 Tweaking the previous example by giving our image a depth of 3 for the RGB values
62 allows us to generate colorful noise:
63 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
64 array img = 255 * randu(width, height, 3); //make random [0, 255] distribution
65 window.image( img.as(u8) );
66 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67 <img src="gfx_docs_images/color_noise.png" alt="Forge image plot of color noise" width="20%" />
68 Note that Forge automatically handles any af::array type passed from Arrayfire.
69 In the first example we passed in an image of floats in the range [0, 1].
70 In the last example we cast our array to an unsigned byte array with the range
71 [0, 255]. The type-handling properties are consistent for all Forge drawing functions.
74 The af::Window::plot() function visualizes an array as a 2d-line plot. Let's see
76 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
77 array X = seq(-af::Pi, af::Pi, 0.01);
80 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82 <img src="gfx_docs_images/sin_plot.png" alt="Forge 2d line plot of sin() function" width="30%" />
83 The plot function has the signature:
85 > **void plot( const array &X, const array &Y, const char * const title = NULL );**
87 Both the x and y coordinates of the points are required to plot. This allows for
88 non-uniform, or parametric plots:
89 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
90 array t = seq(0, 100, 0.01);
91 array X = sin(t) * (exp(cos(t)) - 2 * cos(4 * t) - pow(sin(t / 12), 5));
92 array Y = cos(t) * (exp(cos(t)) - 2 * cos(4 * t) - pow(sin(t / 12), 5));
94 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 <img src="gfx_docs_images/butterfly_plot.png" alt="Forge 2d line plot of butterfly function" width="30%" />
99 The af::Window::plot3() function will plot a curve in 3d-space.
101 > **void plot3 (const array &in, const char * title = NULL);**
102 The input array expects xyz-triplets in sequential order. The points can be in a
103 flattened one dimensional (*3n x 1*) array, or in one of the (*3 x n*), (*n x 3*) matrix forms.
104 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
105 array Z = seq(0.1f, 10.f, 0.01);
106 array Y = sin(10 * Z) / Z;
107 array X = cos(10 * Z) / Z;
109 array Pts = join(1, X, Y, Z);
110 //Pts can be passed in as a matrix in the from n x 3, 3 x n
111 //or in the flattened xyz-triplet array with size 3n x 1
113 //both of the following are equally valid
114 //window.plot3(transpose(Pts));
115 //window.plot3(flat(Pts));
116 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117 <img src="gfx_docs_images/spiral_plot3.png" alt="Forge 3d line plot" width="40%" />
119 ## Histogram {#histogram}
120 The af::Window::hist() function renders an input array as a histogram.
121 In our example, the input array will be created with Arrayfire's histogram()
122 function, which actually counts and bins each sample. The output from histogram()
123 can directly be fed into the af::Window::hist() rendering function.
125 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
126 const int BINS = 128; SAMPLES = 9162;
127 array norm = randn(SAMPLES);
128 array hist_arr = histogram(norm, BINS);
130 win.hist(hist_arr, 0, BINS);
131 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 In addition to the histogram array with the number of samples in each bin, the
133 af::Window::hist() function takes two additional parameters -- the minimum and
134 maximum values of all datapoints in the histogram array. This effectively sets
135 the range of the binned data. The full signature of af::Window::hist() is:
136 > **void hist(const array & X, const double minval, const double maxval, const char * const title = NULL);**
137 <img src="gfx_docs_images/norm_histogram.png" alt="Forge 3d scatter plot" width="40%" />
140 ## Surface {#surface}
141 The af::Window::surface() function will plot af::arrays as a 3d surface.
142 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
143 array Z = randu(21, 21);
144 window.surface(Z, "Random Surface"); //equal to next function call
145 //window.surface( seq(-1, 1, 0.1), seq(-1, 1, 0.1), Z, "Random Surface");
146 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147 <img src="gfx_docs_images/rand_surface.png" alt="Forge random surface plot" width="30%" />
148 There are two overloads for the af::Window::surface() function:
149 > **void surface (const array & S, const char * const title )**
150 > // Accepts a 2d matrix with the z values of the surface
152 > **void surface (const array &xVals, const array &yVals, const array &S, const char * const title)**
153 > // accepts additional vectors that define the x,y coordinates for the surface points.
155 The second overload has two options for the x, y coordinate vectors. Assuming a surface grid of size **m x n**:
156 1. Short vectors defining the spacing along each axis. Vectors will have sizes **m x 1** and **n x 1**.
157 2. Vectors containing the coordinates of each and every point.
158 Each of the vectors will have length **mn x 1**.
159 This can be used for completely non-uniform or parametric surfaces.
161 # Conclusion {#conclusion}
162 There is a fairly comprehensive collection of methods to visualize data in Arrayfire.
163 Thanks to the high-performance gpu plotting library Forge, the provided Arrayfire
164 functions not only make visualizations as simple as possible, but keep them as
165 robust as the rest of the Arrayfire library.