#include "sequence_generator.h"
-THByteTensor *generate_sequence(long nb_sequences, long nb_images_per_sequence, long image_width, long image_height) {
+THByteTensor *generate_sequence(long nb_sequences,
+ long nb_images,
+ long image_height, long image_width,
+ int random_shape_size, int random_colors) {
+
long nb_channels = 3;
unsigned char *a, *b;
long s, c, k, i, j, st0, st1, st2, st3, st4;
THLongStorage *size = THLongStorage_newWithSize(5);
size->data[0] = nb_sequences;
- size->data[1] = nb_images_per_sequence;
+ size->data[1] = nb_images;
size->data[2] = nb_channels;
size->data[3] = image_height;
size->data[4] = image_width;
st3 = THByteTensor_stride(result, 3);
st4 = THByteTensor_stride(result, 4);
- unsigned char tmp_buffer[nb_images_per_sequence * nb_channels * image_width * image_height];
+ unsigned char tmp_buffer[nb_images * nb_channels * image_width * image_height];
for(s = 0; s < nb_sequences; s++) {
a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + s * st0;
- fl_generate_sequences(1, nb_images_per_sequence, image_width, image_height, tmp_buffer);
+ fl_generate_sequence(nb_images, image_width, image_height,
+ random_shape_size, random_colors,
+ tmp_buffer);
unsigned char *r = tmp_buffer;
- for(k = 0; k < nb_images_per_sequence; k++) {
+ for(k = 0; k < nb_images; k++) {
for(c = 0; c < nb_channels; c++) {
for(i = 0; i < image_height; i++) {
b = a + k * st1 + c * st2 + i * st3;
//////////////////////////////////////////////////////////////////////
-extern "C" void fl_generate_sequences(int nb_sequences,
- int nb_images_per_sequence,
- int width, int height,
- unsigned char *output) {
+extern "C" void fl_generate_sequence(int nb_images,
+ int width, int height,
+ int random_shape_size, int random_colors,
+ unsigned char *output) {
const scalar_t world_width = width * 8;
const scalar_t world_height = height * 8;
// nice materials for presentations or papers.
int every_nth = 16;
- int nb_simulated_frames = 1 + (nb_images_per_sequence - 1) * every_nth;
+ int nb_simulated_frames = 1 + (nb_images - 1) * every_nth;
int random_grasp = 1;
- int random_shape_size = 0;
int nb_shapes = 10;
- int skip = -1;
- for(int n = 0; n < nb_sequences; n++) {
+ Universe *universe;
+ Polygon *grabbed_polygon;
- Universe *universe;
- Polygon *grabbed_polygon;
+ universe = new Universe(nb_shapes, world_width, world_height);
- universe = new Universe(nb_shapes, world_width, world_height);
+ const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
+ if(nb_saved_frames != nb_images) {
+ cerr << "It makes no sense." << endl;
+ abort();
+ }
- const int nb_saved_frames = (nb_simulated_frames + every_nth - 1) / every_nth;
- if(nb_saved_frames != nb_images_per_sequence) {
- cerr << "It makes no sense." << endl;
- abort();
- }
+ CanvasCairo *canvases[nb_saved_frames * 2];
- CanvasCairo *canvases[nb_saved_frames * 2];
+ for(int s = 0; s < 2 * nb_saved_frames; s++) {
+ canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
+ }
- for(int s = 0; s < 2 * nb_saved_frames; s++) {
- canvases[s] = new CanvasCairo(scaling, universe->width(), universe->height());
- }
+ scalar_t grab_start_x, grab_start_y;
- scalar_t grab_start_x, grab_start_y;
+ int failed;
+ do {
if(random_grasp) {
grab_start_x = world_width * (0.1 + 0.8 * drand48());
grab_start_y = world_height * (0.1 + 0.8 * drand48());
shape_size = 80;
}
+ scalar_t red, green, blue;
+
+ if(random_colors) {
+ do {
+ red = drand48();
+ green = drand48();
+ blue = drand48();
+ } while(red < 0.9 and green < 0.9 and blue < 0.9 and
+ red > 0.1 and green > 0.1 and blue > 0.1);
+ } else {
+ red = 1.0;
+ green = 1.0;
+ blue = 1.0;
+ }
+
do {
scalar_t x[] = { - shape_size * 0.4, + shape_size * 0.4,
+ shape_size * 0.4, - shape_size * 0.4 };
scalar_t y[] = { - shape_size * 0.6, - shape_size * 0.6,
+ shape_size * 0.6, + shape_size * 0.6 };
- scalar_t delta = shape_size / sqrt(2.0);
-
- scalar_t object_center_x = delta + (world_width - 2 * delta) * drand48();
- scalar_t object_center_y = delta + (world_height - 2 * delta) * drand48();
+ scalar_t object_center_x = world_width * drand48();
+ scalar_t object_center_y = world_height * drand48();
delete pol;
- pol = new Polygon(0.5, 1.0, 1.0, 1.0, x, y, sizeof(x)/sizeof(scalar_t));
+ pol = new Polygon(0.5, red, green, blue, x, y, sizeof(x) / sizeof(scalar_t));
pol->set_position(object_center_x, object_center_y, M_PI * 2 * drand48());
pol->set_speed(0, 0, 0);
universe->initialize_polygon(pol);
nb_attempts++;
- } while(nb_attempts < nb_attempts_max && universe->collide(pol));
+ } while(nb_attempts < nb_attempts_max &&
+ (universe->collide(pol) || universe->collide_with_borders(pol, 2.0 / scaling)));
if(nb_attempts == nb_attempts_max) {
delete pol;
grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
} while(!grabbed_polygon);
- if(skip < 0 || n >= skip) {
+ failed = 0;
- scalar_t grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
- scalar_t grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
+ scalar_t grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
+ scalar_t grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
- for(int s = 0; s < nb_simulated_frames; s++) {
- if(s % every_nth == 0) {
- int t = s / every_nth;
- // scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
- // scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
+ for(int s = 0; !failed && s < nb_simulated_frames; s++) {
+ if(s % every_nth == 0) {
+ int t = s / every_nth;
+ // scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
+ // scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
- // canvases[2 * t + 0]->clear();
- // draw_grabbing_point_on_canvas(canvases[2 * t + 0], scaling,
- // xf, yf, 0.0, 0.0, 0.0);
- // canvases[2 * t + 1]->clear();
- // draw_universe_on_canvas(canvases[2 * t + 1], scaling, universe);
+ // canvases[2 * t + 0]->clear();
+ // draw_grabbing_point_on_canvas(canvases[2 * t + 0], scaling,
+ // xf, yf, 0.0, 0.0, 0.0);
+ // canvases[2 * t + 1]->clear();
+ // draw_universe_on_canvas(canvases[2 * t + 1], scaling, universe);
- canvases[t]->clear();
- draw_universe_on_canvas(canvases[t], scaling, universe);
+ canvases[t]->clear();
+ draw_universe_on_canvas(canvases[t], scaling, universe);
- // if(show_grabbing_point) {
- // draw_grabbing_point_on_canvas(canvases[2 * t + 1], scaling,
- // xf, yf, 1.0, 0.0, 0.0);
- // }
- }
-
- if(s < nb_simulated_frames - 1) {
- // Run the simulation
- for(int i = 0; i < nb_iterations_per_steps; i++) {
- scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
- scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
- grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
- universe->update(dt, 1.0 / scaling);
- }
- }
+ // if(show_grabbing_point) {
+ // draw_grabbing_point_on_canvas(canvases[2 * t + 1], scaling,
+ // xf, yf, 1.0, 0.0, 0.0);
+ // }
}
- for(int t = 0; t < nb_images_per_sequence; t++) {
- unsigned char *src = canvases[t]->_data;
- unsigned char *dst = output + (n * nb_images_per_sequence + t) * width * height * 3;
- for(int d = 0; d < 3; d++) {
- for(int y = 0; y < height; y++) {
- for(int x = 0; x < width; x++) {
- dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
- }
+ if(s < nb_simulated_frames - 1) {
+ // Run the simulation
+ for(int i = 0; i < nb_iterations_per_steps; i++) {
+ scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
+ scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
+ cout << "xf = " << xf << " yf = " << yf << endl;
+ if (xf < 0 || xf >= world_width || yf < 0 || yf >= world_height) {
+ failed = 1;
}
+ grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
+ universe->update(dt, 1.0 / scaling);
}
}
}
- for(int t = 0; t < 2 * nb_saved_frames; t++) {
- delete canvases[t];
+ if(failed) cout << "** FAILED" << endl;
+ else cout << "** DONE" << endl;
+
+ } while(failed);
+
+ for(int t = 0; t < nb_images; t++) {
+ unsigned char *src = canvases[t]->_data;
+ unsigned char *dst = output + t * width * height * 3;
+ for(int d = 0; d < 3; d++) {
+ for(int y = 0; y < height; y++) {
+ for(int x = 0; x < width; x++) {
+ dst[x + width * (y + height * d)] = src[d + 4 * (x + width * y)];
+ }
+ }
}
+ }
- delete universe;
+ for(int t = 0; t < 2 * nb_saved_frames; t++) {
+ delete canvases[t];
}
+
+ delete universe;
}
######################################################################
-def sequences_to_image(x):
+def sequences_to_image(x, gap=1, gap_color = (0, 128, 255)):
from PIL import Image
nb_sequences = x.size(0)
height = x.size(3)
width = x.size(4)
- gap = 1
- gap_color = (0, 128, 255)
result = torch.ByteTensor(nb_channels,
gap + nb_sequences * (height + gap),
for s in range(0, nb_sequences):
for i in range(0, nb_images_per_sequences):
- result.narrow(1, gap + s * (height + gap), height).narrow(2, gap + i * (width + gap), width).copy_(x[s][i])
+ result.narrow(1, gap + s * (height + gap), height) \
+ .narrow(2, gap + i * (width + gap), width) \
+ .copy_(x[s][i])
result_numpy = result.cpu().byte().transpose(0, 2).transpose(0, 1).numpy()
######################################################################
-x = flatland.generate_sequence(5, 3, 128, 96)
+x = flatland.generate_sequence(1, 3, 80, 80, True, True)
-sequences_to_image(x).save('sequences.png')
+sequences_to_image(x, gap = 2, gap_color = (0, 0, 0)).save('sequences.png')