Skip to content

Commit 0968b7e

Browse files
committed
WIP: add pie chart with cpu example
1 parent 34af818 commit 0968b7e

File tree

19 files changed

+1014
-0
lines changed

19 files changed

+1014
-0
lines changed

examples/cpu/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_example(bubblechart bubblechart.cpp cpu CXX11)
66
add_example(field field.cpp cpu)
77
add_example(fractal fractal.cpp cpu)
88
add_example(histogram histogram.cpp cpu)
9+
add_example(pie pie.cpp cpu)
910
add_example(plot3 plot3.cpp cpu)
1011
add_example(plotting plotting.cpp cpu)
1112
add_example(stream stream.cpp cpu)

examples/cpu/pie.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*******************************************************
2+
* Copyright (c) 2015-2019, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <forge.h>
11+
#define USE_FORGE_CPU_COPY_HELPERS
12+
#include <ComputeCopy.h>
13+
#include <cmath>
14+
#include <cstdlib>
15+
#include <vector>
16+
#include <iostream>
17+
18+
const unsigned DIMX = 1000;
19+
const unsigned DIMY = 800;
20+
const unsigned NSECTORS = 10;
21+
22+
std::vector<float> generateSectors(unsigned count = NSECTORS)
23+
{
24+
std::vector<float> result;
25+
float prefixSum = 0;
26+
for (; count > 0; --count) {
27+
result.push_back(prefixSum);
28+
result.push_back(std::rand() & 0xffff);
29+
prefixSum += result.back();
30+
}
31+
return result;
32+
}
33+
34+
std::vector<float> generateColors(unsigned count = NSECTORS)
35+
{
36+
std::vector<float> result;
37+
for (; count > 0; --count) {
38+
for (int channel = 0; channel < 3; ++channel)
39+
result.push_back(std::rand()/(float)RAND_MAX);
40+
}
41+
return result;
42+
}
43+
44+
int main(int argc, char* argv[])
45+
{
46+
/*
47+
* First Forge call should be a window creation call
48+
* so that necessary OpenGL context is created for any
49+
* other forge::* object to be created successfully
50+
*/
51+
forge::Window wnd(DIMX, DIMY, "Pie Demo");
52+
wnd.makeCurrent();
53+
54+
forge::Chart chart(FG_CHART_2D);
55+
56+
/*
57+
* Create pie object specifying number of bins
58+
*/
59+
forge::Pie pie = chart.pie(NSECTORS, forge::f32);
60+
61+
GfxHandle* handles[2];
62+
createGLBuffer(&handles[0], pie.vertices(), FORGE_VERTEX_BUFFER);
63+
createGLBuffer(&handles[1], pie.colors(), FORGE_VERTEX_BUFFER);
64+
65+
std::vector<float> pieArray = generateSectors();
66+
std::vector<float> colArray = generateColors();
67+
68+
/* set the axes limits to minimum and maximum values of data */
69+
float valueRange = pieArray[pieArray.size() - 2] + pieArray[pieArray.size() - 1];
70+
chart.setAxesLimits(0, valueRange, 0, valueRange);
71+
72+
copyToGLBuffer(handles[0], (ComputeResourceHandle)pieArray.data(), pie.verticesSize());
73+
copyToGLBuffer(handles[1], (ComputeResourceHandle)colArray.data(), pie.colorsSize());
74+
75+
do {
76+
wnd.draw(chart);
77+
} while(!wnd.close());
78+
79+
releaseGLBuffer(handles[0]);
80+
releaseGLBuffer(handles[1]);
81+
82+
return 0;
83+
}

include/fg/chart.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <fg/defines.h>
1313
#include <fg/image.h>
14+
#include <fg/pie.h>
1415
#include <fg/plot.h>
1516
#include <fg/surface.h>
1617
#include <fg/vector_field.h>
@@ -148,6 +149,16 @@ FGAPI fg_err fg_append_image_to_chart(fg_chart pChart, fg_image pImage);
148149
*/
149150
FGAPI fg_err fg_append_histogram_to_chart(fg_chart pChart, fg_histogram pHistogram);
150151

152+
/**
153+
Add an existing pie object to chart
154+
155+
\param[in] pChart is the chart to which pie has to be added
156+
\param[in] pPie is the pie to be added to the chart
157+
158+
\return \ref fg_err error code
159+
*/
160+
FGAPI fg_err fg_append_pie_to_chart(fg_chart pChart, fg_pie pPie);
161+
151162
/**
152163
Add an existing plot object to chart
153164
@@ -211,6 +222,20 @@ FGAPI fg_err fg_add_image_to_chart(fg_image* pImage, fg_chart pHandle,
211222
FGAPI fg_err fg_add_histogram_to_chart(fg_histogram* pHistogram, fg_chart pHandle,
212223
const unsigned pNBins, const fg_dtype pType);
213224

225+
/**
226+
Create and add an Pie object to the current chart
227+
228+
\param[out] pPie is the handle of the pie object created
229+
\param[in] pHandle is chart handle
230+
\param[in] pNSectors is number of sectors the data is sorted out
231+
\param[in] pType takes one of the values of \ref fg_dtype that indicates
232+
the integral data type of pie data
233+
234+
\return \ref fg_err error code
235+
*/
236+
FGAPI fg_err fg_add_pie_to_chart(fg_pie* pPie, fg_chart pHandle,
237+
const unsigned pNSectors, const fg_dtype pType);
238+
214239
/**
215240
Create and add an Plot object to the current chart
216241
@@ -406,6 +431,13 @@ class Chart {
406431
*/
407432
FGAPI void add(const Histogram& pHistogram);
408433

434+
/**
435+
Add an existing Pie object to the current chart
436+
437+
\param[in] pPie is the Pie to render on the chart
438+
*/
439+
FGAPI void add(const Pie& pPie);
440+
409441
/**
410442
Add an existing Plot object to the current chart
411443
@@ -449,6 +481,15 @@ class Chart {
449481
*/
450482
FGAPI Histogram histogram(const unsigned pNBins, const dtype pDataType);
451483

484+
/**
485+
Create and add an Pie object to the current chart
486+
487+
\param[in] pNSectors is number of sectors the data is sorted out
488+
\param[in] pDataType takes one of the values of \ref dtype that indicates
489+
the integral data type of pie data
490+
*/
491+
FGAPI Pie pie(const unsigned pNSectors, const dtype pDataType);
492+
452493
/**
453494
Create and add an Plot object to the current chart
454495

include/fg/defines.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ typedef void* fg_image;
4949
/// \brief Histogram handle
5050
typedef void* fg_histogram;
5151

52+
/// \brief Pie handle
53+
typedef void* fg_pie;
54+
5255
/// \brief Plot handle
5356
typedef void* fg_plot;
5457

0 commit comments

Comments
 (0)