Automatic commit
[svrt.git] / vision_problem_13.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_13.h"
26 #include "shape.h"
27
28 VisionProblem_13::VisionProblem_13() { }
29
30 void VisionProblem_13::generate(int label, Vignette *vignette) {
31   Shape big_shape, small_shape;
32   int big_xs1, big_ys1, small_xs1, small_ys1;
33   int big_xs2, big_ys2, small_xs2, small_ys2;
34   int translated_small_xs, translated_small_ys;
35   Vignette tmp;
36   const int dist_min = Vignette::width/4;
37   int nb_attempts;
38   const int max_nb_attempts = 100;
39
40   do {
41     nb_attempts = 0;
42     do {
43
44       vignette->clear();
45
46       big_shape.randomize(big_part_size / 2, big_part_hole_size / 2);
47
48       tmp.clear();
49       do {
50         big_xs1 = int(drand48() * Vignette::width);
51         big_ys1 = int(drand48() * Vignette::height);
52         nb_attempts++;
53       } while(nb_attempts < max_nb_attempts &&
54               big_shape.overwrites(vignette, big_xs1, big_ys1));
55
56       big_shape.draw(vignette, big_xs1, big_ys1);
57       big_shape.draw(&tmp, big_xs1, big_ys1);
58       for(int k = 0; k < dist_min; k++) tmp.grow();
59
60       do {
61         small_shape.randomize(small_part_size / 2, small_part_hole_size / 2);
62         small_xs1 = int(drand48() * Vignette::width);
63         small_ys1 = int(drand48() * Vignette::height);
64         nb_attempts++;
65       } while(nb_attempts < max_nb_attempts &&
66               (!small_shape.overwrites(&tmp, small_xs1, small_ys1) ||
67                small_shape.overwrites(vignette, small_xs1, small_ys1)));
68
69       small_shape.draw(vignette, small_xs1, small_ys1);
70
71       tmp.clear();
72       do {
73         big_xs2 = int(drand48() * Vignette::width);
74         big_ys2 = int(drand48() * Vignette::height);
75         nb_attempts++;
76       } while(nb_attempts < max_nb_attempts &&
77               big_shape.overwrites(vignette, big_xs2, big_ys2));
78       big_shape.draw(vignette, big_xs2, big_ys2);
79       big_shape.draw(&tmp, big_xs2, big_ys2);
80       for(int k = 0; k < dist_min; k++) tmp.grow();
81
82       translated_small_xs = small_xs1 + (big_xs2 - big_xs1);
83       translated_small_ys = small_ys1 + (big_ys2 - big_ys1);
84
85     } while(nb_attempts < max_nb_attempts &&
86             small_shape.overwrites(vignette,
87                                    translated_small_xs,
88                                    translated_small_ys));
89
90     if(label) {
91       small_xs2 = translated_small_xs;
92       small_ys2 = translated_small_ys;
93     } else {
94       do {
95         small_xs2 = int(drand48() * Vignette::width);
96         small_ys2 = int(drand48() * Vignette::height);
97         nb_attempts++;
98       } while(nb_attempts < max_nb_attempts &&
99               (sq(small_xs2 - translated_small_xs) + sq(small_ys2 - translated_small_ys) < sq(dist_min) ||
100                !small_shape.overwrites(&tmp, small_xs2, small_ys2) ||
101                small_shape.overwrites(vignette, small_xs2, small_ys2)));
102     }
103   } while(nb_attempts >= max_nb_attempts);
104   small_shape.draw(vignette, small_xs2, small_ys2);
105 }