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 General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 # error This library requires C++
38 #include <simgear/compiler.h>
40 #ifdef SG_MATH_EXCEPTION_CLASH
41 # define exception c_exception
44 #ifdef SG_HAVE_STD_INCLUDES
49 # include <iostream.h>
56 // I don't understand ... <math.h> or <cmath> should be included
57 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
58 // can go ahead and add this -- CLO
60 SG_USING_NAMESPACE(std);
63 SG_USING_STD(ostream);
64 SG_USING_STD(istream);
67 const double fgPoint3_Epsilon = 0.0000001;
69 enum {PX, PY, PZ}; // axes
71 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
73 istream& operator>> ( istream&, Point3D& );
74 ostream& operator<< ( ostream&, const Point3D& );
75 Point3D operator- (const Point3D& p); // -p1
76 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
91 /** Default constructor */
93 Point3D(const double x, const double y, const double z);
94 explicit Point3D(const double d);
95 Point3D(const Point3D &p);
97 static Point3D fromSGGeod(const SGGeod& geod);
98 static Point3D fromSGGeoc(const SGGeoc& geoc);
99 static Point3D fromSGVec3(const SGVec3<double>& cart);
100 static Point3D fromSGVec3(const SGVec3<float>& cart);
101 static Point3D fromSGVec2(const SGVec2<double>& cart);
103 // Assignment operators
105 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
106 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
107 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
108 Point3D& operator *= ( const double d ); // multiplication by a constant
109 Point3D& operator /= ( const double d ); // division by a constant
111 void setx(const double x);
112 void sety(const double y);
113 void setz(const double z);
114 void setlon(const double x);
115 void setlat(const double y);
116 void setradius(const double z);
117 void setelev(const double z);
121 double& operator [] ( int i); // indexing
122 double operator[] (int i) const; // read-only indexing
124 inline const double *get_n() const { return n; };
125 double x() const; // cartesian x
126 double y() const; // cartesian y
127 double z() const; // cartesian z
129 double lon() const; // polar longitude
130 double lat() const; // polar latitude
131 double radius() const; // polar radius
132 double elev() const; // geodetic elevation (if specifying a surface point)
134 SGGeod toSGGeod(void) const;
135 SGGeoc toSGGeoc(void) const;
137 SGVec3d toSGVec3d(void) const;
138 SGVec3f toSGVec3f(void) const;
139 SGVec2f toSGVec2f(void) const;
142 friend Point3D operator - (const Point3D& p); // -p1
143 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
144 friend istream& operator>> ( istream&, Point3D& );
145 friend ostream& operator<< ( ostream&, const Point3D& );
148 double distance3D(const Point3D& a) const; // distance between
149 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
155 operator >> ( istream& in, Point3D& p)
161 // read past optional comma
162 while ( in.get(c) ) {
163 if ( (c != ' ') && (c != ',') ) {
164 // push back on the stream
172 // read past optional comma
173 while ( in.get(c) ) {
174 if ( (c != ' ') && (c != ',') ) {
175 // push back on the stream
187 operator<< ( ostream& out, const Point3D& p )
189 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
192 ///////////////////////////
194 // Point3D Member functions
196 ///////////////////////////
200 inline Point3D::Point3D()
206 inline Point3D::Point3D(const double x, const double y, const double z)
208 n[PX] = x; n[PY] = y; n[PZ] = z;
211 inline Point3D::Point3D(const double d)
213 n[PX] = n[PY] = n[PZ] = d;
216 inline Point3D::Point3D(const Point3D& p)
218 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
221 inline Point3D Point3D::fromSGGeod(const SGGeod& geod)
224 pt.setlon(geod.getLongitudeRad());
225 pt.setlat(geod.getLatitudeRad());
226 pt.setelev(geod.getElevationM());
230 inline Point3D Point3D::fromSGGeoc(const SGGeoc& geoc)
233 pt.setlon(geoc.getLongitudeRad());
234 pt.setlat(geoc.getLatitudeRad());
235 pt.setradius(geoc.getRadiusM());
239 inline Point3D Point3D::fromSGVec3(const SGVec3<double>& cart)
248 inline Point3D Point3D::fromSGVec3(const SGVec3<float>& cart)
257 inline Point3D Point3D::fromSGVec2(const SGVec2<double>& cart)
266 // ASSIGNMENT OPERATORS
268 inline Point3D& Point3D::operator = (const Point3D& p)
270 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
273 inline Point3D& Point3D::operator += ( const Point3D& p )
275 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
278 inline Point3D& Point3D::operator -= ( const Point3D& p )
280 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
283 inline Point3D& Point3D::operator *= ( const double d )
285 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
288 inline Point3D& Point3D::operator /= ( const double d )
290 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
294 inline void Point3D::setx(const double x) {
298 inline void Point3D::sety(const double y) {
302 inline void Point3D::setz(const double z) {
306 inline void Point3D::setlon(const double x) {
310 inline void Point3D::setlat(const double y) {
314 inline void Point3D::setradius(const double z) {
318 inline void Point3D::setelev(const double z) {
324 inline double& Point3D::operator [] ( int i)
326 assert(! (i < PX || i > PZ));
330 inline double Point3D::operator [] ( int i) const {
331 assert(! (i < PX || i > PZ));
336 inline double Point3D::x() const { return n[PX]; }
338 inline double Point3D::y() const { return n[PY]; }
340 inline double Point3D::z() const { return n[PZ]; }
342 inline double Point3D::lon() const { return n[PX]; }
344 inline double Point3D::lat() const { return n[PY]; }
346 inline double Point3D::radius() const { return n[PZ]; }
348 inline double Point3D::elev() const { return n[PZ]; }
350 inline SGGeod Point3D::toSGGeod(void) const
353 geod.setLongitudeRad(lon());
354 geod.setLatitudeRad(lat());
355 geod.setElevationM(elev());
359 inline SGGeoc Point3D::toSGGeoc(void) const
362 geoc.setLongitudeRad(lon());
363 geoc.setLatitudeRad(lat());
364 geoc.setRadiusM(radius());
368 inline SGVec3d Point3D::toSGVec3d(void) const
370 return SGVec3d(x(), y(), z());
373 inline SGVec3f Point3D::toSGVec3f(void) const
375 return SGVec3f(x(), y(), z());
378 inline SGVec2f Point3D::toSGVec2f(void) const
380 return SGVec2f(x(), y());
385 inline Point3D operator - (const Point3D& a)
387 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
390 inline Point3D operator + (const Point3D& a, const Point3D& b)
392 return Point3D(a) += b;
395 inline Point3D operator - (const Point3D& a, const Point3D& b)
397 return Point3D(a) -= b;
400 inline Point3D operator * (const Point3D& a, const double d)
402 return Point3D(a) *= d;
405 inline Point3D operator * (const double d, const Point3D& a)
410 inline Point3D operator / (const Point3D& a, const double d)
412 return Point3D(a) *= (1.0 / d );
415 inline bool operator == (const Point3D& a, const Point3D& b)
418 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
419 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
420 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
423 inline bool operator != (const Point3D& a, const Point3D& b)
431 Point3D::distance3D(const Point3D& a ) const
439 return sqrt(x*x + y*y + z*z);
444 Point3D::distance3Dsquared(const Point3D& a ) const
452 return(x*x + y*y + z*z);
456 #endif // _POINT3D_HXX