card_path.cpp 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. return std::move(rc);
  719. }
  720. std::vector<point> find(const point&from,const point&to)
  721. {
  722. int f=geo.find(from.x,from.y);
  723. int t=geo.find(to.x,to.y);
  724. if(f<0 || t<0 || f==t)
  725. return {};
  726. #if 0
  727. std::vector<point> rc[2];
  728. for(int i=0;i<2;i++)
  729. {
  730. std::vector<int> ret;
  731. std::set<int> ex({l[f][1-i]});
  732. if(find(ret,l[f][i],l[t],ex,2))
  733. {
  734. for(auto id:ret)
  735. rc[i].push_back(v[id]);
  736. }
  737. }
  738. if(!rc[0].empty() && (rc[1].empty() || rc[0].size() < rc[1].size()))
  739. return std::move(rc[0]);
  740. else if(!rc[1].empty())
  741. return std::move(rc[1]);
  742. #endif
  743. std::vector<int> ret;
  744. int size=v.size();
  745. std::vector<double> mat(size+1,-1);
  746. std::vector<double> trace(size+1,-1);
  747. std::vector<bool> val(size+2,false);
  748. mat[l[t][0]]=to.dist(v[l[t][0]]);
  749. mat[l[t][1]]=to.dist(v[l[t][1]]);
  750. int f0=l[f][0],f1=l[f][1];
  751. for(int i=0;i<2;i++)
  752. {
  753. if(mat[f0]!=-1 && mat[f1]!=-1)
  754. break;
  755. for(int j=0;j<size;j++)
  756. if(mat[j]!=-1)val[j]=true;
  757. for(int j=0;j<size;j++)
  758. {
  759. if(val[j]==false)continue;
  760. for(unsigned int k=0;k<d[j].size();k++)
  761. {
  762. double dist=v[j].dist(v[d[j][k]]);
  763. if(mat[d[j][k]]==-1 || mat[j]+dist < mat[d[j][k]])
  764. {
  765. mat[d[j][k]] = mat[j]+dist;
  766. trace[d[j][k]] = j;
  767. }
  768. }
  769. }
  770. }
  771. mat[f0]=mat[f0]+from.dist(v[l[f][0]]);
  772. mat[f1]=mat[f1]+from.dist(v[l[f][1]]);
  773. std::vector<point> rc;
  774. if(mat[f0]!=-1 && (mat[f1]==-1 || mat[f0] < mat[f1]))
  775. {
  776. int temp=f0;
  777. while(temp!=-1)
  778. {
  779. rc.push_back(v[temp]);
  780. temp=trace[temp];
  781. }
  782. return std::move(rc);
  783. }
  784. else if(mat[f1]!=-1)
  785. {
  786. int temp=f1;
  787. while(temp!=-1)
  788. {
  789. rc.push_back(v[temp]);
  790. temp=trace[temp];
  791. }
  792. return std::move(rc);
  793. }
  794. return {};
  795. }
  796. };
  797. safe_shared_ptr<graph> g_graph(std::make_shared<graph>());
  798. }//namespace
  799. void card_path::init()
  800. {
  801. std::shared_ptr<graph> local_graph=std::make_shared<graph>();
  802. handle_path hp;
  803. sit_list::instance()->accept(hp);
  804. std::vector<base_path> opath=init_path(hp.ret,hp.v);
  805. local_graph->init(hp.v,opath);
  806. g_graph.set(local_graph);
  807. }
  808. card_path&card_path::inst()
  809. {
  810. static card_path path;
  811. return path;
  812. }
  813. std::vector<point> card_path::find_path(const point&from,const point&to)const
  814. {
  815. return g_graph.get()->find2(from,to);
  816. }
  817. bool card_path::is_at_path(const point&pt)const
  818. {
  819. return g_graph.get()->is_at_path(pt);
  820. }
  821. std::vector<line_v> card_path::find_possible_path(const point&from,double dist) const
  822. {
  823. return std::move(g_graph.get()->find_possible_path(from,dist));
  824. }
  825. #ifdef __DEBUG__
  826. void test_at_path(card_path&cp,const point&pt)
  827. {
  828. if(cp.is_at_path(pt))
  829. log_info("test (%.3lf,%.3lf) is-at-path:true\n",pt.x,pt.y);
  830. else
  831. log_info("test (%.3lf,%.3lf) is-at-path:false\n",pt.x,pt.y);
  832. }
  833. void test_find_path(card_path&cp,const point&p1,const point&p2)
  834. {
  835. printf("\nfind-path: from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
  836. std::vector<point> rc=cp.find_path(p1,p2);
  837. for(uint32_t i=0;i<rc.size();i++)
  838. log_info("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
  839. }
  840. void test_find_poss_path(const card_path&cp ,const point&from)
  841. {
  842. log_info("\nfind-poss_path: from=(%.3lf,%.3lf)\n",from.x,from.y);
  843. std::vector<line_v> rc=cp.find_possible_path(from,3);
  844. for(uint32_t i=0;i<rc.size();i++)
  845. {
  846. 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);
  847. }
  848. }
  849. int main()
  850. {
  851. // std::unique_ptr<sit_list> sites(new sit_list());
  852. //sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  853. sit_list::instance()->load("1.txt","2.txt");
  854. card_path cp;
  855. cp.init();
  856. #if 0
  857. {
  858. test_at_path(cp,point(4773.104654,-104.0887));
  859. test_at_path(cp,point(4727,-74.8));
  860. test_at_path(cp,point(4727,-75.0));
  861. test_at_path(cp,point(4727,-75.2));
  862. test_at_path(cp,point(4727,-75.8));
  863. test_at_path(cp,point(4727,-90));
  864. test_at_path(cp,point(4727,-89.6));
  865. test_at_path(cp,point(4727,-89.8));
  866. test_at_path(cp,point(4727,-90));
  867. test_at_path(cp,point(4727,-90.2));
  868. test_at_path(cp,point(4727,-90.4));
  869. }
  870. #endif
  871. //test_at_path(cp,point(4726.90,376.85));
  872. test_find_path(cp,point(-227.6,174.9),point(-201.3,175.5));
  873. test_find_path(cp,point(-227.625219,174.945670),point(-170.085722,176.101574));
  874. test_find_path(cp,point(-393.1944,262.5324),point(-366.0754,274.1253));
  875. //test_find_path(cp,point(4637.000000,-75),point(4727,135));
  876. #if 1
  877. {
  878. test_find_path(cp,point(4637.000000,-75),point(4727,135));
  879. test_find_path(cp,point(4637.000000,-75),point(4727,120));
  880. test_find_path(cp,point(4700,-75),point(4727,120));
  881. test_find_path(cp,point(4725,-75),point(4727,135));
  882. test_find_path(cp,point(4727,-89.7),point(6144.3,-523));
  883. }
  884. #endif
  885. #if 0
  886. {
  887. test_find_poss_path(cp, point(4727,-89));
  888. test_find_poss_path(cp, point(4727,-90));
  889. test_find_poss_path(cp, point(4724,-75));
  890. test_find_poss_path(cp, point(4725,-75));
  891. test_find_poss_path(cp, point(4726,-75));
  892. test_find_poss_path(cp, point(4727,-75));
  893. test_find_poss_path(cp, point(4728,-75));
  894. test_find_poss_path(cp, point(4727,-7));
  895. test_find_poss_path(cp, point(4726.6,33));
  896. }
  897. #endif
  898. return 0;
  899. }
  900. #endif