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_enu_to_offset_ecef.h"
7 #include <math.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /* Double precision. */
oskar_convert_enu_to_offset_ecef_d(int num_points,const double * horizon_x,const double * horizon_y,const double * horizon_z,double lon_rad,double lat_rad,double * offset_ecef_x,double * offset_ecef_y,double * offset_ecef_z)14 void oskar_convert_enu_to_offset_ecef_d(int num_points,
15         const double* horizon_x, const double* horizon_y,
16         const double* horizon_z, double lon_rad, double lat_rad,
17         double* offset_ecef_x, double* offset_ecef_y, double* offset_ecef_z)
18 {
19     /* Precompute some trig. */
20     int i = 0;
21     const double sin_lon = sin(lon_rad);
22     const double cos_lon = cos(lon_rad);
23     const double sin_lat = sin(lat_rad);
24     const double cos_lat = cos(lat_rad);
25 
26     /* Loop over points. */
27     for (i = 0; i < num_points; ++i)
28     {
29         double xi = 0.0, yi = 0.0, zi = 0.0, xt = 0.0, yt = 0.0, zt = 0.0;
30 
31         /* Get the input coordinates. */
32         xi = horizon_x[i];
33         yi = horizon_y[i];
34         zi = horizon_z[i];
35 
36         /* Apply rotation matrix. */
37         xt = -xi * sin_lon - yi * sin_lat * cos_lon + zi * cos_lat * cos_lon;
38         yt =  xi * cos_lon - yi * sin_lat * sin_lon + zi * cos_lat * sin_lon;
39         zt =  yi * cos_lat + zi * sin_lat;
40 
41         /* Save the rotated values. */
42         offset_ecef_x[i] = xt;
43         offset_ecef_y[i] = yt;
44         offset_ecef_z[i] = zt;
45     }
46 }
47 
48 /* Single precision. */
oskar_convert_enu_to_offset_ecef_f(int num_points,const float * horizon_x,const float * horizon_y,const float * horizon_z,float lon_rad,float lat_rad,float * offset_ecef_x,float * offset_ecef_y,float * offsec_ecef_z)49 void oskar_convert_enu_to_offset_ecef_f(int num_points,
50         const float* horizon_x, const float* horizon_y,
51         const float* horizon_z, float lon_rad, float lat_rad,
52         float* offset_ecef_x, float* offset_ecef_y, float* offsec_ecef_z)
53 {
54     /* Precompute some trig. */
55     int i = 0;
56     const double sin_lon = sin((double)lon_rad);
57     const double cos_lon = cos((double)lon_rad);
58     const double sin_lat = sin((double)lat_rad);
59     const double cos_lat = cos((double)lat_rad);
60 
61     /* Loop over points. */
62     for (i = 0; i < num_points; ++i)
63     {
64         double xi = 0.0, yi = 0.0, zi = 0.0, xt = 0.0, yt = 0.0, zt = 0.0;
65 
66         /* Get the input coordinates. */
67         xi = (double) (horizon_x[i]);
68         yi = (double) (horizon_y[i]);
69         zi = (double) (horizon_z[i]);
70 
71         /* Apply rotation matrix. */
72         xt = -xi * sin_lon - yi * sin_lat * cos_lon + zi * cos_lat * cos_lon;
73         yt =  xi * cos_lon - yi * sin_lat * sin_lon + zi * cos_lat * sin_lon;
74         zt =  yi * cos_lat + zi * sin_lat;
75 
76         /* Save the rotated values. */
77         offset_ecef_x[i] = (float)xt;
78         offset_ecef_y[i] = (float)yt;
79         offsec_ecef_z[i] = (float)zt;
80     }
81 }
82 
83 #ifdef __cplusplus
84 }
85 #endif
86