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