INTRODUCTION
Overview
Download and Install
Quick Start
Documentation
Publications

NONFRAMEWORK CODE
Driver Interfaces
Drivers
Libraries
Utilities

FRAMEWORK CODE
Interfaces
Components
Libraries
Utilities

Full Software Listings

DEVELOPER
Tutorials
Examples
Dev Guide
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-2009 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 Rect {
00058     public:
00059 
00060         Rect( const geom2d::Point &bottomLeft,
00061               const geom2d::Point &topRight )
00062             : bottomLeft_(bottomLeft), topRight_(topRight) {}
00063 
00064         geom2d::Point &bottomLeft() { return bottomLeft_; }
00065         geom2d::Point &topRight() { return topRight_; }
00066         const geom2d::Point &bottomLeft() const { return bottomLeft_; }
00067         const geom2d::Point &topRight() const { return topRight_; }
00068 
00069     private:
00070         // Extents of the rectangle
00071         geom2d::Point bottomLeft_, topRight_;
00072     };
00073     std::string toString( const Rect &rect );
00074     inline std::ostream &operator<<( std::ostream &s, const Rect &rect )
00075     { return s << toString(rect); }
00076 
00080     class SOEXPORT PolarPoint {
00081     public:
00082         
00083         explicit PolarPoint() {}
00084         explicit PolarPoint( double r, double b )
00085             { p_[0] = r; p_[1] = b; }
00086 
00087         double range()   const { return p_[0]; }
00088         double bearing() const { return p_[1]; }
00089 
00090         double &range()   { return p_[0]; }
00091         double &bearing() { return p_[1]; }
00092 
00093         // allow treatment as a vector
00094         double operator[]( int i ) const { return p_[i]; }
00095 
00096         // convert to cartesian
00097         double x() const { return range()*std::cos(bearing()); }
00098         double y() const { return range()*std::sin(bearing()); }
00099 
00100         std::string toString()   const;
00101         std::string toStringXY() const;
00102         std::string toStringRB() const;
00103 
00104     private:
00105         double p_[2];
00106     };
00107 
00109     inline double dist( const Point &p1, const Point &p2 )
00110     { return std::sqrt((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00112     inline double dist( const PolarPoint &p1, const PolarPoint &p2 )
00113     { return std::sqrt((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00114 
00116     inline double distSq( const Point &p1, const Point &p2 )
00117     { return ((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00119     inline double distSq( const PolarPoint &p1, const PolarPoint &p2 )
00120     { return ((p1.y()-p2.y())*(p1.y()-p2.y())+(p1.x()-p2.x())*(p1.x()-p2.x())); }
00121 
00123     inline bool operator==( const Point &p1, const Point &p2 )
00124     { return (p1.x()==p2.x()) && (p1.y()==p2.y()); }
00126     inline bool operator!=( const Point &p1, const Point &p2 )
00127     { return !(p1==p2); }
00128 
00130     // in place
00131     inline void rotate( Point &p, double theta )
00132     {
00133         const double ct = std::cos(theta);
00134         const double st = std::sin(theta);
00135         double xNew = p.x()*ct - p.y()*st;
00136         double yNew = p.x()*st + p.y()*ct;
00137         p.x()=xNew;
00138         p.y()=yNew;
00139     }
00141     // not in place
00142     inline void rotate( const Point &p, double theta, Point &q )
00143     {
00144         const double ct = std::cos(theta);
00145         const double st = std::sin(theta);
00146         q.x() = p.x()*ct - p.y()*st;
00147         q.y() = p.x()*st + p.y()*ct;
00148     }
00149 
00152     // in place
00153     inline void addTransform( Point &p,
00154                               double x, double y, double theta )
00155     {
00156         rotate( p, theta );
00157         p.x() += x;
00158         p.y() += y;
00159     }
00162     // not in place
00163     inline void addTransform( const Point &p,
00164                               double x, double y, double theta,
00165                               Point &tp )
00166     {
00167         rotate( p, theta, tp );
00168         tp.x() += x;
00169         tp.y() += y;
00170     }
00171 
00174     // in place
00175     inline void subtractTransform( Point &p,
00176                                    double x, double y, double theta )
00177     {
00178         p.x() -= x;
00179         p.y() -= y;
00180         rotate( p, -theta );
00181     }
00184     // not in place
00185     inline void subtractTransform( const Point &p,
00186                                    double x, double y, double theta,
00187                                    Point &tp )
00188     {
00189         tp.x() = p.x()-x;
00190         tp.y() = p.y()-y;
00191         rotate( tp, -theta );
00192     }
00193 
00194     inline std::ostream &operator<<( std::ostream &s, const Point &p )
00195     { return s << p.toString(); }
00196 
00197     inline std::ostream &operator<<( std::ostream &s, const PolarPoint &p )
00198     { return s << p.toString(); }
00199 
00209     SOEXPORT bool circleCircleIntersection( double a1,
00210                                             double b1,
00211                                             double r1,
00212                                             double a2,
00213                                             double b2,
00214                                             double r2,
00215                                             double &px,
00216                                             double &py,
00217                                             double &qx,
00218                                             double &qy );
00219 
00221     inline bool circleCircleIntersection( const Point &c1,
00222                                           double       r1,
00223                                           const Point &c2,
00224                                           double       r2,
00225                                           Point  &p,
00226                                           Point  &q )
00227     {
00228         return circleCircleIntersection( c1.x(),c1.y(),r1, c2.x(),c2.y(),r2, p.x(),p.y(), q.x(),q.y() );
00229     }
00230 
00234     class SOEXPORT Line {
00235     public:
00236 
00238         Line( const Point &p1, const Point &p2 );
00239 
00241         Line( const Point &p, double vx, double vy );
00242 
00243         void getUnitVectorParallel( double v[2] ) const
00244             { v[0] = -b_; v[1] = a_; }
00245         void getUnitVectorPerpendicular( double v[2] ) const
00246             { v[0] = a_; v[1] = b_; }
00247 
00249         double a() const { return a_; }
00251         double b() const { return b_; }
00253         double c() const { return c_; }
00254 
00255     private:
00256 
00257         // Represent in general equation form: ax + by + c = 0
00258         // (normalised such that hypot(a,b) = 1)
00259         double a_, b_, c_;
00260     };
00261     std::string toString( const Line &l );
00262     inline std::ostream &operator<<( std::ostream &s, const Line &l )
00263     { return s << toString(l); }
00264 
00266     SOEXPORT double dist( const Line &l, const Point &p );
00267 
00269     inline double dotProduct( double v1[2], double v2[2] )
00270     { return v1[0]*v2[0] + v1[1]*v2[1]; }
00271 
00273     inline Point closestPointToOrigin( const Line &l )
00274     { return Point( -l.a()*l.c(), -l.b()*l.c() ); }
00275 
00277     SOEXPORT Point closestPointOnLine( const Line &line, const Point &point );
00278 
00281     SOEXPORT bool lineLineIntersection( const Line &l1,
00282                                         const Line &l2,
00283                                         Point &p );
00284     
00287     SOEXPORT void convertToRhoAlpha( const Line &line, double &rho, double &alpha );
00288 
00291     SOEXPORT double rho( const Line &line );
00292 
00295     SOEXPORT double alpha( const Line &line );
00296 
00297 }
00298 
00299 #endif
 

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


Generated for Orca Robotics by  doxygen 1.4.5