card_path.cpp 19 KB

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