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 library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, USA.
30 # error This library requires C++
33 #include <simgear/compiler.h>
35 #ifdef FG_MATH_EXCEPTION_CLASH
36 # define exception c_exception
39 #ifdef FG_HAVE_STD_INCLUDES
44 # include <iostream.h>
49 #include <simgear/math/localconsts.hxx>
51 // I don't understand ... <math.h> or <cmath> should be included
52 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
53 // can go ahead and add this -- CLO
55 FG_USING_NAMESPACE(std);
58 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
59 FG_USING_STD(ostream);
60 FG_USING_STD(istream);
64 const double fgPoint3_Epsilon = 0.0000001;
66 enum {PX, PY, PZ}; // axes
68 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
70 istream& operator>> ( istream&, Point3D& );
71 ostream& operator<< ( ostream&, const Point3D& );
72 Point3D operator- (const Point3D& p); // -p1
73 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
76 ///////////////////////////
80 ///////////////////////////
93 Point3D(const double x, const double y, const double z);
94 explicit Point3D(const double d);
95 Point3D(const Point3D &p);
97 // Assignment operators
99 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
100 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
101 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
102 Point3D& operator *= ( const double d ); // multiplication by a constant
103 Point3D& operator /= ( const double d ); // division by a constant
105 void setx(const double x);
106 void sety(const double y);
107 void setz(const double z);
111 double& operator [] ( int i); // indexing
112 double operator[] (int i) const; // read-only indexing
114 inline const double *get_n() const { return n; };
115 double x() const; // cartesian x
116 double y() const; // cartesian y
117 double z() const; // cartesian z
119 double lon() const; // polar longitude
120 double lat() const; // polar latitude
121 double radius() const; // polar radius
122 double elev() const; // geodetic elevation (if specifying a surface point)
125 friend Point3D operator - (const Point3D& p); // -p1
126 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
127 friend istream& operator>> ( istream&, Point3D& );
128 friend ostream& operator<< ( ostream&, const Point3D& );
131 double distance3D(const Point3D& a) const; // distance between
132 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
138 operator >> ( istream& in, Point3D& p)
144 // read past optional comma
145 while ( in.get(c) ) {
146 if ( (c != ' ') && (c != ',') ) {
147 // push back on the stream
155 // read past optional comma
156 while ( in.get(c) ) {
157 if ( (c != ' ') && (c != ',') ) {
158 // push back on the stream
170 operator<< ( ostream& out, const Point3D& p )
172 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
175 ///////////////////////////
177 // Point3D Member functions
179 ///////////////////////////
183 inline Point3D::Point3D() {}
185 inline Point3D::Point3D(const double x, const double y, const double z)
187 n[PX] = x; n[PY] = y; n[PZ] = z;
190 inline Point3D::Point3D(const double d)
192 n[PX] = n[PY] = n[PZ] = d;
195 inline Point3D::Point3D(const Point3D& p)
197 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
200 // ASSIGNMENT OPERATORS
202 inline Point3D& Point3D::operator = (const Point3D& p)
204 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
207 inline Point3D& Point3D::operator += ( const Point3D& p )
209 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
212 inline Point3D& Point3D::operator -= ( const Point3D& p )
214 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
217 inline Point3D& Point3D::operator *= ( const double d )
219 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
222 inline Point3D& Point3D::operator /= ( const double d )
224 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
228 inline void Point3D::setx(const double x) {
232 inline void Point3D::sety(const double y) {
236 inline void Point3D::setz(const double z) {
242 inline double& Point3D::operator [] ( int i)
244 assert(! (i < PX || i > PZ));
248 inline double Point3D::operator [] ( int i) const {
249 assert(! (i < PX || i > PZ));
254 inline double Point3D::x() const { return n[PX]; }
256 inline double Point3D::y() const { return n[PY]; }
258 inline double Point3D::z() const { return n[PZ]; }
260 inline double Point3D::lon() const { return n[PX]; }
262 inline double Point3D::lat() const { return n[PY]; }
264 inline double Point3D::radius() const { return n[PZ]; }
266 inline double Point3D::elev() const { return n[PZ]; }
271 inline Point3D operator - (const Point3D& a)
273 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
276 inline Point3D operator + (const Point3D& a, const Point3D& b)
278 return Point3D(a) += b;
281 inline Point3D operator - (const Point3D& a, const Point3D& b)
283 return Point3D(a) -= b;
286 inline Point3D operator * (const Point3D& a, const double d)
288 return Point3D(a) *= d;
291 inline Point3D operator * (const double d, const Point3D& a)
296 inline Point3D operator / (const Point3D& a, const double d)
298 return Point3D(a) *= (1.0 / d );
301 inline bool operator == (const Point3D& a, const Point3D& b)
304 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
305 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
306 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
309 inline bool operator != (const Point3D& a, const Point3D& b)
317 Point3D::distance3D(const Point3D& a ) const
325 return sqrt(x*x + y*y + z*z);
330 Point3D::distance3Dsquared(const Point3D& a ) const
338 return(x*x + y*y + z*z);
342 #endif // _POINT3D_HXX