00001 #ifndef POLARLINE_H
00002 #define POLARLINE_H
00003
00004 #include "opencvext.h"
00005 #include <iostream>
00006 #include <iomanip>
00007
00008 namespace seemicro
00009 {
00010
00011 extern int DEBUGLEVEL;
00012
00022 struct polarLine
00023 {
00031 double r, theta;
00032 polarLine() {}
00033 polarLine(double r1, double t1) { r = r1; theta = t1; }
00034
00038 void normalize()
00039 {
00040 if(r<0)
00041 {
00042 r=fabs(r);
00043 theta += M_PI;
00044 if(theta>=2*M_PI) theta-=2*M_PI;
00045 }
00046 }
00047
00056 void fromCart(int w, int h, CvPoint p1, CvPoint p2)
00057 {
00058
00059
00060
00061
00062
00063
00064
00065 p1.x -= w/2;
00066 p2.x -= w/2;
00067 p1.y -= h/2;
00068 p2.y -= h/2;
00069
00070
00071 p1.y = -p1.y;
00072 p2.y = -p2.y;
00073
00074 double dx = p1.x - p2.x;
00075 double dy = p1.y - p2.y;
00076
00077 theta = atan2(dy, dx);
00078
00079
00080
00081
00082
00083 if(theta<-M_PI/2)
00084 {
00085 theta = 3*M_PI/2+theta;
00086 }
00087 else if(theta<0)
00088 {
00089 theta = 3*M_PI/2+theta;
00090 }
00091 else if(theta>M_PI/2)
00092 {
00093 theta = theta-M_PI/2;
00094 }
00095 else
00096 theta = theta+M_PI/2;
00097
00098 double theta1 = atan2(double(p1.y), double(p1.x));
00099
00100 double r1 = sqrt(sqr(p1.x)+sqr(p1.y));
00101
00102 r = cos(theta-theta1)*r1;
00103
00104
00105
00106
00107 normalize();
00108 assert(r>=0);
00109 assert(theta>=0);
00110 assert(theta<=2*M_PI);
00111 }
00112
00113 void draw(IplImage *img, double color=255);
00114
00121 int find_p1_p2(mypoint& p1, mypoint& p2, float radius)
00122 {
00123
00124 float p = -2*r*sin(theta);
00125 float q = r*r-radius*radius*cos(theta)*cos(theta);
00126 float det = p*p/4 - q;
00127
00128 if(det<0)
00129 {
00130 p1.p.x = p1.p.y = p2.p.x = p2.p.y = 0;
00131 #if defined(DEBUG) || defined(_DEBUG)
00132 if(DEBUGLEVEL>=1)
00133 std::cerr << "find_p1_p2(): det = " << det << " < 0\n";
00134 #endif
00135 return -1;
00136 }
00137
00138 float y1 = -p/2 + sqrt(det);
00139 float y2 = -p/2 - sqrt(det);
00140
00141
00142
00143
00144 float rct = 0;
00145 float ct=cos(theta);
00146 if(fabs(ct) > 1e-6)
00147 {
00148 rct = r/ct;
00149
00150 p1.p.x = int((rct-y1*tan(theta)));
00151 p2.p.x = int((rct-y2*tan(theta)));
00152 }
00153 else
00154 {
00155
00156 p1.p.x = int( radius*cos(asin(r/radius)) );
00157 p2.p.x = -p1.p.x;
00158
00159
00160 }
00161
00162 p1.p.y = -int(y1);
00163 p2.p.y = -int(y2);
00164 return 0;
00165 }
00166
00167 };
00168
00169 std::ostream& operator<<(std::ostream& s, const polarLine& l);
00170
00171 }
00172
00173 #endif
00174