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 namespace csl {
00021 
00022 #ifdef CSL_ENUMS
00023 typedef enum {              // point types
00024     kCartesian,
00025     kPolar
00026 } PointMode;
00027 #else
00028     #define kCartesian 0
00029     #define kPolar 1
00030     typedef int PointMode;
00031 #endif
00032 
00033 //  CPoint Class Definition
00034 
00035 class CPoint {
00036 
00037 public:
00038     unsigned dimn;      // # dimensions (1, 2, or 3)
00039     double x, y, z;     // z = 0 for 2D, y = z = 0 for 1D
00040 
00041                         // Lots of Constructors
00042     CPoint() { dimn = 3; x = y = z = 0;  }
00043     
00044     // ~~~~~~~~~ 1D ~~~~~~~~~~~~~~
00045     CPoint(int a) { dimn = 1; x = a; y = z = 0; }
00046     CPoint(float a) { dimn = 1; x = a; y = z = 0;  }
00047     CPoint(double a) { dimn = 1; x = a; y = z = 0;  }
00048     
00049     // ~~~~~~~~~ 2D (defaults are Cartesian) ~~~~~~~~~~~~~~
00050     CPoint(int a, int b) { dimn = 2; x = a; y = b; z = 0;  }
00051     CPoint(float a, float b) { dimn = 2; x = (double)a; y = (double)b; z = 0;  }
00052     CPoint(double a, double b) { dimn = 2; x = a; y = b; z = 0;  }
00053     
00054     // ~~~~~~~~~ 3D ~~~~~~~~~~~~~~
00055     CPoint(int a, int b, int c) { dimn = 3; x = a; y = b; z = c;  }
00056     CPoint(float a, float b, float c) { dimn = 3; x = (double)a; y = (double)b; z = (double)c;  }
00057     CPoint(double a, double b, double c) { dimn = 3; x = a; y = b; z = c;  }
00058     
00059     // ~~~~~~~~~ 2-D and 3-D polar ~~~~~~~~~~~~~~
00060     CPoint(PointMode m, float tr, float ttheta);
00061     CPoint(char s, double tr, double ttheta) { dimn=2; x=tr * cos(ttheta); y=tr * sin(ttheta); z=0; }
00062     
00063     CPoint(PointMode m, float tr, float ttheta, float psi);
00064     CPoint(char s, double tr, double ttheta, double tele){ 
00065             dimn=3; x=tr*cos(ttheta)*cos(tele); y=tr*sin(ttheta)*cos(tele); z=tr*sin(tele);}
00066     
00067 //  CPoint(CPoint & other);     // Copy constructor -- use '=' instead
00068     ~CPoint() { };              // Destructor
00069 
00070     // ~~~~~~~~~ Accessors ~~~~~~~~~~~~~~
00071     void set(int a, int b) { dimn = 2; x = a; y = b; z = 0;  }
00072     void set(int a, int b, int c) { dimn = 3; x = a; y = b; z = c;  }
00073     void set(float a, float b) { dimn = 2; x = (double)a; y = (double)b; z = 0;  }
00074     void set(float a, float b, float c) { dimn = 3; x = (double)a; y = (double)b; z = (double)c;  }
00075     void set(double a, double b) { dimn = 2; x = a; y = b; z = 0;  }
00076     void set(double a, double b, double c) { dimn = 3; x = a; y = b; z = c;  }
00077     void set(PointMode m, float a, float b);
00078     void set(PointMode m, float a, float b, float c);
00079     void set(char s, double tr, double ttheta) { dimn=2; x=tr * cos(ttheta); y=tr * sin(ttheta); z=0; }
00080     void set(char s, double tr, double ttheta, double tele) { 
00081             dimn=3; x=tr*cos(ttheta)*cos(tele); y=tr*sin(ttheta)*cos(tele); z=tr*sin(tele);}
00082 
00083     void setAzimuth(double taz);
00084     void setElevation(double tele);
00085     void setMagnitude(double tmag);
00086     
00087     unsigned dim() { return dimn; } // get dimension
00088     unsigned setdim(unsigned);      // set new dimension
00089     
00090     //----------------------------------------------------------
00091     // CVector Unary Operations
00092     CPoint operator-();                // unary minus
00093     CPoint operator~();                // unary 2D perp operator
00094     
00095     int operator == (CPoint);
00096                         // Comparison (dimension must match, or not)
00097     int operator != (CPoint);
00098                         // Point and Vector Operations (always valid) 
00099 //  CPoint operator - (CPoint);     // Vector difference
00100 //  CPoint  operator + (CPoint);        // +translate
00101 //  CPoint& operator += (CPoint);       // inc translate
00102 //  CPoint& operator -= (CPoint);       // dec translate
00103     
00104         //----------------------------------------------------------
00105     // CVector Arithmetic Operations
00106     CPoint operator+( CPoint);        // vector add
00107     CPoint operator-( CPoint);        // vector subtract
00108     double operator*( CPoint);        // inner dot product
00109     double operator|( CPoint);        // 2D exterior perp product
00110     CPoint operator^( CPoint);        // 3D exterior cross product
00111 
00112     CPoint& operator*=( double);      // vector scalar mult
00113     CPoint& operator/=( double);      // vector scalar div
00114     CPoint& operator+=( CPoint);      // vector increment
00115     CPoint& operator-=( CPoint);      // vector decrement
00116     CPoint& operator^=( CPoint);      // 3D exterior cross product
00117 
00118                         // CPoint Scalar Operations (convenient but often illegal)
00119                         // Scalar Multiplication
00120     friend CPoint operator * (int, CPoint);
00121     friend CPoint operator * (float, CPoint);
00122     friend CPoint operator * (double, CPoint);
00123     friend CPoint operator * (CPoint, int);
00124     friend CPoint operator * (CPoint, float);
00125     friend CPoint operator * (CPoint, double);
00126                         // Scalar Division
00127     friend CPoint operator / (CPoint, int);
00128     friend CPoint operator / (CPoint, float);
00129     friend CPoint operator / (CPoint, double);
00130                         // CPoint Relations
00131     double distance(CPoint *);      // Distance
00132     double distance2(CPoint *);     // Distance^2
00133     double distance(CPoint &);      // Distance
00134     double distance2(CPoint &);     // Distance^2
00135 //  double isLeft(CPoint, CPoint);      // 2D only
00136 
00137    double operator () (unsigned idx) const;
00138     
00139     //----------------------------------------------------------
00140     // CVector Properties
00141     double len() {                    // vector length
00142         return sqrt(x*x + y*y + z*z);
00143     }
00144     
00145     double len2() {                   // vector length squared (faster)
00146         return (x*x + y*y + z*z);
00147     }   
00148                         // Polar/Spherical coordinates
00149     double r() { return len(); };
00150     double theta();
00151     double phi();
00152 
00153     double ele();   
00154                         // 2D Rotation
00155     void rotateBy(double angle);
00156                         // Pretty-printing
00157     void dump() { printf(" CP: %g @ %g @ %g", x, y, z); }
00158     void dumpPol() {
00159         printf(" CP: %g @ %g @ %g", 
00160                 r(), 
00161                 theta()* CSL_DEGS_PER_RAD, 
00162                 ele()* CSL_DEGS_PER_RAD);
00163     }
00164 
00165     void normalize();                 // convert vector to unit length
00166 
00167 };
00168 
00169 }
00170 
00171 #endif      // SS_Point_H

Generated on Thu Sep 17 23:14:16 2009 for CSL by  doxygen 1.5.8