Declick before the end of seamless loops, not after the end, so that loops are render...
[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 namespace PBD {
26
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);
29         
30 struct AngularVector;
31
32 struct CartesianVector {
33         double x;
34         double y;
35         double z;
36
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) {}
39
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;
45         }
46
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;
52         }
53
54         void angular (AngularVector&) const;
55 };
56
57 struct AngularVector {
58         double azi;
59         double ele;
60         double length;
61
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) {}
64
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;
71         }
72
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;
79         }
80
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;
85         }
86
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;
91         }
92
93         void cartesian (CartesianVector& c) const {
94                 spherical_to_cartesian (azi, ele, length, c.x, c.y, c.z);
95         }
96 };
97
98 inline void CartesianVector::angular (AngularVector& a) const {
99         cartesian_to_spherical (x, y, z, a.azi, a.ele, a.length);
100 }
101
102 }
103
104 #endif /* __libpbd_cartesian_h__ */