1
0

select_tool.h 31 KB

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