]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
Patch from Melchior Franz:
[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     void setlon(const double x);
112     void setlat(const double y);
113     void setradius(const double z);
114     void setelev(const double z);
115
116     // Queries 
117
118     double& operator [] ( int i);                // indexing
119     double operator[] (int i) const;             // read-only indexing
120
121     inline const double *get_n() const { return n; };
122     double x() const;      // cartesian x
123     double y() const;      // cartesian y
124     double z() const;      // cartesian z
125
126     double lon() const;    // polar longitude
127     double lat() const;    // polar latitude
128     double radius() const; // polar radius
129     double elev() const;   // geodetic elevation (if specifying a surface point)
130
131     // friends
132     friend Point3D operator - (const Point3D& p);                   // -p1
133     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
134     friend istream& operator>> ( istream&, Point3D& );
135     friend ostream& operator<< ( ostream&, const Point3D& );
136
137     // Special functions
138     double distance3D(const Point3D& a) const;        // distance between
139     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
140 };
141
142
143 // input from stream
144 inline istream&
145 operator >> ( istream& in, Point3D& p)
146 {
147     char c;
148
149     in >> p.n[PX];
150
151     // read past optional comma
152     while ( in.get(c) ) {
153         if ( (c != ' ') && (c != ',') ) {
154             // push back on the stream
155             in.putback(c);
156             break;
157         }
158     }
159         
160     in >> p.n[PY];
161
162     // read past optional comma
163     while ( in.get(c) ) {
164         if ( (c != ' ') && (c != ',') ) {
165             // push back on the stream
166             in.putback(c);
167             break;
168         }
169     }
170         
171     in >> p.n[PZ];
172
173     return in;
174 }
175
176 inline ostream&
177 operator<< ( ostream& out, const Point3D& p )
178 {
179     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
180 }
181
182 ///////////////////////////
183 //
184 // Point3D Member functions
185 //
186 ///////////////////////////
187
188 // CONSTRUCTORS
189
190 inline Point3D::Point3D() {}
191
192 inline Point3D::Point3D(const double x, const double y, const double z)
193 {
194     n[PX] = x; n[PY] = y; n[PZ] = z;
195 }
196
197 inline Point3D::Point3D(const double d)
198 {
199     n[PX] = n[PY] = n[PZ] = d;
200 }
201
202 inline Point3D::Point3D(const Point3D& p)
203 {
204     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
205 }
206
207 // ASSIGNMENT OPERATORS
208
209 inline Point3D& Point3D::operator = (const Point3D& p)
210 {
211     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
212 }
213
214 inline Point3D& Point3D::operator += ( const Point3D& p )
215 {
216     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
217 }
218
219 inline Point3D& Point3D::operator -= ( const Point3D& p )
220 {
221     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
222 }
223
224 inline Point3D& Point3D::operator *= ( const double d )
225 {
226     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
227 }
228
229 inline Point3D& Point3D::operator /= ( const double d )
230 {
231     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
232     return *this;
233 }
234
235 inline void Point3D::setx(const double x) {
236     n[PX] = x;
237 }
238
239 inline void Point3D::sety(const double y) {
240     n[PY] = y;
241 }
242
243 inline void Point3D::setz(const double z) {
244     n[PZ] = z;
245 }
246
247 inline void Point3D::setlon(const double x) {
248     n[PX] = x;
249 }
250
251 inline void Point3D::setlat(const double y) {
252     n[PY] = y;
253 }
254
255 inline void Point3D::setradius(const double z) {
256     n[PZ] = z;
257 }
258
259 inline void Point3D::setelev(const double z) {
260     n[PZ] = z;
261 }
262
263 // QUERIES
264
265 inline double& Point3D::operator [] ( int i)
266 {
267     assert(! (i < PX || i > PZ));
268     return n[i];
269 }
270
271 inline double Point3D::operator [] ( int i) const {
272     assert(! (i < PX || i > PZ));
273     return n[i];
274 }
275
276
277 inline double Point3D::x() const { return n[PX]; }
278
279 inline double Point3D::y() const { return n[PY]; }
280
281 inline double Point3D::z() const { return n[PZ]; }
282
283 inline double Point3D::lon() const { return n[PX]; }
284
285 inline double Point3D::lat() const { return n[PY]; }
286
287 inline double Point3D::radius() const { return n[PZ]; }
288
289 inline double Point3D::elev() const { return n[PZ]; }
290
291
292 // FRIENDS
293
294 inline Point3D operator - (const Point3D& a)
295 {
296     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
297 }
298
299 inline Point3D operator + (const Point3D& a, const Point3D& b)
300 {
301     return Point3D(a) += b;
302 }
303
304 inline Point3D operator - (const Point3D& a, const Point3D& b)
305 {
306     return Point3D(a) -= b;
307 }
308
309 inline Point3D operator * (const Point3D& a, const double d)
310 {
311     return Point3D(a) *= d;
312 }
313
314 inline Point3D operator * (const double d, const Point3D& a)
315 {
316     return a*d;
317 }
318
319 inline Point3D operator / (const Point3D& a, const double d)
320 {
321     return Point3D(a) *= (1.0 / d );
322 }
323
324 inline bool operator == (const Point3D& a, const Point3D& b)
325 {
326     return
327         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
328         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
329         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
330 }
331
332 inline bool operator != (const Point3D& a, const Point3D& b)
333 {
334     return !(a == b);
335 }
336
337 // Special functions
338
339 inline double
340 Point3D::distance3D(const Point3D& a ) const
341 {
342     double x, y, z;
343
344     x = n[PX] - a.n[PX];
345     y = n[PY] - a.n[PY];
346     z = n[PZ] - a.n[PZ];
347
348     return sqrt(x*x + y*y + z*z);
349 }
350
351
352 inline double
353 Point3D::distance3Dsquared(const Point3D& a ) const
354 {
355     double x, y, z;
356
357     x = n[PX] - a.n[PX];
358     y = n[PY] - a.n[PY];
359     z = n[PZ] - a.n[PZ];
360
361     return(x*x + y*y + z*z);
362 }
363
364
365 #endif // _POINT3D_HXX
366
367