1 // point3d.hxx -- a 3d point class.
3 // Adapted from algebra3 by Jean-Francois Doue, started October 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
22 // (Log is kept at end of this file)
30 # error This library requires C++
36 #if defined( __BORLANDC__ )
37 # define exception c_exception
38 #elif defined( __FreeBSD__ )
42 const double fgPoint3_Epsilon = 0.0000001;
44 enum {PX, PY, PZ}; // axes
47 ///////////////////////////
51 ///////////////////////////
64 Point3D(const double x, const double y, const double z);
65 explicit Point3D(const double d);
66 Point3D(const Point3D &p);
68 // Assignment operators
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
76 void setx(const double x);
77 void sety(const double y);
78 void setz(const double z);
82 double& operator [] ( int i); // indexing
83 double operator[] (int i) const; // read-only indexing
85 double x() const; // cartesian x
86 double y() const; // cartesian y
87 double z() const; // cartesian z
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)
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& );
101 double distance3D(const Point3D& a) const; // distance between
107 operator >> ( istream& in, Point3D& p)
113 // read past optional comma
114 while ( in.get(c) ) {
115 if ( (c != ' ') && (c != ',') ) {
116 // push back on the stream
124 // read past optional comma
125 while ( in.get(c) ) {
126 if ( (c != ' ') && (c != ',') ) {
127 // push back on the stream
139 operator<< ( ostream& out, const Point3D& p )
141 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
144 ///////////////////////////
146 // Point3D Member functions
148 ///////////////////////////
152 inline Point3D::Point3D() {}
154 inline Point3D::Point3D(const double x, const double y, const double z)
156 n[PX] = x; n[PY] = y; n[PZ] = z;
159 inline Point3D::Point3D(const double d)
161 n[PX] = n[PY] = n[PZ] = d;
164 inline Point3D::Point3D(const Point3D& p)
166 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
169 // ASSIGNMENT OPERATORS
171 inline Point3D& Point3D::operator = (const Point3D& p)
173 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
176 inline Point3D& Point3D::operator += ( const Point3D& p )
178 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
181 inline Point3D& Point3D::operator -= ( const Point3D& p )
183 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
186 inline Point3D& Point3D::operator *= ( const double d )
188 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
191 inline Point3D& Point3D::operator /= ( const double d )
193 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
197 inline void Point3D::setx(const double x) {
201 inline void Point3D::sety(const double y) {
205 inline void Point3D::setz(const double z) {
211 inline double& Point3D::operator [] ( int i)
213 assert(! (i < PX || i > PZ));
217 inline double Point3D::operator [] ( int i) const {
218 assert(! (i < PX || i > PZ));
223 inline double Point3D::x() const { return n[PX]; }
225 inline double Point3D::y() const { return n[PY]; }
227 inline double Point3D::z() const { return n[PZ]; }
229 inline double Point3D::lon() const { return n[PX]; }
231 inline double Point3D::lat() const { return n[PY]; }
233 inline double Point3D::radius() const { return n[PZ]; }
235 inline double Point3D::elev() const { return n[PZ]; }
240 inline Point3D operator - (const Point3D& a)
242 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
245 inline Point3D operator + (const Point3D& a, const Point3D& b)
247 return Point3D(a) += b;
250 inline Point3D operator - (const Point3D& a, const Point3D& b)
252 return Point3D(a) -= b;
255 inline Point3D operator * (const Point3D& a, const double d)
257 return Point3D(a) *= d;
260 inline Point3D operator * (const double d, const Point3D& a)
265 inline Point3D operator / (const Point3D& a, const double d)
267 return Point3D(a) *= (1.0 / d );
270 inline bool operator == (const Point3D& a, const Point3D& b)
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;
278 inline bool operator != (const Point3D& a, const Point3D& b)
286 Point3D::distance3D(const Point3D& a ) const
294 return sqrt(x*x + y*y + z*z);
297 #endif // _POINT3D_HXX
301 // Revision 1.6 1998/11/23 21:46:37 curt
302 // Borland portability tweaks.
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
308 // Revision 1.4 1998/11/11 00:18:38 curt
309 // Check for domain error in fgGeoctoGeod()
311 // Revision 1.3 1998/10/20 18:21:49 curt
312 // Tweaks from Bernie Bright.
314 // Revision 1.2 1998/10/18 01:17:12 curt
317 // Revision 1.1 1998/10/16 00:50:29 curt
318 // Added point3d.hxx to replace cheezy fgPoint3d struct.