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.
29 # error This library requires C++
32 #include <Include/compiler.h>
34 #ifdef FG_MATH_EXCEPTION_CLASH
35 # define exception c_exception
38 #ifdef FG_HAVE_STD_INCLUDES
43 # include <iostream.h>
48 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
49 FG_USING_STD(ostream);
50 FG_USING_STD(istream);
53 // -dw- someone seems to have forgotten this...
59 const double fgPoint3_Epsilon = 0.0000001;
61 enum {PX, PY, PZ}; // axes
63 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
65 istream& operator>> ( istream&, Point3D& );
66 ostream& operator<< ( ostream&, const Point3D& );
67 Point3D operator- (const Point3D& p); // -p1
68 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
71 ///////////////////////////
75 ///////////////////////////
88 Point3D(const double x, const double y, const double z);
89 explicit Point3D(const double d);
90 Point3D(const Point3D &p);
92 // Assignment operators
94 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
95 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
96 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
97 Point3D& operator *= ( const double d ); // multiplication by a constant
98 Point3D& operator /= ( const double d ); // division by a constant
100 void setx(const double x);
101 void sety(const double y);
102 void setz(const double z);
106 double& operator [] ( int i); // indexing
107 double operator[] (int i) const; // read-only indexing
109 double x() const; // cartesian x
110 double y() const; // cartesian y
111 double z() const; // cartesian z
113 double lon() const; // polar longitude
114 double lat() const; // polar latitude
115 double radius() const; // polar radius
116 double elev() const; // geodetic elevation (if specifying a surface point)
119 friend Point3D operator - (const Point3D& p); // -p1
120 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
121 friend istream& operator>> ( istream&, Point3D& );
122 friend ostream& operator<< ( ostream&, const Point3D& );
125 double distance3D(const Point3D& a) const; // distance between
126 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
132 operator >> ( istream& in, Point3D& p)
138 // read past optional comma
139 while ( in.get(c) ) {
140 if ( (c != ' ') && (c != ',') ) {
141 // push back on the stream
149 // read past optional comma
150 while ( in.get(c) ) {
151 if ( (c != ' ') && (c != ',') ) {
152 // push back on the stream
164 operator<< ( ostream& out, const Point3D& p )
166 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
169 ///////////////////////////
171 // Point3D Member functions
173 ///////////////////////////
177 inline Point3D::Point3D() {}
179 inline Point3D::Point3D(const double x, const double y, const double z)
181 n[PX] = x; n[PY] = y; n[PZ] = z;
184 inline Point3D::Point3D(const double d)
186 n[PX] = n[PY] = n[PZ] = d;
189 inline Point3D::Point3D(const Point3D& p)
191 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
194 // ASSIGNMENT OPERATORS
196 inline Point3D& Point3D::operator = (const Point3D& p)
198 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
201 inline Point3D& Point3D::operator += ( const Point3D& p )
203 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
206 inline Point3D& Point3D::operator -= ( const Point3D& p )
208 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
211 inline Point3D& Point3D::operator *= ( const double d )
213 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
216 inline Point3D& Point3D::operator /= ( const double d )
218 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
222 inline void Point3D::setx(const double x) {
226 inline void Point3D::sety(const double y) {
230 inline void Point3D::setz(const double z) {
236 inline double& Point3D::operator [] ( int i)
238 assert(! (i < PX || i > PZ));
242 inline double Point3D::operator [] ( int i) const {
243 assert(! (i < PX || i > PZ));
248 inline double Point3D::x() const { return n[PX]; }
250 inline double Point3D::y() const { return n[PY]; }
252 inline double Point3D::z() const { return n[PZ]; }
254 inline double Point3D::lon() const { return n[PX]; }
256 inline double Point3D::lat() const { return n[PY]; }
258 inline double Point3D::radius() const { return n[PZ]; }
260 inline double Point3D::elev() const { return n[PZ]; }
265 inline Point3D operator - (const Point3D& a)
267 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
270 inline Point3D operator + (const Point3D& a, const Point3D& b)
272 return Point3D(a) += b;
275 inline Point3D operator - (const Point3D& a, const Point3D& b)
277 return Point3D(a) -= b;
280 inline Point3D operator * (const Point3D& a, const double d)
282 return Point3D(a) *= d;
285 inline Point3D operator * (const double d, const Point3D& a)
290 inline Point3D operator / (const Point3D& a, const double d)
292 return Point3D(a) *= (1.0 / d );
295 inline bool operator == (const Point3D& a, const Point3D& b)
298 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
299 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
300 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
303 inline bool operator != (const Point3D& a, const Point3D& b)
311 Point3D::distance3D(const Point3D& a ) const
319 return sqrt(x*x + y*y + z*z);
324 Point3D::distance3Dsquared(const Point3D& a ) const
332 return(x*x + y*y + z*z);
336 #endif // _POINT3D_HXX