select_tool.h 30 KB

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