CSL  6.0
CPoint.h
Go to the documentation of this file.
1 //
2 // CPoint.h -- n-dimensional point class specification
3 //
4 // Copyright 2002, softSurfer (www.softsurfer.com)
5 // This code may be freely used and modified for any purpose
6 // providing that this copyright notice is included with it.
7 // SoftSurfer makes no warranty for this code, and cannot be held
8 // liable for any real or imagined damage resulting from it's use.
9 // Users of this code must verify correctness for their application.
10 //
11 // Extended by Stephen Pope -- see the CSL copyright notice
12 
13 #ifndef CSL_Point_H
14 #define CSL_Point_H
15 
16 #include "CSL_Types.h" // TRUE/FALSE
17 #include <stdio.h> // printf
18 #include <math.h> // trig fcns.
19 
20 #define COORD_TYPE float // type of point members
21 
22 namespace csl {
23 
24 #ifdef CSL_ENUMS
25 typedef enum { // point types
26  kCartesian,
27  kPolar
28 } PointMode;
29 #else
30  #define kCartesian 0
31  #define kPolar 1
32  typedef int PointMode;
33 #endif
34 
35 // CPoint Class Definition
36 
37 class CPoint {
38 
39 public:
40  unsigned dimn; // # dimensions (1, 2, or 3)
41  COORD_TYPE x, y, z; // z = 0 for 2D, y = z = 0 for 1D
42 
43  // Lots of Constructors
44  CPoint() { dimn = 3; x = y = z = 0; }
45 
46  // ~~~~~~~~~ 1D ~~~~~~~~~~~~~~
47  CPoint(int a) { dimn = 1; x = (float) a; y = z = 0; }
48  CPoint(float a) { dimn = 1; x = a; y = z = 0; }
49  CPoint(double a) { dimn = 1; x = a; y = z = 0; }
50 
51  // ~~~~~~~~~ 2D (defaults are Cartesian) ~~~~~~~~~~~~~~
52  CPoint(int a, int b) { dimn = 2; x = (float) a; y = (float) b; z = 0; }
53  CPoint(float a, float b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0; }
54  CPoint(double a, double b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0; }
55 
56  // ~~~~~~~~~ 3D ~~~~~~~~~~~~~~
57  CPoint(int a, int b, int c) { dimn = 3; x = (float) a; y = (float) b; z = c; }
58  CPoint(float a, float b, float c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c; }
59  CPoint(double a, double b, double c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c; }
60 
61  // ~~~~~~~~~ 2-D and 3-D polar ~~~~~~~~~~~~~~
62  CPoint(PointMode m, float tr, float ttheta);
63  CPoint(char s, double tr, double ttheta) { dimn=2; x=tr * cosf(ttheta); y=tr * sinf(ttheta); z=0; }
64 
65  CPoint(PointMode m, float tr, float ttheta, float psi);
66  CPoint(char s, double tr, double ttheta, double tele){
67  dimn=3; x=tr*cosf(ttheta)*cosf(tele); y=tr*sinf(ttheta)*cosf(tele); z=tr*sinf(tele);}
68 
69 // CPoint(CPoint & other); // Copy constructor -- use '=' instead
70  ~CPoint() { }; // Destructor
71 
72  // ~~~~~~~~~ Accessors ~~~~~~~~~~~~~~
73  void set(int a, int b) { dimn = 2; x = (float) a; y = (float) b; z = 0; }
74  void set(int a, int b, int c) { dimn = 3; x = (float) a; y = (float) b; z = (float) c; }
75  void set(float a, float b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0; }
76  void set(float a, float b, float c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c; }
77  void set(double a, double b) { dimn = 2; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = 0; }
78  void set(double a, double b, double c) { dimn = 3; x = (COORD_TYPE)a; y = (COORD_TYPE)b; z = (COORD_TYPE)c; }
79  void set(PointMode m, float a, float b);
80  void set(PointMode m, float a, float b, float c);
81  void set(char s, double tr, double ttheta) { dimn=2; x=tr * cosf(ttheta); y=tr * sinf(ttheta); z=0; }
82  void set(char s, double tr, double ttheta, double tele) {
83  dimn=3; x=tr*cosf(ttheta)*cos(tele); y=tr*sinf(ttheta)*cosf(tele); z=tr*sinf(tele);}
84 
85  void setAzimuth(double taz);
86  void setElevation(double tele);
87  void setMagnitude(double tmag);
88 
89  unsigned dim() { return dimn; } // get dimension
90  unsigned setdim(unsigned); // set new dimension
91 
92  //----------------------------------------------------------
93  // CVector Unary Operations
94  CPoint operator-(); // unary minus
95  CPoint operator~(); // unary 2D perp operator
96 
97  int operator == (CPoint);
98  // Comparison (dimension must match, or not)
99  int operator != (CPoint);
100  // Point and Vector Operations (always valid)
101 // CPoint operator - (CPoint); // Vector difference
102 // CPoint operator + (CPoint); // +translate
103 // CPoint& operator += (CPoint); // inc translate
104 // CPoint& operator -= (CPoint); // dec translate
105 
106  //----------------------------------------------------------
107  // CVector Arithmetic Operations
108  CPoint operator+(CPoint); // vector add
109  CPoint operator-(CPoint); // vector subtract
110  COORD_TYPE operator*(CPoint); // inner dot product
111  COORD_TYPE operator|(CPoint); // 2D exterior perp product
112  CPoint operator^(CPoint); // 3D exterior cross product
113 
114  CPoint& operator*=(double); // vector scalar mult
115  CPoint& operator/=(double); // vector scalar div
116  CPoint& operator+=(CPoint); // vector increment
117  CPoint& operator-=(CPoint); // vector decrement
118  CPoint& operator^=(CPoint); // 3D exterior cross product
119 
120  // CPoint Scalar Operations (convenient but often illegal)
121  // Scalar Multiplication
122  friend CPoint operator * (int, CPoint);
123  friend CPoint operator * (float, CPoint);
124  friend CPoint operator * (double, CPoint);
125  friend CPoint operator * (CPoint, int);
126  friend CPoint operator * (CPoint, float);
127  friend CPoint operator * (CPoint, double);
128  // Scalar Division
129  friend CPoint operator / (CPoint, int);
130  friend CPoint operator / (CPoint, float);
131  friend CPoint operator / (CPoint, double);
132  // CPoint Relations
133  COORD_TYPE distance(CPoint *); // Distance
134  COORD_TYPE distance2(CPoint *); // Distance^2
135  COORD_TYPE distance(CPoint &); // Distance
136  COORD_TYPE distance2(CPoint &); // Distance^2
137 // COORD_TYPE isLeft(CPoint, CPoint); // 2D only
138 
139  COORD_TYPE operator () (unsigned idx) const;
140 
141  //----------------------------------------------------------
142  // CVector Properties
143  COORD_TYPE len() { // vector length
144  return sqrtf(x*x + y*y + z*z);
145  }
146 
147  COORD_TYPE len2() { // vector length squared (faster)
148  return (x*x + y*y + z*z);
149  }
150  // Polar/Spherical coordinates
151  COORD_TYPE r() { return len(); };
152  COORD_TYPE theta();
153  COORD_TYPE phi();
154 
155  COORD_TYPE ele();
156  // 2D Rotation
157  void rotateBy(double angle);
158  // Pretty-printing
159  void dump() { fprintf(stderr, " CP: %g @ %g @ %g", x, y, z); }
160  void dumpPol() {
161  fprintf(stderr, " CP: %g @ %g @ %g",
162  r(),
164  ele()* CSL_DEGS_PER_RAD);
165  }
166 
167  void normalize(); // convert vector to unit length
168 
169 };
170 
171 }
172 
173 #endif // SS_Point_H
COORD_TYPE distance2(CPoint *)
Definition: CPoint.cpp:360
int operator!=(CPoint)
Definition: CPoint.cpp:147
CPoint()
Definition: CPoint.h:44
COORD_TYPE operator*(CPoint)
Definition: CPoint.cpp:185
COORD_TYPE operator|(CPoint)
Definition: CPoint.cpp:190
CPoint(char s, double tr, double ttheta, double tele)
Definition: CPoint.h:66
unsigned dimn
Definition: CPoint.h:40
COORD_TYPE theta()
Definition: CPoint.cpp:383
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
void setElevation(double tele)
Definition: CPoint.cpp:93
CPoint(float a, float b)
Definition: CPoint.h:53
CPoint operator~()
Definition: CPoint.cpp:124
CPoint & operator-=(CPoint)
Definition: CPoint.cpp:227
COORD_TYPE len()
Definition: CPoint.h:143
void setAzimuth(double taz)
Definition: CPoint.cpp:87
void set(double a, double b, double c)
Definition: CPoint.h:78
void set(char s, double tr, double ttheta, double tele)
Definition: CPoint.h:82
COORD_TYPE ele()
Definition: CPoint.cpp:410
CPoint operator+(CPoint)
Definition: CPoint.cpp:162
#define kCartesian
Definition: CPoint.h:30
#define COORD_TYPE
Definition: CPoint.h:20
CPoint(double a, double b)
Definition: CPoint.h:54
CPoint operator^(CPoint)
Definition: CPoint.cpp:196
CPoint & operator/=(double)
Definition: CPoint.cpp:212
unsigned setdim(unsigned)
Definition: CPoint.cpp:53
void set(float a, float b)
Definition: CPoint.h:75
void normalize()
Definition: CPoint.cpp:423
COORD_TYPE x
Definition: CPoint.h:41
#define kPolar
Definition: CPoint.h:31
void rotateBy(double angle)
Definition: CPoint.cpp:416
int operator==(CPoint)
Definition: CPoint.cpp:134
CPoint(int a)
Definition: CPoint.h:47
friend CPoint operator/(CPoint, int)
Definition: CPoint.cpp:300
COORD_TYPE r()
Definition: CPoint.h:151
COORD_TYPE len2()
Definition: CPoint.h:147
CPoint & operator^=(CPoint)
Definition: CPoint.cpp:235
COORD_TYPE z
Definition: CPoint.h:41
COORD_TYPE distance(CPoint *)
Definition: CPoint.cpp:353
unsigned dim()
Definition: CPoint.h:89
void set(float a, float b, float c)
Definition: CPoint.h:76
CPoint(float a, float b, float c)
Definition: CPoint.h:58
COORD_TYPE operator()(unsigned idx) const
Definition: CPoint.cpp:338
CPoint(int a, int b)
Definition: CPoint.h:52
CPoint & operator+=(CPoint)
Definition: CPoint.cpp:219
void dumpPol()
Definition: CPoint.h:160
COORD_TYPE y
Definition: CPoint.h:41
CPoint(double a)
Definition: CPoint.h:49
CPoint(char s, double tr, double ttheta)
Definition: CPoint.h:63
CPoint(double a, double b, double c)
Definition: CPoint.h:59
void set(char s, double tr, double ttheta)
Definition: CPoint.h:81
~CPoint()
Definition: CPoint.h:70
void set(double a, double b)
Definition: CPoint.h:77
CPoint & operator*=(double)
Definition: CPoint.cpp:205
CPoint operator-()
Definition: CPoint.cpp:115
int PointMode
Definition: CPoint.h:32
void setMagnitude(double tmag)
Definition: CPoint.cpp:101
void dump()
Definition: CPoint.h:159
CPoint(float a)
Definition: CPoint.h:48
void set(int a, int b, int c)
Definition: CPoint.h:74
void set(int a, int b)
Definition: CPoint.h:73
CPoint(int a, int b, int c)
Definition: CPoint.h:57
COORD_TYPE phi()
Definition: CPoint.cpp:403
#define CSL_DEGS_PER_RAD
Definition: CSL_Types.h:342