3 * A 3d point class (depricated). This class is depricated and we are
4 * in the process of removing all usage of it in favor of plib's "sg"
5 * library of point, vector, and math routines. Plib's sg lib is less
6 * object oriented, but integrates more seamlessly with opengl.
8 * Adapted from algebra3 by Jean-Francois Doue, started October 1998.
11 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Library General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Library General Public License for more details.
23 // You should have received a copy of the GNU Library General Public
24 // License along with this library; if not, write to the
25 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 // Boston, MA 02111-1307, USA.
36 # error This library requires C++
39 #include <simgear/compiler.h>
41 #ifdef SG_MATH_EXCEPTION_CLASH
42 # define exception c_exception
45 #ifdef SG_HAVE_STD_INCLUDES
50 # include <iostream.h>
55 #include <simgear/math/localconsts.hxx>
57 // I don't understand ... <math.h> or <cmath> should be included
58 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
59 // can go ahead and add this -- CLO
61 SG_USING_NAMESPACE(std);
64 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
65 SG_USING_STD(ostream);
66 SG_USING_STD(istream);
70 const double fgPoint3_Epsilon = 0.0000001;
72 enum {PX, PY, PZ}; // axes
74 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
76 istream& operator>> ( istream&, Point3D& );
77 ostream& operator<< ( ostream&, const Point3D& );
78 Point3D operator- (const Point3D& p); // -p1
79 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
94 /** Default constructor */
96 Point3D(const double x, const double y, const double z);
97 explicit Point3D(const double d);
98 Point3D(const Point3D &p);
100 // Assignment operators
102 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
103 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
104 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
105 Point3D& operator *= ( const double d ); // multiplication by a constant
106 Point3D& operator /= ( const double d ); // division by a constant
108 void setx(const double x);
109 void sety(const double y);
110 void setz(const double z);
114 double& operator [] ( int i); // indexing
115 double operator[] (int i) const; // read-only indexing
117 inline const double *get_n() const { return n; };
118 double x() const; // cartesian x
119 double y() const; // cartesian y
120 double z() const; // cartesian z
122 double lon() const; // polar longitude
123 double lat() const; // polar latitude
124 double radius() const; // polar radius
125 double elev() const; // geodetic elevation (if specifying a surface point)
128 friend Point3D operator - (const Point3D& p); // -p1
129 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
130 friend istream& operator>> ( istream&, Point3D& );
131 friend ostream& operator<< ( ostream&, const Point3D& );
134 double distance3D(const Point3D& a) const; // distance between
135 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
141 operator >> ( istream& in, Point3D& p)
147 // read past optional comma
148 while ( in.get(c) ) {
149 if ( (c != ' ') && (c != ',') ) {
150 // push back on the stream
158 // read past optional comma
159 while ( in.get(c) ) {
160 if ( (c != ' ') && (c != ',') ) {
161 // push back on the stream
173 operator<< ( ostream& out, const Point3D& p )
175 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
178 ///////////////////////////
180 // Point3D Member functions
182 ///////////////////////////
186 inline Point3D::Point3D() {}
188 inline Point3D::Point3D(const double x, const double y, const double z)
190 n[PX] = x; n[PY] = y; n[PZ] = z;
193 inline Point3D::Point3D(const double d)
195 n[PX] = n[PY] = n[PZ] = d;
198 inline Point3D::Point3D(const Point3D& p)
200 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
203 // ASSIGNMENT OPERATORS
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 Point3D& p )
217 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
220 inline Point3D& Point3D::operator *= ( const double d )
222 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
225 inline Point3D& Point3D::operator /= ( const double d )
227 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
231 inline void Point3D::setx(const double x) {
235 inline void Point3D::sety(const double y) {
239 inline void Point3D::setz(const double z) {
245 inline double& Point3D::operator [] ( int i)
247 assert(! (i < PX || i > PZ));
251 inline double Point3D::operator [] ( int i) const {
252 assert(! (i < PX || i > PZ));
257 inline double Point3D::x() const { return n[PX]; }
259 inline double Point3D::y() const { return n[PY]; }
261 inline double Point3D::z() const { return n[PZ]; }
263 inline double Point3D::lon() const { return n[PX]; }
265 inline double Point3D::lat() const { return n[PY]; }
267 inline double Point3D::radius() const { return n[PZ]; }
269 inline double Point3D::elev() const { return n[PZ]; }
274 inline Point3D operator - (const Point3D& a)
276 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
279 inline Point3D operator + (const Point3D& a, const Point3D& b)
281 return Point3D(a) += b;
284 inline Point3D operator - (const Point3D& a, const Point3D& b)
286 return Point3D(a) -= b;
289 inline Point3D operator * (const Point3D& a, const double d)
291 return Point3D(a) *= d;
294 inline Point3D operator * (const double d, const Point3D& a)
299 inline Point3D operator / (const Point3D& a, const double d)
301 return Point3D(a) *= (1.0 / d );
304 inline bool operator == (const Point3D& a, const Point3D& b)
307 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
308 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
309 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
312 inline bool operator != (const Point3D& a, const Point3D& b)
320 Point3D::distance3D(const Point3D& a ) const
328 return sqrt(x*x + y*y + z*z);
333 Point3D::distance3Dsquared(const Point3D& a ) const
341 return(x*x + y*y + z*z);
345 #endif // _POINT3D_HXX