Skip to content

Commit

Permalink
clang format examples
Browse files Browse the repository at this point in the history
  • Loading branch information
9prady9 committed Dec 23, 2019
1 parent e32f585 commit 390ea53
Show file tree
Hide file tree
Showing 25 changed files with 1,229 additions and 1,297 deletions.
56 changes: 29 additions & 27 deletions examples/cpu/bubblechart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,32 @@
#include <forge.h>
#define USE_FORGE_CPU_COPY_HELPERS
#include <ComputeCopy.h>
#include <complex>
#include <cmath>
#include <vector>
#include <iostream>
#include <random>
#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

const unsigned DIMX = 1000;
const unsigned DIMY = 800;

const float FRANGE_START = 0.f;
const float FRANGE_END = 2.f * 3.1415926f;
const float FRANGE_END = 2.f * 3.1415926f;

using namespace std;
void map_range_to_vec_vbo(float range_start, float range_end, float dx,
std::vector<float> &vec,
float (*map) (float))
{
if(range_start > range_end && dx > 0) return;
for(float i=range_start; i < range_end; i+=dx){
std::vector<float>& vec, float (*map)(float)) {
if (range_start > range_end && dx > 0) return;
for (float i = range_start; i < range_end; i += dx) {
vec.push_back(i);
vec.push_back((*map)(i));
}
}

int main(void)
{
int main(void) {
std::vector<float> cosData;
std::vector<float> tanData;

Expand All @@ -58,7 +55,7 @@ int main(void)
auto rnd = std::bind(nDist, e1);
auto alp = std::bind(fDist, gen);

std::vector<float> colors(3*tanData.size());
std::vector<float> colors(3 * tanData.size());
std::vector<float> alphas(tanData.size());
std::vector<float> radii(tanData.size());

Expand All @@ -80,14 +77,16 @@ int main(void)
/* Create several plot objects which creates the necessary
* vertex buffer objects to hold the different plot types
*/
forge::Plot plt1 = chart.plot((unsigned)(cosData.size()/2), forge::f32,
FG_PLOT_LINE, FG_MARKER_TRIANGLE); //or specify a specific plot type
forge::Plot plt2 = chart.plot((unsigned)(tanData.size()/2), forge::f32,
FG_PLOT_LINE, FG_MARKER_CIRCLE); //last parameter specifies marker shape
forge::Plot plt1 =
chart.plot((unsigned)(cosData.size() / 2), forge::f32, FG_PLOT_LINE,
FG_MARKER_TRIANGLE); // or specify a specific plot type
forge::Plot plt2 =
chart.plot((unsigned)(tanData.size() / 2), forge::f32, FG_PLOT_LINE,
FG_MARKER_CIRCLE); // last parameter specifies marker shape

/* Set plot colors */
plt1.setColor(FG_RED);
plt2.setColor(FG_GREEN); //use a forge predefined color
plt2.setColor(FG_GREEN); // use a forge predefined color
/* Set plot legends */
plt1.setLegend("Cosine");
plt2.setLegend("Tangent");
Expand All @@ -110,19 +109,22 @@ int main(void)
createGLBuffer(&handles[4], plt2.radii(), FORGE_VERTEX_BUFFER);

// copy the data from compute buffer to graphics buffer
copyToGLBuffer(handles[0], (ComputeResourceHandle)cosData.data(), plt1.verticesSize());
copyToGLBuffer(handles[1], (ComputeResourceHandle)tanData.data(), plt2.verticesSize());
copyToGLBuffer(handles[0], (ComputeResourceHandle)cosData.data(),
plt1.verticesSize());
copyToGLBuffer(handles[1], (ComputeResourceHandle)tanData.data(),
plt2.verticesSize());

/* update color value for tan graph */
copyToGLBuffer(handles[2], (ComputeResourceHandle)colors.data(), plt2.colorsSize());
copyToGLBuffer(handles[2], (ComputeResourceHandle)colors.data(),
plt2.colorsSize());
/* update alpha values for tan graph */
copyToGLBuffer(handles[3], (ComputeResourceHandle)alphas.data(), plt2.alphasSize());
copyToGLBuffer(handles[3], (ComputeResourceHandle)alphas.data(),
plt2.alphasSize());
/* update marker sizes for tan graph markers */
copyToGLBuffer(handles[4], (ComputeResourceHandle)radii.data(), plt2.radiiSize());
copyToGLBuffer(handles[4], (ComputeResourceHandle)radii.data(),
plt2.radiiSize());

do {
wnd.draw(chart);
} while(!wnd.close());
do { wnd.draw(chart); } while (!wnd.close());

// destroy GL-CPU Interop buffer
releaseGLBuffer(handles[0]);
Expand Down
59 changes: 30 additions & 29 deletions examples/cpu/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,38 @@
#include <forge.h>
#define USE_FORGE_CPU_COPY_HELPERS
#include <ComputeCopy.h>
#include <complex>
#include <cmath>
#include <vector>
#include <complex>
#include <iostream>
#include <vector>

const unsigned DIMX = 640;
const unsigned DIMY = 480;
const float PI = 3.14159265359f;
const float MINIMUM = 1.0f;
const float MAXIMUM = 20.f;
const float STEP = 2.0f;
const float NELEMS = (MAXIMUM-MINIMUM+1)/STEP;
const unsigned DIMX = 640;
const unsigned DIMY = 480;
const float PI = 3.14159265359f;
const float MINIMUM = 1.0f;
const float MAXIMUM = 20.f;
const float STEP = 2.0f;
const float NELEMS = (MAXIMUM - MINIMUM + 1) / STEP;
const unsigned DPOINTS[] = {5, 5, 5, 15, 15, 5, 15, 15};

using namespace std;

void generatePoints(std::vector<float> &points, std::vector<float> &dirs)
{
void generatePoints(std::vector<float> &points, std::vector<float> &dirs) {
points.clear();

for (int j=0; j<NELEMS; ++j) {
float y = MINIMUM + j*STEP;
for (int i=0; i<NELEMS; ++i) {
float x = MINIMUM + i*STEP;
for (int j = 0; j < NELEMS; ++j) {
float y = MINIMUM + j * STEP;
for (int i = 0; i < NELEMS; ++i) {
float x = MINIMUM + i * STEP;
points.push_back(x);
points.push_back(y);
dirs.push_back(sin(2*PI*x/10.f));
dirs.push_back(sin(2*PI*y/10.f));
dirs.push_back(sin(2 * PI * x / 10.f));
dirs.push_back(sin(2 * PI * y / 10.f));
}
}
}

int main(void)
{
int main(void) {
/*
* First Forge call should be a window creation call
* so that necessary OpenGL context is created for any
Expand All @@ -53,34 +51,37 @@ int main(void)
wnd.makeCurrent();

forge::Chart chart(FG_CHART_2D);
chart.setAxesLimits(MINIMUM-1.0f, MAXIMUM, MINIMUM-1.0f, MAXIMUM);
chart.setAxesLimits(MINIMUM - 1.0f, MAXIMUM, MINIMUM - 1.0f, MAXIMUM);
chart.setAxesTitles("x-axis", "y-axis");

forge::Plot divPoints = chart.plot(4, forge::u32, FG_PLOT_SCATTER, FG_MARKER_CIRCLE);
forge::Plot divPoints =
chart.plot(4, forge::u32, FG_PLOT_SCATTER, FG_MARKER_CIRCLE);
divPoints.setColor(0.9f, 0.9f, 0.0f, 1.f);
divPoints.setLegend("Convergence Points");
divPoints.setMarkerSize(24);

forge::VectorField field = chart.vectorField((unsigned)(NELEMS*NELEMS), forge::f32);
forge::VectorField field =
chart.vectorField((unsigned)(NELEMS * NELEMS), forge::f32);
field.setColor(0.f, 0.6f, 0.3f, 1.f);

std::vector<float> points;
std::vector<float> dirs;
generatePoints(points, dirs);

GfxHandle* handles[3];
GfxHandle *handles[3];

createGLBuffer(&handles[0], divPoints.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[1], field.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[2], field.directions(), FORGE_VERTEX_BUFFER);

copyToGLBuffer(handles[0], (ComputeResourceHandle)DPOINTS, divPoints.verticesSize());
copyToGLBuffer(handles[1], (ComputeResourceHandle)points.data(), field.verticesSize());
copyToGLBuffer(handles[2], (ComputeResourceHandle)dirs.data(), field.directionsSize());
copyToGLBuffer(handles[0], (ComputeResourceHandle)DPOINTS,
divPoints.verticesSize());
copyToGLBuffer(handles[1], (ComputeResourceHandle)points.data(),
field.verticesSize());
copyToGLBuffer(handles[2], (ComputeResourceHandle)dirs.data(),
field.directionsSize());

do {
wnd.draw(chart);
} while(!wnd.close());
do { wnd.draw(chart); } while (!wnd.close());

// destroy GL-cpu interop buffers
releaseGLBuffer(handles[0]);
Expand Down
58 changes: 24 additions & 34 deletions examples/cpu/fractal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include <forge.h>
#define USE_FORGE_CPU_COPY_HELPERS
#include <ComputeCopy.h>
#include <complex>
#include <cmath>
#include <complex>

const unsigned DIMX = 512;
const unsigned DIMY = 512;

struct Bitmap {
unsigned char *ptr;
unsigned char* ptr;
unsigned width;
unsigned height;
};
Expand All @@ -27,8 +27,7 @@ void destroyBitmap(Bitmap& bmp);
void kernel(Bitmap& bmp);
int julia(int x, int y, int width, int height);

int main(void)
{
int main(void) {
Bitmap bmp = createBitmap(DIMX, DIMY);

/*
Expand Down Expand Up @@ -73,57 +72,48 @@ int main(void)
// copy the data from compute buffer to graphics buffer
copyToGLBuffer(handle, (ComputeResourceHandle)bmp.ptr, img.size());

do {
wnd.draw(img);
} while(!wnd.close());
do { wnd.draw(img); } while (!wnd.close());

// destroy GL-CPU Interop buffer
releaseGLBuffer(handle);
destroyBitmap(bmp);
return 0;
}

Bitmap createBitmap(unsigned w, unsigned h)
{
Bitmap createBitmap(unsigned w, unsigned h) {
Bitmap retVal;
retVal.width = w;
retVal.height= h;
retVal.ptr = new unsigned char[4*w*h];
retVal.width = w;
retVal.height = h;
retVal.ptr = new unsigned char[4 * w * h];
return retVal;
}

void destroyBitmap(Bitmap& bmp)
{
delete[] bmp.ptr;
}

void kernel(Bitmap& bmp)
{
for (unsigned y=0; y<bmp.height; ++y) {
for (unsigned x=0; x<bmp.width; ++x) {
int offset = x + y * bmp.width;
int juliaVal= julia(x, y, bmp.width, bmp.height);
bmp.ptr[offset*4 + 0] = 255 * juliaVal;
bmp.ptr[offset*4 + 1] = 0;
bmp.ptr[offset*4 + 2] = 0;
bmp.ptr[offset*4 + 3] = 255;
void destroyBitmap(Bitmap& bmp) { delete[] bmp.ptr; }

void kernel(Bitmap& bmp) {
for (unsigned y = 0; y < bmp.height; ++y) {
for (unsigned x = 0; x < bmp.width; ++x) {
int offset = x + y * bmp.width;
int juliaVal = julia(x, y, bmp.width, bmp.height);
bmp.ptr[offset * 4 + 0] = 255 * juliaVal;
bmp.ptr[offset * 4 + 1] = 0;
bmp.ptr[offset * 4 + 2] = 0;
bmp.ptr[offset * 4 + 3] = 255;
}
}
}

int julia(int x, int y, int width, int height)
{
int julia(int x, int y, int width, int height) {
const float scale = 1.5;
float jx = scale * (float)(width/2.0f - x)/(width/2.0f);
float jy = scale * (float)(height/2.0f - y)/(height/2.0f);
float jx = scale * (float)(width / 2.0f - x) / (width / 2.0f);
float jy = scale * (float)(height / 2.0f - y) / (height / 2.0f);

std::complex<float> c(-0.8f, 0.156f);
std::complex<float> a(jx, jy);

for (int i=0; i<200; i++) {
for (int i = 0; i < 200; i++) {
a = a * a + c;
if (abs(a) > 1000)
return 0;
if (abs(a) > 1000) return 0;
}

return 1;
Expand Down
Loading

0 comments on commit 390ea53

Please sign in to comment.