b0dfd875a5c070dd1514d7250193f2664ddb6a69
[universe.git] / generate.cc
1
2 ////////////////////////////////////////////////////////////////////
3 // START_IP_HEADER                                                //
4 //                                                                //
5 // Written by Francois Fleuret                                    //
6 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
7 //                                                                //
8 // END_IP_HEADER                                                  //
9 ////////////////////////////////////////////////////////////////////
10
11 #include <iostream>
12 #include <fstream>
13 #include <cmath>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <errno.h>
18 #include <string.h>
19
20 using namespace std;
21
22 #include "misc.h"
23 #include "task.h"
24 #include "simple_window.h"
25 #include "universe.h"
26 #include "plotter.h"
27 #include "retina.h"
28 #include "manipulator.h"
29 #include "intelligence.h"
30 #include "canvas_cairo.h"
31
32 void generate_png(Universe *universe, scalar_t scale, FILE *file) {
33   CanvasCairo canvas(scale, universe->width(), universe->height());
34   universe->draw(&canvas);
35   canvas.write_png(file);
36 }
37
38 int main(int argc, char **argv) {
39   scalar_t world_width = 400;
40   scalar_t world_height = 400;
41   scalar_t square_size = 100;
42   scalar_t hand_x = world_width * 0.5;
43   scalar_t hand_y = world_height * 0.5;
44
45   Universe *universe;
46   Polygon *grabbed_polygon;
47   int nb_attempts = 0;
48
49   srand48(atoi(argv[1]));
50
51   do {
52     universe = new Universe(10, world_width, world_height);
53
54     // scalar_t object_center_x = world_width * 0.5;
55     // scalar_t object_center_y = world_height * 0.5;
56
57     scalar_t object_center_x = world_width * drand48();
58     scalar_t object_center_y = world_height * drand48();
59
60     scalar_t x[] = {
61       - square_size * 0.5,
62       + square_size * 0.5,
63       + square_size * 0.5,
64       - square_size * 0.5,
65     };
66
67     scalar_t y[] = {
68       - square_size * 0.5,
69       - square_size * 0.5,
70       + square_size * 0.5,
71       + square_size * 0.5,
72     };
73
74     Polygon *pol = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
75     pol->set_position(object_center_x, object_center_y, M_PI/3);
76     pol->set_speed(0, 0, 0);
77     universe->initialize_polygon(pol);
78     universe->add_polygon(pol);
79
80     grabbed_polygon = universe->pick_polygon(hand_x, hand_y);
81
82     nb_attempts++;
83     if(nb_attempts > 1000) {
84       cerr << "Could not initialize the universe with a grabbed polygon after 1000 attempts. Aborting().";
85       abort();
86     }
87
88   } while(!grabbed_polygon);
89
90   {
91     FILE *file = fopen("universe1.png", "w");
92     generate_png(universe, 0.25, file);
93   }
94
95   scalar_t grab_relative_x = grabbed_polygon->relative_x(hand_x, hand_y);
96   scalar_t grab_relative_y = grabbed_polygon->relative_y(hand_x, hand_y);
97
98   scalar_t dt = 1.0;
99   for(int i = 0; i < 10; i++) {
100     scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
101     scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
102     scalar_t force_x = (hand_x - xf) * 10;
103     scalar_t force_y = (hand_y - yf) * 10;
104     grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
105     universe->update(dt);
106   }
107
108   {
109     FILE *file = fopen("universe2.png", "w");
110     generate_png(universe, 0.25, file);
111   }
112
113   delete universe;
114 }