Removed the definition of basename, which confuses an existing system one.
[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 version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  folded-ctf is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "pose_cell_scored_set.h"
26 #include "global.h"
27 #include "fusion_sort.h"
28
29 PoseCellScoredSet::PoseCellScoredSet() {
30   _scores = new scalar_t[_max_nb];
31 }
32
33 PoseCellScoredSet::~PoseCellScoredSet() {
34   delete[] _scores;
35 }
36
37 void PoseCellScoredSet::add_cell_with_score(PoseCell *cell, scalar_t score) {
38   _scores[_nb_added] = score;
39   add_cell(cell);
40 }
41
42 void PoseCellScoredSet::decimate_hit(int level) {
43   if(_nb_added == 0) return;
44
45   Pose *poses = new Pose[_nb_added];
46   int *indexes = new int[_nb_added];
47   int *sorted_indexes = new int[_nb_added];
48
49   for(int c = 0; c < _nb_added; c++) {
50     _cells[c].get_centroid(poses + c);
51     indexes[c] = c;
52   }
53
54   indexed_fusion_dec_sort(_nb_added, indexes, sorted_indexes, _scores);
55
56   int nb_remaining = _nb_added, current = 0;
57
58   while(current < nb_remaining) {
59     int e = current + 1;
60     for(int d = current + 1; d < nb_remaining; d++)
61       if(!poses[sorted_indexes[current]].hit(level, poses + sorted_indexes[d]))
62         sorted_indexes[e++] = sorted_indexes[d];
63     nb_remaining = e;
64     current++;
65   }
66
67   PoseCell *tmp_cells = new PoseCell[max_nb_cells];
68   scalar_t *tmp_scores = new scalar_t[max_nb_cells];
69
70   for(int n = 0; n < nb_remaining; n++) {
71     tmp_cells[n] = _cells[sorted_indexes[n]];
72     tmp_scores[n] = _scores[sorted_indexes[n]];
73   }
74
75   delete[] _cells;
76   delete[] _scores;
77   _cells = tmp_cells;
78   _scores = tmp_scores;
79   _nb_added = nb_remaining;
80
81   delete[] poses;
82   delete[] indexes;
83   delete[] sorted_indexes;
84 }
85
86 void PoseCellScoredSet::decimate_collide(int level) {
87   if(_nb_added == 0) return;
88
89   Pose *poses = new Pose[_nb_added];
90   int *indexes = new int[_nb_added];
91   int *sorted_indexes = new int[_nb_added];
92
93   for(int c = 0; c < _nb_added; c++) {
94     _cells[c].get_centroid(poses + c);
95     indexes[c] = c;
96   }
97
98   indexed_fusion_dec_sort(_nb_added, indexes, sorted_indexes, _scores);
99
100   int nb_remaining = _nb_added, current = 0;
101
102   while(current < nb_remaining) {
103     int e = current + 1;
104     for(int d = current + 1; d < nb_remaining; d++)
105       if(!poses[sorted_indexes[current]].collide(level, poses + sorted_indexes[d]))
106         sorted_indexes[e++] = sorted_indexes[d];
107     nb_remaining = e;
108     current++;
109   }
110
111   PoseCell *tmp_cells = new PoseCell[max_nb_cells];
112   scalar_t *tmp_scores = new scalar_t[max_nb_cells];
113
114   for(int n = 0; n < nb_remaining; n++) {
115     tmp_cells[n] = _cells[sorted_indexes[n]];
116     tmp_scores[n] = _scores[sorted_indexes[n]];
117   }
118
119   delete[] _cells;
120   delete[] _scores;
121   _cells = tmp_cells;
122   _scores = tmp_scores;
123   _nb_added = nb_remaining;
124
125   delete[] poses;
126   delete[] indexes;
127   delete[] sorted_indexes;
128 }