ant.h.bak 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #ifndef _ANT_LIST_HPP_
  2. #define _ANT_LIST_HPP_
  3. #include <math.h>
  4. #include <array>
  5. #include <deque>
  6. #include <tuple>
  7. #include <memory>
  8. #include <algorithm>
  9. #include "line.h"
  10. #include <iostream>
  11. #include "point.h"
  12. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  13. struct ant:point
  14. {
  15. std::array<std::array<point,6>,3> m_path;//映射的天线坐标、路径坐标、夹角
  16. std::array<point,6>&operator[](int i)
  17. {
  18. return m_path[i];
  19. }
  20. const std::array<point,6>&operator[](int i)const
  21. {
  22. return m_path[i];
  23. }
  24. };
  25. struct site:point
  26. {
  27. site(int id=-1);
  28. int m_id;
  29. bool m_path_empty;
  30. std::array<ant,2> m_ant;
  31. std::array<std::shared_ptr<line_v>,2> m_line;
  32. /*
  33. const point&operator[](int ant_id)const
  34. {
  35. return m_ant[ant_id];
  36. }
  37. */
  38. int line_n(int &n) const
  39. {
  40. int ret = 0;
  41. for(unsigned int i = 0 ; i < m_line.size();i++)
  42. {
  43. if(!m_line[i])
  44. {
  45. ret++;
  46. break;
  47. }
  48. else
  49. n = i;
  50. }
  51. return ret;
  52. }
  53. point &getlinePoint(const point &p) const
  54. {
  55. double d0 = m_line[0]->dist(p);
  56. double d1 = m_line[1]->dist(p);
  57. point p0 = m_line[0]->projection(p);
  58. point p1 = m_line[1]->projection(p);
  59. return d0==-1?p1:d1==-1?p0:d0>d1?p1:p0;
  60. }
  61. void mapping(const point &p,point &tmp) const
  62. {
  63. int n = 0;
  64. switch(line_n(n))
  65. {
  66. case 0:
  67. tmp = getlinePoint(p);
  68. break;
  69. case 1:
  70. tmp = m_line[n]->projection(p);
  71. break;
  72. case 2:
  73. std::cout << "++++++++++no path" <<m_id<<std::endl;
  74. break;
  75. default:
  76. std::cout << "++++++++++error path" <<m_id<<std::endl;
  77. break;
  78. }
  79. }
  80. void sort_path();
  81. bool is_path_empty()const
  82. {
  83. return m_path_empty;
  84. }
  85. bool have_valid_path()const
  86. {
  87. return m_ant[0].dist(m_ant[1])>0.1 && (dist(path(0))>1 || dist(path(1))>1);
  88. }
  89. std::string to_string()const
  90. {
  91. char buf[128];
  92. int len=sprintf(buf,"id=%d, ant=[(%.2f,%.2f),(%.2f,%.2f)],path=[(%.2f,%.2f),(%.2f,%.2f)],%d"
  93. ,m_id
  94. ,m_ant[0].x,m_ant[0].y
  95. ,m_ant[1].x,m_ant[1].y
  96. ,m_ant[0].m_path[0][1].x,m_ant[0].m_path[0][1].y
  97. ,m_ant[0].m_path[1][1].x,m_ant[0].m_path[1][1].y
  98. ,check_k()
  99. );
  100. return std::move(std::string(buf,len));
  101. }
  102. const point&path(int i)const
  103. {
  104. return m_ant[0].m_path[i][1];
  105. }
  106. bool check_k()const
  107. {
  108. if(is_path_empty())
  109. return false;
  110. return eq(path(0).cos_k(*this),cos_k(path(1)),0.1);
  111. }
  112. //Cannot handle a non straight line.
  113. point intersection(const site&o)const
  114. {
  115. //The cover area is not a straight line, giving up
  116. if(!check_k() || !o.check_k())
  117. return point();
  118. //The same slope, directly back to the empty
  119. if(eq(cos_k(path(0)),o.cos_k(o.path(0)),0.0001))
  120. return point();
  121. auto abc1= get_abc(path(0));
  122. auto abc2=o.get_abc(o.path(0));
  123. point pt(
  124. (std::get<2>(abc1)*std::get<1>(abc2)-std::get<2>(abc2)*std::get<1>(abc1))
  125. /(std::get<0>(abc2)*std::get<1>(abc1)-std::get<0>(abc1)*std::get<1>(abc2)) ,
  126. (std::get<2>(abc1)*std::get<0>(abc2)-std::get<2>(abc2)*std::get<0>(abc1))
  127. /(std::get<1>(abc2)*std::get<0>(abc1)-std::get<1>(abc1)*std::get<0>(abc2))
  128. );
  129. // printf("cp=(%lf,%lf)\n",pt.x,pt.y);
  130. return pt;
  131. }
  132. double dist_to_site(const site&o)const
  133. {
  134. point pt=intersection(o);
  135. return dist(pt)+pt.dist(o);
  136. }
  137. double getSolPoint(const ant&a,int path,double dist) const
  138. {
  139. double real_dist = 0;
  140. double last_d = 0;
  141. double next_d = dist;
  142. std::for_each(a[path].begin()+3,a[path].end(),[&next_d,&dist,&real_dist,&last_d](const point & p){
  143. if(!p.empty() && next_d)
  144. {
  145. double dis = p.x-last_d;
  146. last_d = p.x;
  147. if(dist > p.x)
  148. {
  149. real_dist += dis*dis/(dis+p.y);
  150. next_d = dist-p.x;
  151. }
  152. else
  153. {
  154. real_dist += next_d*dis/(dis+p.y);
  155. next_d = 0;
  156. }
  157. }
  158. else if(next_d != dist && next_d)
  159. {
  160. real_dist += next_d;
  161. next_d = 0;
  162. }
  163. });
  164. return !real_dist?dist:real_dist;
  165. }
  166. void solving(point**o, int ant_id, double dist)const
  167. {
  168. point*r=*o;
  169. const ant&a=m_ant[ant_id];
  170. {
  171. if(dist<25 && dist>1.5)
  172. {
  173. dist=sqrt(dist*dist-1.5*1.5);
  174. }
  175. }
  176. double rd = getSolPoint(a,0,dist);
  177. *r++=point(a[0][0].x+rd*a[0][2].x, a[0][0].y+rd*a[0][2].y); //11
  178. rd = getSolPoint(a,1,dist);
  179. *r++=point(a[1][0].x+rd*a[1][2].x, a[1][0].y+rd*a[1][2].y); //22
  180. *o=r;
  181. }
  182. };
  183. struct sit_list
  184. {
  185. std::array<site,1000> m_list;
  186. public:
  187. sit_list();
  188. void load(const char*ant_file,const char*path_file)
  189. {
  190. read_sit_list(ant_file);
  191. read_ant_path(path_file);
  192. }
  193. std::string to_string()const
  194. {
  195. std::string rt;
  196. rt.reserve(4096);
  197. for(auto s:m_list)
  198. {
  199. if(s.m_id==-1)
  200. continue;
  201. rt+=s.to_string();
  202. rt+="\n";
  203. }
  204. return std::move(rt);
  205. }
  206. void read_sit_list(const char*fname);
  207. void read_ant_path(const char*fname);
  208. void init_sit_list(int32_t readerid,int32_t antid, double ax, double ay);
  209. void init_sit_list();
  210. void init_ant_path(int32_t readerid,int32_t antid, double ax, double ay);
  211. void init_ant_path();
  212. const site& operator[](int id) const;
  213. };
  214. NAMESPACE_POINT_END(NAMESPACE_POINT)
  215. #endif