card_path.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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,const char*add_info="")
  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:%s %.6lf, %03d,(%d,%s),(%d,%s)",add_info,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. #ifdef __DEBUG__
  220. log_path(ret,v);
  221. printf("++++++++++++++++++++++++++++++++++++++++++++");
  222. #endif
  223. std::sort(ret.begin(),ret.end());
  224. #ifdef __DEBUG__
  225. log_path(ret,v);
  226. printf("++++++++++++++++++++++++++++++++++++++++++++");
  227. #endif
  228. ret.erase(std::unique(ret.begin(),ret.end()),ret.end());
  229. #ifdef __DEBUG__
  230. log_path(ret,v);
  231. printf("+++++++++++++++++nnnnn+++++++++++++++++++++++++++");
  232. #endif
  233. std::sort(ret.begin(),ret.end(),[&v](const base_path&p1,const base_path&p2){
  234. double arg=p1.arg(v)-p2.arg(v);
  235. if(fabs(arg)<0.1)
  236. {
  237. return v[p1[0]]<v[p2[0]];
  238. }
  239. return arg<0;
  240. });
  241. log_path(ret,v,"合并前");
  242. //合并相同
  243. for(int i=0,len=ret.size();i<len;i++)
  244. {
  245. line_v li=ret[i].as_line(v);
  246. for(int j=i+1;j<len;j++)
  247. {
  248. line_v lj=ret[j].as_line(v);
  249. if(!lj.is_same(li,2.5))
  250. continue;
  251. line_v ij=lj.projection(li);
  252. if(ij.empty())
  253. continue;
  254. point p0=point::min(v[ret[j][0]],v[ret[i][0]]);
  255. point p1=point::max(v[ret[j][1]],v[ret[i][1]]);
  256. ret[j][0]=v.add(p0,0);
  257. ret[j][1]=v.add(p1,0);
  258. ret[i][0]=0;
  259. ret[i][1]=0;
  260. break;
  261. }
  262. }
  263. ret.erase(std::remove_if(ret.begin(),ret.end(),[&v](const base_path&p){
  264. return p[0]==0||p[1]==0;
  265. }),ret.end());
  266. std::sort(ret.begin(),ret.end(),[&v](const base_path&p1,const base_path&p2){
  267. double arg=p1.arg(v)-p2.arg(v);
  268. if(fabs(arg)<0.1)
  269. {
  270. return v[p1[0]]<v[p2[0]];
  271. }
  272. return arg<0;
  273. });
  274. log_path(ret,v,"合并后");
  275. //非路径交点配置
  276. std::vector<point> no_cross;
  277. auto in_no_cross=[&no_cross](const point&pt){
  278. for(auto p:no_cross)
  279. {
  280. if(p.dist(pt)<3)
  281. return true;
  282. }
  283. return false;
  284. };
  285. do
  286. {
  287. config_file cfg;
  288. if(cfg.open("../etc/path-not-cross.ini"))
  289. break;
  290. auto kset=cfg.keys();
  291. if(kset.empty())
  292. break;
  293. for(auto k:kset)
  294. {
  295. const char* v=cfg.get(k,"");
  296. point tmp;
  297. if(sscanf(v,"%lf,%lf",&tmp.x,&tmp.y)==2)
  298. no_cross.push_back(tmp);
  299. }
  300. for(auto p:no_cross)
  301. {
  302. log_info("no_cross_point:x=%lf,y=%lf",p.x,p.y);
  303. }
  304. break;
  305. }while(1);
  306. std::vector<std::vector<int>> p0(ret.size());
  307. std::vector<base_path> ret2;
  308. for(int i=0,len=ret.size();i<len;i++)
  309. {
  310. p0[i].push_back(ret[i][0]);
  311. for(int j=i+1;j<len;j++)
  312. {
  313. if(i==j) continue;
  314. point cr=ret[i].cross(v,ret[j]);
  315. if(cr.empty())
  316. continue;
  317. if(in_no_cross(cr))
  318. continue;
  319. double arg=fabs(ret[i].as_line(v).arg()-ret[j].as_line(v).arg());
  320. while(arg>PI/2)
  321. arg-=PI/2;
  322. if(arg/PI*180<5)//相交小于5度,不切分已有路径
  323. continue;
  324. int id=v.add(cr,arg,v[ret[i][0]].sid0,v[ret[j][0]].sid0);
  325. p0[i].push_back(id);
  326. p0[j].push_back(id);
  327. }
  328. p0[i].push_back(ret[i][1]);
  329. std::sort(p0[i].begin(),p0[i].end(),[&v](int i0,int i1){
  330. return v[i0]<v[i1];
  331. });
  332. auto it=std::unique(p0[i].begin(),p0[i].end());
  333. p0[i].erase(it,p0[i].end());
  334. for(int j=1,cnt=p0[i].size();j<cnt;j++)
  335. {
  336. ret2.push_back(base_path(p0[i][j - 1], p0[i][j]));
  337. ret2.back().sid = ret[i].sid;
  338. }
  339. }
  340. ret2.erase(std::remove_if(ret2.begin(),ret2.end(),[&v](base_path&p){
  341. point&p0=v[p[0]];
  342. point&p1=v[p[1]];
  343. //return p0.dist(p1)<0.1 || p0.empty() || p1.empty();
  344. return p0.dist(p1)<1;
  345. }),ret2.end());
  346. std::sort(ret2.begin(),ret2.end());
  347. ret2.erase(std::unique(ret2.begin(),ret2.end()),ret2.end());
  348. std::sort(ret2.begin(),ret2.end(),[&v](const base_path&p1,const base_path&p2){
  349. double arg=p1.arg(v)-p2.arg(v);
  350. if(fabs(arg)<0.1)
  351. {
  352. return v[p1[0]]<v[p2[0]];
  353. }
  354. return arg<0;
  355. });
  356. log_path(ret2,v,"拆分后");
  357. return std::move(ret2);
  358. }
  359. #if 0
  360. struct ghash
  361. {
  362. static std::tuple<int,int> decode(unsigned h)
  363. {
  364. const uint64_t S=0x8000000000000000;
  365. int x=0,y=0;
  366. for(int i=0;i<64;i++)
  367. {
  368. x<<=1;
  369. y<<=1;
  370. if(h&S)
  371. x|=1;
  372. h<<=1;
  373. if(h&S)
  374. y|=1;
  375. h<<=1;
  376. }
  377. return std::make_tuple(x-2147483648,y-2147483648);
  378. }
  379. static uint64_t encode(int64_t x, int64_t y)
  380. {
  381. return encode_(x+2147483648,y+2147483648);
  382. }
  383. public: //test
  384. static void test_code(int64_t x,int64_t y)
  385. {
  386. uint64_t h=ghash::encode(x,y);
  387. auto t=ghash::decode(h);
  388. 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));
  389. }
  390. static void test()
  391. {
  392. for(int i=0;i<10;i++)
  393. {
  394. test_code((4<<i)-1,(4<<i)-1);
  395. test_code((4<<i)-1,(4<<i));
  396. test_code((4<<i)-1,(4<<i)-1);
  397. test_code((4<<i),(4<<i)-1);
  398. }
  399. }
  400. private:
  401. static unsigned encode_(unsigned short x, unsigned short y)
  402. {
  403. const unsigned S=0x8000;
  404. unsigned r=0;
  405. for(int i=0;i<16;i++)
  406. {
  407. r<<=2;
  408. if(x&S)
  409. {
  410. r|=(y&S)?3:2;
  411. }
  412. else
  413. {
  414. if(y&S) r|=1;
  415. }
  416. x<<=1;
  417. y<<=1;
  418. }
  419. return r;
  420. }
  421. };
  422. #endif
  423. struct geolist
  424. {
  425. std::unordered_map<uint64_t,int> geo2path;
  426. public:
  427. static uint64_t encode(double x0,double y0)
  428. {
  429. uint64_t x=(uint32_t)(x0*10);
  430. uint64_t y=(uint32_t)(y0*10);
  431. return (x<<32)|y;
  432. }
  433. int find(double x,double y)const
  434. {
  435. uint64_t h=encode(x,y);
  436. auto it=geo2path.find(h);
  437. if(it==geo2path.end())
  438. return -1;
  439. return it->second;
  440. }
  441. void add(double x,double y,int id)
  442. {
  443. uint64_t h=encode(x,y);
  444. geo2path.insert(std::make_pair(h,id));
  445. }
  446. int size()
  447. {
  448. return geo2path.size();
  449. }
  450. void print()
  451. {
  452. for(auto it=geo2path.begin();it!=geo2path.end();++it)
  453. {
  454. //std::cout<<it->first<<"\n";
  455. printf("%ld\n",it->first);
  456. }
  457. }
  458. geolist()
  459. {
  460. }
  461. ~geolist()
  462. {
  463. }
  464. };
  465. struct graph
  466. {
  467. vertex_list v;
  468. std::vector<std::vector<int>> d; //索引为起始顶点,数组内容为目标点
  469. std::vector<std::array<int,2>> l; //bast_path list
  470. geolist geo;
  471. graph()
  472. {}
  473. void init(const vertex_list&v_,const std::vector<base_path>&bp)
  474. {
  475. for(auto&p:bp)
  476. {
  477. int from=v.add(v_[p[0]]);
  478. int to=v.add(v_[p[1]]);
  479. l.push_back({from,to});
  480. int id=l.size()-1;
  481. line_v lv(v[from],v[to]);
  482. log_info("graph::init() site:%d line:%s\n",p.sid, lv.to_string().c_str());
  483. double cos=lv.cos_k();
  484. double sin=lv.sin_k();
  485. double x,y;
  486. for(double r=0,len=lv.length();r<len+0.1;r+=0.05)
  487. {
  488. x=lv[0].x+r*cos;
  489. y=lv[0].y+r*sin;
  490. // printf("x=%lf,y=%lf\n",x,y);
  491. for(int i=-1;i<=1;i++)
  492. for(int j=-1;j<=1;j++)
  493. {
  494. geo.add(x+i/10.,y+j/10.,id);
  495. }
  496. }
  497. add(from,to);
  498. }
  499. }
  500. std::vector<line_v> find_possible_path(const point&from,double dist) const
  501. {
  502. std::vector<line_v> ret;
  503. int start_pt_id=v.find(from,dist);
  504. if(start_pt_id==-1)
  505. return std::move(ret);
  506. point start_pt=v[start_pt_id];
  507. for(uint32_t i=0;i<d[start_pt_id].size();i++)
  508. {
  509. ret.push_back(line_v(start_pt,v[d[start_pt_id][i]]));
  510. }
  511. return std::move(ret);
  512. }
  513. void add(int from,int to)
  514. {
  515. if((int)d.size()<=from) d.resize(from+1);
  516. if((int)d.size()<=to) d.resize(to+1);
  517. d[from].push_back(to);
  518. d[to].push_back(from);
  519. }
  520. bool find(std::vector<int>&ret,int from,std::array<int,2>&to,std::set<int>&ex,int level)
  521. {
  522. if(level--<0)
  523. return false;
  524. ret.push_back(from);
  525. for(int p:d[from]) //在当前路径
  526. {
  527. if(ex.find(p)!=ex.end()) //找过
  528. continue;
  529. if(p==to[0]||p==to[1])//找到
  530. return true;
  531. // {
  532. // ret.push_back(p);
  533. // return true;
  534. // }
  535. }
  536. ex.insert(from);
  537. for(int p:d[from])//在下级路径
  538. {
  539. if(ex.find(p)!=ex.end())//找过
  540. continue;
  541. if(find(ret,p,to,ex,level)) //找到
  542. {
  543. return true;
  544. }
  545. }
  546. ret.pop_back();
  547. return false;
  548. }
  549. bool is_at_path(const point&pt)const
  550. {
  551. return geo.find(pt.x,pt.y)>=0;
  552. }
  553. struct item
  554. {
  555. item(int _vid=-1, int _g=0,int _h=0,std::shared_ptr<item> _parent=nullptr)
  556. :vid(_vid)
  557. ,g(_g)
  558. ,h(_h)
  559. ,parent(_parent)
  560. {
  561. }
  562. int vid=-1,g=0,h=0;
  563. std::shared_ptr<item> parent;
  564. int cost()const {return g+h;}
  565. bool operator< (const item&i) const
  566. {
  567. return vid<i.vid;
  568. }
  569. };
  570. std::vector<point> find2(const point&from,const point&to)
  571. {
  572. int f=geo.find(from.x,from.y);
  573. int t=geo.find(to.x,to.y);
  574. if(f<0 || t<0 || f==t)
  575. return {};
  576. std::set<std::shared_ptr<item>> open_set;
  577. std::set<int> close_set;
  578. open_set.insert(std::make_shared<item>(l[f][0],from.dist(v[l[f][0]]),to.dist(v[l[f][0]])));
  579. open_set.insert(std::make_shared<item>(l[f][1],from.dist(v[l[f][1]]),to.dist(v[l[f][1]])));
  580. std::shared_ptr<item> cur;
  581. while(open_set.size()>0)
  582. {
  583. cur=*open_set.begin();
  584. for(auto&i:open_set)
  585. {
  586. if(i->cost()<cur->cost())
  587. cur=i;
  588. }
  589. open_set.erase(cur);
  590. close_set.insert(cur->vid);
  591. if(cur->vid==l[t][0]||cur->vid==l[t][1])
  592. break;
  593. for(auto vid:d[cur->vid])
  594. {
  595. if(close_set.find(vid)!=close_set.end())
  596. continue;
  597. open_set.insert(std::make_shared<item>(vid,cur->g+v[cur->vid].dist(v[vid]),to.dist(v[vid]),cur));
  598. }
  599. }
  600. if(open_set.size()==0)
  601. {
  602. return {};
  603. }
  604. std::vector<point> rc;
  605. while(cur.get())
  606. {
  607. rc.push_back(v[cur->vid]);
  608. cur=cur->parent;
  609. }
  610. std::reverse(rc.begin(),rc.end());
  611. rc.push_back(to);
  612. auto bp=from;
  613. auto it1=rc.begin();
  614. for(auto it2=it1+1;it1!=rc.end() && it2!=rc.end();it2++)
  615. {
  616. if(line(bp,*it1).dist(*it2)<0.1)
  617. {
  618. *it1=*it2;
  619. }
  620. else
  621. {
  622. bp=*it1++;
  623. *it1=*it2;
  624. }
  625. }
  626. rc.erase(it1,rc.end());
  627. if(rc.size() && from.dist(*rc.begin())<0.1)
  628. {
  629. rc.erase(rc.begin());
  630. }
  631. return std::move(rc);
  632. }
  633. std::vector<point> find(const point&from,const point&to)
  634. {
  635. int f=geo.find(from.x,from.y);
  636. int t=geo.find(to.x,to.y);
  637. if(f<0 || t<0 || f==t)
  638. return {};
  639. #if 0
  640. std::vector<point> rc[2];
  641. for(int i=0;i<2;i++)
  642. {
  643. std::vector<int> ret;
  644. std::set<int> ex({l[f][1-i]});
  645. if(find(ret,l[f][i],l[t],ex,2))
  646. {
  647. for(auto id:ret)
  648. rc[i].push_back(v[id]);
  649. }
  650. }
  651. if(!rc[0].empty() && (rc[1].empty() || rc[0].size() < rc[1].size()))
  652. return std::move(rc[0]);
  653. else if(!rc[1].empty())
  654. return std::move(rc[1]);
  655. #endif
  656. std::vector<int> ret;
  657. int size=v.size();
  658. std::vector<double> mat(size+1,-1);
  659. std::vector<double> trace(size+1,-1);
  660. std::vector<bool> val(size+2,false);
  661. mat[l[t][0]]=to.dist(v[l[t][0]]);
  662. mat[l[t][1]]=to.dist(v[l[t][1]]);
  663. int f0=l[f][0],f1=l[f][1];
  664. for(int i=0;i<2;i++)
  665. {
  666. if(mat[f0]!=-1 && mat[f1]!=-1)
  667. break;
  668. for(int j=0;j<size;j++)
  669. if(mat[j]!=-1)val[j]=true;
  670. for(int j=0;j<size;j++)
  671. {
  672. if(val[j]==false)continue;
  673. for(unsigned int k=0;k<d[j].size();k++)
  674. {
  675. double dist=v[j].dist(v[d[j][k]]);
  676. if(mat[d[j][k]]==-1 || mat[j]+dist < mat[d[j][k]])
  677. {
  678. mat[d[j][k]] = mat[j]+dist;
  679. trace[d[j][k]] = j;
  680. }
  681. }
  682. }
  683. }
  684. mat[f0]=mat[f0]+from.dist(v[l[f][0]]);
  685. mat[f1]=mat[f1]+from.dist(v[l[f][1]]);
  686. std::vector<point> rc;
  687. if(mat[f0]!=-1 && (mat[f1]==-1 || mat[f0] < mat[f1]))
  688. {
  689. int temp=f0;
  690. while(temp!=-1)
  691. {
  692. rc.push_back(v[temp]);
  693. temp=trace[temp];
  694. }
  695. return std::move(rc);
  696. }
  697. else if(mat[f1]!=-1)
  698. {
  699. int temp=f1;
  700. while(temp!=-1)
  701. {
  702. rc.push_back(v[temp]);
  703. temp=trace[temp];
  704. }
  705. return std::move(rc);
  706. }
  707. return {};
  708. }
  709. };
  710. safe_shared_ptr<graph> g_graph(std::make_shared<graph>());
  711. }//namespace
  712. void card_path::init()
  713. {
  714. std::shared_ptr<graph> local_graph=std::make_shared<graph>();
  715. handle_path hp;
  716. sit_list::instance()->accept(hp);
  717. std::vector<base_path> opath=init_path(hp.ret,hp.v);
  718. local_graph->init(hp.v,opath);
  719. g_graph.set(local_graph);
  720. }
  721. card_path&card_path::inst()
  722. {
  723. static card_path path;
  724. return path;
  725. }
  726. std::vector<point> card_path::find_path(const point&from,const point&to)const
  727. {
  728. return g_graph.get()->find2(from,to);
  729. }
  730. bool card_path::is_at_path(const point&pt)const
  731. {
  732. return g_graph.get()->is_at_path(pt);
  733. }
  734. std::vector<line_v> card_path::find_possible_path(const point&from,double dist) const
  735. {
  736. return std::move(g_graph.get()->find_possible_path(from,dist));
  737. }
  738. #ifdef __DEBUG__
  739. void test_at_path(card_path&cp,const point&pt)
  740. {
  741. if(cp.is_at_path(pt))
  742. log_info("test (%.3lf,%.3lf) is-at-path:true\n",pt.x,pt.y);
  743. else
  744. log_info("test (%.3lf,%.3lf) is-at-path:false\n",pt.x,pt.y);
  745. }
  746. void test_find_path(card_path&cp,const point&p1,const point&p2)
  747. {
  748. printf("\nfind-path: from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
  749. std::vector<point> rc=cp.find_path(p1,p2);
  750. for(uint32_t i=0;i<rc.size();i++)
  751. log_info("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
  752. }
  753. void test_find_poss_path(const card_path&cp ,const point&from)
  754. {
  755. log_info("\nfind-poss_path: from=(%.3lf,%.3lf)\n",from.x,from.y);
  756. std::vector<line_v> rc=cp.find_possible_path(from,3);
  757. for(uint32_t i=0;i<rc.size();i++)
  758. {
  759. 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);
  760. }
  761. }
  762. int main()
  763. {
  764. // std::unique_ptr<sit_list> sites(new sit_list());
  765. //sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  766. sit_list::instance()->load("1.txt","2.txt");
  767. card_path cp;
  768. cp.init();
  769. #if 0
  770. {
  771. test_at_path(cp,point(4773.104654,-104.0887));
  772. test_at_path(cp,point(4727,-74.8));
  773. test_at_path(cp,point(4727,-75.0));
  774. test_at_path(cp,point(4727,-75.2));
  775. test_at_path(cp,point(4727,-75.8));
  776. test_at_path(cp,point(4727,-90));
  777. test_at_path(cp,point(4727,-89.6));
  778. test_at_path(cp,point(4727,-89.8));
  779. test_at_path(cp,point(4727,-90));
  780. test_at_path(cp,point(4727,-90.2));
  781. test_at_path(cp,point(4727,-90.4));
  782. }
  783. #endif
  784. //test_at_path(cp,point(4726.90,376.85));
  785. test_find_path(cp,point(-227.6,174.9),point(-201.3,175.5));
  786. test_find_path(cp,point(-227.625219,174.945670),point(-170.085722,176.101574));
  787. test_find_path(cp,point(-393.1944,262.5324),point(-366.0754,274.1253));
  788. //test_find_path(cp,point(4637.000000,-75),point(4727,135));
  789. #if 1
  790. {
  791. test_find_path(cp,point(4637.000000,-75),point(4727,135));
  792. test_find_path(cp,point(4637.000000,-75),point(4727,120));
  793. test_find_path(cp,point(4700,-75),point(4727,120));
  794. test_find_path(cp,point(4725,-75),point(4727,135));
  795. test_find_path(cp,point(4727,-89.7),point(6144.3,-523));
  796. }
  797. #endif
  798. #if 0
  799. {
  800. test_find_poss_path(cp, point(4727,-89));
  801. test_find_poss_path(cp, point(4727,-90));
  802. test_find_poss_path(cp, point(4724,-75));
  803. test_find_poss_path(cp, point(4725,-75));
  804. test_find_poss_path(cp, point(4726,-75));
  805. test_find_poss_path(cp, point(4727,-75));
  806. test_find_poss_path(cp, point(4728,-75));
  807. test_find_poss_path(cp, point(4727,-7));
  808. test_find_poss_path(cp, point(4726.6,33));
  809. }
  810. #endif
  811. return 0;
  812. }
  813. #endif