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