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