automatic commit
[folded-ctf.git] / pose_cell_scored_set.cc
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
11  *  folded-ctf is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published
13  *  by the Free Software Foundation, either version 3 of the License,
14  *  or (at your option) any later version.
15  *
16  *  folded-ctf is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 #include "pose_cell_scored_set.h"
27 #include "global.h"
28 #include "fusion_sort.h"
29
30 PoseCellScoredSet::PoseCellScoredSet() {
31   _scores = new scalar_t[_max_nb];
32 }
33
34 PoseCellScoredSet::~PoseCellScoredSet() {
35   delete[] _scores;
36 }
37
38 void PoseCellScoredSet::add_cell_with_score(PoseCell *cell, scalar_t score) {
39   _scores[_nb_added] = score;
40   add_cell(cell);
41 }
42
43 void PoseCellScoredSet::decimate_hit(int level) {
44   if(_nb_added == 0) return;
45
46   Pose *poses = new Pose[_nb_added];
47   int *indexes = new int[_nb_added];
48   int *sorted_indexes = new int[_nb_added];
49
50   for(int c = 0; c < _nb_added; c++) {
51     _cells[c].get_centroid(poses + c);
52     indexes[c] = c;
53   }
54
55   indexed_fusion_dec_sort(_nb_added, indexes, sorted_indexes, _scores);
56
57   int nb_remaining = _nb_added, current = 0;
58
59   while(current < nb_remaining) {
60     int e = current + 1;
61     for(int d = current + 1; d < nb_remaining; d++)
62       if(!poses[sorted_indexes[current]].hit(level, poses + sorted_indexes[d]))
63         sorted_indexes[e++] = sorted_indexes[d];
64     nb_remaining = e;
65     current++;
66   }
67
68   PoseCell *tmp_cells = new PoseCell[max_nb_cells];
69   scalar_t *tmp_scores = new scalar_t[max_nb_cells];
70
71   for(int n = 0; n < nb_remaining; n++) {
72     tmp_cells[n] = _cells[sorted_indexes[n]];
73     tmp_scores[n] = _scores[sorted_indexes[n]];
74   }
75
76   delete[] _cells;
77   delete[] _scores;
78   _cells = tmp_cells;
79   _scores = tmp_scores;
80   _nb_added = nb_remaining;
81
82   delete[] poses;
83   delete[] indexes;
84   delete[] sorted_indexes;
85 }
86
87 void PoseCellScoredSet::decimate_collide(int level) {
88   if(_nb_added == 0) return;
89
90   Pose *poses = new Pose[_nb_added];
91   int *indexes = new int[_nb_added];
92   int *sorted_indexes = new int[_nb_added];
93
94   for(int c = 0; c < _nb_added; c++) {
95     _cells[c].get_centroid(poses + c);
96     indexes[c] = c;
97   }
98
99   indexed_fusion_dec_sort(_nb_added, indexes, sorted_indexes, _scores);
100
101   int nb_remaining = _nb_added, current = 0;
102
103   while(current < nb_remaining) {
104     int e = current + 1;
105     for(int d = current + 1; d < nb_remaining; d++)
106       if(!poses[sorted_indexes[current]].collide(level, poses + sorted_indexes[d]))
107         sorted_indexes[e++] = sorted_indexes[d];
108     nb_remaining = e;
109     current++;
110   }
111
112   PoseCell *tmp_cells = new PoseCell[max_nb_cells];
113   scalar_t *tmp_scores = new scalar_t[max_nb_cells];
114
115   for(int n = 0; n < nb_remaining; n++) {
116     tmp_cells[n] = _cells[sorted_indexes[n]];
117     tmp_scores[n] = _scores[sorted_indexes[n]];
118   }
119
120   delete[] _cells;
121   delete[] _scores;
122   _cells = tmp_cells;
123   _scores = tmp_scores;
124   _nb_added = nb_remaining;
125
126   delete[] poses;
127   delete[] indexes;
128   delete[] sorted_indexes;
129 }