Update.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 31 Mar 2017 18:48:22 +0000 (20:48 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 31 Mar 2017 18:48:22 +0000 (20:48 +0200)
sequence_generator.cc

index cd4d313..5caae66 100644 (file)
@@ -77,18 +77,17 @@ extern "C" void fl_generate_sequence(int nb_images,
   const scalar_t world_height = height * super_definition;
   const scalar_t scaling = 1 / super_definition;
 
-  const scalar_t dt = 0.1;
-  const int nb_iterations_per_steps = 5;
+  int nb_iterations_per_dt = 50;
+  scalar_t dt = 5.0 / scalar_t(nb_iterations_per_dt);
 
   //////////////////////////////////////////////////////////////////////
 
-  // We will generate images { 0, every_nth, 2 * every_nth, ..., k * every_nth < nb_simulated_frames }
+  // We will generate images { 0, nb_iterations_per_dt, 2 * nb_iterations_per_dt, ..., k * nb_iterations_per_dt < nb_simulated_frames }
 
-  // The framerate every_nth may be set to smaller value to generate
+  // The framerate nb_iterations_per_dt may be set to smaller value to generate
   // nice materials for presentations or papers.
 
-  int every_nth = 16;
-  int nb_simulated_frames = 1 + (nb_images - 1) * every_nth;
+  int nb_simulated_frames = 1 + (nb_images - 1) * nb_iterations_per_dt;
   int random_grasp = 1;
 
   Universe *universe;
@@ -96,7 +95,7 @@ extern "C" void fl_generate_sequence(int nb_images,
 
   universe = new Universe(nb_shapes, world_width, world_height);
 
-  const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
+  const int nb_saved_frames = (nb_simulated_frames + nb_iterations_per_dt - 1) / nb_iterations_per_dt;
   if(nb_saved_frames != nb_images) {
     cerr << "It makes no sense." << endl;
     abort();
@@ -143,9 +142,9 @@ extern "C" void fl_generate_sequence(int nb_images,
         scalar_t shape_size;
 
         if(random_shape_size) {
-          shape_size = 40 + 80 * drand48();
+          shape_size = 80 + 80 * drand48();
         } else {
-          shape_size = 80;
+          shape_size = 120;
         }
 
         scalar_t red, green, blue;
@@ -209,8 +208,8 @@ extern "C" void fl_generate_sequence(int nb_images,
     }
 
     for(int s = 0; !failed && s < nb_simulated_frames; s++) {
-      if(s % every_nth == 0) {
-        int t = s / every_nth;
+      if(s % nb_iterations_per_dt == 0) {
+        int t = s / nb_iterations_per_dt;
 
         canvases[t]->clear();
         draw_universe_on_canvas(canvases[t], scaling, universe);
@@ -220,22 +219,20 @@ extern "C" void fl_generate_sequence(int nb_images,
 
         // Run the simulation
 
-        for(int i = 0; i < nb_iterations_per_steps; i++) {
-          if(pulling) {
-            // Pulling the grabbed rectangle
-            scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
-            scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
-            if (xf < 0 || xf >= world_width || yf < 0 || yf >= world_height) {
-              failed = 1;
-            }
-            grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
-          } else {
-            // Gravity
-            universe->apply_gravity(dt, gravity_fx, gravity_fy);
+        if(pulling) {
+          // Pulling the grabbed rectangle
+          scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
+          scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
+          if (xf < 0 || xf >= world_width || yf < 0 || yf >= world_height) {
+            failed = 1;
           }
-
-          universe->update(dt, 1.0 / scaling);
+          grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
+        } else {
+          // Gravity
+          universe->apply_gravity(dt, gravity_fx, gravity_fy);
         }
+
+        universe->update(dt, 1.0 / scaling);
       }
     }