1
0

card_path.cpp 17 KB

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