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