ant.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. 
  2. #ifndef _ANT_LIST_HPP_
  3. #define _ANT_LIST_HPP_
  4. #include <math.h>
  5. #include <array>
  6. #include <deque>
  7. #include <tuple>
  8. #include <memory>
  9. #include <algorithm>
  10. #include "line.h"
  11. #include <iostream>
  12. #include "point.h"
  13. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  14. struct ant:point
  15. {
  16. std::array<std::array<point,6>,2> m_path;//映射的天线坐标、路径坐标、夹角
  17. std::array<point,6>&operator[](int i)
  18. {
  19. return m_path[i];
  20. }
  21. const std::array<point,6>&operator[](int i)const
  22. {
  23. return m_path[i];
  24. }
  25. };
  26. struct site:point
  27. {
  28. site(int id=-1);
  29. mutable double m_height;
  30. int m_id;
  31. bool m_path_empty;
  32. std::array<ant,2> m_ant;
  33. std::array<line_v,2> m_line;
  34. mutable double m_ant_dist;
  35. mutable double m_ant_dist_sum_new;
  36. mutable int m_ant_dist_cnt_new;
  37. double m_scale;
  38. void count_ant_dist(double dist_tof1, double dist_tof2)const
  39. {
  40. if(dist_tof1<10 || dist_tof2<10)
  41. return;
  42. double dist = fabs(dist_tof1 - dist_tof2);
  43. if(dist>5)
  44. return;
  45. m_ant_dist_sum_new += dist;
  46. m_ant_dist_cnt_new++;
  47. if(m_ant_dist_cnt_new >= 2500)
  48. {
  49. m_ant_dist = m_ant_dist_sum_new / m_ant_dist_cnt_new;
  50. m_ant_dist_sum_new = 0;
  51. m_ant_dist_cnt_new = 0;
  52. }
  53. }
  54. /* mutable double m_test_dist=0;
  55. mutable int m_test_cnt=0;
  56. void test_dist(double d)const
  57. {
  58. if(fabs(m_ant_dist)<0.001)return;
  59. if(d>10)return;
  60. m_test_dist+=d;
  61. m_test_cnt++;
  62. }*/
  63. /*
  64. const point&operator[](int ant_id)const
  65. {
  66. return m_ant[ant_id];
  67. }
  68. */
  69. double ant_dist()const
  70. {
  71. return m_ant[0].dist(m_ant[1]);
  72. }
  73. void mapping(const point &p,point &tmp) const
  74. {
  75. point rp;
  76. double d,dmin=1e4;
  77. for(auto&line:m_line)
  78. {
  79. if(line.empty())
  80. continue;
  81. rp=line.line::projection(p);
  82. d=p.dist(rp);
  83. if(d<dmin)
  84. {
  85. dmin=d;
  86. tmp=rp;
  87. }
  88. }
  89. if(dmin>5) tmp.set(0,0);
  90. }
  91. void sort_path();
  92. bool is_path_empty()const
  93. {
  94. return m_path_empty;
  95. }
  96. bool have_valid_path()const
  97. {
  98. return m_id!=-1 && m_ant[0].dist(m_ant[1])>0.1 && (dist(path(0))>1 || dist(path(1))>1);
  99. }
  100. std::string to_string()const
  101. {
  102. char buf[128];
  103. int len=sprintf(buf,"id=%d, ant=[(%.2f,%.2f),(%.2f,%.2f)],path=[(%.2f,%.2f),(%.2f,%.2f)],%d"
  104. ,m_id
  105. ,m_ant[0].x,m_ant[0].y
  106. ,m_ant[1].x,m_ant[1].y
  107. ,m_ant[0].m_path[0][1].x,m_ant[0].m_path[0][1].y
  108. ,m_ant[0].m_path[1][1].x,m_ant[0].m_path[1][1].y
  109. ,check_k()
  110. );
  111. return std::move(std::string(buf,len));
  112. }
  113. const point&path(int i)const
  114. {
  115. return m_ant[0].m_path[i][1];
  116. }
  117. bool check_k()const
  118. {
  119. if(is_path_empty())
  120. return false;
  121. return eq(path(0).cos_k(*this),cos_k(path(1)),0.1);
  122. }
  123. //Cannot handle a non straight line.
  124. point intersection(const site&o)const
  125. {
  126. //The cover area is not a straight line, giving up
  127. if(!check_k() || !o.check_k())
  128. return point();
  129. //The same slope, directly back to the empty
  130. if(eq(cos_k(path(0)),o.cos_k(o.path(0)),0.0001))
  131. return point();
  132. auto abc1= get_abc(path(0));
  133. auto abc2=o.get_abc(o.path(0));
  134. point pt(
  135. (std::get<2>(abc1)*std::get<1>(abc2)-std::get<2>(abc2)*std::get<1>(abc1))
  136. /(std::get<0>(abc2)*std::get<1>(abc1)-std::get<0>(abc1)*std::get<1>(abc2)) ,
  137. (std::get<2>(abc1)*std::get<0>(abc2)-std::get<2>(abc2)*std::get<0>(abc1))
  138. /(std::get<1>(abc2)*std::get<0>(abc1)-std::get<1>(abc1)*std::get<0>(abc2))
  139. );
  140. // printf("cp=(%lf,%lf)\n",pt.x,pt.y);
  141. return pt;
  142. }
  143. double dist_to_site(const site&o)const
  144. {
  145. point pt=intersection(o);
  146. return dist(pt)+pt.dist(o);
  147. }
  148. double getSolPoint(const ant&a,int path,double dist) const
  149. {
  150. double real_dist = 0;
  151. double last_d = 0;
  152. double next_d = dist;
  153. std::for_each(a[path].begin()+3,a[path].end(),[&next_d,&dist,&real_dist,&last_d](const point & p){
  154. if(!p.empty() && next_d)
  155. {
  156. double dis = p.x-last_d;
  157. last_d = p.x;
  158. if(dist > p.x)
  159. {
  160. real_dist += dis*dis/(dis+p.y);
  161. next_d = dist-p.x;
  162. }
  163. else
  164. {
  165. real_dist += next_d*dis/(dis+p.y);
  166. next_d = 0;
  167. }
  168. }
  169. else if(next_d != dist && next_d)
  170. {
  171. real_dist += next_d;
  172. next_d = 0;
  173. }
  174. });
  175. return !real_dist?dist:real_dist;
  176. }
  177. void solving(point**o, int ant_id, double dist)const
  178. {
  179. point*r=*o;
  180. const ant&a=m_ant[ant_id];
  181. if(dist<50 && dist>0)
  182. {
  183. if(dist<m_height)
  184. {
  185. m_height=dist;
  186. dist=0;
  187. }
  188. else
  189. {
  190. dist=sqrt(dist*dist-m_height*m_height);
  191. }
  192. }
  193. double rd1 = getSolPoint(a,0,dist);
  194. point p1=point(a[0][0].x+rd1*a[0][2].x, a[0][0].y+rd1*a[0][2].y);
  195. double rd2 = getSolPoint(a,1,dist);
  196. point p2=point(a[1][0].x+rd2*a[1][2].x, a[1][0].y+rd2*a[1][2].y);
  197. if(fabs(m_ant_dist)>0.001)
  198. {
  199. double delta = (m_ant_dist - ant_dist())/2;
  200. if(p1.dist(*this)>p2.dist(*this))
  201. {
  202. rd1+=delta;
  203. rd2-=delta;
  204. }
  205. else
  206. {
  207. rd1-=delta;
  208. rd2+=delta;
  209. }
  210. point tmp1=point(a[0][0].x+rd1*a[0][2].x, a[0][0].y+rd1*a[0][2].y);
  211. point tmp2=point(a[1][0].x+rd2*a[1][2].x, a[1][0].y+rd2*a[1][2].y);
  212. point mapped1,mapped2;
  213. mapping(tmp1,mapped1);
  214. mapping(tmp2,mapped2);
  215. *r++=mapped1;
  216. *r++=mapped2;
  217. }
  218. else
  219. {
  220. point mapped1,mapped2;
  221. mapping(p1,mapped1);
  222. mapping(p2,mapped2);
  223. *r++=mapped1;
  224. *r++=mapped2;
  225. }
  226. *o=r;
  227. }
  228. };
  229. struct sit_list
  230. {
  231. std::array<site,2000> m_list;
  232. public:
  233. sit_list();
  234. void load(const char*ant_file,const char*path_file)
  235. {
  236. read_sit_list(ant_file);
  237. read_ant_path(path_file);
  238. }
  239. std::string to_string()const
  240. {
  241. std::string rt;
  242. rt.reserve(4096);
  243. for(auto s:m_list)
  244. {
  245. if(s.m_id==-1)
  246. continue;
  247. rt+=s.to_string();
  248. rt+="\n";
  249. }
  250. return std::move(rt);
  251. }
  252. void read_sit_list(const char*fname);
  253. void read_ant_path(const char*fname);
  254. void init_sit_list(int32_t readerid,int32_t antid, double ax, double ay,double scale);
  255. void init_sit_list();
  256. void init_ant_path(int32_t readerid,int32_t antid, double ax, double ay,std::vector<std::string>::iterator &b,std::vector<std::string>::iterator& e);
  257. void init_ant_path();
  258. const site& operator[](int id) const;
  259. };
  260. NAMESPACE_POINT_END(NAMESPACE_POINT)
  261. #endif