[arrayfire] 100/284: remove state globals

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun Feb 7 18:59:23 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/experimental
in repository arrayfire.

commit 5c0160863c9dd64c1733497fdce24dfdef823bc7
Author: syurkevi <stefan at arrayfire.com>
Date:   Mon Dec 21 13:24:01 2015 -0500

    remove state globals
---
 examples/graphics/gravity_sim.cpp | 79 +++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 41 deletions(-)

diff --git a/examples/graphics/gravity_sim.cpp b/examples/graphics/gravity_sim.cpp
index 94d321b..25ca0da 100644
--- a/examples/graphics/gravity_sim.cpp
+++ b/examples/graphics/gravity_sim.cpp
@@ -17,43 +17,36 @@ using namespace std;
 static const int width = 512, height = 512;
 static const int pixels_per_unit = 20;
 
-af::array p_x;
-af::array p_y;
-af::array vels_x;
-af::array vels_y;
-af::array forces_x;
-af::array forces_y;
-
-void simulate(float dt){
-    p_x += vels_x * pixels_per_unit * dt;
-    p_y += vels_y * pixels_per_unit * dt;
+void simulate(af::array *pos, af::array *vels, af::array *forces, float dt){
+    pos[0] += vels[0] * pixels_per_unit * dt;
+    pos[1] += vels[1] * pixels_per_unit * dt;
 
     //calculate distance to center
-    af::array diff_x = p_x - width/2;
-    af::array diff_y = p_y - height/2;
+    af::array diff_x = pos[0] - width/2;
+    af::array diff_y = pos[1] - height/2;
     af::array dist = sqrt( diff_x*diff_x + diff_y*diff_y );
 
     //calculate normalised force vectors
-    forces_x = -1 * diff_x / dist;
-    forces_y = -1 * diff_y / dist;
+    forces[0] = -1 * diff_x / dist;
+    forces[1] = -1 * diff_y / dist;
     //update force scaled to time and magnitude constant
-    forces_x *= pixels_per_unit * dt;
-    forces_y *= pixels_per_unit * dt;
+    forces[0] *= pixels_per_unit * dt;
+    forces[1] *= pixels_per_unit * dt;
 
     //dampening
-    vels_x *= 1 - (0.005*dt);
-    vels_y *= 1 - (0.005*dt);
+    vels[0] *= 1 - (0.005*dt);
+    vels[1] *= 1 - (0.005*dt);
 
     //update velocities from forces
-    vels_x += forces_x;
-    vels_y += forces_y;
+    vels[0] += forces[0];
+    vels[1] += forces[1];
 
 }
 
-void collisions(){
+void collisions(af::array *pos, af::array *vels){
     //clamp particles inside screen border
-    af::array projected_px = min(width, max(0, p_x));
-    af::array projected_py = min(height - 1, max(0, p_y));
+    af::array projected_px = min(width, max(0, pos[0]));
+    af::array projected_py = min(height - 1, max(0, pos[1]));
 
     //calculate distance to center
     af::array diff_x = projected_px - width/2;
@@ -64,15 +57,15 @@ void collisions(){
     const int radius = 50;
     const float elastic_constant = 0.91f;
     if(sum<int>(dist<radius) > 0) {
-        vels_x(dist<radius) = -elastic_constant * vels_x(dist<radius);
-        vels_y(dist<radius) = -elastic_constant * vels_y(dist<radius);
+        vels[0](dist<radius) = -elastic_constant * vels[0](dist<radius);
+        vels[1](dist<radius) = -elastic_constant * vels[1](dist<radius);
 
         //normalize diff vector
         diff_x /= dist;
         diff_y /= dist;
         //place all particle colliding with sphere on surface
-        p_x(dist<radius) = width/2 + diff_x(dist<radius) * radius;
-        p_y(dist<radius) = height/2 +  diff_y(dist<radius) * radius;
+        pos[0](dist<radius) = width/2 + diff_x(dist<radius) * radius;
+        pos[1](dist<radius) = height/2 +  diff_y(dist<radius) * radius;
     }
 }
 
@@ -92,15 +85,19 @@ int main(int argc, char *argv[])
         // Initialize the kernel array just once
         const af::array draw_kernel = gaussianKernel(3, 3);
 
+        af::array pos[2];
+        af::array vels[2];
+        af::array forces[2];
+
         // Generate a random starting state
-        p_x = af::randu(total_particles) * width;
-        p_y = af::randu(total_particles) * height;
+        pos[0] = af::randu(total_particles) * width;
+        pos[1] = af::randu(total_particles) * height;
 
-        vels_x = af::randn(total_particles);
-        vels_y = af::randn(total_particles);
+        vels[0] = af::randn(total_particles);
+        vels[1] = af::randn(total_particles);
 
-        forces_x = af::randn(total_particles);
-        forces_y = af::randn(total_particles);
+        forces[0] = af::randn(total_particles);
+        forces[1] = af::randn(total_particles);
 
         af::array image = af::constant(0, width, height);
         af::array ids(total_particles, u32);
@@ -110,7 +107,7 @@ int main(int argc, char *argv[])
             float dt = af::timer::stop(timer);
             timer = af::timer::start();
 
-            ids = (p_x.as(u32) * height) + p_y.as(u32);
+            ids = (pos[0].as(u32) * height) + pos[1].as(u32);
             image(ids) += 255;
             image = convolve2(image, draw_kernel);
             myWindow.image(image);
@@ -119,18 +116,18 @@ int main(int argc, char *argv[])
 
             // Generate a random starting state
             if(frame_count % reset == 0) {
-                p_x = af::randu(total_particles) * width;
-                p_y = af::randu(total_particles) * height;
+                pos[0] = af::randu(total_particles) * width;
+                pos[1] = af::randu(total_particles) * height;
 
-                vels_x = af::randn(total_particles);
-                vels_y = af::randn(total_particles);
+                vels[0] = af::randn(total_particles);
+                vels[1] = af::randn(total_particles);
             }
 
-            //check for collisions and adjust velocities accordingly
-            collisions();
+            //check for collisions and adjust positions/velocities accordingly
+            collisions(pos, vels);
 
             //run force simulation and update particles
-            simulate(dt);
+            simulate(pos, vels, forces, dt);
 
         }
     } catch (af::exception& e) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list