ant.cpp 10 KB

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