select_tool.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 m_time)
  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 m_time)
  425. {
  426. std_info("revise_____-before:%f,%f",pt.x,pt.y);
  427. bool flag =false;
  428. if(m_line.empty() || !m_line.contain(m_d(0),0.1))
  429. {
  430. m_last_fit_valid = false;
  431. m_last_fit_time_sec = 0;
  432. m_last_fit_k = 0;
  433. m_last_fit_kb = 0;
  434. m_last_fit_ka = 0;
  435. m_last_fit_xo = 0;
  436. m_last_fit_yo = 0;
  437. m_last_fit_nearest_time_sec = 0;
  438. m_last_time_sec = 0;
  439. reset_turning();
  440. generate_list(pt, sit, false);
  441. return true;
  442. }
  443. // convert pt to distance
  444. double dist = convert_pt_to_dist(pt, sit);
  445. double m_time_sec = m_time / 1000.; //second
  446. //if(m_time_sec - m_last_fit_nearest_time_sec > 30)m_last_fit_valid = false;
  447. if(m_time_sec - m_last_fit_time_sec > 60)m_last_fit_valid = false;
  448. // update acc
  449. //m_accumulate_acc = m_d(0).m_acc;
  450. // choose data by fit
  451. const fit_result*fit=best_fit();
  452. bool if_change_fit=false;
  453. if(fit!=nullptr && fit->ke<=1 && m_time_sec - m_fitk.x(0) <= 15 && fabs(fit->k) < m_pos_differ)
  454. { //printf("change fit time:%f,%f,%f\n",m_time_sec, fit->d.x(0), m_time_sec - fit->d.x(0));
  455. // put m_acccumulate_acc into consideration
  456. // fit->k - m_last_fit_k < m_accumulate_acc
  457. if(m_last_fit_valid == true && m_last_fit_k * fit->k > -0.6)
  458. //if((sit->dist(pt)<20 ||m_last_fit_k * fit->k > -0.6))
  459. { //if point is too near the sit: do not not judge the backwards
  460. double est1 = estimate_point_by_history(sit, m_last_fit_time_sec);
  461. double est2 = fit->k * (m_time_sec-fit->xo) + fit->kb + fit->yo;
  462. //printf("change fit:1(%f,%f),2(%f,%f),differ:(%f,%f)\n",
  463. // m_last_fit_nearest_time_sec,est1,m_time_sec,est2,m_time_sec-m_last_fit_nearest_time_sec,est2-est1);
  464. //if(fabs(est1-est2)>40)printf("change fit:%f,%f,%f\n",fabs(est1-est2),est1,est2);
  465. if(fabs(est1-est2)< (m_time_sec - m_last_fit_time_sec) * 5) // large jump is not allowed
  466. if_change_fit=true;
  467. }
  468. else if(m_last_fit_valid==false)
  469. if_change_fit=true;
  470. }
  471. if(if_change_fit)
  472. {
  473. m_last_fit_valid = true;
  474. m_last_fit_time_sec = m_fitk.x(0);
  475. m_last_fit_k = fit->k;
  476. m_last_fit_kb = fit->kb;
  477. m_last_fit_ka = fit->ka;
  478. m_last_fit_xo = fit->xo;
  479. m_last_fit_yo = fit->yo;
  480. m_last_fit_nearest_time_sec = m_fitk.x(0);
  481. int fidx=find_first(0);
  482. if(fidx<0)
  483. {
  484. m_last_fit_md_x = 0;
  485. m_last_fit_md_y = 0;
  486. }
  487. else{
  488. loc_point&f=m_d[fidx];
  489. m_last_fit_md_x = f.m_sol[0].x;
  490. m_last_fit_md_y = f.m_sol[0].y;
  491. }
  492. // update acc
  493. //m_accumulate_acc=0
  494. printf("change line, k=%f,ke=%f\n",fit->k,fit->ke);
  495. }
  496. // revise
  497. double estimate_dist = estimate_point_by_history(sit, m_time_sec);
  498. //printf("revise:est:%f, d:%f, fitvalid:%d, timesecdiffer:%f\n",
  499. //estimate_dist, dist, m_last_fit_valid, m_time_sec - m_last_fit_time_sec);
  500. if(m_last_fit_valid && m_time_sec - m_last_fit_time_sec < 20)
  501. {
  502. if(fabs(m_last_fit_k) > 0.5 && fabs(estimate_dist-dist)>m_fit_differ)
  503. dist=estimate_dist;
  504. else if(fabs(m_last_fit_k) <= 0.5 && fabs(estimate_dist-dist)>m_fit_differ * 2)
  505. dist=estimate_dist;
  506. else flag = true;
  507. //m_last_fit_nearest_time_sec = m_time_sec; //need more tests to uncomment this sentence
  508. }
  509. else m_last_fit_nearest_time_sec = m_time_sec;
  510. m_last_time_sec = m_time_sec;
  511. // convert the estimated dist to pt
  512. point mpt = convert_dist_to_pt(dist, sit);
  513. // judging turning
  514. detect_turning(mpt, sit);
  515. // create the list
  516. //if(m_accumulate_acc<-10)generate(mpt, sit,false); generate single point
  517. if(m_last_fit_valid && m_time/1000. - m_last_fit_time_sec < 20 && fabs(m_last_fit_k) > 0.5)
  518. generate_list(mpt, sit, true); //generate the whole list
  519. else
  520. generate_list(mpt, sit, false); //generate single point
  521. //turning map
  522. turning_mapping(mpt, sit);
  523. pt = mpt;
  524. std_info("revise_____-end:%f,%f,useless:%s",pt.x,pt.y,flag?"true":"false");
  525. return flag;
  526. }
  527. void detect_turning(point &mpt, const site*sit)
  528. {
  529. if(m_if_turning)return;
  530. //IMPORTANT: only car-1121 and car-1136 have accurate rav values currently. May delete this sentence in the future.
  531. //if(m_id!=1121 && m_id!=1136)return;
  532. double detect_area = 4;
  533. double detect_angle = 15; //15
  534. double detect_para = 0.25; //0.25
  535. // check angle
  536. double angle=-m_d(0).m_rav; // right+ left-
  537. if(fabs(angle)>180)return; // invalid data
  538. if(fabs(angle)<detect_angle || !m_last_fit_valid || fabs(m_last_fit_k)<0.01)return;
  539. // find turning point
  540. std::vector<line_v> turning_list=card_path::inst().find_possible_path(mpt, detect_area);;
  541. //angle1
  542. int fidx=find_first(0);
  543. if(fidx<0)return;
  544. double dist=m_d[fidx].loc_dist(mpt);
  545. point pt1;
  546. pt1.set(m_d[fidx].m_sol[0]);
  547. double angle1;
  548. if(m_last_fit_k * dist>0)
  549. angle1=calc_turning_angle(pt1, mpt);
  550. else
  551. angle1=calc_turning_angle(mpt, pt1);
  552. if(angle1<0)return;
  553. //finding
  554. for(unsigned int i=0;i<turning_list.size();i++)
  555. {
  556. line_v &l=turning_list[i];
  557. // get map angle
  558. double angle2=calc_turning_angle(l.v[0], l.v[1]);
  559. double delta=angle1-angle2;
  560. if(delta>180)delta=delta-360;
  561. if(delta<-180)delta=delta+360;
  562. if(fabs(delta)<5)continue;
  563. if(fabs(delta)>175)continue;
  564. //printf("angle:%f, delta:%f. mul:%f\n",angle, delta, delta*detect_para);
  565. // turning angle must be correct
  566. if(angle*delta>0 && fabs(angle)>fabs(delta)*detect_para)
  567. {
  568. printf("turning:(%.5f,%.5f)(%.5f,%.5f)(%.5f,%.5f),a1:%f,a2:%f,delta:%f,angle:%f\n",
  569. pt1.x,pt1.y,l.v[0].x,l.v[0].y,l.v[1].x,l.v[1].y,angle1,angle2,delta,angle);
  570. m_if_turning=true;
  571. m_turning_pt.set(l.v[0]);
  572. m_turning_ept.set(l.v[1]);
  573. break;
  574. }
  575. }
  576. }
  577. double calc_turning_angle(point &a, point &b)
  578. {
  579. if(fabs(a.x-b.x)<0.001)
  580. {
  581. if(fabs(a.y-b.y)<0.001)return -1;
  582. return b.y>a.y?90:270;
  583. }
  584. double angle=std::atan((b.y-a.y)/(b.x-a.x))*180/3.1415926;
  585. if(a.x>b.x)angle=angle+180;
  586. if(angle<0)angle=angle+360;
  587. return angle;
  588. }
  589. };
  590. struct select_tool
  591. {
  592. select_point_object *m_spo=nullptr;
  593. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm)=0;
  594. virtual ~select_tool()
  595. {
  596. if(m_spo !=nullptr)
  597. delete m_spo;
  598. }
  599. };
  600. //--------------person------solution one--------
  601. struct select_tool_person_1:select_tool
  602. {
  603. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm);
  604. ~select_tool_person_1()
  605. {
  606. }
  607. };
  608. struct select_tool_person_2:select_tool
  609. {
  610. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm)
  611. {
  612. loc_point lp;
  613. return lp;
  614. }
  615. };
  616. //----------------------car----------
  617. struct select_tool_car_1:select_tool
  618. {
  619. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm);
  620. };
  621. //---------------------drivingfaceCar
  622. struct select_tool_drivingface_car_1:select_tool
  623. {
  624. virtual loc_point select_solution(const std::vector<point> p,const std::vector<loc_message>&lm)
  625. {
  626. loc_point lp;
  627. return lp;
  628. }
  629. };
  630. //---------------------smooth
  631. struct smooth_tool
  632. {
  633. std::shared_ptr<select_tool> m_st=nullptr;
  634. bool smooth_initial_setting;
  635. double smooth_speed;
  636. double smooth_speed_presentation;
  637. int smooth_speed_presentation_cnt;
  638. point smooth_last_position;
  639. double smooth_last_time_sec;
  640. point smooth_last_true_position;
  641. line smooth_line;
  642. bool smooth_line_reset; //if line reset
  643. bool smooth_halt_condition; //if halting
  644. int smooth_halt_count; //halting count
  645. point smooth_halt_position; //position while begin halting
  646. point smooth_halt_position_plus;
  647. point smooth_halt_position_minus;
  648. smooth_tool()=default;
  649. smooth_tool(std::shared_ptr<select_tool> st)
  650. :m_st(st)
  651. ,smooth_initial_setting(false)
  652. ,smooth_speed(0)
  653. ,smooth_speed_presentation(0)
  654. ,smooth_speed_presentation_cnt(0)
  655. ,smooth_last_time_sec(0)
  656. ,smooth_halt_condition(0)
  657. ,smooth_halt_count(0)
  658. {}
  659. void smooth_set_loc_point(double t, int ct, const site*sit, loc_point *lp)
  660. {
  661. point pt;
  662. if(smooth_halt_condition)
  663. pt.set(smooth_halt_position);
  664. else
  665. pt.set(smooth_last_position);
  666. lp->m_dist2=sit->dist_direct(pt);
  667. lp->m_smooth_x=pt.x;
  668. lp->m_smooth_y=pt.y;
  669. lp->m_time=(int64_t)(t*1000);
  670. lp->m_ct=ct;
  671. if(smooth_halt_condition)
  672. {
  673. lp->m_speed=0;
  674. lp->m_stat=0;
  675. }
  676. else
  677. {
  678. lp->m_speed=smooth_speed_presentation * (3.6*sit->m_scale) ; //(m/s) to (km/h)
  679. if(smooth_speed<0)
  680. lp->m_speed = -lp->m_speed;
  681. if(std::isnan(lp->m_speed))
  682. lp->m_speed=0;
  683. lp->m_stat=1;
  684. //if(fabs(smooth_speed_presentation) < 0.1)
  685. // lp->m_speed=0;
  686. }
  687. }
  688. void smooth_reset()
  689. {
  690. smooth_initial_setting=false;
  691. smooth_speed=0; //smoothed speed
  692. //smooth_speed_presentation=0;
  693. smooth_speed_presentation_cnt=0;
  694. smooth_last_position=point(0,0); //last position of smoothed point
  695. smooth_last_true_position=point(0,0); //last position of true point
  696. smooth_last_time_sec=0; //last time second
  697. smooth_halt_condition = false;
  698. smooth_halt_count=0;
  699. }
  700. bool smooth_initiate(point &pt, double t, const site*sit)
  701. {
  702. smooth_initial_setting=true;
  703. smooth_speed=0;
  704. //smooth_speed_presentation=0;
  705. smooth_speed_presentation_cnt=0;
  706. smooth_last_position = pt;
  707. smooth_last_true_position = pt;
  708. smooth_last_time_sec = t;
  709. smooth_halt_condition=false;
  710. smooth_halt_count=0;
  711. smooth_halt_position=pt;
  712. smooth_halt_position_plus=pt;
  713. smooth_halt_position_minus=pt;
  714. return true;
  715. }
  716. virtual void smooth_strategy() = 0;
  717. virtual void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr)=0;
  718. virtual ~smooth_tool(){}
  719. };
  720. struct smooth_tool_person_1:smooth_tool
  721. {
  722. smooth_tool_person_1(std::shared_ptr<select_tool> m)
  723. :smooth_tool(m)
  724. {}
  725. virtual void smooth_strategy(){}
  726. void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr)
  727. {
  728. if(smooth_line.empty() || !smooth_line.contain(pt,0.1))
  729. {
  730. if(!smooth_line_reset)
  731. {
  732. smooth_reset();
  733. smooth_line_reset=true;
  734. }
  735. else
  736. {
  737. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  738. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  739. path.clear();
  740. if(path.empty())
  741. {
  742. smooth_line.set(smooth_last_true_position, pt);
  743. smooth_line_reset=false;
  744. }
  745. else
  746. {
  747. smooth_reset();
  748. }
  749. }
  750. }
  751. else
  752. smooth_line_reset=false;
  753. if(!smooth_initial_setting)
  754. {
  755. smooth_initiate(pt, t, sit);
  756. }
  757. else
  758. {
  759. printf("lemon2");
  760. double current_dist = dstp.dist_direct(pt);
  761. double last_true_position = dstp.dist_direct(smooth_last_true_position);
  762. double max_span = 100;
  763. if(fabs(current_dist-last_true_position)<max_span && t - smooth_last_time_sec < 10)
  764. {
  765. double new_speed = (current_dist-last_true_position) / (t - smooth_last_time_sec);
  766. double speed_differ = fabs(new_speed-smooth_speed);
  767. if(speed_differ>1)new_speed=smooth_speed +1*(new_speed>smooth_speed?1:-1);
  768. smooth_speed = smooth_speed * 0.4 + new_speed * 0.6;
  769. smooth_last_true_position = pt;
  770. smooth_last_position = pt;
  771. smooth_last_time_sec = t;
  772. if(fabs(smooth_speed_presentation)<1e-6 || std::isnan(smooth_speed_presentation))
  773. {
  774. smooth_speed_presentation=fabs(smooth_speed);
  775. }
  776. else
  777. smooth_speed_presentation = smooth_speed_presentation * 0.4 + fabs(smooth_speed) * 0.6;
  778. if(fabs(smooth_speed)<0.1)smooth_speed_presentation=0;
  779. }
  780. else
  781. {
  782. smooth_reset();
  783. smooth_initiate(pt, t, sit);
  784. }
  785. }
  786. if(m_lp != nullptr)//m_time,m_ct,x,y,m_speed,m_stat
  787. {
  788. smooth_set_loc_point(t, ct, sit, m_lp);
  789. }
  790. }
  791. };
  792. struct smooth_tool_car_1:smooth_tool
  793. {
  794. smooth_tool_car_1(std::shared_ptr<select_tool> m)
  795. :smooth_tool(m)
  796. {}
  797. virtual void smooth_strategy(){}
  798. void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr)
  799. {
  800. point init_pt(pt.x, pt.y);
  801. if(smooth_line.empty() || !smooth_line.contain(pt,0.1) || smooth_halt_count>6)
  802. {
  803. if(!smooth_line_reset)
  804. {
  805. if(!smooth_line.empty() && !smooth_line.contain(pt,0.1) && !smooth_last_true_position.empty())
  806. {
  807. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  808. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  809. path.clear();
  810. printf("generating critical point in smooth(car):(%.2f,%.2f)->(%.2f,%.2f)\n",
  811. smooth_last_true_position.x, smooth_last_true_position.y, pt.x, pt.y);
  812. if(!path.empty())
  813. {
  814. point critical_point=path[0];
  815. printf("critical point generated in smooth(car):pt=(%.2f,%.2f),(%.2f,%.2f)->(%.2f,%.2f)\n",
  816. critical_point.x, critical_point.y, smooth_last_true_position.x, smooth_last_true_position.y,
  817. pt.x, pt.y);
  818. init_pt.set(critical_point);
  819. }
  820. }
  821. smooth_reset();
  822. smooth_line_reset=true;
  823. }
  824. else
  825. {
  826. std::vector<point> path=card_path::inst().find_path(smooth_last_true_position, pt);
  827. if(!path.empty() && smooth_last_true_position.dist(path[0])>200)
  828. path.clear();
  829. if(path.empty())
  830. {
  831. smooth_line.set(smooth_last_true_position, pt);
  832. smooth_line_reset=false;
  833. }
  834. else
  835. {
  836. smooth_reset();
  837. }
  838. }
  839. }
  840. else
  841. smooth_line_reset=false;
  842. if(!smooth_initial_setting)
  843. {
  844. smooth_initiate(init_pt, t, sit);
  845. }
  846. else
  847. {
  848. double current_dist = dstp.dist_direct(pt);
  849. double last_position = dstp.dist_direct(smooth_last_position);
  850. double last_true_position = dstp.dist_direct(smooth_last_true_position);
  851. double rec_kx=0;
  852. double rec_ky=0;
  853. if(current_dist!=0)
  854. {
  855. rec_kx = (pt.x - dstp.x)/current_dist;
  856. rec_ky = (pt.y - dstp.y)/current_dist;
  857. }
  858. double next_dist = last_position + smooth_speed * (t-smooth_last_time_sec);
  859. double max_span = 200;
  860. //printf("smooth dist:%f,%f,%f\n",next_dist,current_dist,next_dist-current_dist);
  861. if(fabs(next_dist-current_dist)<max_span && t - smooth_last_time_sec < 10)
  862. {
  863. double new_speed = (current_dist-last_true_position) / (t - smooth_last_time_sec);
  864. // judge halting
  865. if(fabs(new_speed)<0.1)smooth_halt_count++;
  866. else{
  867. smooth_halt_count=0;
  868. }
  869. if(!smooth_halt_condition && smooth_halt_count>=3 && fabs(smooth_speed) < 0.2)
  870. {
  871. smooth_halt_condition=true;
  872. smooth_halt_position=smooth_last_position;
  873. smooth_halt_position_plus=pt;
  874. smooth_halt_position_minus=pt;
  875. }
  876. // handle speed
  877. if(smooth_halt_condition)
  878. {
  879. double halt_position = dstp.dist_direct(smooth_halt_position);
  880. double halt_position_plus = dstp.dist_direct(smooth_halt_position_plus);
  881. double halt_position_minus = dstp.dist_direct(smooth_halt_position_minus);
  882. if(halt_position_plus<current_dist)halt_position_plus=current_dist;
  883. if(halt_position_minus>current_dist)halt_position_minus=current_dist;
  884. smooth_halt_position_plus = point(dstp.x + halt_position_plus * rec_kx, dstp.y + halt_position_plus * rec_ky);
  885. smooth_halt_position_minus = point(dstp.x + halt_position_minus * rec_kx, dstp.y + halt_position_minus * rec_ky);
  886. if(fabs(halt_position_plus - halt_position_minus)>1)
  887. {
  888. smooth_halt_condition=false;
  889. last_position = halt_position;
  890. smooth_speed = 0;
  891. //printf("smooth stop halting\n");
  892. }
  893. }
  894. else
  895. {
  896. if(fabs(smooth_speed)<1e-6 || std::isnan(smooth_speed))
  897. {
  898. smooth_speed=new_speed;
  899. if(smooth_speed>2.5)smooth_speed=2.5;
  900. if(smooth_speed<-2.5)smooth_speed=-2.5;
  901. }
  902. else
  903. {
  904. double speed_differ = fabs(new_speed-smooth_speed);
  905. if(speed_differ>1)
  906. new_speed=smooth_speed +1*(new_speed>smooth_speed?1:-1);
  907. smooth_speed = smooth_speed * 0.4 + new_speed * 0.6;
  908. }
  909. if(fabs(smooth_speed_presentation)<1e-6 || std::isnan(smooth_speed_presentation))
  910. {
  911. smooth_speed_presentation=fabs(smooth_speed);
  912. }
  913. else
  914. smooth_speed_presentation = smooth_speed_presentation * 0.4 + fabs(smooth_speed) * 0.6;
  915. //printf(",%f,%f\n",new_speed,smooth_speed);
  916. // must obey speed direction
  917. if(smooth_speed * (current_dist-last_position) > 0)
  918. {
  919. last_position = last_position+smooth_speed*(t-smooth_last_time_sec);
  920. if(smooth_speed * (current_dist-last_position) < 0)
  921. {
  922. last_position = current_dist;
  923. }
  924. smooth_speed_presentation_cnt=0;
  925. }
  926. else
  927. {
  928. if(smooth_speed_presentation_cnt<3)
  929. smooth_speed_presentation_cnt++;
  930. else
  931. smooth_speed_presentation=0;
  932. }
  933. if(fabs(smooth_speed)<0.1)smooth_speed_presentation=0;
  934. double revise_para = 0.2;
  935. if(fabs(smooth_speed) < 0.01 || smooth_speed * (current_dist-last_position) < 0)
  936. revise_para=0;
  937. last_position=last_position+(current_dist-last_position)*revise_para;
  938. }
  939. smooth_last_position = point(dstp.x + last_position * rec_kx, dstp.y + last_position * rec_ky);
  940. smooth_last_true_position = pt;
  941. smooth_last_time_sec = t;
  942. }
  943. else
  944. {
  945. smooth_reset();
  946. smooth_initiate(pt, t, sit);
  947. }
  948. }
  949. if(m_lp != nullptr)//m_time,m_ct,x,y,m_speed,m_stat
  950. {
  951. smooth_set_loc_point(t, ct, sit, m_lp);
  952. }
  953. }
  954. };
  955. struct smooth_tool_drivingface_car_1:smooth_tool
  956. {
  957. smooth_tool_drivingface_car_1(std::shared_ptr<select_tool> m)
  958. :smooth_tool(m)
  959. {}
  960. virtual void smooth_strategy()
  961. {}
  962. virtual void smooth_dist(point &pt, double t, int ct, const site*sit, point dstp, loc_point *m_lp = nullptr){}
  963. };
  964. //---------------------------------
  965. struct select_tool_manage
  966. {
  967. void create_tool(const std::string &s,std::shared_ptr<select_tool> &set,std::shared_ptr<smooth_tool> &smt)
  968. {
  969. if(s.compare(std::string{"person1"}))
  970. {
  971. set=std::make_shared<select_tool_person_1>();
  972. smt=std::make_shared<smooth_tool_person_1>(set);
  973. }
  974. else if(s.compare(std::string{"person2"}))
  975. {
  976. set=std::make_shared<select_tool_person_2>();
  977. smt=std::make_shared<smooth_tool_person_1>(set);
  978. }
  979. else if(s.compare(std::string{"car1"}))
  980. {
  981. set=std::make_shared<select_tool_car_1>();
  982. smt=std::make_shared<smooth_tool_car_1>(set);
  983. }
  984. else if(s.compare(std::string{"drivingface1"}))
  985. {
  986. set=std::make_shared<select_tool_drivingface_car_1>();
  987. smt=std::make_shared<smooth_tool_drivingface_car_1>(set);
  988. }
  989. }
  990. static select_tool_manage * instance();
  991. };
  992. #endif