automatic commit
[folded-ctf.git] / sample_set.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 "sample_set.h"
22
23 SampleSet::SampleSet(int nb_features, int nb_samples) {
24   _nb_features = nb_features;
25   _nb_samples = nb_samples;
26   _shared_feature_values = new SharedResponses(_nb_features, _nb_samples);
27   _shared_feature_values->grab();
28
29   _labels = new int[_nb_samples];
30   _feature_values = new scalar_t *[_nb_samples];
31   for(int s = 0; s < _nb_samples; s++)
32     _feature_values[s] = _shared_feature_values->_responses + _nb_features * s;
33
34 }
35
36 SampleSet::SampleSet(SampleSet *father, int nb, int *indexes) {
37   _nb_features = father->_nb_features;
38   _nb_samples = nb;
39   _shared_feature_values = father->_shared_feature_values;
40   _shared_feature_values->grab();
41
42   _labels = new int[_nb_samples];
43   _feature_values = new scalar_t *[_nb_samples];
44   for(int s = 0; s < _nb_samples; s++) {
45     _feature_values[s] = father->_feature_values[indexes[s]];
46     _labels[s] = father->_labels[indexes[s]];
47   }
48 }
49
50 SampleSet::~SampleSet() {
51   _shared_feature_values->release();
52   delete[] _feature_values;
53   delete[] _labels;
54 }
55
56 void SampleSet::set_sample(int n,
57                            PiFeatureFamily *pi_feature_family,
58                            RichImage *image,
59                            PoseCell *cell, int label) {
60   ASSERT(n >= 0 && n < _nb_samples);
61   _labels[n] = label;
62   PiReferential referential(cell);
63   for(int f = 0; f < _nb_features; f++) {
64     _feature_values[n][f] = pi_feature_family->get_feature(f)->response(image, &referential);
65     ASSERT(!isnan(_feature_values[n][f]));
66   }
67 }