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, (C) IDIAP                                //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "labelled_image.h"
20
21 LabelledImage::LabelledImage() : RichImage() {
22   _target_poses = 0;
23 }
24
25 LabelledImage::LabelledImage(int width, int height, int nb_targets) : RichImage(width, height) {
26   _nb_targets = nb_targets;
27   _target_poses = new Pose[_nb_targets];
28 }
29
30 LabelledImage::~LabelledImage() {
31   delete[] _target_poses;
32 }
33
34 int LabelledImage::pose_cell_label(PoseCell *cell) {
35   int positive = 0;
36   int negative = 1;
37
38   for(int t = 0; t < _nb_targets; t++) {
39     if(cell->contains(_target_poses + t))
40       positive = 1;
41     if(!cell->negative_for_train(_target_poses + t))
42       negative = 0;
43   }
44
45   if(positive) return 1;
46   if(negative) return -1;
47   return 0;
48 }
49
50 void LabelledImage::write(ostream *out) {
51   int v = file_format_version;
52   write_var(out, &v);
53   RichImage::write(out);
54   write_var(out, &_nb_targets);
55   for(int t = 0; t < _nb_targets; t++)
56     _target_poses[t].write(out);
57 }
58
59 void LabelledImage::read(istream *in) {
60   int v;
61   read_var(in, &v);
62   if(v != file_format_version) {
63     cerr << "Pool file format version " << file_format_version << " expected,"
64          << " the file is version " << v
65          << endl;
66     exit(1);
67   }
68   RichImage::read(in);
69   delete[] _target_poses;
70   read_var(in, &_nb_targets);
71   _target_poses = new Pose[_nb_targets];
72   for(int t = 0; t < _nb_targets; t++)
73     _target_poses[t].read(in);
74 }