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 // I don't understand ... <math.h> or <cmath> should be included
50 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
51 // can go ahead and add this -- CLO
53 FG_USING_NAMESPACE(std);
56 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
57 FG_USING_STD(ostream);
58 FG_USING_STD(istream);
62 const double fgPoint3_Epsilon = 0.0000001;
64 enum {PX, PY, PZ}; // axes
66 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
68 istream& operator>> ( istream&, Point3D& );
69 ostream& operator<< ( ostream&, const Point3D& );
70 Point3D operator- (const Point3D& p); // -p1
71 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
74 ///////////////////////////
78 ///////////////////////////
91 Point3D(const double x, const double y, const double z);
92 explicit Point3D(const double d);
93 Point3D(const Point3D &p);
95 // Assignment operators
97 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
98 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
99 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
100 Point3D& operator *= ( const double d ); // multiplication by a constant
101 Point3D& operator /= ( const double d ); // division by a constant
103 void setx(const double x);
104 void sety(const double y);
105 void setz(const double z);
109 double& operator [] ( int i); // indexing
110 double operator[] (int i) const; // read-only indexing
112 inline const double *get_n() const { return n; };
113 double x() const; // cartesian x
114 double y() const; // cartesian y
115 double z() const; // cartesian z
117 double lon() const; // polar longitude
118 double lat() const; // polar latitude
119 double radius() const; // polar radius
120 double elev() const; // geodetic elevation (if specifying a surface point)
123 friend Point3D operator - (const Point3D& p); // -p1
124 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
125 friend istream& operator>> ( istream&, Point3D& );
126 friend ostream& operator<< ( ostream&, const Point3D& );
129 double distance3D(const Point3D& a) const; // distance between
130 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
136 operator >> ( istream& in, Point3D& p)
142 // read past optional comma
143 while ( in.get(c) ) {
144 if ( (c != ' ') && (c != ',') ) {
145 // push back on the stream
153 // read past optional comma
154 while ( in.get(c) ) {
155 if ( (c != ' ') && (c != ',') ) {
156 // push back on the stream
168 operator<< ( ostream& out, const Point3D& p )
170 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
173 ///////////////////////////
175 // Point3D Member functions
177 ///////////////////////////
181 inline Point3D::Point3D() {}
183 inline Point3D::Point3D(const double x, const double y, const double z)
185 n[PX] = x; n[PY] = y; n[PZ] = z;
188 inline Point3D::Point3D(const double d)
190 n[PX] = n[PY] = n[PZ] = d;
193 inline Point3D::Point3D(const Point3D& p)
195 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
198 // ASSIGNMENT OPERATORS
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 Point3D& p )
212 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
215 inline Point3D& Point3D::operator *= ( const double d )
217 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
220 inline Point3D& Point3D::operator /= ( const double d )
222 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
226 inline void Point3D::setx(const double x) {
230 inline void Point3D::sety(const double y) {
234 inline void Point3D::setz(const double z) {
240 inline double& Point3D::operator [] ( int i)
242 assert(! (i < PX || i > PZ));
246 inline double Point3D::operator [] ( int i) const {
247 assert(! (i < PX || i > PZ));
252 inline double Point3D::x() const { return n[PX]; }
254 inline double Point3D::y() const { return n[PY]; }
256 inline double Point3D::z() const { return n[PZ]; }
258 inline double Point3D::lon() const { return n[PX]; }
260 inline double Point3D::lat() const { return n[PY]; }
262 inline double Point3D::radius() const { return n[PZ]; }
264 inline double Point3D::elev() const { return n[PZ]; }
269 inline Point3D operator - (const Point3D& a)
271 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
274 inline Point3D operator + (const Point3D& a, const Point3D& b)
276 return Point3D(a) += b;
279 inline Point3D operator - (const Point3D& a, const Point3D& b)
281 return Point3D(a) -= b;
284 inline Point3D operator * (const Point3D& a, const double d)
286 return Point3D(a) *= d;
289 inline Point3D operator * (const double d, const Point3D& a)
294 inline Point3D operator / (const Point3D& a, const double d)
296 return Point3D(a) *= (1.0 / d );
299 inline bool operator == (const Point3D& a, const Point3D& b)
302 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
303 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
304 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
307 inline bool operator != (const Point3D& a, const Point3D& b)
315 Point3D::distance3D(const Point3D& a ) const
323 return sqrt(x*x + y*y + z*z);
328 Point3D::distance3Dsquared(const Point3D& a ) const
336 return(x*x + y*y + z*z);
340 #endif // _POINT3D_HXX