Line.C
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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef LINE_C_DEFINED
00039 #define LINE_C_DEFINED
00040
00041 #include "FeatureMatching/Line.H"
00042 #include "Image/DrawOps.H"
00043 #include "GUI/DebugWin.H"
00044 #include "Util/FastMathFunctions.H"
00045
00046
00047 Line::Line(float sx, float sy, float ex, float ey) :
00048 p1(sx,sy),
00049 p2(ex,ey),
00050 itsDirectionIdx(-1)
00051 {}
00052
00053
00054 Line::Line(Point2D<float> inP1, Point2D<float> inP2) :
00055 p1(inP1),
00056 p2(inP2),
00057 itsDirectionIdx(-1)
00058 {}
00059
00060
00061 Line::Line(Point2D<int> inP1, Point2D<int> inP2) :
00062 p1(inP1),
00063 p2(inP2),
00064 itsDirectionIdx(-1)
00065 {}
00066
00067
00068 double Line::getOri()
00069 {
00070 double theta = atan2(p2.j-p1.j,p2.i-p1.i);
00071 if (theta<0)
00072 theta += M_PI;
00073 return theta;
00074 }
00075
00076
00077 double Line::getLength()
00078 {
00079 double dx = p2.i - p1.i;
00080 double dy = p2.j - p1.j;
00081 return sqrt(dx*dx + dy*dy);
00082 }
00083
00084
00085 void Line::scale(double s)
00086 {
00087 p1.i *= s;
00088 p1.j *= s;
00089 p2.i *= s;
00090 p2.j *= s;
00091 }
00092
00093
00094 void Line::scale(double sx, double sy)
00095 {
00096 p1.i *= sx;
00097 p1.j *= sy;
00098 p2.i *= sx;
00099 p2.j *= sy;
00100 }
00101
00102
00103 Point2D<float> Line::getCenter()
00104 {
00105 Point2D<float> c = (p1+p2)/2;
00106
00107 return c;
00108
00109 }
00110
00111
00112 void Line::rotate(double theta)
00113 {
00114
00115 double sinTheta;
00116 double cosTheta;
00117 double mat[2][2];
00118
00119 sinTheta = sin(theta);
00120 cosTheta = cos(theta);
00121 mat[0][0] = cosTheta;
00122 mat[0][1] = -sinTheta;
00123 mat[1][0] = sinTheta;
00124 mat[1][1] = cosTheta;
00125
00126 double x, y;
00127 x = p1.i, y = p1.j;
00128 p1.i = x*mat[0][0] + y*mat[0][1];
00129 p1.j = x*mat[1][0] + y*mat[1][1];
00130
00131 x = p2.i, y = p2.j;
00132 p2.i = x*mat[0][0] + y*mat[0][1];
00133 p2.j = x*mat[1][0] + y*mat[1][1];
00134 }
00135
00136
00137 void Line::shear(double k1, double k2)
00138 {
00139 float tmpP1i = p1.i + p1.j*k2;
00140 float tmpP1j = p1.i*k1 + p1.j;
00141
00142 float tmpP2i = p2.i + p2.j*k2;
00143 float tmpP2j = p2.i*k1 + p2.j;
00144
00145 p1.i = tmpP1i;
00146 p1.j = tmpP1j;
00147 p2.i = tmpP2i;
00148 p2.j = tmpP2j;
00149 }
00150
00151
00152 void Line::trans(Point2D<float> p)
00153 {
00154 p1 += p;
00155 p2 += p;
00156
00157 }
00158
00159
00160 void Line::quantize(int numDirections)
00161 {
00162 Point2D<float> center = getCenter();
00163 trans(center*-1);
00164
00165 double ori = getOri();
00166 itsDirectionIdx = floor ((ori *numDirections) / (M_PI+1e-5));
00167 double dOri = (((itsDirectionIdx)*M_PI)/numDirections + M_PI/(2*numDirections)) - ori;
00168 rotate(dOri);
00169 trans(center);
00170 }
00171
00172 void Line::setQuantizedDir(int numDirections)
00173 {
00174 double ori = getOri();
00175 itsDirectionIdx = (int) floor ((ori *numDirections) / (M_PI+1e-5));
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185 #endif
00186