src/Geometry.h

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice, this
00009 list of conditions and the following disclaimer.
00010 
00011 2. Redistributions in binary form must reproduce the above copyright notice,
00012 this list of conditions and the following disclaimer in the documentation
00013 and/or other materials provided with the distribution.
00014 
00015 3. Neither the name of the University of Amsterdam nor the names of its
00016 contributors may be used to endorse or promote products derived from this
00017 software without specific prior written permission.
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00023 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 */
00030 
00051 #ifndef _GEOMETRY_
00052 #define _GEOMETRY_
00053 
00054 #include "math.h"       // needed for M_PI constant
00055 #include <string>       // needed for string
00056 #include <iostream>
00057 
00058 using namespace std;
00059 
00060 typedef double AngRad;  
00061 typedef double AngDeg;  
00063 #define EPSILON 0.0001  
00065 // auxiliary numeric functions for determining the
00066 // maximum and minimum of two given double values and the sign of a value
00067 double max     ( double d1, double d2 );
00068 double min     ( double d1, double d2 );
00069 int    sign    ( double d1            );
00070 
00071 // auxiliary goniometric functions which enable you to
00072 // specify angles in degrees rather than in radians
00073 AngDeg Rad2Deg ( AngRad x             );
00074 AngRad Deg2Rad ( AngDeg x             );
00075 double cosDeg  ( AngDeg x             );
00076 double sinDeg  ( AngDeg x             );
00077 double tanDeg  ( AngDeg x             );
00078 AngDeg atanDeg ( double x             );
00079 double atan2Deg( double x,  double y  );
00080 AngDeg acosDeg ( double x             );
00081 AngDeg asinDeg ( double x             );
00082 
00083 // various goniometric functions
00084 bool   isAngInInterval     ( AngDeg ang,    AngDeg angMin,    AngDeg angMax );
00085 AngDeg getBisectorTwoAngles( AngDeg angMin, AngDeg angMax );
00086 
00093 enum CoordSystemT {
00094   CARTESIAN,
00095   POLAR
00096 };
00097 
00098 /*****************************************************************************/
00099 /********************   CLASS VECPOSITION   **********************************/
00100 /*****************************************************************************/
00101 
00108 class VecPosition
00109 {
00110   // private member data
00111 private:
00112 
00113   double m_x;   
00114   double m_y;   
00116   // public methods
00117 public:
00118   // constructor for VecPosition class
00119   VecPosition                               ( double            vx = 0,
00120                                               double            vy = 0,
00121                                               CoordSystemT      cs =CARTESIAN);
00122 
00123   // overloaded arithmetic operators
00124   VecPosition        operator -             (                                );
00125   VecPosition        operator +             ( const double      &d           );
00126   VecPosition        operator +             ( const VecPosition &p           );
00127   VecPosition        operator -             ( const double      &d           );
00128   VecPosition        operator -             ( const VecPosition &p           );
00129   VecPosition        operator *             ( const double      &d           );
00130   VecPosition        operator *             ( const VecPosition &p           );
00131   VecPosition        operator /             ( const double      &d           );
00132   VecPosition        operator /             ( const VecPosition &p           );
00133   void               operator =             ( const double      &d           );
00134   void               operator +=            ( const VecPosition &p           );
00135   void               operator +=            ( const double      &d           );
00136   void               operator -=            ( const VecPosition &p           );
00137   void               operator -=            ( const double      &d           );
00138   void               operator *=            ( const VecPosition &p           );
00139   void               operator *=            ( const double      &d           );
00140   void               operator /=            ( const VecPosition &p           );
00141   void               operator /=            ( const double      &d           );
00142   bool               operator !=            ( const VecPosition &p           );
00143   bool               operator !=            ( const double      &d           );
00144   bool               operator ==            ( const VecPosition &p           );
00145   bool               operator ==            ( const double      &d           );
00146 
00147   // methods for producing output
00148   friend ostream&    operator <<            ( ostream           &os,
00149                                               VecPosition       p            );
00150   void               show                   ( CoordSystemT      cs =CARTESIAN);
00151   string             str                    ( CoordSystemT      cs =CARTESIAN);
00152 
00153   // set- and get methods for private member variables
00154   bool               setX                   ( double            dX           );
00155   double             getX                   (                          ) const;
00156   bool               setY                   ( double            dY           );
00157   double             getY                   (                          ) const;
00158 
00159   // set- and get methods for derived position information
00160   void               setVecPosition         ( double            dX = 0,
00161                                               double            dY = 0,
00162                                               CoordSystemT      cs =CARTESIAN);
00163   double             getDistanceTo          ( const VecPosition p            );
00164   double             getDistanceTo          ( double            x,
00165                                               double            y            );
00166   VecPosition        setMagnitude           ( double            d            );
00167   double             getMagnitude           (                          ) const;
00168   AngDeg             getDirection           (                          ) const;
00169 
00170   // comparison methods for positions
00171   bool               isInFrontOf            ( const VecPosition &p           );
00172   bool               isInFrontOf            ( const double      &d           );
00173   bool               isBehindOf             ( const VecPosition &p           );
00174   bool               isBehindOf             ( const double      &d           );
00175   bool               isLeftOf               ( const VecPosition &p           );
00176   bool               isLeftOf               ( const double      &d           );
00177   bool               isRightOf              ( const VecPosition &p           );
00178   bool               isRightOf              ( const double      &d           );
00179   bool               isBetweenX             ( const VecPosition &p1,
00180                                               const VecPosition &p2          );
00181   bool               isBetweenX             ( const double      &d1,
00182                                               const double      &d2          );
00183   bool               isBetweenY             ( const VecPosition &p1,
00184                                               const VecPosition &p2          );
00185   bool               isBetweenY             ( const double      &d1,
00186                                               const double      &d2          );
00187 
00188   // conversion methods for positions
00189   VecPosition        normalize              (                                );
00190   VecPosition        rotate                 ( AngDeg            angle        );
00191   VecPosition        globalToRelative       ( VecPosition       orig,
00192                                               AngDeg            ang          );
00193   VecPosition        relativeToGlobal       ( VecPosition       orig,
00194                                               AngDeg            ang          );
00195   VecPosition        getVecPositionOnLineFraction( VecPosition  &p,
00196                                               double            dFrac        );
00197 
00198   // static class methods
00199   static VecPosition getVecPositionFromPolar( double            dMag,
00200                                               AngDeg            ang          );
00201   static AngDeg      normalizeAngle         ( AngDeg            angle        );
00202   static double      pointsDistance         ( double            x1,
00203                                               double            y1,
00204                                               double            x2,
00205                                               double            y2           );  
00206 };
00207 
00208 /*****************************************************************************/
00209 /*********************   CLASS GEOMETRY   ************************************/
00210 /*****************************************************************************/
00211 
00213 class Geometry
00214 {
00215 
00216 public:
00217 
00218   // geometric series
00219   static double getLengthGeomSeries(double dFirst,double dRatio,double dSum  );
00220   static double getSumGeomSeries   (double dFirst,double dRatio,double dLen  );
00221   static double getSumInfGeomSeries(double dFirst,double dRatio              );
00222   static double getFirstGeomSeries (double dSum,  double dRatio,double dLen  );
00223   static double getFirstInfGeomSeries(double dSum,double dRatio              );
00224 
00225   // abc formula
00226   static int    abcFormula(double a,double b, double c, double *s1,double *s2);
00227 };
00228 
00229 /*****************************************************************************/
00230 /********************** CLASS CIRCLE *****************************************/
00231 /*****************************************************************************/
00232 
00235 class Circle
00236 {
00237     VecPosition m_posCenter;            
00238     double      m_dRadius;              
00240 public:
00241     Circle( );
00242     Circle( VecPosition pos, double dR );
00243 
00244     void        show                  ( ostream& os = cout );
00245 
00246     // get and set methods
00247     bool        setCircle             ( VecPosition pos,
00248                                         double      dR  );
00249     bool        setRadius             ( double dR       );
00250     double      getRadius             (                 );
00251     bool        setCenter             ( VecPosition pos );
00252     VecPosition getCenter             (                 );
00253     double      getCircumference      (                 );
00254     double      getArea               (                 );
00255 
00256     // calculate intersection points and area with other circle
00257     bool        isInside              ( VecPosition pos );
00258     int         getIntersectionPoints ( Circle      c,
00259                                         VecPosition *p1,
00260                                         VecPosition *p2 );
00261     double      getIntersectionArea   ( Circle c        );
00262 
00263 
00264 }  ;
00265 
00266 /*****************************************************************************/
00267 /*********************** CLASS LINE ******************************************/
00268 /*****************************************************************************/
00269 
00273 class Line
00274 {
00275   // a line is defined by the formula: ay + bx + c = 0
00276   double m_a; 
00277   double m_b; 
00278   double m_c; 
00280 public:
00281   Line( double a, double b, double c );
00282 
00283   // print methods
00284   void        show( ostream& os = cout );
00285   friend      ostream& operator << (ostream & os, Line l);
00286 
00287   // get intersection points with this line
00288   VecPosition getIntersection            ( Line        line                  );
00289   int         getCircleIntersectionPoints( Circle      circle,
00290                                            VecPosition *posSolution1,
00291                                            VecPosition *posSolution2         );
00292   Line        getTangentLine             ( VecPosition pos                   );
00293   VecPosition getPointOnLineClosestTo    ( VecPosition pos                   );
00294   double      getDistanceWithPoint       ( VecPosition pos                   );
00295   bool        isInBetween                ( VecPosition pos,
00296                                            VecPosition point1,
00297                                            VecPosition point2                );
00298 
00299   // calculate associated variables in the line
00300   double      getYGivenX                 ( double      x );
00301   double      getXGivenY                 ( double      y );
00302   double      getACoefficient            (               ) const;
00303   double      getBCoefficient            (               ) const;
00304   double      getCCoefficient            (               ) const;
00305 
00306   // static methods to make a line using an easier representation.
00307   static Line makeLineFromTwoPoints      ( VecPosition pos1,
00308                                            VecPosition pos2                  );
00309   static Line makeLineFromPositionAndAngle( VecPosition vec,
00310                                            AngDeg angle                      );
00311 };
00312 
00313 /*****************************************************************************/
00314 /********************** CLASS RECTANGLE **************************************/
00315 /******************************************************************************/
00319 class Rect
00320 {
00321   VecPosition m_posLeftTop;     
00322   VecPosition m_posRightBottom; 
00324 public:
00325   Rect                          ( VecPosition pos, VecPosition pos2 );
00326 
00327   void        show              ( ostream& os = cout                );
00328 
00329   // checks whether point lies inside the rectangle
00330   bool        isInside          ( VecPosition pos                   );
00331 
00332   // standard get and set methosd
00333   void        setRectanglePoints( VecPosition pos1,
00334                                   VecPosition pos2                  );
00335   bool        setPosLeftTop     ( VecPosition pos                   );
00336   VecPosition getPosLeftTop     (                                   );
00337   bool        setPosRightBottom ( VecPosition pos                   );
00338   VecPosition getPosRightBottom (                                   );
00339 };
00340 
00341 #endif

Generated on Thu Apr 26 22:45:27 2007 for GangOfSix(GOS)-RoboCupTeamProject by  doxygen 1.5.1-p1