Update.
[universe.git] / main.cc
diff --git a/main.cc b/main.cc
index cb94b7f..0f82f3f 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -1,17 +1,6 @@
 
-////////////////////////////////////////////////////////////////////////////////
-// This program is free software; you can redistribute it and/or              //
-// modify it under the terms of the GNU General Public License                //
-// version 2 as published by the Free Software Foundation.                    //
-//                                                                            //
-// This program 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.                                   //
-//                                                                            //
-// Written and (C) by François Fleuret                                        //
-// Contact <francois.fleuret@epfl.ch> for comments & bug reports              //
-////////////////////////////////////////////////////////////////////////////////
+// Written and (C) by Francois Fleuret
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports
 
 #include <iostream>
 #include <fstream>
@@ -28,10 +17,18 @@ using namespace std;
 #include "task.h"
 #include "simple_window.h"
 #include "universe.h"
-#include "retina.h"
+#include "plotter.h"
 #include "manipulator.h"
 #include "intelligence.h"
 
+#include "canvas_cairo.h"
+
+void generate_png(Universe *universe, scalar_t scale, FILE *file) {
+  CanvasCairo canvas(scale, universe->width(), universe->height());
+  universe->draw(&canvas);
+  canvas.write_png(file);
+}
+
 // 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
 
@@ -40,7 +37,7 @@ using namespace std;
 
 //////////////////////////////////////////////////////////////////////
 
-void check_opt(int argc, char **argv, int n_opt, int n, char *help) {
+void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
   if(n_opt + n >= argc) {
     cerr << "Missing argument for " << argv[n_opt] << "." << endl;
     cerr << "Expecting " << help << "." << endl;
@@ -49,8 +46,6 @@ void check_opt(int argc, char **argv, int n_opt, int n, char *help) {
 }
 
 void print_help_and_exit(int e) {
-  cout << "$Id: main.cc,v 1.89 2007-06-16 13:51:53 fleuret Exp $" << endl;
-  cout << endl;
   cout << "Arguments:" << endl;
   cout << "  --no-window" << endl;
   cout << "  --nb-ticks=<int: number of ticks>" << endl;
@@ -89,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;
+  bool save_frames = false;
 
   //////////////////////////////////////////////////////////////////////
   //                    Parsing the shell arguments
@@ -122,23 +118,28 @@ int main(int argc, char **argv) {
         while(*o) *u++ = *o++;
       }
 
-      if(strcmp(variable_name, "nb-ticks") == 0)
+      if(strcmp(variable_name, "nb-ticks") == 0) {
         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);
-      else if(strcmp(variable_name, "proportion-for-training") == 0)
+      } else if(strcmp(variable_name, "proportion-for-training") == 0) {
         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;
-      else if(strcmp(variable_name, "save-file") == 0)
+      } else if(strcmp(variable_name, "save-file") == 0) {
         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);
-      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);
         }
@@ -154,10 +155,9 @@ int main(int argc, char **argv) {
   }
 
   cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
-  cout << "$Id: main.cc,v 1.89 2007-06-16 13:51:53 fleuret Exp $" << endl;
 
   if(!task) {
-    task = load_task("dummy.task");
+    task = load_task("./dummy.so");
     task_degree = 0;
   }
 
@@ -176,17 +176,17 @@ int main(int argc, char **argv) {
 
   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);
-  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;
 
-  int winfd = -1;
+#ifdef CAIRO_SUPPORT
+  // cairo_t *window_main_cairo_cr = 0;
+#endif
 
   MapConcatener sensory_map(2);
-  sensory_map.add_map(&retina);
   sensory_map.add_map(&manipulator);
   sensory_map.init();
 
@@ -209,18 +209,17 @@ int main(int argc, char **argv) {
     cout << "done." << endl ;
   }
 
-  if(!no_window) {
+  if(no_window) {
+    cout << "Started without windows." << endl;
+  } else {
     window_main = new SimpleWindow("Universe (main window)", 4, 4, task->width(), task->height());
-    winfd = window_main->file_descriptor();
+    window_main_fd = window_main->file_descriptor();
     window_main->map();
+#ifdef CAIRO_SUPPORT
+    // 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;
-    window_brain = new SimpleWindow("Universe (brain)",
-                                     12 + task->width(), 4,
-                                     retina.width(), retina.height() + manipulator.parameter_height());
-    window_brain->map();
-  } else {
-    cout << "Started without windows." << endl;
   }
 
   int tick = 0;
@@ -236,14 +235,24 @@ int main(int argc, char **argv) {
     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;
       FD_ZERO (&fds);
-      FD_SET (winfd, &fds);
+      FD_SET (window_main_fd, &fds);
       tv.tv_sec = 0;
       tv.tv_usec = 5000; // 0.05s
-      r = select(winfd + 1, &fds, 0, 0, &tv);
+      r = select(window_main_fd + 1, &fds, 0, 0, &tv);
     } else r = 0;
 
     time_t t = time(0);
@@ -279,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);
+          universe.apply_gravity(dt, 0.0, 2.0);
           changed |= universe.update(dt);
         }
 
