Minor update.
[pysvrt.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 svrt.  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 = 0, 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(random_uniform_0_1() * Vignette::width);
64             ys = int(random_uniform_0_1() * Vignette::height);
65           } else {
66             xs = pxs + int(4 * (random_uniform_0_1() - 0.5) * small_part_hole_size);
67             ys = pys + int(4 * (random_uniform_0_1() - 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         if(nb_attempts < max_nb_attempts) {
75           shape.draw(s, &tmp, xs, ys);
76           tmp.fill(xs, ys, 128);
77           i = tmp.intersection(&mask);
78         }
79
80         nb_attempts++;
81       } while(nb_attempts < max_nb_attempts &&
82               s > 0 &&
83               ((label == 0 && i > 0) || (label == 1 && (i < 1 || i > 4))));
84
85       if(nb_attempts < max_nb_attempts) {
86         shape.draw(s, vignette, xs, ys);
87         pxs = xs; pys = ys;
88
89         if(label == 0) {
90           for(int k = 0; k < dist_min; k++) tmp.grow();
91         }
92
93         mask.superpose(&mask, &tmp);
94       }
95     }
96   } while(nb_attempts >= max_nb_attempts);
97 }