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 - http://www.flightgear.org/~curt
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 SG_USING_STD(ostream);
65 SG_USING_STD(istream);
68 const double fgPoint3_Epsilon = 0.0000001;
70 enum {PX, PY, PZ}; // axes
72 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
74 istream& operator>> ( istream&, Point3D& );
75 ostream& operator<< ( ostream&, const Point3D& );
76 Point3D operator- (const Point3D& p); // -p1
77 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
92 /** Default constructor */
94 Point3D(const double x, const double y, const double z);
95 explicit Point3D(const double d);
96 Point3D(const Point3D &p);
98 // Assignment operators
100 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
101 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
102 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
103 Point3D& operator *= ( const double d ); // multiplication by a constant
104 Point3D& operator /= ( const double d ); // division by a constant
106 void setx(const double x);
107 void sety(const double y);
108 void setz(const double z);
109 void setlon(const double x);
110 void setlat(const double y);
111 void setradius(const double z);
112 void setelev(const double z);
116 double& operator [] ( int i); // indexing
117 double operator[] (int i) const; // read-only indexing
119 inline const double *get_n() const { return n; };
120 double x() const; // cartesian x
121 double y() const; // cartesian y
122 double z() const; // cartesian z
124 double lon() const; // polar longitude
125 double lat() const; // polar latitude
126 double radius() const; // polar radius
127 double elev() const; // geodetic elevation (if specifying a surface point)
130 friend Point3D operator - (const Point3D& p); // -p1
131 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
132 friend istream& operator>> ( istream&, Point3D& );
133 friend ostream& operator<< ( ostream&, const Point3D& );
136 double distance3D(const Point3D& a) const; // distance between
137 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
143 operator >> ( istream& in, Point3D& p)
149 // read past optional comma
150 while ( in.get(c) ) {
151 if ( (c != ' ') && (c != ',') ) {
152 // push back on the stream
160 // read past optional comma
161 while ( in.get(c) ) {
162 if ( (c != ' ') && (c != ',') ) {
163 // push back on the stream
175 operator<< ( ostream& out, const Point3D& p )
177 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
180 ///////////////////////////
182 // Point3D Member functions
184 ///////////////////////////
188 inline Point3D::Point3D() {}
190 inline Point3D::Point3D(const double x, const double y, const double z)
192 n[PX] = x; n[PY] = y; n[PZ] = z;
195 inline Point3D::Point3D(const double d)
197 n[PX] = n[PY] = n[PZ] = d;
200 inline Point3D::Point3D(const Point3D& p)
202 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
205 // ASSIGNMENT OPERATORS
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 Point3D& p )
219 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
222 inline Point3D& Point3D::operator *= ( const double d )
224 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
227 inline Point3D& Point3D::operator /= ( const double d )
229 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
233 inline void Point3D::setx(const double x) {
237 inline void Point3D::sety(const double y) {
241 inline void Point3D::setz(const double z) {
245 inline void Point3D::setlon(const double x) {
249 inline void Point3D::setlat(const double y) {
253 inline void Point3D::setradius(const double z) {
257 inline void Point3D::setelev(const double z) {
263 inline double& Point3D::operator [] ( int i)
265 assert(! (i < PX || i > PZ));
269 inline double Point3D::operator [] ( int i) const {
270 assert(! (i < PX || i > PZ));
275 inline double Point3D::x() const { return n[PX]; }
277 inline double Point3D::y() const { return n[PY]; }
279 inline double Point3D::z() const { return n[PZ]; }
281 inline double Point3D::lon() const { return n[PX]; }
283 inline double Point3D::lat() const { return n[PY]; }
285 inline double Point3D::radius() const { return n[PZ]; }
287 inline double Point3D::elev() const { return n[PZ]; }
292 inline Point3D operator - (const Point3D& a)
294 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
297 inline Point3D operator + (const Point3D& a, const Point3D& b)
299 return Point3D(a) += b;
302 inline Point3D operator - (const Point3D& a, const Point3D& b)
304 return Point3D(a) -= b;
307 inline Point3D operator * (const Point3D& a, const double d)
309 return Point3D(a) *= d;
312 inline Point3D operator * (const double d, const Point3D& a)
317 inline Point3D operator / (const Point3D& a, const double d)
319 return Point3D(a) *= (1.0 / d );
322 inline bool operator == (const Point3D& a, const Point3D& b)
325 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
326 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
327 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
330 inline bool operator != (const Point3D& a, const Point3D& b)
338 Point3D::distance3D(const Point3D& a ) const
346 return sqrt(x*x + y*y + z*z);
351 Point3D::distance3Dsquared(const Point3D& a ) const
359 return(x*x + y*y + z*z);
363 #endif // _POINT3D_HXX