select_tool.h 32 KB

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