-
Notifications
You must be signed in to change notification settings - Fork 7
/
rot_paratrans.c
411 lines (343 loc) · 13.2 KB
/
rot_paratrans.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <assert.h>
#include <fftw3.h>
#include <mpi.h>
#include <hdf5.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_sort_long.h>
#include "raytrace.h"
void generate_rotmat_axis_angle_countercw(double axis[3], double angle, double rotmat[3][3])
{
double cosangle,sinangle;
sinangle = sin(angle);
cosangle = cos(angle);
int i,j;
assert(fabs(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2] - 1.0) < 1e-15);
for(i=0;i<3;++i)
for(j=0;j<3;++j)
rotmat[i][j] = 0.0;
rotmat[0][0] = cosangle;
rotmat[1][1] = cosangle;
rotmat[2][2] = cosangle;
for(i=0;i<3;++i)
for(j=0;j<3;++j)
rotmat[i][j] += axis[i]*axis[j]*(1.0 - cosangle);
rotmat[0][1] -= axis[2]*sinangle;
rotmat[0][2] += axis[1]*sinangle;
rotmat[1][2] -= axis[0]*sinangle;
rotmat[1][0] += axis[2]*sinangle;
rotmat[2][0] -= axis[1]*sinangle;
rotmat[2][1] += axis[0]*sinangle;
}
void generate_rotmat_axis_angle_cw(double axis[3], double angle, double rotmat[3][3])
{
//just call function above with angle -> -1*angle
generate_rotmat_axis_angle_countercw(axis,-1.0*angle,rotmat);
}
void rot_vec_axis_angle_countercw(double vec[3], double rvec[3], double axis[3], double angle)
{
double cosangle,sinangle;
double axisdotvec,axiscrossvec[3];
assert(fabs(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2] - 1.0) < 1e-15);
sinangle = sin(angle);
cosangle = cos(angle);
axisdotvec = axis[0]*vec[0] + axis[1]*vec[1] + axis[2]*vec[2];
axiscrossvec[0] = axis[1]*vec[2] - axis[2]*vec[1];
axiscrossvec[1] = axis[2]*vec[0] - axis[0]*vec[2];
axiscrossvec[2] = axis[0]*vec[1] - axis[1]*vec[0];
rvec[0] = vec[0]*cosangle + axis[0]*axisdotvec*(1.0 - cosangle) + axiscrossvec[0]*sinangle;
rvec[1] = vec[1]*cosangle + axis[1]*axisdotvec*(1.0 - cosangle) + axiscrossvec[1]*sinangle;
rvec[2] = vec[2]*cosangle + axis[2]*axisdotvec*(1.0 - cosangle) + axiscrossvec[2]*sinangle;
}
void rot_vec_axis_angle_cw(double vec[3], double rvec[3], double axis[3], double angle)
{
//just call function above with angle -> -1*angle
rot_vec_axis_angle_countercw(vec,rvec,axis,-1.0*angle);
}
void rot_vec_axis_trigangle_countercw(double vec[3], double rvec[3], double axis[3], double cosangle, double sinangle)
{
double axisdotvec,axiscrossvec[3];
assert(fabs(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2] - 1.0) < 1e-15);
axisdotvec = axis[0]*vec[0] + axis[1]*vec[1] + axis[2]*vec[2];
axiscrossvec[0] = axis[1]*vec[2] - axis[2]*vec[1];
axiscrossvec[1] = axis[2]*vec[0] - axis[0]*vec[2];
axiscrossvec[2] = axis[0]*vec[1] - axis[1]*vec[0];
rvec[0] = vec[0]*cosangle + axis[0]*axisdotvec*(1.0 - cosangle) + axiscrossvec[0]*sinangle;
rvec[1] = vec[1]*cosangle + axis[1]*axisdotvec*(1.0 - cosangle) + axiscrossvec[1]*sinangle;
rvec[2] = vec[2]*cosangle + axis[2]*axisdotvec*(1.0 - cosangle) + axiscrossvec[2]*sinangle;
}
/* parallel transport a vector on the sphere along the great circle connecting vec to rvec
this function is based on the healpix package rotate_coord.pro IDL routine
tvec is the tangent vector on the sphere where tvec[0] points along theta unit vector and tvec[1] points along phi unit vector
vec is the location on the sphere of this tangent vector
rvec is position to which tvec is to be parallel transported from vec
rtvec is the parallel transported vector
*/
void paratrans_tangvec(double tvec[2], double _vec[3], double _rvec[3], double rtvec[2])
{
double cospsi,sinpsi,norm,axis[3],cosangle,sinangle,p[3];
double rephi_vec[3],etheta_rvec[3],ephi_rvec[3];
double norm_rvec,norm_vec;
double vec[3],rvec[3];
//get rotation info
norm_vec = sqrt(_vec[0]*_vec[0] + _vec[1]*_vec[1] + _vec[2]*_vec[2]);
vec[0] = _vec[0]/norm_vec;
vec[1] = _vec[1]/norm_vec;
vec[2] = _vec[2]/norm_vec;
norm_rvec = sqrt(_rvec[0]*_rvec[0] + _rvec[1]*_rvec[1] + _rvec[2]*_rvec[2]);
rvec[0] = _rvec[0]/norm_rvec;
rvec[1] = _rvec[1]/norm_rvec;
rvec[2] = _rvec[2]/norm_rvec;
axis[0] = vec[1]*rvec[2] - vec[2]*rvec[1];
axis[1] = vec[2]*rvec[0] - vec[0]*rvec[2];
axis[2] = vec[0]*rvec[1] - vec[1]*rvec[0];
cosangle = vec[0]*rvec[0] + vec[1]*rvec[1] + vec[2]*rvec[2];
sinangle = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
if(sinangle != 0.0)
{
axis[0] /= sinangle;
axis[1] /= sinangle;
axis[2] /= sinangle;
}
else
{
axis[0] = 1.0;
axis[1] = 0.0;
axis[2] = 0.0;
}
p[0] = -vec[1];
p[1] = vec[0];
p[2] = 0.0;
rot_vec_axis_trigangle_countercw(p,rephi_vec,axis,cosangle,sinangle);
ephi_rvec[0] = -rvec[1];
ephi_rvec[1] = rvec[0];
ephi_rvec[2] = 0.0;
etheta_rvec[0] = rvec[2]*rvec[0];
etheta_rvec[1] = rvec[2]*rvec[1];
etheta_rvec[2] = -1.0*(rvec[0]*rvec[0] + rvec[1]*rvec[1]);
norm = sqrt((1.0 - rvec[2])*(1.0 + rvec[2])*(1.0 - vec[2])*(1.0 + vec[2]));
sinpsi = (rephi_vec[0]*etheta_rvec[0] + rephi_vec[1]*etheta_rvec[1] + rephi_vec[2]*etheta_rvec[2])/norm;
cospsi = (rephi_vec[0]*ephi_rvec[0] + rephi_vec[1]*ephi_rvec[1] + rephi_vec[2]*ephi_rvec[2])/norm;
//debugging fprintf statements
//fprintf(stderr,"cospsi = %f, sinpsi = %f\n",cospsi,sinpsi);
/* psi is defined as
R(e_theta) = cos(psi) e_theta' - sin(psi) e_phi'
R(e_phi) = sin(psi) e_theta' + cos(psi) e_phi'
thus to rotate tangent vector
t = t_theta R(e_theta) + t_phi R(e_phi)
we plug and chug to get
t = (t_theta*cos(psi) + t_phi*sin(psi)) e_theta' + (-t_theta*sin(psi) + t_phi*cos(psi)) e_phi'
*/
rtvec[0] = tvec[0]*cospsi + tvec[1]*sinpsi;
rtvec[1] = -1.0*tvec[0]*sinpsi + tvec[1]*cospsi;
}
/* parallel transport a tensor on the sphere along the great circle connecting vec to rvec
this function is based on the healpix package rotate_coord.pro IDL routine
ttensor is a tensor on the sphere where the i,j component has basis vectors with 0 = e_theta, and 1 = e_phi for i,j in {0,1}
vec is the location on the sphere of this tensor
rvec is position to which ttensor is to be parallel transported from vec
rttensor is the parallel transported vector
*/
void paratrans_tangtensor(double ttensor[2][2], double _vec[3], double _rvec[3], double rttensor[2][2])
{
double cospsi,sinpsi,norm,axis[3],cosangle,sinangle,p[3];
double rephi_vec[3],etheta_rvec[3],ephi_rvec[3];
double norm_rvec,norm_vec;
double vec[3],rvec[3];
//get rotation info
norm_vec = sqrt(_vec[0]*_vec[0] + _vec[1]*_vec[1] + _vec[2]*_vec[2]);
vec[0] = _vec[0]/norm_vec;
vec[1] = _vec[1]/norm_vec;
vec[2] = _vec[2]/norm_vec;
norm_rvec = sqrt(_rvec[0]*_rvec[0] + _rvec[1]*_rvec[1] + _rvec[2]*_rvec[2]);
rvec[0] = _rvec[0]/norm_rvec;
rvec[1] = _rvec[1]/norm_rvec;
rvec[2] = _rvec[2]/norm_rvec;
axis[0] = vec[1]*rvec[2] - vec[2]*rvec[1];
axis[1] = vec[2]*rvec[0] - vec[0]*rvec[2];
axis[2] = vec[0]*rvec[1] - vec[1]*rvec[0];
cosangle = vec[0]*rvec[0] + vec[1]*rvec[1] + vec[2]*rvec[2];
sinangle = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
if(sinangle != 0.0)
{
axis[0] /= sinangle;
axis[1] /= sinangle;
axis[2] /= sinangle;
}
else
{
axis[0] = 1.0;
axis[1] = 0.0;
axis[2] = 0.0;
}
p[0] = -vec[1];
p[1] = vec[0];
p[2] = 0.0;
rot_vec_axis_trigangle_countercw(p,rephi_vec,axis,cosangle,sinangle);
ephi_rvec[0] = -rvec[1];
ephi_rvec[1] = rvec[0];
ephi_rvec[2] = 0.0;
etheta_rvec[0] = rvec[2]*rvec[0];
etheta_rvec[1] = rvec[2]*rvec[1];
etheta_rvec[2] = -1.0*(rvec[0]*rvec[0] + rvec[1]*rvec[1]);
norm = sqrt((1.0 - rvec[2])*(1.0 + rvec[2])*(1.0 - vec[2])*(1.0 + vec[2]));
sinpsi = (rephi_vec[0]*etheta_rvec[0] + rephi_vec[1]*etheta_rvec[1] + rephi_vec[2]*etheta_rvec[2])/norm;
cospsi = (rephi_vec[0]*ephi_rvec[0] + rephi_vec[1]*ephi_rvec[1] + rephi_vec[2]*ephi_rvec[2])/norm;
//debugging fprintf statements
//fprintf(stderr,"cospsi = %f, sinpsi = %f\n",cospsi,sinpsi);
/* psi is defined as
R(e_theta) = cos(psi) e_theta' - sin(psi) e_phi'
R(e_phi) = sin(psi) e_theta' + cos(psi) e_phi'
under this coordinate change we have that
T' = R x T x Transpose(R)
where
T = | t_00 t_01 |
| t_10 t_11 |
R = | cos(psi) sin(psi) |
| -sin(psi) cos(psi) |
*/
double r[2][2],rt[2][2],t1[2][2];
int i,j;
r[0][0] = cospsi;
r[0][1] = -1.0*sinpsi;
r[1][0] = sinpsi;
r[1][1] = cospsi;
rt[0][0] = r[0][0];
rt[0][1] = r[1][0];
rt[1][0] = r[0][1];
rt[1][1] = r[1][1];
for(i=0;i<2;++i)
for(j=0;j<2;++j)
t1[i][j] = ttensor[i][0]*r[0][j] + ttensor[i][1]*r[1][j];
for(i=0;i<2;++i)
for(j=0;j<2;++j)
rttensor[i][j] = rt[i][0]*t1[0][j] + rt[i][1]*t1[1][j];
}
// parallel transports a ray to the observer position from its current position
void paratrans_ray_curr2obs(HEALPixRay *ray)
{
double ttensor[2][2],rttensor[2][2];
double obs[3];
//get obseerver position
nest2vec(ray->nest,obs,rayTraceData.rayOrder);
//do the tensors
ttensor[0][0] = ray->Aprev[2*0+0];
ttensor[0][1] = ray->Aprev[2*0+1];
ttensor[1][0] = ray->Aprev[2*1+0];
ttensor[1][1] = ray->Aprev[2*1+1];
paratrans_tangtensor(ttensor,ray->n,obs,rttensor);
ray->Aprev[2*0+0] = rttensor[0][0];
ray->Aprev[2*0+1] = rttensor[0][1];
ray->Aprev[2*1+0] = rttensor[1][0];
ray->Aprev[2*1+1] = rttensor[1][1];
ttensor[0][0] = ray->A[2*0+0];
ttensor[0][1] = ray->A[2*0+1];
ttensor[1][0] = ray->A[2*1+0];
ttensor[1][1] = ray->A[2*1+1];
paratrans_tangtensor(ttensor,ray->n,obs,rttensor);
ray->A[2*0+0] = rttensor[0][0];
ray->A[2*0+1] = rttensor[0][1];
ray->A[2*1+0] = rttensor[1][0];
ray->A[2*1+1] = rttensor[1][1];
}
// parallel transports a ray to the current position from its observer position
void paratrans_ray_obs2curr(HEALPixRay *ray)
{
double ttensor[2][2],rttensor[2][2];
double obs[3];
//get obseerver position
nest2vec(ray->nest,obs,rayTraceData.rayOrder);
//do the tensors
ttensor[0][0] = ray->Aprev[2*0+0];
ttensor[0][1] = ray->Aprev[2*0+1];
ttensor[1][0] = ray->Aprev[2*1+0];
ttensor[1][1] = ray->Aprev[2*1+1];
paratrans_tangtensor(ttensor,obs,ray->n,rttensor);
ray->Aprev[2*0+0] = rttensor[0][0];
ray->Aprev[2*0+1] = rttensor[0][1];
ray->Aprev[2*1+0] = rttensor[1][0];
ray->Aprev[2*1+1] = rttensor[1][1];
ttensor[0][0] = ray->A[2*0+0];
ttensor[0][1] = ray->A[2*0+1];
ttensor[1][0] = ray->A[2*1+0];
ttensor[1][1] = ray->A[2*1+1];
paratrans_tangtensor(ttensor,obs,ray->n,rttensor);
ray->A[2*0+0] = rttensor[0][0];
ray->A[2*0+1] = rttensor[0][1];
ray->A[2*1+0] = rttensor[1][0];
ray->A[2*1+1] = rttensor[1][1];
}
// converts a ray from ra-dec basis to theta-phi basis
void rot_ray_radec2ang(HEALPixRay *ray)
{
double Ard[2][2];
double alphard[2];
alphard[0] = ray->alpha[0];
alphard[1] = ray->alpha[1];
ray->alpha[0] = -1.0*alphard[1];
ray->alpha[1] = alphard[0];
Ard[0][0] = ray->A[2*0+0];
Ard[1][0] = ray->A[2*1+0];
Ard[0][1] = ray->A[2*0+1];
Ard[1][1] = ray->A[2*1+1];
ray->A[2*0+0] = Ard[1][1];
ray->A[2*1+0] = -1.0*Ard[0][1];
ray->A[2*0+1] = -1.0*Ard[1][0];
ray->A[2*1+1] = Ard[0][0];
Ard[0][0] = ray->Aprev[2*0+0];
Ard[1][0] = ray->Aprev[2*1+0];
Ard[0][1] = ray->Aprev[2*0+1];
Ard[1][1] = ray->Aprev[2*1+1];
ray->Aprev[2*0+0] = Ard[1][1];
ray->Aprev[2*1+0] = -1.0*Ard[0][1];
ray->Aprev[2*0+1] = -1.0*Ard[1][0];
ray->Aprev[2*1+1] = Ard[0][0];
Ard[0][0] = ray->U[2*0+0];
Ard[1][0] = ray->U[2*1+0];
Ard[0][1] = ray->U[2*0+1];
Ard[1][1] = ray->U[2*1+1];
ray->U[2*0+0] = Ard[1][1];
ray->U[2*1+0] = -1.0*Ard[0][1];
ray->U[2*0+1] = -1.0*Ard[1][0];
ray->U[2*1+1] = Ard[0][0];
}
// converts a ray from ra-dec basis to theta-phi basis
void rot_ray_ang2radec(HEALPixRay *ray)
{
double Atp[2][2];
double alphatp[2];
alphatp[0] = ray->alpha[0];
alphatp[1] = ray->alpha[1];
ray->alpha[0] = alphatp[1];
ray->alpha[1] = -1.0*alphatp[0];
Atp[0][0] = ray->A[2*0+0];
Atp[1][0] = ray->A[2*1+0];
Atp[0][1] = ray->A[2*0+1];
Atp[1][1] = ray->A[2*1+1];
ray->A[2*0+0] = Atp[1][1];
ray->A[2*1+0] = -1.0*Atp[0][1];
ray->A[2*0+1] = -1.0*Atp[1][0];
ray->A[2*1+1] = Atp[0][0];
Atp[0][0] = ray->Aprev[2*0+0];
Atp[1][0] = ray->Aprev[2*1+0];
Atp[0][1] = ray->Aprev[2*0+1];
Atp[1][1] = ray->Aprev[2*1+1];
ray->Aprev[2*0+0] = Atp[1][1];
ray->Aprev[2*1+0] = -1.0*Atp[0][1];
ray->Aprev[2*0+1] = -1.0*Atp[1][0];
ray->Aprev[2*1+1] = Atp[0][0];
Atp[0][0] = ray->U[2*0+0];
Atp[1][0] = ray->U[2*1+0];
Atp[0][1] = ray->U[2*0+1];
Atp[1][1] = ray->U[2*1+1];
ray->U[2*0+0] = Atp[1][1];
ray->U[2*1+0] = -1.0*Atp[0][1];
ray->U[2*0+1] = -1.0*Atp[1][0];
ray->U[2*1+1] = Atp[0][0];
}