line.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <utils/math/angle.h>
00027 #include <models/shape/line.h>
00028
00029
00030
00031
00032 using namespace std;
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 LineShape::LineShape(unsigned int roi_width, unsigned int roi_height)
00043 {
00044 r = 0;
00045 phi = 0;
00046 count = 0;
00047
00048 max_length = (int)sqrt( roi_width * roi_width + roi_height * roi_height );
00049 last_calc_r = last_calc_phi = 0.f;
00050
00051 this->roi_width = roi_width;
00052 this->roi_height = roi_height;
00053
00054 }
00055
00056
00057
00058 LineShape::~LineShape()
00059 {
00060 }
00061
00062
00063
00064
00065
00066 void
00067 LineShape::printToStream(std::ostream &stream)
00068 {
00069 stream << "r=" << r << " phi=" << phi
00070 << " count= " << count;
00071 }
00072
00073 void
00074 LineShape::setMargin(unsigned int margin)
00075 {
00076 this->margin = margin;
00077 }
00078
00079
00080 bool
00081 LineShape::isClose(unsigned int in_roi_x, unsigned int in_roi_y)
00082 {
00083 return false;
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 }
00099
00100
00101 void
00102 LineShape::calcPoints()
00103 {
00104
00105 if ((last_calc_r == r) && (last_calc_phi == phi)) return;
00106 last_calc_r = r;
00107 last_calc_phi = phi;
00108
00109 float rad_angle = fawkes::deg2rad(phi);
00110
00111
00112 bool reverse_direction = false;
00113
00114
00115
00116
00117 if ( rad_angle < M_PI/4 ) {
00118 x1 = (int)round( r * cos( rad_angle ) );
00119 y1 = (int)round( r * sin( rad_angle ) );
00120 y2 = 0;
00121 x2 = (int)round( r / cos( rad_angle ) );
00122 } else if ( rad_angle < M_PI/2 ) {
00123 x1 = (int)round( r * cos( rad_angle ) );
00124 y1 = (int)round( r * sin( rad_angle ) );
00125 x2 = 0;
00126 y2 = (int)round( r / cos( M_PI/2 - rad_angle ) );
00127 } else if ( rad_angle < 3.0/4.0 * M_PI ) {
00128 x1 = (int)round(-r * cos( M_PI - rad_angle ) );
00129 y1 = (int)round( r * sin( M_PI - rad_angle ) );
00130 x2 = 0;
00131 y2 = (int)round( r / cos( rad_angle - M_PI/2 ) );
00132
00133
00134
00135 if (r >= 0.0) {
00136 reverse_direction = true;
00137 }
00138 } else {
00139
00140 x1 = (int)round(-r * cos( M_PI - rad_angle ) );
00141 y1 = (int)round( r * sin( M_PI - rad_angle ) );
00142 y2 = 0;
00143 x2 = (int)round(-r / cos( M_PI - rad_angle ) );
00144
00145
00146
00147 if (r < 0.0) {
00148 reverse_direction = true;
00149 }
00150 }
00151
00152 if ( ! (x1 == x2 &&
00153 y1 == y2 ) ) {
00154
00155
00156 float vx, vy, length;
00157 vx = x1 - x2 ;
00158 vy = y1 - y2 ;
00159 length = sqrt( vx * vx + vy * vy );
00160
00161 vx /= length;
00162 vy /= length;
00163 vx *= max_length;
00164 vy *= max_length;
00165
00166 if ( ! reverse_direction) {
00167 x1 += (int)vx;
00168 y1 += (int)vy;
00169 } else {
00170 x1 -= (int)vx;
00171 y1 -= (int)vy;
00172 }
00173
00174 } else {
00175
00176
00177 if (x2 == 0) {
00178 x1 = roi_width;
00179 y1 = y2;
00180 } else if (y2 == 0) {
00181 x1 = x2;
00182 y1 = roi_height;
00183 } else {
00184 cout << "ERROR!" << endl
00185 << " This case should not have occurred. Please have a look at method" << endl
00186 << " \"LineShape::calc()\". Treatment of special case is not correct." << endl;
00187
00188 }
00189 }
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199 void
00200 LineShape::getPoints(int *x1, int *y1, int *x2, int *y2)
00201 {
00202 calcPoints();
00203
00204 *x1 = this->x1;
00205 *y1 = this->y1;
00206 *x2 = this->x2;
00207 *y2 = this->y2;
00208 }
00209
00210
00211