Arrayfire 作为一个库,旨在为高性能、并行和 GPU 计算提供一个健壮且易于使用的平台。
作为Opengl 可视化库, Forge 的目标是提供同样健壮的可视化,可以在 Arrayfire 数据结构和 OpenGL 上下文之间进行互操作,方便显示计算结果。
Arrayfire 提供了包装器函数,这些函数被设计成一个简单的接口,用于数据af::array的可视化。 这些函数执行各种互操作任务。 其中一个特别之处在于,我们不必浪费时间将数据从 GPU 复制到主机再重新格式化到 GPU,而是可以直接从 GPU 数据绘制到 GPU 缓冲区帧! 这将节省2个内存副本。
通过下面几个例子,让我们看看什么样的视觉效果,我们可以阐明与锻造和数据库之间的数据退火
配置
在调用 Forge 函数之前,我们需要设置相关的画布“ canvas”类。 Forge 函数与 af: : Window 类绑定在一起。 首先让我们创建一个窗口:
const static int width = 512, height = 512;
af::Window window(width, height, "2D plot example title");
do{
//drawing functions here
} while( !window.close() );
渲染函数
Documentation for rendering functions can be found here.
渲染函数一种15种, 可以涌现显示图片、散点、曲线、曲面和体。
Functions | |
void | |
| Renders the input array as an image to the window. More... |
| |
void | |
| Renders the input array as an 3d line plot to the window. More... |
| |
void | |
| Renders the input arrays as a 2D or 3D plot to the window. More... |
| |
void | plot (const array &X, const array &Y, const array &Z, const char *const title=NULL) |
| Renders the input arrays as a 3D plot to the window. More... |
| |
void | plot (const array &X, const array &Y, const char *const title=NULL) |
| Renders the input arrays as a 2D plot to the window. More... |
| |
void | scatter (const array &in, const af::markerType marker=AF_MARKER_POINT, const char *const title=NULL) |
| Renders the input arrays as a 2D or 3D scatter-plot to the window. More... |
| |
void | scatter (const array &X, const array &Y, const array &Z, const af::markerType marker=AF_MARKER_POINT, const char *const title=NULL) |
| Renders the input arrays as a 3D scatter-plot to the window. More... |
| |
void | scatter (const array &X, const array &Y, const af::markerType marker=AF_MARKER_POINT, const char *const title=NULL) |
| Renders the input arrays as a 2D scatter-plot to the window. More... |
| |
void | scatter3 (const array &P, const af::markerType marker=AF_MARKER_POINT, const char *const title=NULL) |
| Renders the input arrays as a 3D scatter-plot to the window. More... |
| |
void | hist (const array &X, const double minval, const double maxval, const char *const title=NULL) |
| Renders the input array as a histogram to the window. More... |
| |
void | |
| Renders the input arrays as a 3D surface plot to the window. More... |
| |
void | surface (const array &xVals, const array &yVals, const array &S, const char *const title=NULL) |
| Renders the input arrays as a 3D surface plot to the window. More... |
| |
void | vectorField (const array &points, const array &directions, const char *const title=NULL) |
| Renders the input arrays as a 2D or 3D vector field plot to the window. More... |
| |
void | vectorField (const array &xPoints, const array &yPoints, const array &zPoints, const array &xDirs, const array &yDirs, const array &zDirs, const char *const title=NULL) |
| Renders the input arrays as a 3D vector field plot to the window. More... |
| |
void | vectorField (const array &xPoints, const array &yPoints, const array &xDirs, const array &yDirs, const char *const title=NULL) |
| Renders the input arrays as a 2D vector field plot to the window. More... |
Image显示图片
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:
array img = constant(0, width, height); //make a black image
array random = randu(width, height); //make random [0,1] distribution
img(random > 0.5) = 1; //set all pixels where distribution > 0.5 to white
window.image(img);
Tweaking the previous example by giving our image a depth of 3 for the RGB values allows us to generate colorful noise:
array img = 255 * randu(width, height, 3); //make random [0, 255] distribution
window.image( img.as(u8) );
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.
Plot显示二维线
The af::Window::plot() function visualizes an array as a 2d-line plot. Let's see a simple example:
array X = seq(-af::Pi, af::Pi, 0.01);
array Y = sin(X);
window.plot(X, Y);
The plot function has the signature:
void plot( const array &X, const array &Y, const char * const title = NULL );
Both the x and y coordinates of the points are required to plot. This allows for non-uniform, or parametric plots:
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);
Plot3显示三维线
The af::Window::plot3() function will plot a curve in 3d-space. Its signature is:
void plot3 (const array &in, const char * title = NULL);
The input array expects xyz-triplets in sequential order. The points can be in a flattened one dimensional (3n x 1) array, or in one of the (3 x n), (n x 3) matrix forms.
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));
Histogram显示直方图
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.
const int BINS = 128; SAMPLES = 9162;
array norm = randn(SAMPLES);
array hist_arr = histogram(norm, BINS);
win.hist(hist_arr, 0, BINS);
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:
void hist(const array & X, const double minval, const double maxval, const char * const title = NULL);
Surface显示曲面
The af::Window::surface() function will plot af::arrays as a 3d surface.
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");
There are two overloads for the af::Window::surface() function:
void surface (const array & S, const char * const title ) // Accepts a 2d matrix with the z values of the surface
void surface (const array &xVals, const array &yVals, const array &S, const char * const title) // accepts additional vectors that define the x,y coordinates for the surface points.
The second overload has two options for the x, y coordinate vectors. Assuming a surface grid of size m x n:
- Short vectors defining the spacing along each axis. Vectors will have sizes m x 1 and n x 1.
- Vectors containing the coordinates of each and every point. Each of the vectors will have length mn x 1. This can be used for completely non-uniform or parametric surfaces.
Conclusion结论
在 Arrayfire 有一个相当全面的数据可视化方法的集合。 由于高性能的 gpu 标绘图库 Forge,所提供的 Arrayfire 函数不仅使可视化尽可能简单,而且保持它们像 Arrayfire 库的其余部分一样健壮。
参考资料
http://arrayfire.org/docs/forge_visualization.htm
http://arrayfire.org/docs/group__gfx__func__draw.htm