CPoint.h

Go to the documentation of this file.
00001 //
00002 // CPoint.h -- n-dimensional point class specification
00003 //
00004 // Copyright 2002, softSurfer (www.softsurfer.com)
00005 // This code may be freely used and modified for any purpose
00006 // providing that this copyright notice is included with it.
00007 // SoftSurfer makes no warranty for this code, and cannot be held
00008 // liable for any real or imagined damage resulting from it's use.
00009 // Users of this code must verify correctness for their application.
00010 //
00011 // Extended by Stephen Pope -- see the CSL copyright notice
00012 
00013 #ifndef CSL_Point_H
00014 #define CSL_Point_H
00015 
00016 #include "CSL_Types.h"      // TRUE/FALSE
00017 #include <stdio.h>          // printf
00018 #include <math.h>           // trig fcns.
00019 
00020 #define COORD_TYPE  float       // type of point members
00021 
00022 namespace csl {
00023 
00024 #ifdef CSL_ENUMS
00025 typedef enum {              // point types
00026     kCartesian,
00027     kPolar
00028 } PointMode;
00029 #else
00030     #define kCartesian 0
00031     #define kPolar 1
00032     typedef int PointMode;
00033 #endif
00034 
00035 //  CPoint Class Definition
00036 
00037 class CPoint {
00038 
00039 public:
00040     unsigned dimn;      // # dimensions (1, 2, or 3)
00041     COORD_TYPE x, y, z;     // z = 0 for 2D, y = z = 0 for 1D
00042 
00043                         // Lots of Constructors
00044     CPoint() { dimn = 3; x = y = z = 0;  }
00045     
00046     // ~~~~~~~~~ 1D ~~~~~~~~~~~~~~
00047     CPoint(int a) { dimn = 1; x = (float) a; y = z = 0; }
00048     CPoint(float a) { dimn = 1; x = a; y = z = 0;  }
00049     CPoint(double a) { dimn = 1; x = a; y = z = 0;  }
00050     
00051     // ~~~~~~~~~ 2D (defaults are Cartesian) ~~~~~~~~~~~~~~
00052     CPoint(int a, int b) { dimn = 2; x = (float) a; y = (float) b; z = 0;  }
00053     CPoint(float a, float b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0;  }
00054     CPoint(double a, double b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0;  }
00055     
00056     // ~~~~~~~~~ 3D ~~~~~~~~~~~~~~
00057     CPoint(int a, int b, int c) { dimn = 3; x = (float) a; y = (float) b; z = c;  }
00058     CPoint(float a, float b, float c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c;  }
00059     CPoint(double a, double b, double c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c;  }
00060     
00061     // ~~~~~~~~~ 2-D and 3-D polar ~~~~~~~~~~~~~~
00062     CPoint(PointMode m, float tr, float ttheta);
00063     CPoint(char s, double tr, double ttheta) { dimn=2; x=tr * cosf(ttheta); y=tr * sinf(ttheta); z=0; }
00064     
00065     CPoint(PointMode m, float tr, float ttheta, float psi);
00066     CPoint(char s, double tr, double ttheta, double tele){ 
00067             dimn=3; x=tr*cosf(ttheta)*cosf(tele); y=tr*sinf(ttheta)*cosf(tele); z=tr*sinf(tele);}
00068     
00069 //  CPoint(CPoint & other);     // Copy constructor -- use '=' instead
00070     ~CPoint() { };              // Destructor
00071 
00072     // ~~~~~~~~~ Accessors ~~~~~~~~~~~~~~
00073     void set(int a, int b) { dimn = 2; x = (float) a; y = (float) b; z = 0;  }
00074     void set(int a, int b, int c) { dimn = 3; x = (float) a; y = (float) b; z = (float) c;  }
00075     void set(float a, float b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0;  }
00076     void set(float a, float b, float c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c;  }
00077     void set(double a, double b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0;  }
00078     void set(double a, double b, double c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c;  }
00079     void set(PointMode m, float a, float b);
00080     void set(PointMode m, float a, float b, float c);
00081     void set(char s, double tr, double ttheta) { dimn=2; x=tr * cosf(ttheta); y=tr * sinf(ttheta); z=0; }
00082     void set(char s, double tr, double ttheta, double tele) { 
00083             dimn=3; x=tr*cosf(ttheta)*cos(tele); y=tr*sinf(ttheta)*cosf(tele); z=tr*sinf(tele);}
00084 
00085     void setAzimuth(double taz);
00086     void setElevation(double tele);
00087     void setMagnitude(double tmag);
00088     
00089     unsigned dim() { return dimn; } // get dimension
00090     unsigned setdim(unsigned);      // set new dimension
00091     
00092     //----------------------------------------------------------
00093     // CVector Unary Operations
00094     CPoint operator-();                // unary minus
00095     CPoint operator~();                // unary 2D perp operator
00096     
00097     int operator == (CPoint);
00098                         // Comparison (dimension must match, or not)
00099     int operator != (CPoint);
00100                         // Point and Vector Operations (always valid) 
00101 //  CPoint operator - (CPoint);     // Vector difference
00102 //  CPoint  operator + (CPoint);        // +translate
00103 //  CPoint& operator += (CPoint);       // inc translate
00104 //  CPoint& operator -= (CPoint);       // dec translate
00105     
00106         //----------------------------------------------------------
00107     // CVector Arithmetic Operations
00108     CPoint operator+(CPoint);        // vector add
00109     CPoint operator-(CPoint);        // vector subtract
00110     COORD_TYPE operator*(CPoint);    // inner dot product
00111     COORD_TYPE operator|(CPoint);    // 2D exterior perp product
00112     CPoint operator^(CPoint);        // 3D exterior cross product
00113 
00114     CPoint& operator*=(double);      // vector scalar mult
00115     CPoint& operator/=(double);      // vector scalar div
00116     CPoint& operator+=(CPoint);      // vector increment
00117     CPoint& operator-=(CPoint);      // vector decrement
00118     CPoint& operator^=(CPoint);      // 3D exterior cross product
00119 
00120                         // CPoint Scalar Operations (convenient but often illegal)
00121                         // Scalar Multiplication
00122     friend CPoint operator * (int, CPoint);
00123     friend CPoint operator * (float, CPoint);
00124     friend CPoint operator * (double, CPoint);
00125     friend CPoint operator * (CPoint, int);
00126     friend CPoint operator * (CPoint, float);
00127     friend CPoint operator * (CPoint, double);
00128                         // Scalar Division
00129     friend CPoint operator / (CPoint, int);
00130     friend CPoint operator / (CPoint, float);
00131     friend CPoint operator / (CPoint, double);
00132                         // CPoint Relations
00133     COORD_TYPE distance(CPoint *);      // Distance
00134     COORD_TYPE distance2(CPoint *);     // Distance^2
00135     COORD_TYPE distance(CPoint &);      // Distance
00136     COORD_TYPE distance2(CPoint &);     // Distance^2
00137 //  COORD_TYPE isLeft(CPoint, CPoint);  // 2D only
00138 
00139    COORD_TYPE operator () (unsigned idx) const;
00140     
00141     //----------------------------------------------------------
00142     // CVector Properties
00143     COORD_TYPE len() {                    // vector length
00144         return sqrtf(x*x + y*y + z*z);
00145     }
00146     
00147     COORD_TYPE len2() {                   // vector length squared (faster)
00148         return (x*x + y*y + z*z);
00149     }   
00150                         // Polar/Spherical coordinates
00151     COORD_TYPE r() { return len(); };
00152     COORD_TYPE theta();
00153     COORD_TYPE phi();
00154 
00155     COORD_TYPE ele();   
00156                         // 2D Rotation
00157     void rotateBy(double angle);
00158                         // Pretty-printing
00159     void dump() { printf(" CP: %g @ %g @ %g", x, y, z); }
00160     void dumpPol() {
00161         printf(" CP: %g @ %g @ %g", 
00162                 r(), 
00163                 theta()* CSL_DEGS_PER_RAD, 
00164                 ele()* CSL_DEGS_PER_RAD);
00165     }
00166 
00167     void normalize();                 // convert vector to unit length
00168 
00169 };
00170 
00171 }
00172 
00173 #endif      // SS_Point_H

Generated on Sat Oct 17 14:12:30 2009 for CSL by  doxygen 1.4.5-20051010