Skip to content

Commit 6347cd3

Browse files
authored
Add files via upload
1 parent 0df5417 commit 6347cd3

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

xyz2enu_v0.1.2.c

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<math.h>
4+
5+
#define version "0.1.2 (20211217T0545Z)"
6+
7+
8+
int main(int argc, char* argv[]) {
9+
long double X0, Y0, Z0, X1, Y1, Z1,
10+
dX, dY, dZ,
11+
lat0, lon0, hgt0;
12+
long double a = 6378137.0,
13+
invf;
14+
//invf = 1.0 / 3.35281066474E-3;
15+
//invf = 298.25722210088;
16+
invf = 298.2572235637;
17+
long double pi = 3.1415926535898;
18+
long double p, f, e2, N, phi0;
19+
20+
long double e, n, u, s01, Az01, z01;
21+
22+
f = 1.0 / invf;
23+
e2 = f * (2.0 -f);
24+
25+
if(argc == 7 ){
26+
//for(int i=0; i<argc; i++)
27+
// printf("[%s ]\n", argv[i]);
28+
X0 = atof(argv[1]);
29+
Y0 = atof(argv[2]);
30+
Z0 = atof(argv[3]);
31+
X1 = atof(argv[4]);
32+
Y1 = atof(argv[5]);
33+
Z1 = atof(argv[6]);
34+
/*
35+
printf("Your inputs: \n");
36+
printf("X0 [%16.4Lf ]\n", X0);
37+
printf("Y0 [%16.4Lf ]\n", Y0);
38+
printf("Z0 [%16.4Lf ]\n", Z0);
39+
printf("X1 [%16.4Lf ]\n", X1);
40+
printf("Y1 [%16.4Lf ]\n", Y1);
41+
printf("Z1 [%16.4Lf ]\n", Z1);
42+
*/
43+
dX = X1 - X0;
44+
dY = Y1 - Y0;
45+
dZ = Z1 - Z0;
46+
47+
p = sqrtl(X0 * X0 + Y0 * Y0);
48+
phi0 = atan2l(Z0, (p * (1.0 - e2)) );
49+
for(int iter=0; iter<10; iter++) {
50+
N = a / sqrt(1.0 - e2 * sin(phi0) * sin(phi0));
51+
hgt0 = p / cos(phi0) - N;
52+
phi0 = atan2l(Z0, (p * (1.0 - e2 * N / (N + hgt0))));
53+
54+
lat0 = phi0;
55+
}
56+
lon0 = atan2l(Y0, X0);
57+
58+
59+
// topocentric to local horizon system (c.f. Ben notes)
60+
61+
// n = -sla*clo*px -sla*slo*py +cla*pz;
62+
// e = -slo*px + clo*py ;
63+
// u = cla*clo*px +cla*slo*py +sla*pz;
64+
65+
// va =Math.asin(u/rho);
66+
// az =Math.atan2(e,n);
67+
68+
69+
e = -sin(lon0) * dX + cos(lon0) * dY + 0.0 * dZ;
70+
n = -sin(lat0) * cos(lon0) * dX - sin(lat0) * sin(lon0) * dY + cos(lat0) * dZ;
71+
u = cos(lat0) * cos(lon0) * dX + cos(lat0) * sin(lon0) * dY + sin(lat0) * dZ;
72+
73+
74+
// Kenu = Rs x Kxyz * Rs'
75+
76+
printf("\nR' -----------------------------------------------------------------\n");
77+
printf("n %20.15f %20.15f %20.15f -sin(lat0)*cos(lon0) | -sin(lat0)*sin(lon0) | cos(lat0)\n", -sin(lat0) * cos(lon0), -sin(lat0) * sin(lon0), cos(lat0) );
78+
printf("e %20.15f %20.15f %20.15f -sin(lon0) | cos(lon0) | 0\n", -sin(lon0), cos(lon0), 0.0 );
79+
printf("u %20.15f %20.15f %20.15f cos(lat0)*cos(lon0) | cos(lat0)*sin(lon0) | sin(lat0)\n", cos(lat0) * cos(lon0), cos(lat0) * sin(lon0), sin(lat0) );
80+
printf("--------------------------------------------------------------------\n\n");
81+
82+
s01 = sqrt(e * e + n * n + u * u);
83+
Az01 = atan2l(e, n);
84+
if(Az01<0.0) Az01 += 2.0 * pi;
85+
z01 = acosl(u / s01);
86+
/*
87+
printf("My outputs: \n");
88+
printf("Lat0 [%16.9Lf ]\n", lat0 * 180.0 / pi);
89+
printf("Lon0 [%16.9Lf ]\n", lon0 * 180.0 / pi);
90+
printf("hgt0 [%16.9Lf ]\n", hgt0);
91+
printf("ENU coordinates: \n");
92+
printf("e [%16.9Lf ]\n", e);
93+
printf("n [%16.9Lf ]\n", n);
94+
printf("u [%16.9Lf ]\n", u);
95+
*/
96+
printf("a f⁻1 = %16.6Lf %16.11Lf\n", a, invf );
97+
printf("XYZ0 XYZ1 = %16.6Lf %16.6Lf %16.6Lf %16.6Lf %16.6Lf %16.6Lf\n", X0, Y0, Z0, X1, Y1, Z1 );
98+
printf("dxyz = %16.6Lf %16.6Lf %16.6Lf \n", dX, dY, dZ);
99+
printf("lat lon hgt = %16.10Lf %16.10Lf %16.10Lf\n", lat0 * 180.0 / pi, lon0 * 180.0 / pi, hgt0);
100+
printf("enu = %16.9Lf %16.9Lf %16.9Lf\n", e, n, u);
101+
printf("s Az z = %16.6Lf %16.10Lf %16.10Lf\n", s01, Az01 * 180.0 / pi, z01 * 180.0 / pi);
102+
// printf("%16.9Lf %16.9Lf %16.9Lf \n%16.6Lf %16.6Lf %16.6Lf %16.6Lf %16.6Lf %16.6Lf \n%16.10Lf %16.10Lf %16.10Lf %16.6Lf %16.10Lf %16.10Lf \n", e, n, u, X0, Y0, Z0, X1, Y1, Z1, lat0 * 180.0 / pi, lon0 * 180.0 / pi, hgt0, s01, Az01 * 180.0 / pi, z01 * 180.0 / pi);
103+
return 0;
104+
} else {
105+
fprintf(stderr, "\nUsage: \n %s X0 Y0 Z0 X1 Y1 Z1\n", argv[0]);
106+
// 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
107+
fprintf(stderr, "Note: by default, uses the WGS84 ellipsoid parameters.\n");
108+
fprintf(stderr, "Note: a : %15.4Lf\n f^-1: %15.10Lf\n", a, invf);
109+
fprintf(stderr, "\n Version: %s\n", version);
110+
return -1;
111+
}
112+
}

0 commit comments

Comments
 (0)