studio_geo_algo_c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. ******************************************************************************
  3. * @file : studio_geo_algo_c.c
  4. * @author : wangyingjie
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2025/5/11
  8. ******************************************************************************
  9. */
  10. #include "studio_geo_algo_c.h"
  11. #include "geography/studio_proj_c.h"
  12. /////////////////// 基础适量算法 ///////////////////
  13. double distance_squared_c(const studio_point_c *p1, const studio_point_c *p2)
  14. {
  15. const double dx = p1->x - p2->x;
  16. const double dy = p1->y - p2->y;
  17. return dx * dx + dy * dy;
  18. }
  19. double point_line_dist_square_c(const studio_point_c *p, const studio_point_c *start, const studio_point_c *end)
  20. {
  21. double A = p->x - start->x;
  22. double B = p->y - start->y;
  23. double C = end->x - start->x;
  24. double D = end->y - start->y;
  25. double dot = A * C + B * D;
  26. double len_sq = C * C + D * D;
  27. double param = len_sq == 0 ? -1 : dot / len_sq;
  28. double xx, yy;
  29. if (param < 0)
  30. {
  31. xx = start->x;
  32. yy = start->y;
  33. } else if (param > 1)
  34. {
  35. xx = end->x;
  36. yy = end->y;
  37. } else
  38. {
  39. xx = start->x + param * C;
  40. yy = start->y + param * D;
  41. }
  42. double dist_square = (xx - p->x) * (xx - p->x) + (yy - p->y) * (yy - p->y);
  43. return dist_square;
  44. }
  45. double azimuth_angle_c(const studio_point_c *p_from, const studio_point_c *p_to)
  46. {
  47. double delta_x = p_to->x - p_from->x;
  48. double delta_y = p_to->y - p_from->y;
  49. double theta_rad = atan2(delta_x, delta_y);
  50. double theta_deg = theta_rad * (180.0 / AO_M_PI);
  51. if (theta_deg < 0.0)
  52. {
  53. theta_deg += 360.0;
  54. }
  55. return theta_deg;
  56. }
  57. double distance_squared_gcs_c(const studio_point_c *p1, const studio_point_c *p2)
  58. {
  59. // 经纬度转高斯投影
  60. // double central = ((int) (p1->x) / 3) * 3;
  61. double central = floor((p1->x + 1.5) / 3.0) * 3.0;
  62. double gx_1, gy_1;
  63. lonlat_to_gauss(central, p1->x, p1->y, &gx_1, &gy_1);
  64. studio_point_c gs_p1 = studio_point_init(gx_1, gy_1);
  65. double gx_2, gy_2;
  66. lonlat_to_gauss(central, p2->x, p2->y, &gx_2, &gy_2);
  67. studio_point_c gs_p2 = studio_point_init(gx_2, gy_2);
  68. double dist_square = distance_squared_c(&gs_p1, &gs_p2);
  69. return dist_square;
  70. }
  71. double point_line_dist_square_gcs_c(const studio_point_c *p, const studio_point_c *start, const studio_point_c *end)
  72. {
  73. // 经纬度转高斯投影
  74. // double central = ((int) (p->x) / 3) * 3;
  75. double central = floor((p->x + 1.5) / 3.0) * 3.0;
  76. double gx_p, gy_p;
  77. lonlat_to_gauss(central, p->x, p->y, &gx_p, &gy_p);
  78. studio_point_c gs_p = studio_point_init(gx_p, gy_p);
  79. double gx_start, gy_start;
  80. lonlat_to_gauss(central, start->x, start->y, &gx_start, &gy_start);
  81. studio_point_c gs_start = studio_point_init(gx_start, gy_start);
  82. double gx_end, gy_end;
  83. lonlat_to_gauss(central, end->x, end->y, &gx_end, &gy_end);
  84. studio_point_c gs_end = studio_point_init(gx_end, gy_end);
  85. double dist_square = point_line_dist_square_c(&gs_p, &gs_start, &gs_end);
  86. return dist_square;
  87. }
  88. double azimuth_angle_gcs_c(const studio_point_c *p_from, const studio_point_c *p_to)
  89. {
  90. // 经纬度转高斯投影
  91. // double central = ((int) (p_from->x) / 3) * 3;
  92. double central = floor((p_from->x + 1.5) / 3.0) * 3.0;
  93. double gx_from, gy_from;
  94. lonlat_to_gauss(central, p_from->x, p_from->y, &gx_from, &gy_from);
  95. studio_point_c gs_from = studio_point_init(gx_from, gy_from);
  96. double gx_to, gy_to;
  97. lonlat_to_gauss(central, p_to->x, p_to->y, &gx_to, &gy_to);
  98. studio_point_c gs_to = studio_point_init(gx_to, gy_to);
  99. double theta_deg = azimuth_angle_c(&gs_from, &gs_to);
  100. return theta_deg;
  101. }
  102. studio_point_c lonlat_move_by(const studio_point_c *pt, double delta_x, double delta_y)
  103. {
  104. // 中央经线
  105. double central = floor((pt->x + 1.5) / 3.0) * 3.0;
  106. // 转换为高斯平面坐标
  107. double gx, gy;
  108. lonlat_to_gauss(central, pt->x, pt->y, &gx, &gy);
  109. // 应用平面坐标偏移(东向加delta_x,北向加delta_y)
  110. const double new_gx = gx + delta_x; // 单位:米
  111. const double new_gy = gy + delta_y;
  112. // 转回经纬度
  113. double new_lon, new_lat;
  114. gauss_to_lonlat(central, new_gx, new_gy, &new_lon, &new_lat);
  115. // 经度范围修正(-180~180)
  116. new_lon = fmod(new_lon + 360.0, 360.0); // 先转0~360
  117. if (new_lon > 180.0)
  118. {
  119. new_lon -= 360.0; // 再转-180~180
  120. }
  121. studio_point_c new_point = studio_point_init(new_lon, new_lat);
  122. return new_point;
  123. }
  124. /////////////////// 矢量(线)抽稀算法 ///////////////////
  125. void douglas_peucker_c(const studio_line_c *points, size_t start, size_t end, double epsilon, unsigned int *indices,
  126. unsigned int *index_count)
  127. {
  128. // 计算最大距离
  129. double max_dist = 0;
  130. size_t index = start;
  131. double epsilon_square = epsilon * epsilon;
  132. // 遍历从start+1到end-1的点
  133. for (size_t i = start + 1; i < end; ++i)
  134. {
  135. // 计算点到线段的距离的平方
  136. double dist_square = point_line_dist_square_c(&points->data[i], &points->data[start], &points->data[end]);
  137. // 如果距离大于最大距离,则更新最大距离和索引
  138. if (dist_square > max_dist)
  139. {
  140. index = i;
  141. max_dist = dist_square;
  142. }
  143. }
  144. // 如果最大距离大于epsilon的平方,并且索引不是start和end,则递归调用Douglas-Peucker算法
  145. if (max_dist > epsilon_square && index != start && index != end)
  146. {
  147. douglas_peucker_c(points, start, index, epsilon, indices, index_count);
  148. douglas_peucker_c(points, index, end, epsilon, indices, index_count);
  149. }
  150. // 否则,将start和end的索引添加到indices数组中
  151. else
  152. {
  153. indices[*index_count] = start;
  154. (*index_count)++;
  155. if (start != end)
  156. {
  157. indices[*index_count] = end;
  158. (*index_count)++;
  159. }
  160. }
  161. }
  162. unsigned int hash(unsigned int value, unsigned int size)
  163. {
  164. return value % size;
  165. }
  166. void remove_duplicates(unsigned int **indices, unsigned int *size)
  167. {
  168. if (*size == 0)
  169. {
  170. return;
  171. }
  172. unsigned int *unique_indices = (unsigned int *) malloc(*size * sizeof(unsigned int));
  173. unsigned int unique_count = 0;
  174. // 创建一个哈希表
  175. unsigned int hash_table_size = *size * 4; // 哈希表大小可以稍微增大以减少冲突 这个可能有问题
  176. bool *hash_table = (bool *) malloc(hash_table_size * sizeof(bool));
  177. for (unsigned int i = 0; i < hash_table_size; i++)
  178. {
  179. hash_table[i] = false; // 初始化哈希表
  180. }
  181. for (unsigned int i = 0; i < *size; i++)
  182. {
  183. unsigned int current_value = (*indices)[i];
  184. unsigned int hash_index = hash(current_value, hash_table_size);
  185. // 检查该元素是否已经在哈希表中
  186. if (!hash_table[hash_index])
  187. {
  188. hash_table[hash_index] = true;
  189. unique_indices[unique_count++] = current_value;
  190. }
  191. }
  192. // 释放原有的 indices 和哈希表
  193. free(*indices);
  194. free(hash_table);
  195. // 更新 indices 指针和大小
  196. *indices = unique_indices;
  197. *size = unique_count;
  198. }
  199. bool line_vacuate_c(const studio_line_c *line, const int max_points, const double epsilon, studio_line_c *vacuate_line)
  200. {
  201. // 初始化状态变量
  202. bool status = false;
  203. // 如果输入的线段为空,则打印错误信息并返回false
  204. if (line->size == 0)
  205. {
  206. printf("line is empty\n");
  207. return false;
  208. }
  209. // 如果输入的线段点数小于等于最大点数,则直接将线段点添加到vacuate_line中,并返回true
  210. if (line->size <= max_points)
  211. {
  212. for (size_t i = 0; i < line->size; ++i)
  213. {
  214. studio_line_c_add_point(vacuate_line, line->data[i]);
  215. }
  216. status = true;
  217. return status;
  218. }
  219. // 分配内存空间存储线段点的索引
  220. unsigned int *indices = (unsigned int *) malloc(line->size * sizeof(unsigned int));
  221. unsigned int index_count = 0;
  222. double t_epsilon = epsilon;
  223. // 循环执行Douglas-Peucker算法,直到索引点数小于等于最大点数
  224. while (1)
  225. {
  226. index_count = 0;
  227. douglas_peucker_c(line, 0, line->size - 1, t_epsilon, indices, &index_count);
  228. // 将indices 中的数据去重
  229. remove_duplicates(&indices, &index_count);
  230. if (index_count <= (unsigned int) max_points)
  231. {
  232. break;
  233. }
  234. t_epsilon *= 1.1;
  235. }
  236. // 将索引点添加到vacuate_line中
  237. for (unsigned int i = 0; i < index_count; ++i)
  238. {
  239. studio_line_c_add_point(vacuate_line, line->data[indices[i]]);
  240. }
  241. // 释放内存空间
  242. free(indices);
  243. status = true;
  244. return status;
  245. }
  246. bool line_vacuate_gcs_c(const studio_line_c *line, const int max_points, const double epsilon,
  247. studio_line_c *vacuate_line)
  248. {
  249. bool status = false;
  250. if (!line || !vacuate_line || line->size <= 1) // 添加空指针和长度校验
  251. {
  252. printf("line or vacuate_line is null or line size <= 1");
  253. return status;
  254. }
  255. // 经纬度转高斯克吕格投影
  256. studio_line_c gauss_line = studio_line_c_init();
  257. // double central = ((int) (line->data[0].x) / 3) * 3;
  258. double central = floor((line->data[0].x + 1.5) / 3.0) * 3.0;
  259. for (unsigned int i = 0; i < line->size; i++)
  260. {
  261. double gx, gy;
  262. lonlat_to_gauss(central, line->data[i].x, line->data[i].y, &gx, &gy);
  263. studio_point_c point = studio_point_init(gx, gy);
  264. studio_line_c_add_point(&gauss_line, point);
  265. }
  266. studio_line_c vac_gauss_line = studio_line_c_init();
  267. bool res = line_vacuate_c(&gauss_line, max_points, epsilon, &vac_gauss_line);
  268. if (!res)
  269. {
  270. printf("Failed to simplify line.");
  271. return status;
  272. }
  273. studio_line_c_destroy(&gauss_line);
  274. // 高斯克吕格转经纬度
  275. for (unsigned int i = 0; i < vac_gauss_line.size; i++)
  276. {
  277. double lon, lat;
  278. gauss_to_lonlat(central, vac_gauss_line.data[i].x, vac_gauss_line.data[i].y, &lon, &lat);
  279. studio_point_c point = studio_point_init(lon, lat);
  280. studio_line_c_add_point(vacuate_line, point);
  281. }
  282. studio_line_c_destroy(&vac_gauss_line);
  283. status = true;
  284. return status;
  285. }
  286. bool remove_outliers_c(const studio_line_c *line, double distance, studio_line_c *outliers_line)
  287. {
  288. bool status = false;
  289. if (!line || !outliers_line || line->size <= 1) // 添加空指针和长度校验
  290. {
  291. printf("line or outliers_line is null or line size <= 1");
  292. return status;
  293. }
  294. double dist_square = distance * distance;
  295. // 经纬度转高斯克吕格投影
  296. studio_line_c gauss_line = studio_line_c_init();
  297. // double central = ((int) (line->data[0].x) / 3) * 3;
  298. double central = floor((line->data[0].x + 1.5) / 3.0) * 3.0;
  299. for (unsigned int i = 0; i < line->size; i++)
  300. {
  301. double gx, gy;
  302. lonlat_to_gauss(central, line->data[i].x, line->data[i].y, &gx, &gy);
  303. studio_point_c point = studio_point_init(gx, gy);
  304. studio_line_c_add_point(&gauss_line, point);
  305. }
  306. // 离群点检测 ------------------------------------------------
  307. if (gauss_line.size <= 1)
  308. {
  309. printf("gauss_line size <= 1");
  310. return status;
  311. }
  312. studio_line_c gs_filtered_line = studio_line_c_init(); // 新建过滤后的线段
  313. studio_line_c_add_point(&gs_filtered_line, gauss_line.data[0]); // 保留第一个点
  314. for (unsigned int i = 1; i < gauss_line.size; i++) // 遍历相邻点对
  315. {
  316. double d_sq = distance_squared_c(&gauss_line.data[i - 1], &gauss_line.data[i]);
  317. if (d_sq <= dist_square)
  318. {
  319. studio_line_c_add_point(&gs_filtered_line, gauss_line.data[i]);
  320. } else
  321. {
  322. int a = 0;
  323. }
  324. }
  325. studio_line_c_destroy(&gauss_line); // 释放原始数据的高斯投影
  326. // 高斯克吕格转经纬度
  327. for (unsigned int i = 0; i < gs_filtered_line.size; i++)
  328. {
  329. double lon, lat;
  330. gauss_to_lonlat(central, gs_filtered_line.data[i].x, gs_filtered_line.data[i].y, &lon, &lat);
  331. studio_point_c point = studio_point_init(lon, lat);
  332. studio_line_c_add_point(outliers_line, point);
  333. }
  334. studio_line_c_destroy(&gs_filtered_line); // 释放移除离群点后的高斯投影
  335. status = true;
  336. return status;
  337. }