select_tool.h 31 KB

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