card_path.cpp 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. std::vector<point> rc;
  657. while(cur.get())
  658. {
  659. rc.push_back(v[cur->vid]);
  660. cur=cur->parent;
  661. }
  662. return std::move(rc);
  663. }
  664. std::vector<point> find(const point&from,const point&to)
  665. {
  666. int f=geo.find(from.x,from.y);
  667. int t=geo.find(to.x,to.y);
  668. if(f<0 || t<0 || f==t)
  669. return {};
  670. #if 0
  671. std::vector<point> rc[2];
  672. for(int i=0;i<2;i++)
  673. {
  674. std::vector<int> ret;
  675. std::set<int> ex({l[f][1-i]});
  676. if(find(ret,l[f][i],l[t],ex,2))
  677. {
  678. for(auto id:ret)
  679. rc[i].push_back(v[id]);
  680. }
  681. }
  682. if(!rc[0].empty() && (rc[1].empty() || rc[0].size() < rc[1].size()))
  683. return std::move(rc[0]);
  684. else if(!rc[1].empty())
  685. return std::move(rc[1]);
  686. #endif
  687. std::vector<int> ret;
  688. int size=v.size();
  689. std::vector<double> mat(size+1,-1);
  690. std::vector<double> trace(size+1,-1);
  691. std::vector<bool> val(size+2,false);
  692. mat[l[t][0]]=to.dist(v[l[t][0]]);
  693. mat[l[t][1]]=to.dist(v[l[t][1]]);
  694. int f0=l[f][0],f1=l[f][1];
  695. for(int i=0;i<2;i++)
  696. {
  697. if(mat[f0]!=-1 && mat[f1]!=-1)
  698. break;
  699. for(int j=0;j<size;j++)
  700. if(mat[j]!=-1)val[j]=true;
  701. for(int j=0;j<size;j++)
  702. {
  703. if(val[j]==false)continue;
  704. for(unsigned int k=0;k<d[j].size();k++)
  705. {
  706. double dist=v[j].dist(v[d[j][k]]);
  707. if(mat[d[j][k]]==-1 || mat[j]+dist < mat[d[j][k]])
  708. {
  709. mat[d[j][k]] = mat[j]+dist;
  710. trace[d[j][k]] = j;
  711. }
  712. }
  713. }
  714. }
  715. mat[f0]=mat[f0]+from.dist(v[l[f][0]]);
  716. mat[f1]=mat[f1]+from.dist(v[l[f][1]]);
  717. std::vector<point> rc;
  718. if(mat[f0]!=-1 && (mat[f1]==-1 || mat[f0] < mat[f1]))
  719. {
  720. int temp=f0;
  721. while(temp!=-1)
  722. {
  723. rc.push_back(v[temp]);
  724. temp=trace[temp];
  725. }
  726. return std::move(rc);
  727. }
  728. else if(mat[f1]!=-1)
  729. {
  730. int temp=f1;
  731. while(temp!=-1)
  732. {
  733. rc.push_back(v[temp]);
  734. temp=trace[temp];
  735. }
  736. return std::move(rc);
  737. }
  738. return {};
  739. }
  740. };
  741. safe_shared_ptr<graph> g_graph(std::make_shared<graph>());
  742. }//namespace
  743. void card_path::init()
  744. {
  745. std::shared_ptr<graph> local_graph=std::make_shared<graph>();
  746. handle_path hp;
  747. sit_list::instance()->accept(hp);
  748. std::vector<base_path> opath=init_path(hp.ret,hp.v);
  749. local_graph->init(hp.v,opath);
  750. g_graph.set(local_graph);
  751. }
  752. card_path&card_path::inst()
  753. {
  754. static card_path path;
  755. return path;
  756. }
  757. std::vector<point> card_path::find_path(const point&from,const point&to)const
  758. {
  759. return g_graph.get()->find2(from,to);
  760. }
  761. bool card_path::is_at_path(const point&pt)const
  762. {
  763. return g_graph.get()->is_at_path(pt);
  764. }
  765. std::vector<line_v> card_path::find_possible_path(const point&from,double dist) const
  766. {
  767. return std::move(g_graph.get()->find_possible_path(from,dist));
  768. }
  769. #ifdef __DEBUG__
  770. void test_at_path(card_path&cp,const point&pt)
  771. {
  772. if(cp.is_at_path(pt))
  773. log_info("test (%.3lf,%.3lf) is-at-path:true\n",pt.x,pt.y);
  774. else
  775. log_info("test (%.3lf,%.3lf) is-at-path:false\n",pt.x,pt.y);
  776. }
  777. void test_find_path(card_path&cp,const point&p1,const point&p2)
  778. {
  779. printf("\nfind-path: from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
  780. std::vector<point> rc=cp.find_path(p1,p2);
  781. for(uint32_t i=0;i<rc.size();i++)
  782. log_info("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
  783. }
  784. void test_find_poss_path(const card_path&cp ,const point&from)
  785. {
  786. log_info("\nfind-poss_path: from=(%.3lf,%.3lf)\n",from.x,from.y);
  787. std::vector<line_v> rc=cp.find_possible_path(from,3);
  788. for(uint32_t i=0;i<rc.size();i++)
  789. {
  790. 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);
  791. }
  792. }
  793. int main()
  794. {
  795. // std::unique_ptr<sit_list> sites(new sit_list());
  796. //sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  797. sit_list::instance()->load("1.txt","2.txt");
  798. card_path cp;
  799. cp.init();
  800. #if 0
  801. {
  802. test_at_path(cp,point(4773.104654,-104.0887));
  803. test_at_path(cp,point(4727,-74.8));
  804. test_at_path(cp,point(4727,-75.0));
  805. test_at_path(cp,point(4727,-75.2));
  806. test_at_path(cp,point(4727,-75.8));
  807. test_at_path(cp,point(4727,-90));
  808. test_at_path(cp,point(4727,-89.6));
  809. test_at_path(cp,point(4727,-89.8));
  810. test_at_path(cp,point(4727,-90));
  811. test_at_path(cp,point(4727,-90.2));
  812. test_at_path(cp,point(4727,-90.4));
  813. }
  814. #endif
  815. //test_at_path(cp,point(4726.90,376.85));
  816. test_find_path(cp,point(-227.6,174.9),point(-201.3,175.5));
  817. test_find_path(cp,point(-227.625219,174.945670),point(-170.085722,176.101574));
  818. test_find_path(cp,point(-393.1944,262.5324),point(-366.0754,274.1253));
  819. //test_find_path(cp,point(4637.000000,-75),point(4727,135));
  820. #if 1
  821. {
  822. test_find_path(cp,point(4637.000000,-75),point(4727,135));
  823. test_find_path(cp,point(4637.000000,-75),point(4727,120));
  824. test_find_path(cp,point(4700,-75),point(4727,120));
  825. test_find_path(cp,point(4725,-75),point(4727,135));
  826. test_find_path(cp,point(4727,-89.7),point(6144.3,-523));
  827. }
  828. #endif
  829. #if 0
  830. {
  831. test_find_poss_path(cp, point(4727,-89));
  832. test_find_poss_path(cp, point(4727,-90));
  833. test_find_poss_path(cp, point(4724,-75));
  834. test_find_poss_path(cp, point(4725,-75));
  835. test_find_poss_path(cp, point(4726,-75));
  836. test_find_poss_path(cp, point(4727,-75));
  837. test_find_poss_path(cp, point(4728,-75));
  838. test_find_poss_path(cp, point(4727,-7));
  839. test_find_poss_path(cp, point(4726.6,33));
  840. }
  841. #endif
  842. return 0;
  843. }
  844. #endif