card_path.cpp 20 KB

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