1998-10-16 00:50:29 +00:00
|
|
|
// point3d.hxx -- a 3d point class.
|
|
|
|
//
|
|
|
|
// Adapted from algebra3 by Jean-Francois Doue, started October 1998.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _POINT3D_HXX
|
|
|
|
#define _POINT3D_HXX
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
|
1998-10-16 00:50:29 +00:00
|
|
|
#ifndef __cplusplus
|
|
|
|
# error This library requires C++
|
|
|
|
#endif
|
|
|
|
|
1999-04-27 15:54:39 +00:00
|
|
|
#include <Include/compiler.h>
|
1998-10-16 00:50:29 +00:00
|
|
|
|
1999-01-27 04:46:14 +00:00
|
|
|
#ifdef FG_MATH_EXCEPTION_CLASH
|
|
|
|
# define exception c_exception
|
1998-11-23 21:46:36 +00:00
|
|
|
#endif
|
1998-10-16 00:50:29 +00:00
|
|
|
|
1999-01-27 04:46:14 +00:00
|
|
|
#ifdef FG_HAVE_STD_INCLUDES
|
|
|
|
# include <iostream>
|
|
|
|
# include <cassert>
|
|
|
|
# include <cmath>
|
|
|
|
#else
|
|
|
|
# include <iostream.h>
|
|
|
|
# include <assert.h>
|
|
|
|
# include <math.h>
|
|
|
|
#endif
|
|
|
|
|
1999-03-02 01:01:42 +00:00
|
|
|
#ifndef FG_HAVE_NATIVE_SGI_COMPILERS
|
1999-01-27 04:46:14 +00:00
|
|
|
FG_USING_STD(ostream);
|
|
|
|
FG_USING_STD(istream);
|
1999-03-02 01:01:42 +00:00
|
|
|
#endif
|
1999-01-27 04:46:14 +00:00
|
|
|
|
1999-04-27 15:54:39 +00:00
|
|
|
// -dw- someone seems to have forgotten this...
|
1999-01-19 20:56:53 +00:00
|
|
|
#ifdef __MWERKS__
|
1999-04-27 15:54:39 +00:00
|
|
|
FG_USING_STD(std);
|
1999-01-19 20:56:53 +00:00
|
|
|
#endif
|
|
|
|
|
1999-04-27 15:54:39 +00:00
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
const double fgPoint3_Epsilon = 0.0000001;
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
enum {PX, PY, PZ}; // axes
|
|
|
|
|
1999-01-27 04:46:14 +00:00
|
|
|
// Kludge for msvc++ 6.0 - requires forward decls of friend functions.
|
|
|
|
class Point3D;
|
|
|
|
istream& operator>> ( istream&, Point3D& );
|
|
|
|
ostream& operator<< ( ostream&, const Point3D& );
|
|
|
|
Point3D operator- (const Point3D& p); // -p1
|
|
|
|
bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
|
|
|
|
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
//
|
|
|
|
// 3D Point
|
|
|
|
//
|
|
|
|
///////////////////////////
|
|
|
|
|
|
|
|
class Point3D {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
double n[3];
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
|
|
|
|
Point3D();
|
|
|
|
Point3D(const double x, const double y, const double z);
|
1998-10-20 18:21:49 +00:00
|
|
|
explicit Point3D(const double d);
|
1998-10-16 00:50:29 +00:00
|
|
|
Point3D(const Point3D &p);
|
|
|
|
|
|
|
|
// Assignment operators
|
|
|
|
|
|
|
|
Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
|
|
|
|
Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
|
|
|
|
Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
|
1998-10-20 18:21:49 +00:00
|
|
|
Point3D& operator *= ( const double d ); // multiplication by a constant
|
1998-10-16 00:50:29 +00:00
|
|
|
Point3D& operator /= ( const double d ); // division by a constant
|
1998-10-18 01:17:11 +00:00
|
|
|
|
1998-10-16 00:50:29 +00:00
|
|
|
void setx(const double x);
|
|
|
|
void sety(const double y);
|
|
|
|
void setz(const double z);
|
|
|
|
|
|
|
|
// Queries
|
|
|
|
|
|
|
|
double& operator [] ( int i); // indexing
|
|
|
|
double operator[] (int i) const; // read-only indexing
|
|
|
|
|
|
|
|
double x() const; // cartesian x
|
|
|
|
double y() const; // cartesian y
|
|
|
|
double z() const; // cartesian z
|
|
|
|
|
|
|
|
double lon() const; // polar longitude
|
|
|
|
double lat() const; // polar latitude
|
|
|
|
double radius() const; // polar radius
|
|
|
|
double elev() const; // geodetic elevation (if specifying a surface point)
|
|
|
|
|
|
|
|
// friends
|
|
|
|
friend Point3D operator - (const Point3D& p); // -p1
|
1998-10-20 18:21:49 +00:00
|
|
|
friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
|
|
|
|
friend istream& operator>> ( istream&, Point3D& );
|
|
|
|
friend ostream& operator<< ( ostream&, const Point3D& );
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
// Special functions
|
1999-02-01 21:08:28 +00:00
|
|
|
double distance3D(const Point3D& a) const; // distance between
|
|
|
|
double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
|
1998-10-16 00:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// input from stream
|
|
|
|
inline istream&
|
|
|
|
operator >> ( istream& in, Point3D& p)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
in >> p.n[PX];
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
// read past optional comma
|
|
|
|
while ( in.get(c) ) {
|
|
|
|
if ( (c != ' ') && (c != ',') ) {
|
|
|
|
// push back on the stream
|
|
|
|
in.putback(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
in >> p.n[PY];
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
// read past optional comma
|
|
|
|
while ( in.get(c) ) {
|
|
|
|
if ( (c != ' ') && (c != ',') ) {
|
|
|
|
// push back on the stream
|
|
|
|
in.putback(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
in >> p.n[PZ];
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
inline ostream&
|
|
|
|
operator<< ( ostream& out, const Point3D& p )
|
|
|
|
{
|
1998-11-11 00:18:36 +00:00
|
|
|
return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
|
1998-10-20 18:21:49 +00:00
|
|
|
}
|
|
|
|
|
1998-10-16 00:50:29 +00:00
|
|
|
///////////////////////////
|
|
|
|
//
|
|
|
|
// Point3D Member functions
|
|
|
|
//
|
|
|
|
///////////////////////////
|
|
|
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
|
|
|
|
inline Point3D::Point3D() {}
|
|
|
|
|
|
|
|
inline Point3D::Point3D(const double x, const double y, const double z)
|
|
|
|
{
|
|
|
|
n[PX] = x; n[PY] = y; n[PZ] = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D::Point3D(const double d)
|
|
|
|
{
|
|
|
|
n[PX] = n[PY] = n[PZ] = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D::Point3D(const Point3D& p)
|
|
|
|
{
|
|
|
|
n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASSIGNMENT OPERATORS
|
|
|
|
|
|
|
|
inline Point3D& Point3D::operator = (const Point3D& p)
|
|
|
|
{
|
|
|
|
n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D& Point3D::operator += ( const Point3D& p )
|
|
|
|
{
|
|
|
|
n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D& Point3D::operator -= ( const Point3D& p )
|
|
|
|
{
|
|
|
|
n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D& Point3D::operator *= ( const double d )
|
|
|
|
{
|
|
|
|
n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D& Point3D::operator /= ( const double d )
|
|
|
|
{
|
|
|
|
double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Point3D::setx(const double x) {
|
|
|
|
n[PX] = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Point3D::sety(const double y) {
|
|
|
|
n[PY] = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Point3D::setz(const double z) {
|
|
|
|
n[PZ] = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
// QUERIES
|
|
|
|
|
|
|
|
inline double& Point3D::operator [] ( int i)
|
|
|
|
{
|
|
|
|
assert(! (i < PX || i > PZ));
|
|
|
|
return n[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline double Point3D::operator [] ( int i) const {
|
|
|
|
assert(! (i < PX || i > PZ));
|
|
|
|
return n[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline double Point3D::x() const { return n[PX]; }
|
|
|
|
|
|
|
|
inline double Point3D::y() const { return n[PY]; }
|
|
|
|
|
|
|
|
inline double Point3D::z() const { return n[PZ]; }
|
|
|
|
|
|
|
|
inline double Point3D::lon() const { return n[PX]; }
|
|
|
|
|
|
|
|
inline double Point3D::lat() const { return n[PY]; }
|
|
|
|
|
|
|
|
inline double Point3D::radius() const { return n[PZ]; }
|
|
|
|
|
|
|
|
inline double Point3D::elev() const { return n[PZ]; }
|
|
|
|
|
|
|
|
|
|
|
|
// FRIENDS
|
|
|
|
|
|
|
|
inline Point3D operator - (const Point3D& a)
|
|
|
|
{
|
|
|
|
return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D operator + (const Point3D& a, const Point3D& b)
|
|
|
|
{
|
1998-10-20 18:21:49 +00:00
|
|
|
return Point3D(a) += b;
|
1998-10-16 00:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D operator - (const Point3D& a, const Point3D& b)
|
|
|
|
{
|
1998-10-20 18:21:49 +00:00
|
|
|
return Point3D(a) -= b;
|
1998-10-16 00:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D operator * (const Point3D& a, const double d)
|
|
|
|
{
|
1998-10-20 18:21:49 +00:00
|
|
|
return Point3D(a) *= d;
|
1998-10-16 00:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D operator * (const double d, const Point3D& a)
|
|
|
|
{
|
|
|
|
return a*d;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Point3D operator / (const Point3D& a, const double d)
|
|
|
|
{
|
1998-10-20 18:21:49 +00:00
|
|
|
return Point3D(a) *= (1.0 / d );
|
1998-10-16 00:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator == (const Point3D& a, const Point3D& b)
|
|
|
|
{
|
|
|
|
return
|
1998-10-20 18:21:49 +00:00
|
|
|
(a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
|
|
|
|
(a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
|
|
|
|
(a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
|
1998-10-16 00:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator != (const Point3D& a, const Point3D& b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special functions
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
inline double
|
|
|
|
Point3D::distance3D(const Point3D& a ) const
|
1998-10-16 00:50:29 +00:00
|
|
|
{
|
|
|
|
double x, y, z;
|
|
|
|
|
1998-10-20 18:21:49 +00:00
|
|
|
x = n[PX] - a.n[PX];
|
|
|
|
y = n[PY] - a.n[PY];
|
|
|
|
z = n[PZ] - a.n[PZ];
|
1998-10-16 00:50:29 +00:00
|
|
|
|
|
|
|
return sqrt(x*x + y*y + z*z);
|
|
|
|
}
|
|
|
|
|
1999-02-01 21:08:28 +00:00
|
|
|
|
|
|
|
inline double
|
|
|
|
Point3D::distance3Dsquared(const Point3D& a ) const
|
|
|
|
{
|
|
|
|
double x, y, z;
|
|
|
|
|
|
|
|
x = n[PX] - a.n[PX];
|
|
|
|
y = n[PY] - a.n[PY];
|
|
|
|
z = n[PZ] - a.n[PZ];
|
|
|
|
|
|
|
|
return(x*x + y*y + z*z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-16 00:50:29 +00:00
|
|
|
#endif // _POINT3D_HXX
|
|
|
|
|
|
|
|
|