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)
29 # error This library requires C++
33 #include <Misc/fgstream.hxx>
35 #include "Include/fg_stl_config.h"
37 #ifdef NEEDNAMESPACESTD
49 #include <Include/fg_constants.h>
52 enum {PX, PY, PZ}; // axes
55 ///////////////////////////
59 ///////////////////////////
72 Point3D(const double x, const double y, const double z);
73 Point3D(const double d);
74 Point3D(const Point3D &p);
76 // Assignment operators
78 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
79 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
80 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
81 Point3D& operator *= ( const double d ); // multiplication by a constant
82 Point3D& operator /= ( const double d ); // division by a constant
83 void setvals(const double x, const double y, const double z);
84 void setx(const double x);
85 void sety(const double y);
86 void setz(const double z);
90 double& operator [] ( int i); // indexing
91 double operator[] (int i) const; // read-only indexing
93 double x() const; // cartesian x
94 double y() const; // cartesian y
95 double z() const; // cartesian z
97 double lon() const; // polar longitude
98 double lat() const; // polar latitude
99 double radius() const; // polar radius
100 double elev() const; // geodetic elevation (if specifying a surface point)
104 friend Point3D operator - (const Point3D& p); // -p1
105 friend Point3D operator + (const Point3D& a, const Point3D& b); // p1 + p2
106 friend Point3D operator - (const Point3D& a, const Point3D& b); // p1 - p2
107 friend Point3D operator * (const Point3D& a, const double d); // p1 * 3.0
108 friend Point3D operator * (const double d, const Point3D& a); // 3.0 * p1
109 friend Point3D operator / (const Point3D& a, const double d); // p1 / 3.0
110 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
111 friend bool operator != (const Point3D& a, const Point3D& b); // p1 != p2?
114 double distance3D(const Point3D& a, const Point3D& b); // distance between
120 operator >> ( istream& in, Point3D& p)
127 // read past optional comma
128 while ( in.get(c) ) {
129 if ( (c != ' ') && (c != ',') ) {
130 // push back on the stream
138 // read past optional comma
139 while ( in.get(c) ) {
140 if ( (c != ' ') && (c != ',') ) {
141 // push back on the stream
154 ///////////////////////////
156 // Point3D Member functions
158 ///////////////////////////
162 inline Point3D::Point3D() {}
164 inline Point3D::Point3D(const double x, const double y, const double z)
166 n[PX] = x; n[PY] = y; n[PZ] = z;
169 inline Point3D::Point3D(const double d)
171 n[PX] = n[PY] = n[PZ] = d;
174 inline Point3D::Point3D(const Point3D& p)
176 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
179 // ASSIGNMENT OPERATORS
181 inline Point3D& Point3D::operator = (const Point3D& p)
183 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
186 inline Point3D& Point3D::operator += ( const Point3D& p )
188 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
191 inline Point3D& Point3D::operator -= ( const Point3D& p )
193 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
196 inline Point3D& Point3D::operator *= ( const double d )
198 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
201 inline Point3D& Point3D::operator /= ( const double d )
203 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
207 inline void Point3D::setvals(const double x, const double y, const double z) {
208 n[PX] = x; n[PY] = y; n[PZ] = z;
211 inline void Point3D::setx(const double x) {
215 inline void Point3D::sety(const double y) {
219 inline void Point3D::setz(const double z) {
225 inline double& Point3D::operator [] ( int i)
227 assert(! (i < PX || i > PZ));
231 inline double Point3D::operator [] ( int i) const {
232 assert(! (i < PX || i > PZ));
237 inline double Point3D::x() const { return n[PX]; }
239 inline double Point3D::y() const { return n[PY]; }
241 inline double Point3D::z() const { return n[PZ]; }
243 inline double Point3D::lon() const { return n[PX]; }
245 inline double Point3D::lat() const { return n[PY]; }
247 inline double Point3D::radius() const { return n[PZ]; }
249 inline double Point3D::elev() const { return n[PZ]; }
254 inline Point3D operator - (const Point3D& a)
256 return Point3D(-a.n[PX],-a.n[PY],-a.n[PZ]);
259 inline Point3D operator + (const Point3D& a, const Point3D& b)
261 return Point3D(a.n[PX]+ b.n[PX], a.n[PY] + b.n[PY], a.n[PZ] + b.n[PZ]);
264 inline Point3D operator - (const Point3D& a, const Point3D& b)
266 return Point3D(a.n[PX]-b.n[PX], a.n[PY]-b.n[PY], a.n[PZ]-b.n[PZ]);
269 inline Point3D operator * (const Point3D& a, const double d)
271 return Point3D(d*a.n[PX], d*a.n[PY], d*a.n[PZ]);
274 inline Point3D operator * (const double d, const Point3D& a)
279 inline Point3D operator / (const Point3D& a, const double d)
282 return Point3D(a.n[PX]*d_inv, a.n[PY]*d_inv, a.n[PZ]*d_inv);
285 inline bool operator == (const Point3D& a, const Point3D& b)
288 (a.n[PX] - b.n[PX]) < FG_EPSILON &&
289 (a.n[PY] - b.n[PY]) < FG_EPSILON &&
290 (a.n[PZ] - b.n[PZ]) < FG_EPSILON;
293 inline bool operator != (const Point3D& a, const Point3D& b)
300 inline double distance3D(const Point3D& a, const Point3D& b)
308 return sqrt(x*x + y*y + z*z);
311 #endif // _POINT3D_HXX
315 // Revision 1.1 1998/10/16 00:50:29 curt
316 // Added point3d.hxx to replace cheezy fgPoint3d struct.