select_tool.h 31 KB

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