card_path.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. printf("%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. void log_path(const std::vector<base_path>&path,const vertex_list&v_list)
  155. {
  156. printf("%s\n","----------------------------------------------------------------");
  157. for(int i=0,len=path.size();i<len;i++)
  158. {
  159. double c=path[i].arg(v_list)/PI*180;
  160. printf("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());
  161. }
  162. }
  163. struct handle_path :visitor<std::shared_ptr<site>>
  164. {
  165. std::vector<base_path> ret;
  166. vertex_list v;
  167. handle_path()
  168. {
  169. v.add(point(0,0),0,-1);
  170. }
  171. bool visit(std::shared_ptr<site> sit)
  172. {
  173. //auto&s=sites[i];
  174. const auto &s = *sit;
  175. if(!s.have_valid_path())
  176. return false;
  177. if(s.path(0).empty()||s.path(1).empty())
  178. return false;
  179. if(s[0].size()<2)
  180. return false;
  181. line_v l000 = s[0][0][0];
  182. line_v l010 = s[0][1][0];
  183. if(l000.is_same(l010))
  184. {
  185. printf("same....%d",s.m_id);
  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. log_path(ret,v);
  286. printf("++++++++++++++++++++++++++++++++++++++++++++");
  287. std::sort(ret.begin(),ret.end());
  288. log_path(ret,v);
  289. printf("++++++++++++++++++++++++++++++++++++++++++++");
  290. ret.erase(std::unique(ret.begin(),ret.end()),ret.end());
  291. log_path(ret,v);
  292. printf("+++++++++++++++++nnnnn+++++++++++++++++++++++++++");
  293. std::sort(ret.begin(),ret.end(),[&v](const base_path&p1,const base_path&p2){
  294. double arg=p1.arg(v)-p2.arg(v);
  295. if(fabs(arg)<0.1)
  296. {
  297. return v[p1[0]]<v[p2[0]];
  298. }
  299. return arg<0;
  300. });
  301. log_path(ret,v);
  302. for(int i=0,len=ret.size();i<len;i++)
  303. {
  304. line_v li=ret[i].as_line(v);
  305. for(int j=i+1;j<len;j++)
  306. {
  307. line_v lj=ret[j].as_line(v);
  308. if(!lj.is_same(li,2.5))
  309. continue;
  310. line_v ij=lj.projection(li);
  311. if(ij.empty())
  312. continue;
  313. point p0=point::min(v[ret[j][0]],v[ret[i][0]]);
  314. point p1=point::max(v[ret[j][1]],v[ret[i][1]]);
  315. ret[j][0]=v.add(p0,0);
  316. ret[j][1]=v.add(p1,0);
  317. ret[i][0]=0;
  318. ret[i][1]=0;
  319. break;
  320. }
  321. }
  322. ret.erase(std::remove_if(ret.begin(),ret.end(),[&v](const base_path&p){
  323. return p[0]==0||p[1]==0;
  324. }),ret.end());
  325. #ifdef __DEBUG__
  326. std::sort(ret.begin(),ret.end(),[&v](const base_path&p1,const base_path&p2){
  327. double arg=p1.arg(v)-p2.arg(v);
  328. if(fabs(arg)<0.1)
  329. {
  330. return v[p1[0]]<v[p2[0]];
  331. }
  332. return arg<0;
  333. });
  334. log_path(ret,v);
  335. #endif
  336. std::vector<std::vector<int>> p0(ret.size());
  337. std::vector<base_path> ret2;
  338. for(int i=0,len=ret.size();i<len;i++)
  339. {
  340. p0[i].push_back(ret[i][0]);
  341. for(int j=i+1;j<len;j++)
  342. {
  343. if(i==j) continue;
  344. point cr=ret[i].cross(v,ret[j]);
  345. if(cr.empty())
  346. continue;
  347. double arg=fabs(ret[i].as_line(v).arg()-ret[j].as_line(v).arg());
  348. while(arg>PI/2)
  349. arg-=PI/2;
  350. if(arg/PI*180<5)//相交小于5度,不切分已有路径
  351. continue;
  352. int id=v.add(cr,arg,v[ret[i][0]].sid0,v[ret[j][0]].sid0);
  353. p0[i].push_back(id);
  354. p0[j].push_back(id);
  355. }
  356. p0[i].push_back(ret[i][1]);
  357. std::sort(p0[i].begin(),p0[i].end(),[&v](int i0,int i1){
  358. return v[i0]<v[i1];
  359. });
  360. auto it=std::unique(p0[i].begin(),p0[i].end());
  361. p0[i].erase(it,p0[i].end());
  362. for(int j=1,cnt=p0[i].size();j<cnt;j++)
  363. ret2.push_back(base_path(p0[i][j-1],p0[i][j]));
  364. }
  365. ret2.erase(std::remove_if(ret2.begin(),ret2.end(),[&v](base_path&p){
  366. point&p0=v[p[0]];
  367. point&p1=v[p[1]];
  368. //return p0.dist(p1)<0.1 || p0.empty() || p1.empty();
  369. return p0.dist(p1)<0.1;
  370. }),ret2.end());
  371. std::sort(ret2.begin(),ret2.end());
  372. ret2.erase(std::unique(ret2.begin(),ret2.end()),ret2.end());
  373. /*
  374. ret.clear();
  375. for(int i=0,len=ret2.size();i<len;i++)
  376. {
  377. std::vector<line_v> tmp;
  378. tmp.push_back(ret2[i].as_line(v));
  379. for(int j=0;j<len;j++)
  380. {
  381. if(i==j) continue;
  382. if(ret[j].level(v)<ret[i].level(v))
  383. continue;
  384. line l1=ret2[j].as_line(v);
  385. }
  386. }
  387. std::sort(ret2.begin(),ret2.end(),[&v](base_path&p1,base_path&p2){
  388. return p1.arg(v)<p2.arg(v);
  389. });
  390. */
  391. #ifdef __DEBUG__
  392. std::sort(ret2.begin(),ret2.end(),[&v](const base_path&p1,const base_path&p2){
  393. double arg=p1.arg(v)-p2.arg(v);
  394. if(fabs(arg)<0.1)
  395. {
  396. return v[p1[0]]<v[p2[0]];
  397. }
  398. return arg<0;
  399. });
  400. log_path(ret2,v);
  401. #endif
  402. return std::move(ret2);
  403. }
  404. #if 0
  405. struct ghash
  406. {
  407. static std::tuple<int,int> decode(unsigned h)
  408. {
  409. const uint64_t S=0x8000000000000000;
  410. int x=0,y=0;
  411. for(int i=0;i<64;i++)
  412. {
  413. x<<=1;
  414. y<<=1;
  415. if(h&S)
  416. x|=1;
  417. h<<=1;
  418. if(h&S)
  419. y|=1;
  420. h<<=1;
  421. }
  422. return std::make_tuple(x-2147483648,y-2147483648);
  423. }
  424. static uint64_t encode(int64_t x, int64_t y)
  425. {
  426. return encode_(x+2147483648,y+2147483648);
  427. }
  428. public: //test
  429. static void test_code(int64_t x,int64_t y)
  430. {
  431. uint64_t h=ghash::encode(x,y);
  432. auto t=ghash::decode(h);
  433. 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));
  434. }
  435. static void test()
  436. {
  437. for(int i=0;i<10;i++)
  438. {
  439. test_code((4<<i)-1,(4<<i)-1);
  440. test_code((4<<i)-1,(4<<i));
  441. test_code((4<<i)-1,(4<<i)-1);
  442. test_code((4<<i),(4<<i)-1);
  443. }
  444. }
  445. private:
  446. static unsigned encode_(unsigned short x, unsigned short y)
  447. {
  448. const unsigned S=0x8000;
  449. unsigned r=0;
  450. for(int i=0;i<16;i++)
  451. {
  452. r<<=2;
  453. if(x&S)
  454. {
  455. r|=(y&S)?3:2;
  456. }
  457. else
  458. {
  459. if(y&S) r|=1;
  460. }
  461. x<<=1;
  462. y<<=1;
  463. }
  464. return r;
  465. }
  466. };
  467. #endif
  468. struct geolist
  469. {
  470. std::unordered_map<uint64_t,int> geo2path;
  471. public:
  472. static uint64_t encode(double x0,double y0)
  473. {
  474. uint64_t x=(uint32_t)(x0*10);
  475. uint64_t y=(uint32_t)(y0*10);
  476. return (x<<32)|y;
  477. }
  478. int find(double x,double y)const
  479. {
  480. uint64_t h=encode(x,y);
  481. auto it=geo2path.find(h);
  482. if(it==geo2path.end())
  483. return -1;
  484. return it->second;
  485. }
  486. void add(double x,double y,int id)
  487. {
  488. uint64_t h=encode(x,y);
  489. geo2path.insert(std::make_pair(h,id));
  490. }
  491. int size()
  492. {
  493. return geo2path.size();
  494. }
  495. void print()
  496. {
  497. for(auto it=geo2path.begin();it!=geo2path.end();++it)
  498. {
  499. //std::cout<<it->first<<"\n";
  500. printf("%ld\n",it->first);
  501. }
  502. }
  503. geolist()
  504. {
  505. }
  506. ~geolist()
  507. {
  508. }
  509. };
  510. struct graph
  511. {
  512. vertex_list v;
  513. std::vector<std::vector<int>> d; //索引为起始顶点,数组内容为目标点
  514. std::vector<std::array<int,2>> l; //bast_path list
  515. geolist geo;
  516. graph()
  517. {}
  518. void init(const vertex_list&v_,const std::vector<base_path>&bp)
  519. {
  520. for(auto&p:bp)
  521. {
  522. int from=v.add(v_[p[0]]);
  523. int to=v.add(v_[p[1]]);
  524. l.push_back({from,to});
  525. int id=l.size()-1;
  526. line_v lv(v[from],v[to]);
  527. printf("line:%s\n",lv.to_string().c_str());
  528. double cos=lv.cos_k();
  529. double sin=lv.sin_k();
  530. double x,y;
  531. for(double r=0,len=lv.length();r<len+0.1;r+=0.05)
  532. {
  533. x=lv[0].x+r*cos;
  534. y=lv[0].y+r*sin;
  535. // printf("x=%lf,y=%lf\n",x,y);
  536. for(int i=-1;i<=1;i++)
  537. for(int j=-1;j<=1;j++)
  538. {
  539. geo.add(x+i/10.,y+j/10.,id);
  540. }
  541. }
  542. add(from,to);
  543. }
  544. }
  545. std::vector<line_v> find_possible_path(const point&from,double dist) const
  546. {
  547. std::vector<line_v> ret;
  548. int start_pt_id=v.find(from,dist);
  549. if(start_pt_id==-1)
  550. return std::move(ret);
  551. point start_pt=v[start_pt_id];
  552. for(uint32_t i=0;i<d[start_pt_id].size();i++)
  553. {
  554. ret.push_back(line_v(start_pt,v[d[start_pt_id][i]]));
  555. }
  556. return std::move(ret);
  557. }
  558. void add(int from,int to)
  559. {
  560. if((int)d.size()<=from) d.resize(from+1);
  561. if((int)d.size()<=to) d.resize(to+1);
  562. d[from].push_back(to);
  563. d[to].push_back(from);
  564. }
  565. bool find(std::vector<int>&ret,int from,std::array<int,2>&to,std::set<int>&ex,int level)
  566. {
  567. if(level--<0)
  568. return false;
  569. ret.push_back(from);
  570. for(int p:d[from]) //在当前路径
  571. {
  572. if(ex.find(p)!=ex.end()) //找过
  573. continue;
  574. if(p==to[0]||p==to[1])//找到
  575. return true;
  576. // {
  577. // ret.push_back(p);
  578. // return true;
  579. // }
  580. }
  581. ex.insert(from);
  582. for(int p:d[from])//在下级路径
  583. {
  584. if(ex.find(p)!=ex.end())//找过
  585. continue;
  586. if(find(ret,p,to,ex,level)) //找到
  587. {
  588. return true;
  589. }
  590. }
  591. ret.pop_back();
  592. return false;
  593. }
  594. bool is_at_path(const point&pt)const
  595. {
  596. return geo.find(pt.x,pt.y)>=0;
  597. }
  598. std::vector<point> find(const point&from,const point&to)
  599. {
  600. int f=geo.find(from.x,from.y);
  601. int t=geo.find(to.x,to.y);
  602. if(f<0 || t<0 || f==t)
  603. return {};
  604. #if 0
  605. std::vector<point> rc[2];
  606. for(int i=0;i<2;i++)
  607. {
  608. std::vector<int> ret;
  609. std::set<int> ex({l[f][1-i]});
  610. if(find(ret,l[f][i],l[t],ex,2))
  611. {
  612. for(auto id:ret)
  613. rc[i].push_back(v[id]);
  614. }
  615. }
  616. if(!rc[0].empty() && (rc[1].empty() || rc[0].size() < rc[1].size()))
  617. return std::move(rc[0]);
  618. else if(!rc[1].empty())
  619. return std::move(rc[1]);
  620. #endif
  621. std::vector<int> ret;
  622. int size=v.size();
  623. std::vector<double> mat(size+1,-1);
  624. std::vector<double> trace(size+1,-1);
  625. std::vector<bool> val(size+2,false);
  626. mat[l[t][0]]=to.dist(v[l[t][0]]);
  627. mat[l[t][1]]=to.dist(v[l[t][1]]);
  628. int f0=l[f][0],f1=l[f][1];
  629. for(int i=0;i<2;i++)
  630. {
  631. if(mat[f0]!=-1 && mat[f1]!=-1)
  632. break;
  633. for(int j=0;j<size;j++)
  634. if(mat[j]!=-1)val[j]=true;
  635. for(int j=0;j<size;j++)
  636. {
  637. if(val[j]==false)continue;
  638. for(unsigned int k=0;k<d[j].size();k++)
  639. {
  640. double dist=v[j].dist(v[d[j][k]]);
  641. if(mat[d[j][k]]==-1 || mat[j]+dist < mat[d[j][k]])
  642. {
  643. mat[d[j][k]] = mat[j]+dist;
  644. trace[d[j][k]] = j;
  645. }
  646. }
  647. }
  648. }
  649. mat[f0]=mat[f0]+from.dist(v[l[f][0]]);
  650. mat[f1]=mat[f1]+from.dist(v[l[f][1]]);
  651. std::vector<point> rc;
  652. if(mat[f0]!=-1 && (mat[f1]==-1 || mat[f0] < mat[f1]))
  653. {
  654. int temp=f0;
  655. while(temp!=-1)
  656. {
  657. rc.push_back(v[temp]);
  658. temp=trace[temp];
  659. }
  660. return std::move(rc);
  661. }
  662. else if(mat[f1]!=-1)
  663. {
  664. int temp=f1;
  665. while(temp!=-1)
  666. {
  667. rc.push_back(v[temp]);
  668. temp=trace[temp];
  669. }
  670. return std::move(rc);
  671. }
  672. return {};
  673. }
  674. };
  675. std::atomic<int> g_init_flag(0);
  676. graph g_graph;
  677. }//namespace
  678. void card_path::init()
  679. {
  680. //Ensure only ont thread can init path.
  681. int expect=0;
  682. if(g_init_flag.compare_exchange_strong(expect,1))
  683. {
  684. handle_path hp;
  685. //std::vector<base_path> opath=init_path(sites,v_list);
  686. sit_list::instance()->accept(hp);
  687. std::vector<base_path> opath=init_path(hp.ret,hp.v);
  688. g_graph.init(hp.v,opath);
  689. ++g_init_flag;
  690. }
  691. //if != 2 then wait here until init over.
  692. while(g_init_flag.load()!=2)
  693. {
  694. std::this_thread::sleep_for (std::chrono::seconds(1));
  695. }
  696. }
  697. card_path&card_path::inst()
  698. {
  699. static card_path path;
  700. return path;
  701. }
  702. std::vector<point> card_path::find_path(const point&from,const point&to)const
  703. {
  704. return g_graph.find(from,to);
  705. }
  706. bool card_path::is_at_path(const point&pt)const
  707. {
  708. return g_graph.is_at_path(pt);
  709. }
  710. std::vector<line_v> card_path::find_possible_path(const point&from,double dist) const
  711. {
  712. return std::move(g_graph.find_possible_path(from,dist));
  713. }
  714. #ifdef __DEBUG__
  715. void test_at_path(card_path&cp,const point&pt)
  716. {
  717. if(cp.is_at_path(pt))
  718. printf("test (%.3lf,%.3lf) is-at-path:true\n",pt.x,pt.y);
  719. else
  720. printf("test (%.3lf,%.3lf) is-at-path:false\n",pt.x,pt.y);
  721. }
  722. void test_find_path(card_path&cp,const point&p1,const point&p2)
  723. {
  724. printf("\nfind-path: from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
  725. std::vector<point> rc=cp.find_path(p1,p2);
  726. for(uint32_t i=0;i<rc.size();i++)
  727. printf("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
  728. }
  729. void test_find_poss_path(const card_path&cp ,const point&from)
  730. {
  731. printf("\nfind-poss_path: from=(%.3lf,%.3lf)\n",from.x,from.y);
  732. std::vector<line_v> rc=cp.find_possible_path(from,3);
  733. for(uint32_t i=0;i<rc.size();i++)
  734. {
  735. printf("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);
  736. }
  737. }
  738. int main()
  739. {
  740. // std::unique_ptr<sit_list> sites(new sit_list());
  741. sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  742. card_path cp;
  743. cp.init();
  744. #if 0
  745. {
  746. test_at_path(cp,point(4773.104654,-104.0887));
  747. test_at_path(cp,point(4727,-74.8));
  748. test_at_path(cp,point(4727,-75.0));
  749. test_at_path(cp,point(4727,-75.2));
  750. test_at_path(cp,point(4727,-75.8));
  751. test_at_path(cp,point(4727,-90));
  752. test_at_path(cp,point(4727,-89.6));
  753. test_at_path(cp,point(4727,-89.8));
  754. test_at_path(cp,point(4727,-90));
  755. test_at_path(cp,point(4727,-90.2));
  756. test_at_path(cp,point(4727,-90.4));
  757. }
  758. #endif
  759. //test_at_path(cp,point(4726.90,376.85));
  760. test_find_path(cp,point(-227.6,174.9),point(-201.3,175.5));
  761. test_find_path(cp,point(-227.625219,174.945670),point(-170.085722,176.101574));
  762. test_find_path(cp,point(-393.1944,262.5324),point(-366.0754,274.1253));
  763. //test_find_path(cp,point(4637.000000,-75),point(4727,135));
  764. #if 1
  765. {
  766. test_find_path(cp,point(4637.000000,-75),point(4727,135));
  767. test_find_path(cp,point(4637.000000,-75),point(4727,120));
  768. test_find_path(cp,point(4700,-75),point(4727,120));
  769. test_find_path(cp,point(4725,-75),point(4727,135));
  770. test_find_path(cp,point(4727,-89.7),point(6144.3,-523));
  771. }
  772. #endif
  773. #if 0
  774. {
  775. test_find_poss_path(cp, point(4727,-89));
  776. test_find_poss_path(cp, point(4727,-90));
  777. test_find_poss_path(cp, point(4724,-75));
  778. test_find_poss_path(cp, point(4725,-75));
  779. test_find_poss_path(cp, point(4726,-75));
  780. test_find_poss_path(cp, point(4727,-75));
  781. test_find_poss_path(cp, point(4728,-75));
  782. test_find_poss_path(cp, point(4727,-7));
  783. test_find_poss_path(cp, point(4726.6,33));
  784. }
  785. #endif
  786. return 0;
  787. }
  788. #endif