1 /*
2  * Copyright (c) 2013-2021, The OSKAR Developers.
3  * See the LICENSE file at the top-level directory of this distribution.
4  */
5 
6 #include "convert/oskar_convert_theta_phi_to_enu_directions.h"
7 #include <math.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
oskar_convert_theta_phi_to_enu_directions(unsigned int num,const oskar_Mem * theta,const oskar_Mem * phi,oskar_Mem * x,oskar_Mem * y,oskar_Mem * z,int * status)13 void oskar_convert_theta_phi_to_enu_directions(unsigned int num,
14         const oskar_Mem* theta, const oskar_Mem* phi, oskar_Mem* x,
15         oskar_Mem* y, oskar_Mem* z, int* status)
16 {
17     if (*status) return;
18     const int type = oskar_mem_type(x);
19     const int location = oskar_mem_location(x);
20     if (oskar_mem_type(y) != type || oskar_mem_type(z) != type ||
21             oskar_mem_type(theta) != type || oskar_mem_type(phi) != type)
22     {
23         *status = OSKAR_ERR_TYPE_MISMATCH;
24         return;
25     }
26     if (oskar_mem_location(y) != location ||
27             oskar_mem_location(z) != location ||
28             oskar_mem_location(theta) != location ||
29             oskar_mem_location(phi) != location)
30     {
31         *status = OSKAR_ERR_LOCATION_MISMATCH;
32         return;
33     }
34     if (location != OSKAR_CPU)
35     {
36         *status = OSKAR_ERR_BAD_LOCATION;
37         return;
38     }
39     if (oskar_mem_length(x) < num || oskar_mem_length(y) < num ||
40             oskar_mem_length(z) < num || oskar_mem_length(theta) < num ||
41             oskar_mem_length(phi) < num)
42     {
43         *status = OSKAR_ERR_DIMENSION_MISMATCH;
44         return;
45     }
46     if (type == OSKAR_DOUBLE)
47     {
48         oskar_convert_theta_phi_to_enu_directions_d(num,
49                 oskar_mem_double_const(theta, status),
50                 oskar_mem_double_const(phi, status),
51                 oskar_mem_double(x, status), oskar_mem_double(y, status),
52                 oskar_mem_double(z, status));
53     }
54     else if (type == OSKAR_SINGLE)
55     {
56         oskar_convert_theta_phi_to_enu_directions_f(num,
57                 oskar_mem_float_const(theta, status),
58                 oskar_mem_float_const(phi, status),
59                 oskar_mem_float(x, status), oskar_mem_float(y, status),
60                 oskar_mem_float(z, status));
61     }
62     else
63     {
64         *status = OSKAR_ERR_BAD_DATA_TYPE;
65         return;
66     }
67 }
68 
oskar_convert_theta_phi_to_enu_directions_d(unsigned int num,const double * theta,const double * phi,double * x,double * y,double * z)69 void oskar_convert_theta_phi_to_enu_directions_d(unsigned int num,
70         const double* theta, const double* phi, double* x, double* y,
71         double* z)
72 {
73     unsigned int i = 0;
74     for (i = 0; i < num; ++i)
75     {
76         const double sin_theta = sin(theta[i]);
77         x[i] = sin_theta * cos(phi[i]);
78         y[i] = sin_theta * sin(phi[i]);
79         z[i] = cos(theta[i]);
80     }
81 }
82 
oskar_convert_theta_phi_to_enu_directions_f(unsigned int num,const float * theta,const float * phi,float * x,float * y,float * z)83 void oskar_convert_theta_phi_to_enu_directions_f(unsigned int num,
84         const float* theta, const float* phi, float* x, float* y,
85         float* z)
86 {
87     unsigned int i = 0;
88     for (i = 0; i < num; ++i)
89     {
90         const float sin_theta = sinf(theta[i]);
91         x[i] = sin_theta * cosf(phi[i]);
92         y[i] = sin_theta * sinf(phi[i]);
93         z[i] = cosf(theta[i]);
94     }
95 }
96 
97 #ifdef __cplusplus
98 }
99 #endif
100