select_tool.h 29 KB

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