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 // -rp- assert.h is buggy under MWCWP3, as multiple #include undef assert !
58 const double fgPoint3_Epsilon = 0.0000001;
60 enum {PX, PY, PZ}; // axes
62 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
64 istream& operator>> ( istream&, Point3D& );
65 ostream& operator<< ( ostream&, const Point3D& );
66 Point3D operator- (const Point3D& p); // -p1
67 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
70 ///////////////////////////
74 ///////////////////////////
87 Point3D(const double x, const double y, const double z);
88 explicit Point3D(const double d);
89 Point3D(const Point3D &p);
91 // Assignment operators
93 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
94 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
95 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
96 Point3D& operator *= ( const double d ); // multiplication by a constant
97 Point3D& operator /= ( const double d ); // division by a constant
99 void setx(const double x);
100 void sety(const double y);
101 void setz(const double z);
105 double& operator [] ( int i); // indexing
106 double operator[] (int i) const; // read-only indexing
108 double x() const; // cartesian x
109 double y() const; // cartesian y
110 double z() const; // cartesian z
112 double lon() const; // polar longitude
113 double lat() const; // polar latitude
114 double radius() const; // polar radius
115 double elev() const; // geodetic elevation (if specifying a surface point)
118 friend Point3D operator - (const Point3D& p); // -p1
119 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
120 friend istream& operator>> ( istream&, Point3D& );
121 friend ostream& operator<< ( ostream&, const Point3D& );
124 double distance3D(const Point3D& a) const; // distance between
125 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
131 operator >> ( istream& in, Point3D& p)
137 // read past optional comma
138 while ( in.get(c) ) {
139 if ( (c != ' ') && (c != ',') ) {
140 // push back on the stream
148 // read past optional comma
149 while ( in.get(c) ) {
150 if ( (c != ' ') && (c != ',') ) {
151 // push back on the stream
163 operator<< ( ostream& out, const Point3D& p )
165 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
168 ///////////////////////////
170 // Point3D Member functions
172 ///////////////////////////
176 inline Point3D::Point3D() {}
178 inline Point3D::Point3D(const double x, const double y, const double z)
180 n[PX] = x; n[PY] = y; n[PZ] = z;
183 inline Point3D::Point3D(const double d)
185 n[PX] = n[PY] = n[PZ] = d;
188 inline Point3D::Point3D(const Point3D& p)
190 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
193 // ASSIGNMENT OPERATORS
195 inline Point3D& Point3D::operator = (const Point3D& p)
197 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
200 inline Point3D& Point3D::operator += ( const Point3D& p )
202 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
205 inline Point3D& Point3D::operator -= ( const Point3D& p )
207 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
210 inline Point3D& Point3D::operator *= ( const double d )
212 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
215 inline Point3D& Point3D::operator /= ( const double d )
217 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
221 inline void Point3D::setx(const double x) {
225 inline void Point3D::sety(const double y) {
229 inline void Point3D::setz(const double z) {
235 inline double& Point3D::operator [] ( int i)
237 assert(! (i < PX || i > PZ));
241 inline double Point3D::operator [] ( int i) const {
242 assert(! (i < PX || i > PZ));
247 inline double Point3D::x() const { return n[PX]; }
249 inline double Point3D::y() const { return n[PY]; }
251 inline double Point3D::z() const { return n[PZ]; }
253 inline double Point3D::lon() const { return n[PX]; }
255 inline double Point3D::lat() const { return n[PY]; }
257 inline double Point3D::radius() const { return n[PZ]; }
259 inline double Point3D::elev() const { return n[PZ]; }
264 inline Point3D operator - (const Point3D& a)
266 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
269 inline Point3D operator + (const Point3D& a, const Point3D& b)
271 return Point3D(a) += b;
274 inline Point3D operator - (const Point3D& a, const Point3D& b)
276 return Point3D(a) -= b;
279 inline Point3D operator * (const Point3D& a, const double d)
281 return Point3D(a) *= d;
284 inline Point3D operator * (const double d, const Point3D& a)
289 inline Point3D operator / (const Point3D& a, const double d)
291 return Point3D(a) *= (1.0 / d );
294 inline bool operator == (const Point3D& a, const Point3D& b)
297 (a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
298 (a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
299 (a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
302 inline bool operator != (const Point3D& a, const Point3D& b)
310 Point3D::distance3D(const Point3D& a ) const
318 return sqrt(x*x + y*y + z*z);
323 Point3D::distance3Dsquared(const Point3D& a ) const
331 return(x*x + y*y + z*z);
335 #endif // _POINT3D_HXX