Update.
authorFrancois Fleuret <francois@fleuret.org>
Thu, 2 Mar 2017 16:41:21 +0000 (17:41 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Thu, 2 Mar 2017 16:41:21 +0000 (17:41 +0100)
flatland.c
flatland.h
polygon.cc
polygon.h
sequence_generator.cc
sequence_generator.h
test.py
universe.cc
universe.h

index 032336a..77f89aa 100644 (file)
 
 #include "sequence_generator.h"
 
-THByteTensor *generate_sequence(long nb_sequences, long nb_images_per_sequence, long image_width, long image_height) {
+THByteTensor *generate_sequence(long nb_sequences,
+                                long nb_images,
+                                long image_height, long image_width,
+                                int random_shape_size, int random_colors) {
+
   long nb_channels = 3;
   unsigned char *a, *b;
   long s, c, k, i, j, st0, st1, st2, st3, st4;
 
   THLongStorage *size = THLongStorage_newWithSize(5);
   size->data[0] = nb_sequences;
-  size->data[1] = nb_images_per_sequence;
+  size->data[1] = nb_images;
   size->data[2] = nb_channels;
   size->data[3] = image_height;
   size->data[4] = image_width;
@@ -38,13 +42,15 @@ THByteTensor *generate_sequence(long nb_sequences, long nb_images_per_sequence,
   st3 = THByteTensor_stride(result, 3);
   st4 = THByteTensor_stride(result, 4);
 
-  unsigned char tmp_buffer[nb_images_per_sequence * nb_channels * image_width * image_height];
+  unsigned char tmp_buffer[nb_images * nb_channels * image_width * image_height];
 
   for(s = 0; s < nb_sequences; s++) {
     a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + s * st0;
-    fl_generate_sequences(1, nb_images_per_sequence, image_width, image_height, tmp_buffer);
+    fl_generate_sequence(nb_images, image_width, image_height,
+                         random_shape_size, random_colors,
+                         tmp_buffer);
     unsigned char *r = tmp_buffer;
-    for(k = 0; k < nb_images_per_sequence; k++) {
+    for(k = 0; k < nb_images; k++) {
       for(c = 0; c < nb_channels; c++) {
         for(i = 0; i < image_height; i++) {
           b = a + k * st1 + c * st2 + i * st3;
index 734add4..e86533e 100644 (file)
@@ -1,4 +1,5 @@
 
 THByteTensor *generate_sequence(long nb_sequences,
-                                long nb_images_per_sequence,
-                                long image_width, long image_height);
+                                long nb_images,
+                                long image_height, long image_width,
+                                int random_shape_size, int random_colors);
index b64fbe4..bfa95f8 100644 (file)
@@ -517,6 +517,14 @@ void Polygon::apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p) {
 
 }
 
+bool Polygon::collide_with_borders(scalar_t xmin, scalar_t ymin,
+                                   scalar_t xmax, scalar_t ymax) {
+  for(int n = 0; n < _nb_vertices; n++) {
+    if(_x[n] <= xmin || _x[n] >= xmax || _y[n] <= ymin || _y[n] >= ymax) return true;
+  }
+  return false;
+}
+
 bool Polygon::collide(Polygon *p) {
   for(int n = 0; n < _nb_vertices; n++) {
     int np = (n+1)%_nb_vertices;
index 557fdd6..acf9775 100644 (file)
--- a/polygon.h
+++ b/polygon.h
@@ -122,6 +122,9 @@ public:
   void apply_border_forces(scalar_t dt, scalar_t xmin, scalar_t ymin, scalar_t xmax, scalar_t ymax);
   void apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p);
 
+  bool collide_with_borders(scalar_t xmin, scalar_t ymin,
+                            scalar_t xmax, scalar_t ymax);
+
   bool collide(Polygon *p);
 };
 
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;
 }
index 9a9247a..76f1d33 100644 (file)
@@ -3,10 +3,10 @@
 extern "C" {
 #endif
 
-void fl_generate_sequences(int nb_sequences,
-                           int nb_images_per_sequence,
-                           int width, int height,
-                           unsigned char *output);
+void fl_generate_sequence(int nb_images,
+                          int width, int height,
+                          int random_shape_size, int random_colors,
+                          unsigned char *output);
 
 #ifdef __cplusplus
 }
diff --git a/test.py b/test.py
index c6b6c48..2853309 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -8,7 +8,7 @@ from _ext import flatland
 
 ######################################################################
 
-def sequences_to_image(x):
+def sequences_to_image(x, gap=1, gap_color = (0, 128, 255)):
     from PIL import Image
 
     nb_sequences = x.size(0)
@@ -21,8 +21,6 @@ def sequences_to_image(x):
 
     height = x.size(3)
     width = x.size(4)
-    gap = 1
-    gap_color = (0, 128, 255)
 
     result = torch.ByteTensor(nb_channels,
                               gap + nb_sequences * (height + gap),
@@ -34,7 +32,9 @@ def sequences_to_image(x):
 
     for s in range(0, nb_sequences):
         for i in range(0, nb_images_per_sequences):
-            result.narrow(1, gap + s * (height + gap), height).narrow(2, gap + i * (width + gap), width).copy_(x[s][i])
+            result.narrow(1, gap + s * (height + gap), height) \
+                  .narrow(2, gap + i * (width + gap), width) \
+                  .copy_(x[s][i])
 
     result_numpy = result.cpu().byte().transpose(0, 2).transpose(0, 1).numpy()
 
@@ -42,6 +42,6 @@ def sequences_to_image(x):
 
 ######################################################################
 
-x = flatland.generate_sequence(5, 3, 128, 96)
+x = flatland.generate_sequence(1, 3, 80, 80, True, True)
 
-sequences_to_image(x).save('sequences.png')
+sequences_to_image(x, gap = 2, gap_color = (0, 0, 0)).save('sequences.png')
index b897d0c..32e6f33 100644 (file)
@@ -62,6 +62,11 @@ void Universe::add_polygon(Polygon *p) {
   }
 }
 
+bool Universe::collide_with_borders(Polygon *p, scalar_t padding) {
+  return p->collide_with_borders(padding, padding,_width - padding, _height - padding);
+
+}
+
 bool Universe::collide(Polygon *p) {
   for(int n = 0; n < _nb_polygons; n++)
     if(_polygons[n] && _polygons[n]->collide(p)) return true;
index c80bf2c..3537750 100644 (file)
@@ -50,11 +50,14 @@ public:
   void initialize_polygon(Polygon *p);
   void clear();
   void add_polygon(Polygon *p);
+
+  bool collide_with_borders(Polygon *p, scalar_t padding);
   bool collide(Polygon *p);
 
   // Compute collisions between projections of the polygons on a few
   // axis to speed up the computation
   void compute_pseudo_collisions(int nb_axis, int *nb_colliding_axis);
+
   void apply_collision_forces(scalar_t dt);
   bool update(scalar_t dt, scalar_t padding);