select_tool.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. #ifndef __SELECT_TOOL__H
  2. #define __SELECT_TOOL__H
  3. #include <mutex>
  4. #include "loc_point.h"
  5. #include "line.h"
  6. #include <memory>
  7. #include "ant.h"
  8. #include "card_path.h"
  9. #include "loc_message.h"
  10. #include <zexception.h>
  11. //#include "optimize_tool.h"
  12. struct solpoint:point
  13. {
  14. solpoint()
  15. :m_score(100)
  16. {
  17. }
  18. double m_score;
  19. bool operator<(const solpoint&p)const
  20. {
  21. return m_score<p.m_score;
  22. }
  23. void set_sol(const point&p,double score=100)
  24. {
  25. set(p);
  26. this->m_score=score;
  27. }
  28. double score()const
  29. {
  30. return m_score;
  31. }
  32. };
  33. struct push_data_point:point
  34. {
  35. push_data_point()
  36. :ct(0)
  37. ,valid(false)
  38. ,stop_cnt(0)
  39. {
  40. }
  41. point dstp;
  42. int ct;
  43. bool valid; // if valid
  44. int stop_cnt; // if calculate speed
  45. loc_point lp;
  46. };
  47. class select_point_object;
  48. struct select_tool
  49. {
  50. std::mutex m_mtx;
  51. zlist<push_data_point,16> m_push_list;
  52. select_point_object *m_spo=nullptr;
  53. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm)=0;
  54. push_data_point getDpt()
  55. {
  56. push_data_point dpt;
  57. std::lock_guard<std::mutex> lock(m_mtx);
  58. if (m_push_list.empty()) //empty
  59. {
  60. return dpt;
  61. }
  62. else if(m_push_list.size()==1) //size=1
  63. {
  64. dpt = m_push_list[0];
  65. m_push_list[0].valid=false;
  66. if(m_push_list[0].stop_cnt>5)m_push_list[0].stop_cnt=5;
  67. if(m_push_list[0].stop_cnt>0)m_push_list[0].stop_cnt--;
  68. }
  69. else{ //size>1
  70. dpt = m_push_list[0];
  71. m_push_list.skip(1);
  72. }
  73. return dpt;
  74. }
  75. virtual ~select_tool();
  76. };
  77. //--------------person------solution one--------
  78. struct select_tool_person_1:select_tool
  79. {
  80. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm);
  81. ~select_tool_person_1()
  82. {
  83. }
  84. };
  85. struct select_tool_person_2:select_tool
  86. {
  87. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm)
  88. {
  89. loc_point lp;
  90. return lp;
  91. }
  92. };
  93. //----------------------car----------
  94. struct select_tool_car_1:select_tool
  95. {
  96. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm);
  97. };
  98. //---------------------drivingfaceCar
  99. struct select_tool_drivingface_car_1:select_tool
  100. {
  101. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm)
  102. {
  103. loc_point lp;
  104. return lp;
  105. }
  106. };
  107. //----------------------------------------
  108. struct select_point_object
  109. {
  110. select_tool * m_owner;
  111. public:
  112. select_point_object(select_tool * owner)
  113. :m_owner(owner)
  114. ,m_ct(-10)
  115. {
  116. att_initiate();
  117. }
  118. // 上次ct号
  119. int last_ct()
  120. {
  121. return m_ct;
  122. }
  123. public:
  124. const static int max_histime=60; //±£Áô×îºó60sµÄÊý¾Ý
  125. zlist<loc_point,128> m_d;
  126. line m_line;
  127. int m_ct = -10; // 计数号
  128. fit_batch m_fitk,m_fita;
  129. fit_result m_cur_fit; // 最新拟合结果
  130. loc_point* m_begin; // 起始点
  131. loc_point* m_last; // 结束点
  132. //static opt_tool_main m_opt_tool;
  133. public:
  134. int find_last(int start);
  135. int find_first(int start);
  136. void save_k();
  137. void att_initiate();
  138. void remove_history();
  139. bool make_line();
  140. point select_solution0(std::vector<point> &vp,const double scale);
  141. bool select_solution(const std::vector<point> &vp,const site*sit,loc_point &p);
  142. loc_point select_solution_impl(const std::vector<point> p,const std::vector<loc_message>&lm);
  143. bool select_pdoa_solution_impl(const std::vector<point> p, const std::vector<loc_message>& lm, loc_point& lp);
  144. bool select_pdoa_solution(const std::vector<point> vp, const std::vector<loc_message>& lm, loc_point&lp, const float& pdoa);
  145. bool select_pdoa_solution2(const std::vector<point> vp, const std::vector<loc_message>& lm, loc_point&lp, const float& pdoa);
  146. bool select_pdoa_1d_solution(const std::vector<point> p, const std::vector<loc_message>& lm, loc_point& lp);
  147. bool select_pdoa_2d_solution(const std::vector<point> p, const std::vector<loc_message>& lm, loc_point& lp);
  148. bool select_pdoa_3d_solution(const std::vector<point> p, const std::vector<loc_message>& lm, loc_point& lp);
  149. fit_result* best_fit_raw(int num_point=0,int start=0,int end=-1);
  150. bool filter_by_acc(loc_point&c,std::array<solpoint,4>&v,double a);
  151. bool filter_by_fit(loc_point & c,const std::vector<point> & vp,const double scale);
  152. void select_one_ant(loc_point &c,const std::vector<point> & vp);
  153. int select_max_solution(const float& lhs, const float& rhs, std::vector<point> vp, loc_point& lp);
  154. int select_min_solution(const float& lhs, const float& rhs, std::vector<point> vp, loc_point& lp);
  155. float get_pdoa(const double poa[], const double& offset);
  156. double get_speed(const loc_point& p, const point& lp, const double& offset);
  157. double get_distance(const int& rid, const int& lrid);
  158. private:
  159. int get_algo_index(const int& idx);
  160. public:
  161. virtual void select_solution1(loc_point &c,const std::vector<point> &vp,const double scale)=0;
  162. virtual fit_result * get_best_fit()=0;
  163. virtual double getA(const fit_result * fit,const double scale,const double dt)=0;
  164. virtual void reset_fit(double d)=0;
  165. virtual bool revise_by_history(point & pt, const site*sit, int64_t m_time)=0;
  166. virtual ~select_point_object(){}
  167. };
  168. /*
  169. * 人员定位坐标过滤类
  170. *
  171. * */
  172. struct person_point_filter:select_point_object
  173. {
  174. person_point_filter(select_tool * owner)
  175. :select_point_object(owner)
  176. ,m_filter_man_count(0)
  177. {}
  178. int m_filter_man_count=0; // 过滤计数
  179. // 重置拟合
  180. virtual void reset_fit(double d)
  181. {
  182. if(d>1)
  183. m_filter_man_count++;
  184. else
  185. m_filter_man_count = 0;
  186. if(m_filter_man_count==3)
  187. {
  188. m_fitk.reset_data();
  189. m_fita.reset_data();
  190. m_cur_fit.reset();
  191. m_begin = m_last = nullptr;
  192. m_filter_man_count = 0;
  193. }
  194. }
  195. virtual void select_solution1(loc_point &c,const std::vector<point> &vp,const double scale)
  196. {
  197. if(filter_by_fit(c,vp,scale))
  198. {
  199. c.inc_cl(40);
  200. }
  201. else if(c.cl()>0 && vp.size()==2)
  202. {
  203. c[0]=vp[0];
  204. c[1]=vp[1];
  205. c.inc_cl(10);
  206. }
  207. else
  208. {
  209. select_one_ant(c,vp);
  210. }
  211. loc_point &refc=m_d(1);
  212. if(c.m_type==4 && c.cl()<=20 && refc.is_same_site(c) && refc.time_off(c)<5 ){
  213. logn_info(3,"{test}first point:site=%d,card=%d,(%f,%f) else:(%f,%f),cred:%d,size:%d",c.m_sid,c.m_cid,c[0].x,c[0].y,c[1].x,c[1].y,c.cl(),vp.size());
  214. if(const auto & site_ptr=sit_list::instance()->get(c.m_sid)){
  215. double d1=0,d2=0,d3=0;
  216. d1=refc.m_dist1;
  217. d2=site_ptr->dist_direct(c[0]);
  218. d3=site_ptr->dist_direct(c[1]);
  219. if(d1*d2<0 && d1*d3>0){
  220. logn_info(3,"{test}first point:site=%d,card=%d,(%f,%f) else:(%f,%f),cred:%d,size:%d",c.m_sid,c.m_cid,c[0].x,c[0].y,c[1].x,c[1].y,c.cl(),vp.size());
  221. point t=c[0];
  222. c[0]=c[1];
  223. c[1]=t;
  224. c.set_cl(0);
  225. }
  226. else if(d1*d2<0 && d1*d3<0)
  227. c[0].set(refc);
  228. }
  229. }
  230. }
  231. virtual fit_result * get_best_fit()
  232. {
  233. return best_fit_raw(4,4);
  234. }
  235. virtual double getA(const fit_result * fit,const double scale,const double dt)
  236. {
  237. double a=2.5;
  238. if(fabs(fit->k)<2/(3.6*scale) && fabs(fit->k)>0.5/(3.6*scale) && dt<10)
  239. {
  240. a=0.3;
  241. }
  242. return a;
  243. }
  244. virtual bool revise_by_history(point & pt, const site*sit, int64_t timestamp)
  245. {
  246. if(!card_path::inst().is_at_path(pt)) //if point not on the path
  247. return false;
  248. point dstp = sit->get_dstp(pt);
  249. if(dstp.empty())
  250. {
  251. log_warn("person.dstp empty,");
  252. }
  253. push_data_point dp;
  254. dp.dstp.set(dstp);
  255. dp.ct = m_d(0).m_ct;
  256. dp.valid = true;
  257. dp.stop_cnt = 5;
  258. dp.lp = m_d(0);
  259. dp.set(pt);
  260. { //don't delete the braces
  261. std::lock_guard<std::mutex> lock(m_owner->m_mtx);
  262. m_owner->m_push_list.clear();
  263. m_owner->m_push_list.push(dp);
  264. //dp.lp.debug_out("person");
  265. }
  266. return true;
  267. }
  268. };
  269. struct car_point_filter:select_point_object
  270. {
  271. car_point_filter(select_tool * owner)
  272. :select_point_object(owner)
  273. ,m_last_fit_valid(false)
  274. ,m_last_fit_k(0)
  275. ,m_last_fit_kb(0)
  276. ,m_last_fit_ka(0)
  277. ,m_last_fit_xo(0)
  278. ,m_last_fit_yo(0)
  279. ,m_last_fit_md_x(0)
  280. ,m_last_fit_md_y(0)
  281. ,m_last_fit_time_sec(0)
  282. ,m_last_fit_nearest_time_sec(0)
  283. ,m_last_time_sec(0)
  284. ,m_if_turning(false)
  285. {}
  286. bool m_last_fit_valid = false;
  287. double m_last_fit_k ;
  288. double m_last_fit_kb;
  289. double m_last_fit_ka;
  290. double m_last_fit_xo ;
  291. double m_last_fit_yo;
  292. double m_last_fit_md_x;
  293. double m_last_fit_md_y;
  294. double m_last_fit_time_sec; //x(0) of latest valid fit
  295. double m_last_fit_nearest_time_sec;
  296. double m_last_time_sec=0;
  297. bool m_if_turning=false;
  298. point m_turning_pt;
  299. point m_turning_ept;
  300. double m_simple_rec_kx,m_simple_rec_ky;
  301. const double m_fit_differ=4;
  302. const double m_pos_differ=8;
  303. virtual void reset_fit(double d){}
  304. virtual void select_solution1(loc_point &c,const std::vector<point> &vp,const double scale)
  305. {
  306. //two ants.
  307. if(c.cl()>0 && vp.size()==2)//m_card->smoothFlag() )
  308. {
  309. c[0]=vp[0];
  310. c[1]=vp[1];
  311. c.inc_cl(50);
  312. }
  313. else if(filter_by_fit(c,vp,scale))
  314. {
  315. c.inc_cl(40);
  316. }
  317. else
  318. {
  319. select_one_ant(c,vp);
  320. }
  321. }
  322. const fit_result* best_fit()const
  323. {
  324. if(m_cur_fit.k==0 && m_cur_fit.ke==0)
  325. return nullptr;
  326. return &m_cur_fit;
  327. }
  328. virtual fit_result * get_best_fit()
  329. {
  330. return best_fit_raw(5);
  331. }
  332. virtual double getA(const fit_result * fit,const double scale,const double dt)
  333. {
  334. double a=2.5;
  335. if(fabs(fit->k)<10/(3.6*scale) && fabs(fit->k)>1/(3.6*scale) && dt<20)
  336. {
  337. a=1;
  338. }
  339. if(fabs(fit->k)<=1/(3.6*scale) && dt<20)
  340. {
  341. a=-1;
  342. }
  343. return a;
  344. }
  345. void reset_turning()
  346. {
  347. m_if_turning=false;
  348. m_turning_pt.set(0,0);
  349. m_turning_ept.set(0,0);
  350. }
  351. void generate_list(point &pt, const site*sit, bool is_whole_list)
  352. {
  353. if(is_whole_list)
  354. {
  355. put_loc_point(pt, sit, m_d(0).m_ct, m_d(0));
  356. }
  357. else
  358. {
  359. put_single_loc_point(pt, sit, m_d(0).m_ct, m_d(0));
  360. }
  361. }
  362. void turning_mapping(point &rpt,const site*sit)
  363. {
  364. if(!m_if_turning)return;
  365. point sit_location; //projection
  366. sit_location = sit->get_dstp(m_turning_pt);
  367. if(sit_location.empty())
  368. {
  369. log_error("%d get_dstp point error.....",sit->m_id);
  370. }
  371. double dist1=sit_location.dist(rpt);
  372. double dist2=sit_location.dist(m_turning_pt);
  373. double dist3=m_turning_pt.dist(rpt); // dist1 is supposed to be = dist2+dist3
  374. if(dist1<=dist2 || dist1<=dist3)
  375. {
  376. if(dist2-dist1>3||dist1<=dist3)
  377. {
  378. //printf("reset turning\n");
  379. reset_turning(); // may encounter problems
  380. }
  381. return;
  382. }
  383. if(dist3>10)dist3=10; // turning distance no more than 10
  384. double turning_x,turning_y;
  385. double dist4=m_turning_pt.dist(m_turning_ept);
  386. turning_x=m_turning_pt.x + dist3 * (m_turning_ept.x - m_turning_pt.x) / dist4;
  387. turning_y=m_turning_pt.y + dist3 * (m_turning_ept.y - m_turning_pt.y) / dist4;
  388. rpt.set(turning_x,turning_y);
  389. }
  390. void put_single_loc_point(point &pt, const site*sit, int ct, loc_point &lp)
  391. {
  392. point rpt;
  393. rpt.set(pt);
  394. turning_mapping(rpt,sit);
  395. if(!card_path::inst().is_at_path(rpt)) //if point not on the path
  396. {
  397. lp.debug_out();
  398. return;
  399. }
  400. point dstp; //projection
  401. if(!m_if_turning)
  402. {
  403. //dstp.set(sit->x,sit->y);
  404. dstp = sit->get_dstp(pt);
  405. if(dstp.empty())
  406. {
  407. log_error("error.here....here.");
  408. }
  409. }
  410. else
  411. {
  412. dstp.set(m_turning_pt);
  413. }
  414. push_data_point dp;
  415. dp.dstp.set(dstp);
  416. dp.ct=ct;
  417. dp.valid=true;
  418. dp.stop_cnt=5;
  419. dp.lp=lp;
  420. dp.set(rpt);
  421. { //don't delete the braces
  422. std::lock_guard<std::mutex> lock(m_owner->m_mtx);
  423. m_owner->m_push_list.clear();
  424. m_owner->m_push_list.push(dp);
  425. //dp.lp.debug_out("single point .");
  426. }
  427. }
  428. /* 根据历史数据估计定位点
  429. *
  430. * param
  431. * const site* sit 分站
  432. * double m_time_sec 秒数
  433. * return
  434. *
  435. *
  436. */
  437. double estimate_point_by_history(const site*sit, double m_time_sec)
  438. {
  439. double estimate_dist = m_last_fit_k * (m_time_sec-m_last_fit_xo) + m_last_fit_kb + m_last_fit_yo;
  440. point pt(m_last_fit_md_x + estimate_dist * m_simple_rec_kx, m_last_fit_md_y + estimate_dist * m_simple_rec_ky);
  441. int fidx = find_first(0);
  442. if(fidx >= 0)
  443. {
  444. loc_point& f = m_d[fidx];
  445. estimate_dist = f.loc_dist(pt);
  446. }
  447. else
  448. estimate_dist = 0;
  449. return estimate_dist;
  450. }
  451. /*
  452. * 转换距离为坐标点
  453. * */
  454. point convert_dist_to_pt(double dist, const site*sit)
  455. {
  456. int fidx = find_first(0);
  457. if(fidx < 0)
  458. return point(0, 0);
  459. loc_point& f = m_d[fidx];
  460. return point(f.m_sol[0].x + dist * m_simple_rec_kx, f.m_sol[0].y + dist * m_simple_rec_ky);
  461. }
  462. void put_loc_point(point &pt, const site*sit, int ct, loc_point &lp)
  463. {
  464. point rpt;
  465. rpt.set(pt);
  466. turning_mapping(rpt,sit);
  467. if(!card_path::inst().is_at_path(pt)) //if point not on the path
  468. {
  469. lp.debug_out();
  470. return;
  471. }
  472. point dstp; //projection
  473. if(!m_if_turning)
  474. {
  475. dstp = sit->get_dstp(pt);
  476. if(dstp.empty())
  477. log_error("dstp.empty()..");
  478. }
  479. else
  480. {
  481. dstp.set(m_turning_pt);
  482. }
  483. int size = 0;
  484. push_data_point dp[13];
  485. dp[size].dstp.set(dstp);
  486. dp[size].ct=ct;
  487. dp[size].valid=true;
  488. dp[size].stop_cnt=5;
  489. dp[size].lp=lp;
  490. dp[size].set(rpt);
  491. double missing_time = m_d(0).m_time/1000.;
  492. size++;
  493. for(;size<13;size++)
  494. {
  495. dp[size].dstp.set(dstp);
  496. dp[size].ct = ct;
  497. dp[size].valid=true;
  498. dp[size].stop_cnt=5;
  499. double mt = missing_time + size;
  500. double missing_dist = estimate_point_by_history(sit, mt);
  501. point missing_point = convert_dist_to_pt(missing_dist, sit);
  502. if(!card_path::inst().is_at_path(missing_point)) //if point not on the path
  503. {
  504. break;
  505. }
  506. turning_mapping(missing_point,sit); //turning
  507. dp[size].set(missing_point);
  508. dp[size].lp.set(missing_point);
  509. dp[size].lp.m_time=(int64_t)(missing_time * 1000);
  510. dp[size].lp.m_sid = sit->m_id;
  511. }
  512. {
  513. std::lock_guard<std::mutex> lock(m_owner->m_mtx);
  514. m_owner->m_push_list.clear();
  515. for(int i=0;i<size;i++)
  516. {
  517. m_owner->m_push_list.push(dp[i]);
  518. }
  519. }
  520. }
  521. /*
  522. * 转换点为距离
  523. * */
  524. double convert_pt_to_dist(point &pt, const site* sit)
  525. {
  526. double dist = 0;
  527. int fidx = find_first(0);
  528. if(fidx>=0)
  529. {
  530. loc_point&f=m_d[fidx];
  531. dist = f.loc_dist(pt);
  532. if(dist!=0)
  533. {
  534. m_simple_rec_kx = (pt.x - f.m_sol[0].x) / dist;
  535. m_simple_rec_ky = (pt.y - f.m_sol[0].y) / dist;
  536. }
  537. }
  538. if(fidx<0 || dist==0)
  539. {
  540. m_simple_rec_kx = 0;
  541. m_simple_rec_ky = 0;
  542. }
  543. return dist;
  544. }
  545. virtual bool revise_by_history(point & pt, const site*sit, int64_t timestamp)
  546. {
  547. bool flag =false;
  548. if(m_line.empty() || !m_line.contain(m_d(0),0.1))
  549. {
  550. m_last_fit_valid = false;
  551. m_last_fit_time_sec = 0;
  552. m_last_fit_k = 0;
  553. m_last_fit_kb = 0;
  554. m_last_fit_ka = 0;
  555. m_last_fit_xo = 0;
  556. m_last_fit_yo = 0;
  557. m_last_fit_nearest_time_sec = 0;
  558. m_last_time_sec = 0;
  559. reset_turning();
  560. generate_list(pt, sit, false);
  561. return true;
  562. }
  563. // convert pt to distance
  564. double dist = convert_pt_to_dist(pt, sit);
  565. double m_time_sec = timestamp / 1000.; //second
  566. if(m_time_sec - m_last_fit_time_sec > 60)m_last_fit_valid = false;
  567. // update acc
  568. //m_accumulate_acc = m_d(0).m_acc;
  569. // choose data by fit
  570. const fit_result*fit=best_fit();
  571. bool if_change_fit=false;
  572. if(fit!=nullptr && fit->ke<=1 && m_time_sec - m_fitk.x(0) <= 15 && fabs(fit->k) < m_pos_differ)
  573. {
  574. // put m_acccumulate_acc into consideration
  575. // fit->k - m_last_fit_k < m_accumulate_acc
  576. if(m_last_fit_valid == true && m_last_fit_k * fit->k > -0.6)
  577. {
  578. //if point is too near the sit: do not not judge the backwards
  579. double est1 = estimate_point_by_history(sit, m_last_fit_time_sec);
  580. double est2 = fit->k * (m_time_sec-fit->xo) + fit->kb + fit->yo;
  581. if(fabs(est1-est2)< (m_time_sec - m_last_fit_time_sec) * 5) // large jump is not allowed
  582. if_change_fit=true;
  583. }
  584. else if(m_last_fit_valid==false)
  585. if_change_fit=true;
  586. }
  587. if(if_change_fit)
  588. {
  589. m_last_fit_valid = true;
  590. m_last_fit_time_sec = m_fitk.x(0);
  591. m_last_fit_k = fit->k;
  592. m_last_fit_kb = fit->kb;
  593. m_last_fit_ka = fit->ka;
  594. m_last_fit_xo = fit->xo;
  595. m_last_fit_yo = fit->yo;
  596. m_last_fit_nearest_time_sec = m_fitk.x(0);
  597. int fidx=find_first(0);
  598. if(fidx<0)
  599. {
  600. m_last_fit_md_x = 0;
  601. m_last_fit_md_y = 0;
  602. }
  603. else{
  604. loc_point&f=m_d[fidx];
  605. m_last_fit_md_x = f.m_sol[0].x;
  606. m_last_fit_md_y = f.m_sol[0].y;
  607. }
  608. }
  609. // revise
  610. double estimate_dist = estimate_point_by_history(sit, m_time_sec);
  611. if(m_last_fit_valid && m_time_sec - m_last_fit_time_sec < 20)
  612. {
  613. if(fabs(m_last_fit_k) > 0.5 && fabs(estimate_dist-dist)>m_fit_differ)
  614. dist=estimate_dist;
  615. else if(fabs(m_last_fit_k) <= 0.5 && fabs(estimate_dist-dist)>m_fit_differ * 2)
  616. dist=estimate_dist;
  617. else flag = true;
  618. }
  619. else m_last_fit_nearest_time_sec = m_time_sec;
  620. m_last_time_sec = m_time_sec;
  621. // convert the estimated dist to pt
  622. point mpt = convert_dist_to_pt(dist, sit);
  623. // judging turning
  624. detect_turning(mpt, sit);
  625. // create the list
  626. //if(m_accumulate_acc<-10)generate(mpt, sit,false); generate single point
  627. if(m_last_fit_valid && timestamp/1000. - m_last_fit_time_sec < 20 && fabs(m_last_fit_k) > 0.5)
  628. generate_list(mpt, sit, true); //generate the whole list
  629. else
  630. generate_list(mpt, sit, false); //generate single point
  631. //turning map
  632. turning_mapping(mpt, sit);
  633. pt = mpt;
  634. return flag;
  635. }
  636. // 检测掉头或转弯
  637. void detect_turning(point &mpt, const site*sit)
  638. {
  639. if(m_if_turning)return;
  640. double detect_area = 4;
  641. double detect_angle = 15; //15
  642. double detect_para = 0.25; //0.25
  643. // check angle
  644. double angle=-m_d(0).m_rav; // right+ left-
  645. if(fabs(angle)>180)
  646. return; // invalid data
  647. if(fabs(angle)<detect_angle || !m_last_fit_valid || fabs(m_last_fit_k)<0.01)
  648. return;
  649. // find turning point
  650. std::vector<line_v> turning_list=card_path::inst().find_possible_path(mpt, detect_area);;
  651. //angle1
  652. int fidx = find_first(0);
  653. if(fidx<0)
  654. return;
  655. double dist = m_d[fidx].loc_dist(mpt);
  656. point pt1;
  657. pt1.set(m_d[fidx].m_sol[0]);
  658. double angle1;
  659. if(m_last_fit_k * dist>0)
  660. angle1 = calc_turning_angle(pt1, mpt);
  661. else
  662. angle1 = calc_turning_angle(mpt, pt1);
  663. if(angle1<0)
  664. return;
  665. //finding
  666. for(unsigned int i=0;i<turning_list.size();i++)
  667. {
  668. line_v &l=turning_list[i];
  669. // get map angle
  670. double angle2 = calc_turning_angle(l.v[0], l.v[1]);
  671. double delta = angle1-angle2;
  672. if(delta>180)
  673. delta=delta-360;
  674. if(delta<-180)
  675. delta=delta+360;
  676. if(fabs(delta)<5)continue;
  677. if(fabs(delta)>175)continue;
  678. // turning angle must be correct
  679. if(angle*delta>0 && fabs(angle)>fabs(delta)*detect_para)
  680. {
  681. log_info("turning:(%.5f,%.5f)(%.5f,%.5f)(%.5f,%.5f),a1:%f,a2:%f,delta:%f,angle:%f\n",
  682. pt1.x,pt1.y,l.v[0].x,l.v[0].y,l.v[1].x,l.v[1].y,angle1,angle2,delta,angle);
  683. m_if_turning=true;
  684. m_turning_pt.set(l.v[0]);
  685. m_turning_ept.set(l.v[1]);
  686. break;
  687. }
  688. }
  689. }
  690. // 计算掉头角度
  691. double calc_turning_angle(point &a, point &b)
  692. {
  693. if(fabs(a.x-b.x)<0.001)
  694. {
  695. if(fabs(a.y-b.y)<0.001)return -1;
  696. return b.y>a.y?90:270;
  697. }
  698. double angle=std::atan((b.y-a.y)/(b.x-a.x))*180/3.1415926;
  699. if(a.x>b.x)angle=angle+180;
  700. if(angle<0)angle=angle+360;
  701. return angle;
  702. }
  703. };
  704. //---------------------smooth
  705. struct smooth_tool
  706. {
  707. std::unique_ptr<select_tool>& m_st; // 选择工具类
  708. bool smooth_initial_setting; // 平滑初始化状态
  709. double smooth_speed; // 平滑速度
  710. double smooth_speed_presentation;
  711. int smooth_speed_presentation_cnt;
  712. point smooth_last_position; // 上一个平滑位置
  713. double smooth_last_time_sec; // 上一个平滑时间
  714. point smooth_last_true_position; // 上一个真实位置
  715. line smooth_line; // 平滑线
  716. bool smooth_line_reset=false; //if line reset
  717. bool smooth_halt_condition; //if halting
  718. int smooth_halt_count; //halting count
  719. point smooth_halt_position; //position while begin halting
  720. point smooth_halt_position_plus;
  721. point smooth_halt_position_minus;
  722. smooth_tool()=default;
  723. smooth_tool(std::unique_ptr<select_tool>&st)
  724. :m_st(st)
  725. ,smooth_initial_setting(false)
  726. ,smooth_speed(0)
  727. ,smooth_speed_presentation(0)
  728. ,smooth_speed_presentation_cnt(0)
  729. ,smooth_last_time_sec(0)
  730. ,smooth_halt_condition(0)
  731. ,smooth_halt_count(0)
  732. {}
  733. void smooth_set_loc_point(double t, int ct, const site*sit, loc_point *lp)
  734. {
  735. point pt;
  736. if(smooth_halt_condition)
  737. pt.set(smooth_halt_position);
  738. else
  739. pt.set(smooth_last_position);
  740. lp->m_dist2 = sit->dist_direct(pt);
  741. lp->m_smooth_x = pt.x;
  742. lp->m_smooth_y = pt.y;
  743. lp->m_time = (int64_t)(t*1000);
  744. lp->m_ct = ct;
  745. if(smooth_halt_condition)
  746. {
  747. lp->m_speed=0;
  748. lp->m_stat=0;
  749. }
  750. else
  751. {
  752. lp->m_speed = smooth_speed_presentation * (3.6*sit->m_scale) ; //(m/s) to (km/h)
  753. if(smooth_speed < 0)
  754. lp->m_speed = -lp->m_speed;
  755. if(std::isnan(lp->m_speed))
  756. lp->m_speed=0;
  757. lp->m_stat=1;
  758. }
  759. }
  760. void smooth_reset()
  761. {
  762. smooth_initial_setting = false;
  763. smooth_speed = 0; //smoothed speed
  764. smooth_speed_presentation_cnt = 0;
  765. smooth_last_position = point(0,0); //last position of smoothed point
  766. smooth_last_true_position = point(0,0); //last position of true point
  767. smooth_last_time_sec = 0; //last time second
  768. smooth_halt_condition = false;
  769. smooth_halt_count = 0;
  770. }
  771. bool smooth_initiate(point &pt, double t, const site*sit)
  772. {
  773. smooth_initial_setting=true;
  774. smooth_speed=0;
  775. smooth_speed_presentation_cnt=0;
  776. smooth_last_position = pt;
  777. smooth_last_true_position = pt;
  778. smooth_last_time_sec = t;
  779. smooth_halt_condition=false;
  780. smooth_halt_count=0;
  781. smooth_halt_position=pt;
  782. smooth_halt_position_plus=pt;
  783. smooth_halt_position_minus=pt;
  784. return true;
  785. }
  786. virtual void set(point &pt,loc_point &lp)=0;
  787. loc_point smooth_strategy()
  788. {
  789. loc_point lp;
  790. push_data_point dpt = m_st->getDpt();
  791. if(dpt.empty())
  792. return lp;
  793. point pt;
  794. pt.set(dpt);
  795. double current_t = time(NULL);
  796. const auto & sit = sit_list::instance()->get(dpt.lp.m_sid);
  797. if(dpt.valid)
  798. {
  799. smooth_dist(pt, current_t, dpt.ct, sit.get(), dpt.dstp, &lp);
  800. set(pt,lp);
  801. dpt.lp.m_dist2 = lp.m_dist2;
  802. dpt.lp.m_time = lp.m_time;
  803. dpt.lp.m_ct = lp.m_ct;
  804. dpt.lp.set(lp);
  805. dpt.lp.m_dist = sit->dist_direct(dpt);
  806. dpt.lp.m_speed = lp.m_speed;
  807. dpt.lp.m_stat = lp.m_stat;
  808. dpt.lp.debug_out("get_point");
  809. }
  810. else
  811. {
  812. smooth_set_loc_point(current_t, dpt.ct, sit.get(), &lp);
  813. if(dpt.stop_cnt<=0)
  814. {
  815. lp.m_speed = 0;
  816. lp.m_stat = 0;
  817. }
  818. set(pt, lp);
  819. }
  820. return lp;
  821. }
  822. virtual void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr)=0;
  823. virtual ~smooth_tool(){}
  824. };
  825. struct smooth_tool_person_1:smooth_tool
  826. {
  827. smooth_tool_person_1(std::unique_ptr<select_tool>&m)
  828. :smooth_tool(m)
  829. {}
  830. virtual void set(point &pt,loc_point &lp)
  831. {
  832. lp.set(pt);
  833. }
  834. void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr)
  835. {
  836. if(smooth_line.empty() || !smooth_line.contain(pt,0.1))
  837. {
  838. if(!smooth_line_reset)
  839. {
  840. smooth_reset();
  841. smooth_line_reset=true;
  842. }
  843. else
  844. {
  845. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  846. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  847. path.clear();
  848. if(path.empty())
  849. {
  850. smooth_line.set(smooth_last_true_position, pt);
  851. smooth_line_reset=false;
  852. }
  853. else
  854. {
  855. smooth_reset();
  856. }
  857. }
  858. }
  859. else
  860. smooth_line_reset=false;
  861. if(!smooth_initial_setting)
  862. {
  863. smooth_initiate(pt, t, sit);
  864. }
  865. else
  866. {
  867. double current_dist = dstp.dist_direct(pt);
  868. double last_true_position = dstp.dist_direct(smooth_last_true_position);
  869. double max_span = 100;
  870. if(fabs(current_dist-last_true_position)<max_span && t - smooth_last_time_sec < 10)
  871. {
  872. double new_speed = (current_dist-last_true_position) / (t - smooth_last_time_sec);
  873. double speed_differ = fabs(new_speed-smooth_speed);
  874. if(speed_differ>1)new_speed=smooth_speed +1*(new_speed>smooth_speed?1:-1);
  875. smooth_speed = smooth_speed * 0.4 + new_speed * 0.6;
  876. smooth_last_true_position = pt;
  877. smooth_last_position = pt;
  878. smooth_last_time_sec = t;
  879. if(fabs(smooth_speed_presentation)<1e-6 || std::isnan(smooth_speed_presentation))
  880. {
  881. smooth_speed_presentation=fabs(smooth_speed);
  882. }
  883. else
  884. smooth_speed_presentation = smooth_speed_presentation * 0.4 + fabs(smooth_speed) * 0.6;
  885. if(fabs(smooth_speed)<0.1)smooth_speed_presentation=0;
  886. }
  887. else
  888. {
  889. smooth_reset();
  890. smooth_initiate(pt, t, sit);
  891. }
  892. }
  893. if(m_lp != nullptr)//m_time,m_ct,x,y,m_speed,m_stat
  894. {
  895. smooth_set_loc_point(t, ct, sit, m_lp);
  896. }
  897. }
  898. };
  899. struct smooth_tool_car_1:smooth_tool
  900. {
  901. smooth_tool_car_1(std::unique_ptr<select_tool>&m)
  902. :smooth_tool(m)
  903. {}
  904. virtual void set(point &pt,loc_point &lp)
  905. {
  906. lp.set(lp.m_smooth_x,lp.m_smooth_y);
  907. }
  908. void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr)
  909. {
  910. point init_pt(pt.x, pt.y);
  911. if(smooth_line.empty() || !smooth_line.contain(pt,0.1) || smooth_halt_count>6)
  912. {
  913. if(!smooth_line_reset)
  914. {
  915. if(!smooth_line.empty() && !smooth_line.contain(pt,0.1) && !smooth_last_true_position.empty())
  916. {
  917. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  918. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  919. path.clear();
  920. log_info("generating critical point in smooth(car):(%.2f,%.2f)->(%.2f,%.2f)\n",
  921. smooth_last_true_position.x, smooth_last_true_position.y, pt.x, pt.y);
  922. if(!path.empty())
  923. {
  924. point critical_point=path[0];
  925. log_info("critical point generated in smooth(car):pt=(%.2f,%.2f),(%.2f,%.2f)->(%.2f,%.2f)\n",
  926. critical_point.x, critical_point.y, smooth_last_true_position.x, smooth_last_true_position.y,
  927. pt.x, pt.y);
  928. init_pt.set(critical_point);
  929. }
  930. }
  931. smooth_reset();
  932. smooth_line_reset=true;
  933. }
  934. else
  935. {
  936. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  937. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  938. path.clear();
  939. if(path.empty())
  940. {
  941. smooth_line.set(smooth_last_true_position, pt);
  942. smooth_line_reset=false;
  943. }
  944. else
  945. {
  946. smooth_reset();
  947. }
  948. }
  949. }
  950. else
  951. smooth_line_reset=false;
  952. if(!smooth_initial_setting)
  953. {
  954. smooth_initiate(init_pt, t, sit);
  955. }
  956. else
  957. {
  958. double current_dist = dstp.dist_direct(pt);
  959. double last_position = dstp.dist_direct(smooth_last_position);
  960. double last_true_position = dstp.dist_direct(smooth_last_true_position);
  961. double rec_kx=0;
  962. double rec_ky=0;
  963. if(current_dist!=0)
  964. {
  965. rec_kx = (pt.x - dstp.x)/current_dist;
  966. rec_ky = (pt.y - dstp.y)/current_dist;
  967. }
  968. double next_dist = last_position + smooth_speed * (t-smooth_last_time_sec);
  969. double max_span = 200;
  970. if(fabs(next_dist-current_dist)<max_span && t - smooth_last_time_sec < 10)
  971. {
  972. double new_speed = (current_dist-last_true_position) / (t - smooth_last_time_sec);
  973. // judge halting
  974. if(fabs(new_speed)<0.1)smooth_halt_count++;
  975. else{
  976. smooth_halt_count=0;
  977. }
  978. if(!smooth_halt_condition && smooth_halt_count>=3 && fabs(smooth_speed) < 0.2)
  979. {
  980. smooth_halt_condition=true;
  981. smooth_halt_position=smooth_last_position;
  982. smooth_halt_position_plus=pt;
  983. smooth_halt_position_minus=pt;
  984. }
  985. // handle speed
  986. if(smooth_halt_condition)
  987. {
  988. double halt_position = dstp.dist_direct(smooth_halt_position);
  989. double halt_position_plus = dstp.dist_direct(smooth_halt_position_plus);
  990. double halt_position_minus = dstp.dist_direct(smooth_halt_position_minus);
  991. if(halt_position_plus<current_dist)halt_position_plus=current_dist;
  992. if(halt_position_minus>current_dist)halt_position_minus=current_dist;
  993. smooth_halt_position_plus = point(dstp.x + halt_position_plus * rec_kx, dstp.y + halt_position_plus * rec_ky);
  994. smooth_halt_position_minus = point(dstp.x + halt_position_minus * rec_kx, dstp.y + halt_position_minus * rec_ky);
  995. if(fabs(halt_position_plus - halt_position_minus)>1)
  996. {
  997. smooth_halt_condition=false;
  998. last_position = halt_position;
  999. smooth_speed = 0;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. if(fabs(smooth_speed)<1e-6 || std::isnan(smooth_speed))
  1005. {
  1006. smooth_speed=new_speed;
  1007. if(smooth_speed>2.5)smooth_speed=2.5;
  1008. if(smooth_speed<-2.5)smooth_speed=-2.5;
  1009. }
  1010. else
  1011. {
  1012. double speed_differ = fabs(new_speed-smooth_speed);
  1013. if(speed_differ>1)
  1014. new_speed=smooth_speed +1*(new_speed>smooth_speed?1:-1);
  1015. smooth_speed = smooth_speed * 0.4 + new_speed * 0.6;
  1016. }
  1017. if(fabs(smooth_speed_presentation)<1e-6 || std::isnan(smooth_speed_presentation))
  1018. {
  1019. smooth_speed_presentation=fabs(smooth_speed);
  1020. }
  1021. else
  1022. smooth_speed_presentation = smooth_speed_presentation * 0.4 + fabs(smooth_speed) * 0.6;
  1023. // must obey speed direction
  1024. if(smooth_speed * (current_dist-last_position) > 0)
  1025. {
  1026. last_position = last_position+smooth_speed*(t-smooth_last_time_sec);
  1027. if(smooth_speed * (current_dist-last_position) < 0)
  1028. {
  1029. last_position = current_dist;
  1030. }
  1031. smooth_speed_presentation_cnt=0;
  1032. }
  1033. else
  1034. {
  1035. if(smooth_speed_presentation_cnt<3)
  1036. smooth_speed_presentation_cnt++;
  1037. else
  1038. smooth_speed_presentation=0;
  1039. }
  1040. if(fabs(smooth_speed)<0.1)smooth_speed_presentation=0;
  1041. double revise_para = 0.2;
  1042. if(fabs(smooth_speed) < 0.01 || smooth_speed * (current_dist-last_position) < 0)
  1043. revise_para=0;
  1044. last_position=last_position+(current_dist-last_position)*revise_para;
  1045. }
  1046. smooth_last_position = point(dstp.x + last_position * rec_kx, dstp.y + last_position * rec_ky);
  1047. smooth_last_true_position = pt;
  1048. smooth_last_time_sec = t;
  1049. }
  1050. else
  1051. {
  1052. smooth_reset();
  1053. smooth_initiate(pt, t, sit);
  1054. }
  1055. }
  1056. if(m_lp != nullptr)//m_time,m_ct,x,y,m_speed,m_stat
  1057. {
  1058. smooth_set_loc_point(t, ct, sit, m_lp);
  1059. }
  1060. }
  1061. };
  1062. struct smooth_tool_drivingface_car_1:smooth_tool
  1063. {
  1064. smooth_tool_drivingface_car_1(std::unique_ptr<select_tool>&m)
  1065. :smooth_tool(m)
  1066. {}
  1067. virtual void set(point&p,loc_point&lp){}
  1068. virtual void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr){}
  1069. };
  1070. //---------------------------------
  1071. DEF_EXCEPTION (no_tool);
  1072. struct select_tool_manage
  1073. {
  1074. void create_tool(const std::string &s,std::unique_ptr <select_tool> &set,std::unique_ptr <smooth_tool> &smt)
  1075. {
  1076. if(!s.compare(std::string{"PS_1"}))
  1077. {
  1078. set.reset(new select_tool_person_1());
  1079. smt.reset(new smooth_tool_person_1(set));
  1080. }
  1081. else if(!s.compare(std::string{"PS_2"}))
  1082. {
  1083. set.reset(new select_tool_person_2());
  1084. smt.reset(new smooth_tool_person_1(set));
  1085. }
  1086. else if(!s.compare(std::string{"CS_1"}))
  1087. {
  1088. set.reset(new select_tool_car_1());
  1089. smt.reset(new smooth_tool_car_1(set));
  1090. }
  1091. else if(!s.compare(std::string{"WS_1"}))
  1092. {
  1093. set.reset(new select_tool_person_1());
  1094. smt.reset(new smooth_tool_person_1(set));
  1095. }
  1096. else
  1097. {
  1098. THROW_EXCEPTION(no_tool,"未定义%s的策略,需要在config.ini中定义",s.c_str());
  1099. }
  1100. }
  1101. static select_tool_manage * instance();
  1102. };
  1103. #endif