automatic commit
[folded-ctf.git] / pose.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 <string.h>
27
28 #include "pose.h"
29
30 Pose::Pose() {
31   memset(this, 0, sizeof(*this));
32 }
33
34 void Pose::horizontal_flip(scalar_t scene_width) {
35   _head_xc = scene_width - 1 - _head_xc;
36   _belly_xc = scene_width - 1 - _belly_xc;
37 }
38
39 const scalar_t tolerance_scale_ratio_for_hit = 1.5;
40 const scalar_t tolerance_distance_factor_for_hit = 1.0;
41
42 bool Pose::hit(int level, Pose *pose) {
43
44   // Head size
45
46   if(_head_radius/pose->_head_radius > tolerance_scale_ratio_for_hit ||
47      pose->_head_radius/_head_radius > tolerance_scale_ratio_for_hit)
48     return false;
49
50   scalar_t sq_delta = _head_radius * pose->_head_radius * sq(tolerance_distance_factor_for_hit);
51
52   // Head location
53
54   if(sq(_head_xc - pose->_head_xc) + sq(_head_yc - pose->_head_yc) > sq_delta)
55     return false;
56
57   if(level == 0) return true;
58
59   // Belly location
60
61   if(sq(_belly_xc - pose->_belly_xc) + sq(_belly_yc - pose->_belly_yc) > 4 * sq_delta)
62     return false;
63
64   if(level == 1) return true;
65
66   cerr << "Hit criterion is undefined for level " << level << endl;
67
68   abort();
69 }
70
71 const scalar_t tolerance_scale_ratio_for_collide = 1.2;
72 const scalar_t tolerance_distance_factor_for_collide = 0.25;
73
74 bool Pose::collide(int level, Pose *pose) {
75
76   // Head size
77
78   if(_head_radius/pose->_head_radius > tolerance_scale_ratio_for_collide ||
79      pose->_head_radius/_head_radius > tolerance_scale_ratio_for_collide)
80     return false;
81
82   scalar_t sq_delta = _head_radius * pose->_head_radius * sq(tolerance_distance_factor_for_collide);
83
84   // Head location
85
86   if(sq(_head_xc - pose->_head_xc) + sq(_head_yc - pose->_head_yc) <= sq_delta)
87     return true;
88
89   if(level == 0) return false;
90
91   // Belly location
92
93   if(sq(_belly_xc - pose->_belly_xc) + sq(_belly_yc - pose->_belly_yc) <= sq_delta)
94     return true;
95
96   if(level == 1) return false;
97
98   cerr << "Colliding criterion is undefined for level " << level << endl;
99
100   abort();
101 }
102
103 void Pose::draw(int thickness, int r, int g, int b, int level, RGBImage *image) {
104
105   // Draw the head circle
106
107   image->draw_ellipse(thickness, r, g, b,
108                       _head_xc, _head_yc, _head_radius, _head_radius, 0);
109
110   if(level == 1) {
111
112     scalar_t vx = _belly_xc - _head_xc, vy = _belly_yc - _head_yc;
113     scalar_t l = sqrt(vx * vx + vy * vy);
114     vx /= l;
115     vy /= l;
116
117     scalar_t belly_radius = 12 + thickness / 2;
118
119     if(l > _head_radius + thickness + belly_radius) {
120       image->draw_line(thickness, r, g, b,
121                        _head_xc + vx * (_head_radius + thickness/2),
122                        _head_yc + vy * (_head_radius + thickness/2),
123                        _belly_xc - vx * (belly_radius - thickness/2),
124                        _belly_yc - vy * (belly_radius - thickness/2));
125     }
126
127     // An ugly way to make a filled disc
128
129     for(scalar_t u = 0; u < belly_radius; u += thickness / 2) {
130       image->draw_ellipse(thickness, r, g, b, _belly_xc, _belly_yc, u, u, 0);
131     }
132
133   }
134 }
135
136 void Pose::print(ostream *out) {
137   (*out) << "  _head_xc " << _head_xc << endl;
138   (*out) << "  _head_yc " << _head_yc << endl;
139   (*out) << "  _head_radius " << _head_radius << endl;
140   (*out) << "  _belly_xc " << _belly_xc << endl;
141   (*out) << "  _belly_yc " << _belly_yc << endl;
142 }
143
144 void Pose::write(ostream *out) {
145   write_var(out, &_bounding_box_xmin);
146   write_var(out, &_bounding_box_ymin);
147   write_var(out, &_bounding_box_xmax);
148   write_var(out, &_bounding_box_ymax);
149   write_var(out, &_head_xc); write_var(out, &_head_yc);
150   write_var(out, &_head_radius);
151   write_var(out, &_belly_xc);
152   write_var(out, &_belly_yc);
153 }
154
155 void Pose::read(istream *in) {
156   read_var(in, &_bounding_box_xmin);
157   read_var(in, &_bounding_box_ymin);
158   read_var(in, &_bounding_box_xmax);
159   read_var(in, &_bounding_box_ymax);
160   read_var(in, &_head_xc); read_var(in, &_head_yc);
161   read_var(in, &_head_radius);
162   read_var(in, &_belly_xc);
163   read_var(in, &_belly_yc);
164 }