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