orca-robotics


INTRODUCTION
Overview
Download and Install
Documentation

REPOSITORY
Interfaces
Drivers
Libraries
Utilities
Software Map

DEVELOPER
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         

geom2d.h

00001 /*
00002  * Orca-Robotics Project: Components for robotics 
00003  *               http://orca-robotics.sf.net/
00004  * Copyright (c) 2004-2008 Alex Brooks
00005  *
00006  * This copy of Orca is licensed to you under the terms described in
00007  * the LICENSE file included in this distribution.
00008  *
00009  */
00010 
00011 #ifndef GEOM2D_H
00012 #define GEOM2D_H
00013 
00014 #include <string>
00015 #include <cmath>
00016 #include <iostream>
00017 
00018 #include <hydroportability/sharedlib.h>
00019 
00020 namespace geom2d {
00021 
00025     class SOEXPORT Point {
00026     public:
00027 
00028         explicit Point() {}
00029         explicit Point( double x, double y )
00030             { p_[0] = x; p_[1] = y; }
00031 
00032         double x() const { return p_[0]; }
00033         double y() const { return p_[1]; }
00034 
00035         double &x() { return p_[0]; }
00036         double &y() { return p_[1]; }
00037 
00038         // allow treatment as a vector
00039         double operator[]( int i ) const { return p_[i]; }
00040         double &operator[]( int i ) { return p_[i]; }
00041 
00042         // convert to polar
00043         double range() const { return std::sqrt(y()*y()+x()*x()); }
00044         double bearing() const { return std::atan2(y(),x()); }
00045 
00046         std::string toString()   const;
00047         std::string toStringXY() const;
00048         std::string toStringRB() const;
00049 
00050     private:
00051         double p_[2];
00052     };
00053 
00057     class SOEXPORT PolarPoint {
00058     public:
00059         
00060         explicit PolarPoint() {}
00061         explicit PolarPoint( double r, double b )
00062             { p_[0] = r; p_[1] = b; }
00063 
00064         double range()   const { return p_[0]; }
00065         double bearing() const { return p_[1]; }
00066 
00067         double &range()   { return p_[0]; }
00068         double &bearing() { return p_[1]; }
00069 
00070         // allow treatment as a vector
00071         double operator[]( int i ) const { return p_[i]; }
00072 
00073         // convert to cartesian
00074         double x() const { return range()*std::cos(bearing()); }
00075         double y() const { return range()*std::sin(bearing()); }
00076 
00077         std::string toString()   const;
00078         std::string toStringXY() const;
00079         std::string toStringRB() const;
00080 
00081     private:
00082         double p_[2];
00083     };
00084 
00086     inline double dist( const Point &p1, const Point &p2 )
00087     { return std::sqrt((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00089     inline double dist( const PolarPoint &p1, const PolarPoint &p2 )
00090     { return std::sqrt((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00091 
00093     inline double distSq( const Point &p1, const Point &p2 )
00094     { return ((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00096     inline double distSq( const PolarPoint &p1, const PolarPoint &p2 )
00097     { return ((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00098 
00100     inline bool operator==( const Point &p1, const Point &p2 )
00101     { return (p1.x()==p2.x()) && (p1.y()==p2.y()); }
00103     inline bool operator!=( const Point &p1, const Point &p2 )
00104     { return !(p1==p2); }
00105 
00107     // in place
00108     inline void rotate( Point &p, double theta )
00109     {
00110         const double ct = std::cos(theta);
00111         const double st = std::sin(theta);
00112         double xNew = p.x()*ct - p.y()*st;
00113         double yNew = p.x()*st + p.y()*ct;
00114         p.x()=xNew;
00115         p.y()=yNew;
00116     }
00118     // not in place
00119     inline void rotate( const Point &p, double theta, Point &q )
00120     {
00121         const double ct = std::cos(theta);
00122         const double st = std::sin(theta);
00123         q.x() = p.x()*ct - p.y()*st;
00124         q.y() = p.x()*st + p.y()*ct;
00125     }
00126 
00129     // in place
00130     inline void addTransform( Point &p,
00131                               double x, double y, double theta )
00132     {
00133         rotate( p, theta );
00134         p.x() += x;
00135         p.y() += y;
00136     }
00139     // not in place
00140     inline void addTransform( const Point &p,
00141                               double x, double y, double theta,
00142                               Point &tp )
00143     {
00144         rotate( p, theta, tp );
00145         tp.x() += x;
00146         tp.y() += y;
00147     }
00148 
00151     // in place
00152     inline void subtractTransform( Point &p,
00153                                    double x, double y, double theta )
00154     {
00155         p.x() -= x;
00156         p.y() -= y;
00157         rotate( p, -theta );
00158     }
00161     // not in place
00162     inline void subtractTransform( const Point &p,
00163                                    double x, double y, double theta,
00164                                    Point &tp )
00165     {
00166         tp.x() = p.x()-x;
00167         tp.y() = p.y()-y;
00168         rotate( tp, -theta );
00169     }
00170 
00171     inline std::ostream &operator<<( std::ostream &s, const Point &p )
00172     { return s << p.toString(); }
00173 
00174     inline std::ostream &operator<<( std::ostream &s, const PolarPoint &p )
00175     { return s << p.toString(); }
00176 
00186     SOEXPORT bool circleCircleIntersection( double a1,
00187                                             double b1,
00188                                             double r1,
00189                                             double a2,
00190                                             double b2,
00191                                             double r2,
00192                                             double &px,
00193                                             double &py,
00194                                             double &qx,
00195                                             double &qy );
00196 
00198     inline bool circleCircleIntersection( const Point &c1,
00199                                           double       r1,
00200                                           const Point &c2,
00201                                           double       r2,
00202                                           Point  &p,
00203                                           Point  &q )
00204     {
00205         return circleCircleIntersection( c1.x(),c1.y(),r1, c2.x(),c2.y(),r2, p.x(),p.y(), q.x(),q.y() );
00206     }
00207 
00211     class SOEXPORT Line {
00212     public:
00213 
00215         Line( const Point &p1, const Point &p2 );
00216 
00218         Line( const Point &p, double vx, double vy );
00219 
00220         void getUnitVectorParallel( double v[2] ) const
00221             { v[0] = -b_; v[1] = a_; }
00222         void getUnitVectorPerpendicular( double v[2] ) const
00223             { v[0] = a_; v[1] = b_; }
00224 
00226         double a() const { return a_; }
00228         double b() const { return b_; }
00230         double c() const { return c_; }
00231 
00232     private:
00233 
00234         // Represent in general equation form: ax + by + c = 0
00235         // (normalised such that hypot(a,b) = 1)
00236         double a_, b_, c_;
00237     };
00238     std::string toString( const Line &l );
00239     inline std::ostream &operator<<( std::ostream &s, const Line &l )
00240     { return s << toString(l); }
00241 
00243     SOEXPORT double dist( const Line &l, const Point &p );
00244 
00246     inline double dotProduct( double v1[2], double v2[2] )
00247     { return v1[0]*v2[0] + v1[1]*v2[1]; }
00248 
00250     inline Point closestPointToOrigin( const Line &l )
00251     { return Point( -l.a()*l.c(), -l.b()*l.c() ); }
00252 
00254     SOEXPORT Point closestPointOnLine( const Line &line, const Point &point );
00255 
00258     SOEXPORT bool lineLineIntersection( const Line &l1,
00259                                         const Line &l2,
00260                                         Point &p );
00261     
00264     SOEXPORT void convertToRhoAlpha( const Line &line, double &rho, double &alpha );
00265 
00268     SOEXPORT double rho( const Line &line );
00269 
00272     SOEXPORT double alpha( const Line &line );
00273 
00274 }
00275 
00276 #endif
 

Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)


Generated for Orca Robotics by  doxygen 1.4.5