1
0

card_path.cpp 19 KB

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