automatic commit
[folded-ctf.git] / labelled_image.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret                                           //
16 // (C) Idiap Research Institute                                          //
17 //                                                                       //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
19 ///////////////////////////////////////////////////////////////////////////
20
21 #include "labelled_image.h"
22
23 LabelledImage::LabelledImage() : RichImage() {
24   _target_poses = 0;
25 }
26
27 LabelledImage::LabelledImage(int width, int height, int nb_targets) : RichImage(width, height) {
28   _nb_targets = nb_targets;
29   _target_poses = new Pose[_nb_targets];
30 }
31
32 LabelledImage::~LabelledImage() {
33   delete[] _target_poses;
34 }
35
36 int LabelledImage::pose_cell_label(PoseCell *cell) {
37   int positive = 0;
38   int negative = 1;
39
40   for(int t = 0; t < _nb_targets; t++) {
41     if(cell->contains(_target_poses + t))
42       positive = 1;
43     if(!cell->negative_for_train(_target_poses + t))
44       negative = 0;
45   }
46
47   if(positive) return 1;
48   if(negative) return -1;
49   return 0;
50 }
51
52 void LabelledImage::write(ostream *out) {
53   int v = file_format_version;
54   write_var(out, &v);
55   RichImage::write(out);
56   write_var(out, &_nb_targets);
57   for(int t = 0; t < _nb_targets; t++)
58     _target_poses[t].write(out);
59 }
60
61 void LabelledImage::read(istream *in) {
62   int v;
63   read_var(in, &v);
64   if(v != file_format_version) {
65     cerr << "Pool file format version " << file_format_version << " expected,"
66          << " the file is version " << v
67          << endl;
68     exit(1);
69   }
70   RichImage::read(in);
71   delete[] _target_poses;
72   read_var(in, &_nb_targets);
73   _target_poses = new Pose[_nb_targets];
74   for(int t = 0; t < _nb_targets; t++)
75     _target_poses[t].read(in);
76 }