]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
easyxml.cxx: add missing endXML visitor call
[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 General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26 //
27 // $Id$
28
29
30 #ifndef _POINT3D_HXX
31 #define _POINT3D_HXX
32
33
34 #ifndef __cplusplus
35 # error This library requires C++
36 #endif
37
38 #include <simgear/compiler.h>
39
40 #ifdef SG_MATH_EXCEPTION_CLASH
41 # define exception c_exception
42 #endif
43
44 #ifdef SG_HAVE_STD_INCLUDES
45 # include <iostream>
46 # include <cassert>
47 # include <cmath>
48 #else
49 # include <iostream.h>
50 # include <assert.h>
51 # include <math.h>
52 #endif
53
54 #include "SGMath.hxx"
55
56 // I don't understand ... <math.h> or <cmath> should be included
57 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
58 // can go ahead and add this -- CLO
59 #ifdef __MWERKS__
60 SG_USING_NAMESPACE(std);
61 #endif
62
63 SG_USING_STD(ostream);
64 SG_USING_STD(istream);
65
66
67 const double fgPoint3_Epsilon = 0.0000001;
68
69 enum {PX, PY, PZ};                  // axes
70
71 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
72 class Point3D;
73 istream& operator>> ( istream&, Point3D& );
74 ostream& operator<< ( ostream&, const Point3D& );
75 Point3D operator- (const Point3D& p);               // -p1
76 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
77
78
79 /**
80  * 3D Point class.
81  */
82
83 class Point3D {
84
85 protected:
86
87     double n[3];
88
89 public:
90
91     /** Default constructor */
92     Point3D();
93     Point3D(const double x, const double y, const double z);
94     explicit Point3D(const double d);
95     Point3D(const Point3D &p);
96
97     static Point3D fromSGGeod(const SGGeod& geod);
98     static Point3D fromSGGeoc(const SGGeoc& geoc);
99     static Point3D fromSGVec3(const SGVec3<double>& cart);
100     static Point3D fromSGVec3(const SGVec3<float>& cart);
101     static Point3D fromSGVec2(const SGVec2<double>& cart);
102
103     // Assignment operators
104
105     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
106     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
107     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
108     Point3D& operator *= ( const double d );     // multiplication by a constant
109     Point3D& operator /= ( const double d );     // division by a constant
110
111     void setx(const double x);
112     void sety(const double y);
113     void setz(const double z);
114     void setlon(const double x);
115     void setlat(const double y);
116     void setradius(const double z);
117     void setelev(const double z);
118
119     // Queries 
120
121     double& operator [] ( int i);                // indexing
122     double operator[] (int i) const;             // read-only indexing
123
124     inline const double *get_n() const { return n; };
125     double x() const;      // cartesian x
126     double y() const;      // cartesian y
127     double z() const;      // cartesian z
128
129     double lon() const;    // polar longitude
130     double lat() const;    // polar latitude
131     double radius() const; // polar radius
132     double elev() const;   // geodetic elevation (if specifying a surface point)
133
134     SGGeod toSGGeod(void) const;
135     SGGeoc toSGGeoc(void) const;
136
137     SGVec3d toSGVec3d(void) const;
138     SGVec3f toSGVec3f(void) const;
139     SGVec2f toSGVec2f(void) const;
140
141     // friends
142     friend Point3D operator - (const Point3D& p);                   // -p1
143     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
144     friend istream& operator>> ( istream&, Point3D& );
145     friend ostream& operator<< ( ostream&, const Point3D& );
146
147     // Special functions
148     double distance3D(const Point3D& a) const;        // distance between
149     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
150 };
151
152
153 // input from stream
154 inline istream&
155 operator >> ( istream& in, Point3D& p)
156 {
157     char c;
158
159     in >> p.n[PX];
160
161     // read past optional comma
162     while ( in.get(c) ) {
163         if ( (c != ' ') && (c != ',') ) {
164             // push back on the stream
165             in.putback(c);
166             break;
167         }
168     }
169         
170     in >> p.n[PY];
171
172     // read past optional comma
173     while ( in.get(c) ) {
174         if ( (c != ' ') && (c != ',') ) {
175             // push back on the stream
176             in.putback(c);
177             break;
178         }
179     }
180         
181     in >> p.n[PZ];
182
183     return in;
184 }
185
186 inline ostream&
187 operator<< ( ostream& out, const Point3D& p )
188 {
189     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
190 }
191
192 ///////////////////////////
193 //
194 // Point3D Member functions
195 //
196 ///////////////////////////
197
198 // CONSTRUCTORS
199
200 inline Point3D::Point3D()
201 {
202    n[PX] = n[PY] = 0.0;
203    n[PZ] = -9999.0;
204 }
205
206 inline Point3D::Point3D(const double x, const double y, const double z)
207 {
208     n[PX] = x; n[PY] = y; n[PZ] = z;
209 }
210
211 inline Point3D::Point3D(const double d)
212 {
213     n[PX] = n[PY] = n[PZ] = d;
214 }
215
216 inline Point3D::Point3D(const Point3D& p)
217 {
218     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
219 }
220
221 inline Point3D Point3D::fromSGGeod(const SGGeod& geod)
222 {
223   Point3D pt;
224   pt.setlon(geod.getLongitudeRad());
225   pt.setlat(geod.getLatitudeRad());
226   pt.setelev(geod.getElevationM());
227   return pt;
228 }
229
230 inline Point3D Point3D::fromSGGeoc(const SGGeoc& geoc)
231 {
232   Point3D pt;
233   pt.setlon(geoc.getLongitudeRad());
234   pt.setlat(geoc.getLatitudeRad());
235   pt.setradius(geoc.getRadiusM());
236   return pt;
237 }
238
239 inline Point3D Point3D::fromSGVec3(const SGVec3<double>& cart)
240 {
241   Point3D pt;
242   pt.setx(cart.x());
243   pt.sety(cart.y());
244   pt.setz(cart.z());
245   return pt;
246 }
247
248 inline Point3D Point3D::fromSGVec3(const SGVec3<float>& cart)
249 {
250   Point3D pt;
251   pt.setx(cart.x());
252   pt.sety(cart.y());
253   pt.setz(cart.z());
254   return pt;
255 }
256
257 inline Point3D Point3D::fromSGVec2(const SGVec2<double>& cart)
258 {
259   Point3D pt;
260   pt.setx(cart.x());
261   pt.sety(cart.y());
262   pt.setz(0);
263   return pt;
264 }
265
266 // ASSIGNMENT OPERATORS
267
268 inline Point3D& Point3D::operator = (const Point3D& p)
269 {
270     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
271 }
272
273 inline Point3D& Point3D::operator += ( const Point3D& p )
274 {
275     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
276 }
277
278 inline Point3D& Point3D::operator -= ( const Point3D& p )
279 {
280     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
281 }
282
283 inline Point3D& Point3D::operator *= ( const double d )
284 {
285     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
286 }
287
288 inline Point3D& Point3D::operator /= ( const double d )
289 {
290     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
291     return *this;
292 }
293
294 inline void Point3D::setx(const double x) {
295     n[PX] = x;
296 }
297
298 inline void Point3D::sety(const double y) {
299     n[PY] = y;
300 }
301
302 inline void Point3D::setz(const double z) {
303     n[PZ] = z;
304 }
305
306 inline void Point3D::setlon(const double x) {
307     n[PX] = x;
308 }
309
310 inline void Point3D::setlat(const double y) {
311     n[PY] = y;
312 }
313
314 inline void Point3D::setradius(const double z) {
315     n[PZ] = z;
316 }
317
318 inline void Point3D::setelev(const double z) {
319     n[PZ] = z;
320 }
321
322 // QUERIES
323
324 inline double& Point3D::operator [] ( int i)
325 {
326     assert(! (i < PX || i > PZ));
327     return n[i];
328 }
329
330 inline double Point3D::operator [] ( int i) const {
331     assert(! (i < PX || i > PZ));
332     return n[i];
333 }
334
335
336 inline double Point3D::x() const { return n[PX]; }
337
338 inline double Point3D::y() const { return n[PY]; }
339
340 inline double Point3D::z() const { return n[PZ]; }
341
342 inline double Point3D::lon() const { return n[PX]; }
343
344 inline double Point3D::lat() const { return n[PY]; }
345
346 inline double Point3D::radius() const { return n[PZ]; }
347
348 inline double Point3D::elev() const { return n[PZ]; }
349
350 inline SGGeod Point3D::toSGGeod(void) const
351 {
352   SGGeod geod;
353   geod.setLongitudeRad(lon());
354   geod.setLatitudeRad(lat());
355   geod.setElevationM(elev());
356   return geod;
357 }
358
359 inline SGGeoc Point3D::toSGGeoc(void) const
360 {
361   SGGeoc geoc;
362   geoc.setLongitudeRad(lon());
363   geoc.setLatitudeRad(lat());
364   geoc.setRadiusM(radius());
365   return geoc;
366 }
367
368 inline SGVec3d Point3D::toSGVec3d(void) const
369 {
370   return SGVec3d(x(), y(), z());
371 }
372
373 inline SGVec3f Point3D::toSGVec3f(void) const
374 {
375   return SGVec3f(x(), y(), z());
376 }
377
378 inline SGVec2f Point3D::toSGVec2f(void) const
379 {
380   return SGVec2f(x(), y());
381 }
382
383 // FRIENDS
384
385 inline Point3D operator - (const Point3D& a)
386 {
387     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
388 }
389
390 inline Point3D operator + (const Point3D& a, const Point3D& b)
391 {
392     return Point3D(a) += b;
393 }
394
395 inline Point3D operator - (const Point3D& a, const Point3D& b)
396 {
397     return Point3D(a) -= b;
398 }
399
400 inline Point3D operator * (const Point3D& a, const double d)
401 {
402     return Point3D(a) *= d;
403 }
404
405 inline Point3D operator * (const double d, const Point3D& a)
406 {
407     return a*d;
408 }
409
410 inline Point3D operator / (const Point3D& a, const double d)
411 {
412     return Point3D(a) *= (1.0 / d );
413 }
414
415 inline bool operator == (const Point3D& a, const Point3D& b)
416 {
417     return
418         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
419         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
420         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
421 }
422
423 inline bool operator != (const Point3D& a, const Point3D& b)
424 {
425     return !(a == b);
426 }
427
428 // Special functions
429
430 inline double
431 Point3D::distance3D(const Point3D& a ) const
432 {
433     double x, y, z;
434
435     x = n[PX] - a.n[PX];
436     y = n[PY] - a.n[PY];
437     z = n[PZ] - a.n[PZ];
438
439     return sqrt(x*x + y*y + z*z);
440 }
441
442
443 inline double
444 Point3D::distance3Dsquared(const Point3D& a ) const
445 {
446     double x, y, z;
447
448     x = n[PX] - a.n[PX];
449     y = n[PY] - a.n[PY];
450     z = n[PZ] - a.n[PZ];
451
452     return(x*x + y*y + z*z);
453 }
454
455
456 #endif // _POINT3D_HXX
457
458