switch cartesian/spherical function names and make them use length. still a tweak...
[ardour2.git] / libs / pbd / pbd / cartesian.h
blobffc91c2fd62fd709e32d12c6885c78e156dc663c
1 /*
2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #ifndef __libpbd_cartesian_h__
20 #define __libpbd_cartesian_h__
22 #include <cfloat>
23 #include <cmath>
25 namespace PBD {
27 void spherical_to_cartesian (double azi, double ele, double len, double& x, double& y, double& z);
28 void cartesian_to_spherical (double x, double y, double z, double& azi, double& ele, double& len);
30 struct AngularVector;
32 struct CartesianVector {
33 double x;
34 double y;
35 double z;
37 CartesianVector () : x(0.0), y(0.0), z(0.0) {}
38 CartesianVector (double xp, double yp, double zp = 0.0) : x(xp), y(yp), z(zp) {}
40 CartesianVector& translate (CartesianVector& other, double xtranslate, double ytranslate, double ztranslate = 0.0) {
41 other.x += xtranslate;
42 other.y += ytranslate;
43 other.z += ztranslate;
44 return other;
47 CartesianVector& scale (CartesianVector& other, double xscale, double yscale, double zscale = 1.0) {
48 other.x *= xscale;
49 other.y *= yscale;
50 other.z *= zscale;
51 return other;
54 void angular (AngularVector&) const;
57 struct AngularVector {
58 double azi;
59 double ele;
60 double length;
62 AngularVector () : azi(0.0), ele(0.0), length (0.0) {}
63 AngularVector (double a, double e, double l = 1.0) : azi(a), ele(e), length (l) {}
65 AngularVector operator- (const AngularVector& other) const {
66 AngularVector r;
67 r.azi = azi - other.azi;
68 r.ele = ele - other.ele;
69 r.length = length - other.length;
70 return r;
73 AngularVector operator+ (const AngularVector& other) const {
74 AngularVector r;
75 r.azi = azi + other.azi;
76 r.ele = ele + other.ele;
77 r.length = length + other.length;
78 return r;
81 bool operator== (const AngularVector& other) const {
82 return fabs (azi - other.azi) <= FLT_EPSILON &&
83 fabs (ele - other.ele) <= FLT_EPSILON &&
84 fabs (length - other.length) <= FLT_EPSILON;
87 bool operator!= (const AngularVector& other) const {
88 return fabs (azi - other.azi) > FLT_EPSILON ||
89 fabs (ele - other.ele) > FLT_EPSILON ||
90 fabs (length - other.length) > FLT_EPSILON;
93 void cartesian (CartesianVector& c) const {
94 spherical_to_cartesian (azi, ele, length, c.x, c.y, c.z);
98 inline void CartesianVector::angular (AngularVector& a) const {
99 cartesian_to_spherical (x, y, z, a.azi, a.ele, a.length);
104 #endif /* __libpbd_cartesian_h__ */