]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
cygwin and mingw fixes
[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 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 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 // ASSIGNMENT OPERATORS
206
207 inline Point3D& Point3D::operator = (const Point3D& p)
208 {
209     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
210 }
211
212 inline Point3D& Point3D::operator += ( const Point3D& p )
213 {
214     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
215 }
216
217 inline Point3D& Point3D::operator -= ( const Point3D& p )
218 {
219     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
220 }
221
222 inline Point3D& Point3D::operator *= ( const double d )
223 {
224     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
225 }
226
227 inline Point3D& Point3D::operator /= ( const double d )
228 {
229     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
230     return *this;
231 }
232
233 inline void Point3D::setx(const double x) {
234     n[PX] = x;
235 }
236
237 inline void Point3D::sety(const double y) {
238     n[PY] = y;
239 }
240
241 inline void Point3D::setz(const double z) {
242     n[PZ] = z;
243 }
244
245 inline void Point3D::setlon(const double x) {
246     n[PX] = x;
247 }
248
249 inline void Point3D::setlat(const double y) {
250     n[PY] = y;
251 }
252
253 inline void Point3D::setradius(const double z) {
254     n[PZ] = z;
255 }
256
257 inline void Point3D::setelev(const double z) {
258     n[PZ] = z;
259 }
260
261 // QUERIES
262
263 inline double& Point3D::operator [] ( int i)
264 {
265     assert(! (i < PX || i > PZ));
266     return n[i];
267 }
268
269 inline double Point3D::operator [] ( int i) const {
270     assert(! (i < PX || i > PZ));
271     return n[i];
272 }
273
274
275 inline double Point3D::x() const { return n[PX]; }
276
277 inline double Point3D::y() const { return n[PY]; }
278
279 inline double Point3D::z() const { return n[PZ]; }
280
281 inline double Point3D::lon() const { return n[PX]; }
282
283 inline double Point3D::lat() const { return n[PY]; }
284
285 inline double Point3D::radius() const { return n[PZ]; }
286
287 inline double Point3D::elev() const { return n[PZ]; }
288
289
290 // FRIENDS
291
292 inline Point3D operator - (const Point3D& a)
293 {
294     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
295 }
296
297 inline Point3D operator + (const Point3D& a, const Point3D& b)
298 {
299     return Point3D(a) += b;
300 }
301
302 inline Point3D operator - (const Point3D& a, const Point3D& b)
303 {
304     return Point3D(a) -= b;
305 }
306
307 inline Point3D operator * (const Point3D& a, const double d)
308 {
309     return Point3D(a) *= d;
310 }
311
312 inline Point3D operator * (const double d, const Point3D& a)
313 {
314     return a*d;
315 }
316
317 inline Point3D operator / (const Point3D& a, const double d)
318 {
319     return Point3D(a) *= (1.0 / d );
320 }
321
322 inline bool operator == (const Point3D& a, const Point3D& b)
323 {
324     return
325         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
326         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
327         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
328 }
329
330 inline bool operator != (const Point3D& a, const Point3D& b)
331 {
332     return !(a == b);
333 }
334
335 // Special functions
336
337 inline double
338 Point3D::distance3D(const Point3D& a ) const
339 {
340     double x, y, z;
341
342     x = n[PX] - a.n[PX];
343     y = n[PY] - a.n[PY];
344     z = n[PZ] - a.n[PZ];
345
346     return sqrt(x*x + y*y + z*z);
347 }
348
349
350 inline double
351 Point3D::distance3Dsquared(const Point3D& a ) const
352 {
353     double x, y, z;
354
355     x = n[PX] - a.n[PX];
356     y = n[PY] - a.n[PY];
357     z = n[PZ] - a.n[PZ];
358
359     return(x*x + y*y + z*z);
360 }
361
362
363 #endif // _POINT3D_HXX
364
365