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++
37 const double fgPoint3_Epsilon = 0.0000001;
39 enum {PX, PY, PZ}; // axes
42 ///////////////////////////
46 ///////////////////////////
59 Point3D(const double x, const double y, const double z);
60 explicit Point3D(const double d);
61 Point3D(const Point3D &p);
63 // Assignment operators
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
71 void setx(const double x);
72 void sety(const double y);
73 void setz(const double z);
77 double& operator [] ( int i); // indexing
78 double operator[] (int i) const; // read-only indexing
80 double x() const; // cartesian x
81 double y() const; // cartesian y
82 double z() const; // cartesian z
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)
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& );
96 double distance3D(const Point3D& a) const; // distance between
102 operator >> ( istream& in, Point3D& p)
108 // read past optional comma
109 while ( in.get(c) ) {
110 if ( (c != ' ') && (c != ',') ) {
111 // push back on the stream
119 // read past optional comma
120 while ( in.get(c) ) {
121 if ( (c != ' ') && (c != ',') ) {
122 // push back on the stream
134 operator<< ( ostream& out, const Point3D& p )
136 return out << p.n[PX] << ',' << p.n[PY] << ',' << p.n[PZ];
139 ///////////////////////////
141 // Point3D Member functions
143 ///////////////////////////
147 inline Point3D::Point3D() {}
149 inline Point3D::Point3D(const double x, const double y, const double z)
151 n[PX] = x; n[PY] = y; n[PZ] = z;
154 inline Point3D::Point3D(const double d)
156 n[PX] = n[PY] = n[PZ] = d;
159 inline Point3D::Point3D(const Point3D& p)
161 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
164 // ASSIGNMENT OPERATORS
166 inline Point3D& Point3D::operator = (const Point3D& p)
168 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
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 double d )
183 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
186 inline Point3D& Point3D::operator /= ( const double d )
188 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
192 inline void Point3D::setx(const double x) {
196 inline void Point3D::sety(const double y) {
200 inline void Point3D::setz(const double z) {
206 inline double& Point3D::operator [] ( int i)
208 assert(! (i < PX || i > PZ));
212 inline double Point3D::operator [] ( int i) const {
213 assert(! (i < PX || i > PZ));
218 inline double Point3D::x() const { return n[PX]; }
220 inline double Point3D::y() const { return n[PY]; }
222 inline double Point3D::z() const { return n[PZ]; }
224 inline double Point3D::lon() const { return n[PX]; }
226 inline double Point3D::lat() const { return n[PY]; }
228 inline double Point3D::radius() const { return n[PZ]; }
230 inline double Point3D::elev() const { return n[PZ]; }
235 inline Point3D operator - (const Point3D& a)
237 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
240 inline Point3D operator + (const Point3D& a, const Point3D& b)
242 return Point3D(a) += b;
245 inline Point3D operator - (const Point3D& a, const Point3D& b)
247 return Point3D(a) -= b;
250 inline Point3D operator * (const Point3D& a, const double d)
252 return Point3D(a) *= d;
255 inline Point3D operator * (const double d, const Point3D& a)
260 inline Point3D operator / (const Point3D& a, const double d)
262 return Point3D(a) *= (1.0 / d );
265 inline bool operator == (const Point3D& a, const Point3D& b)
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;
273 inline bool operator != (const Point3D& a, const Point3D& b)
281 Point3D::distance3D(const Point3D& a ) const
289 return sqrt(x*x + y*y + z*z);
292 #endif // _POINT3D_HXX
296 // Revision 1.3 1998/10/20 18:21:49 curt
297 // Tweaks from Bernie Bright.
299 // Revision 1.2 1998/10/18 01:17:12 curt
302 // Revision 1.1 1998/10/16 00:50:29 curt
303 // Added point3d.hxx to replace cheezy fgPoint3d struct.