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 r, float theta);
00061     CPoint(char s, double r, double theta) { dimn=2; x=r * cos(theta); y=r * sin(theta); z=0; }
00062     
00063     CPoint(PointMode m, float r, float theta, float psi);
00064     CPoint(char s, double r, double theta, double ele){ dimn=3; x=r*cos(theta)*cos(ele); y=r*sin(theta)*cos(ele); z=r*sin(ele);}
00065     
00066                         
00067 //  CPoint(CPoint &other);      // Copy
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 r, double theta) { dimn=2; x=r * cos(theta); y=r * sin(theta); z=0; }
00080     void set(char s, double r, double theta, double ele){ dimn=3; x=r*cos(theta)*cos(ele); y=r*sin(theta)*cos(ele); z=r*sin(ele);}
00081     
00082     unsigned dim() { return dimn; } // get dimension
00083     unsigned setdim(unsigned);      // set new dimension
00084     
00085     //----------------------------------------------------------
00086     // CVector Unary Operations
00087     CPoint operator-();                // unary minus
00088     CPoint operator~();                // unary 2D perp operator
00089     
00090     int operator == (CPoint);
00091                         // Comparison (dimension must match, or not)
00092     int operator != (CPoint);
00093                         // Point and Vector Operations (always valid) 
00094 //  CPoint operator - (CPoint);     // Vector difference
00095 //  CPoint  operator + (CPoint);        // +translate
00096 //  CPoint& operator += (CPoint);       // inc translate
00097 //  CPoint& operator -= (CPoint);       // dec translate
00098     
00099         //----------------------------------------------------------
00100     // CVector Arithmetic Operations
00101     CPoint operator+( CPoint);        // vector add
00102     CPoint operator-( CPoint);        // vector subtract
00103     double operator*( CPoint);        // inner dot product
00104     double operator|( CPoint);        // 2D exterior perp product
00105     CPoint operator^( CPoint);        // 3D exterior cross product
00106 
00107     CPoint& operator*=( double);      // vector scalar mult
00108     CPoint& operator/=( double);      // vector scalar div
00109     CPoint& operator+=( CPoint);      // vector increment
00110     CPoint& operator-=( CPoint);      // vector decrement
00111     CPoint& operator^=( CPoint);      // 3D exterior cross product
00112 
00113                         // CPoint Scalar Operations (convenient but often illegal)
00114                         // Scalar Multiplication
00115     friend CPoint operator * (int, CPoint);
00116     friend CPoint operator * (float, CPoint);
00117     friend CPoint operator * (double, CPoint);
00118     friend CPoint operator * (CPoint, int);
00119     friend CPoint operator * (CPoint, float);
00120     friend CPoint operator * (CPoint, double);
00121                         // Scalar Division
00122     friend CPoint operator / (CPoint, int);
00123     friend CPoint operator / (CPoint, float);
00124     friend CPoint operator / (CPoint, double);
00125                         // CPoint Relations
00126     double distance(CPoint *);      // Distance
00127     double distance2(CPoint *);     // Distance^2
00128     double distance(CPoint &);      // Distance
00129     double distance2(CPoint &);     // Distance^2
00130 //  double isLeft(CPoint, CPoint);      // 2D only
00131     
00132     //----------------------------------------------------------
00133     // CVector Properties
00134     double len() {                    // vector length
00135         return sqrt(x*x + y*y + z*z);
00136     }
00137     
00138     double len2() {                   // vector length squared (faster)
00139         return (x*x + y*y + z*z);
00140     }   
00141                         // Polar/Spherical coordinates
00142     double r() { return len(); };
00143     double theta();
00144     double phi();
00145 
00146     double ele();   
00147                         // 2D Rotation
00148     void rotateBy(double angle);
00149                         // Pretty-printing
00150     void dump() { printf(" CP: %g @ %g @ %g", x, y, z); }
00151     
00152     void normalize();                 // convert vector to unit length
00153 
00154 };
00155 
00156 }
00157 
00158 #endif      // SS_Point_H
00159 

Generated on Fri Apr 6 20:18:14 2007 for CSL by  doxygen 1.4.5-20051010