设计坐标系统类,命名为CoordSys。成员变量包含坐标系统的参考椭球 参数。该参数在构造函数中进行初始化。成员函数包含坐标转换功能函数,具 备XYZ2BLH,BLH2XYZ,XYZ2ENU三个函数。
时间: 2023-12-18 22:05:58 浏览: 78
各种坐标转换和参数
以下是CoordSys类的代码实现:
```c++
#include <cmath>
class CoordSys {
private:
double a; // 参考椭球长半轴
double b; // 参考椭球短半轴
double e2; // 第一偏心率平方
public:
CoordSys(double a, double f) { // 构造函数,传入参考椭球长半轴和扁率f
this->a = a;
this->b = a * (1.0 - f);
this->e2 = (a * a - b * b) / (a * a);
}
// XYZ转BLH
void XYZ2BLH(double X, double Y, double Z, double& B, double& L, double& H) {
double p = sqrt(X * X + Y * Y);
double theta = atan2(Z * a, p * b);
B = atan2(Z + e2 * b * pow(sin(theta), 3), p - e2 * a * pow(cos(theta), 3));
L = atan2(Y, X);
double N = a / sqrt(1 - e2 * pow(sin(B), 2));
H = p / cos(B) - N;
}
// BLH转XYZ
void BLH2XYZ(double B, double L, double H, double& X, double& Y, double& Z) {
double N = a / sqrt(1 - e2 * pow(sin(B), 2));
X = (N + H) * cos(B) * cos(L);
Y = (N + H) * cos(B) * sin(L);
Z = (N * (1 - e2) + H) * sin(B);
}
// XYZ转ENU
void XYZ2ENU(double X, double Y, double Z, double B0, double L0, double& E, double& N, double& U) {
double sinB = sin(B0);
double cosB = cos(B0);
double sinL = sin(L0);
double cosL = cos(L0);
double dx = -X * sinL + Y * cosL;
double dy = -X * sinB * cosL - Y * sinB * sinL + Z * cosB;
double dz = X * cosB * cosL + Y * cosB * sinL + Z * sinB;
E = dx;
N = dy;
U = dz;
}
};
```
使用示例:
```c++
#include <iostream>
#include "CoordSys.h"
int main() {
CoordSys coordSys(6378137.0, 1.0 / 298.257223563); // WGS84参考椭球
double X = 5000000.0;
double Y = 400000.0;
double Z = 1000000.0;
double B, L, H;
coordSys.XYZ2BLH(X, Y, Z, B, L, H);
std::cout << "BLH: " << B << ", " << L << ", " << H << std::endl;
double X2, Y2, Z2;
coordSys.BLH2XYZ(B, L, H, X2, Y2, Z2);
std::cout << "XYZ: " << X2 << ", " << Y2 << ", " << Z2 << std::endl;
double E, N, U;
coordSys.XYZ2ENU(X, Y, Z, B, L, E, N, U);
std::cout << "ENU: " << E << ", " << N << ", " << U << std::endl;
return 0;
}
```
阅读全文