geomatry.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include "geomatry.h"
  2. #include "helpler_funtions.h"
  3. #include "math.h"
  4. #include "my_math.h"
  5. #include "pilot_navigation.h"
  6. #include "soft_time.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #ifndef M_PI
  10. #define M_PI 3.14159265358979323846
  11. #endif
  12. // @brief 给定平面两条直线求交点坐标
  13. // @params const Vector3d* la //直线
  14. // sconst Vector3d* lb //直线
  15. // @retval Coords2d cp 二维交点的坐标
  16. Coords2df lineInterLine(const Vector3ddouble *la, const Vector3ddouble *lb)
  17. {
  18. Coords2df cp;
  19. double a, b, c, A, B, C;
  20. a = la->x;
  21. b = la->y;
  22. c = la->z;
  23. if (b == 0.0f)
  24. {
  25. c = c / a;
  26. a = 1;
  27. }
  28. else
  29. {
  30. a = a / b;
  31. c = c / b;
  32. b = 1;
  33. }
  34. A = lb->x;
  35. B = lb->y;
  36. C = lb->z;
  37. if (B == 0.0f)
  38. {
  39. C = C / A;
  40. A = 1;
  41. }
  42. else
  43. {
  44. A = A / B;
  45. C = C / B;
  46. B = 1;
  47. }
  48. // 不平行才有交点
  49. if (a != A || b != B)
  50. {
  51. cp.x = -(b * C - B * c) / (A * b - a * B);
  52. cp.y = -(A * c - a * C) / (A * b - a * B);
  53. }
  54. return cp;
  55. }
  56. // 函数:求两点直线
  57. // 功能:给定两个点a\b,求所在直线参数
  58. // 参数:const Coords2d* pa //点a
  59. // const Vector2d* pb //点b
  60. // 返回:直calLineEquation线方程参数
  61. Vector3ddouble calLineEquation(const Coords2ddouble *pa,
  62. const Coords2ddouble *pb)
  63. {
  64. double a, b, c;
  65. if (pa->x == pb->x)
  66. {
  67. a = 1;
  68. b = 0;
  69. c = -pa->x;
  70. }
  71. else
  72. {
  73. a = -(double)(pa->y - pb->y) / (double)(pa->x - pb->x);
  74. b = 1;
  75. c = (double)(pa->y * pb->x - pa->x * pb->y) / (double)(pa->x - pb->x);
  76. }
  77. Vector3ddouble params;
  78. params.x = a;
  79. params.y = b;
  80. params.z = c;
  81. return params;
  82. }
  83. // 函数:点在直线上的垂直投影
  84. // 功能:给定一点和一条直线,求该点在该直线上的垂直投影
  85. // 参数:const Coords2d* p //点p
  86. // const Vector3d* lineParam //直线方程参数
  87. // 返回:投影点坐标
  88. Coords2df pointProjection2Line(const Coords2df *p, const Vector3ddouble *params)
  89. {
  90. Coords2df pointProjection;
  91. //如果点在线上,投影点就是该点
  92. if ((int)(params->x * p->x + params->y * p->y + params->z) == 0)
  93. pointProjection = *p;
  94. else
  95. {
  96. //通过该点的与给定直线垂直的直线
  97. double lineParamTemp[3] = {-params->y, params->x,
  98. params->y * p->x - params->x * p->y};
  99. //两条线的交点就是垂直投影点
  100. const Coords3ddouble *param1, *param2;
  101. param1 = (Coords3ddouble *)params;
  102. param2 = (Coords3ddouble *)lineParamTemp;
  103. pointProjection = lineInterLine(param1, param2);
  104. }
  105. return pointProjection;
  106. }
  107. // @brief 判断点在多边形内部
  108. // @param const Coords2dint *point 点坐标指针
  109. // @param const Coords2dint *polygon 多变形顶点坐标指针
  110. // @param int count 多边形顶点个数
  111. // @retval
  112. bool PointInPolygon(const Coords3dint *point, const Coords3dint *polygon,
  113. int count)
  114. {
  115. int cross = 0;
  116. int x_max = polygon[0].x, y_max = polygon[0].y;
  117. int x_min = polygon[0].x, y_min = polygon[0].y;
  118. for (int i = 1; i < count; ++i)
  119. {
  120. if (polygon[i].x > x_max)
  121. x_max = polygon[i].x;
  122. if (polygon[i].x < x_min)
  123. x_min = polygon[i].x;
  124. if (polygon[i].y > y_max)
  125. y_max = polygon[i].y;
  126. if (polygon[i].y < y_min)
  127. y_min = polygon[i].y;
  128. }
  129. if (point->x > x_max || point->x < x_min || point->y > y_max ||
  130. point->y < y_min)
  131. {
  132. return false;
  133. }
  134. for (int i = 0; i < count; ++i)
  135. {
  136. const Coords3dint *p1, *p2;
  137. p1 = &polygon[i];
  138. p2 = &polygon[(i + 1) % count];
  139. // 以 y = point.y 为射线,求其与多变形的交点个数
  140. // p1, p2 连线与 x 轴平行,则为无交点
  141. if (p1->y == p2->y)
  142. continue;
  143. // 交点落在 p1 p2 延长线上
  144. else if (point->y < (p1->y < p2->y ? p1->y : p2->y))
  145. continue;
  146. else if (point->y >= (p1->y > p2->y ? p1->y : p2->y))
  147. continue;
  148. else
  149. {
  150. double x;
  151. x = (double)(point->y - p1->y) * (double)(p2->x - p1->x) /
  152. (double)(p2->y - p1->y) +
  153. p1->x;
  154. if (x > point->x)
  155. {
  156. cross++;
  157. }
  158. }
  159. }
  160. if (cross % 2)
  161. return true;
  162. else
  163. return false;
  164. }
  165. // @brief 求二维平面点到线段的最短距离,以及改点到最短距离点的方位角
  166. // @param *p 点的坐标数据指针
  167. // @param *segment_a 线段端点 a 的坐标指针
  168. // @param *segment_b 线段端点 b 的坐标指针
  169. // @param *dist 点到线段上最近点的距离
  170. // @param *angle 点到线段上最近点的方位角
  171. void PointToSegmentDist(const Coords2dint *p, const Coords2dint *segment_a,
  172. const Coords2dint *segment_b, float *dist, float *angle)
  173. {
  174. // 计算向量 AB, AP
  175. Vector2dint AB = {.x = segment_b->x - segment_a->x,
  176. .y = segment_b->y - segment_a->y};
  177. Vector2dint AP = {.x = p->x - segment_a->x, .y = p->y - segment_a->y};
  178. // 计算 AB * AP
  179. float AB_dot_AP = (float)AB.x * AP.x + (float)AB.y * AP.y;
  180. // 计算 |AB|^2
  181. float AB_dist_squar = (float)AB.x * AB.x + (float)AB.y * (float)AB.y;
  182. // 如果 AB AP 的点积小于零,说明二者夹角大于 90 deg,PA 为最短距离
  183. if (AB_dot_AP < 0.0f)
  184. {
  185. *dist = get_norm(segment_a->x - p->x, segment_a->y - p->y);
  186. *angle = atan2f(segment_a->x - p->x, segment_a->y - p->y) * RAD_TO_DEG;
  187. }
  188. // 如果 AB AP 的夹角小于 90 deg
  189. else
  190. {
  191. if (AB_dot_AP > AB_dist_squar)
  192. {
  193. // 如果 AP 在 AB 上的投影超过了 AB,那么最短距离为 PB
  194. *dist = get_norm(segment_b->x - p->x, segment_b->y - p->y);
  195. *angle =
  196. atan2f(segment_b->x - p->x, segment_b->y - p->y) * RAD_TO_DEG;
  197. }
  198. else
  199. {
  200. // 如果 AP 在 AB 上的投影在 AB 线段内,则最短距离为 AP 在 AB
  201. // 法线方向上的投影
  202. // 角度为 AB 方向 +- (pi / 2)
  203. float angle_AB = atan2f(segment_b->x - segment_a->x,
  204. segment_b->y - segment_a->y);
  205. float angle_AP = atan2f(p->x - segment_a->x, p->y - segment_a->y);
  206. float angle_AP_AB = wrap_pi(angle_AB - angle_AP);
  207. *dist = get_norm(p->x - segment_a->x, p->y - segment_a->y) *
  208. sinf(angle_AP_AB);
  209. if (angle_AP_AB > 0.0f)
  210. {
  211. *angle = wrap_pi(angle_AB + M_PI_F / 2.0f) * RAD_TO_DEG;
  212. }
  213. else
  214. {
  215. *angle = wrap_pi(angle_AB - M_PI_F / 2.0f) * RAD_TO_DEG;
  216. }
  217. }
  218. }
  219. }
  220. // @brief 求点到多边形最短距离
  221. // @param *p 点的坐标数据指针
  222. // @param *polygon 多边形顶点坐标数据指针
  223. // @param count 多边形顶点个数,从 1 开始数
  224. // @param *dist 传出结果,点到多边形的最短距离
  225. // @param *angle 传出结果,到多边形最近点的方位角,单位 deg
  226. void PointToPolygonDist(const Coords3dint *p, const Coords3dint *polygon,
  227. int count, float *dist, float *angle)
  228. {
  229. float min_dist = INFINITY, min_dist_angle = 0.0f;
  230. for (int i = 0; i < count; ++i)
  231. {
  232. Coords2dint A = {.x = polygon[i].x, .y = polygon[i].y};
  233. Coords2dint B = {.x = polygon[(i + 1) / count].x,
  234. .y = polygon[(i + 1) / count].y};
  235. Coords2dint P = {p->x, p->y};
  236. float tmp_dist, tmp_angle;
  237. PointToSegmentDist(&P, &A, &B, &tmp_dist, &tmp_angle);
  238. if (tmp_dist < min_dist)
  239. {
  240. min_dist = tmp_dist;
  241. min_dist_angle = tmp_angle;
  242. }
  243. }
  244. *dist = min_dist;
  245. *angle = min_dist_angle;
  246. }
  247. static double deg2rad(double deg) { return deg * M_PI / 180; }
  248. /**
  249. * @brief Vincenty 经纬度转平面距离
  250. *
  251. * @param latA A 点纬度
  252. * @param lngA A 点经度
  253. * @param latB B 点纬度
  254. * @param lngB B 点经度
  255. * @return double 距离
  256. */
  257. double distanceVincenty(double latA, double lngA, double latB, double lngB)
  258. {
  259. double b = 6356752.3142;
  260. double f = 1 / 298.257223563;
  261. const double earthR = 6378137.0;
  262. double L = deg2rad(lngB - lngA);
  263. double U1 = atan((1 - f) * tan(deg2rad(latA)));
  264. double U2 = atan((1 - f) * tan(deg2rad(latB)));
  265. double sinU1 = sin(U1);
  266. double cosU1 = cos(U1);
  267. double sinU2 = sin(U2);
  268. double cosU2 = cos(U2);
  269. double lambda = L;
  270. double lambdaP = 2 * M_PI;
  271. double iterLimit = 20;
  272. double sinLambda, cosLambda;
  273. double sinSigma = 0;
  274. double cosSigma = 0;
  275. double sigma = 0;
  276. double alpha;
  277. double cosSqAlpha = 0;
  278. double cos2SigmaM = 0;
  279. double C;
  280. while (fabs(lambda - lambdaP) > 1e-12 && --iterLimit > 0)
  281. {
  282. sinLambda = sin(lambda);
  283. cosLambda = cos(lambda);
  284. sinSigma = sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda) +
  285. (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) *
  286. (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
  287. if (sinSigma == 0)
  288. {
  289. return 0; // co-incident points
  290. }
  291. cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
  292. sigma = atan2(sinSigma, cosSigma);
  293. alpha = asin(cosU1 * cosU2 * sinLambda / sinSigma);
  294. cosSqAlpha = cos(alpha) * cos(alpha);
  295. cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
  296. C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
  297. lambdaP = lambda;
  298. lambda = L + (1 - C) * f * sin(alpha) *
  299. (sigma + C * sinSigma *
  300. (cos2SigmaM +
  301. C * cosSigma *
  302. (-1 + 2 * cos2SigmaM * cos2SigmaM)));
  303. }
  304. if (iterLimit == 0)
  305. {
  306. return 0.0; // formula failed to converge
  307. }
  308. double uSq = cosSqAlpha * (earthR * earthR - b * b) / (b * b);
  309. double A =
  310. 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
  311. double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
  312. double deltaSigma =
  313. B * sinSigma *
  314. (cos2SigmaM + B / 4 *
  315. (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) -
  316. B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) *
  317. (-3 + 4 * cos2SigmaM * cos2SigmaM)));
  318. return b * A * (sigma - deltaSigma);
  319. }
  320. /**
  321. * @brief 计算经纬度转平面坐标距离的系数
  322. *
  323. * @param lat 纬度
  324. * @param lng 经度
  325. * @param ratiox 经度转平面距离m系数
  326. * @param ratioy 纬度转平面距离m系数
  327. */
  328. void Cal_LngLat_To_FlatM_Ratio(double lat, double lng, double *ratiox,
  329. double *ratioy)
  330. {
  331. double DIFF = 0.0001;
  332. double deltax = distanceVincenty(lat, lng, lat, lng + DIFF);
  333. double deltay = distanceVincenty(lat, lng, lat + DIFF, lng);
  334. *ratiox = deltax / DIFF;
  335. *ratioy = deltay / DIFF;
  336. }
  337. /**
  338. * @brief 根据经纬度计算距离和方位角
  339. *
  340. * @param lon1 经度1
  341. * @param lat1 纬度1
  342. * @param lon2 经度2
  343. * @param lat2 纬度2
  344. * @param lonToM 经度差转平面距离m系数
  345. * @param latToM 纬度差转平面距离m系数
  346. * @param dist_m 距离m
  347. * @param bearing_rad 1->2方位角rad
  348. */
  349. void LanLat_To_Flat_Dist_Bearing(int lon1, int lat1, int lon2, int lat2,
  350. double lonToM, double latToM, float *dist_m,
  351. float *bearing_rad)
  352. {
  353. double lon_e = wrap_double(lon2 - lon1, -180.0 * 1e7, 180.0 * 1e7);
  354. double lat_e = wrap_double(lat2 - lat1, -180.0 * 1e7, 180 * 1e7);
  355. double lon_delta = (double)lon_e * 1e-7;
  356. double lat_delta = (double)lat_e * 1e-7;
  357. float dist_e = lonToM * lon_delta;
  358. float dist_n = latToM * lat_delta;
  359. *dist_m = get_norm(dist_e, dist_n);
  360. *bearing_rad = atan2f(dist_e, dist_n);
  361. }