Added REAME.md
[svrt.git] / vision_problem_6.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_6.h"
26 #include "shape.h"
27
28 VisionProblem_6::VisionProblem_6() { }
29
30 void VisionProblem_6::generate(int label, Vignette *vignette) {
31   const int nb_shapes = 4;
32   int xs[nb_shapes], ys[nb_shapes];
33   int shape_number[nb_shapes];
34
35   ASSERT(nb_shapes == 4);
36
37   int too_ambiguous;
38
39   int error;
40
41   do {
42     Shape shape1, shape2;
43     shape1.randomize(part_size/2, hole_size/2);
44     shape2.randomize(part_size/2, hole_size/2);
45
46     scalar_t xc1, yc1, alpha1;
47     scalar_t xc2, yc2, alpha2;
48     scalar_t r;
49     shape_number[0] = 0;
50     shape_number[1] = 0;
51     shape_number[2] = 1;
52     shape_number[3] = 1;
53     do {
54       if(label == 1) {
55         xc1 = drand48() * (Vignette::width - part_size) ;
56         yc1 = drand48() * (Vignette::width - part_size) ;
57         alpha1 = drand48() * M_PI * 2;
58         r = drand48() * (Vignette::width + Vignette::height)/2;
59
60         xc2 = drand48() * (Vignette::width - part_size) ;
61         yc2 = drand48() * (Vignette::width - part_size) ;
62         alpha2 = drand48() * M_PI * 2;
63
64         xs[0] = int(xc1 + r * cos(alpha1));
65         ys[0] = int(yc1 + r * sin(alpha1));
66         xs[1] = int(xc1 - r * cos(alpha1));
67         ys[1] = int(yc1 - r * sin(alpha1));
68         xs[2] = int(xc2 + r * cos(alpha2));
69         ys[2] = int(yc2 + r * sin(alpha2));
70         xs[3] = int(xc2 - r * cos(alpha2));
71         ys[3] = int(yc2 - r * sin(alpha2));
72         too_ambiguous = 0;
73       } else {
74         for(int n = 0; n < nb_shapes; n++) {
75           xs[n] = int(drand48() * (Vignette::width - part_size));
76           ys[n] = int(drand48() * (Vignette::width - part_size));
77         }
78         scalar_t d1 = sqrt(sq(xs[0] - xs[1]) + sq(ys[0] - ys[1]));
79         scalar_t d2 = sqrt(sq(xs[2] - xs[3]) + sq(ys[2] - ys[3]));
80         too_ambiguous = abs(d1 - d2) < scalar_t(part_size);
81       }
82     } while(too_ambiguous ||
83             cluttered_shapes(part_size, nb_shapes, xs, ys));
84
85     vignette->clear();
86
87     error = 0;
88     for(int n = 0; n < nb_shapes; n++) {
89       if(shape_number[n] == 0) {
90         error |= shape1.overwrites(vignette, xs[n], ys[n]);
91         shape1.draw(vignette, xs[n], ys[n]);
92       } else {
93         error |= shape2.overwrites(vignette, xs[n], ys[n]);
94         shape2.draw(vignette, xs[n], ys[n]);
95       }
96     }
97   } while(error);
98 }