ant.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. /*
  35. void sit_list::read_sit_list(const char*fname)
  36. {
  37. FILE*fp=fopen(fname,"r");
  38. char buf[512];
  39. int t,id;
  40. char* s[20];
  41. while(fgets(buf,sizeof(buf),fp))
  42. {
  43. t=str_split(buf,&s[0]);
  44. if(t<8)
  45. continue;
  46. id=atoi(s[1]);
  47. int antid=atoi(s[2])-1;
  48. if(antid>=2)
  49. continue;
  50. auto tmp = m_instance->get(id);
  51. if(tmp==nullptr)
  52. tmp = std::make_shared<site>(id);
  53. tmp->m_ant[antid].set(atof(s[4]),-atof(s[5]));
  54. sit_list::instance()->add(id,tmp);
  55. //std_info("211111 id=%dsize = %d",id,instance()->m_map.size());
  56. //m_list[id].m_id=id;
  57. //m_list[id].m_ant[antid].set(atof(s[4]),-atof(s[5]));
  58. }
  59. for(auto&sit_:sit_list::instance()->m_map)
  60. {
  61. auto & sit = *(sit_.second);
  62. if(sit.m_id==-1)
  63. continue;
  64. if(sit.m_ant[0]==sit.m_ant[1])
  65. {
  66. log_warn("%d分站天线坐标相等.",sit.m_id);
  67. }
  68. sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
  69. }
  70. fclose(fp);
  71. }
  72. */
  73. void sit_list::read_sit_list(const char*fname)
  74. {
  75. std::unordered_map<int,std::shared_ptr<site>> map;
  76. const char *sql = "SELECT antenna_id, a.reader_id, idx, a.x, a.y, a.z, a.angle \
  77. FROM dat_antenna a, dat_reader r \
  78. WHERE a.reader_id = r.reader_id;";
  79. std::string Error;
  80. YADB::CDBResultSet DBRes;
  81. sDBConnPool.Query(sql,DBRes,Error);
  82. uint64_t nCount = DBRes.GetRecordCount( Error );
  83. if (nCount > 0)
  84. {
  85. log_info( "init_antenna. The record count=%ld\n", nCount );
  86. while ( DBRes.GetNextRecod(Error) )
  87. {
  88. int reader_id = 0;
  89. DBRes.GetField( "reader_id",reader_id, Error );
  90. int idx=0;
  91. DBRes.GetField( "idx",idx, Error );
  92. int antid = idx-1;
  93. if(antid >= 2)
  94. continue;
  95. double x= 0;
  96. DBRes.GetField( "x",x, Error );
  97. double y= 0;
  98. DBRes.GetField( "y",y, Error );
  99. double z= 0;
  100. DBRes.GetField( "z",z, Error );
  101. auto site_ptr=sit_list::instance()->get(reader_id);
  102. if(nullptr==site_ptr)
  103. {
  104. site_ptr = std::make_shared<site>(reader_id);
  105. map.insert({reader_id,site_ptr});
  106. }
  107. site_ptr->m_ant[antid].set(x,-y);
  108. }
  109. }
  110. sit_list::instance()->add(map);
  111. for(auto&sit_:map)
  112. {
  113. auto & sit = *(sit_.second);
  114. if(sit.m_id==-1)
  115. continue;
  116. if(sit.m_ant[0]==sit.m_ant[1])
  117. {
  118. log_warn("%d分站天线坐标相等.",sit.m_id);
  119. }
  120. sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
  121. }
  122. }
  123. /*
  124. void sit_list::read_ant_path(const char*fname)
  125. {
  126. FILE*fp=fopen(fname,"r");
  127. char buf[512],*p;
  128. int t,id,pid;
  129. char* s[20];
  130. while((p=fgets(buf,sizeof(buf),fp)))
  131. {
  132. t=str_split(buf,&s[0]);
  133. if(t<9)
  134. continue;
  135. id=atoi(s[0]);
  136. auto s_ = sit_list::instance()->get(id);
  137. if(s_==nullptr)
  138. continue;
  139. pid=atoi(s[1]);
  140. if(pid>2)
  141. continue;
  142. point p1(atof(s[2]),-atof(s[3]));
  143. point p2(atof(s[5]),-atof(s[6]));
  144. auto &sit_ = *s_;
  145. if(pid == 0)
  146. {
  147. line_v l(p1,p2);
  148. {
  149. point px = l.line::projection(sit_);
  150. sit_.set(px);
  151. for(int i=0;i<2;i++)
  152. {
  153. path p;
  154. p.m_slope[0] = atof(s[8]);
  155. p.m_line[0] = line_v(px,l[i]);
  156. sit_.m_ant[i].m_path.push_back(p);
  157. }
  158. }
  159. }
  160. else
  161. {
  162. ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
  163. if(a.m_path.size()!=0)
  164. {
  165. path &p = a.m_path[0];
  166. p.m_line[abs(pid)-1] = line_v(p1,p2);
  167. p.m_slope[abs(pid)-1] = atof(s[8]);
  168. }
  169. else
  170. {
  171. path p;
  172. p.m_line[abs(pid)-1] = line_v(p1,p2);
  173. p.m_slope[abs(pid)-1] = atof(s[8]);
  174. a.m_path.push_back(p);
  175. }
  176. if(abs(pid)==1)
  177. sit_.set(p1);
  178. }
  179. }
  180. fclose(fp);
  181. for(auto&_s:sit_list::instance()->m_map)
  182. {
  183. auto & s = *(_s.second);
  184. if(s.m_id==-1)
  185. continue;
  186. s.swap();
  187. if((s.path(0).empty() && s.path(1).empty()))
  188. continue;
  189. s.m_path_empty=false;
  190. for(auto &a:s.m_ant)
  191. for(auto &p:a.m_path)
  192. {
  193. if(!p.m_line[0].empty())
  194. {
  195. point px = p.m_line[0].line::projection(a);
  196. p.m_line[0]=line_v(px,p.m_line[0][1]);
  197. }
  198. }
  199. //std_info("%s",s.to_string().c_str());
  200. log_info("%s",s.to_string().c_str());
  201. //std_info("%f----%f",s.x,s.y);
  202. }
  203. }
  204. */
  205. void sit_list::read_ant_path(const char*fname)
  206. {
  207. FILE*fp=fopen(fname,"r");
  208. char buf[512],*p;
  209. int t,id,pid;
  210. char* s[20];
  211. while((p=fgets(buf,sizeof(buf),fp)))
  212. {
  213. t=str_split(buf,&s[0]);
  214. if(t<9)
  215. continue;
  216. id=atoi(s[0]);
  217. auto s_ = sit_list::instance()->get(id);
  218. if(s_==nullptr)
  219. continue;
  220. pid=atoi(s[1]);
  221. if(pid>2)
  222. continue;
  223. point p1(atof(s[2]),-atof(s[3]));
  224. point p2(atof(s[5]),-atof(s[6]));
  225. auto &sit_ = *s_;
  226. if(pid == 0)
  227. {
  228. line_v l(p1,p2);
  229. {
  230. point px = l.line::projection(sit_);
  231. sit_.set(px);
  232. for(int i=0;i<2;i++)
  233. {
  234. path p;
  235. p.m_slope[0] = atof(s[8]);
  236. p.m_line[0] = line_v(px,l[i]);
  237. sit_.m_ant[i].m_path.push_back(p);
  238. }
  239. }
  240. }
  241. else
  242. {
  243. ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
  244. if(a.m_path.size()!=0)
  245. {
  246. path &p = a.m_path[0];
  247. p.m_line[abs(pid)-1] = line_v(p1,p2);
  248. p.m_slope[abs(pid)-1] = atof(s[8]);
  249. }
  250. else
  251. {
  252. path p;
  253. p.m_line[abs(pid)-1] = line_v(p1,p2);
  254. p.m_slope[abs(pid)-1] = atof(s[8]);
  255. a.m_path.push_back(p);
  256. }
  257. if(abs(pid)==1)
  258. sit_.set(p1);
  259. }
  260. }
  261. fclose(fp);
  262. for(auto&_s:sit_list::instance()->m_map)
  263. {
  264. auto & s = *(_s.second);
  265. if(s.m_id==-1)
  266. continue;
  267. s.swap();
  268. if((s.path(0).empty() && s.path(1).empty()))
  269. continue;
  270. s.m_path_empty=false;
  271. for(auto &a:s.m_ant)
  272. for(auto &p:a.m_path)
  273. {
  274. if(!p.m_line[0].empty())
  275. {
  276. point px = p.m_line[0].line::projection(a);
  277. p.m_line[0]=line_v(px,p.m_line[0][1]);
  278. }
  279. }
  280. //std_info("%s",s.to_string().c_str());
  281. log_info("%s",s.to_string().c_str());
  282. //std_info("%f----%f",s.x,s.y);
  283. }
  284. }
  285. void sit_list::init_site()
  286. {
  287. const char *sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
  288. area_id, device_type_id, dimension, dat_map.scale\
  289. FROM dat_reader, dat_map where \
  290. dat_reader.map_id=dat_map.map_id and state=0;";
  291. std::string Error;
  292. YADB::CDBResultSet DBRes;
  293. sDBConnPool.Query(sql,DBRes,Error);
  294. uint64_t nCount = DBRes.GetRecordCount( Error );
  295. if (nCount > 0)
  296. {
  297. log_info( "init_site. The record count=%ld\n", nCount );
  298. while ( DBRes.GetNextRecod(Error) )
  299. {
  300. int reader_id = 0;
  301. DBRes.GetField( "reader_id",reader_id, Error );
  302. auto site_ptr=sit_list::instance()->get(reader_id);
  303. if(nullptr==site_ptr)
  304. {
  305. site_ptr = std::make_shared<site>(reader_id);
  306. sit_list::instance()->add(reader_id,site_ptr);
  307. }
  308. int reader_type_id = 0;
  309. DBRes.GetField( "reader_type_id",reader_type_id, Error );
  310. int map_id = 0;
  311. DBRes.GetField( "map_id",map_id, Error );
  312. int area_id = 0;
  313. DBRes.GetField( "area_id",area_id, Error );
  314. int device_type_id = 0;
  315. DBRes.GetField( "device_type_id",device_type_id, Error );
  316. int dimension = 0;
  317. DBRes.GetField( "dimension",dimension, Error );
  318. double scale= 0;
  319. DBRes.GetField( "scale",scale, Error );
  320. site_ptr->m_reader_type_id = reader_type_id;
  321. site_ptr->m_map_id = map_id;
  322. site_ptr->m_area_id = area_id;
  323. site_ptr->m_device_type_id = device_type_id;
  324. site_ptr->m_dimension = dimension;
  325. site_ptr->m_scale = scale;
  326. }
  327. }
  328. }
  329. algo_config site::g_config[]=
  330. {
  331. { "tof-1", 1, 2, 0.1, 1 },
  332. { "tdoa-1", 2, 2, 0.1, 1 },
  333. { "tof-2", 2, 3, 0.1, 1 },
  334. { "tdoa-2", 3, 3, 0.1, 1 },
  335. { "tof-3", 3, 4, 0.1, 1 },
  336. { "tdoa-3", 4, 4, 0.1, 1 }
  337. };
  338. #ifdef _TEST
  339. int main()
  340. {
  341. log_init("./log.ini");
  342. //sit_list *sl = sit_list::instance();
  343. sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  344. sit_list::instance()->get(209)->solving(0,100);
  345. sit_list::instance()->get(209)->solving(1,100.5);
  346. //std_info("---%d",(*sl)[209].m_ant[0].m_path.size());
  347. //std_info("---%d",(*sl)[209][0].size());
  348. //std_info("---%s",(*sl)[209][0][0][0].to_string().c_str());
  349. }
  350. #endif