Started to implement the gravity by making the pulling optional.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 31 Mar 2017 17:23:59 +0000 (19:23 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 31 Mar 2017 17:23:59 +0000 (19:23 +0200)
flatland.c
flatland.h
sequence_generator.cc
sequence_generator.h
test.py

index a77d7c1..df5075b 100644 (file)
 
 #include "sequence_generator.h"
 
-THByteTensor *generate_sequence(long nb_sequences,
+THByteTensor *generate_sequence(int pulling,
+                                long nb_sequences,
                                 long nb_images,
                                 long image_height, long image_width,
-                                int nb_shapes,
+                                long nb_shapes,
                                 int random_shape_size, int random_colors) {
 
   long nb_channels = 3;
@@ -49,6 +50,7 @@ THByteTensor *generate_sequence(long nb_sequences,
     a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + s * st0;
     fl_generate_sequence(nb_images, image_width, image_height, nb_shapes,
                          random_shape_size, random_colors,
+                         pulling,
                          tmp_buffer);
     unsigned char *r = tmp_buffer;
     for(k = 0; k < nb_images; k++) {
index ba91f67..cdde100 100644 (file)
@@ -1,6 +1,7 @@
 
-THByteTensor *generate_sequence(long nb_sequences,
+THByteTensor *generate_sequence(int pulling,
+                                long nb_sequences,
                                 long nb_images,
                                 long image_height, long image_width,
-                                int nb_shapes,
+                                long nb_shapes,
                                 int random_shape_size, int random_colors);
index ddc337b..bfba036 100644 (file)
@@ -69,6 +69,7 @@ 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 super_definition = 8;
@@ -115,12 +116,14 @@ extern "C" void fl_generate_sequence(int nb_images,
   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 {
@@ -188,45 +191,41 @@ 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;
+
+    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);
+    }
 
     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[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);
-          if (xf < 0 || xf >= world_width || yf < 0 || yf >= world_height) {
-            failed = 1;
+          if(pulling) {
+            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
           }
-          grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
           universe->update(dt, 1.0 / scaling);
         }
       }
index 7bfc7ea..daaf36b 100644 (file)
@@ -7,6 +7,7 @@ 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);
 
 #ifdef __cplusplus
diff --git a/test.py b/test.py
index ac3eb7e..e058269 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -9,8 +9,8 @@ from _ext import flatland
 ######################################################################
 
 parser = argparse.ArgumentParser(
-    description='Dummy test of the flatland sequence generation.',
-    formatter_class=argparse.ArgumentDefaultsHelpFormatter
+    description = 'Dummy test of the flatland sequence generation.',
+    formatter_class = argparse.ArgumentDefaultsHelpFormatter
 )
 
 parser.add_argument('--seed',
@@ -84,7 +84,8 @@ def sequences_to_image(x, gap = 1, gap_color = (0, 128, 255)):
 
 ######################################################################
 
-x = flatland.generate_sequence(args.nb_sequences,
+x = flatland.generate_sequence(False,
+                               args.nb_sequences,
                                args.nb_images_per_sequences,
                                args.height, args.width,
                                args.nb_shapes,