]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
Ignore generated binaries.
[simgear.git] / simgear / math / point3d.hxx
1 /**
2  * \file point3d.hxx
3  * A 3d point class (depricated).  This class is depricated and we are
4  * in the process of removing all usage of it in favor of plib's "sg"
5  * library of point, vector, and math routines.  Plib's sg lib is less
6  * object oriented, but integrates more seamlessly with opengl.
7  *
8  * Adapted from algebra3 by Jean-Francois Doue, started October 1998.
9  */
10
11 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Library General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU Library General Public
24 // License along with this library; if not, write to the
25 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 // Boston, MA  02111-1307, USA.
27 //
28 // $Id$
29
30
31 #ifndef _POINT3D_HXX
32 #define _POINT3D_HXX
33
34
35 #ifndef __cplusplus                                                          
36 # error This library requires C++
37 #endif                                   
38
39 #include <simgear/compiler.h>
40
41 #ifdef SG_MATH_EXCEPTION_CLASH
42 # define exception c_exception
43 #endif
44
45 #ifdef SG_HAVE_STD_INCLUDES
46 # include <iostream>
47 # include <cassert>
48 # include <cmath>
49 #else
50 # include <iostream.h>
51 # include <assert.h>
52 # include <math.h>
53 #endif
54
55 #include "SGMath.hxx"
56
57 // I don't understand ... <math.h> or <cmath> should be included
58 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
59 // can go ahead and add this -- CLO
60 #ifdef __MWERKS__
61 SG_USING_NAMESPACE(std);
62 #endif
63
64 SG_USING_STD(ostream);
65 SG_USING_STD(istream);
66
67
68 const double fgPoint3_Epsilon = 0.0000001;
69
70 enum {PX, PY, PZ};                  // axes
71
72 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
73 class Point3D;
74 istream& operator>> ( istream&, Point3D& );
75 ostream& operator<< ( ostream&, const Point3D& );
76 Point3D operator- (const Point3D& p);               // -p1
77 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
78
79
80 /**
81  * 3D Point class.
82  */
83
84 class Point3D {
85
86 protected:
87
88     double n[3];
89
90 public:
91
92     /** Default constructor */
93     Point3D();
94     Point3D(const double x, const double y, const double z);
95     explicit Point3D(const double d);
96     Point3D(const Point3D &p);
97
98     static Point3D fromSGGeod(const SGGeod& geod);
99     static Point3D fromSGGeoc(const SGGeoc& geoc);
100     static Point3D fromSGVec3(const SGVec3<double>& cart);
101
102     // Assignment operators
103
104     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
105     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
106     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
107     Point3D& operator *= ( const double d );     // multiplication by a constant
108     Point3D& operator /= ( const double d );     // division by a constant
109
110     void setx(const double x);
111     void sety(const double y);
112     void setz(const double z);
113     void setlon(const double x);
114     void setlat(const double y);
115     void setradius(const double z);
116     void setelev(const double z);
117
118     // Queries 
119
120     double& operator [] ( int i);                // indexing
121     double operator[] (int i) const;             // read-only indexing
122
123     inline const double *get_n() const { return n; };
124     double x() const;      // cartesian x
125     double y() const;      // cartesian y
126     double z() const;      // cartesian z
127
128     double lon() const;    // polar longitude
129     double lat() const;    // polar latitude
130     double radius() const; // polar radius
131     double elev() const;   // geodetic elevation (if specifying a surface point)
132
133     SGGeod toSGGeod(void) const;
134     SGGeoc toSGGeoc(void) const;
135
136     // friends
137     friend Point3D operator - (const Point3D& p);                   // -p1
138     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
139     friend istream& operator>> ( istream&, Point3D& );
140     friend ostream& operator<< ( ostream&, const Point3D& );
141
142     // Special functions
143     double distance3D(const Point3D& a) const;        // distance between
144     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
145 };
146
147
148 // input from stream
149 inline istream&
150 operator >> ( istream& in, Point3D& p)
151 {
152     char c;
153
154     in >> p.n[PX];
155
156     // read past optional comma
157     while ( in.get(c) ) {
158         if ( (c != ' ') && (c != ',') ) {
159             // push back on the stream
160             in.putback(c);
161             break;
162         }
163     }
164         
165     in >> p.n[PY];
166
167     // read past optional comma
168     while ( in.get(c) ) {
169         if ( (c != ' ') && (c != ',') ) {
170             // push back on the stream
171             in.putback(c);
172             break;
173         }
174     }
175         
176     in >> p.n[PZ];
177
178     return in;
179 }
180
181 inline ostream&
182 operator<< ( ostream& out, const Point3D& p )
183 {
184     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
185 }
186
187 ///////////////////////////
188 //
189 // Point3D Member functions
190 //
191 ///////////////////////////
192
193 // CONSTRUCTORS
194
195 inline Point3D::Point3D()
196 {
197    n[PX] = n[PY] = 0.0;
198    n[PZ] = -9999.0;
199 }
200
201 inline Point3D::Point3D(const double x, const double y, const double z)
202 {
203     n[PX] = x; n[PY] = y; n[PZ] = z;
204 }
205
206 inline Point3D::Point3D(const double d)
207 {
208     n[PX] = n[PY] = n[PZ] = d;
209 }
210
211 inline Point3D::Point3D(const Point3D& p)
212 {
213     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
214 }
215
216 inline Point3D Point3D::fromSGGeod(const SGGeod& geod)
217 {
218   Point3D pt;
219   pt.setlon(geod.getLongitudeRad());
220   pt.setlat(geod.getLatitudeRad());
221   pt.setelev(geod.getElevationM());
222   return pt;
223 }
224
225 inline Point3D Point3D::fromSGGeoc(const SGGeoc& geoc)
226 {
227   Point3D pt;
228   pt.setlon(geoc.getLongitudeRad());
229   pt.setlat(geoc.getLatitudeRad());
230   pt.setradius(geoc.getRadiusM());
231   return pt;
232 }
233
234 inline Point3D Point3D::fromSGVec3(const SGVec3<double>& cart)
235 {
236   Point3D pt;
237   pt.setx(cart.x());
238   pt.sety(cart.y());
239   pt.setz(cart.z());
240   return pt;
241 }
242
243 // ASSIGNMENT OPERATORS
244
245 inline Point3D& Point3D::operator = (const Point3D& p)
246 {
247     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
248 }
249
250 inline Point3D& Point3D::operator += ( const Point3D& p )
251 {
252     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
253 }
254
255 inline Point3D& Point3D::operator -= ( const Point3D& p )
256 {
257     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
258 }
259
260 inline Point3D& Point3D::operator *= ( const double d )
261 {
262     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
263 }
264
265 inline Point3D& Point3D::operator /= ( const double d )
266 {
267     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
268     return *this;
269 }
270
271 inline void Point3D::setx(const double x) {
272     n[PX] = x;
273 }
274
275 inline void Point3D::sety(const double y) {
276     n[PY] = y;
277 }
278
279 inline void Point3D::setz(const double z) {
280     n[PZ] = z;
281 }
282
283 inline void Point3D::setlon(const double x) {
284     n[PX] = x;
285 }
286
287 inline void Point3D::setlat(const double y) {
288     n[PY] = y;
289 }
290
291 inline void Point3D::setradius(const double z) {
292     n[PZ] = z;
293 }
294
295 inline void Point3D::setelev(const double z) {
296     n[PZ] = z;
297 }
298
299 // QUERIES
300
301 inline double& Point3D::operator [] ( int i)
302 {
303     assert(! (i < PX || i > PZ));
304     return n[i];
305 }
306
307 inline double Point3D::operator [] ( int i) const {
308     assert(! (i < PX || i > PZ));
309     return n[i];
310 }
311
312
313 inline double Point3D::x() const { return n[PX]; }
314
315 inline double Point3D::y() const { return n[PY]; }
316
317 inline double Point3D::z() const { return n[PZ]; }
318
319 inline double Point3D::lon() const { return n[PX]; }
320
321 inline double Point3D::lat() const { return n[PY]; }
322
323 inline double Point3D::radius() const { return n[PZ]; }
324
325 inline double Point3D::elev() const { return n[PZ]; }
326
327 inline SGGeod Point3D::toSGGeod(void) const
328 {
329   SGGeod geod;
330   geod.setLongitudeRad(lon());
331   geod.setLatitudeRad(lat());
332   geod.setElevationM(elev());
333   return geod;
334 }
335
336 inline SGGeoc Point3D::toSGGeoc(void) const
337 {
338   SGGeoc geoc;
339   geoc.setLongitudeRad(lon());
340   geoc.setLatitudeRad(lat());
341   geoc.setRadiusM(radius());
342   return geoc;
343 }
344
345 // FRIENDS
346
347 inline Point3D operator - (const Point3D& a)
348 {
349     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
350 }
351
352 inline Point3D operator + (const Point3D& a, const Point3D& b)
353 {
354     return Point3D(a) += b;
355 }
356
357 inline Point3D operator - (const Point3D& a, const Point3D& b)
358 {
359     return Point3D(a) -= b;
360 }
361
362 inline Point3D operator * (const Point3D& a, const double d)
363 {
364     return Point3D(a) *= d;
365 }
366
367 inline Point3D operator * (const double d, const Point3D& a)
368 {
369     return a*d;
370 }
371
372 inline Point3D operator / (const Point3D& a, const double d)
373 {
374     return Point3D(a) *= (1.0 / d );
375 }
376
377 inline bool operator == (const Point3D& a, const Point3D& b)
378 {
379     return
380         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
381         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
382         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
383 }
384
385 inline bool operator != (const Point3D& a, const Point3D& b)
386 {
387     return !(a == b);
388 }
389
390 // Special functions
391
392 inline double
393 Point3D::distance3D(const Point3D& a ) const
394 {
395     double x, y, z;
396
397     x = n[PX] - a.n[PX];
398     y = n[PY] - a.n[PY];
399     z = n[PZ] - a.n[PZ];
400
401     return sqrt(x*x + y*y + z*z);
402 }
403
404
405 inline double
406 Point3D::distance3Dsquared(const Point3D& a ) const
407 {
408     double x, y, z;
409
410     x = n[PX] - a.n[PX];
411     y = n[PY] - a.n[PY];
412     z = n[PZ] - a.n[PZ];
413
414     return(x*x + y*y + z*z);
415 }
416
417
418 #endif // _POINT3D_HXX
419
420