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++
39 const double fgPoint3_Epsilon = 0.0000001;
41 enum {PX, PY, PZ}; // axes
44 ///////////////////////////
48 ///////////////////////////
61 Point3D(const double x, const double y, const double z);
62 explicit Point3D(const double d);
63 Point3D(const Point3D &p);
65 // Assignment operators
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
73 void setx(const double x);
74 void sety(const double y);
75 void setz(const double z);
79 double& operator [] ( int i); // indexing
80 double operator[] (int i) const; // read-only indexing
82 double x() const; // cartesian x
83 double y() const; // cartesian y
84 double z() const; // cartesian z
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)
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& );
98 double distance3D(const Point3D& a) const; // distance between
104 operator >> ( istream& in, Point3D& p)
110 // read past optional comma
111 while ( in.get(c) ) {
112 if ( (c != ' ') && (c != ',') ) {
113 // push back on the stream
121 // read past optional comma
122 while ( in.get(c) ) {
123 if ( (c != ' ') && (c != ',') ) {
124 // push back on the stream
136 operator<< ( ostream& out, const Point3D& p )
138 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
141 ///////////////////////////
143 // Point3D Member functions
145 ///////////////////////////
149 inline Point3D::Point3D() {}
151 inline Point3D::Point3D(const double x, const double y, const double z)
153 n[PX] = x; n[PY] = y; n[PZ] = z;
156 inline Point3D::Point3D(const double d)
158 n[PX] = n[PY] = n[PZ] = d;
161 inline Point3D::Point3D(const Point3D& p)
163 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
166 // ASSIGNMENT OPERATORS
168 inline Point3D& Point3D::operator = (const Point3D& p)
170 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
173 inline Point3D& Point3D::operator += ( const Point3D& p )
175 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
178 inline Point3D& Point3D::operator -= ( const Point3D& p )
180 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
183 inline Point3D& Point3D::operator *= ( const double d )
185 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
188 inline Point3D& Point3D::operator /= ( const double d )
190 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
194 inline void Point3D::setx(const double x) {
198 inline void Point3D::sety(const double y) {
202 inline void Point3D::setz(const double z) {
208 inline double& Point3D::operator [] ( int i)
210 assert(! (i < PX || i > PZ));
214 inline double Point3D::operator [] ( int i) const {
215 assert(! (i < PX || i > PZ));
220 inline double Point3D::x() const { return n[PX]; }
222 inline double Point3D::y() const { return n[PY]; }
224 inline double Point3D::z() const { return n[PZ]; }
226 inline double Point3D::lon() const { return n[PX]; }
228 inline double Point3D::lat() const { return n[PY]; }
230 inline double Point3D::radius() const { return n[PZ]; }
232 inline double Point3D::elev() const { return n[PZ]; }
237 inline Point3D operator - (const Point3D& a)
239 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
242 inline Point3D operator + (const Point3D& a, const Point3D& b)
244 return Point3D(a) += b;
247 inline Point3D operator - (const Point3D& a, const Point3D& b)
249 return Point3D(a) -= b;
252 inline Point3D operator * (const Point3D& a, const double d)
254 return Point3D(a) *= d;
257 inline Point3D operator * (const double d, const Point3D& a)
262 inline Point3D operator / (const Point3D& a, const double d)
264 return Point3D(a) *= (1.0 / d );
267 inline bool operator == (const Point3D& a, const Point3D& b)
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;
275 inline bool operator != (const Point3D& a, const Point3D& b)
283 Point3D::distance3D(const Point3D& a ) const
291 return sqrt(x*x + y*y + z*z);
294 #endif // _POINT3D_HXX
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
302 // Revision 1.4 1998/11/11 00:18:38 curt
303 // Check for domain error in fgGeoctoGeod()
305 // Revision 1.3 1998/10/20 18:21:49 curt
306 // Tweaks from Bernie Bright.
308 // Revision 1.2 1998/10/18 01:17:12 curt
311 // Revision 1.1 1998/10/16 00:50:29 curt
312 // Added point3d.hxx to replace cheezy fgPoint3d struct.