1
0

test.cpp 6.1 KB

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