Update.
[universe.git] / main.cc
diff --git a/main.cc b/main.cc
index bbea852..0f82f3f 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -17,59 +17,17 @@ using namespace std;
 #include "task.h"
 #include "simple_window.h"
 #include "universe.h"
 #include "task.h"
 #include "simple_window.h"
 #include "universe.h"
-#include "retina.h"
+#include "plotter.h"
 #include "manipulator.h"
 #include "intelligence.h"
 #include "manipulator.h"
 #include "intelligence.h"
-#include "xfig_tracer.h"
 
 
-#ifdef CAIRO_SUPPORT
-#include <cairo.h>
-
-static cairo_status_t write_cairo_to_file(void *closure,
-                                          const unsigned char *data,
-                                          unsigned int length) {
-  fwrite(data, 1, length, (FILE *) closure);
-  return CAIRO_STATUS_SUCCESS;
-}
+#include "canvas_cairo.h"
 
 void generate_png(Universe *universe, scalar_t scale, FILE *file) {
 
 void generate_png(Universe *universe, scalar_t scale, FILE *file) {
-  const int depth = 4;
-  const int width = int(universe->width() * scale);
-  const int height = int(universe->height() * scale);
-
-  cairo_surface_t *image;
-  cairo_t* context_resource;
-  unsigned char *data;
-
-  data = new unsigned char [width * height * depth];
-
-  image = cairo_image_surface_create_for_data(data,
-                                              CAIRO_FORMAT_RGB24,
-                                              width,
-                                              height,
-                                              width * depth);
-
-  context_resource = cairo_create(image);
-
-  cairo_scale(context_resource, scale, scale);
-
-  cairo_set_source_rgb(context_resource, 1.0, 1.0, 1.0);
-
-  cairo_rectangle(context_resource, 0, 0,
-                  universe->width(), universe->height());
-
-  cairo_fill(context_resource);
-
-  universe->draw(context_resource);
-
-  cairo_surface_write_to_png_stream(image, write_cairo_to_file, file);
-
-  cairo_destroy(context_resource);
-  cairo_surface_destroy(image);
-
-  delete[] data;
+  CanvasCairo canvas(scale, universe->width(), universe->height());
+  universe->draw(&canvas);
+  canvas.write_png(file);
 }
 }
-#endif
 
 // To train
 // ./main --task hit_shape.task 0 --action-mode=random --nb-ticks=5000 --proportion-for-training=0.5 --save-file=dump.mem --no-window
 
 // To train
 // ./main --task hit_shape.task 0 --action-mode=random --nb-ticks=5000 --proportion-for-training=0.5 --save-file=dump.mem --no-window
@@ -126,6 +84,7 @@ int main(int argc, char **argv) {
   scalar_t last_hand_x = 0, last_hand_y = 0;
   Polygon *last_grabbing = 0;
   bool no_window = false;
   scalar_t last_hand_x = 0, last_hand_y = 0;
   Polygon *last_grabbing = 0;
   bool no_window = false;
+  bool save_frames = false;
 
   //////////////////////////////////////////////////////////////////////
   //                    Parsing the shell arguments
 
   //////////////////////////////////////////////////////////////////////
   //                    Parsing the shell arguments
@@ -159,23 +118,28 @@ int main(int argc, char **argv) {
         while(*o) *u++ = *o++;
       }
 
         while(*o) *u++ = *o++;
       }
 
