Update.
[flatland.git] / sequence_generator.cc
index 6e6b747..540c574 100644 (file)
@@ -65,10 +65,10 @@ void draw_grabbing_point_on_canvas(CanvasCairo *canvas, scalar_t scaling,
 
 //////////////////////////////////////////////////////////////////////
 
-extern "C" void fl_generate_sequences(int nb_sequences,
-                                      int nb_images_per_sequence,
-                                      int width, int height,
-                                      unsigned char *output) {
+extern "C" void fl_generate_sequence(int nb_images,
+                                     int width, int height,
+                                     int random_shape_size, int random_colors,
+                                     unsigned char *output) {
 
   const scalar_t world_width = width * 8;
   const scalar_t world_height = height * 8;
@@ -85,33 +85,32 @@ extern "C" void fl_generate_sequences(int nb_sequences,
   // nice materials for presentations or papers.
 
   int every_nth = 16;
-  int nb_simulated_frames = 1 + (nb_images_per_sequence - 1) * every_nth;
+  int nb_simulated_frames = 1 + (nb_images - 1) * every_nth;
   int random_grasp = 1;
-  int random_shape_size = 0;
   int nb_shapes = 10;
-  int skip = -1;
 
-  for(int n = 0; n < nb_sequences; n++) {
+  Universe *universe;
+  Polygon *grabbed_polygon;
 
-    Universe *universe;
-    Polygon *grabbed_polygon;
+  universe = new Universe(nb_shapes, world_width, world_height);
 
-    universe = new Universe(nb_shapes, world_width, world_height);
+  const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
+  if(nb_saved_frames != nb_images) {
+    cerr << "It makes no sense." << endl;
+    abort();
+  }
 
-    const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
-    if(nb_saved_frames != nb_images_per_sequence) {
-      cerr << "It makes no sense." << endl;
-      abort();
-    }
+  CanvasCairo *canvases[nb_saved_frames * 2];
 
-    CanvasCairo *canvases[nb_saved_frames * 2];
+  for(int s = 0; s < 2 * nb_saved_frames; s++) {
+    canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
+  }
 
-    for(int s = 0; s < 2 * nb_saved_frames; s++) {
-      canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
-    }
+  scalar_t grab_start_x, grab_start_y;
 
-    scalar_t grab_start_x, grab_start_y;
+  int failed;
 
+  do {
     if(random_grasp) {
       grab_start_x = world_width * (0.1 + 0.8 * drand48());
       grab_start_y = world_height * (0.1 + 0.8 * drand48());
@@ -139,6 +138,21 @@ extern "C" void fl_generate_sequences(int nb_sequences,
           shape_size = 80;
         }
 
+        scalar_t red, green, blue;
+
+        if(random_colors) {
+          do {
+            red = drand48();
+            green = drand48();
+            blue = drand48();
+          } while(red < 0.9 and green < 0.9 and blue < 0.9 and
+                  red > 0.1 and green > 0.1 and blue > 0.1);
+        } else {
+          red = 1.0;
+          green = 1.0;
+          blue = 1.0;
+        }
+
         do {
           scalar_t x[] = { - shape_size * 0.4, + shape_size * 0.4,
                            + shape_size * 0.4, - shape_size * 0.4 };
@@ -146,20 +160,19 @@ extern "C" void fl_generate_sequences(int nb_sequences,
           scalar_t y[] = { - shape_size * 0.6, - shape_size * 0.6,
                            + shape_size * 0.6, + shape_size * 0.6 };
 
-          scalar_t delta = shape_size / sqrt(2.0);
-
-          scalar_t object_center_x = delta + (world_width - 2 * delta) * drand48();
-          scalar_t object_center_y = delta + (world_height - 2 * delta) * drand48();
+          scalar_t object_center_x = world_width * drand48();
+          scalar_t object_center_y = world_height * drand48();
 
           delete pol;
-          pol = new Polygon(0.5, 1.0, 1.0, 1.0, x, y, sizeof(x)/sizeof(scalar_t));
+          pol = new Polygon(0.5, red, green, blue, x, y, sizeof(x) / sizeof(scalar_t));
           pol->set_position(object_center_x, object_center_y, M_PI * 2 * drand48());
           pol->set_speed(0, 0, 0);
 
           universe->initialize_polygon(pol);
 
           nb_attempts++;
-        } while(nb_attempts < nb_attempts_max && universe->collide(pol));
+        } while(nb_attempts < nb_attempts_max &&
+                (universe->collide(pol) || universe->collide_with_borders(pol, 2.0 / scaling)));
 
         if(nb_attempts == nb_attempts_max) {
           delete pol;
@@ -174,60 +187,67 @@ extern "C" void fl_generate_sequences(int nb_sequences,
       grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
     } while(!grabbed_polygon);
 
-    if(skip < 0 || n >= skip) {
+    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 = 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);
 
-      for(int s = 0; 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);
+    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);
 
-          // 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);
+        // 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);
 
-          canvases[t]->clear();
-          draw_universe_on_canvas(canvases[t], scaling, universe);
+        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++) {
-            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);
-            grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
-            universe->update(dt, 1.0 / scaling);
-          }
-        }
+        // if(show_grabbing_point) {
+        // draw_grabbing_point_on_canvas(canvases[2 * t + 1], scaling,
+        // xf, yf, 1.0, 0.0, 0.0);
+        // }
       }
 
-      for(int t = 0; t < nb_images_per_sequence; t++) {
-        unsigned char *src = canvases[t]->_data;
-        unsigned char *dst = output + (n * nb_images_per_sequence + t) * width * height * 3;
-        for(int d = 0; d < 3; d++) {
-          for(int y = 0; y < height; y++) {
-            for(int x = 0; x < width; x++) {
-              dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
-            }
+      if(s < nb_simulated_frames - 1) {
+        // Run the simulation
+        for(int i = 0; i < nb_iterations_per_steps; i++) {
+          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);
         }
       }
     }
 
-    for(int t = 0; t < 2 * nb_saved_frames; t++) {
-      delete canvases[t];
+    if(failed) cout << "** FAILED" << endl;
+    else cout << "** DONE" << endl;
+
+  } while(failed);
+
+  for(int t = 0; t < nb_images; t++) {
+    unsigned char *src = canvases[t]->_data;
+    unsigned char *dst = output + t * width * height * 3;
+    for(int d = 0; d < 3; d++) {
+      for(int y = 0; y < height; y++) {
+        for(int x = 0; x < width; x++) {
+          dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
+        }
+      }
     }
+  }
 
-    delete universe;
+  for(int t = 0; t < 2 * nb_saved_frames; t++) {
+    delete canvases[t];
   }
+
+  delete universe;
 }