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