automatic commit
[folded-ctf.git] / pose_cell_scored_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 "pose_cell_scored_set.h"
20 #include "global.h"
21 #include "fusion_sort.h"
22
23 PoseCellScoredSet::PoseCellScoredSet() {
24   _scores = new scalar_t[_max_nb];
25 }
26
27 PoseCellScoredSet::~PoseCellScoredSet() {
28   delete[] _scores;
29 }
30
31 void PoseCellScoredSet::add_cell_with_score(PoseCell *cell, scalar_t score) {
32   _scores[_nb_added] = score;
33   add_cell(cell);
34 }
35
36 void PoseCellScoredSet::decimate_hit(int level) {
37   if(_nb_added == 0) return;
38
39   Pose *poses = new Pose[_nb_added];
40   int *indexes = new int[_nb_added];
41   int *sorted_indexes = new int[_nb_added];
42
43   for(int c = 0; c < _nb_added; c++) {
44     _cells[c].get_centroid(poses + c);
45     indexes[c] = c;
46   }
47
48   indexed_fusion_dec_sort(_nb_added, indexes, sorted_indexes, _scores);
49
50   int nb_remaining = _nb_added, current = 0;
51
52   while(current < nb_remaining) {
53     int e = current + 1;
54     for(int d = current + 1; d < nb_remaining; d++)
55       if(!poses[sorted_indexes[current]].hit(level, poses + sorted_indexes[d]))
56         sorted_indexes[e++] = sorted_indexes[d];
57     nb_remaining = e;
58     current++;
59   }
60
61   PoseCell *tmp_cells = new PoseCell[max_nb_cells];
62   scalar_t *tmp_scores = new scalar_t[max_nb_cells];
63
64   for(int n = 0; n < nb_remaining; n++) {
65     tmp_cells[n] = _cells[sorted_indexes[n]];
66     tmp_scores[n] = _scores[sorted_indexes[n]];
67   }
68
69   delete[] _cells;
70   delete[] _scores;
71   _cells = tmp_cells;
72   _scores = tmp_scores;
73   _nb_added = nb_remaining;
74
75   delete[] poses;
76   delete[] indexes;
77   delete[] sorted_indexes;
78 }
79
80 void PoseCellScoredSet::decimate_collide(int level) {
81   if(_nb_added == 0) return;
82
83   Pose *poses = new Pose[_nb_added];
84   int *indexes = new int[_nb_added];
85   int *sorted_indexes = new int[_nb_added];
86
87   for(int c = 0; c < _nb_added; c++) {
88     _cells[c].get_centroid(poses + c);
89     indexes[c] = c;
90   }
91
92   indexed_fusion_dec_sort(_nb_added, indexes, sorted_indexes, _scores);
93
94   int nb_remaining = _nb_added, current = 0;
95
96   while(current < nb_remaining) {
97     int e = current + 1;
98     for(int d = current + 1; d < nb_remaining; d++)
99       if(!poses[sorted_indexes[current]].collide(level, poses + sorted_indexes[d]))
100         sorted_indexes[e++] = sorted_indexes[d];
101     nb_remaining = e;
102     current++;
103   }
104
105   PoseCell *tmp_cells = new PoseCell[max_nb_cells];
106   scalar_t *tmp_scores = new scalar_t[max_nb_cells];
107
108   for(int n = 0; n < nb_remaining; n++) {
109     tmp_cells[n] = _cells[sorted_indexes[n]];
110     tmp_scores[n] = _scores[sorted_indexes[n]];
111   }
112
113   delete[] _cells;
114   delete[] _scores;
115   _cells = tmp_cells;
116   _scores = tmp_scores;
117   _nb_added = nb_remaining;
118
119   delete[] poses;
120   delete[] indexes;
121   delete[] sorted_indexes;
122 }