Minor update.
[pysvrt.git] / vision_problem_9.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_9.h"
26 #include "shape.h"
27
28 VisionProblem_9::VisionProblem_9() { }
29
30 void VisionProblem_9::generate(int label, Vignette *vignette) {
31   int nb_shapes = 3;
32   int xs[nb_shapes], ys[nb_shapes];
33   Shape big_shape, small_shape;
34
35   int error;
36
37   do {
38     scalar_t x1 = int(random_uniform_0_1() * Vignette::width);
39     scalar_t y1 = int(random_uniform_0_1() * Vignette::height);
40     scalar_t x2 = int(random_uniform_0_1() * Vignette::width);
41     scalar_t y2 = int(random_uniform_0_1() * Vignette::height);
42     scalar_t alpha = 0.25 + 0.5 * random_uniform_0_1();
43
44     big_shape.randomize(part_size, hole_size);
45     small_shape.copy(&big_shape);
46     small_shape.scale(0.5);
47
48     if(label == 0) {
49       xs[0] = int(x1); ys[0] = int(y1);
50       xs[1] = int(x2); ys[1] = int(y2);
51       xs[2] = int(alpha * x1 + (1 - alpha) * x2); ys[2] = int(alpha * y1 + (1 - alpha) * y2);
52     } else {
53       xs[0] = int(x1); ys[0] = int(y1);
54       xs[1] = int(alpha * x1 + (1 - alpha) * x2); ys[1] = int(alpha * y1 + (1 - alpha) * y2);
55       xs[2] = int(x2); ys[2] = int(y2);
56     }
57
58     vignette->clear();
59
60     error = 0;
61     for(int n = 0; n < nb_shapes; n++) {
62       if(n < 2) {
63         error |= small_shape.overwrites(vignette, xs[n], ys[n]);
64         if(!error) {
65           small_shape.draw(n, vignette, xs[n], ys[n]);
66         }
67       } else {
68         error |= big_shape.overwrites(vignette, xs[n], ys[n]);
69         if(!error) {
70           big_shape.draw(n, vignette, xs[n], ys[n]);
71         }
72       }
73       vignette->fill(xs[n], ys[n], 128);
74     }
75
76     vignette->replace_value(128, 255);
77   } while(error);
78 }