select_tool.h 31 KB

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