ant.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include "ant.h"
  5. #include "db_api/CDBConnPool.h"
  6. template<> std::shared_ptr<sit_list>
  7. single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();
  8. int site::index()const
  9. {
  10. return m_algo+(m_num_dims<<1);
  11. }
  12. site::site(int id)
  13. :m_algo(0)
  14. ,m_num_dims(0)
  15. ,m_id(id)
  16. ,m_path_empty(true)
  17. {
  18. }
  19. const algo_config&site::config()const
  20. {
  21. return g_config[index()];
  22. }
  23. static int str_split(char*s,char**rc)
  24. {
  25. char**o=rc;
  26. for(;*s;)
  27. {
  28. *o=strtok_r(s,",",&s);
  29. o++;
  30. }
  31. return o-rc;
  32. }
  33. //1, 101, 1, '101-1', 4727, 75, 0, 0, '2017-08-29 10:21:14'
  34. void sit_list::read_sit_list(const char*fname)
  35. {
  36. FILE*fp=fopen(fname,"r");
  37. char buf[512];
  38. int t,id;
  39. char* s[20];
  40. while(fgets(buf,sizeof(buf),fp))
  41. {
  42. t=str_split(buf,&s[0]);
  43. if(t<8)
  44. continue;
  45. id=atoi(s[1]);
  46. int antid=atoi(s[2])-1;
  47. if(antid>=2)
  48. continue;
  49. auto tmp = m_instance->get(id);
  50. if(tmp==nullptr)
  51. tmp = std::make_shared<site>(id);
  52. tmp->m_ant[antid].set(atof(s[4]),-atof(s[5]));
  53. sit_list::instance()->add(id,tmp);
  54. //std_info("211111 id=%dsize = %d",id,instance()->m_map.size());
  55. //m_list[id].m_id=id;
  56. //m_list[id].m_ant[antid].set(atof(s[4]),-atof(s[5]));
  57. }
  58. for(auto&sit_:sit_list::instance()->m_map)
  59. {
  60. auto & sit = *(sit_.second);
  61. if(sit.m_id==-1)
  62. continue;
  63. if(sit.m_ant[0]==sit.m_ant[1])
  64. {
  65. log_warn("%d分站天线坐标相等.",sit.m_id);
  66. }
  67. sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
  68. }
  69. fclose(fp);
  70. }
  71. void sit_list::read_ant_path(const char*fname)
  72. {
  73. FILE*fp=fopen(fname,"r");
  74. char buf[512],*p;
  75. int t,id,pid;
  76. char* s[20];
  77. while((p=fgets(buf,sizeof(buf),fp)))
  78. {
  79. t=str_split(buf,&s[0]);
  80. if(t<9)
  81. continue;
  82. id=atoi(s[0]);
  83. auto s_ = sit_list::instance()->get(id);
  84. if(s_==nullptr)
  85. continue;
  86. pid=atoi(s[1]);
  87. if(pid>2)
  88. continue;
  89. point p1(atof(s[2]),-atof(s[3]));
  90. point p2(atof(s[5]),-atof(s[6]));
  91. auto &sit_ = *s_;
  92. if(pid == 0)
  93. {
  94. line_v l(p1,p2);
  95. {
  96. point px = l.line::projection(sit_);
  97. sit_.set(px);
  98. for(int i=0;i<2;i++)
  99. {
  100. path p;
  101. p.m_slope[0] = atof(s[8]);
  102. p.m_line[0] = line_v(px,l[i]);
  103. sit_.m_ant[i].m_path.push_back(p);
  104. }
  105. }
  106. }
  107. else
  108. {
  109. ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
  110. if(a.m_path.size()!=0)
  111. {
  112. path &p = a.m_path[0];
  113. p.m_line[abs(pid)-1] = line_v(p1,p2);
  114. p.m_slope[abs(pid)-1] = atof(s[8]);
  115. }
  116. else
  117. {
  118. path p;
  119. p.m_line[abs(pid)-1] = line_v(p1,p2);
  120. p.m_slope[abs(pid)-1] = atof(s[8]);
  121. a.m_path.push_back(p);
  122. }
  123. if(abs(pid)==1)
  124. sit_.set(p1);
  125. }
  126. }
  127. fclose(fp);
  128. for(auto&_s:sit_list::instance()->m_map)
  129. {
  130. auto & s = *(_s.second);
  131. if(s.m_id==-1)
  132. continue;
  133. s.swap();
  134. if((s.path(0).empty() && s.path(1).empty()))
  135. continue;
  136. s.m_path_empty=false;
  137. for(auto &a:s.m_ant)
  138. for(auto &p:a.m_path)
  139. {
  140. if(!p.m_line[0].empty())
  141. {
  142. point px = p.m_line[0].line::projection(a);
  143. p.m_line[0]=line_v(px,p.m_line[0][1]);
  144. }
  145. }
  146. //std_info("%s",s.to_string().c_str());
  147. log_info("%s",s.to_string().c_str());
  148. //std_info("%f----%f",s.x,s.y);
  149. }
  150. }
  151. void sit_list::init_site()
  152. {
  153. const char *sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
  154. area_id, device_type_id, dimension, dat_map.scale\
  155. FROM dat_reader, dat_map where \
  156. dat_reader.map_id=dat_map.map_id and state=0;";
  157. std::string Error;
  158. YADB::CDBResultSet DBRes;
  159. sDBConnPool.Query(sql,DBRes,Error);
  160. uint64_t nCount = DBRes.GetRecordCount( Error );
  161. if (nCount > 0)
  162. {
  163. log_info( "init_site. The record count=%ld\n", nCount );
  164. while ( DBRes.GetNextRecod(Error) )
  165. {
  166. int reader_id = 0;
  167. DBRes.GetField( "reader_id",reader_id, Error );
  168. auto site_ptr=sit_list::instance()->get(reader_id);
  169. if(nullptr==site_ptr)
  170. {
  171. site_ptr = std::make_shared<site>(reader_id);
  172. sit_list::instance()->add(reader_id,site_ptr);
  173. }
  174. int reader_type_id = 0;
  175. DBRes.GetField( "reader_type_id",reader_type_id, Error );
  176. int map_id = 0;
  177. DBRes.GetField( "map_id",map_id, Error );
  178. int area_id = 0;
  179. DBRes.GetField( "area_id",area_id, Error );
  180. int device_type_id = 0;
  181. DBRes.GetField( "device_type_id",device_type_id, Error );
  182. int dimension = 0;
  183. DBRes.GetField( "dimension",dimension, Error );
  184. double scale= 0;
  185. DBRes.GetField( "scale",scale, Error );
  186. site_ptr->m_reader_type_id = reader_type_id;
  187. site_ptr->m_map_id = map_id;
  188. site_ptr->m_area_id = area_id;
  189. site_ptr->m_device_type_id = device_type_id;
  190. site_ptr->m_dimension = dimension;
  191. site_ptr->m_scale = scale;
  192. }
  193. }
  194. }
  195. loc_message::loc_message()
  196. :m_num_ticks(0)
  197. {
  198. }
  199. int loc_message::tool_index()const
  200. {
  201. return m_sit.index();
  202. }
  203. algo_config site::g_config[]=
  204. {
  205. { "tof-1", 1, 2, 0.1, 1 },
  206. { "tdoa-1", 2, 2, 0.1, 1 },
  207. { "tof-2", 2, 3, 0.1, 1 },
  208. { "tdoa-2", 3, 3, 0.1, 1 },
  209. { "tof-3", 3, 4, 0.1, 1 },
  210. { "tdoa-3", 4, 4, 0.1, 1 }
  211. };
  212. #ifdef _TEST
  213. int main()
  214. {
  215. log_init("./log.ini");
  216. //sit_list *sl = sit_list::instance();
  217. sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  218. sit_list::instance()->get(209)->solving(0,100);
  219. sit_list::instance()->get(209)->solving(1,100.5);
  220. //std_info("---%d",(*sl)[209].m_ant[0].m_path.size());
  221. //std_info("---%d",(*sl)[209][0].size());
  222. //std_info("---%s",(*sl)[209][0][0][0].to_string().c_str());
  223. }
  224. #endif