Minor update.
[pysvrt.git] / vision_problem_14.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_14.h"
26 #include "shape.h"
27
28 VisionProblem_14::VisionProblem_14() { }
29
30 void VisionProblem_14::sample_shapes_positions_aligned(int nb_shapes, int *xs, int *ys) {
31   for(int n = 0; n < nb_shapes - 1; n++) {
32     xs[n] = int(random_uniform_0_1() * (Vignette::width - part_size + 1)) + part_size/2;
33     ys[n] = int(random_uniform_0_1() * (Vignette::height - part_size + 1)) + part_size/2;
34   }
35   scalar_t alpha = random_uniform_0_1();
36   xs[nb_shapes - 1] = int(alpha * xs[0] + (1 - alpha) * xs[1]);
37   ys[nb_shapes - 1] = int(alpha * ys[0] + (1 - alpha) * ys[1]);
38 }
39
40 void VisionProblem_14::sample_shapes_positions_uniformly(int nb_shapes, int *xs, int *ys) {
41   for(int n = 0; n < nb_shapes; n++) {
42     xs[n] = int(random_uniform_0_1() * (Vignette::width - part_size + 1)) + part_size/2;
43     ys[n] = int(random_uniform_0_1() * (Vignette::height - part_size + 1)) + part_size/2;
44   }
45 }
46
47 int VisionProblem_14::there_is_an_alignment(scalar_t dist_threshold, int nb_shapes, int *xs, int *ys) {
48   for(int a = 0; a < nb_shapes; a++) {
49     for(int b = 0; b < nb_shapes; b++) {
50       for(int c = 0; c < b; c++) {
51         if(a != b && a != c) {
52           if(point_in_band(scalar_t(xs[a]), scalar_t(ys[a]),
53                            scalar_t(xs[b]), scalar_t(ys[b]),
54                            scalar_t(xs[c]), scalar_t(ys[c]),
55                            dist_threshold)) return 1;
56         }
57       }
58     }
59   }
60   return 0;
61 }
62
63
64 void VisionProblem_14::generate(int label, Vignette *vignette) {
65   int nb_shapes = 3;
66   int xs[nb_shapes], ys[nb_shapes];
67   int error;
68
69   do {
70     if(label) {
71       sample_shapes_positions_aligned(nb_shapes, xs, ys);
72     } else {
73       do {
74         sample_shapes_positions_uniformly(nb_shapes, xs, ys);
75       } while(there_is_an_alignment(Vignette::width/20, 3, xs, ys));
76     }
77
78     vignette->clear();
79
80     Shape shape;
81
82     error = 0;
83     for(int n = 0; n < nb_shapes; n++) {
84       if(n == 0) {
85         shape.randomize(part_size/2, hole_size/2);
86       }
87       error |= shape.overwrites(vignette, xs[n], ys[n]);
88       if(!error) {
89         shape.draw(n, vignette, xs[n], ys[n]);
90       }
91     }
92   } while(error);
93 }