merge (w/fix) with master
[ardour.git] / libs / pbd / pbd / cartesian.h
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19 #ifndef __libpbd_cartesian_h__
20 #define __libpbd_cartesian_h__
21
22 #include <cfloat>
23 #include <cmath>
24
25 #include "pbd/libpbd_visibility.h"
26
27 namespace PBD {
28
29 LIBPBD_API void spherical_to_cartesian (double azi, double ele, double len, double& x, double& y, double& z);
30 LIBPBD_API void cartesian_to_spherical (double x, double y, double z, double& azi, double& ele, double& len);
31         
32 struct AngularVector;
33
34 struct LIBPBD_API CartesianVector {
35         double x;
36         double y;
37         double z;
38
39         CartesianVector () : x(0.0), y(0.0), z(0.0) {}
40         CartesianVector (double xp, double yp, double zp = 0.0) : x(xp), y(yp), z(zp) {}
41
42         CartesianVector& translate (CartesianVector& other, double xtranslate, double ytranslate, double ztranslate = 0.0) {
43                 other.x += xtranslate;
44                 other.y += ytranslate;
45                 other.z += ztranslate;
46                 return other;
47         }
48
49         CartesianVector& scale (CartesianVector& other, double xscale, double yscale, double zscale = 1.0) {
50                 other.x *= xscale;
51                 other.y *= yscale;
52                 other.z *= zscale;
53                 return other;
54         }
55
56         inline void angular (AngularVector& a) const;
57 };
58
59 struct LIBPBD_API AngularVector {
60         double azi;
61         double ele;
62         double length;
63
64         AngularVector () : azi(0.0), ele(0.0), length (0.0) {}
65         AngularVector (double a, double e, double l = 1.0) : azi(a), ele(e), length (l) {}
66
67         AngularVector operator- (const AngularVector& other) const {
68                 AngularVector r;
69                 r.azi = azi - other.azi;
70                 r.ele = ele - other.ele;
71                 r.length = length - other.length;
72                 return r;
73         }
74
75         AngularVector operator+ (const AngularVector& other) const {
76                 AngularVector r;
77                 r.azi = azi + other.azi;
78                 r.ele = ele + other.ele;
79                 r.length = length + other.length;
80                 return r;
81         }
82
83         bool operator== (const AngularVector& other) const {
84                 return fabs (azi - other.azi) <= FLT_EPSILON &&
85                 fabs (ele - other.ele) <= FLT_EPSILON &&
86                 fabs (length - other.length) <= FLT_EPSILON;
87         }
88
89         bool operator!= (const AngularVector& other) const {
90                 return fabs (azi - other.azi) > FLT_EPSILON ||
91                         fabs (ele - other.ele) > FLT_EPSILON ||
92                         fabs (length - other.length) > FLT_EPSILON;
93         }
94
95         void cartesian (CartesianVector& c) const {
96                 spherical_to_cartesian (azi, ele, length, c.x, c.y, c.z);
97         }
98 };
99
100 inline void CartesianVector::angular (AngularVector& a) const {
101         cartesian_to_spherical (x, y, z, a.azi, a.ele, a.length);
102 }
103
104 }
105
106 #endif /* __libpbd_cartesian_h__ */