ant.h 7.3 KB

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