test_algo_c.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. ******************************************************************************
  3. * @file : task_alog_c.cpp
  4. * @author : wyj
  5. * @brief : C语言语法测试
  6. * @attention : None
  7. * @date : 2025/5/9
  8. ******************************************************************************
  9. */
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. #include "control/pid/studio_pid_c.h"
  15. #include "geography/studio_proj_c.h"
  16. #include "geometry/studio_geo_algo_c.h"
  17. #ifdef __cplusplus
  18. }
  19. #endif
  20. #include <stdio.h>
  21. #include <stdio.h>
  22. #include "geometry/studio_geo_utils.h"
  23. #include "file/studio_dir.h"
  24. void studio_line_to_c_line(const studio_line& line, studio_line_c* c_line)
  25. {
  26. for (size_t i = 0; i < line.size(); ++i)
  27. {
  28. studio_point_c p = studio_point_init(line[i].x, line[i].y);
  29. studio_line_c_add_point(c_line, p);
  30. }
  31. }
  32. void c_line_to_studio_line(const studio_line_c* c_line, studio_line& line)
  33. {
  34. for (size_t i = 0; i < c_line->size; ++i)
  35. {
  36. const studio_point_c* p = studio_line_c_get_point(c_line, i);
  37. line.push_back(studio_point(p->x, p->y));
  38. }
  39. }
  40. ///////////////////////////////// 以上是工具转化函数 //////////////////////////////////
  41. ////////////////// 路径压缩算法测试 /////////////////////
  42. int main()
  43. {
  44. printf("\n\n===================== %s =====================\n\n", __FILE__);
  45. studio_geo_utils::init_gdal_env();
  46. std::string src_root;
  47. std::string dst_root;
  48. #ifdef IS_WINDOWS
  49. src_root = "D:/9_data/2_readfile/geojson/path_compress/";
  50. dst_root = "D:/9_data/2_readfile/geojson/path_compress/dst/";
  51. #else
  52. src_root = "/home/wyj/myself/2_data/2_geojson/path_compress/";
  53. dst_root = "/home/wyj/myself/2_data/2_geojson/path_compress/";
  54. #endif
  55. std::vector<std::string> all_paths = studio_dir::getFilesOrDirs(src_root);
  56. for (const auto& path : all_paths)
  57. {
  58. std::cout << "src path: " << path << std::endl;
  59. std::vector<studio_geo_coll> res_collections;
  60. std::vector<studio_geo_coll> collections;
  61. studio_geo_utils::read_geo_coll(path, collections);
  62. for (auto& coll : collections)
  63. {
  64. studio_line_c line_c = studio_line_c_init();
  65. studio_line_c vac_line_c = studio_line_c_init();
  66. studio_line_to_c_line(coll.m_line, &line_c);
  67. std::cout << "原始数据量:" << line_c.size << std::endl;
  68. // 简化线段,目标点数为30个
  69. int max_points = 30;
  70. double epsilon = 1.0;
  71. bool res = line_vacuate_gcs_c(&line_c, max_points, epsilon, &vac_line_c);
  72. if (!res)
  73. {
  74. std::cout << "Failed to simplify line." << std::endl;
  75. return 1;
  76. }
  77. studio_line simplified_line;
  78. c_line_to_studio_line(&vac_line_c, simplified_line);
  79. std::cout << "简化后数据量:" << vac_line_c.size << std::endl;
  80. studio_geo_coll temp;
  81. if (simplified_line.size() > 0)
  82. {
  83. temp.m_type = enum_geometry_type::egtLineString;
  84. temp.m_line = simplified_line;
  85. res_collections.push_back(temp);
  86. }
  87. studio_line_c_destroy(&line_c);
  88. studio_line_c_destroy(&vac_line_c);
  89. break;
  90. }
  91. std::string name = std::filesystem::path(path).filename().stem().string();
  92. std::string output_path = std::filesystem::path(dst_root).append(name + "_dst.geojson").string();
  93. studio_geo_utils::write_geo_coll(output_path, res_collections);
  94. }
  95. studio_geo_utils::destroy_gdal_env();
  96. return 0;
  97. }
  98. //// ///////////////////// 路径压缩错误测试 /////////////////////
  99. //
  100. // int main()
  101. //{
  102. // printf("\n\n===================== %s =====================\n\n", __FILE__);
  103. // studio_geo_utils::init_gdal_env();
  104. // std::string path;
  105. // #ifdef IS_WINDOWS
  106. // path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  107. // #else
  108. // path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  109. // #endif
  110. //
  111. // std::cout << "path: " << path << std::endl;
  112. // std::vector<studio_geo_coll> res_collections;
  113. // std::vector<studio_geo_coll> collections;
  114. //
  115. // // ------------- 转换为高斯投影 -------------
  116. // studio_line gauss_line;
  117. // double central = static_cast<int>(120.039001 / 3) * 3;
  118. //
  119. // studio_line_c gcs_line_c = studio_line_c_init();
  120. // studio_line_c vac_gcs_line_c = studio_line_c_init();
  121. //
  122. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.039001, 30.982000));
  123. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.380996, 30.843999));
  124. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.469001, 30.371999));
  125. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.453002, 30.420000));
  126. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.277000, 30.131999));
  127. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.691001, 30.601999));
  128. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.210998, 30.250000));
  129. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.544998, 30.871999));
  130. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.752998, 30.440000));
  131. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.864997, 30.0000000));
  132. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.102996, 30.125999));
  133. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.766998, 30.365999));
  134. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.583000, 30.158000));
  135. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.663002, 30.326000));
  136. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.406997, 30.445999));
  137. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.516998, 30.131999));
  138. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.972999, 30.555999));
  139. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.654998, 30.253999));
  140. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.838996, 30.270000));
  141. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.855003, 30.110000));
  142. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.060997, 30.131999));
  143. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.925003, 30.139999));
  144. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.845001, 30.299999));
  145. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.189002, 30.299999));
  146. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.861000, 30.211999));
  147. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.154998, 30.722000));
  148. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.306999, 30.610000));
  149. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.302001, 30.745000));
  150. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.152999, 30.079999));
  151. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.425003, 30.656000));
  152. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.782997, 30.054000));
  153. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.151000, 30.517999));
  154. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.303001, 30.558000));
  155. // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.238998, 30.733999));
  156. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07207105000001,36.15950345000000));
  157. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07206542000000,36.15950579000000));
  158. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07205912000001,36.15950948000000));
  159. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07205174000001,36.15951394000000));
  160. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07204096000000,36.15952022000000));
  161. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07203156000000,36.15952566000000));
  162. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07202411999999,36.15952983000000));
  163. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07200829000000,36.15953788000000));
  164. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07199709000000,36.15954257000000));
  165. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07198529000000,36.15954627000000));
  166. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07172613000000,36.15949536000000));
  167. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07171595000000,36.15949109000000));
  168. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07170811000000,36.15948855000000));
  169. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07169195000000,36.15948536000000));
  170. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07168092000001,36.15948474000000));
  171. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07166998000000,36.15948535000000));
  172. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07166058000000,36.15948630000000));
  173. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07165083000000,36.15948751000000));
  174. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07164358999999,36.15948821000000));
  175. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07163668000000,36.15948832000000));
  176. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155865000000,36.15941322000000));
  177. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155723000000,36.15940347000000));
  178. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155523000000,36.15939333000000));
  179. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155331000000,36.15938564000000));
  180. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07154843000001,36.15937024000000));
  181. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07154410000000,36.15936034000000));
  182. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07153860000000,36.15935092000000));
  183. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07153018000000,36.15934010000000));
  184. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07152281000000,36.15933237000000));
  185. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07151780000000,36.15932673000000));
  186. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07151684000000,36.15932353000000));
  187. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07152006000000,36.15932156000000));
  188. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07152524000000,36.15932060000000));
  189. // // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07153203999999,36.15931997000000));
  190. //
  191. // std::cout << "befor line size:" << gcs_line_c.size << std::endl;
  192. // {
  193. // studio_line stu_line;
  194. // c_line_to_studio_line(&gcs_line_c, stu_line);
  195. // studio_geo_coll temp;
  196. // temp.m_type = enum_geometry_type::egtLineString;
  197. // temp.m_line = stu_line;
  198. // collections.push_back(temp);
  199. // std::string temp_out = "/home/wyj/myself/2_data/2_geojson/multi_point/20250603_aaa.geojson";
  200. // studio_geo_utils::write_geo_coll(temp_out, collections);
  201. // }
  202. //
  203. //
  204. // // 简化线段,目标点数为28个
  205. // int max_points = 30;
  206. // double epsilon = 1.0;
  207. //
  208. // bool res = line_vacuate_gcs_c(&gcs_line_c, max_points, epsilon, &vac_gcs_line_c);
  209. // if (!res)
  210. // {
  211. // std::cout << "Failed to simplify line." << std::endl;
  212. // return 1;
  213. // }
  214. // std::cout << "Simplified line size:" << vac_gcs_line_c.size << std::endl;
  215. // studio_line simplified_line;
  216. // c_line_to_studio_line(&vac_gcs_line_c, simplified_line);
  217. //
  218. // studio_geo_coll temp;
  219. // temp.m_type = enum_geometry_type::egtLineString;
  220. // temp.m_line = simplified_line;
  221. // res_collections.push_back(temp);
  222. //
  223. // studio_line_c_destroy(&gcs_line_c);
  224. // studio_line_c_destroy(&vac_gcs_line_c);
  225. //
  226. // std::string output_path;
  227. // #ifdef IS_WINDOWS
  228. // output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
  229. // #else
  230. // output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/20250603_res.geojson";
  231. // #endif
  232. // studio_geo_utils::write_geo_coll(output_path, res_collections);
  233. //
  234. // studio_geo_utils::destroy_gdal_env();
  235. //
  236. // return 0;
  237. //}
  238. //int main()
  239. //{
  240. // printf("===================== %s =====================\n", __FILE__);
  241. //
  242. // // 初始化线段
  243. // studio_line_c line = studio_line_c_init();
  244. //
  245. // // 添加点
  246. // for (int i = 0; i < 10; ++i)
  247. // {
  248. // studio_point_c p = studio_point_init(i * 1.0, i * 2.0);
  249. // studio_line_c_add_point(&line, p);
  250. // }
  251. //
  252. // // 遍历点
  253. // for (size_t i = 0; i < line.size; ++i)
  254. // {
  255. // studio_point_c p = studio_line_c_get_point(&line, i);
  256. // printf("Point %zu: (%.1f, %.1f)\n", i, p.x, p.y);
  257. // }
  258. // for (size_t i = 0; i < line.size; ++i)
  259. // {
  260. // studio_point_c p = studio_point_init(111, 111);
  261. // studio_line_c_set_point(&line, i, p);
  262. // }
  263. // for (size_t i = 0; i < line.size; ++i)
  264. // {
  265. // studio_point_c p = studio_line_c_get_point(&line, i);
  266. // printf("Point %zu: (%.1f, %.1f)\n", i, p.x, p.y);
  267. // }
  268. //
  269. // // 释放内存
  270. // studio_line_c_destroy(&line);
  271. // return 0;
  272. //
  273. //
  274. //
  275. // return 0;
  276. //}
  277. // ///////////////////// PID算法 /////////////////////
  278. // int main()
  279. // {
  280. // double ini = 2, goal = 50, p = 0.5, i = 0.001, d = 0.6;
  281. // studio_pid pid;
  282. //
  283. // // 初始化PID
  284. // init_pid(&pid, ini, goal, p, i, d, 100, 200);
  285. //
  286. // // 第一阶段控制到 50
  287. // double current_value = ini;
  288. // for (int i = 0; i < 20; ++i)
  289. // {
  290. // double output = compute_pid(&pid, current_value);
  291. // current_value += output; // 积分项和微分项会影响下一次计算
  292. // printf("Step %d: 目标=%.2f, 当前值=%.2f, 输出=%.2f\n", i + 1, get_goal(&pid), current_value, output);
  293. // }
  294. //
  295. // printf("到达目标值,开始第二阶段控制\n");
  296. // // 改变目标值并重置PID状态
  297. // set_goal(&pid, 100);
  298. //
  299. // // 第二阶段控制到 100
  300. // for (int i = 0; i < 20; ++i)
  301. // {
  302. // double output = compute_pid(&pid, current_value);
  303. // current_value += output; // 积分项和微分项会影响下一次计算
  304. // printf("Step %d: 目标=%.2f, 当前值=%.2f, 输出=%.2f\n", i + 1, get_goal(&pid), current_value, output);
  305. // }
  306. //
  307. // return 0;
  308. // }
  309. // ///////////////////// 移除离群点 /////////////////////
  310. // int main()
  311. // {
  312. // printf("\n\n===================== %s =====================\n\n", __FILE__);
  313. // studio_geo_utils::init_gdal_env();
  314. // std::string path;
  315. // #ifdef IS_WINDOWS
  316. // path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  317. // #else
  318. // path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  319. // #endif
  320. //
  321. // std::cout << "path: " << path << std::endl;
  322. // std::vector<studio_geo_coll> res_collections;
  323. // std::vector<studio_geo_coll> collections;
  324. // studio_geo_utils::read_geo_coll(path, collections);
  325. //
  326. // for (auto &coll: collections)
  327. // {
  328. // studio_line_c line_c = studio_line_c_init();
  329. // studio_line_to_c_line(coll.m_line, &line_c);
  330. // std::cout << "原始数据量:" << coll.m_line.size() << std::endl;
  331. //
  332. // studio_line_c outliers_line = studio_line_c_init();
  333. // remove_outliers_c(&line_c, 1, &outliers_line);
  334. //
  335. // studio_line simplified_line;
  336. // c_line_to_studio_line(&outliers_line, simplified_line);
  337. // std::cout << "简化后数据量:" << simplified_line.size() << std::endl;
  338. //
  339. // studio_geo_coll temp;
  340. // temp.m_type = enum_geometry_type::egtLineString;
  341. // temp.m_line = simplified_line;
  342. // res_collections.push_back(temp);
  343. //
  344. // studio_line_c_destroy(&line_c);
  345. // studio_line_c_destroy(&outliers_line);
  346. //
  347. // break;
  348. // }
  349. //
  350. //
  351. // std::string output_path;
  352. // #ifdef IS_WINDOWS
  353. // output_path = "D:/5_file/2_readfile/geojson/multi_point/remove_outliers.geojson";
  354. // #else
  355. // output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/remove_outliers.geojson";
  356. // #endif
  357. // studio_geo_utils::write_geo_coll(output_path, res_collections);
  358. // studio_geo_utils::destroy_gdal_env();
  359. // return 0;
  360. // }
  361. // int main()
  362. //{
  363. // printf("\n\n===================== %s =====================\n\n", __FILE__);
  364. // studio_geo_utils::init_gdal_env();
  365. // std::string path;
  366. // #ifdef IS_WINDOWS
  367. // path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
  368. // #else
  369. // path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
  370. // #endif
  371. // std::cout << "path: " << path << std::endl;
  372. // std::vector<studio_geo_coll> res_collections;
  373. // std::vector<studio_geo_coll> collections;
  374. // studio_geo_utils::read_geo_coll(path, collections);
  375. // for (auto &coll : collections)
  376. // {
  377. // // ------------- 转换为高斯投影 -------------
  378. // studio_line gauss_line;
  379. // double central = static_cast<int>(coll.m_line[0].x / 3) * 3;
  380. // for (auto &point : coll.m_line)
  381. // {
  382. // double gx = 0.0;
  383. // double gy = 0.0;
  384. // lonlat_to_gauss(central, point.x, point.y, &gx, &gy);
  385. // gauss_line.push_back(studio_point(gx, gy));
  386. // }
  387. // studio_line_c gauss_line_c = studio_line_c_init();
  388. // studio_line_c vac_gauss_line_c = studio_line_c_init();
  389. // studio_line_to_c_line(gauss_line, &gauss_line_c);
  390. // // 简化线段,目标点数为28个
  391. // int max_points = 28;
  392. // double epsilon = 1.0;
  393. // bool res = line_vacuate_c(&gauss_line_c, max_points, epsilon, &vac_gauss_line_c);
  394. // if (!res)
  395. // {
  396. // std::cout << "Failed to simplify line." << std::endl;
  397. // return 1;
  398. // }
  399. // // 高斯投影在转回经纬度
  400. // studio_line simplified_line;
  401. // for (int i = 0; i < vac_gauss_line_c.size; i++)
  402. // {
  403. // double lon = 0.0;
  404. // double lat = 0.0;
  405. // const studio_point_c *p = studio_line_c_get_point(&vac_gauss_line_c, i);
  406. // gauss_to_lonlat(central, p->x, p->y, &lon, &lat);
  407. // simplified_line.push_back(studio_point(lon, lat));
  408. // }
  409. // studio_geo_coll temp;
  410. // temp.m_type = enum_geometry_type::egtLineString;
  411. // temp.m_line = simplified_line;
  412. // res_collections.push_back(temp);
  413. // studio_line_c_destroy(&gauss_line_c);
  414. // studio_line_c_destroy(&vac_gauss_line_c);
  415. // break;
  416. // }
  417. // std::string output_path;
  418. // #ifdef IS_WINDOWS
  419. // output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
  420. // #else
  421. // output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples_res_1_c.geojson";
  422. // #endif
  423. // studio_geo_utils::write_geo_coll(output_path, res_collections);
  424. // studio_geo_utils::destroy_gdal_env();
  425. // return 0;
  426. //}