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