test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include <unistd.h>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <sstream>
  6. #include <vector>
  7. #include <line.h>
  8. #define log_error printf
  9. #define log_info printf
  10. struct path
  11. {
  12. std::array<line_v,2> m_line;
  13. path()
  14. {
  15. }
  16. std::string to_str() const
  17. {
  18. std::stringstream ss;
  19. for(int i=0;i<2;i++)
  20. {
  21. ss<<"line:" <<m_line[i].to_string()<<"slope:"<<m_line[i][0].z<< " cos:"<<m_line[i].cos()<<" sin:"<<m_line[i].sin()<<" | ";
  22. }
  23. return ss.str();
  24. }
  25. bool vaild() const
  26. {
  27. return !m_line[0].empty();
  28. }
  29. line_v & operator[](int i)
  30. {
  31. return m_line[i];
  32. }
  33. const line_v & operator[](int i) const
  34. {
  35. return m_line[i];
  36. }
  37. };
  38. struct tant:point
  39. {
  40. int m_id;
  41. void set_path(const std::vector<line_v>&v_line,std::vector<line_v>::const_iterator itm)
  42. {
  43. std::array<path,2> m_path;
  44. auto
  45. it=itm;
  46. for(int i=0;i<2 && it!=v_line.end();++it,++i)
  47. m_path[0][i]=*it;
  48. it=itm-1;
  49. for(int i=0;i<2 && it>=v_line.begin();--it,++i)
  50. {
  51. m_path[1][i]=*it;
  52. m_path[1][i].swap_point();
  53. }
  54. for(auto&p:m_path)
  55. {
  56. log_info("%s\n",p.to_str().c_str());
  57. }
  58. }
  59. void set_path2(const std::vector<line_v>&v_line_)
  60. {
  61. std::vector<line_v> vl(v_line_);
  62. vl.reserve(vl.size()+2);
  63. //找到距离天线最近的端点
  64. auto min_it=vl.begin();
  65. double dist=10;
  66. for(auto it=vl.begin();it!=vl.end();it++)
  67. {
  68. double d=it->as_line().dist(*this);
  69. if(d<dist)
  70. {
  71. dist=d;
  72. min_it=it;
  73. }
  74. }
  75. if(min_it==vl.end())
  76. {
  77. log_error("分站路径距离分站太远site_id=%d",m_id);
  78. return;
  79. }
  80. if(abs(min_it->v[0].dist(*this)-dist)<1)
  81. {
  82. set_path(vl,min_it);
  83. }
  84. else if(abs(min_it->v[1].dist(*this)-dist)<1)
  85. {
  86. set_path(vl,min_it+1);
  87. }
  88. else
  89. {
  90. point proj=min_it->projection(*this);
  91. vl.insert(min_it+1,line_v(proj,min_it->v[1]));
  92. min_it->set_point(1,proj);
  93. if(min_it->v[0].z)//slope ..555
  94. {
  95. double slope=min_it->v[0].z;
  96. double len_a=min_it->v[0].dist((min_it+1)->v[1]);
  97. double len_0=slope*min_it->length()/len_a;
  98. double len_1=slope-len_0;
  99. min_it->v[0].z=min_it->v[1].z=len_0;
  100. (min_it+1)->v[0].z=(min_it+1)->v[1].z=len_1;
  101. }
  102. set_path(vl,min_it+1);
  103. }
  104. }
  105. };
  106. void print_vl(const std::vector<line_v>&vl)
  107. {
  108. printf("-----------------------------------\n");
  109. for(auto&v:vl)
  110. {
  111. printf("%s\n",v.to_string().c_str());
  112. }
  113. }
  114. void set_path(const std::vector<line_v>&v_line,const std::vector<double>&slope)
  115. {
  116. if(v_line.empty())
  117. return;
  118. const auto&find_line=[](const point&pt,int first,std::vector<line_v>&vl){
  119. for(auto it=vl.begin();it!=vl.end();it++)
  120. {
  121. if(it->v[first].dist(pt)<1)
  122. return it;
  123. if(it->v[first?0:1].dist(pt)<1)
  124. {
  125. point p=it->v[0];
  126. it->v[0]=it->v[1];
  127. it->v[1]=p;
  128. return it;
  129. }
  130. }
  131. return vl.end();
  132. };
  133. //构造一个首尾相连的结构
  134. print_vl(v_line);
  135. std::vector<line_v> vl(v_line.begin()+1,v_line.end());
  136. std::vector<line_v> target(v_line.begin(),v_line.begin()+1);
  137. target[0][0].z=target[0][1].z=slope[0];
  138. for(int i=0,c=vl.size();i<c;i++)
  139. vl[i][0].z=vl[i][1].z=slope[i+1];
  140. for(;;)
  141. {
  142. auto it=find_line(target.back().v[1],0,vl);
  143. if(it==vl.end())
  144. break;
  145. target.insert(target.end(),it,it+1);
  146. vl.erase(it);
  147. }
  148. for(;;)
  149. {
  150. auto it=find_line(target.front().v[0],1,vl);
  151. if(it==vl.end())
  152. break;
  153. target.insert(target.begin(),it,it+1);
  154. vl.erase(it);
  155. }
  156. print_vl(target);
  157. tant ta;
  158. ta.set(100,100);
  159. ta.set_path2(target);
  160. }
  161. void test()
  162. {
  163. {
  164. std::vector<line_v> vl;
  165. vl.push_back(line_v(point(-10,0),point(0,0)));
  166. vl.push_back(line_v(point(0,0),point(100,100.1)));
  167. print_vl(vl);
  168. set_path(vl,{10,20});
  169. }
  170. {
  171. std::vector<line_v> vl;
  172. vl.push_back(line_v(point(200,200),point(100,100.1)));
  173. vl.push_back(line_v(point(200,200),point(200,220)));
  174. set_path(vl,{10,20});
  175. }
  176. {
  177. std::vector<line_v> vl;
  178. vl.push_back(line_v(point(-10,0),point(0,0)));
  179. vl.push_back(line_v(point(0,0),point(200,200)));
  180. set_path(vl,{10,20});
  181. }
  182. {
  183. std::vector<line_v> vl;
  184. vl.push_back(line_v(point(0,0),point(200,200)));
  185. set_path(vl,{10});
  186. }
  187. }
  188. int STATUS_HELP=0x80;
  189. int m_id=1;
  190. int help_bit=0;
  191. time_t help_last_time=0;
  192. void help(int st)
  193. {
  194. time_t now=time(0);
  195. if((help_bit & 1) && (st & STATUS_HELP))
  196. {
  197. // 1111111111
  198. // ^
  199. help_last_time=now;
  200. }
  201. else if((help_bit & 1) && (st & STATUS_HELP)==0)
  202. {
  203. // 11111111100000
  204. // ^
  205. help_bit<<=1;
  206. }
  207. else if((help_bit & 1)==0 && (st & STATUS_HELP))
  208. {
  209. // 00000000011111
  210. // ^
  211. if((help_bit&0x3)==2)
  212. {
  213. printf("handle_help,card_id:%d\n",m_id);
  214. }
  215. help_last_time=now;
  216. help_bit<<=1;
  217. help_bit|=1;
  218. }
  219. else
  220. {
  221. // 11111111100000
  222. // ^
  223. if(now-help_last_time>60)
  224. {
  225. help_bit=0;
  226. }
  227. }
  228. }
  229. void gen_help(int cnt,int bit=1)
  230. {
  231. for(int i=0;i<cnt;i++)
  232. {
  233. help(bit?STATUS_HELP:0);
  234. sleep(1);
  235. printf("%d",bit);
  236. fflush(stdout);
  237. }
  238. }
  239. int main()
  240. {
  241. help_bit=0;
  242. gen_help(10,0);
  243. gen_help(60,1);
  244. gen_help(10,0);
  245. gen_help(10,1);
  246. printf("\n");
  247. help_bit=0;
  248. gen_help(120,1);
  249. printf("\n");
  250. help_bit=0;
  251. gen_help(10,0);
  252. gen_help(60,1);
  253. gen_help(70,0);
  254. gen_help(10,1);
  255. gen_help(10,0);
  256. gen_help(60,1);
  257. printf("\n");
  258. help_bit=0;
  259. gen_help(120,0);
  260. printf("\n");
  261. return 0;
  262. }