card_path.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <unordered_map>
  4. #include <set>
  5. #include <memory>
  6. #include <atomic>
  7. #include <thread>
  8. #include <list>
  9. #include "zlist.h"
  10. #include "line.h"
  11. #include "ant.h"
  12. #include "config_file.h"
  13. #include "card_path.h"
  14. #include "visit.h"
  15. extern config_file config;
  16. namespace{
  17. static const double PI=3.1416;
  18. inline bool eq(double a,double b,double e=0.0001)
  19. {
  20. return fabs(a-b)<e;
  21. }
  22. struct vertex:point
  23. {
  24. float level;
  25. int sid0,sid1;
  26. vertex()
  27. :level(0)
  28. ,sid0(0)
  29. ,sid1(0)
  30. {}
  31. vertex(const point&pt,float level_=0,int site_id0=0,int site_id1=0)
  32. :point(pt)
  33. ,level(level_)
  34. ,sid0(site_id0)
  35. ,sid1(site_id1)
  36. {
  37. }
  38. std::string to_string()const
  39. {
  40. char buf[128];
  41. sprintf(buf,"%.2f,%.3lf,%d,%d,%.2lf",x,y,sid0,sid1,level/PI*180);
  42. return buf;
  43. }
  44. };
  45. struct vertex_list;
  46. struct base_path:std::array<int,2>
  47. {
  48. int sid;
  49. base_path(int i1=-1,int i2=-1)
  50. {
  51. at(0)=i1;
  52. at(1)=i2;
  53. }
  54. bool operator<(const base_path&o)const
  55. {
  56. int c=at(0)-o.at(0);
  57. if(c!=0)
  58. return c<0;
  59. return at(1)-o.at(1)<0;
  60. }
  61. point cross(const vertex_list&v_list, const base_path&o)const;
  62. bool contains(const vertex_list&v_list, const point&o, double e)const;
  63. double arg(const vertex_list&v)const;
  64. int type(const vertex_list&v)const;
  65. line_v as_line(const vertex_list&v)const;
  66. int level(const vertex_list&v)const;
  67. };
  68. struct vertex_list
  69. {
  70. std::vector<vertex> v;
  71. std::string to_string()const
  72. {
  73. std::string ret;
  74. ret.reserve(1024);
  75. char buf[128];
  76. for(int i=0,len=v.size();i<len;i++)
  77. {
  78. sprintf(buf,"vertex:%3d,%s\n",i,v[i].to_string().c_str());
  79. ret+=buf;
  80. }
  81. return std::move(ret);
  82. }
  83. void log()
  84. {
  85. log_info("%s",to_string().c_str());
  86. }
  87. static unsigned hash(const point&p)
  88. {
  89. return (((((unsigned)p.x)>>2)<<16) + (((unsigned)p.y)>>2));
  90. }
  91. int find(const point&p,double x=1)const
  92. {
  93. int rc=-1;
  94. double t;
  95. for(int i=0,len=v.size();i<len;i++)
  96. {
  97. t=p.dist(v[i]);
  98. if(t<x)
  99. {
  100. rc=i;
  101. x=t;
  102. }
  103. }
  104. return rc;
  105. }
  106. int add(const point&p,float level=0,int sid0=0,int sid1=0)
  107. {
  108. int i=find(p);
  109. if(i<0)
  110. {
  111. v.push_back(vertex(p,level,sid0,sid1));
  112. return v.size()-1;
  113. }
  114. vertex&v0=v[i];
  115. v0.level=std::max(v0.level,level);
  116. return i;
  117. }
  118. int size()const
  119. {
  120. return v.size();
  121. }
  122. const vertex&operator[](int i)const
  123. {
  124. return v[i];
  125. }
  126. vertex&operator[](int i)
  127. {
  128. return v[i];
  129. }
  130. };
  131. int base_path::level(const vertex_list&v)const
  132. {
  133. const vertex&p0=v[at(0)];
  134. const vertex&p1=v[at(1)];
  135. return p0.level+p1.level;
  136. }
  137. line_v base_path::as_line(const vertex_list&v)const
  138. {
  139. const vertex&p0=v[at(0)];
  140. const vertex&p1=v[at(1)];
  141. return line_v(p0,p1);
  142. }
  143. double base_path::arg(const vertex_list&v)const
  144. {
  145. return as_line(v).arg();
  146. }
  147. bool base_path::contains(const vertex_list&v, const point&o, double e)const
  148. {
  149. return as_line(v).contain(o,e);
  150. }
  151. point base_path::cross(const vertex_list&v, const base_path&o)const
  152. {
  153. line_v l0=as_line(v);
  154. line_v l1=o.as_line(v);
  155. return l0.crossing(l1);
  156. }
  157. void log_path(const std::vector<base_path>&path,const vertex_list&v_list)
  158. {
  159. log_info("%s\n","----------------------------------------------------------------");
  160. for(int i=0,len=path.size();i<len;i++)
  161. {
  162. double c=path[i].arg(v_list)/PI*180;
  163. log_info("base_path %.6lf, %03d,(%d,%s),(%d,%s)",c,i,path[i][0],v_list[path[i][0]].to_string().c_str(),path[i][1],v_list[path[i][1]].to_string().c_str());
  164. }
  165. }
  166. struct handle_path :visitor<std::shared_ptr<site>>
  167. {
  168. std::vector<base_path> ret;
  169. vertex_list v;
  170. handle_path()
  171. {
  172. v.add(point(0,0),0,-1);
  173. }
  174. bool visit(std::shared_ptr<site> sit)
  175. {
  176. //auto&s=sites[i];
  177. const auto &s = *sit;
  178. if(!s.have_valid_path())
  179. return false;
  180. if(s.path(0).empty()||s.path(1).empty())
  181. return false;
  182. if(s[0].size()<2)
  183. return false;
  184. line_v l000 = s[0][0][0];
  185. line_v l010 = s[0][1][0];
  186. if(l000.is_same(l010))
  187. {
  188. int p0=v.add(point::min(s.path(0),s.path(1)),0,s.m_id);
  189. int p1=v.add(point::max(s.path(0),s.path(1)),0,s.m_id);
  190. ret.push_back(base_path(p0,p1));
  191. ret.back().sid=s.m_id;
  192. }
  193. else
  194. {
  195. point px = l000.line::crossing(l010);
  196. for(int i=0;i<2;i++)
  197. {
  198. int p0=v.add(point::min(px,s.path(i)),0,s.m_id);
  199. int p1=v.add(point::max(px,s.path(i)),0,s.m_id);
  200. ret.push_back(base_path(p0,p1));
  201. ret.back().sid=s.m_id;
  202. }
  203. }
  204. for(int i=0;i<2;i++)
  205. {
  206. if(!s[0][i][1].empty())
  207. {
  208. int p0=v.add(point::min(s[0][i][1][0],s[0][i][1][1]),0,s.m_id);
  209. int p1=v.add(point::max(s[0][i][1][0],s[0][i][1][1]),0,s.m_id);
  210. ret.push_back(base_path(p0,p1));
  211. ret.back().sid=s.m_id;
  212. }
  213. }
  214. return true;
  215. }
  216. };
  217. static std::vector<base_path> init_path(std::vector<base_path> & ret,vertex_list&v)
  218. {
  219. // for(uint32_t i=0;i<sites.m_list.size();i++)
  220. // {
  221. // auto&s=sites[i];
  222. //
  223. // if(!s.have_valid_path())
  224. // continue;
  225. //
  226. // if(s.path(0).empty()||s.path(1).empty())
  227. // continue;
  228. // if(s[0].size()<2)
  229. // continue;
  230. // line_v l000 = s[0][0][0];
  231. // line_v l010 = s[0][1][0];
  232. // if(l000.is_same(l010))
  233. // {
  234. // printf("same....%d",s.m_id);
  235. // int p0=v.add(point::min(s.path(0),s.path(1)),0,s.m_id);
  236. // int p1=v.add(point::max(s.path(0),s.path(1)),0,s.m_id);
  237. //
  238. // ret.push_back(base_path(p0,p1));
  239. // ret.back().sid=s.m_id;
  240. // }
  241. // else
  242. // {
  243. // point px = l000.line::crossing(l010);
  244. // for(int i=0;i<2;i++)
  245. // {
  246. // int p0=v.add(point::min(px,s.path(i)),0,s.m_id);
  247. // int p1=v.add(point::max(px,s.path(i)),0,s.m_id);
  248. //
  249. // ret.push_back(base_path(p0,p1));
  250. // ret.back().sid=s.m_id;
  251. // }
  252. // }
  253. // for(int i=0;i<2;i++)
  254. // {
  255. // if(!s[0][i][1].empty())
  256. // {
  257. // int p0=v.add(point::min(s[0][i][1][0],s[0][i][1][1]),0,s.m_id);
  258. // int p1=v.add(point::max(s[0][i][1][0],s[0][i][1][1]),0,s.m_id);
  259. //
  260. // ret.push_back(base_path(p0,p1));
  261. // ret.back().sid=s.m_id;
  262. //
  263. // }
  264. // }
  265. // }
  266. /*
  267. if(x.contain(s,2.5))
  268. {
  269. int p0=v.add(point::min(s.path(0),s.path(1)),0,s.m_id);
  270. int p1=v.add(point::max(s.path(0),s.path(1)),0,s.m_id);
  271. ret.push_back(base_path(p0,p1));
  272. ret.back().sid=s.m_id;
  273. continue;
  274. }
  275. for(int j=0;j<2;j++)
  276. {
  277. if(!s.path(j).empty() && s.dist(s.path(j))<2)
  278. continue;
  279. point p=s.path(j);
  280. int p0=v.add(point::min(s,p),0,s.m_id);
  281. int p1=v.add(point::max(s,p),0,s.m_id);
  282. ret.push_back(base_path(p0,p1));
  283. ret.back().sid=s.m_id;
  284. }
  285. }
  286. */
  287. #ifdef __DEBUG__
  288. log_path(ret,v);
  289. printf("++++++++++++++++++++++++++++++++++++++++++++");
  290. #endif
  291. std::sort(ret.begin(),ret.end());
  292. #ifdef __DEBUG__
  293. log_path(ret,v);
  294. printf("++++++++++++++++++++++++++++++++++++++++++++");
  295. #endif
  296. ret.erase(std::unique(ret.begin(),ret.end()),ret.end());
  297. #ifdef __DEBUG__
  298. log_path(ret,v);
  299. printf("+++++++++++++++++nnnnn+++++++++++++++++++++++++++");
  300. #endif
  301. std::sort(ret.begin(),ret.end(),[&v](const base_path&p1,const base_path&p2){
  302. double arg=p1.arg(v)-p2.arg(v);
  303. if(fabs(arg)<0.1)
  304. {
  305. return v[p1[0]]<v[p2[0]];
  306. }
  307. return arg<0;
  308. });
  309. #ifdef __DEBUG__
  310. log_path(ret,v);
  311. #endif
  312. for(int i=0,len=ret.size();i<len;i++)
  313. {
  314. line_v li=ret[i].as_line(v);
  315. for(int j=i+1;j<len;j++)
  316. {
  317. line_v lj=ret[j].as_line(v);
  318. if(!lj.is_same(li,2.5))
  319. continue;
  320. line_v ij=lj.projection(li);
  321. if(ij.empty())
  322. continue;
  323. point p0=point::min(v[ret[j][0]],v[ret[i][0]]);
  324. point p1=point::max(v[ret[j][1]],v[ret[i][1]]);
  325. ret[j][0]=v.add(p0,0);
  326. ret[j][1]=v.add(p1,0);
  327. ret[i][0]=0;
  328. ret[i][1]=0;
  329. break;
  330. }
  331. }
  332. ret.erase(std::remove_if(ret.begin(),ret.end(),[&v](const base_path&p){
  333. return p[0]==0||p[1]==0;
  334. }),ret.end());
  335. #ifdef __DEBUG__
  336. std::sort(ret.begin(),ret.end(),[&v](const base_path&p1,const base_path&p2){
  337. double arg=p1.arg(v)-p2.arg(v);
  338. if(fabs(arg)<0.1)
  339. {
  340. return v[p1[0]]<v[p2[0]];
  341. }
  342. return arg<0;
  343. });
  344. log_path(ret,v);
  345. #endif
  346. //非路径交点配置
  347. std::vector<point> no_cross;
  348. auto in_no_cross=[&no_cross](const point&pt){
  349. for(auto p:no_cross)
  350. {
  351. if(p.dist(pt)<3)
  352. return true;
  353. }
  354. return false;
  355. };
  356. do
  357. {
  358. config_file cfg;
  359. if(cfg.open("../etc/path-not-cross.ini"))
  360. break;
  361. auto kset=cfg.keys();
  362. if(kset.empty())
  363. break;
  364. for(auto k:kset)
  365. {
  366. const char* v=cfg.get(k,"");
  367. point tmp;
  368. if(sscanf(v,"%lf,%lf",&tmp.x,&tmp.y)==2)
  369. no_cross.push_back(tmp);
  370. }
  371. for(auto p:no_cross)
  372. {
  373. log_info("no_cross_point:x=%lf,y=%lf",p.x,p.y);
  374. }
  375. break;
  376. }while(1);
  377. std::vector<std::vector<int>> p0(ret.size());
  378. std::vector<base_path> ret2;
  379. for(int i=0,len=ret.size();i<len;i++)
  380. {
  381. p0[i].push_back(ret[i][0]);
  382. for(int j=i+1;j<len;j++)
  383. {
  384. if(i==j) continue;
  385. point cr=ret[i].cross(v,ret[j]);
  386. if(cr.empty())
  387. continue;
  388. if(in_no_cross(cr))
  389. continue;
  390. double arg=fabs(ret[i].as_line(v).arg()-ret[j].as_line(v).arg());
  391. while(arg>PI/2)
  392. arg-=PI/2;
  393. if(arg/PI*180<5)//相交小于5度,不切分已有路径
  394. continue;
  395. int id=v.add(cr,arg,v[ret[i][0]].sid0,v[ret[j][0]].sid0);
  396. p0[i].push_back(id);
  397. p0[j].push_back(id);
  398. }
  399. p0[i].push_back(ret[i][1]);
  400. std::sort(p0[i].begin(),p0[i].end(),[&v](int i0,int i1){
  401. return v[i0]<v[i1];
  402. });
  403. auto it=std::unique(p0[i].begin(),p0[i].end());
  404. p0[i].erase(it,p0[i].end());
  405. for(int j=1,cnt=p0[i].size();j<cnt;j++)
  406. {
  407. ret2.push_back(base_path(p0[i][j - 1], p0[i][j]));
  408. ret2.back().sid = ret[i].sid;
  409. }
  410. }
  411. ret2.erase(std::remove_if(ret2.begin(),ret2.end(),[&v](base_path&p){
  412. point&p0=v[p[0]];
  413. point&p1=v[p[1]];
  414. //return p0.dist(p1)<0.1 || p0.empty() || p1.empty();
  415. return p0.dist(p1)<0.1;
  416. }),ret2.end());
  417. std::sort(ret2.begin(),ret2.end());
  418. ret2.erase(std::unique(ret2.begin(),ret2.end()),ret2.end());
  419. /*
  420. ret.clear();
  421. for(int i=0,len=ret2.size();i<len;i++)
  422. {
  423. std::vector<line_v> tmp;
  424. tmp.push_back(ret2[i].as_line(v));
  425. for(int j=0;j<len;j++)
  426. {
  427. if(i==j) continue;
  428. if(ret[j].level(v)<ret[i].level(v))
  429. continue;
  430. line l1=ret2[j].as_line(v);
  431. }
  432. }
  433. std::sort(ret2.begin(),ret2.end(),[&v](base_path&p1,base_path&p2){
  434. return p1.arg(v)<p2.arg(v);
  435. });
  436. */
  437. #ifdef __DEBUG__
  438. std::sort(ret2.begin(),ret2.end(),[&v](const base_path&p1,const base_path&p2){
  439. double arg=p1.arg(v)-p2.arg(v);
  440. if(fabs(arg)<0.1)
  441. {
  442. return v[p1[0]]<v[p2[0]];
  443. }
  444. return arg<0;
  445. });
  446. #endif
  447. log_path(ret2,v);
  448. return std::move(ret2);
  449. }
  450. #if 0
  451. struct ghash
  452. {
  453. static std::tuple<int,int> decode(unsigned h)
  454. {
  455. const uint64_t S=0x8000000000000000;
  456. int x=0,y=0;
  457. for(int i=0;i<64;i++)
  458. {
  459. x<<=1;
  460. y<<=1;
  461. if(h&S)
  462. x|=1;
  463. h<<=1;
  464. if(h&S)
  465. y|=1;
  466. h<<=1;
  467. }
  468. return std::make_tuple(x-2147483648,y-2147483648);
  469. }
  470. static uint64_t encode(int64_t x, int64_t y)
  471. {
  472. return encode_(x+2147483648,y+2147483648);
  473. }
  474. public: //test
  475. static void test_code(int64_t x,int64_t y)
  476. {
  477. uint64_t h=ghash::encode(x,y);
  478. auto t=ghash::decode(h);
  479. printf("src x=%X,y=%X hash=%X,check x=%X,y=%X\n",x,y,h,std::get<0>(t),std::get<1>(t));
  480. }
  481. static void test()
  482. {
  483. for(int i=0;i<10;i++)
  484. {
  485. test_code((4<<i)-1,(4<<i)-1);
  486. test_code((4<<i)-1,(4<<i));
  487. test_code((4<<i)-1,(4<<i)-1);
  488. test_code((4<<i),(4<<i)-1);
  489. }
  490. }
  491. private:
  492. static unsigned encode_(unsigned short x, unsigned short y)
  493. {
  494. const unsigned S=0x8000;
  495. unsigned r=0;
  496. for(int i=0;i<16;i++)
  497. {
  498. r<<=2;
  499. if(x&S)
  500. {
  501. r|=(y&S)?3:2;
  502. }
  503. else
  504. {
  505. if(y&S) r|=1;
  506. }
  507. x<<=1;
  508. y<<=1;
  509. }
  510. return r;
  511. }
  512. };
  513. #endif
  514. struct geolist
  515. {
  516. std::unordered_map<uint64_t,int> geo2path;
  517. public:
  518. static uint64_t encode(double x0,double y0)
  519. {
  520. uint64_t x=(uint32_t)(x0*10);
  521. uint64_t y=(uint32_t)(y0*10);
  522. return (x<<32)|y;
  523. }
  524. int find(double x,double y)const
  525. {
  526. uint64_t h=encode(x,y);
  527. auto it=geo2path.find(h);
  528. if(it==geo2path.end())
  529. return -1;
  530. return it->second;
  531. }
  532. void add(double x,double y,int id)
  533. {
  534. uint64_t h=encode(x,y);
  535. geo2path.insert(std::make_pair(h,id));
  536. }
  537. int size()
  538. {
  539. return geo2path.size();
  540. }
  541. void print()
  542. {
  543. for(auto it=geo2path.begin();it!=geo2path.end();++it)
  544. {
  545. //std::cout<<it->first<<"\n";
  546. printf("%ld\n",it->first);
  547. }
  548. }
  549. geolist()
  550. {
  551. }
  552. ~geolist()
  553. {
  554. }
  555. };
  556. struct graph
  557. {
  558. vertex_list v;
  559. std::vector<std::vector<int>> d; //索引为起始顶点,数组内容为目标点
  560. std::vector<std::array<int,2>> l; //bast_path list
  561. geolist geo;
  562. graph()
  563. {}
  564. void init(const vertex_list&v_,const std::vector<base_path>&bp)
  565. {
  566. for(auto&p:bp)
  567. {
  568. int from=v.add(v_[p[0]]);
  569. int to=v.add(v_[p[1]]);
  570. l.push_back({from,to});
  571. int id=l.size()-1;
  572. line_v lv(v[from],v[to]);
  573. log_info("graph::init() site:%d line:%s\n",p.sid, lv.to_string().c_str());
  574. double cos=lv.cos_k();
  575. double sin=lv.sin_k();
  576. double x,y;
  577. for(double r=0,len=lv.length();r<len+0.1;r+=0.05)
  578. {
  579. x=lv[0].x+r*cos;
  580. y=lv[0].y+r*sin;
  581. // printf("x=%lf,y=%lf\n",x,y);
  582. for(int i=-1;i<=1;i++)
  583. for(int j=-1;j<=1;j++)
  584. {
  585. geo.add(x+i/10.,y+j/10.,id);
  586. }
  587. }
  588. add(from,to);
  589. }
  590. }
  591. std::vector<line_v> find_possible_path(const point&from,double dist) const
  592. {
  593. std::vector<line_v> ret;
  594. int start_pt_id=v.find(from,dist);
  595. if(start_pt_id==-1)
  596. return std::move(ret);
  597. point start_pt=v[start_pt_id];
  598. for(uint32_t i=0;i<d[start_pt_id].size();i++)
  599. {
  600. ret.push_back(line_v(start_pt,v[d[start_pt_id][i]]));
  601. }
  602. return std::move(ret);
  603. }
  604. void add(int from,int to)
  605. {
  606. if((int)d.size()<=from) d.resize(from+1);
  607. if((int)d.size()<=to) d.resize(to+1);
  608. d[from].push_back(to);
  609. d[to].push_back(from);
  610. }
  611. bool find(std::vector<int>&ret,int from,std::array<int,2>&to,std::set<int>&ex,int level)
  612. {
  613. if(level--<0)
  614. return false;
  615. ret.push_back(from);
  616. for(int p:d[from]) //在当前路径
  617. {
  618. if(ex.find(p)!=ex.end()) //找过
  619. continue;
  620. if(p==to[0]||p==to[1])//找到
  621. return true;
  622. // {
  623. // ret.push_back(p);
  624. // return true;
  625. // }
  626. }
  627. ex.insert(from);
  628. for(int p:d[from])//在下级路径
  629. {
  630. if(ex.find(p)!=ex.end())//找过
  631. continue;
  632. if(find(ret,p,to,ex,level)) //找到
  633. {
  634. return true;
  635. }
  636. }
  637. ret.pop_back();
  638. return false;
  639. }
  640. bool is_at_path(const point&pt)const
  641. {
  642. return geo.find(pt.x,pt.y)>=0;
  643. }
  644. struct item
  645. {
  646. item(int _vid=-1, int _g=0,int _h=0,std::shared_ptr<item> _parent=nullptr)
  647. :vid(_vid)
  648. ,g(_g)
  649. ,h(_h)
  650. ,parent(_parent)
  651. {
  652. }
  653. int vid=-1,g=0,h=0;
  654. std::shared_ptr<item> parent;
  655. int cost()const {return g+h;}
  656. bool operator< (const item&i) const
  657. {
  658. return vid<i.vid;
  659. }
  660. };
  661. std::vector<point> find2(const point&from,const point&to)
  662. {
  663. int f=geo.find(from.x,from.y);
  664. int t=geo.find(to.x,to.y);
  665. if(f<0 || t<0 || f==t)
  666. return {};
  667. std::set<std::shared_ptr<item>> open_set;
  668. std::set<int> close_set;
  669. open_set.insert(std::make_shared<item>(l[f][0],from.dist(v[l[f][0]]),to.dist(v[l[f][0]])));
  670. open_set.insert(std::make_shared<item>(l[f][1],from.dist(v[l[f][1]]),to.dist(v[l[f][1]])));
  671. std::shared_ptr<item> cur;
  672. while(open_set.size()>0)
  673. {
  674. cur=*open_set.begin();
  675. for(auto&i:open_set)
  676. {
  677. if(i->cost()<cur->cost())
  678. cur=i;
  679. }
  680. open_set.erase(cur);
  681. close_set.insert(cur->vid);
  682. if(cur->vid==l[t][0]||cur->vid==l[t][1])
  683. break;
  684. for(auto vid:d[cur->vid])
  685. {
  686. if(close_set.find(vid)!=close_set.end())
  687. continue;
  688. open_set.insert(std::make_shared<item>(vid,cur->g+v[cur->vid].dist(v[vid]),to.dist(v[vid]),cur));
  689. }
  690. }
  691. if(open_set.size()==0)
  692. {
  693. return {};
  694. }
  695. std::vector<point> rc;
  696. while(cur.get())
  697. {
  698. rc.push_back(v[cur->vid]);
  699. cur=cur->parent;
  700. }
  701. std::reverse(rc.begin(),rc.end());
  702. rc.push_back(to);
  703. auto bp=from;
  704. auto it1=rc.begin();
  705. for(auto it2=it1+1;it1!=rc.end() && it2!=rc.end();it2++)
  706. {
  707. if(line(bp,*it1).dist(*it2)<0.1)
  708. {
  709. *it1=*it2;
  710. }
  711. else
  712. {
  713. bp=*it1++;
  714. *it1=*it2;
  715. }
  716. }
  717. rc.erase(it1,rc.end());
  718. if(rc.size() && from.dist(*rc.begin())<0.1)
  719. {
  720. rc.erase(rc.begin());
  721. }
  722. return std::move(rc);
  723. }
  724. std::vector<point> find(const point&from,const point&to)
  725. {
  726. int f=geo.find(from.x,from.y);
  727. int t=geo.find(to.x,to.y);
  728. if(f<0 || t<0 || f==t)
  729. return {};
  730. #if 0
  731. std::vector<point> rc[2];
  732. for(int i=0;i<2;i++)
  733. {
  734. std::vector<int> ret;
  735. std::set<int> ex({l[f][1-i]});
  736. if(find(ret,l[f][i],l[t],ex,2))
  737. {
  738. for(auto id:ret)
  739. rc[i].push_back(v[id]);
  740. }
  741. }
  742. if(!rc[0].empty() && (rc[1].empty() || rc[0].size() < rc[1].size()))
  743. return std::move(rc[0]);
  744. else if(!rc[1].empty())
  745. return std::move(rc[1]);
  746. #endif
  747. std::vector<int> ret;
  748. int size=v.size();
  749. std::vector<double> mat(size+1,-1);
  750. std::vector<double> trace(size+1,-1);
  751. std::vector<bool> val(size+2,false);
  752. mat[l[t][0]]=to.dist(v[l[t][0]]);
  753. mat[l[t][1]]=to.dist(v[l[t][1]]);
  754. int f0=l[f][0],f1=l[f][1];
  755. for(int i=0;i<2;i++)
  756. {
  757. if(mat[f0]!=-1 && mat[f1]!=-1)
  758. break;
  759. for(int j=0;j<size;j++)
  760. if(mat[j]!=-1)val[j]=true;
  761. for(int j=0;j<size;j++)
  762. {
  763. if(val[j]==false)continue;
  764. for(unsigned int k=0;k<d[j].size();k++)
  765. {
  766. double dist=v[j].dist(v[d[j][k]]);
  767. if(mat[d[j][k]]==-1 || mat[j]+dist < mat[d[j][k]])
  768. {
  769. mat[d[j][k]] = mat[j]+dist;
  770. trace[d[j][k]] = j;
  771. }
  772. }
  773. }
  774. }
  775. mat[f0]=mat[f0]+from.dist(v[l[f][0]]);
  776. mat[f1]=mat[f1]+from.dist(v[l[f][1]]);
  777. std::vector<point> rc;
  778. if(mat[f0]!=-1 && (mat[f1]==-1 || mat[f0] < mat[f1]))
  779. {
  780. int temp=f0;
  781. while(temp!=-1)
  782. {
  783. rc.push_back(v[temp]);
  784. temp=trace[temp];
  785. }
  786. return std::move(rc);
  787. }
  788. else if(mat[f1]!=-1)
  789. {
  790. int temp=f1;
  791. while(temp!=-1)
  792. {
  793. rc.push_back(v[temp]);
  794. temp=trace[temp];
  795. }
  796. return std::move(rc);
  797. }
  798. return {};
  799. }
  800. };
  801. safe_shared_ptr<graph> g_graph(std::make_shared<graph>());
  802. }//namespace
  803. void card_path::init()
  804. {
  805. std::shared_ptr<graph> local_graph=std::make_shared<graph>();
  806. handle_path hp;
  807. sit_list::instance()->accept(hp);
  808. std::vector<base_path> opath=init_path(hp.ret,hp.v);
  809. local_graph->init(hp.v,opath);
  810. g_graph.set(local_graph);
  811. }
  812. card_path&card_path::inst()
  813. {
  814. static card_path path;
  815. return path;
  816. }
  817. std::vector<point> card_path::find_path(const point&from,const point&to)const
  818. {
  819. return g_graph.get()->find2(from,to);
  820. }
  821. bool card_path::is_at_path(const point&pt)const
  822. {
  823. return g_graph.get()->is_at_path(pt);
  824. }
  825. std::vector<line_v> card_path::find_possible_path(const point&from,double dist) const
  826. {
  827. return std::move(g_graph.get()->find_possible_path(from,dist));
  828. }
  829. #ifdef __DEBUG__
  830. void test_at_path(card_path&cp,const point&pt)
  831. {
  832. if(cp.is_at_path(pt))
  833. log_info("test (%.3lf,%.3lf) is-at-path:true\n",pt.x,pt.y);
  834. else
  835. log_info("test (%.3lf,%.3lf) is-at-path:false\n",pt.x,pt.y);
  836. }
  837. void test_find_path(card_path&cp,const point&p1,const point&p2)
  838. {
  839. printf("\nfind-path: from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
  840. std::vector<point> rc=cp.find_path(p1,p2);
  841. for(uint32_t i=0;i<rc.size();i++)
  842. log_info("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
  843. }
  844. void test_find_poss_path(const card_path&cp ,const point&from)
  845. {
  846. log_info("\nfind-poss_path: from=(%.3lf,%.3lf)\n",from.x,from.y);
  847. std::vector<line_v> rc=cp.find_possible_path(from,3);
  848. for(uint32_t i=0;i<rc.size();i++)
  849. {
  850. log_info("poss path from=(%.3lf,%.3lf) to=(%.3lf,%.3lf)\n", rc[i][0].x,rc[i][0].y, rc[i][1].x,rc[i][1].y);
  851. }
  852. }
  853. int main()
  854. {
  855. // std::unique_ptr<sit_list> sites(new sit_list());
  856. //sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  857. sit_list::instance()->load("1.txt","2.txt");
  858. card_path cp;
  859. cp.init();
  860. #if 0
  861. {
  862. test_at_path(cp,point(4773.104654,-104.0887));
  863. test_at_path(cp,point(4727,-74.8));
  864. test_at_path(cp,point(4727,-75.0));
  865. test_at_path(cp,point(4727,-75.2));
  866. test_at_path(cp,point(4727,-75.8));
  867. test_at_path(cp,point(4727,-90));
  868. test_at_path(cp,point(4727,-89.6));
  869. test_at_path(cp,point(4727,-89.8));
  870. test_at_path(cp,point(4727,-90));
  871. test_at_path(cp,point(4727,-90.2));
  872. test_at_path(cp,point(4727,-90.4));
  873. }
  874. #endif
  875. //test_at_path(cp,point(4726.90,376.85));
  876. test_find_path(cp,point(-227.6,174.9),point(-201.3,175.5));
  877. test_find_path(cp,point(-227.625219,174.945670),point(-170.085722,176.101574));
  878. test_find_path(cp,point(-393.1944,262.5324),point(-366.0754,274.1253));
  879. //test_find_path(cp,point(4637.000000,-75),point(4727,135));
  880. #if 1
  881. {
  882. test_find_path(cp,point(4637.000000,-75),point(4727,135));
  883. test_find_path(cp,point(4637.000000,-75),point(4727,120));
  884. test_find_path(cp,point(4700,-75),point(4727,120));
  885. test_find_path(cp,point(4725,-75),point(4727,135));
  886. test_find_path(cp,point(4727,-89.7),point(6144.3,-523));
  887. }
  888. #endif
  889. #if 0
  890. {
  891. test_find_poss_path(cp, point(4727,-89));
  892. test_find_poss_path(cp, point(4727,-90));
  893. test_find_poss_path(cp, point(4724,-75));
  894. test_find_poss_path(cp, point(4725,-75));
  895. test_find_poss_path(cp, point(4726,-75));
  896. test_find_poss_path(cp, point(4727,-75));
  897. test_find_poss_path(cp, point(4728,-75));
  898. test_find_poss_path(cp, point(4727,-7));
  899. test_find_poss_path(cp, point(4726.6,33));
  900. }
  901. #endif
  902. return 0;
  903. }
  904. #endif