Added some explanation about the main function.
[flatland.git] / flatland.c
1
2 /*
3
4    flatland is a simple 2d physical simulator
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 flatland
10
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.
14
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.
19
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/>.
22
23 */
24
25 #include <TH/TH.h>
26
27 #include "sequence_generator.h"
28
29 THByteTensor *generate_sequence(int pulling,
30                                 long nb_sequences,
31                                 long nb_images,
32                                 long image_height, long image_width,
33                                 long nb_shapes,
34                                 int random_shape_size, int random_colors) {
35
36   long nb_channels = 3;
37   unsigned char *a, *b;
38   long s, c, k, i, j, st0, st1, st2, st3, st4;
39
40   THLongStorage *size = THLongStorage_newWithSize(5);
41   size->data[0] = nb_sequences;
42   size->data[1] = nb_images;
43   size->data[2] = nb_channels;
44   size->data[3] = image_height;
45   size->data[4] = image_width;
46   THByteTensor *result = THByteTensor_newWithSize(size, NULL);
47   THLongStorage_free(size);
48
49   st0 = THByteTensor_stride(result, 0);
50   st1 = THByteTensor_stride(result, 1);
51   st2 = THByteTensor_stride(result, 2);
52   st3 = THByteTensor_stride(result, 3);
53   st4 = THByteTensor_stride(result, 4);
54
55   unsigned char tmp_buffer[nb_images * nb_channels * image_width * image_height];
56
57   for(s = 0; s < nb_sequences; s++) {
58     a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + s * st0;
59     fl_generate_sequence(nb_images, image_width, image_height, nb_shapes,
60                          random_shape_size, random_colors,
61                          pulling,
62                          tmp_buffer);
63     unsigned char *r = tmp_buffer;
64     for(k = 0; k < nb_images; k++) {
65       for(c = 0; c < nb_channels; c++) {
66         for(i = 0; i < image_height; i++) {
67           b = a + k * st1 + c * st2 + i * st3;
68           for(j = 0; j < image_width; j++) {
69             *b = (unsigned char) (*r);
70             r++;
71             b += st4;
72           }
73         }
74       }
75     }
76   }
77
78   return result;
79 }