head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.56; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.02; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.58; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# point3d.hxx

Go to the documentation of this file.

    00001
    00011 // Copyright (C) 1998  Curtis L. Olson  - curt@@me.umn.edu
    00012 //
    00013 // This library is free software; you can redistribute it and/or
    00014 // modify it under the terms of the GNU Library General Public
    00015 // License as published by the Free Software Foundation; either
    00016 // version 2 of the License, or (at your option) any later version.
    00017 //
    00018 // This library is distributed in the hope that it will be useful,
    00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00021 // Library General Public License for more details.
    00022 //
    00023 // You should have received a copy of the GNU Library General Public
    00024 // License along with this library; if not, write to the
    00025 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00026 // Boston, MA  02111-1307, USA.
    00027 //
    00028 // $Id: point3d.hxx,v 1.8 2001/04/24 20:01:52 curt Exp $
    00029
    00030
    00031 #ifndef _POINT3D_HXX
    00032 #define _POINT3D_HXX
    00033
    00034
    00035 #ifndef __cplusplus
    00036 # error This library requires C++
    00037 #endif
    00038
    00039 #include <simgear/compiler.h>
    00040
    00041 #ifdef SG_MATH_EXCEPTION_CLASH
    00042 # define exception c_exception
    00043 #endif
    00044
    00045 #ifdef SG_HAVE_STD_INCLUDES
    00046 # include <iostream>
    00047 # include <cassert>
    00048 # include <cmath>
    00049 #else
    00050 # include <iostream.h>
    00051 # include <assert.h>
    00052 # include <math.h>
    00053 #endif
    00054
    00055 #include <simgear/math/localconsts.hxx>
    00056
    00057 // I don't understand ... <math.h> or <cmath> should be included
    00058 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
    00059 // can go ahead and add this -- CLO
    00060 #ifdef __MWERKS__
    00061 SG_USING_NAMESPACE(std);
    00062 #endif
    00063
    00064 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
    00065 SG_USING_STD(ostream);
    00066 SG_USING_STD(istream);
    00067 #endif
    00068
    00069
    00070 const double fgPoint3_Epsilon = 0.0000001;
    00071
    00072 enum {PX, PY, PZ};                  // axes
    00073
    00074 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
    00075 class Point3D;
    00076 istream& operator>> ( istream&, Point3D& );
    00077 ostream& operator<< ( ostream&, const Point3D& );
    00078 Point3D operator- (const Point3D& p);               // -p1
    00079 bool operator== (const Point3D& a, const Point3D& b);  // p1 == p2?
    00080
    00081
    00086 class Point3D {
    00087
    00088 protected:
    00089
    00090     double n[3];
    00091
    00092 public:
    00093
    00095     Point3D();
    00096     Point3D(const double x, const double y, const double z);
    00097     explicit Point3D(const double d);
    00098     Point3D(const Point3D &p);
    00099
    00100     // Assignment operators
    00101
    00102     Point3D& operator = ( const Point3D& p );    // assignment of a Point3D
    00103     Point3D& operator += ( const Point3D& p );   // incrementation by a Point3D
    00104     Point3D& operator -= ( const Point3D& p );   // decrementation by a Point3D
    00105     Point3D& operator *= ( const double d );     // multiplication by a constant
    00106     Point3D& operator /= ( const double d );     // division by a constant
    00107
    00108     void setx(const double x);
    00109     void sety(const double y);
    00110     void setz(const double z);
    00111     void setlon(const double x);
    00112     void setlat(const double y);
    00113     void setradius(const double z);
    00114     void setelev(const double z);
    00115
    00116     // Queries
    00117
    00118     double& operator [] ( int i);                // indexing
    00119     double operator[] (int i) const;             // read-only indexing
    00120
    00121     inline const double *get_n() const { return n; };
    00122     double x() const;      // cartesian x
    00123     double y() const;      // cartesian y
    00124     double z() const;      // cartesian z
    00125
    00126     double lon() const;    // polar longitude
    00127     double lat() const;    // polar latitude
    00128     double radius() const; // polar radius
    00129     double elev() const;   // geodetic elevation (if specifying a surface point)
    00130
    00131     // friends
    00132     friend Point3D operator - (const Point3D& p);                   // -p1
    00133     friend bool operator == (const Point3D& a, const Point3D& b);  // p1 == p2?
    00134     friend istream& operator>> ( istream&, Point3D& );
    00135     friend ostream& operator<< ( ostream&, const Point3D& );
    00136
    00137     // Special functions
    00138     double distance3D(const Point3D& a) const;        // distance between
    00139     double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
    00140 };
    00141
    00142
    00143 // input from stream
    00144 inline istream&
    00145 operator >> ( istream& in, Point3D& p)
    00146 {
    00147     char c;
    00148
    00149     in >> p.n[PX];
    00150
    00151     // read past optional comma
    00152     while ( in.get(c) ) {
    00153         if ( (c != ' ') && (c != ',') ) {
    00154             // push back on the stream
    00155             in.putback(c);
    00156             break;
    00157         }
    00158     }
    00159
    00160     in >> p.n[PY];
    00161
    00162     // read past optional comma
    00163     while ( in.get(c) ) {
    00164         if ( (c != ' ') && (c != ',') ) {
    00165             // push back on the stream
    00166             in.putback(c);
    00167             break;
    00168         }
    00169     }
    00170
    00171     in >> p.n[PZ];
    00172
    00173     return in;
    00174 }
    00175
    00176 inline ostream&
    00177 operator<< ( ostream& out, const Point3D& p )
    00178 {
    00179     return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
    00180 }
    00181
    00183 //
    00184 // Point3D Member functions
    00185 //
    00187
    00188 // CONSTRUCTORS
    00189
    00190 inline Point3D::Point3D() {}
    00191
    00192 inline Point3D::Point3D(const double x, const double y, const double z)
    00193 {
    00194     n[PX] = x; n[PY] = y; n[PZ] = z;
    00195 }
    00196
    00197 inline Point3D::Point3D(const double d)
    00198 {
    00199     n[PX] = n[PY] = n[PZ] = d;
    00200 }
    00201
    00202 inline Point3D::Point3D(const Point3D& p)
    00203 {
    00204     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
    00205 }
    00206
    00207 // ASSIGNMENT OPERATORS
    00208
    00209 inline Point3D& Point3D::operator = (const Point3D& p)
    00210 {
    00211     n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
    00212 }
    00213
    00214 inline Point3D& Point3D::operator += ( const Point3D& p )
    00215 {
    00216     n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
    00217 }
    00218
    00219 inline Point3D& Point3D::operator -= ( const Point3D& p )
    00220 {
    00221     n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
    00222 }
    00223
    00224 inline Point3D& Point3D::operator *= ( const double d )
    00225 {
    00226     n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
    00227 }
    00228
    00229 inline Point3D& Point3D::operator /= ( const double d )
    00230 {
    00231     double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
    00232     return *this;
    00233 }
    00234
    00235 inline void Point3D::setx(const double x) {
    00236     n[PX] = x;
    00237 }
    00238
    00239 inline void Point3D::sety(const double y) {
    00240     n[PY] = y;
    00241 }
    00242
    00243 inline void Point3D::setz(const double z) {
    00244     n[PZ] = z;
    00245 }
    00246
    00247 inline void Point3D::setlon(const double x) {
    00248     n[PX] = x;
    00249 }
    00250
    00251 inline void Point3D::setlat(const double y) {
    00252     n[PY] = y;
    00253 }
    00254
    00255 inline void Point3D::setradius(const double z) {
    00256     n[PZ] = z;
    00257 }
    00258
    00259 inline void Point3D::setelev(const double z) {
    00260     n[PZ] = z;
    00261 }
    00262
    00263 // QUERIES
    00264
    00265 inline double& Point3D::operator [] ( int i)
    00266 {
    00267     assert(! (i < PX || i > PZ));
    00268     return n[i];
    00269 }
    00270
    00271 inline double Point3D::operator [] ( int i) const {
    00272     assert(! (i < PX || i > PZ));
    00273     return n[i];
    00274 }
    00275
    00276
    00277 inline double Point3D::x() const { return n[PX]; }
    00278
    00279 inline double Point3D::y() const { return n[PY]; }
    00280
    00281 inline double Point3D::z() const { return n[PZ]; }
    00282
    00283 inline double Point3D::lon() const { return n[PX]; }
    00284
    00285 inline double Point3D::lat() const { return n[PY]; }
    00286
    00287 inline double Point3D::radius() const { return n[PZ]; }
    00288
    00289 inline double Point3D::elev() const { return n[PZ]; }
    00290
    00291
    00292 // FRIENDS
    00293
    00294 inline Point3D operator - (const Point3D& a)
    00295 {
    00296     return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
    00297 }
    00298
    00299 inline Point3D operator + (const Point3D& a, const Point3D& b)
    00300 {
    00301     return Point3D(a) += b;
    00302 }
    00303
    00304 inline Point3D operator - (const Point3D& a, const Point3D& b)
    00305 {
    00306     return Point3D(a) -= b;
    00307 }
    00308
    00309 inline Point3D operator * (const Point3D& a, const double d)
    00310 {
    00311     return Point3D(a) *= d;
    00312 }
    00313
    00314 inline Point3D operator * (const double d, const Point3D& a)
    00315 {
    00316     return a*d;
    00317 }
    00318
    00319 inline Point3D operator / (const Point3D& a, const double d)
    00320 {
    00321     return Point3D(a) *= (1.0 / d );
    00322 }
    00323
    00324 inline bool operator == (const Point3D& a, const Point3D& b)
    00325 {
    00326     return
    00327         fabs(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
    00328         fabs(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
    00329         fabs(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
    00330 }
    00331
    00332 inline bool operator != (const Point3D& a, const Point3D& b)
    00333 {
    00334     return !(a == b);
    00335 }
    00336
    00337 // Special functions
    00338
    00339 inline double
    00340 Point3D::distance3D(const Point3D& a ) const
    00341 {
    00342     double x, y, z;
    00343
    00344     x = n[PX] - a.n[PX];
    00345     y = n[PY] - a.n[PY];
    00346     z = n[PZ] - a.n[PZ];
    00347
    00348     return sqrt(x*x + y*y + z*z);
    00349 }
    00350
    00351
    00352 inline double
    00353 Point3D::distance3Dsquared(const Point3D& a ) const
    00354 {
    00355     double x, y, z;
    00356
    00357     x = n[PX] - a.n[PX];
    00358     y = n[PY] - a.n[PY];
    00359     z = n[PZ] - a.n[PZ];
    00360
    00361     return(x*x + y*y + z*z);
    00362 }
    00363
    00364
    00365 #endif // _POINT3D_HXX
    00366
    00367

@ 1.3 log @Oops, try again. @ text @d360 1 a360 1