ant.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "ant.h"
  6. #include "ProcessRemodule.h"
  7. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  8. site::site(int id)
  9. :m_id(id)
  10. ,m_path_empty(false)
  11. ,m_height(1.5)
  12. ,m_ant_dist(0)
  13. ,m_ant_dist_sum_new(0)
  14. ,m_ant_dist_cnt_new(0)
  15. {
  16. }
  17. void site::sort_path()
  18. {
  19. if(m_ant[0][0][1]<m_ant[0][1][1])
  20. return;
  21. m_ant[0][0].swap(m_ant[0][1]);
  22. m_ant[1][0].swap(m_ant[1][1]);
  23. }
  24. sit_list::sit_list()
  25. {
  26. }
  27. char *strtok_r(char *s, const char *delim, char **save_ptr) {
  28. char *token;
  29. if (s == NULL) s = *save_ptr;
  30. /* Scan leading delimiters. */
  31. s += strspn(s, delim);
  32. if (*s == '\0')
  33. return NULL;
  34. /* Find the end of the token. */
  35. token = s;
  36. s = strpbrk(token, delim);
  37. if (s == NULL)
  38. /* This token finishes the string. */
  39. *save_ptr = strchr(token, '\0');
  40. else {
  41. /* Terminate the token and make *SAVE_PTR point past it. */
  42. *s = '\0';
  43. *save_ptr = s + 1;
  44. }
  45. return token;
  46. }
  47. static int str_split(char*s,char**rc)
  48. {
  49. char**o=rc;
  50. for(;*s;)
  51. {
  52. *o=strtok_r(s,",",&s);
  53. o++;
  54. }
  55. return o-rc;
  56. }
  57. void sit_list::init_sit_list(int32_t readerid,int32_t antid, double ax, double ay,double scale)
  58. {
  59. if(readerid>(int)m_list.size()|| antid >= 2)
  60. {
  61. debug_print_syslog(0,"[lemon smooth Error_sit]:ReaderId is too long:%d or antid is too big antid:%d",readerid,antid);
  62. return;
  63. }
  64. debug_print_syslog(0,"[lemon smooth _sit]:ReaderId:%d antid:%d,scale:%f",readerid,antid,scale);
  65. m_list[readerid].m_id=readerid;
  66. m_list[readerid].m_ant[antid].set(ax,-ay);
  67. m_list[readerid].m_scale = scale;
  68. }
  69. void sit_list::init_sit_list()
  70. {
  71. for(auto&sit:m_list)
  72. {
  73. if(sit.m_id==-1)
  74. continue;
  75. if(sit.m_ant[0]==sit.m_ant[1])
  76. {
  77. //debug_print_syslog(0,"[lemon smooth Error_sit]:has the same point x,y");
  78. }
  79. sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
  80. }
  81. }
  82. //1, 101, 1, '101-1', 4727, 75, 0, 0, '2017-08-29 10:21:14'
  83. void sit_list::read_sit_list(const char*fname)
  84. {
  85. FILE*fp=fopen(fname,"r");
  86. char buf[512];
  87. int t,id;
  88. char* s[20];
  89. while(fgets(buf,sizeof(buf),fp))
  90. {
  91. t=str_split(buf,&s[0]);
  92. if(t<8)
  93. continue;
  94. id=atoi(s[1]);
  95. if(id>(int)m_list.size())
  96. continue;
  97. int antid=atoi(s[2])-1;
  98. if(antid>=2)
  99. continue;
  100. m_list[id].m_id=id;
  101. m_list[id].m_ant[antid].set(atof(s[4]),-atof(s[5]));
  102. }
  103. for(auto&sit:m_list)
  104. {
  105. if(sit.m_id==-1)
  106. continue;
  107. if(sit.m_ant[0]==sit.m_ant[1])
  108. {
  109. // printf("%d分站天线坐标相等.\n",sit.m_id);
  110. }
  111. sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
  112. }
  113. fclose(fp);
  114. }
  115. //315, 1, 5541.65, 338, 0, NULL, -1
  116. //315, 2, 5674.04, 379, 0, NULL, -1
  117. void sit_list::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)
  118. {
  119. if(readerid>(int)m_list.size())
  120. {
  121. debug_print_syslog(0,"[lemon smooth Error_ant]:ReaderId is too long:%d or antid is too big antid:%d",readerid,antid);
  122. return;
  123. }
  124. if(antid>=2)
  125. antid=1;
  126. m_list[readerid].m_ant[0][antid][1].set(ax,-ay);
  127. m_list[readerid].m_ant[1][antid][1].set(ax,-ay);
  128. int i = 0;
  129. while (b != e)
  130. {
  131. std::string bs = *b;
  132. std::string ns = *(++b);
  133. m_list[readerid].m_ant[0][antid][3+i>5?5:3+i].set(m_list[readerid].m_ant[0].dist(m_list[atoi(bs.c_str())]),atof(ns.c_str()));
  134. m_list[readerid].m_ant[1][antid][3+i>5?5:3+i].set(m_list[readerid].m_ant[1].dist(m_list[atoi(bs.c_str())]),atof(ns.c_str()));
  135. ++b;
  136. ++i;
  137. }
  138. }
  139. void sit_list::init_ant_path()
  140. {
  141. for(auto&s:m_list)
  142. {
  143. if(s.m_id==-1)
  144. continue;
  145. if((s.m_path_empty=s.path(0).empty() && s.path(1).empty()))
  146. continue;
  147. s.sort_path();
  148. for(auto&a:s.m_ant)
  149. for(int i=0;i<2;i++)
  150. {
  151. double dx=s.path(i).x-s.x;
  152. double dy=s.path(i).y-s.y;
  153. double r=sqrt(dx*dx+dy*dy);
  154. if(r<1e-10)
  155. {
  156. //printf("empty path:sid=%d,path=%d\n",s.m_id,i);
  157. continue;
  158. }
  159. double cos=dx/r;
  160. double sin=dy/r;
  161. double d=s.m_ant[0].dist(s.m_ant[1])/2;
  162. double p_a=a.dist(s.path(i));
  163. double sign=p_a>r?-1:1;
  164. a[i][2].set(cos,sin);
  165. a[i][0].set(s.x+d*cos*sign,s.y+d*sin*sign);
  166. }
  167. line path(s.path(0),s.path(1));
  168. if(path.dist(s)<2)
  169. {
  170. s.m_line[0] = line_v(s.path(0),s.path(1));
  171. }
  172. else
  173. {
  174. s.m_line[0] = line_v(s.path(0),s);
  175. s.m_line[1] = line_v(s.path(1),s);
  176. }
  177. // s.m_line[0] = std::make_shared<line_v> (s.m_ant[0][0][1],s.m_ant[0][1][1]);
  178. // if(!s.m_ant[0][2][1].empty())
  179. // s.m_line[1] = std::make_shared<line_v>(s.m_ant[0][1][1],s.m_ant[0][2][1]);
  180. }
  181. }
  182. void sit_list::read_ant_path(const char*fname)
  183. {
  184. FILE*fp=fopen(fname,"r");
  185. char buf[512],*p;
  186. int t,id,pid;
  187. char* s[20];
  188. while((p=fgets(buf,sizeof(buf),fp)))
  189. {
  190. t=str_split(buf,&s[0]);
  191. if(t<4)
  192. continue;
  193. id=atoi(s[0]);
  194. if(id>(int)m_list.size())
  195. continue;
  196. pid=atoi(s[1])-1;
  197. if(pid>=2)
  198. pid=1;
  199. m_list[id].m_ant[0][pid][1].set(atof(s[2]),-atof(s[3]));
  200. m_list[id].m_ant[1][pid][1].set(atof(s[2]),-atof(s[3]));
  201. for(int i = 0 ;i < (t-7)/2;i++)
  202. {
  203. m_list[id].m_ant[0][pid][3+i>5?5:3+i].set(m_list[id].m_ant[0].dist(m_list[atoi(s[7+i*2])]),atof(s[8+i*2]));
  204. m_list[id].m_ant[1][pid][3+i>5?5:3+i].set(m_list[id].m_ant[1].dist(m_list[atoi(s[7+i*2])]),atof(s[8+i*2]));
  205. }
  206. }
  207. fclose(fp);
  208. for(auto&s:m_list)
  209. {
  210. if(s.m_id==-1)
  211. continue;
  212. if((s.m_path_empty=s.path(0).empty() && s.path(1).empty()))
  213. continue;
  214. s.sort_path();
  215. for(auto&a:s.m_ant)
  216. for(int i=0;i<2;i++)
  217. {
  218. double dx=s.path(i).x-s.x;
  219. double dy=s.path(i).y-s.y;
  220. double r=sqrt(dx*dx+dy*dy);
  221. if(r<1e-10)
  222. {
  223. // printf("empty path:sid=%d,path=%d\n",s.m_id,i);
  224. continue;
  225. }
  226. double cos=dx/r;
  227. double sin=dy/r;
  228. double d=s.m_ant[0].dist(s.m_ant[1])/2;
  229. double p_a=a.dist(s.path(i));
  230. double sign=p_a>r?-1:1;
  231. a[i][2].set(cos,sin);
  232. a[i][0].set(s.x+d*cos*sign,s.y+d*sin*sign);
  233. }
  234. line path(s.path(0),s.path(1));
  235. if(path.dist(s)<2)
  236. {
  237. s.m_line[0] = line_v(s.path(0),s.path(1));
  238. }
  239. else
  240. {
  241. s.m_line[0] = line_v(s.path(0),s);
  242. s.m_line[1] = line_v(s.path(1),s);
  243. }
  244. }
  245. }
  246. const site& sit_list::operator[](int id) const
  247. {
  248. return m_list[id];
  249. }
  250. NAMESPACE_POINT_END(NAMESPACE_POINT)