]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
Modified Files:
[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
101     // Assignment operators
102
103     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
104     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
105     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
106     Point3D& operator *= ( const double d );     // multiplication by a constant
107     Point3D& operator /= ( const double d );     // division by a constant
108
109     void setx(const double x);
110     void sety(const double y);
111     void setz(const double z);
112     void setlon(const double x);
113     void setlat(const double y);
114     void setradius(const double z);
115     void setelev(const double z);
116
117     // Queries 
118
119     double& operator [] ( int i);                // indexing
120     double operator[] (int i) const;             // read-only indexing
121
122     inline const double *get_n() const { return n; };
123     double x() const;      // cartesian x
124     double y() const;      // cartesian y
125     double z() const;      // cartesian z
126
127     double lon() const;    // polar longitude
128     double lat() const;    // polar latitude
129     double radius() const; // polar radius
130     double elev() const;   // geodetic elevation (if specifying a surface point)
131
132     SGGeod toSGGeod(void) const;
133     SGGeoc toSGGeoc(void) const;
134
135     SGVec3d toSGVec3d(void) const;
136     SGVec3f toSGVec3f(void) const;
137
138     // friends
139     friend Point3D operator - (const Point3D& p);                   // -p1
140     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
141     friend istream& operator>> ( istream&, Point3D& );
142     friend ostream& operator<< ( ostream&, const Point3D& );
143
144     // Special functions
145     double distance3D(const Point3D& a) const;        // distance between
146     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
147 };
148
149
150 // input from stream
151 inline istream&
152 operator >> ( istream& in, Point3D& p)
153 {
154     char c;
155
156     in >> p.n[PX];
157
158     // read past optional comma
159     while ( in.get(c) ) {
160         if ( (c != ' ') && (c != ',') ) {
161             // push back on the stream
162             in.putback(c);
163             break;
164         }
165     }
166         
167     in >> p.n[PY];
168
169     // read past optional comma
170     while ( in.get(c) ) {
171         if ( (c != ' ') && (c != ',') ) {
172             // push back on the stream
173             in.putback(c);
174             break;
175         }
176     }
177         
178     in >> p.n[PZ];
179
180     return in;
181 }
182
183 inline ostream&
184 operator<< ( ostream& out, const Point3D& p )
185 {
186     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
187 }
188
189 ///////////////////////////
190 //
191 // Point3D Member functions
192 //
193 ///////////////////////////
194
195 // CONSTRUCTORS
196
197 inline Point3D::Point3D()
198 {
199    n[PX] = n[PY] = 0.0;
200    n[PZ] = -9999.0;
201 }
202
203 inline Point3D::Point3D(const double x, const double y, const double z)
204 {
205     n[PX] = x; n[PY] = y; n[PZ] = z;
206 }
207
208 inline Point3D::Point3D(const double d)
209 {
210     n[PX] = n[PY] = n[PZ] = d;
211 }
212
213 inline Point3D::Point3D(const Point3D& p)
214 {
215     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
216 }
217
218 inline Point3D Point3D::fromSGGeod(const SGGeod& geod)
219 {
220   Point3D pt;
221   pt.setlon(geod.getLongitudeRad());
222   pt.setlat(geod.getLatitudeRad());
223   pt.setelev(geod.getElevationM());
224   return pt;
225 }
226
227 inline Point3D Point3D::fromSGGeoc(const SGGeoc& geoc)
228 {
229   Point3D pt;
230   pt.setlon(geoc.getLongitudeRad());
231   pt.setlat(geoc.getLatitudeRad());
232   pt.setradius(geoc.getRadiusM());
233   return pt;
234 }
235
236 inline Point3D Point3D::fromSGVec3(const SGVec3<double>& cart)
237 {
238   Point3D pt;
239   pt.setx(cart.x());
240   pt.sety(cart.y());
241   pt.setz(cart.z());
242   return pt;
243 }
244
245 // ASSIGNMENT OPERATORS
246
247 inline Point3D& Point3D::operator = (const Point3D& p)
248 {
249     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
250 }
251
252 inline Point3D& Point3D::operator += ( const Point3D& p )
253 {
254     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
255 }
256
257 inline Point3D& Point3D::operator -= ( const Point3D& p )
258 {
259     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
260 }
261
262 inline Point3D& Point3D::operator *= ( const double d )
263 {
264     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
265 }
266
267 inline Point3D& Point3D::operator /= ( const double d )
268 {
269     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
270     return *this;
271 }
272
273 inline void Point3D::setx(const double x) {
274     n[PX] = x;
275 }
276
277 inline void Point3D::sety(const double y) {
278     n[PY] = y;
279 }
280
281 inline void Point3D::setz(const double z) {
282     n[PZ] = z;
283 }
284
285 inline void Point3D::setlon(const double x) {
286     n[PX] = x;
287 }
288
289 inline void Point3D::setlat(const double y) {
290     n[PY] = y;
291 }
292
293 inline void Point3D::setradius(const double z) {
294     n[PZ] = z;
295 }
296
297 inline void Point3D::setelev(const double z) {
298     n[PZ] = z;
299 }
300
301 // QUERIES
302
303 inline double& Point3D::operator [] ( int i)
304 {
305     assert(! (i < PX || i > PZ));
306     return n[i];
307 }
308
309 inline double Point3D::operator [] ( int i) const {
310     assert(! (i < PX || i > PZ));
311     return n[i];
312 }
313
314
315 inline double Point3D::x() const { return n[PX]; }
316
317 inline double Point3D::y() const { return n[PY]; }
318
319 inline double Point3D::z() const { return n[PZ]; }
320
321 inline double Point3D::lon() const { return n[PX]; }
322
323 inline double Point3D::lat() const { return n[PY]; }
324
325 inline double Point3D::radius() const { return n[PZ]; }
326
327 inline double Point3D::elev() const { return n[PZ]; }
328
329 inline SGGeod Point3D::toSGGeod(void) const
330 {
331   SGGeod geod;
332   geod.setLongitudeRad(lon());
333   geod.setLatitudeRad(lat());
334   geod.setElevationM(elev());
335   return geod;
336 }
337
338 inline SGGeoc Point3D::toSGGeoc(void) const
339 {
340   SGGeoc geoc;
341   geoc.setLongitudeRad(lon());
342   geoc.setLatitudeRad(lat());
343   geoc.setRadiusM(radius());
344   return geoc;
345 }
346
347 inline SGVec3d Point3D::toSGVec3d(void) const
348 {
349   return SGVec3d(x(), y(), z());
350 }
351
352 inline SGVec3f Point3D::toSGVec3f(void) const
353 {
354   return SGVec3f(x(), y(), z());
355 }
356
357 // FRIENDS
358
359 inline Point3D operator - (const Point3D& a)
360 {
361     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
362 }
363
364 inline Point3D operator + (const Point3D& a, const Point3D& b)
365 {
366     return Point3D(a) += b;
367 }
368
369 inline Point3D operator - (const Point3D& a, const Point3D& b)
370 {
371     return Point3D(a) -= b;
372 }
373
374 inline Point3D operator * (const Point3D& a, const double d)
375 {
376     return Point3D(a) *= d;
377 }
378
379 inline Point3D operator * (const double d, const Point3D& a)
380 {
381     return a*d;
382 }
383
384 inline Point3D operator / (const Point3D& a, const double d)
385 {
386     return Point3D(a) *= (1.0 / d );
387 }
388
389 inline bool operator == (const Point3D& a, const Point3D& b)
390 {
391     return
392         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
393         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
394         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
395 }
396
397 inline bool operator != (const Point3D& a, const Point3D& b)
398 {
399     return !(a == b);
400 }
401
402 // Special functions
403
404 inline double
405 Point3D::distance3D(const Point3D& a ) const
406 {
407     double x, y, z;
408
409     x = n[PX] - a.n[PX];
410     y = n[PY] - a.n[PY];
411     z = n[PZ] - a.n[PZ];
412
413     return sqrt(x*x + y*y + z*z);
414 }
415
416
417 inline double
418 Point3D::distance3Dsquared(const Point3D& a ) const
419 {
420     double x, y, z;
421
422     x = n[PX] - a.n[PX];
423     y = n[PY] - a.n[PY];
424     z = n[PZ] - a.n[PZ];
425
426     return(x*x + y*y + z*z);
427 }
428
429
430 #endif // _POINT3D_HXX
431
432