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