]> git.mxchange.org Git - flightgear.git/blob - Lib/Math/point3d.hxx
Merge FG_Lib as subdirectory
[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 // (Log is kept at end of this file)
23
24
25 #ifndef _POINT3D_HXX
26 #define _POINT3D_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33 #include "Include/compiler.h"
34
35 #ifdef FG_MATH_EXCEPTION_CLASH
36 # define exception c_exception
37 #endif
38
39 #ifdef FG_HAVE_STD_INCLUDES
40 # include <iostream>
41 # include <cassert>
42 # include <cmath>
43 #else
44 # include <iostream.h>
45 # include <assert.h>
46 # include <math.h>
47 #endif
48
49 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
50 FG_USING_STD(ostream);
51 FG_USING_STD(istream);
52 #endif
53
54 // -rp- assert.h is buggy under MWCWP3, as multiple #include undef assert !
55 #ifdef __MWERKS__
56 #  define assert(x)
57 #endif
58
59 const double fgPoint3_Epsilon = 0.0000001;
60
61 enum {PX, PY, PZ};                  // axes
62
63 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
64 class Point3D;
65 istream& operator>> ( istream&, Point3D& );
66 ostream& operator<< ( ostream&, const Point3D& );
67 Point3D operator- (const Point3D& p);               // -p1
68 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
69
70
71 ///////////////////////////
72 //
73 // 3D Point
74 //
75 ///////////////////////////
76
77 class Point3D {
78
79 protected:
80
81     double n[3];
82
83 public:
84
85     // Constructors
86
87     Point3D();
88     Point3D(const double x, const double y, const double z);
89     explicit Point3D(const double d);
90     Point3D(const Point3D &p);
91
92     // Assignment operators
93
94     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
95     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
96     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
97     Point3D& operator *= ( const double d );     // multiplication by a constant
98     Point3D& operator /= ( const double d );     // division by a constant
99
100     void setx(const double x);
101     void sety(const double y);
102     void setz(const double z);
103
104     // Queries 
105
106     double& operator [] ( int i);                // indexing
107     double operator[] (int i) const;             // read-only indexing
108
109     double x() const;      // cartesian x
110     double y() const;      // cartesian y
111     double z() const;      // cartesian z
112
113     double lon() const;    // polar longitude
114     double lat() const;    // polar latitude
115     double radius() const; // polar radius
116     double elev() const;   // geodetic elevation (if specifying a surface point)
117
118     // friends
119     friend Point3D operator - (const Point3D& p);                   // -p1
120     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
121     friend istream& operator>> ( istream&, Point3D& );
122     friend ostream& operator<< ( ostream&, const Point3D& );
123
124     // Special functions
125     double distance3D(const Point3D& a) const;        // distance between
126     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
127 };
128
129
130 // input from stream
131 inline istream&
132 operator >> ( istream& in, Point3D& p)
133 {
134     char c;
135
136     in >> p.n[PX];
137
138     // read past optional comma
139     while ( in.get(c) ) {
140         if ( (c != ' ') && (c != ',') ) {
141             // push back on the stream
142             in.putback(c);
143             break;
144         }
145     }
146         
147     in >> p.n[PY];
148
149     // read past optional comma
150     while ( in.get(c) ) {
151         if ( (c != ' ') && (c != ',') ) {
152             // push back on the stream
153             in.putback(c);
154             break;
155         }
156     }
157         
158     in >> p.n[PZ];
159
160     return in;
161 }
162
163 inline ostream&
164 operator<< ( ostream& out, const Point3D& p )
165 {
166     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
167 }
168
169 ///////////////////////////
170 //
171 // Point3D Member functions
172 //
173 ///////////////////////////
174
175 // CONSTRUCTORS
176
177 inline Point3D::Point3D() {}
178
179 inline Point3D::Point3D(const double x, const double y, const double z)
180 {
181     n[PX] = x; n[PY] = y; n[PZ] = z;
182 }
183
184 inline Point3D::Point3D(const double d)
185 {
186     n[PX] = n[PY] = n[PZ] = d;
187 }
188
189 inline Point3D::Point3D(const Point3D& p)
190 {
191     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
192 }
193
194 // ASSIGNMENT OPERATORS
195
196 inline Point3D& Point3D::operator = (const Point3D& p)
197 {
198     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
199 }
200
201 inline Point3D& Point3D::operator += ( const Point3D& p )
202 {
203     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
204 }
205
206 inline Point3D& Point3D::operator -= ( const Point3D& p )
207 {
208     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
209 }
210
211 inline Point3D& Point3D::operator *= ( const double d )
212 {
213     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
214 }
215
216 inline Point3D& Point3D::operator /= ( const double d )
217 {
218     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
219     return *this;
220 }
221
222 inline void Point3D::setx(const double x) {
223     n[PX] = x;
224 }
225
226 inline void Point3D::sety(const double y) {
227     n[PY] = y;
228 }
229
230 inline void Point3D::setz(const double z) {
231     n[PZ] = z;
232 }
233
234 // QUERIES
235
236 inline double& Point3D::operator [] ( int i)
237 {
238     assert(! (i < PX || i > PZ));
239     return n[i];
240 }
241
242 inline double Point3D::operator [] ( int i) const {
243     assert(! (i < PX || i > PZ));
244     return n[i];
245 }
246
247
248 inline double Point3D::x() const { return n[PX]; }
249
250 inline double Point3D::y() const { return n[PY]; }
251
252 inline double Point3D::z() const { return n[PZ]; }
253
254 inline double Point3D::lon() const { return n[PX]; }
255
256 inline double Point3D::lat() const { return n[PY]; }
257
258 inline double Point3D::radius() const { return n[PZ]; }
259
260 inline double Point3D::elev() const { return n[PZ]; }
261
262
263 // FRIENDS
264
265 inline Point3D operator - (const Point3D& a)
266 {
267     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
268 }
269
270 inline Point3D operator + (const Point3D& a, const Point3D& b)
271 {
272     return Point3D(a) += b;
273 }
274
275 inline Point3D operator - (const Point3D& a, const Point3D& b)
276 {
277     return Point3D(a) -= b;
278 }
279
280 inline Point3D operator * (const Point3D& a, const double d)
281 {
282     return Point3D(a) *= d;
283 }
284
285 inline Point3D operator * (const double d, const Point3D& a)
286 {
287     return a*d;
288 }
289
290 inline Point3D operator / (const Point3D& a, const double d)
291 {
292     return Point3D(a) *= (1.0 / d );
293 }
294
295 inline bool operator == (const Point3D& a, const Point3D& b)
296 {
297     return
298         (a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
299         (a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
300         (a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
301 }
302
303 inline bool operator != (const Point3D& a, const Point3D& b)
304 {
305     return !(a == b);
306 }
307
308 // Special functions
309
310 inline double
311 Point3D::distance3D(const Point3D& a ) const
312 {
313     double x, y, z;
314
315     x = n[PX] - a.n[PX];
316     y = n[PY] - a.n[PY];
317     z = n[PZ] - a.n[PZ];
318
319     return sqrt(x*x + y*y + z*z);
320 }
321
322
323 inline double
324 Point3D::distance3Dsquared(const Point3D& a ) const
325 {
326     double x, y, z;
327
328     x = n[PX] - a.n[PX];
329     y = n[PY] - a.n[PY];
330     z = n[PZ] - a.n[PZ];
331
332     return(x*x + y*y + z*z);
333 }
334
335
336 #endif // _POINT3D_HXX
337
338
339 // $Log$
340 // Revision 1.10  1999/03/02 01:01:52  curt
341 // Tweaks for compiling with native SGI compilers.
342 //
343 // Revision 1.9  1999/02/01 21:08:28  curt
344 // Optimizations from Norman Vine.
345 //
346 // Revision 1.8  1999/01/27 04:46:18  curt
347 // Portability tweaks by Bernie Bright.
348 //
349 // Revision 1.7  1999/01/19 20:56:58  curt
350 // MacOS portability changes contributed by "Robert Puyol" <puyol@abvent.fr>
351 //
352 // Revision 1.6  1998/11/23 21:46:37  curt
353 // Borland portability tweaks.
354 //
355 // Revision 1.5  1998/11/20 01:00:38  curt
356 // Patch in fgGeoc2Geod() to avoid a floating explosion.
357 // point3d.hxx include math.h for FreeBSD
358 //
359 // Revision 1.4  1998/11/11 00:18:38  curt
360 // Check for domain error in fgGeoctoGeod()
361 //
362 // Revision 1.3  1998/10/20 18:21:49  curt
363 // Tweaks from Bernie Bright.
364 //
365 // Revision 1.2  1998/10/18 01:17:12  curt
366 // Point3D tweaks.
367 //
368 // Revision 1.1  1998/10/16 00:50:29  curt
369 // Added point3d.hxx to replace cheezy fgPoint3d struct.
370 //
371 //