aboutsummaryrefslogtreecommitdiff
path: root/src/patch/coords.cc
blob: 946e4be5381c46d767665c26db614a9e9b26fc2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// coords.cc - coordinate conversions etc
// $Id$

//
// local_coords::{mu,nu,phi}_of_((mu,nu,phi))
// local_coords::xyz_of_((mu,nu,phi))
// local_coords::xyz_of_r_((mu,nu,phi))
// local_coords::((mu,nu,phi))_of_xyz
// local_coords::r_((mu,nu,phi))_of_xyz
// local_coords::theta_phi_of_((mu,nu,phi))
//
// local_coords::mu_nu_phi::name_of_coords_set
//

#include <math.h>
#include <float.h>
#include <assert.h>
#include <limits.h>

#include "jt/stdc.h"
#include "jt/util.hh"

#include "fp.hh"
#include "coords.hh"

using jtutil::error_exit;
using jtutil::arctan_xy;
using jtutil::pow2;
using jtutil::hypot3;

//******************************************************************************
//******************************************************************************
//******************************************************************************

//
// these functions give any one of {mu,nu,phi} in terms of the other two
//

//**************************************

// ill-conditioned near z axis
// not valid in xy plane
namespace local_coords
	  {
fp phi_of_mu_nu(fp mu, fp nu)
{
assert(! fuzzy<fp>::EQ(fabs(mu), 0.5*PI));
assert(! fuzzy<fp>::EQ(fabs(nu), 0.5*PI));

const fp y_over_z = tan(mu);
const fp x_over_z = tan(nu);
return arctan_xy(x_over_z, y_over_z);
}
	  }

//**************************************

// ill-conditioned near y axis
// not valid in xz plane
namespace local_coords
	  {
fp nu_of_mu_phi(fp mu, fp phi)
{
const fp  mu_bar = 0.5*PI - mu ;
const fp phi_bar = 0.5*PI - phi;
assert(! fuzzy<fp>::EQ(fabs( mu_bar), 0.5*PI));
assert(! fuzzy<fp>::EQ(fabs(phi_bar), 0.5*PI));

const fp z_over_y = tan( mu_bar);
const fp x_over_y = tan(phi_bar);
return arctan_xy(z_over_y, x_over_y);
}
	  }

//**************************************

// ill-conditioned near x axis
// not valid in yz plane
namespace local_coords
	  {
fp mu_of_nu_phi(fp nu, fp phi)
{
const fp nu_bar = 0.5*PI - nu ;
assert(! fuzzy<fp>::EQ(fabs(nu_bar), 0.5*PI));
assert(! fuzzy<fp>::EQ(fabs(phi   ), 0.5*PI));

const fp z_over_x = tan(nu_bar);
const fp y_over_x = tan(phi   );
return arctan_xy(z_over_x, y_over_x);
}
	  }

//******************************************************************************

//
// these functions convert ((mu,nu,phi)) <--> (x,y,z)
//

//**************************************

// valid near z axis (mu small, nu small)
// not valid in xy plane
namespace local_coords
	  {
void xyz_of_r_mu_nu(fp r, fp mu, fp nu,   fp& x, fp& y, fp& z)
{
assert(! fuzzy<fp>::EQ(fabs(mu), 0.5*PI));
assert(! fuzzy<fp>::EQ(fabs(nu), 0.5*PI));

const fp y_over_z = tan(mu);
const fp x_over_z = tan(nu);
const fp temp = 1.0 / sqrt(1.0 + pow2(y_over_z) + pow2(x_over_z));

z = r * temp;
x = r * x_over_z * temp;
y = r * y_over_z * temp;
}
	  }

//**************************************

// valid near y axis (pi/2-mu small, pi/2-phi small)
// not valid in xz plane
namespace local_coords
	  {
void xyz_of_r_mu_phi(fp r, fp mu, fp phi,   fp& x, fp& y, fp& z)
{
const fp  mu_bar = 0.5*PI - mu ;
const fp phi_bar = 0.5*PI - phi;
assert(! fuzzy<fp>::EQ(fabs( mu_bar), 0.5*PI));
assert(! fuzzy<fp>::EQ(fabs(phi_bar), 0.5*PI));

const fp z_over_y = tan( mu_bar);
const fp x_over_y = tan(phi_bar);
const fp temp = 1.0 / sqrt(1.0 + pow2(z_over_y) + pow2(x_over_y));

y = r * temp;
z = r * z_over_y * temp;
x = r * x_over_y * temp;
}
	  }

//**************************************

// valid near x axis (pi/2-nu small, phi small)
// not valid in yz plane
namespace local_coords
	  {
void xyz_of_r_nu_phi(fp r, fp nu, fp phi,   fp& x, fp& y, fp& z)
{
const fp nu_bar = 0.5*PI - nu ;
assert(! fuzzy<fp>::EQ(fabs(nu_bar), 0.5*PI));
assert(! fuzzy<fp>::EQ(fabs(phi   ), 0.5*PI));

const fp z_over_x = tan(nu_bar);
const fp y_over_x = tan(phi   );
const fp temp = 1.0 / sqrt(1.0 + pow2(z_over_x) + pow2(y_over_x));

x = r * temp;
z = r * z_over_x * temp;
y = r * y_over_x * temp;
}
	  }

//******************************************************************************

// valid near z axis (mu small, nu small)
// ill-conditioned near x or y axes
namespace local_coords
	  {
void r_mu_nu_of_xyz(fp x, fp y, fp z,   fp& r, fp& mu, fp& nu)
{
r = hypot3(x, y, z);
mu = arctan_xy(z,y);
nu = arctan_xy(z,x);
}
	  }

