1 // point3d.hxx -- a 3d point class.
3 // Adapted from algebra3 by Jean-Francois Doue, started October 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
30 # error This library requires C++
33 #include "Include/compiler.h"
35 #ifdef FG_MATH_EXCEPTION_CLASH
36 # define exception c_exception
39 #ifdef FG_HAVE_STD_INCLUDES
44 # include <iostream.h>
49 FG_USING_STD(ostream);
50 FG_USING_STD(istream);
52 // -rp- assert.h is buggy under MWCWP3, as multiple #include undef assert !
57 const double fgPoint3_Epsilon = 0.0000001;
59 enum {PX, PY, PZ}; // axes
61 // Kludge for msvc++ 6.0 - requires forward decls of friend functions.
63 istream& operator>> ( istream&, Point3D& );
64 ostream& operator<< ( ostream&, const Point3D& );
65 Point3D operator- (const Point3D& p); // -p1
66 bool operator== (const Point3D& a, const Point3D& b); // p1 == p2?
69 ///////////////////////////
73 ///////////////////////////
86 Point3D(const double x, const double y, const double z);
87 explicit Point3D(const double d);
88 Point3D(const Point3D &p);
90 // Assignment operators
92 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
93 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
94 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
95 Point3D& operator *= ( const double d ); // multiplication by a constant
96 Point3D& operator /= ( const double d ); // division by a constant
98 void setx(const double x);
99 void sety(const double y);
100 void setz(const double z);
104 double& operator [] ( int i); // indexing
105 double operator[] (int i) const; // read-only indexing
107 double x() const; // cartesian x
108 double y() const; // cartesian y
109 double z() const; // cartesian z
111 double lon() const; // polar longitude
112 double lat() const; // polar latitude
113 double radius() const; // polar radius
114 double elev() const; // geodetic elevation (if specifying a surface point)
117 friend Point3D operator - (const Point3D& p); // -p1
118 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
119 friend istream& operator>> ( istream&, Point3D& );
120 friend ostream& operator<< ( ostream&, const Point3D& );
123 double distance3D(const Point3D& a) const; // distance between
124 double distance3Dsquared(const Point3D& a) const; // distance between ^ 2
130 operator >> ( istream& in, Point3D& p)
136 // read past optional comma
137 while ( in.get(c) ) {
138 if ( (c != ' ') && (c != ',') ) {
139 // push back on the stream
147 // read past optional comma
148 while ( in.get(c) ) {
149 if ( (c != ' ') && (c != ',') ) {
150 // push back on the stream
162 operator<< ( ostream& out, const Point3D& p )
164 return out << p.n[PX] << ", " << p.n[PY] << ", " << p.n[PZ];
167 ///////////////////////////
169 // Point3D Member functions
171 ///////////////////////////
175 inline Point3D::Point3D() {}
177 inline Point3D::Point3D(const double x, const double y, const double z)
179 n[PX] = x; n[PY] = y; n[PZ] = z;
182 inline Point3D::Point3D(const double d)
184 n[PX] = n[PY] = n[PZ] = d;
187 inline Point3D::Point3D(const Point3D& p)
189 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
192 // ASSIGNMENT OPERATORS
194 inline Point3D& Point3D::operator = (const Point3D& p)
196 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
199 inline Point3D& Point3D::operator += ( const Point3D& p )
201 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
204 inline Point3D& Point3D::operator -= ( const Point3D& p )
206 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
209 inline Point3D& Point3D::operator *= ( const double d )
211 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
214 inline Point3D& Point3D::operator /= ( const double d )
216 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
220 inline void Point3D::setx(const double x) {
224 inline void Point3D::sety(const double y) {
228 inline void Point3D::setz(const double z) {
234 inline double& Point3D::operator [] ( int i)
236 assert(! (i < PX || i > PZ));
240 inline double Point3D::operator [] ( int i) const {
241 assert(! (i < PX || i > PZ));
246 inline double Point3D::x() const { return n[PX]; }
248 inline double Point3D::y() const { return n[PY]; }
250 inline double Point3D::z() const { return n[PZ]; }
252 inline double Point3D::lon() const { return n[PX]; }
254 inline double Point3D::lat() const { return n[PY]; }
256 inline double Point3D::radius() const { return n[PZ]; }
258 inline double Point3D::elev() const { return n[PZ]; }
263 inline Point3D operator - (const Point3D& a)
265 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
268 inline Point3D operator + (const Point3D& a, const Point3D& b)
270 return Point3D(a) += b;
273 inline Point3D operator - (const Point3D& a, const Point3D& b)
275 return Point3D(a) -= b;
278 inline Point3D operator * (const Point3D& a, const double d)
280 return Point3D(a) *= d;
283 inline Point3D operator * (const double d, const Point3D& a)
288 inline Point3D operator / (const Point3D& a, const double d)
290 return Point3D(a) *= (1.0 / d );
293 inline bool operator == (const Point3D& a, const Point3D& b)
296 (a.n[PX] - b.n[PX]) < fgPoint3_Epsilon &&
297 (a.n[PY] - b.n[PY]) < fgPoint3_Epsilon &&
298 (a.n[PZ] - b.n[PZ]) < fgPoint3_Epsilon;
301 inline bool operator != (const Point3D& a, const Point3D& b)
309 Point3D::distance3D(const Point3D& a ) const
317 return sqrt(x*x + y*y + z*z);
322 Point3D::distance3Dsquared(const Point3D& a ) const
330 return(x*x + y*y + z*z);
334 #endif // _POINT3D_HXX
338 // Revision 1.9 1999/02/01 21:08:28 curt
339 // Optimizations from Norman Vine.
341 // Revision 1.8 1999/01/27 04:46:18 curt
342 // Portability tweaks by Bernie Bright.
344 // Revision 1.7 1999/01/19 20:56:58 curt
345 // MacOS portability changes contributed by "Robert Puyol" <puyol@abvent.fr>
347 // Revision 1.6 1998/11/23 21:46:37 curt
348 // Borland portability tweaks.
350 // Revision 1.5 1998/11/20 01:00:38 curt
351 // Patch in fgGeoc2Geod() to avoid a floating explosion.
352 // point3d.hxx include math.h for FreeBSD
354 // Revision 1.4 1998/11/11 00:18:38 curt
355 // Check for domain error in fgGeoctoGeod()
357 // Revision 1.3 1998/10/20 18:21:49 curt
358 // Tweaks from Bernie Bright.
360 // Revision 1.2 1998/10/18 01:17:12 curt
363 // Revision 1.1 1998/10/16 00:50:29 curt
364 // Added point3d.hxx to replace cheezy fgPoint3d struct.