1
0

select_tool.h 31 KB

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