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>
47 const double fgPoint3_Epsilon = 0.0000001;
49 enum {PX, PY, PZ}; // axes
51 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
53 std::istream& operator>> ( std::istream&, Point3D& );
54 std::ostream& operator<< ( std::ostream&, const Point3D& );
55 Point3D operator- (const Point3D& p); // -p1
56 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
71 /** Default constructor */
73 Point3D(const double x, const double y, const double z);
74 explicit Point3D(const double d);
75 Point3D(const Point3D &p);
77 static Point3D fromSGGeod(const SGGeod& geod);
78 static Point3D fromSGGeoc(const SGGeoc& geoc);
79 static Point3D fromSGVec3(const SGVec3<double>& cart);
80 static Point3D fromSGVec3(const SGVec3<float>& cart);
81 static Point3D fromSGVec2(const SGVec2<double>& cart);
83 // Assignment operators
85 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
86 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
87 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
88 Point3D& operator *= ( const double d ); // multiplication by a constant
89 Point3D& operator /= ( const double d ); // division by a constant
91 void setx(const double x);
92 void sety(const double y);
93 void setz(const double z);
94 void setlon(const double x);
95 void setlat(const double y);
96 void setradius(const double z);
97 void setelev(const double z);
101 double& operator [] ( int i); // indexing
102 double operator[] (int i) const; // read-only indexing
104 inline const double *get_n() const { return n; };
105 double x() const; // cartesian x
106 double y() const; // cartesian y
107 double z() const; // cartesian z
109 double lon() const; // polar longitude
110 double lat() const; // polar latitude
111 double radius() const; // polar radius
112 double elev() const; // geodetic elevation (if specifying a surface point)
114 SGGeod toSGGeod(void) const;
115 SGGeoc toSGGeoc(void) const;
117 SGVec3d toSGVec3d(void) const;
118 SGVec3f toSGVec3f(void) const;
119 SGVec2f toSGVec2f(void) const;
122 friend Point3D operator - (const Point3D& p); // -p1
123 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
124 friend std::istream& operator>> ( std::istream&, Point3D& );
125 friend std::ostream& operator<< ( std::ostream&, const Point3D& );
128 double distance3D(const Point3D& a) const; // distance between
129 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
135 operator >> ( std::istream& in, Point3D& p)
141 // read past optional comma
142 while ( in.get(c) ) {
143 if ( (c != ' ') && (c != ',') ) {
144 // push back on the stream
152 // read past optional comma
153 while ( in.get(c) ) {
154 if ( (c != ' ') && (c != ',') ) {
155 // push back on the stream
167 operator<< ( std::ostream& out, const Point3D& p )
169 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
172 ///////////////////////////
174 // Point3D Member functions
176 ///////////////////////////
180 inline Point3D::Point3D()
186 inline Point3D::Point3D(const double x, const double y, const double z)
188 n[PX] = x; n[PY] = y; n[PZ] = z;
191 inline Point3D::Point3D(const double d)
193 n[PX] = n[PY] = n[PZ] = d;
196 inline Point3D::Point3D(const Point3D& p)
198 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
201 inline Point3D Point3D::fromSGGeod(const SGGeod& geod)
204 pt.setlon(geod.getLongitudeRad());
205 pt.setlat(geod.getLatitudeRad());
206 pt.setelev(geod.getElevationM());
210 inline Point3D Point3D::fromSGGeoc(const SGGeoc& geoc)
213 pt.setlon(geoc.getLongitudeRad());
214 pt.setlat(geoc.getLatitudeRad());
215 pt.setradius(geoc.getRadiusM());
219 inline Point3D Point3D::fromSGVec3(const SGVec3<double>& cart)
228 inline Point3D Point3D::fromSGVec3(const SGVec3<float>& cart)
237 inline Point3D Point3D::fromSGVec2(const SGVec2<double>& cart)
246 // ASSIGNMENT OPERATORS
248 inline Point3D& Point3D::operator = (const Point3D& p)
250 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
253 inline Point3D& Point3D::operator += ( const Point3D& p )
255 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
258 inline Point3D& Point3D::operator -= ( const Point3D& p )
260 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
263 inline Point3D& Point3D::operator *= ( const double d )
265 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
268 inline Point3D& Point3D::operator /= ( const double d )
270 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
274 inline void Point3D::setx(const double x) {
278 inline void Point3D::sety(const double y) {
282 inline void Point3D::setz(const double z) {
286 inline void Point3D::setlon(const double x) {
290 inline void Point3D::setlat(const double y) {
294 inline void Point3D::setradius(const double z) {
298 inline void Point3D::setelev(const double z) {
304 inline double& Point3D::operator [] ( int i)
306 assert(! (i < PX || i > PZ));
310 inline double Point3D::operator [] ( int i) const {
311 assert(! (i < PX || i > PZ));
316 inline double Point3D::x() const { return n[PX]; }
318 inline double Point3D::y() const { return n[PY]; }
320 inline double Point3D::z() const { return n[PZ]; }
322 inline double Point3D::lon() const { return n[PX]; }
324 inline double Point3D::lat() const { return n[PY]; }
326 inline double Point3D::radius() const { return n[PZ]; }
328 inline double Point3D::elev() const { return n[PZ]; }
330 inline SGGeod Point3D::toSGGeod(void) const
333 geod.setLongitudeRad(lon());
334 geod.setLatitudeRad(lat());
335 geod.setElevationM(elev());
339 inline SGGeoc Point3D::toSGGeoc(void) const
342 geoc.setLongitudeRad(lon());
343 geoc.setLatitudeRad(lat());
344 geoc.setRadiusM(radius());
348 inline SGVec3d Point3D::toSGVec3d(void) const
350 return SGVec3d(x(), y(), z());
353 inline SGVec3f Point3D::toSGVec3f(void) const
355 return SGVec3f(x(), y(), z());
358 inline SGVec2f Point3D::toSGVec2f(void) const
360 return SGVec2f(x(), y());
365 inline Point3D operator - (const Point3D& a)
367 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
370 inline Point3D operator + (const Point3D& a, const Point3D& b)
372 return Point3D(a) += b;
375 inline Point3D operator - (const Point3D& a, const Point3D& b)
377 return Point3D(a) -= b;
380 inline Point3D operator * (const Point3D& a, const double d)
382 return Point3D(a) *= d;
385 inline Point3D operator * (const double d, const Point3D& a)
390 inline Point3D operator / (const Point3D& a, const double d)
392 return Point3D(a) *= (1.0 / d );
395 inline bool operator == (const Point3D& a, const Point3D& b)
398 fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
399 fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
400 fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
403 inline bool operator != (const Point3D& a, const Point3D& b)
411 Point3D::distance3D(const Point3D& a ) const
419 return sqrt(x*x + y*y + z*z);
424 Point3D::distance3Dsquared(const Point3D& a ) const
432 return(x*x + y*y + z*z);
436 #endif // _POINT3D_HXX