//**************************************

// valid near y axis (pi/2-mu small, pi/2-phi small)
// ill-conditioned near x or z axes
namespace local_coords
	  {
void mu_phi_of_xyz(fp x, fp y, fp z,   fp& r, fp& mu, fp& phi)
{
r = hypot3(x, y, z);
mu  = arctan_xy(z,y);
phi = arctan_xy(x,y);
}
	  }

//**************************************

// valid near x axis (pi/2-nu small, pi/2-phi small)
// ill-conditioned near y or z axes
namespace local_coords
	  {
void nu_phi_of_xyz(fp x, fp y, fp z,   fp& r, fp& nu, fp& phi)
{
r = hypot3(x, y, z);
nu  = arctan_xy(z,x);
phi = arctan_xy(x,y);
}
	  }

//******************************************************************************

//
// these functions convert the usual polar spherical (theta,phi) <--> (x,y,z)
//

//**************************************

namespace local_coords
	  {
void xyz_of_r_theta_phi(fp r, fp theta, fp phi,   fp& x, fp& y, fp& z)
{
z = r * cos(theta);
x = r * sin(theta) * cos(phi);
y = r * sin(theta) * sin(phi);
}
	  }

//**************************************

namespace local_coords
	  {
void r_theta_phi_of_xyz(fp x, fp y, fp z,   fp& r, fp& theta, fp& phi)
{
r = hypot3(x, y, z);
theta = arctan_xy( z , hypot(x,y) );
phi = arctan_xy(x, y);
}
	  }

//******************************************************************************

//
// these functions convert ((mu,nu,phi)) <--> usual polar spherical (theta,phi)
// ... note phi is the same coordinate in both systems
//

namespace local_coords
	  {
void theta_phi_of_mu_nu(fp mu, fp nu,   fp& ps_theta, fp& ps_phi)
{
const fp tan_mu = tan(mu);
const fp tan_nu = tan(nu);
ps_theta = atan(hypot(tan_mu, tan_nu));		// page 7 of my mpe notes
ps_phi = arctan_xy(tan_nu, tan_mu);		// page 6 of my mpe notes
}
	  }

//**************************************

// Bugs: computes ps_phi via trig, even though it's trivially == phi
namespace local_coords
	  {
void theta_phi_of_mu_phi(fp mu, fp phi,   fp& ps_theta, fp& ps_phi)
{
fp x, y, z;
xyz_of_r_mu_phi(1.0, mu, phi,   x, y, z);
fp r;
r_theta_phi_of_xyz(x, y, z,   r, ps_theta, ps_phi);
}
	  }

//**************************************

// Bugs: computes ps_phi via trig, even though it's trivially == phi
namespace local_coords
	  {
void r_theta_phi_of_nu_phi(fp nu, fp phi,   fp& ps_theta, fp& ps_phi)
{
fp x, y, z;
xyz_of_r_nu_phi(1.0, nu, phi,   x, y, z);
fp r;
r_theta_phi_of_xyz(x, y, z,   r, ps_theta, ps_phi);
}
	  }

//******************************************************************************

namespace local_coords
	  {
void mu_nu_of_theta_phi(fp ps_theta, fp ps_phi,   fp& mu, fp& nu)
{
fp x, y, z;
xyz_of_r_theta_phi(1.0, ps_theta, ps_phi,   x, y, z);
fp r;
r_mu_nu_of_xyz(x, y, z,   r, mu, nu);
}
	  }

//**************************************

// Bugs: computes phi via trig, even though it's trivially == ps_phi
namespace local_coords
	  {
void mu_phi_of_theta_phi(fp ps_theta, fp ps_phi,   fp& mu, fp& phi)
{
fp x, y, z;
xyz_of_r_theta_phi(1.0, ps_theta, ps_phi,   x, y, z);
fp r;
r_mu_phi_of_xyz(x, y, z,   r, mu, phi);
}
	  }

//**************************************

// Bugs: computes phi via trig, even though it's trivially == ps_phi
namespace local_coords
	  {
void nu_phi_of_theta_phi(fp ps_theta, fp ps_phi,   fp& nu, fp& phi)
{
fp x, y, z;
xyz_of_r_theta_phi(1.0, ps_theta, ps_phi,   x, y, z);
fp r;
r_nu_phi_of_xyz(x, y, z,   r, nu, phi);
}
	  }

//******************************************************************************
//******************************************************************************
//******************************************************************************

//
// This function computes a human-readable name from a (mu,nu,phi)
// coordinates set.
//
const char *local_coords::name_of_coords_set(coords_set S)
{
//
// we have to use an if-else chain because the  coords::set_*  constants
// aren't compile-time constants and hence aren't eligible to be switch
// case labels
//
if	(S == set_empty)
   then return "{}";
else if (S == set_mu)
   then return "mu";
else if (S == set_nu)
   then return "nu";
else if (S == set_phi)
   then return "phi";
else if (S == set_mu|set_nu)
   then return "{mu,nu}";
else if (S == set_mu|set_phi)
   then return "{mu,phi}";
else if (S == set_nu|set_phi)
   then return "{nu,phi}";
else if (S == set_mu|set_nu|set_phi)
   then return "{mu,nu,phi}";
else	error_exit(PANIC_EXIT,
"***** coords::mu_nu_phi::name_of_coords_set:\n"
"        S=0x%x isn't a valid  coords_set  bit vector!\n"
,
		   int(S));					/*NOTREACHED*/
}