Added the gpl
[flatland.git] / sequence_generator.cc
index 540c574..2fa3912 100644 (file)
@@ -1,26 +1,26 @@
 
 /*
- *  dyncnn is a deep-learning algorithm for the prediction of
- *  interacting object dynamics
- *
*  Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
*  Written by Francois Fleuret <francois.fleuret@idiap.ch>
- *
- *  This file is part of dyncnn.
- *
*  dyncnn is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License version 3 as
*  published by the Free Software Foundation.
- *
*  dyncnn is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  General Public License for more details.
- *
*  You should have received a copy of the GNU General Public License
*  along with dyncnn.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
+
+   flatland is a simple 2d physical simulator
+
+   Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
+   Written by Francois Fleuret <francois.fleuret@idiap.ch>
+
+   This file is part of flatland
+
  flatland is free software: you can redistribute it and/or modify it
+   under the terms of the GNU General Public License version 3 as
+   published by the Free Software Foundation.
+
  flatland is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
  along with flatland.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
 
 #include <iostream>
 #include <fstream>
@@ -50,7 +50,7 @@ void draw_universe_on_canvas(CanvasCairo *canvas, scalar_t scaling,
 void draw_grabbing_point_on_canvas(CanvasCairo *canvas, scalar_t scaling,
                                    scalar_t xg, scalar_t yg,
                                    scalar_t r, scalar_t g, scalar_t b) {
-  scalar_t radius = 1/scaling;
+  scalar_t radius = 1 / scaling;
   int n = 36;
   scalar_t xp[n], yp[n];
   for(int k = 0; k < n; k++) {
@@ -67,34 +67,35 @@ void draw_grabbing_point_on_canvas(CanvasCairo *canvas, scalar_t scaling,
 
 extern "C" void fl_generate_sequence(int nb_images,
                                      int width, int height,
+                                     int nb_shapes,
                                      int random_shape_size, int random_colors,
+                                     int pulling,
                                      unsigned char *output) {
 
-  const scalar_t world_width = width * 8;
-  const scalar_t world_height = height * 8;
-  const scalar_t scaling = 0.125;
+  const scalar_t super_definition = 8;
+  const scalar_t world_width = width * super_definition;
+  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 = 100;
+  scalar_t dt = 10.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;
-  int nb_shapes = 10;
 
   Universe *universe;
   Polygon *grabbed_polygon;
 
   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();
@@ -106,17 +107,25 @@ extern "C" void fl_generate_sequence(int nb_images,
     canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
   }
 
+  scalar_t gravity_fx = 0.0;
+  scalar_t gravity_fy = 1.0;
+
   scalar_t grab_start_x, grab_start_y;
 
   int failed;
 
+  int total_nb_attempts = 0;
+  const int max_total_nb_attempts = 1000000;
+
   do {
-    if(random_grasp) {
-      grab_start_x = world_width * (0.1 + 0.8 * drand48());
-      grab_start_y = world_height * (0.1 + 0.8 * drand48());
-    } else {
-      grab_start_x = world_width * 0.5;
-      grab_start_y = world_height * 0.75;
+    if(pulling) {
+      if(random_grasp) {
+        grab_start_x = world_width * (0.1 + 0.8 * drand48());
+        grab_start_y = world_height * (0.1 + 0.8 * drand48());
+      } else {
+        grab_start_x = world_width * 0.5;
+        grab_start_y = world_height * 0.75;
+      }
     }
 
     do {
@@ -133,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 = (10 + 10 * drand48()) * super_definition;
         } else {
-          shape_size = 80;
+          shape_size = 15 * super_definition;
         }
 
         scalar_t red, green, blue;
@@ -184,52 +193,57 @@ extern "C" void fl_generate_sequence(int nb_images,
         }
       }
 
-      grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
-    } while(!grabbed_polygon);
+      if(pulling) {
+        grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
+      }
+    } while(pulling and !grabbed_polygon);
 
     failed = 0;
 
-    scalar_t grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
-    scalar_t grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
+    scalar_t grab_relative_x, grab_relative_y;
 
-    for(int s = 0; !failed && s < nb_simulated_frames; s++) {
-      if(s % every_nth == 0) {
-        int t = s / every_nth;
-        // 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(pulling) {
+      grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
+      grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
+    }
 
-        // canvases[2 * t + 0]->clear();
-        // draw_grabbing_point_on_canvas(canvases[2 * t + 0], scaling,
-        // xf, yf, 0.0, 0.0, 0.0);
-        // canvases[2 * t + 1]->clear();
-        // draw_universe_on_canvas(canvases[2 * t + 1], scaling, universe);
+    for(int s = 0; !failed && s < nb_simulated_frames; s++) {
+      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);
-
-        // if(show_grabbing_point) {
-        // draw_grabbing_point_on_canvas(canvases[2 * t + 1], scaling,
-        // xf, yf, 1.0, 0.0, 0.0);
-        // }
       }
 
       if(s < nb_simulated_frames - 1) {
+
         // 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);
-          cout << "xf = " << xf << " yf = " << yf << endl;
           if (xf < 0 || xf >= world_width || yf < 0 || yf >= world_height) {
             failed = 1;
           }
           grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
-          universe->update(dt, 1.0 / scaling);
+        } else {
+          // Gravity
+          universe->apply_gravity(dt, gravity_fx, gravity_fy);
         }
+
+        universe->update(dt, 1.0 / scaling);
       }
     }
 
-    if(failed) cout << "** FAILED" << endl;
-    else cout << "** DONE" << endl;
+    total_nb_attempts++;
+
+    if(total_nb_attempts >= max_total_nb_attempts) {
+      cerr << "There was "
+           << max_total_nb_attempts
+           << " attempts at generating the sequences, aborting." << endl;
+      abort();
+    }
 
   } while(failed);