1
0

ant.h 6.1 KB

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