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);
111 void setlon(const double x);
112 void setlat(const double y);
113 void setradius(const double z);
114 void setelev(const double z);
118 double& operator [] ( int i); // indexing
119 double operator[] (int i) const; // read-only indexing
121 inline const double *get_n() const { return n; };
122 double x() const; // cartesian x
123 double y() const; // cartesian y
124 double z() const; // cartesian z
126 double lon() const; // polar longitude
127 double lat() const; // polar latitude
128 double radius() const; // polar radius
129 double elev() const; // geodetic elevation (if specifying a surface point)
132 friend Point3D operator - (const Point3D& p); // -p1
133 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
134 friend istream& operator>> ( istream&, Point3D& );
135 friend ostream& operator<< ( ostream&, const Point3D& );
138 double distance3D(const Point3D& a) const; // distance between
139 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
145 operator >> ( istream& in, Point3D& p)
151 // read past optional comma
152 while ( in.get(c) ) {
153 if ( (c != ' ') && (c != ',') ) {
154 // push back on the stream
162 // read past optional comma
163 while ( in.get(c) ) {
164 if ( (c != ' ') && (c != ',') ) {
165 // push back on the stream
177 operator<< ( ostream& out, const Point3D& p )
179 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
182 ///////////////////////////
184 // Point3D Member functions
186 ///////////////////////////
190 inline Point3D::Point3D() {}
192 inline Point3D::Point3D(const double x, const double y, const double z)
194 n[PX] = x; n[PY] = y; n[PZ] = z;
197 inline Point3D::Point3D(const double d)
199 n[PX] = n[PY] = n[PZ] = d;
202 inline Point3D::Point3D(const Point3D& p)
204 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
207 // ASSIGNMENT OPERATORS
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 Point3D& p )
216 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
219 inline Point3D& Point3D::operator -= ( const Point3D& p )
221 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
224 inline Point3D& Point3D::operator *= ( const double d )
226 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
229 inline Point3D& Point3D::operator /= ( const double d )
231 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
235 inline void Point3D::setx(const double x) {
239 inline void Point3D::sety(const double y) {
243 inline void Point3D::setz(const double z) {
247 inline void Point3D::setlon(const double x) {
251 inline void Point3D::setlat(const double y) {
255 inline void Point3D::setradius(const double z) {
259 inline void Point3D::setelev(const double z) {
265 inline double& Point3D::operator [] ( int i)
267 assert(! (i < PX || i > PZ));
271 inline double Point3D::operator [] ( int i) const {
272 assert(! (i < PX || i > PZ));
277 inline double Point3D::x() const { return n[PX]; }
279 inline double Point3D::y() const { return n[PY]; }
281 inline double Point3D::z() const { return n[PZ]; }
283 inline double Point3D::lon() const { return n[PX]; }
285 inline double Point3D::lat() const { return n[PY]; }
287 inline double Point3D::radius() const { return n[PZ]; }
289 inline double Point3D::elev() const { return n[PZ]; }
294 inline Point3D operator - (const Point3D& a)
296 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
299 inline Point3D operator + (const Point3D& a, const Point3D& b)
301 return Point3D(a) += b;
304 inline Point3D operator - (const Point3D& a, const Point3D& b)
306 return Point3D(a) -= b;
309 inline Point3D operator * (const Point3D& a, const double d)
311 return Point3D(a) *= d;
314 inline Point3D operator * (const double d, const Point3D& a)
319 inline Point3D operator / (const Point3D& a, const double d)
321 return Point3D(a) *= (1.0 / d );
324 inline bool operator == (const Point3D& a, const Point3D& b)
327 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
328 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
329 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
332 inline bool operator != (const Point3D& a, const Point3D& b)
340 Point3D::distance3D(const Point3D& a ) const
348 return sqrt(x*x + y*y + z*z);
353 Point3D::distance3Dsquared(const Point3D& a ) const
361 return(x*x + y*y + z*z);
365 #endif // _POINT3D_HXX