Hauptseite | Liste aller Namensbereiche | Klassenhierarchie | Alphabetische Liste | Übersicht | Auflistung der Dateien | Elemente eines Namensbereiches | Datenstruktur-Elemente | Datei-Elemente

polarline.h

gehe zur Dokumentation dieser Datei
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     // for a single point I:
00059     // M(I)=sqrt( X(I)^2+Y(I)^2 ),
00060     // A(I)=atan( Y(I)/X(I) )
00061 
00062     // aus computergraphischem kartesischem koordinatensys.
00063     // int math. Koordinatensys. mit Nullpunkt in Bildmitte
00064     // umrechnen
00065     p1.x -= w/2;
00066     p2.x -= w/2;
00067     p1.y -= h/2;
00068     p2.y -= h/2;
00069 
00070     // an x-Achse spiegeln
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 //    std::cerr << "atan2: " << std::fixed << std::setprecision(1) <<
00079 //      theta*180./M_PI << "° ("<<theta<<")\n";
00080 //    theta = atan(dy/dx);
00081 //    theta += M_PI/2;
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 //    double theta1 = atan(double(p1.y)/double(p1.x));
00100     double r1 = sqrt(sqr(p1.x)+sqr(p1.y));
00101 //    r = sin(theta-theta1)*r1;
00102     r = cos(theta-theta1)*r1;
00103 //    std::cerr << std::fixed << std::setprecision(1) << "dx:" << dx << " dy:" << dy
00104 //      << " theta:" << theta << " theta1:" << theta1
00105 //      << " r1:" << r1 << " r:" << r << std::endl;
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   // lösen einer quadratischen Gleichung (in Normalform)
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   // numerisch instabil, wenn cos(theta) ~= 0,
00142   // da r/cos(theta) -> +-INF
00143   // und yx*tan(theta) -> +-INF
00144   float rct = 0;
00145   float ct=cos(theta);
00146   if(fabs(ct) > 1e-6)
00147   {
00148     rct = r/ct;
00149     //  anders: x = (r-y*sin(theta)) / cos(theta);
00150     p1.p.x = int((rct-y1*tan(theta)));
00151     p2.p.x = int((rct-y2*tan(theta)));
00152   }
00153   else
00154   { // y1~r -> 0 // y1~0 -> r
00155     // geht folgendes kürzer? try mupad!
00156     p1.p.x = int( radius*cos(asin(r/radius)) );
00157     p2.p.x = -p1.p.x;
00158 //    if(DEBUGLEVEL>=1)
00159   //    std::cerr << "find_p1_p2(): p1="<<p1<<std::endl;
00160   }
00161 
00162   p1.p.y = -int(y1);
00163   p2.p.y = -int(y2);
00164   return 0;
00165 }
00166 
00167 }; // polarLine
00168 
00169 std::ostream& operator<<(std::ostream& s, const polarLine& l);
00170 
00171 } // namespace seemicro
00172 
00173 #endif
00174 

Erzeugt am Sun Oct 3 12:52:47 2004 für seemicro von doxygen 1.3.2