]> git.mxchange.org Git - flightgear.git/blob - Lib/Math/point3d.hxx
Removed plib from the source distribution. It must be built and installed
[flightgear.git] / Lib / 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
23
24 #ifndef _POINT3D_HXX
25 #define _POINT3D_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32 #include "Include/compiler.h"
33
34 #ifdef FG_MATH_EXCEPTION_CLASH
35 # define exception c_exception
36 #endif
37
38 #ifdef FG_HAVE_STD_INCLUDES
39 # include <iostream>
40 # include <cassert>
41 # include <cmath>
42 #else
43 # include <iostream.h>
44 # include <assert.h>
45 # include <math.h>
46 #endif
47
48 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
49 FG_USING_STD(ostream);
50 FG_USING_STD(istream);
51 #endif
52
53 // -rp- assert.h is buggy under MWCWP3, as multiple #include undef assert !
54 #ifdef __MWERKS__
55 #  define assert(x)
56 #endif
57
58 const double fgPoint3_Epsilon = 0.0000001;
59
60 enum {PX, PY, PZ};                  // axes
61
62 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
63 class Point3D;
64 istream& operator>> ( istream&, Point3D& );
65 ostream& operator<< ( ostream&, const Point3D& );
66 Point3D operator- (const Point3D& p);               // -p1
67 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
68
69
70 ///////////////////////////
71 //
72 // 3D Point
73 //
74 ///////////////////////////
75
76 class Point3D {
77
78 protected:
79
80     double n[3];
81
82 public:
83
84     // Constructors
85
86     Point3D();
87     Point3D(const double x, const double y, const double z);
88     explicit Point3D(const double d);
89     Point3D(const Point3D &p);
90
91     // Assignment operators
92
93     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
94     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
95     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
96     Point3D& operator *= ( const double d );     // multiplication by a constant
97     Point3D& operator /= ( const double d );     // division by a constant
98
99     void setx(const double x);
100     void sety(const double y);
101     void setz(const double z);
102
103     // Queries 
104
105     double& operator [] ( int i);                // indexing
106     double operator[] (int i) const;             // read-only indexing
107
108     double x() const;      // cartesian x
109     double y() const;      // cartesian y
110     double z() const;      // cartesian z
111
112     double lon() const;    // polar longitude
113     double lat() const;    // polar latitude
114     double radius() const; // polar radius
115     double elev() const;   // geodetic elevation (if specifying a surface point)
116
117     // friends
118     friend Point3D operator - (const Point3D& p);                   // -p1
119     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
120     friend istream& operator>> ( istream&, Point3D& );
121     friend ostream& operator<< ( ostream&, const Point3D& );
122
123     // Special functions
124     double distance3D(const Point3D& a) const;        // distance between
125     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
126 };
127
128
129 // input from stream
130 inline istream&
131 operator >> ( istream& in, Point3D& p)
132 {
133     char c;
134
135     in >> p.n[PX];
136
137     // read past optional comma
138     while ( in.get(c) ) {
139         if ( (c != ' ') && (c != ',') ) {
140             // push back on the stream
141             in.putback(c);
142             break;
143         }
144     }
145         
146     in >> p.n[PY];
147
148     // read past optional comma
149     while ( in.get(c) ) {
150         if ( (c != ' ') && (c != ',') ) {
151             // push back on the stream
152             in.putback(c);
153             break;
154         }
155     }
156         
157     in >> p.n[PZ];
158
159     return in;
160 }
161
162 inline ostream&
163 operator<< ( ostream& out, const Point3D& p )
164 {
165     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
166 }
167
168 ///////////////////////////
169 //
170 // Point3D Member functions
171 //
172 ///////////////////////////
173
174 // CONSTRUCTORS
175
176 inline Point3D::Point3D() {}
177
178 inline Point3D::Point3D(const double x, const double y, const double z)
179 {
180     n[PX] = x; n[PY] = y; n[PZ] = z;
181 }
182
183 inline Point3D::Point3D(const double d)
184 {
185     n[PX] = n[PY] = n[PZ] = d;
186 }
187
188 inline Point3D::Point3D(const Point3D& p)
189 {
190     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
191 }
192
193 // ASSIGNMENT OPERATORS
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 Point3D& p )
206 {
207     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
208 }
209
210 inline Point3D& Point3D::operator *= ( const double d )
211 {
212     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
213 }
214
215 inline Point3D& Point3D::operator /= ( const double d )
216 {
217     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
218     return *this;
219 }
220
221 inline void Point3D::setx(const double x) {
222     n[PX] = x;
223 }
224
225 inline void Point3D::sety(const double y) {
226     n[PY] = y;
227 }
228
229 inline void Point3D::setz(const double z) {
230     n[PZ] = z;
231 }
232
233 // QUERIES
234
235 inline double& Point3D::operator [] ( int i)
236 {
237     assert(! (i < PX || i > PZ));
238     return n[i];
239 }
240
241 inline double Point3D::operator [] ( int i) const {
242     assert(! (i < PX || i > PZ));
243     return n[i];
244 }
245
246
247 inline double Point3D::x() const { return n[PX]; }
248
249 inline double Point3D::y() const { return n[PY]; }
250
251 inline double Point3D::z() const { return n[PZ]; }
252
253 inline double Point3D::lon() const { return n[PX]; }
254
255 inline double Point3D::lat() const { return n[PY]; }
256
257 inline double Point3D::radius() const { return n[PZ]; }
258
259 inline double Point3D::elev() const { return n[PZ]; }
260
261
262 // FRIENDS
263
264 inline Point3D operator - (const Point3D& a)
265 {
266     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
267 }
268
269 inline Point3D operator + (const Point3D& a, const Point3D& b)
270 {
271     return Point3D(a) += b;
272 }
273
274 inline Point3D operator - (const Point3D& a, const Point3D& b)
275 {
276     return Point3D(a) -= b;
277 }
278
279 inline Point3D operator * (const Point3D& a, const double d)
280 {
281     return Point3D(a) *= d;
282 }
283
284 inline Point3D operator * (const double d, const Point3D& a)
285 {
286     return a*d;
287 }
288
289 inline Point3D operator / (const Point3D& a, const double d)
290 {
291     return Point3D(a) *= (1.0 / d );
292 }
293
294 inline bool operator == (const Point3D& a, const Point3D& b)
295 {
296     return
297         (a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
298         (a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
299         (a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
300 }
301
302 inline bool operator != (const Point3D& a, const Point3D& b)
303 {
304     return !(a == b);
305 }
306
307 // Special functions
308
309 inline double
310 Point3D::distance3D(const Point3D& a ) const
311 {
312     double x, y, z;
313
314     x = n[PX] - a.n[PX];
315     y = n[PY] - a.n[PY];
316     z = n[PZ] - a.n[PZ];
317
318     return sqrt(x*x + y*y + z*z);
319 }
320
321
322 inline double
323 Point3D::distance3Dsquared(const Point3D& a ) const
324 {
325     double x, y, z;
326
327     x = n[PX] - a.n[PX];
328     y = n[PY] - a.n[PY];
329     z = n[PZ] - a.n[PZ];
330
331     return(x*x + y*y + z*z);
332 }
333
334
335 #endif // _POINT3D_HXX
336
337