automatic commit
[folded-ctf.git] / pi_feature_family.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 "pi_feature_family.h"
22
23 PiFeatureFamily::PiFeatureFamily() {
24   _nb_features = 0;
25   _pi_features = 0;
26 }
27
28 PiFeatureFamily::~PiFeatureFamily() {
29   delete[] _pi_features;
30 }
31
32 void PiFeatureFamily::read(istream *is) {
33   delete[] _pi_features;
34   read_var(is, &_nb_features);
35   _pi_features = new PiFeature[_nb_features];
36   is->read((char *) _pi_features, sizeof(PiFeature) * _nb_features);
37 }
38
39 void PiFeatureFamily::write(ostream *os) {
40   write_var(os, &_nb_features);
41   os->write((char *) _pi_features, sizeof(PiFeature) * _nb_features);
42 }
43
44 void PiFeatureFamily::resize(int nb_features) {
45   delete[] _pi_features;
46   _nb_features = nb_features;
47   _pi_features = new PiFeature[_nb_features];
48 }
49
50 void PiFeatureFamily::randomize(int level) {
51   for(int f = 0; f < _nb_features; f++) _pi_features[f].randomize(level);
52 }
53
54 void PiFeatureFamily::extract(PiFeatureFamily *pi_feature_family,
55                               bool *used_features, int *new_feature_indexes) {
56   delete[] _pi_features;
57   _nb_features = 0;
58
59   for(int f = 0; f < pi_feature_family->nb_features(); f++)
60     if(used_features[f]) _nb_features++;
61
62   _pi_features = new PiFeature[_nb_features];
63
64   int g = 0;
65
66   for(int f = 0; f < pi_feature_family->nb_features(); f++)
67     if(used_features[f]) {
68       _pi_features[g] = pi_feature_family->_pi_features[f];
69       new_feature_indexes[f] = g;
70       g++;
71     } else new_feature_indexes[f] = -1;
72 }