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