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