ant.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <sstream>
  10. #include "log.h"
  11. #include "line.h"
  12. #include "point.h"
  13. #include "write-copy.h"
  14. class client;
  15. #include"net-service.h"
  16. #include"common.h"
  17. struct path
  18. {
  19. std::array<line_v,2> m_line;
  20. std::array<double,2> m_slope;
  21. path()
  22. {
  23. for(auto &d:m_slope)
  24. d=0;
  25. }
  26. std::string to_str() const
  27. {
  28. std::stringstream ss;
  29. for(int i=0;i<2;i++)
  30. {
  31. ss<<"line:" <<m_line[i].to_string()<<"slope:"<<m_slope[i]<< " cos:"<<m_line[i].cos()<<" sin:"<<m_line[i].sin()<<" | ";
  32. }
  33. return ss.str();
  34. }
  35. bool vaild() const
  36. {
  37. return !m_line[0].empty();
  38. }
  39. line_v & operator[](int i)
  40. {
  41. return m_line[i];
  42. }
  43. const line_v & operator[](int i) const
  44. {
  45. return m_line[i];
  46. }
  47. };
  48. //?
  49. struct algo_config
  50. {
  51. const char*desc;
  52. int min_msg_cnt;
  53. int best_msg_cnt;
  54. double min_wait_time;
  55. double max_wait_time;
  56. };
  57. //
  58. struct ant :point
  59. {
  60. std::vector<path> m_path;
  61. path & operator[](int i)
  62. {
  63. return m_path[i];
  64. }
  65. const path & operator[](int i) const
  66. {
  67. return m_path[i];
  68. }
  69. size_t size() const
  70. {
  71. return m_path.size();
  72. }
  73. std::vector<point> getsol(const double &dist) const
  74. {
  75. std::vector<point> v;
  76. for(const auto & p : m_path)
  77. {
  78. double d = dist;
  79. if(p.vaild())
  80. {
  81. point pt;
  82. if(dist <= p.m_line[0].length() || (dist > p.m_line[0].length() && p.m_line[1].empty()))
  83. {
  84. d += d*p.m_slope[0];
  85. pt = point(p.m_line[0][0].x + d*p.m_line[0].cos() , p.m_line[0][0].y + d*p.m_line[0].sin());
  86. }
  87. else
  88. {
  89. d -= p.m_line[0].length();
  90. d += d*p.m_slope[1];
  91. pt = point(p.m_line[1][0].x+d*p.m_line[1].cos(),p.m_line[1][0].y+d*p.m_line[1].sin());
  92. }
  93. v.push_back(pt);
  94. //std_info("get_sol:x:%.2f,y:%.2f",pt.x,pt.y);
  95. }
  96. else
  97. std_error(".%s","ant::getsol empty path..");
  98. }
  99. return std::move(v);
  100. }
  101. };
  102. struct site:point
  103. {
  104. static algo_config g_config[];
  105. int m_algo; //TOF:0,TDOA:1
  106. int m_num_dims; //1维:0,2维:1,3维:2
  107. double m_scale = 2.0; // 地图比例尺
  108. point m_position;
  109. int index()const;
  110. const algo_config&config()const;
  111. int id()const
  112. {
  113. return m_id;
  114. }
  115. site(int id=-1);
  116. mutable double m_height=1.5;
  117. int m_id;
  118. bool m_path_empty;
  119. std::array<ant,2> m_ant;
  120. mutable double m_ant_dist=0;
  121. mutable double m_ant_dist_sum_new=0;
  122. mutable int m_ant_dist_cnt_new=0;
  123. ///分站位置 READER_TYPE_ID
  124. int m_reader_type_id = 0;
  125. int m_map_id = 0;
  126. int m_area_id = 0;
  127. /// 设备类型,分站、通信分站、交通灯等
  128. int m_device_type_id=0;
  129. /// 指定分站定位类型:一维定位,二维定位,三维定位
  130. int m_dimension=0;
  131. std::shared_ptr<client> m_clt=nullptr;
  132. void set_client(std::shared_ptr<client>& clt)
  133. {
  134. m_clt = clt;
  135. }
  136. std::shared_ptr<client> get_client()
  137. {
  138. return m_clt;
  139. }
  140. bool is_up_site()
  141. {
  142. return READER_TYPE_ID_UP == m_reader_type_id;
  143. }
  144. point get_dstp(const point pt) const
  145. {
  146. point tmp;
  147. for(const auto & p : m_ant[0].m_path)
  148. {
  149. for(int i=0;i<2;i++)
  150. {
  151. if(!p[i].empty())
  152. {
  153. if(p[i].contain(pt,0.01))
  154. {
  155. //if(i==0)
  156. // return *this;
  157. //else
  158. tmp = p[i][0];
  159. }
  160. }
  161. }
  162. }
  163. return tmp;
  164. }
  165. void count_ant_dist(double dist_tof1, double dist_tof2)const
  166. {
  167. if(dist_tof1<10 || dist_tof2<10)
  168. return;
  169. double dist = fabs(dist_tof1 - dist_tof2);
  170. if(dist>5)
  171. return;
  172. m_ant_dist_sum_new += dist;
  173. m_ant_dist_cnt_new++;
  174. if(m_ant_dist_cnt_new >= 2500)
  175. {
  176. m_ant_dist = m_ant_dist_sum_new / m_ant_dist_cnt_new;
  177. m_ant_dist_sum_new = 0;
  178. m_ant_dist_cnt_new = 0;
  179. }
  180. }
  181. void swap()
  182. {
  183. auto v0 = m_ant[0].m_path;
  184. auto v1 = m_ant[1].m_path;
  185. std::copy (std::begin(v0),std::end(v0),std::back_inserter(m_ant[1].m_path));
  186. std::copy (std::begin(v1),std::end(v1),std::back_inserter(m_ant[0].m_path));
  187. }
  188. double ant_dist()const
  189. {
  190. return m_ant[0].dist(m_ant[1]);
  191. }
  192. bool is_path_empty()const
  193. {
  194. return m_path_empty;
  195. }
  196. bool have_valid_path()const
  197. {
  198. return m_id != -1 && ant_dist() > 0.1;
  199. }
  200. std::string to_string()const
  201. {
  202. std::stringstream ss;
  203. ss<<"site_id:"<<m_id<<"x:"<<x<<" y: "<<y<<" scale:"<<m_scale;
  204. for(const auto a:m_ant)
  205. {
  206. ss<<"<";
  207. for(const auto p:a.m_path)
  208. {
  209. ss<<"{"<<p.to_str()<<"}";
  210. }
  211. ss<<">";
  212. }
  213. return ss.str();
  214. }
  215. const point&path(int i)const
  216. {
  217. static point p;
  218. if(i>=(int)m_ant[0].m_path.size())
  219. return p ;
  220. return m_ant[0].m_path[i].m_line[0][1];
  221. }
  222. std::vector<point> solving(int ant_id, double dist)const
  223. {
  224. const ant &a = m_ant[ant_id];
  225. if(dist<50 && dist>0)
  226. {
  227. if(dist<m_height)
  228. {
  229. m_height=dist;
  230. dist=0;
  231. }
  232. else
  233. {
  234. dist=sqrt(dist*dist-m_height*m_height);
  235. }
  236. }
  237. return std::move(a.getsol(dist));
  238. }
  239. ant operator[](int i)
  240. {
  241. return m_ant[i];
  242. }
  243. const ant operator[](int i) const
  244. {
  245. return m_ant[i];
  246. }
  247. };
  248. struct sit_list:single_base<sit_list,int,std::shared_ptr<site>>
  249. {
  250. void load(const char*ant_file,const char*path_file)
  251. {
  252. read_sit_list(ant_file);
  253. read_ant_path(path_file);
  254. }
  255. void load_from_db()
  256. {
  257. load("data_reader_antenna.txt","path_tof.txt");
  258. init_site();
  259. }
  260. void read_sit_list(const char*fname);
  261. void read_ant_path(const char*fname);
  262. void init_site();
  263. };
  264. #endif