]> git.mxchange.org Git - simgear.git/blob - Math/point3d.hxx
Cygnus tools compatibility tweaks.
[simgear.git] / 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 program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
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
34 #include <iostream>
35 #include <assert.h>
36 #if defined( __BORLANDC__ )
37 #  define exception c_exception
38 #elif defined( __FreeBSD__ )
39 #  include <math.h>
40 #endif
41
42 const double fgPoint3_Epsilon = 0.0000001;
43
44 enum {PX, PY, PZ};                  // axes
45
46
47 ///////////////////////////
48 //
49 // 3D Point
50 //
51 ///////////////////////////
52
53 class Point3D {
54
55 protected:
56
57     double n[3];
58
59 public:
60
61     // Constructors
62
63     Point3D();
64     Point3D(const double x, const double y, const double z);
65     explicit Point3D(const double d);
66     Point3D(const Point3D &p);
67
68     // Assignment operators
69
70     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
71     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
72     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
73     Point3D& operator *= ( const double d );     // multiplication by a constant
74     Point3D& operator /= ( const double d );     // division by a constant
75
76     void setx(const double x);
77     void sety(const double y);
78     void setz(const double z);
79
80     // Queries 
81
82     double& operator [] ( int i);                // indexing
83     double operator[] (int i) const;             // read-only indexing
84
85     double x() const;      // cartesian x
86     double y() const;      // cartesian y
87     double z() const;      // cartesian z
88
89     double lon() const;    // polar longitude
90     double lat() const;    // polar latitude
91     double radius() const; // polar radius
92     double elev() const;   // geodetic elevation (if specifying a surface point)
93
94     // friends
95     friend Point3D operator - (const Point3D& p);                   // -p1
96     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
97     friend istream& operator>> ( istream&, Point3D& );
98     friend ostream& operator<< ( ostream&, const Point3D& );
99
100     // Special functions
101     double distance3D(const Point3D& a) const; // distance between
102 };
103
104
105 // input from stream
106 inline istream&
107 operator >> ( istream& in, Point3D& p)
108 {
109     char c;
110
111     in >> p.n[PX];
112
113     // read past optional comma
114     while ( in.get(c) ) {
115         if ( (c != ' ') && (c != ',') ) {
116             // push back on the stream
117             in.putback(c);
118             break;
119         }
120     }
121         
122     in >> p.n[PY];
123
124     // read past optional comma
125     while ( in.get(c) ) {
126         if ( (c != ' ') && (c != ',') ) {
127             // push back on the stream
128             in.putback(c);
129             break;
130         }
131     }
132         
133     in >> p.n[PZ];
134
135     return in;
136 }
137
138 inline ostream&
139 operator<< ( ostream& out, const Point3D& p )
140 {
141     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
142 }
143
144 ///////////////////////////
145 //
146 // Point3D Member functions
147 //
148 ///////////////////////////
149
150 // CONSTRUCTORS
151
152 inline Point3D::Point3D() {}
153
154 inline Point3D::Point3D(const double x, const double y, const double z)
155 {
156     n[PX] = x; n[PY] = y; n[PZ] = z;
157 }
158
159 inline Point3D::Point3D(const double d)
160 {
161     n[PX] = n[PY] = n[PZ] = d;
162 }
163
164 inline Point3D::Point3D(const Point3D& p)
165 {
166     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
167 }
168
169 // ASSIGNMENT OPERATORS
170
171 inline Point3D& Point3D::operator = (const Point3D& p)
172 {
173     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
174 }
175
176 inline Point3D& Point3D::operator += ( const Point3D& p )
177 {
178     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
179 }
180
181 inline Point3D& Point3D::operator -= ( const Point3D& p )
182 {
183     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
184 }
185
186 inline Point3D& Point3D::operator *= ( const double d )
187 {
188     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
189 }
190
191 inline Point3D& Point3D::operator /= ( const double d )
192 {
193     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
194     return *this;
195 }
196
197 inline void Point3D::setx(const double x) {
198     n[PX] = x;
199 }
200
201 inline void Point3D::sety(const double y) {
202     n[PY] = y;
203 }
204
205 inline void Point3D::setz(const double z) {
206     n[PZ] = z;
207 }
208
209 // QUERIES
210
211 inline double& Point3D::operator [] ( int i)
212 {
213     assert(! (i < PX || i > PZ));
214     return n[i];
215 }
216
217 inline double Point3D::operator [] ( int i) const {
218     assert(! (i < PX || i > PZ));
219     return n[i];
220 }
221
222
223 inline double Point3D::x() const { return n[PX]; }
224
225 inline double Point3D::y() const { return n[PY]; }
226
227 inline double Point3D::z() const { return n[PZ]; }
228
229 inline double Point3D::lon() const { return n[PX]; }
230
231 inline double Point3D::lat() const { return n[PY]; }
232
233 inline double Point3D::radius() const { return n[PZ]; }
234
235 inline double Point3D::elev() const { return n[PZ]; }
236
237
238 // FRIENDS
239
240 inline Point3D operator - (const Point3D& a)
241 {
242     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
243 }
244
245 inline Point3D operator + (const Point3D& a, const Point3D& b)
246 {
247     return Point3D(a) += b;
248 }
249
250 inline Point3D operator - (const Point3D& a, const Point3D& b)
251 {
252     return Point3D(a) -= b;
253 }
254
255 inline Point3D operator * (const Point3D& a, const double d)
256 {
257     return Point3D(a) *= d;
258 }
259
260 inline Point3D operator * (const double d, const Point3D& a)
261 {
262     return a*d;
263 }
264
265 inline Point3D operator / (const Point3D& a, const double d)
266 {
267     return Point3D(a) *= (1.0 / d );
268 }
269
270 inline bool operator == (const Point3D& a, const Point3D& b)
271 {
272     return
273         (a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
274         (a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
275         (a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
276 }
277
278 inline bool operator != (const Point3D& a, const Point3D& b)
279 {
280     return !(a == b);
281 }
282
283 // Special functions
284
285 inline double
286 Point3D::distance3D(const Point3D& a ) const
287 {
288     double x, y, z;
289
290     x = n[PX] - a.n[PX];
291     y = n[PY] - a.n[PY];
292     z = n[PZ] - a.n[PZ];
293
294     return sqrt(x*x + y*y + z*z);
295 }
296
297 #endif // _POINT3D_HXX
298
299
300 // $Log$
301 // Revision 1.6  1998/11/23 21:46:37  curt
302 // Borland portability tweaks.
303 //
304 // Revision 1.5  1998/11/20 01:00:38  curt
305 // Patch in fgGeoc2Geod() to avoid a floating explosion.
306 // point3d.hxx include math.h for FreeBSD
307 //
308 // Revision 1.4  1998/11/11 00:18:38  curt
309 // Check for domain error in fgGeoctoGeod()
310 //
311 // Revision 1.3  1998/10/20 18:21:49  curt
312 // Tweaks from Bernie Bright.
313 //
314 // Revision 1.2  1998/10/18 01:17:12  curt
315 // Point3D tweaks.
316 //
317 // Revision 1.1  1998/10/16 00:50:29  curt
318 // Added point3d.hxx to replace cheezy fgPoint3d struct.
319 //
320 //