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