card_path.cppbak 18 KB

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