]> git.mxchange.org Git - flightgear.git/blob - Math/point3d.hxx
Point3D tweaks.
[flightgear.git] / Math / point3d.hxx
1 // point3d.hxx -- a 3d point class.  
2 //
3 // Adapted from algebra3 by Jean-Francois Doue, started October 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifndef _POINT3D_HXX
26 #define _POINT3D_HXX
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32
33 #include <Misc/fgstream.hxx>
34
35 #include "Include/fg_stl_config.h"
36
37 #ifdef NEEDNAMESPACESTD
38 using namespace std;
39 #endif
40
41 #include <assert.h>
42
43 #include <Include/fg_constants.h>
44
45
46 enum {PX, PY, PZ};                  // axes
47
48
49 ///////////////////////////
50 //
51 // 3D Point
52 //
53 ///////////////////////////
54
55 class Point3D {
56
57 protected:
58
59     double n[3];
60
61 public:
62
63     // Constructors
64
65     Point3D();
66     Point3D(const double x, const double y, const double z);
67     Point3D(const double d);
68     Point3D(const Point3D &p);
69
70     // Assignment operators
71
72     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
73     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
74     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
75     Point3D& operator *= ( const double d );     // multiplication by a constant
76     Point3D& operator /= ( const double d );     // division by a constant
77
78     void setx(const double x);
79     void sety(const double y);
80     void setz(const double z);
81
82     // Queries 
83
84     double& operator [] ( int i);                // indexing
85     double operator[] (int i) const;             // read-only indexing
86
87     double x() const;      // cartesian x
88     double y() const;      // cartesian y
89     double z() const;      // cartesian z
90
91     double lon() const;    // polar longitude
92     double lat() const;    // polar latitude
93     double radius() const; // polar radius
94     double elev() const;   // geodetic elevation (if specifying a surface point)
95
96     // friends
97
98     friend Point3D operator - (const Point3D& p);                   // -p1
99     friend Point3D operator + (const Point3D& a, const Point3D& b); // p1 + p2
100     friend Point3D operator - (const Point3D& a, const Point3D& b); // p1 - p2
101     friend Point3D operator * (const Point3D& a, const double d);   // p1 * 3.0
102     friend Point3D operator * (const double d, const Point3D& a);   // 3.0 * p1
103     friend Point3D operator / (const Point3D& a, const double d);   // p1 / 3.0
104     friend bool operator == (const Point3D& a, const Point3D& b);   // p1 == p2?
105     friend bool operator != (const Point3D& a, const Point3D& b);   // p1 != p2?
106
107     // Special functions
108     double distance3D(const Point3D& a, const Point3D& b); // distance between
109 };
110
111
112 // output to stream
113 inline ostream&
114 operator << ( ostream& out, Point3D& p)
115 {
116     double x, y, z;
117
118     x = p.x();
119     y = p.y();
120     z = p.z();
121
122     out << x << " " << y << " " << z;
123
124     return out;
125 }
126
127 // input from stream
128 inline istream&
129 operator >> ( istream& in, Point3D& p)
130 {
131     double x, y, z;
132     char c;
133
134     in >> x;
135
136     // read past optional comma
137     while ( in.get(c) ) {
138         if ( (c != ' ') && (c != ',') ) {
139             // push back on the stream
140             in.putback(c);
141             break;
142         }
143     }
144         
145     in >> y;
146
147     // read past optional comma
148     while ( in.get(c) ) {
149         if ( (c != ' ') && (c != ',') ) {
150             // push back on the stream
151             in.putback(c);
152             break;
153         }
154     }
155         
156     in >> z;
157
158     p = Point3D(x, y, z);
159
160     return in;
161 }
162
163 ///////////////////////////
164 //
165 // Point3D Member functions
166 //
167 ///////////////////////////
168
169 // CONSTRUCTORS
170
171 inline Point3D::Point3D() {}
172
173 inline Point3D::Point3D(const double x, const double y, const double z)
174 {
175     n[PX] = x; n[PY] = y; n[PZ] = z;
176 }
177
178 inline Point3D::Point3D(const double d)
179 {
180     n[PX] = n[PY] = n[PZ] = d;
181 }
182
183 inline Point3D::Point3D(const Point3D& p)
184 {
185     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
186 }
187
188 // ASSIGNMENT OPERATORS
189
190 inline Point3D& Point3D::operator = (const Point3D& p)
191 {
192     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
193 }
194
195 inline Point3D& Point3D::operator += ( const Point3D& p )
196 {
197     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
198 }
199
200 inline Point3D& Point3D::operator -= ( const Point3D& p )
201 {
202     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
203 }
204
205 inline Point3D& Point3D::operator *= ( const double d )
206 {
207     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
208 }
209
210 inline Point3D& Point3D::operator /= ( const double d )
211 {
212     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
213     return *this;
214 }
215
216 inline void Point3D::setx(const double x) {
217     n[PX] = x;
218 }
219
220 inline void Point3D::sety(const double y) {
221     n[PY] = y;
222 }
223
224 inline void Point3D::setz(const double z) {
225     n[PZ] = z;
226 }
227
228 // QUERIES
229
230 inline double& Point3D::operator [] ( int i)
231 {
232     assert(! (i < PX || i > PZ));
233     return n[i];
234 }
235
236 inline double Point3D::operator [] ( int i) const {
237     assert(! (i < PX || i > PZ));
238     return n[i];
239 }
240
241
242 inline double Point3D::x() const { return n[PX]; }
243
244 inline double Point3D::y() const { return n[PY]; }
245
246 inline double Point3D::z() const { return n[PZ]; }
247
248 inline double Point3D::lon() const { return n[PX]; }
249
250 inline double Point3D::lat() const { return n[PY]; }
251
252 inline double Point3D::radius() const { return n[PZ]; }
253
254 inline double Point3D::elev() const { return n[PZ]; }
255
256
257 // FRIENDS
258
259 inline Point3D operator - (const Point3D& a)
260 {
261     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
262 }
263
264 inline Point3D operator + (const Point3D& a, const Point3D& b)
265 {
266     return Point3D(a.n[PX]+ b.n[PX], a.n[PY] + b.n[PY], a.n[PZ] + b.n[PZ]);
267 }
268
269 inline Point3D operator - (const Point3D& a, const Point3D& b)
270 {
271     return Point3D(a.n[PX]-b.n[PX], a.n[PY]-b.n[PY], a.n[PZ]-b.n[PZ]);
272 }
273
274 inline Point3D operator * (const Point3D& a, const double d)
275 {
276     return Point3D(d*a.n[PX], d*a.n[PY], d*a.n[PZ]);
277 }
278
279 inline Point3D operator * (const double d, const Point3D& a)
280 {
281     return a*d;
282 }
283
284 inline Point3D operator / (const Point3D& a, const double d)
285 {
286     double d_inv = 1./d;
287     return Point3D(a.n[PX]*d_inv, a.n[PY]*d_inv, a.n[PZ]*d_inv);
288 }
289
290 inline bool operator == (const Point3D& a, const Point3D& b)
291 {
292     return
293         (a.n[PX] - b.n[PX]) < FG_EPSILON &&
294         (a.n[PY] - b.n[PY]) < FG_EPSILON &&
295         (a.n[PZ] - b.n[PZ]) < FG_EPSILON;
296 }
297
298 inline bool operator != (const Point3D& a, const Point3D& b)
299 {
300     return !(a == b);
301 }
302
303 // Special functions
304
305 inline double distance3D(const Point3D& a, const Point3D& b)
306 {
307     double x, y, z;
308
309     x = a[PX] - b[PX];
310     y = a[PY] - b[PY];
311     z = a[PZ] - b[PZ];
312
313     return sqrt(x*x + y*y + z*z);
314 }
315
316 #endif // _POINT3D_HXX
317
318
319 // $Log$
320 // Revision 1.2  1998/10/18 01:17:12  curt
321 // Point3D tweaks.
322 //
323 // Revision 1.1  1998/10/16 00:50:29  curt
324 // Added point3d.hxx to replace cheezy fgPoint3d struct.
325 //
326 //