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
43 #include <Include/fg_constants.h>
46 enum {PX, PY, PZ}; // axes
49 ///////////////////////////
53 ///////////////////////////
66 Point3D(const double x, const double y, const double z);
67 Point3D(const double d);
68 Point3D(const Point3D &p);
70 // Assignment operators
72 Point3D& operator = ( const Point3D& p ); // assignment of a Point3D
73 Point3D& operator += ( const Point3D& p ); // incrementation by a Point3D
74 Point3D& operator -= ( const Point3D& p ); // decrementation by a Point3D
75 Point3D& operator *= ( const double d ); // multiplication by a constant
76 Point3D& operator /= ( const double d ); // division by a constant
78 void setx(const double x);
79 void sety(const double y);
80 void setz(const double z);
84 double& operator [] ( int i); // indexing
85 double operator[] (int i) const; // read-only indexing
87 double x() const; // cartesian x
88 double y() const; // cartesian y
89 double z() const; // cartesian z
91 double lon() const; // polar longitude
92 double lat() const; // polar latitude
93 double radius() const; // polar radius
94 double elev() const; // geodetic elevation (if specifying a surface point)
98 friend Point3D operator - (const Point3D& p); // -p1
99 friend Point3D operator + (const Point3D& a, const Point3D& b); // p1 + p2
100 friend Point3D operator - (const Point3D& a, const Point3D& b); // p1 - p2
101 friend Point3D operator * (const Point3D& a, const double d); // p1 * 3.0
102 friend Point3D operator * (const double d, const Point3D& a); // 3.0 * p1
103 friend Point3D operator / (const Point3D& a, const double d); // p1 / 3.0
104 friend bool operator == (const Point3D& a, const Point3D& b); // p1 == p2?
105 friend bool operator != (const Point3D& a, const Point3D& b); // p1 != p2?
108 double distance3D(const Point3D& a, const Point3D& b); // distance between
114 operator << ( ostream& out, Point3D& p)
122 out << x << " " << y << " " << z;
129 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
158 p = Point3D(x, y, z);
163 ///////////////////////////
165 // Point3D Member functions
167 ///////////////////////////
171 inline Point3D::Point3D() {}
173 inline Point3D::Point3D(const double x, const double y, const double z)
175 n[PX] = x; n[PY] = y; n[PZ] = z;
178 inline Point3D::Point3D(const double d)
180 n[PX] = n[PY] = n[PZ] = d;
183 inline Point3D::Point3D(const Point3D& p)
185 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ];
188 // ASSIGNMENT OPERATORS
190 inline Point3D& Point3D::operator = (const Point3D& p)
192 n[PX] = p.n[PX]; n[PY] = p.n[PY]; n[PZ] = p.n[PZ]; return *this;
195 inline Point3D& Point3D::operator += ( const Point3D& p )
197 n[PX] += p.n[PX]; n[PY] += p.n[PY]; n[PZ] += p.n[PZ]; return *this;
200 inline Point3D& Point3D::operator -= ( const Point3D& p )
202 n[PX] -= p.n[PX]; n[PY] -= p.n[PY]; n[PZ] -= p.n[PZ]; return *this;
205 inline Point3D& Point3D::operator *= ( const double d )
207 n[PX] *= d; n[PY] *= d; n[PZ] *= d; return *this;
210 inline Point3D& Point3D::operator /= ( const double d )
212 double d_inv = 1./d; n[PX] *= d_inv; n[PY] *= d_inv; n[PZ] *= d_inv;
216 inline void Point3D::setx(const double x) {
220 inline void Point3D::sety(const double y) {
224 inline void Point3D::setz(const double z) {
230 inline double& Point3D::operator [] ( int i)
232 assert(! (i < PX || i > PZ));
236 inline double Point3D::operator [] ( int i) const {
237 assert(! (i < PX || i > PZ));
242 inline double Point3D::x() const { return n[PX]; }
244 inline double Point3D::y() const { return n[PY]; }
246 inline double Point3D::z() const { return n[PZ]; }
248 inline double Point3D::lon() const { return n[PX]; }
250 inline double Point3D::lat() const { return n[PY]; }
252 inline double Point3D::radius() const { return n[PZ]; }
254 inline double Point3D::elev() const { return n[PZ]; }
259 inline Point3D operator - (const Point3D& a)
261 return Point3D(-a.n[PX],-a.n[PY],-a.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 Point3D& b)
271 return Point3D(a.n[PX]-b.n[PX], a.n[PY]-b.n[PY], a.n[PZ]-b.n[PZ]);
274 inline Point3D operator * (const Point3D& a, const double d)
276 return Point3D(d*a.n[PX], d*a.n[PY], d*a.n[PZ]);
279 inline Point3D operator * (const double d, const Point3D& a)
284 inline Point3D operator / (const Point3D& a, const double d)
287 return Point3D(a.n[PX]*d_inv, a.n[PY]*d_inv, a.n[PZ]*d_inv);
290 inline bool operator == (const Point3D& a, const Point3D& b)
293 (a.n[PX] - b.n[PX]) < FG_EPSILON &&
294 (a.n[PY] - b.n[PY]) < FG_EPSILON &&
295 (a.n[PZ] - b.n[PZ]) < FG_EPSILON;
298 inline bool operator != (const Point3D& a, const Point3D& b)
305 inline double distance3D(const Point3D& a, const Point3D& b)
313 return sqrt(x*x + y*y + z*z);
316 #endif // _POINT3D_HXX
320 // Revision 1.2 1998/10/18 01:17:12 curt
323 // Revision 1.1 1998/10/16 00:50:29 curt
324 // Added point3d.hxx to replace cheezy fgPoint3d struct.