@@ -297,15 +307,15 @@ int main(int argc, char **argv) {
           last_hand_x = manipulator.hand_x();
           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();
-            task->draw(window_main);
             universe.draw(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;
@@ -317,12 +327,6 @@ int main(int argc, char **argv) {
             }
 
             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();
-            }
           }
         }
 
@@ -330,7 +334,7 @@ int main(int argc, char **argv) {
 
       got_event = true;
 
-      if(FD_ISSET(winfd, &fds)) {
+      if(FD_ISSET(window_main_fd, &fds)) {
 
         SimpleEvent se;
 
@@ -383,18 +387,17 @@ int main(int argc, char **argv) {
 
           case SimpleEvent::MOUSE_MOTION:
             {
-              if(press_shift) manipulator.force_move(se.x, se.y);
-              else {
-                if(grabbed_polygon) {
-                  scalar_t xf, yf, force_x, force_y, f, fmax = 100;
-                  xf = grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y);
-                  yf = grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y);
-                  force_x = se.x - xf;
-                  force_y = se.y - yf;
-                  f = sqrt(sq(force_x) + sq(force_y));
-                  if(f > fmax) { force_x = (force_x * fmax)/f; force_y = (force_y * fmax)/f; }
-                  grabbed_polygon->apply_force(0.1, xf, yf, force_x, force_y);
-                }
+              if(press_shift) {
+                manipulator.force_move(se.x, se.y);
+              } else if(grabbed_polygon) {
+                scalar_t xf, yf, force_x, force_y, f, fmax = 100;
+                xf = grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y);
+                yf = grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y);
+                force_x = se.x - xf;
+                force_y = se.y - yf;
+                f = sqrt(sq(force_x) + sq(force_y));
+                if(f > fmax) { force_x = (force_x * fmax)/f; force_y = (force_y * fmax)/f; }
+                grabbed_polygon->apply_force(0.1, xf, yf, force_x, force_y);
               }
               break;
             }
@@ -402,23 +405,55 @@ int main(int argc, char **argv) {
 
           case SimpleEvent::KEY_PRESS:
             {
-              if(strcmp(se.key, "q") == 0) quit = true;
+              if(strcmp(se.key, "q") == 0) {
+                quit = true;
+              }
+
               else if(strcmp(se.key, "s") == 0) {
-                retina.save_as_ppm("/tmp/retina.ppm");
-                cout << "Retina screen shot saved in /tmp/retina.ppm" << endl;
+
                 {
-                  ofstream out("/tmp/universe.fig");
-                  universe.print_fig(out);
+                  Plotter plotter(int(universe.width()), int(universe.height()), 4);
+                  plotter.save_as_ppm(&universe, "/tmp/plotter.ppm", 16);
                 }
+
+#ifdef CAIRO_SUPPORT
+                {
+                  FILE *file = fopen("/tmp/screenshot.png", "w");
+                  generate_png(&universe, 0.25, file);
+                  cout << "Universe image saved in /tmp/screenshot.png" << endl;
+                  fclose(file);
+                }
+#endif
+
               }
-              else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = true;
 
-              else if(strcmp(se.key, "Up") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_UP);
-              else if(strcmp(se.key, "Right") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_RIGHT);
-              else if(strcmp(se.key, "Down") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_DOWN);
-              else if(strcmp(se.key, "Left") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_LEFT);
-              else if(strcmp(se.key, "g") == 0) manipulator.do_action(Manipulator::ACTION_GRAB);
-              else if(strcmp(se.key, "r") == 0) manipulator.do_action(Manipulator::ACTION_RELEASE);
+              else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) {
+                press_shift = true;
+              }
+
+              else if(strcmp(se.key, "Up") == 0) {
+                manipulator.do_action(Manipulator::ACTION_MOVE_UP);
+              }
+
+              else if(strcmp(se.key, "Right") == 0) {
+                manipulator.do_action(Manipulator::ACTION_MOVE_RIGHT);
+              }
+
+              else if(strcmp(se.key, "Down") == 0) {
+                manipulator.do_action(Manipulator::ACTION_MOVE_DOWN);
+              }
+
+              else if(strcmp(se.key, "Left") == 0) {
+                manipulator.do_action(Manipulator::ACTION_MOVE_LEFT);
+              }
+
+              else if(strcmp(se.key, "g") == 0) {
+                manipulator.do_action(Manipulator::ACTION_GRAB);
+              }
+
+              else if(strcmp(se.key, "r") == 0) {
+                manipulator.do_action(Manipulator::ACTION_RELEASE);
+              }
 
               else if(strcmp(se.key, "space") == 0) {
                 switch(action_mode) {
@@ -476,7 +511,6 @@ int main(int argc, char **argv) {
     intelligence.save(os);
   }
 
-  delete window_brain;
   delete window_main;
 
 }