Finishing the renaming.
[flatland.git] / sequence_generator.cc
1
2 /*
3  *  dyncnn is a deep-learning algorithm for the prediction of
4  *  interacting object dynamics
5  *
6  *  Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of dyncnn.
10  *
11  *  dyncnn is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  dyncnn is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with dyncnn.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include <iostream>
26 #include <fstream>
27 #include <cmath>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35
36 using namespace std;
37
38 #include "misc.h"
39 #include "universe.h"
40 #include "canvas_cairo.h"
41
42 //////////////////////////////////////////////////////////////////////
43
44 void draw_universe_on_canvas(CanvasCairo *canvas, scalar_t scaling,
45                              Universe *universe) {
46   canvas->set_line_width(1.0 / scaling);
47   universe->draw(canvas);
48 }
49
50 void draw_grabbing_point_on_canvas(CanvasCairo *canvas, scalar_t scaling,
51                                    scalar_t xg, scalar_t yg,
52                                    scalar_t r, scalar_t g, scalar_t b) {
53   scalar_t radius = 1/scaling;
54   int n = 36;
55   scalar_t xp[n], yp[n];
56   for(int k = 0; k < n; k++) {
57     scalar_t alpha = 2 * M_PI * scalar_t(k) / scalar_t(n);
58     xp[k] = xg + radius * cos(alpha);
59     yp[k] = yg + radius * sin(alpha);
60   }
61   canvas->set_drawing_color(r, g, b);
62   canvas->set_line_width(2.0);
63   canvas->draw_polygon(1, n, xp, yp);
64 }
65
66 //////////////////////////////////////////////////////////////////////
67
68 extern "C" void fl_generate_sequences(int nb_sequences,
69                                       int nb_images_per_sequence,
70                                       int width, int height,
71                                       unsigned char *output) {
72
73   const scalar_t world_width = width * 8;
74   const scalar_t world_height = height * 8;
75   const scalar_t scaling = 0.125;
76
77   const scalar_t dt = 0.1;
78   const int nb_iterations_per_steps = 5;
79
80   //////////////////////////////////////////////////////////////////////
81
82   // We will generate images { 0, every_nth, 2 * every_nth, ..., k * every_nth < nb_simulated_frames }
83
84   // The framerate every_nth may be set to smaller value to generate
85   // nice materials for presentations or papers.
86
87   int every_nth = 16;
88   int nb_simulated_frames = 1 + (nb_images_per_sequence - 1) * every_nth;
89   int random_grasp = 1;
90   int random_shape_size = 0;
91   int nb_shapes = 10;
92   int skip = -1;
93
94   for(int n = 0; n < nb_sequences; n++) {
95
96     Universe *universe;
97     Polygon *grabbed_polygon;
98
99     universe = new Universe(nb_shapes, world_width, world_height);
100
101     const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
102     if(nb_saved_frames != nb_images_per_sequence) {
103       cerr << "It makes no sense." << endl;
104       abort();
105     }
106
107     CanvasCairo *canvases[nb_saved_frames * 2];
108
109     for(int s = 0; s < 2 * nb_saved_frames; s++) {
110       canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
111     }
112
113     scalar_t grab_start_x, grab_start_y;
114
115     if(random_grasp) {
116       grab_start_x = world_width * (0.1 + 0.8 * drand48());
117       grab_start_y = world_height * (0.1 + 0.8 * drand48());
118     } else {
119       grab_start_x = world_width * 0.5;
120       grab_start_y = world_height * 0.75;
121     }
122
123     do {
124       universe->clear();
125
126       const int nb_attempts_max = 100;
127       int nb_attempts = 0;
128
129       for(int u = 0; u < nb_shapes; u++) {
130         Polygon *pol = 0;
131
132         nb_attempts = 0;
133
134         scalar_t shape_size;
135
136         if(random_shape_size) {
137           shape_size = 40 + 80 * drand48();
138         } else {
139           shape_size = 80;
140         }
141
142         do {
143           scalar_t x[] = { - shape_size * 0.4, + shape_size * 0.4,
144                            + shape_size * 0.4, - shape_size * 0.4 };
145
146           scalar_t y[] = { - shape_size * 0.6, - shape_size * 0.6,
147                            + shape_size * 0.6, + shape_size * 0.6 };
148
149           scalar_t delta = shape_size / sqrt(2.0);
150
151           scalar_t object_center_x = delta + (world_width - 2 * delta) * drand48();
152           scalar_t object_center_y = delta + (world_height - 2 * delta) * drand48();
153
154           delete pol;
155           pol = new Polygon(0.5, 1.0, 1.0, 1.0, x, y, sizeof(x)/sizeof(scalar_t));
156           pol->set_position(object_center_x, object_center_y, M_PI * 2 * drand48());
157           pol->set_speed(0, 0, 0);
158
159           universe->initialize_polygon(pol);
160
161           nb_attempts++;
162         } while(nb_attempts < nb_attempts_max && universe->collide(pol));
163
164         if(nb_attempts == nb_attempts_max) {
165           delete pol;
166           u = -1;
167           universe->clear();
168           nb_attempts = 0;
169         } else {
170           universe->add_polygon(pol);
171         }
172       }
173
174       grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
175     } while(!grabbed_polygon);
176
177     if(skip < 0 || n >= skip) {
178
179       scalar_t grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
180       scalar_t grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
181
182       for(int s = 0; s < nb_simulated_frames; s++) {
183         if(s % every_nth == 0) {
184           int t = s / every_nth;
185           // scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
186           // scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
187
188           // canvases[2 * t + 0]->clear();
189           // draw_grabbing_point_on_canvas(canvases[2 * t + 0], scaling,
190           // xf, yf, 0.0, 0.0, 0.0);
191           // canvases[2 * t + 1]->clear();
192           // draw_universe_on_canvas(canvases[2 * t + 1], scaling, universe);
193
194           canvases[t]->clear();
195           draw_universe_on_canvas(canvases[t], scaling, universe);
196
197           // if(show_grabbing_point) {
198           // draw_grabbing_point_on_canvas(canvases[2 * t + 1], scaling,
199           // xf, yf, 1.0, 0.0, 0.0);
200           // }
201         }
202
203         if(s < nb_simulated_frames - 1) {
204           // Run the simulation
205           for(int i = 0; i < nb_iterations_per_steps; i++) {
206             scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
207             scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
208             grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
209             universe->update(dt, 1.0 / scaling);
210           }
211         }
212       }
213
214       for(int t = 0; t < nb_images_per_sequence; t++) {
215         unsigned char *src = canvases[t]->_data;
216         unsigned char *dst = output + (n * nb_images_per_sequence + t) * width * height * 3;
217         for(int d = 0; d < 3; d++) {
218           for(int y = 0; y < height; y++) {
219             for(int x = 0; x < width; x++) {
220               dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
221             }
222           }
223         }
224       }
225     }
226
227     for(int t = 0; t < 2 * nb_saved_frames; t++) {
228       delete canvases[t];
229     }
230
231     delete universe;
232   }
233 }