CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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(),
163 theta()* CSL_DEGS_PER_RAD,
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