-      if(strcmp(variable_name, "nb-ticks") == 0)
+      if(strcmp(variable_name, "nb-ticks") == 0) {
         nb_ticks = atoi(variable_value);
         nb_ticks = atoi(variable_value);
-      else if(strcmp(variable_name, "nb-training-iterations") == 0)
+      } else if(strcmp(variable_name, "nb-training-iterations") == 0) {
         nb_training_iterations = atoi(variable_value);
         nb_training_iterations = atoi(variable_value);
-      else if(strcmp(variable_name, "proportion-for-training") == 0)
+      } else if(strcmp(variable_name, "proportion-for-training") == 0) {
         proportion_for_training = atof(variable_value);
         proportion_for_training = atof(variable_value);
-      else if(strcmp(variable_name, "no-window") == 0)
+      } else if(strcmp(variable_name, "no-window") == 0) {
         no_window = true;
         no_window = true;
-      else if(strcmp(variable_name, "save-file") == 0)
+      } else if(strcmp(variable_name, "save-file") == 0) {
         strcpy(intelligence_save_file, variable_value);
         strcpy(intelligence_save_file, variable_value);
-      else if(strcmp(variable_name, "load-file") == 0)
+      } else if(strcmp(variable_name, "load-file") == 0) {
         strcpy(intelligence_load_file, variable_value);
         strcpy(intelligence_load_file, variable_value);
-      else if(strcmp(variable_name, "action-mode") == 0) {
-        if(strcmp(variable_value, "idle") == 0) action_mode = IDLE;
-        else if(strcmp(variable_value, "random") == 0) action_mode = RANDOM;
-        else if(strcmp(variable_value, "intelligent") == 0) action_mode = INTELLIGENT;
-        else {
+      } else if(strcmp(variable_name, "save-frames") == 0) {
+        save_frames = true;
+      } else if(strcmp(variable_name, "action-mode") == 0) {
+        if(strcmp(variable_value, "idle") == 0) {
+          action_mode = IDLE;
+        } else if(strcmp(variable_value, "random") == 0) {
+          action_mode = RANDOM;
+        } else if(strcmp(variable_value, "intelligent") == 0) {
+          action_mode = INTELLIGENT;
+        } else {
           cerr << "The only known modes are idle, random and intelligent" << endl;
           exit(1);
         }
           cerr << "The only known modes are idle, random and intelligent" << endl;
           exit(1);
         }
@@ -193,7 +157,7 @@ int main(int argc, char **argv) {
   cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
 
   if(!task) {
   cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
 
   if(!task) {
-    task = load_task("dummy.so");
+    task = load_task("./dummy.so");
     task_degree = 0;
   }
 
     task_degree = 0;
   }
 
@@ -212,20 +176,17 @@ int main(int argc, char **argv) {
 
   Universe universe(100, task->width(), task->height());
   task->init(&universe, task_degree);
 
   Universe universe(100, task->width(), task->height());
   task->init(&universe, task_degree);
-  Retina retina(&universe);
   Manipulator manipulator(task);
   manipulator.force_move(task->width()/2, task->height()/2);
   Manipulator manipulator(task);
   manipulator.force_move(task->width()/2, task->height()/2);
-  retina.set_location(manipulator.hand_x(), manipulator.hand_y());
 
 
-  SimpleWindow *window_main = 0, *window_brain = 0;
+  SimpleWindow *window_main = 0;
   int window_main_fd = -1;
 
 #ifdef CAIRO_SUPPORT
   int window_main_fd = -1;
 
 #ifdef CAIRO_SUPPORT
-  cairo_t *window_main_cairo_cr = 0;
+  // cairo_t *window_main_cairo_cr = 0;
 #endif
 
   MapConcatener sensory_map(2);
 #endif
 
   MapConcatener sensory_map(2);
-  sensory_map.add_map(&retina);
   sensory_map.add_map(&manipulator);
   sensory_map.init();
 
   sensory_map.add_map(&manipulator);
   sensory_map.init();
 
@@ -255,14 +216,10 @@ int main(int argc, char **argv) {
     window_main_fd = window_main->file_descriptor();
     window_main->map();
 #ifdef CAIRO_SUPPORT
     window_main_fd = window_main->file_descriptor();
     window_main->map();
 #ifdef CAIRO_SUPPORT
-    window_main_cairo_cr = window_main->get_cairo_context_resource();
+    // window_main_cairo_cr = window_main->get_cairo_context_resource();
 #endif
     cout << "When the main window has the focus, press `q' to quit and click and drag to move" << endl
          << "objects." << endl;
 #endif
     cout << "When the main window has the focus, press `q' to quit and click and drag to move" << endl
          << "objects." << endl;
-    window_brain = new SimpleWindow("Universe (brain)",
-                                    12 + task->width(), 4,
-                                    retina.width(), retina.height() + manipulator.parameter_height());
-    window_brain->map();
   }
 
   int tick = 0;
   }
 
   int tick = 0;
@@ -278,6 +235,16 @@ int main(int argc, char **argv) {
     int r;
     fd_set fds;
 
     int r;
     fd_set fds;
 
+#ifdef CAIRO_SUPPORT
+    if(save_frames) {
+      char buffer[1024];
+      sprintf(buffer, "frame-%06d.png", tick);
+      FILE *file = fopen(buffer, "w");
+      generate_png(&universe, 0.25, file);
+      cout << "Universe image saved in " << buffer << endl;
+      fclose(file);
+    }
+#endif
 
     if(window_main) {
       struct timeval tv;
 
     if(window_main) {
       struct timeval tv;
@@ -321,6 +288,7 @@ int main(int argc, char **argv) {
         for(int k = 0; k < nb_it; k++) {
           manipulator.update(dt, &universe);
           task->update(dt, &universe, &manipulator);
         for(int k = 0; k < nb_it; k++) {
           manipulator.update(dt, &universe);
           task->update(dt, &universe, &manipulator);
+          universe.apply_gravity(dt, 0.0, 2.0);
           changed |= universe.update(dt);
         }
 
           changed |= universe.update(dt);
         }
 
@@ -340,23 +308,14 @@ int main(int argc, char **argv) {
           last_hand_y = manipulator.hand_y();
           last_grabbing = manipulator.grabbing();
 
           last_hand_y = manipulator.hand_y();
           last_grabbing = manipulator.grabbing();
 
-          retina.set_location(manipulator.hand_x(),
-                              manipulator.hand_y());
-
           if(window_main) {
             window_main->color(0.0, 0.0, 0.0);
             window_main->color(1.0, 1.0, 1.0);
             window_main->fill();
           if(window_main) {
             window_main->color(0.0, 0.0, 0.0);
             window_main->color(1.0, 1.0, 1.0);
             window_main->fill();
-
-#ifdef CAIRO_SUPPORT
-            universe.draw(window_main_cairo_cr);
-#else
             universe.draw(window_main);
             universe.draw(window_main);
-#endif
 
             task->draw(window_main);
             manipulator.draw_on_universe(window_main);
 
             task->draw(window_main);
             manipulator.draw_on_universe(window_main);
-            retina.draw_on_universe(window_main);
 
             if(grabbed_polygon) {
               int x, y, delta = 3;
 
             if(grabbed_polygon) {
               int x, y, delta = 3;
@@ -368,12 +327,6 @@ int main(int argc, char **argv) {
             }
 
             window_main->show();
             }
 
             window_main->show();
-
-            if(window_brain) {
-              retina.draw_parameters(0, 0, window_brain);
-              manipulator.draw_parameters(0, retina.height() + 1, window_brain);
-              window_brain->show();
-            }
           }
         }
 
           }
         }
 
@@ -458,20 +411,17 @@ int main(int argc, char **argv) {
 
               else if(strcmp(se.key, "s") == 0) {
 
 
               else if(strcmp(se.key, "s") == 0) {
 
-                retina.save_as_ppm("/tmp/retina.ppm");
-                cout << "Retina screen shot saved in /tmp/retina.ppm" << endl;
-
                 {
                 {
-                  XFigTracer tracer("/tmp/universe.fig");
-                  universe.print_xfig(&tracer);
+                  Plotter plotter(int(universe.width()), int(universe.height()), 4);
+                  plotter.save_as_ppm(&universe, "/tmp/plotter.ppm", 16);
                 }
 
 #ifdef CAIRO_SUPPORT
                 {
                 }
 
 #ifdef CAIRO_SUPPORT
                 {
-                  FILE *file = fopen("/tmp/universe.png", "w");
+                  FILE *file = fopen("/tmp/screenshot.png", "w");
                   generate_png(&universe, 0.25, file);
                   generate_png(&universe, 0.25, file);
-                  // generate_png(task->width(), task->height(), &universe, "/tmp/universe.png");
-                  cout << "Universe image saved in /tmp/universe.png" << endl;
+                  cout << "Universe image saved in /tmp/screenshot.png" << endl;
+                  fclose(file);
                 }
 #endif
 
                 }
 #endif
 
@@ -561,7 +511,6 @@ int main(int argc, char **argv) {
     intelligence.save(os);
   }
 
     intelligence.save(os);
   }
 
-  delete window_brain;
   delete window_main;
 
 }
   delete window_main;
 
 }