4 flatland is a simple 2d physical simulator
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 flatland
11 flatland 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 flatland 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 flatland. If not, see <http://www.gnu.org/licenses/>.
40 #include "canvas_cairo.h"
42 //////////////////////////////////////////////////////////////////////
44 void draw_universe_on_canvas(CanvasCairo *canvas, scalar_t scaling,
46 canvas->set_line_width(1.0 / scaling);
47 universe->draw(canvas);
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;
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);
61 canvas->set_drawing_color(r, g, b);
62 canvas->set_line_width(2.0);
63 canvas->draw_polygon(1, n, xp, yp);
66 //////////////////////////////////////////////////////////////////////
68 extern "C" void fl_generate_sequence(int nb_images,
69 int width, int height,
71 int random_shape_size, int random_colors,
73 unsigned char *output) {
75 const scalar_t super_definition = 8;
76 const scalar_t world_width = width * super_definition;
77 const scalar_t world_height = height * super_definition;
78 const scalar_t scaling = 1 / super_definition;
80 int nb_iterations_per_dt = 100;
81 scalar_t dt = 10.0 / scalar_t(nb_iterations_per_dt);
83 //////////////////////////////////////////////////////////////////////
85 // We will generate images { 0, nb_iterations_per_dt, 2 * nb_iterations_per_dt, ..., k * nb_iterations_per_dt < nb_simulated_frames }
87 // The framerate nb_iterations_per_dt may be set to smaller value to generate
88 // nice materials for presentations or papers.
90 int nb_simulated_frames = 1 + (nb_images - 1) * nb_iterations_per_dt;
94 Polygon *grabbed_polygon;
96 universe = new Universe(nb_shapes, world_width, world_height);
98 const int nb_saved_frames = (nb_simulated_frames + nb_iterations_per_dt - 1) / nb_iterations_per_dt;
99 if(nb_saved_frames != nb_images) {
100 cerr << "It makes no sense." << endl;
104 CanvasCairo *canvases[nb_saved_frames * 2];
106 for(int s = 0; s < 2 * nb_saved_frames; s++) {
107 canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
110 scalar_t gravity_fx = 0.0;
111 scalar_t gravity_fy = 1.0;
113 scalar_t grab_start_x, grab_start_y;
117 int total_nb_attempts = 0;
118 const int max_total_nb_attempts = 1000000;
123 grab_start_x = world_width * (0.1 + 0.8 * drand48());
124 grab_start_y = world_height * (0.1 + 0.8 * drand48());
126 grab_start_x = world_width * 0.5;
127 grab_start_y = world_height * 0.75;
134 const int nb_attempts_max = 100;
137 for(int u = 0; u < nb_shapes; u++) {
144 if(random_shape_size) {
145 shape_size = (10 + 10 * drand48()) * super_definition;
147 shape_size = 15 * super_definition;
150 scalar_t red, green, blue;
157 } while(red < 0.9 and green < 0.9 and blue < 0.9 and
158 red > 0.1 and green > 0.1 and blue > 0.1);
166 scalar_t x[] = { - shape_size * 0.4, + shape_size * 0.4,
167 + shape_size * 0.4, - shape_size * 0.4 };
169 scalar_t y[] = { - shape_size * 0.6, - shape_size * 0.6,
170 + shape_size * 0.6, + shape_size * 0.6 };
172 scalar_t object_center_x = world_width * drand48();
173 scalar_t object_center_y = world_height * drand48();
176 pol = new Polygon(0.5, red, green, blue, x, y, sizeof(x) / sizeof(scalar_t));
177 pol->set_position(object_center_x, object_center_y, M_PI * 2 * drand48());
178 pol->set_speed(0, 0, 0);
180 universe->initialize_polygon(pol);
183 } while(nb_attempts < nb_attempts_max &&
184 (universe->collide(pol) || universe->collide_with_borders(pol, 2.0 / scaling)));
186 if(nb_attempts == nb_attempts_max) {
192 universe->add_polygon(pol);
197 grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
199 } while(pulling and !grabbed_polygon);
203 scalar_t grab_relative_x, grab_relative_y;
206 grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
207 grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
210 for(int s = 0; !failed && s < nb_simulated_frames; s++) {
211 if(s % nb_iterations_per_dt == 0) {
212 int t = s / nb_iterations_per_dt;
214 canvases[t]->clear();
215 draw_universe_on_canvas(canvases[t], scaling, universe);
218 if(s < nb_simulated_frames - 1) {
220 // Run the simulation
223 // Pulling the grabbed rectangle
224 scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
225 scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
226 if (xf < 0 || xf >= world_width || yf < 0 || yf >= world_height) {
229 grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
232 universe->apply_gravity(dt, gravity_fx, gravity_fy);
235 universe->update(dt, 1.0 / scaling);
241 if(total_nb_attempts >= max_total_nb_attempts) {
243 << max_total_nb_attempts
244 << " attempts at generating the sequences, aborting." << endl;
250 for(int t = 0; t < nb_images; t++) {
251 unsigned char *src = canvases[t]->_data;
252 unsigned char *dst = output + t * width * height * 3;
253 for(int d = 0; d < 3; d++) {
254 for(int y = 0; y < height; y++) {
255 for(int x = 0; x < width; x++) {
256 dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
262 for(int t = 0; t < 2 * nb_saved_frames; t++) {