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 <simgear/compiler.h>
34 #ifdef FG_MATH_EXCEPTION_CLASH
35 # define exception c_exception
38 #ifdef FG_HAVE_STD_INCLUDES
43 # include <iostream.h>
48 // I don't understand ... <math.h> or <cmath> should be included
49 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
50 // can go ahead and add this -- CLO
52 FG_USING_NAMESPACE(std);
55 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
56 FG_USING_STD(ostream);
57 FG_USING_STD(istream);
61 const double fgPoint3_Epsilon = 0.0000001;
63 enum {PX, PY, PZ}; // axes
65 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
67 istream& operator>> ( istream&, Point3D& );
68 ostream& operator<< ( ostream&, const Point3D& );
69 Point3D operator- (const Point3D& p); // -p1
70 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
73 ///////////////////////////
77 ///////////////////////////
90 Point3D(const double x, const double y, const double z);
91 explicit Point3D(const double d);
92 Point3D(const Point3D &p);
94 // Assignment operators
96 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
97 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
98 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
99 Point3D& operator *= ( const double d ); // multiplication by a constant
100 Point3D& operator /= ( const double d ); // division by a constant
102 void setx(const double x);
103 void sety(const double y);
104 void setz(const double z);
108 double& operator [] ( int i); // indexing
109 double operator[] (int i) const; // read-only indexing
111 inline const double *get_n() const { return n; };
112 double x() const; // cartesian x
113 double y() const; // cartesian y
114 double z() const; // cartesian z
116 double lon() const; // polar longitude
117 double lat() const; // polar latitude
118 double radius() const; // polar radius
119 double elev() const; // geodetic elevation (if specifying a surface point)
122 friend Point3D operator - (const Point3D& p); // -p1
123 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
124 friend istream& operator>> ( istream&, Point3D& );
125 friend ostream& operator<< ( ostream&, const Point3D& );
128 double distance3D(const Point3D& a) const; // distance between
129 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
135 operator >> ( istream& in, Point3D& p)
141 // read past optional comma
142 while ( in.get(c) ) {
143 if ( (c != ' ') && (c != ',') ) {
144 // push back on the stream
152 // read past optional comma
153 while ( in.get(c) ) {
154 if ( (c != ' ') && (c != ',') ) {
155 // push back on the stream
167 operator<< ( ostream& out, const Point3D& p )
169 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
172 ///////////////////////////
174 // Point3D Member functions
176 ///////////////////////////
180 inline Point3D::Point3D() {}
182 inline Point3D::Point3D(const double x, const double y, const double z)
184 n[PX] = x; n[PY] = y; n[PZ] = z;
187 inline Point3D::Point3D(const double d)
189 n[PX] = n[PY] = n[PZ] = d;
192 inline Point3D::Point3D(const Point3D& p)
194 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
197 // ASSIGNMENT OPERATORS
199 inline Point3D& Point3D::operator = (const Point3D& p)
201 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
204 inline Point3D& Point3D::operator += ( const Point3D& p )
206 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
209 inline Point3D& Point3D::operator -= ( const Point3D& p )
211 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
214 inline Point3D& Point3D::operator *= ( const double d )
216 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
219 inline Point3D& Point3D::operator /= ( const double d )
221 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
225 inline void Point3D::setx(const double x) {
229 inline void Point3D::sety(const double y) {
233 inline void Point3D::setz(const double z) {
239 inline double& Point3D::operator [] ( int i)
241 assert(! (i < PX || i > PZ));
245 inline double Point3D::operator [] ( int i) const {
246 assert(! (i < PX || i > PZ));
251 inline double Point3D::x() const { return n[PX]; }
253 inline double Point3D::y() const { return n[PY]; }
255 inline double Point3D::z() const { return n[PZ]; }
257 inline double Point3D::lon() const { return n[PX]; }
259 inline double Point3D::lat() const { return n[PY]; }
261 inline double Point3D::radius() const { return n[PZ]; }
263 inline double Point3D::elev() const { return n[PZ]; }
268 inline Point3D operator - (const Point3D& a)
270 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
273 inline Point3D operator + (const Point3D& a, const Point3D& b)
275 return Point3D(a) += b;
278 inline Point3D operator - (const Point3D& a, const Point3D& b)
280 return Point3D(a) -= b;
283 inline Point3D operator * (const Point3D& a, const double d)
285 return Point3D(a) *= d;
288 inline Point3D operator * (const double d, const Point3D& a)
293 inline Point3D operator / (const Point3D& a, const double d)
295 return Point3D(a) *= (1.0 / d );
298 inline bool operator == (const Point3D& a, const Point3D& b)
301 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
302 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
303 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
306 inline bool operator != (const Point3D& a, const Point3D& b)
314 Point3D::distance3D(const Point3D& a ) const
322 return sqrt(x*x + y*y + z*z);
327 Point3D::distance3Dsquared(const Point3D& a ) const
335 return(x*x + y*y + z*z);
339 #endif // _POINT3D_HXX