new implementation of cartesian -> elevation, avoiding baroque code inherited from...
[ardour.git] / libs / pbd / cartesian.cc
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 #include <iostream>
20 #include <math.h>
21
22 #include "pbd/cartesian.h"
23
24 using namespace std;
25
26 void
27 PBD::azi_ele_to_cart (double azi, double ele, double& x, double& y, double& z)
28 {
29         /* convert from cylindrical coordinates in degrees to cartesian */
30
31         static const double atorad = 2.0 * M_PI / 360.0 ;
32
33         x = cos (azi * atorad) * cos (ele * atorad);
34         y = sin (azi * atorad) * cos (ele * atorad);
35         z = sin (ele * atorad);
36 }
37
38 void 
39 PBD::cart_to_azi_ele (double x, double y, double z, double& azimuth, double& elevation)
40 {
41 #if 1
42         /* converts cartesian coordinates to cylindrical in degrees*/
43
44         double rho, theta, phi;
45
46         rho = sqrt (x*x + y*y + z*z);
47         phi = acos (1.0/rho);
48         theta = atan2 (y, x);
49
50         /* XXX for now, clamp phi to zero */
51
52         phi = 0.0;
53
54         if (theta < 0.0) {
55                 azimuth = 180.0 - (180.0 * (theta / M_PI)); /* LHS is negative */
56         } else {
57                 azimuth = 180.0 * (theta / M_PI);
58         }
59
60         if (phi < 0.0) {
61                 elevation = 180.0 - (180.0 * (phi / M_PI)); /* LHS is negative */
62         } else {
63                 elevation = 180.0 * (phi /  M_PI);
64         }
65 #else
66         /* converts cartesian coordinates to cylindrical in degrees*/
67
68         const double atorad = 2.0 * M_PI / 360.0;
69         double atan_y_per_x, atan_x_pl_y_per_z;
70         double distance;
71
72         if (x == 0.0) {
73                 atan_y_per_x = M_PI / 2;
74         } else {
75                 atan_y_per_x = atan2 (y,x);
76         }
77
78         if (y < 0.0) {
79                 /* below x-axis: atan2 returns 0 .. -PI (negative) so convert to degrees and ADD to 180 */
80                 azimuth = 180.0 + (atan_y_per_x / (M_PI/180.0) + 180.0);
81         } else {
82                 /* above x-axis: atan2 returns 0 .. +PI so convert to degrees */
83                 azimuth = atan_y_per_x / atorad;
84         }
85
86         distance = sqrt (x*x + y*y);
87
88         if (z == 0.0) {
89                 atan_x_pl_y_per_z = 0.0;
90         } else {
91                 atan_x_pl_y_per_z = atan2 (z,distance);
92         }
93
94         if (distance == 0.0) {
95                 if (z < 0.0) {
96                         atan_x_pl_y_per_z = -M_PI/2.0;
97                 } else if (z > 0.0) {
98                         atan_x_pl_y_per_z = M_PI/2.0;
99                 }
100         }
101
102         elevation = atan_x_pl_y_per_z / atorad;
103
104         // distance = sqrtf (x*x + y*y + z*z);
105 #endif
106 }
107