Fixed the header copyrights.
[svrt.git] / vision_problem_11.cc
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt 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  *  svrt 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 selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "vision_problem_11.h"
26 #include "shape.h"
27
28 VisionProblem_11::VisionProblem_11() { }
29
30 void VisionProblem_11::generate(int label, Vignette *vignette) {
31   int nb_shapes;
32   int xs, ys, i, pxs, pys;
33   const int dist_min = Vignette::width/12;
34   int nb_attempts, max_nb_attempts = 100;
35
36   Vignette mask, tmp;
37
38   nb_shapes = 2;
39
40   do {
41     nb_attempts = 0;
42
43     mask.clear();
44     vignette->clear();
45
46     pxs = 0; pys = 0;
47
48     for(int s = 0; nb_attempts < max_nb_attempts && s < nb_shapes; s++) {
49       Shape shape;
50
51       do {
52         tmp.clear();
53
54         do {
55
56           if(s == 0) {
57             shape.randomize(big_part_size / 2, big_part_hole_size / 2);
58           } else {
59             shape.randomize(small_part_size / 2, small_part_hole_size / 2);
60           }
61
62           if(nb_shapes == 2 || s == 0 || label == 0) {
63             xs = int(drand48() * Vignette::width);
64             ys = int(drand48() * Vignette::height);
65           } else {
66             xs = pxs + int(4 * (drand48() - 0.5) * small_part_hole_size);
67             ys = pys + int(4 * (drand48() - 0.5) * small_part_hole_size);
68           }
69           nb_attempts++;
70
71         } while(nb_attempts < max_nb_attempts &&
72                 shape.overwrites(&tmp, xs, ys));
73
74         shape.draw(&tmp, xs, ys);
75         tmp.fill(xs, ys, 128);
76         i = tmp.intersection(&mask);
77
78         nb_attempts++;
79       } while(nb_attempts < max_nb_attempts &&
80               s > 0 &&
81               ((label == 0 && i > 0) || (label == 1 && (i < 1 || i > 4))));
82
83       shape.draw(vignette, xs, ys);
84       pxs = xs; pys = ys;
85
86       if(label == 0) {
87         for(int k = 0; k < dist_min; k++) tmp.grow();
88       }
89
90       mask.superpose(&mask, &tmp);
91     }
92   } while(nb_attempts >= max_nb_attempts);
93 }