3 * dyncnn is a deep-learning algorithm for the prediction of
4 * interacting object dynamics
6 * Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of dyncnn.
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.
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.
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/>.
40 #include "canvas_cairo.h"
42 FILE *safe_fopen(const char *name, const char *mode) {
43 FILE *file = fopen(name, mode);
45 cerr << "Cannot open " << name << endl;
51 void print_help(const char *command) {
52 cerr << command << " <nb sequences to generate> [--dir <dir>] [--seed <seed>]]" << endl;
56 //////////////////////////////////////////////////////////////////////
58 void draw_universe_on_canvas(CanvasCairo *canvas, scalar_t scaling,
60 canvas->set_line_width(1.0 / scaling);
61 universe->draw(canvas);
64 void draw_grabbing_point_on_canvas(CanvasCairo *canvas, scalar_t scaling,
65 scalar_t xg, scalar_t yg,
66 scalar_t r, scalar_t g, scalar_t b) {
67 scalar_t radius = 1/scaling;
69 scalar_t xp[n], yp[n];
70 for(int k = 0; k < n; k++) {
71 scalar_t alpha = 2 * M_PI * scalar_t(k) / scalar_t(n);
72 xp[k] = xg + radius * cos(alpha);
73 yp[k] = yg + radius * sin(alpha);
75 canvas->set_drawing_color(r, g, b);
76 canvas->set_line_width(2.0);
77 canvas->draw_polygon(1, n, xp, yp);
80 //////////////////////////////////////////////////////////////////////
82 extern "C" void fl_generate_sequences(int nb_sequences,
83 int nb_images_per_sequence,
84 int width, int height,
85 unsigned char *output) {
87 const scalar_t world_width = width * 8;
88 const scalar_t world_height = height * 8;
89 const scalar_t scaling = 0.125;
91 const scalar_t dt = 0.1;
92 const int nb_iterations_per_steps = 5;
94 //////////////////////////////////////////////////////////////////////
96 // We will generate images { 0, every_nth, 2 * every_nth, ..., k * every_nth < nb_simulated_frames }
98 // The framerate every_nth may be set to smaller value to generate
99 // nice materials for presentations or papers.
102 int nb_simulated_frames = 1 + (nb_images_per_sequence - 1) * every_nth;
103 int random_grasp = 1;
104 int random_shape_size = 0;
106 // char data_dir[1024] = "/tmp/";
107 // int show_grabbing_point = 0;
110 for(int n = 0; n < nb_sequences; n++) {
113 Polygon *grabbed_polygon;
115 universe = new Universe(nb_shapes, world_width, world_height);
117 const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
118 if(nb_saved_frames != nb_images_per_sequence) {
119 cerr << "It makes no sense." << endl;
123 CanvasCairo *canvases[nb_saved_frames * 2];
125 for(int s = 0; s < 2 * nb_saved_frames; s++) {
126 canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
129 scalar_t grab_start_x, grab_start_y;
132 grab_start_x = world_width * (0.1 + 0.8 * drand48());
133 grab_start_y = world_height * (0.1 + 0.8 * drand48());
135 grab_start_x = world_width * 0.5;
136 grab_start_y = world_height * 0.75;
142 const int nb_attempts_max = 100;
145 for(int u = 0; u < nb_shapes; u++) {
152 if(random_shape_size) {
153 shape_size = 40 + 80 * drand48();
159 scalar_t x[] = { - shape_size * 0.4, + shape_size * 0.4,
160 + shape_size * 0.4, - shape_size * 0.4 };
162 scalar_t y[] = { - shape_size * 0.6, - shape_size * 0.6,
163 + shape_size * 0.6, + shape_size * 0.6 };
165 scalar_t delta = shape_size / sqrt(2.0);
167 scalar_t object_center_x = delta + (world_width - 2 * delta) * drand48();
168 scalar_t object_center_y = delta + (world_height - 2 * delta) * drand48();
171 pol = new Polygon(0.5, 1.0, 1.0, 1.0, x, y, sizeof(x)/sizeof(scalar_t));
172 pol->set_position(object_center_x, object_center_y, M_PI * 2 * drand48());
173 pol->set_speed(0, 0, 0);
175 universe->initialize_polygon(pol);
178 } while(nb_attempts < nb_attempts_max && universe->collide(pol));
180 if(nb_attempts == nb_attempts_max) {
186 universe->add_polygon(pol);
190 grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
191 } while(!grabbed_polygon);
193 if(skip < 0 || n >= skip) {
195 scalar_t grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
196 scalar_t grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
198 for(int s = 0; s < nb_simulated_frames; s++) {
199 if(s % every_nth == 0) {
200 int t = s / every_nth;
201 // scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
202 // scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
204 // canvases[2 * t + 0]->clear();
205 // draw_grabbing_point_on_canvas(canvases[2 * t + 0], scaling,
206 // xf, yf, 0.0, 0.0, 0.0);
207 // canvases[2 * t + 1]->clear();
208 // draw_universe_on_canvas(canvases[2 * t + 1], scaling, universe);
210 canvases[t]->clear();
211 draw_universe_on_canvas(canvases[t], scaling, universe);
213 // if(show_grabbing_point) {
214 // draw_grabbing_point_on_canvas(canvases[2 * t + 1], scaling,
215 // xf, yf, 1.0, 0.0, 0.0);
219 if(s < nb_simulated_frames - 1) {
220 // Run the simulation
221 for(int i = 0; i < nb_iterations_per_steps; i++) {
222 scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
223 scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
224 grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
225 universe->update(dt);
230 for(int t = 0; t < nb_images_per_sequence; t++) {
231 unsigned char *src = canvases[t]->_data;
232 unsigned char *dst = output + (n * nb_images_per_sequence + t) * width * height * 3;
233 for(int d = 0; d < 3; d++) {
234 for(int y = 0; y < height; y++) {
235 for(int x = 0; x < width; x++) {
236 dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
244 cout << canvases[0]->_actual_width << " "<< canvases[1]->_actual_height << endl;
245 cout << "Writing /tmp/sanity.png" << endl;
246 CanvasCairo main_canvas(scaling, nb_images_per_sequence, 1, canvases);
247 FILE *file = safe_fopen("/tmp/sanity.png", "w");
248 main_canvas.write_png(file);
254 for(int t = 0; t < 2 * nb_saved_frames; t++) {