transfer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <adc_tools/transfer.h>
  2. #include <decition_type.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7. #define PI (3.1415926535897932384626433832795)
  8. ///大地转车体
  9. iv::Point2D iv::decition::Coordinate_Transfer(double x_path, double y_path, iv::GPS_INS pos)
  10. {
  11. double x_vehicle, y_vehicle;
  12. double theta = atan2(x_path - pos.gps_x, y_path - pos.gps_y);
  13. double distance = sqrt((x_path - pos.gps_x) * (x_path - pos.gps_x) + (y_path - pos.gps_y) * (y_path - pos.gps_y));
  14. double angle = theta - (-90 + pos.ins_heading_angle) / 180 * PI;
  15. x_vehicle = -cos(angle) * distance;
  16. y_vehicle = sin(angle) * distance;
  17. return iv::Point2D(x_vehicle, y_vehicle);
  18. }
  19. ///车体转大地
  20. iv::GPS_INS iv::decition::Coordinate_UnTransfer(double x_path, double y_path, iv::GPS_INS pos)
  21. {
  22. iv::GPS_INS gps_ins;
  23. double x_vehicle, y_vehicle;
  24. double theta = atan2(x_path, y_path);
  25. double distance = sqrt(x_path * x_path + y_path * y_path);
  26. double angle = (pos.ins_heading_angle / 180 * PI + theta);
  27. x_vehicle = pos.gps_x + distance * sin(angle);
  28. y_vehicle = pos.gps_y + distance * cos(angle);
  29. gps_ins.gps_x = x_vehicle;
  30. gps_ins.gps_y = y_vehicle;
  31. return gps_ins;
  32. }
  33. double iv::decition::GetDistance(iv::Point2D x1, iv::Point2D x2)
  34. {
  35. return sqrt((x1.x - x2.x) * (x1.x - x2.x) + (x1.y - x2.y) * (x1.y - x2.y));
  36. }
  37. double iv::decition::GetDistance(iv::GPS_INS p1, iv::GPS_INS p2)
  38. {
  39. return sqrt((p1.gps_x - p2.gps_x) * (p1.gps_x - p2.gps_x) + (p1.gps_y - p2.gps_y) * (p1.gps_y - p2.gps_y));
  40. }
  41. //GPS转大地坐标
  42. void iv::decition::BLH2XYZ(iv::GPS_INS gp)
  43. {
  44. int nFlag = 2;
  45. double B = gp.gps_lat;
  46. double L = gp.gps_lng;
  47. double N, E, h;
  48. double L0 = GetL0InDegree(L);//根据经度求中央子午线经度
  49. double a = 6378245.0; //地球半径 北京6378245
  50. double F = 298.257223563; //地球扁率
  51. double iPI = 0.0174532925199433; //2pi除以360,用于角度转换
  52. double f = 1 / F;
  53. double b = a * (1 - f);
  54. double ee = (a * a - b * b) / (a * a);
  55. double e2 = (a * a - b * b) / (b * b);
  56. double n = (a - b) / (a + b), n2 = (n * n), n3 = (n2 * n), n4 = (n2 * n2), n5 = (n4 * n);
  57. double al = (a + b) * (1 + n2 / 4 + n4 / 64) / 2;
  58. double bt = -3 * n / 2 + 9 * n3 / 16 - 3 * n5 / 32;
  59. double gm = 15 * n2 / 16 - 15 * n4 / 32;
  60. double dt = -35 * n3 / 48 + 105 * n5 / 256;
  61. double ep = 315 * n4 / 512;
  62. B = B * iPI;
  63. L = L * iPI;
  64. L0 = L0 * iPI;
  65. double l = L - L0, cl = (cos(B) * l), cl2 = (cl * cl), cl3 = (cl2 * cl), cl4 = (cl2 * cl2), cl5 = (cl4 * cl), cl6 = (cl5 * cl), cl7 = (cl6 * cl), cl8 = (cl4 * cl4);
  66. double lB = al * (B + bt * sin(2 * B) + gm * sin(4 * B) + dt * sin(6 * B) + ep * sin(8 * B));
  67. double t = tan(B), t2 = (t * t), t4 = (t2 * t2), t6 = (t4 * t2);
  68. double Nn = a / sqrt(1 - ee * sin(B) * sin(B));
  69. double yt = e2 * cos(B) * cos(B);
  70. N = lB;
  71. N += t * Nn * cl2 / 2;
  72. N += t * Nn * cl4 * (5 - t2 + 9 * yt + 4 * yt * yt) / 24;
  73. N += t * Nn * cl6 * (61 - 58 * t2 + t4 + 270 * yt - 330 * t2 * yt) / 720;
  74. N += t * Nn * cl8 * (1385 - 3111 * t2 + 543 * t4 - t6) / 40320;
  75. E = Nn * cl;
  76. E += Nn * cl3 * (1 - t2 + yt) / 6;
  77. E += Nn * cl5 * (5 - 18 * t2 + t4 + 14 * yt - 58 * t2 * yt) / 120;
  78. E += Nn * cl7 * (61 - 479 * t2 + 179 * t4 - t6) / 5040;
  79. E += 500000;
  80. if (nFlag == 1)
  81. {
  82. //UTM投影
  83. N = 0.9996 * N;
  84. E = 0.9996 * (E - 500000.0) + 500000.0;//Get y
  85. }
  86. if (nFlag == 2)
  87. {
  88. //UTM投影
  89. N = 0.9999 * N;
  90. E = 0.9999 * (E - 500000.0) + 250000.0;//Get y
  91. }
  92. //原
  93. //pt2d.x = N;
  94. //pt2d.y = E;
  95. //
  96. gp.gps_x = E - 280000;
  97. gp.gps_y = N - 4325000;
  98. }
  99. double iv::decition::GetL0InDegree(double dLIn)
  100. {
  101. //3°带求带号及中央子午线经度(d的形式)
  102. //具体公式请参阅《常用大地坐标系及其变换》朱华统,解放军出版社138页
  103. double L = dLIn;//d.d
  104. double L_ddd_Style = L;
  105. double ZoneNumber = (int)((L_ddd_Style - 1.5) / 3.0) + 1;
  106. double L0 = ZoneNumber * 3.0;//degree
  107. return L0;
  108. }