]> git.mxchange.org Git - simgear.git/blob - simgear/math/point3d.hxx
Cleaned up a few poluting #defines.
[simgear.git] / simgear / math / point3d.hxx
1 // point3d.hxx -- a 3d point class.  
2 //
3 // Adapted from algebra3 by Jean-Francois Doue, started October 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 #ifndef _POINT3D_HXX
26 #define _POINT3D_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33 #include <simgear/compiler.h>
34
35 #ifdef FG_MATH_EXCEPTION_CLASH
36 # define exception c_exception
37 #endif
38
39 #ifdef FG_HAVE_STD_INCLUDES
40 # include <iostream>
41 # include <cassert>
42 # include <cmath>
43 #else
44 # include <iostream.h>
45 # include <assert.h>
46 # include <math.h>
47 #endif
48
49 #include "localconsts.hxx"
50
51 // I don't understand ... <math.h> or <cmath> should be included
52 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
53 // can go ahead and add this -- CLO
54 #ifdef __MWERKS__
55 FG_USING_NAMESPACE(std);
56 #endif
57
58 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
59 FG_USING_STD(ostream);
60 FG_USING_STD(istream);
61 #endif
62
63
64 const double fgPoint3_Epsilon = 0.0000001;
65
66 enum {PX, PY, PZ};                  // axes
67
68 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
69 class Point3D;
70 istream& operator>> ( istream&, Point3D& );
71 ostream& operator<< ( ostream&, const Point3D& );
72 Point3D operator- (const Point3D& p);               // -p1
73 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
74
75
76 ///////////////////////////
77 //
78 // 3D Point
79 //
80 ///////////////////////////
81
82 class Point3D {
83
84 protected:
85
86     double n[3];
87
88 public:
89
90     // Constructors
91
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     // Assignment operators
98
99     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
100     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
101     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
102     Point3D& operator *= ( const double d );     // multiplication by a constant
103     Point3D& operator /= ( const double d );     // division by a constant
104
105     void setx(const double x);
106     void sety(const double y);
107     void setz(const double z);
108
109     // Queries 
110
111     double& operator [] ( int i);                // indexing
112     double operator[] (int i) const;             // read-only indexing
113
114     inline const double *get_n() const { return n; };
115     double x() const;      // cartesian x
116     double y() const;      // cartesian y
117     double z() const;      // cartesian z
118
119     double lon() const;    // polar longitude
120     double lat() const;    // polar latitude
121     double radius() const; // polar radius
122     double elev() const;   // geodetic elevation (if specifying a surface point)
123
124     // friends
125     friend Point3D operator - (const Point3D& p);                   // -p1
126     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
127     friend istream& operator>> ( istream&, Point3D& );
128     friend ostream& operator<< ( ostream&, const Point3D& );
129
130     // Special functions
131     double distance3D(const Point3D& a) const;        // distance between
132     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
133 };
134
135
136 // input from stream
137 inline istream&
138 operator >> ( istream& in, Point3D& p)
139 {
140     char c;
141
142     in >> p.n[PX];
143
144     // read past optional comma
145     while ( in.get(c) ) {
146         if ( (c != ' ') && (c != ',') ) {
147             // push back on the stream
148             in.putback(c);
149             break;
150         }
151     }
152         
153     in >> p.n[PY];
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[PZ];
165
166     return in;
167 }
168
169 inline ostream&
170 operator<< ( ostream& out, const Point3D& p )
171 {
172     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
173 }
174
175 ///////////////////////////
176 //
177 // Point3D Member functions
178 //
179 ///////////////////////////
180
181 // CONSTRUCTORS
182
183 inline Point3D::Point3D() {}
184
185 inline Point3D::Point3D(const double x, const double y, const double z)
186 {
187     n[PX] = x; n[PY] = y; n[PZ] = z;
188 }
189
190 inline Point3D::Point3D(const double d)
191 {
192     n[PX] = n[PY] = n[PZ] = d;
193 }
194
195 inline Point3D::Point3D(const Point3D& p)
196 {
197     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
198 }
199
200 // ASSIGNMENT OPERATORS
201
202 inline Point3D& Point3D::operator = (const Point3D& p)
203 {
204     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
205 }
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 double d )
218 {
219     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
220 }
221
222 inline Point3D& Point3D::operator /= ( const double d )
223 {
224     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
225     return *this;
226 }
227
228 inline void Point3D::setx(const double x) {
229     n[PX] = x;
230 }
231
232 inline void Point3D::sety(const double y) {
233     n[PY] = y;
234 }
235
236 inline void Point3D::setz(const double z) {
237     n[PZ] = z;
238 }
239
240 // QUERIES
241
242 inline double& Point3D::operator [] ( int i)
243 {
244     assert(! (i < PX || i > PZ));
245     return n[i];
246 }
247
248 inline double Point3D::operator [] ( int i) const {
249     assert(! (i < PX || i > PZ));
250     return n[i];
251 }
252
253
254 inline double Point3D::x() const { return n[PX]; }
255
256 inline double Point3D::y() const { return n[PY]; }
257
258 inline double Point3D::z() const { return n[PZ]; }
259
260 inline double Point3D::lon() const { return n[PX]; }
261
262 inline double Point3D::lat() const { return n[PY]; }
263
264 inline double Point3D::radius() const { return n[PZ]; }
265
266 inline double Point3D::elev() const { return n[PZ]; }
267
268
269 // FRIENDS
270
271 inline Point3D operator - (const Point3D& a)
272 {
273     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
274 }
275
276 inline Point3D operator + (const Point3D& a, const Point3D& b)
277 {
278     return Point3D(a) += b;
279 }
280
281 inline Point3D operator - (const Point3D& a, const Point3D& b)
282 {
283     return Point3D(a) -= b;
284 }
285
286 inline Point3D operator * (const Point3D& a, const double d)
287 {
288     return Point3D(a) *= d;
289 }
290
291 inline Point3D operator * (const double d, const Point3D& a)
292 {
293     return a*d;
294 }
295
296 inline Point3D operator / (const Point3D& a, const double d)
297 {
298     return Point3D(a) *= (1.0 / d );
299 }
300
301 inline bool operator == (const Point3D& a, const Point3D& b)
302 {
303     return
304         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
305         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
306         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
307 }
308
309 inline bool operator != (const Point3D& a, const Point3D& b)
310 {
311     return !(a == b);
312 }
313
314 // Special functions
315
316 inline double
317 Point3D::distance3D(const Point3D& a ) const
318 {
319     double x, y, z;
320
321     x = n[PX] - a.n[PX];
322     y = n[PY] - a.n[PY];
323     z = n[PZ] - a.n[PZ];
324
325     return sqrt(x*x + y*y + z*z);
326 }
327
328
329 inline double
330 Point3D::distance3Dsquared(const Point3D& a ) const
331 {
332     double x, y, z;
333
334     x = n[PX] - a.n[PX];
335     y = n[PY] - a.n[PY];
336     z = n[PZ] - a.n[PZ];
337
338     return(x*x + y*y + z*z);
339 }
340
341
342 #endif // _POINT3D_HXX
343
344