]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
0cc469b90feb983f8109f2c686759351b17945d3
[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  - curt@me.umn.edu
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 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
65 SG_USING_STD(ostream);
66 SG_USING_STD(istream);
67 #endif
68
69
70 const double fgPoint3_Epsilon = 0.0000001;
71
72 enum {PX, PY, PZ};                  // axes
73
74 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
75 class Point3D;
76 istream& operator>> ( istream&, Point3D& );
77 ostream& operator<< ( ostream&, const Point3D& );
78 Point3D operator- (const Point3D& p);               // -p1
79 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
80
81
82 /**
83  * 3D Point class.
84  */
85
86 class Point3D {
87
88 protected:
89
90     double n[3];
91
92 public:
93
94     /** Default constructor */
95     Point3D();
96     Point3D(const double x, const double y, const double z);
97     explicit Point3D(const double d);
98     Point3D(const Point3D &p);
99
100     // Assignment operators
101
102     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
103     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
104     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
105     Point3D& operator *= ( const double d );     // multiplication by a constant
106     Point3D& operator /= ( const double d );     // division by a constant
107
108     void setx(const double x);
109     void sety(const double y);
110     void setz(const double z);
111
112     // Queries 
113
114     double& operator [] ( int i);                // indexing
115     double operator[] (int i) const;             // read-only indexing
116
117     inline const double *get_n() const { return n; };
118     double x() const;      // cartesian x
119     double y() const;      // cartesian y
120     double z() const;      // cartesian z
121
122     double lon() const;    // polar longitude
123     double lat() const;    // polar latitude
124     double radius() const; // polar radius
125     double elev() const;   // geodetic elevation (if specifying a surface point)
126
127     // friends
128     friend Point3D operator - (const Point3D& p);                   // -p1
129     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
130     friend istream& operator>> ( istream&, Point3D& );
131     friend ostream& operator<< ( ostream&, const Point3D& );
132
133     // Special functions
134     double distance3D(const Point3D& a) const;        // distance between
135     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
136 };
137
138
139 // input from stream
140 inline istream&
141 operator >> ( istream& in, Point3D& p)
142 {
143     char c;
144
145     in >> p.n[PX];
146
147     // read past optional comma
148     while ( in.get(c) ) {
149         if ( (c != ' ') && (c != ',') ) {
150             // push back on the stream
151             in.putback(c);
152             break;
153         }
154     }
155         
156     in >> p.n[PY];
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[PZ];
168
169     return in;
170 }
171
172 inline ostream&
173 operator<< ( ostream& out, const Point3D& p )
174 {
175     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
176 }
177
178 ///////////////////////////
179 //
180 // Point3D Member functions
181 //
182 ///////////////////////////
183
184 // CONSTRUCTORS
185
186 inline Point3D::Point3D() {}
187
188 inline Point3D::Point3D(const double x, const double y, const double z)
189 {
190     n[PX] = x; n[PY] = y; n[PZ] = z;
191 }
192
193 inline Point3D::Point3D(const double d)
194 {
195     n[PX] = n[PY] = n[PZ] = d;
196 }
197
198 inline Point3D::Point3D(const Point3D& p)
199 {
200     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
201 }
202
203 // ASSIGNMENT OPERATORS
204
205 inline Point3D& Point3D::operator = (const Point3D& p)
206 {
207     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
208 }
209
210 inline Point3D& Point3D::operator += ( const Point3D& p )
211 {
212     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
213 }
214
215 inline Point3D& Point3D::operator -= ( const Point3D& p )
216 {
217     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
218 }
219
220 inline Point3D& Point3D::operator *= ( const double d )
221 {
222     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
223 }
224
225 inline Point3D& Point3D::operator /= ( const double d )
226 {
227     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
228     return *this;
229 }
230
231 inline void Point3D::setx(const double x) {
232     n[PX] = x;
233 }
234
235 inline void Point3D::sety(const double y) {
236     n[PY] = y;
237 }
238
239 inline void Point3D::setz(const double z) {
240     n[PZ] = z;
241 }
242
243 // QUERIES
244
245 inline double& Point3D::operator [] ( int i)
246 {
247     assert(! (i < PX || i > PZ));
248     return n[i];
249 }
250
251 inline double Point3D::operator [] ( int i) const {
252     assert(! (i < PX || i > PZ));
253     return n[i];
254 }
255
256
257 inline double Point3D::x() const { return n[PX]; }
258
259 inline double Point3D::y() const { return n[PY]; }
260
261 inline double Point3D::z() const { return n[PZ]; }
262
263 inline double Point3D::lon() const { return n[PX]; }
264
265 inline double Point3D::lat() const { return n[PY]; }
266
267 inline double Point3D::radius() const { return n[PZ]; }
268
269 inline double Point3D::elev() const { return n[PZ]; }
270
271
272 // FRIENDS
273
274 inline Point3D operator - (const Point3D& a)
275 {
276     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
277 }
278
279 inline Point3D operator + (const Point3D& a, const Point3D& b)
280 {
281     return Point3D(a) += b;
282 }
283
284 inline Point3D operator - (const Point3D& a, const Point3D& b)
285 {
286     return Point3D(a) -= b;
287 }
288
289 inline Point3D operator * (const Point3D& a, const double d)
290 {
291     return Point3D(a) *= d;
292 }
293
294 inline Point3D operator * (const double d, const Point3D& a)
295 {
296     return a*d;
297 }
298
299 inline Point3D operator / (const Point3D& a, const double d)
300 {
301     return Point3D(a) *= (1.0 / d );
302 }
303
304 inline bool operator == (const Point3D& a, const Point3D& b)
305 {
306     return
307         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
308         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
309         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
310 }
311
312 inline bool operator != (const Point3D& a, const Point3D& b)
313 {
314     return !(a == b);
315 }
316
317 // Special functions
318
319 inline double
320 Point3D::distance3D(const Point3D& a ) const
321 {
322     double x, y, z;
323
324     x = n[PX] - a.n[PX];
325     y = n[PY] - a.n[PY];
326     z = n[PZ] - a.n[PZ];
327
328     return sqrt(x*x + y*y + z*z);
329 }
330
331
332 inline double
333 Point3D::distance3Dsquared(const Point3D& a ) const
334 {
335     double x, y, z;
336
337     x = n[PX] - a.n[PX];
338     y = n[PY] - a.n[PY];
339     z = n[PZ] - a.n[PZ];
340
341     return(x*x + y*y + z*z);
342 }
343
344
345 #endif // _POINT3D_HXX
346
347