classdef.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. #include "stdafx.h"
  2. #include "classdef.h"
  3. #include "constdef.h"
  4. #include "structdef.h"
  5. #include "locate_algorithm.h"
  6. #include <math.h>
  7. Card::Card( string cardid, int cardtype, double z_offset, double offset_x /*= 12.0*/, double offset_y /*=12.0*/ )
  8. {
  9. InitializeCriticalSection(&m_csCard);
  10. card_type = cardtype;
  11. card_id = cardid;
  12. coor_offset_x = offset_x;
  13. coor_offset_y = offset_y;
  14. this->z_offset = z_offset;
  15. GetSystemTime(&deal_time);
  16. down_time = up_time = enter_area_time = enter_reader_time = rec_time = time(NULL);
  17. time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time_low_power = time(NULL);
  18. x = y = z = last_x = last_y = last_z = stored_x = stored_y = stored_z = 0;
  19. xx = yy = zz = 0;
  20. x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = x4 = y4 = z4 = 0;
  21. a = 0;
  22. t = 0;
  23. init_postion = false;
  24. is_first_location = true;
  25. map_id = map_id_old = area_id = reader_id = 0;
  26. map_scale = 1.0;
  27. state = 0;
  28. state_moving = 0;
  29. state_biz = 0;
  30. pos_state = pos_state_old = DT_INIT;
  31. pos_state_park = pos_state_park_old = AAT_ENTER;
  32. pos_state_count = pos_state_park_count = 0;
  33. dept_id = group_id = 0;
  34. power_state = power_state_last = 0;
  35. status_help = status_area_over_time = status_area_forbidden = status_area_over_speed = 0;
  36. status_over_speed = status_over_time = status_power = status_lost = status_call = 0;
  37. sync_num = 0;
  38. isdealed = isreceive = is_pos_changed = is_hist = is_need_cal = false;
  39. is_area_over_time = is_mine_over_time = false;
  40. id = name = number = department = group = worktype = "";
  41. reader_tickcount = time_stamp = time_stamp_last = 0;
  42. p_dists = new _coordinate*[DIST_COUNT];
  43. p_dists_locate = new _coordinate*[DIST_COUNT];
  44. time_stamp_max = 0;
  45. for(int i = 0; i < DIST_COUNT; i++){
  46. p_dists[i] = NULL;
  47. p_dists_locate[i] = NULL;
  48. }
  49. cal_location3 = new CalLocation3(KALMAN_OFFSET_COOR_TRI, KALMAN_OFFSET_MOVE_TRI, KALMAN_OFFSET_RANGING_TRI, KALMAN_INTERVAL_TRI);
  50. p3_anchors = new Point3[ANCHOR_COUNT];
  51. is_deal_by_algo = false;
  52. pReaderPathMap = NULL;
  53. pTdoaReaderPathMap = NULL;
  54. last_s_locate_reader[0] = last_s_locate_reader[1] = -1;
  55. m_syncNumList.clear();
  56. m_dFirstDiff = m_dSecondDiff = 0.0;
  57. m_nCalcSyncNum = 0;
  58. m_nSyncNumInList = 0;
  59. last_locate.tt = 0;
  60. last_locate.a = last_locate.antenna_id = last_locate.reader_id = last_locate.t = 0;
  61. last_locate.x = last_locate.y = last_locate.z = last_locate.d = last_locate.d_offset = last_locate.v = 0.0;
  62. m_bUseFilter = false;
  63. m_nFilterType = NO_FILTER;
  64. m_pKalmanFilter = NULL;
  65. m_nLastLocateT = 0;
  66. origin_locate.x = origin_locate.y = origin_locate.z = origin_locate.v = 0.0;
  67. last_vx = last_vy = 0.0;
  68. v = 0.0;
  69. ins_weight = uwb_weight = weight = 0;
  70. time_stamp_last = 0;
  71. reader_id_last = 0;
  72. _dists.swap(DistQueMap());
  73. //m_pKalmanFilter = new CKalmanFilter();
  74. //m_pKalmanFilter->Initial(1);
  75. //m_pKalmanFilter->m_bFlag = false;
  76. }
  77. Card::Card( void )
  78. {
  79. }
  80. //void Card::set_pos_state( int val )
  81. //{
  82. // if(val == pos_state) {
  83. // pos_state_count++;
  84. // return;
  85. // }
  86. // pos_state = val;
  87. // pos_state_count = 1;
  88. //}
  89. void Card::set_reader( Reader* preader ) // 设置卡时间
  90. {
  91. //TRACE(_T("time memcpy 11\n"));
  92. this->rec_time = time(NULL);
  93. if(preader->reader_id == this->reader_id){ // 所在分站没有发生变化
  94. this->pos_state_count++;
  95. }else{ // 分站变化
  96. this->p_reader = preader;
  97. this->reader_id = preader->reader_id;
  98. this->enter_reader_time = this->rec_time;
  99. this->map_id_old = this->map_id;
  100. this->map_id = preader->map_id;
  101. this->map_scale = preader->map_scale;
  102. }
  103. }
  104. /*
  105. * 设置地图集覆盖范围,适用TOF
  106. *
  107. */
  108. void Card::set_reader_path(ReaderPathMap* rpm)
  109. {
  110. this->pReaderPathMap = rpm;
  111. }
  112. /*
  113. * 设置地图集覆盖范围,适用TDOA
  114. *
  115. */
  116. void Card::set_reader_path(TDOAReaderPathMap* trpm)
  117. {
  118. this->pTdoaReaderPathMap = trpm;
  119. }
  120. bool Card::is_pos_state_changed( int nval ) // 考勤
  121. {
  122. if(this->pos_state == this->pos_state_old){
  123. if(this->pos_state_count < nval){
  124. this->pos_state_count++;
  125. }
  126. return false;
  127. }
  128. //if(this->pos_state_count < nval ){ // 未达到确认次数
  129. // this->pos_state_count++;
  130. // return false;
  131. //}
  132. this->pos_state_old = this->pos_state;
  133. this->pos_state_count = 1;
  134. return true;
  135. //if(last_area_type_id != cur_area_type_id){
  136. // if(0 ==last_area_type_id || 0==cur_area_type_id){
  137. // return true;
  138. // }
  139. //}
  140. //return false;
  141. //return (this->last_area_type_id != this->cur_area_type_id && 0 == (this->cur_area_type_id & this->last_area_type_id));
  142. }
  143. void Card::add_dist(_coordinate* dist)
  144. {
  145. EnterCriticalSection(&m_csCard);
  146. string s = concat(dist->reader_id, dist->antenna_id);
  147. int idx = FindDistMap(dist->t);
  148. if(-1 == idx){
  149. DistQueMapItem dq;
  150. dq.cardstamp = dist->t;
  151. dq.distmap[s] = dist;
  152. _dists.push_back(dq);
  153. }else{
  154. _dists[idx].distmap[s] = dist;
  155. }
  156. if(_dists.size() >= MAX_DIST_CACHE){ // 超过缓存数量限制
  157. // 计算并删除第一个
  158. get_coordinate();
  159. remove_dist_head();
  160. }
  161. LeaveCriticalSection(&m_csCard);
  162. }
  163. time_t Card::get_working_time()
  164. {
  165. //return (card_type == CT_VEHICLE) ? leave_park_time : down_time;
  166. return down_time;
  167. }
  168. int Card::get_effictive_dist_count( int offset /*= 0*/ )
  169. {
  170. if(0 == _dists.size()) return 0;
  171. time_stamp_cal = _dists.front().cardstamp;
  172. return (_dists.front().distmap.size());
  173. //int nTimeStamp;
  174. //if(time_stamp_max == 0 && offset != 0){
  175. // nTimeStamp = 0xFFFF;
  176. //}else{
  177. // nTimeStamp = time_stamp_max - offset;
  178. //}
  179. //int ret = 0, tmp = -1;
  180. //for(int i = 0; i < DIST_COUNT; i++){
  181. // if(NULL == p_dists[i]) break;
  182. // if(p_dists[i]->t == nTimeStamp){
  183. // ret++;
  184. // }
  185. //}
  186. //return ret;
  187. }
  188. Card::~Card(void)
  189. {
  190. if(m_pKalmanFilter){
  191. delete m_pKalmanFilter;
  192. m_pKalmanFilter = NULL;
  193. }
  194. for(int i = 0;i<DIST_COUNT;i++){
  195. delete p_dists[i];
  196. p_dists[i] = NULL;
  197. delete p_dists_locate[i];
  198. p_dists_locate[i] = NULL;
  199. }
  200. if(p_dists){
  201. delete[] p_dists;
  202. p_dists = NULL;
  203. }
  204. if(p_dists_locate){
  205. delete[] p_dists_locate;
  206. p_dists_locate = NULL;
  207. }
  208. if(p3_anchors){
  209. delete[] p3_anchors;
  210. p3_anchors = NULL;
  211. }
  212. if(cal_location3){
  213. delete cal_location3;
  214. }
  215. DeleteCriticalSection(&m_csCard);
  216. }
  217. void Card::get_coordinate_2d( int cnt )
  218. {
  219. //double d_dist[3];
  220. //Point2 pt2, pt2_1;
  221. //for(int i = 0; i < cnt; i++){
  222. // d_dist[i] = p_dists_locate[i]->d;
  223. //}
  224. //for(int i=0; i< cnt; i++){
  225. // if(p_dists[i]->reader_id == 1){
  226. // p2_anchors[0].x = p_dists[i]->x;
  227. // p2_anchors[0].y = p_dists[i]->y;
  228. // d_dist[0] = p_dists[i]->d;
  229. // }else if(p_dists[i]->reader_id == 2){
  230. // p2_anchors[1].x = p_dists[i]->x;
  231. // p2_anchors[1].y = p_dists[i]->y;
  232. // d_dist[1] = p_dists[i]->d;
  233. // }if(p_dists[i]->reader_id == 3){
  234. // p2_anchors[2].x = p_dists[i]->x;
  235. // p2_anchors[2].y = p_dists[i]->y;
  236. // d_dist[2] = p_dists[i]->d;
  237. // }
  238. //}
  239. //Mat mat(3, 1, d_dist);
  240. //if(is_anchor_changed){
  241. // cal_location->setAnchors(p2_anchors);
  242. // pt2 = cal_location->initPos(mat);
  243. //} else{
  244. // pt2 = cal_location->estiPos(mat);
  245. //}
  246. //pt2_1 = cal_location->getError();
  247. //x = pt2.x;
  248. //y = pt2.y;
  249. //z = 0;
  250. //x1 = pt2_1.x;
  251. //y1 = pt2_1.y;
  252. //z1 = 0;
  253. }
  254. void Card::get_coordinate_3d( int cnt )
  255. {
  256. double d_dist[ANCHOR_COUNT];
  257. Point3 pt3, pt3_1;
  258. for(int i = 0; i < cnt; i++){
  259. d_dist[i] = p_dists_locate[i]->d;
  260. }
  261. Mat mat(ANCHOR_COUNT, 1, d_dist);
  262. if(is_anchor_changed){
  263. set_anchors(cnt);
  264. pt3 = cal_location3->initPos(mat);
  265. } else{
  266. pt3 = cal_location3->estiPos(mat);
  267. }
  268. pt3_1 = cal_location3->getError();
  269. x = pt3.x;
  270. y = pt3.y;
  271. z = pt3.z;
  272. x1 = pt3_1.x;
  273. y1 = pt3_1.y;
  274. z1 = pt3_1.z;
  275. }
  276. double Card::get_speed()
  277. {
  278. double speed = 0;
  279. // 计算速度
  280. return speed;
  281. }
  282. void Card::set_anchors( int cnt )
  283. {
  284. for(int i = 0; i < cnt; i++){
  285. p3_anchors[i].x = p_dists[i]->x;
  286. p3_anchors[i].y = p_dists[i]->y;
  287. p3_anchors[i].z = p_dists[i]->z;
  288. }
  289. cal_location3->setAnchors(p3_anchors, ANCHOR_COUNT);
  290. is_anchor_changed = false;
  291. }
  292. bool operator==(sync_data& a,sync_data&b){
  293. return a.sync_num == b.sync_num;
  294. }
  295. /*
  296. * 采用TOF或者TDOA算法进行定位计算
  297. *
  298. * param
  299. * cnt ------ _dists数据条数
  300. *
  301. * return
  302. * 无返回值
  303. *
  304. */
  305. void Card::get_coordinate( int cnt )
  306. {
  307. int i = 0, nCount = 0;
  308. POS *p = NULL;
  309. POS tmpPos;
  310. tmpPos.posx = 0;
  311. tmpPos.posy = 0;
  312. tmpPos.pos_radius = 0;
  313. #ifdef ALGORITHM_TOF
  314. // if(cnt < 2) return; // 只有一个测量值,忽略不计算
  315. int difT = 1;
  316. //int i = 0, nCount = 0;
  317. bool bfound = false;
  318. DistMap::iterator it = _dists.front()->mp_dist.begin();
  319. if(cnt > 2){
  320. for(; it != _dists.front()->mp_dist.end(); ++it){
  321. bfound = false;
  322. // 判断是否已经加入到列表
  323. for(int j=0; j < i; j++){
  324. if(p_dists_locate[j]->reader_id == it->second->reader_id){
  325. bfound = true;
  326. break;
  327. }
  328. }
  329. if(i < DIST_COUNT && !bfound){
  330. p_dists_locate[i] = it->second;
  331. i++;
  332. }
  333. }
  334. nCount = i;
  335. }else{
  336. for(; it != _dists.front()->mp_dist.end(); ++it,i++){
  337. p_dists_locate[i] = it->second;
  338. }
  339. nCount = cnt;
  340. }
  341. if(!init_postion){
  342. this->x = p_dists_locate[0]->x;
  343. this->y = p_dists_locate[0]->y;
  344. this->z = p_dists_locate[0]->z;
  345. this->a = p_dists_locate[0]->a;
  346. this->distance = p_dists_locate[0]->d;
  347. this->t = p_dists_locate[0]->t;
  348. this->last_x = p_dists_locate[0]->x;
  349. this->last_y = p_dists_locate[0]->y;
  350. this->last_z = p_dists_locate[0]->z;
  351. init_postion = true;
  352. //InitStation();
  353. }
  354. int sta_num = 0;
  355. int ant = 0;
  356. double dist = 0.0;
  357. INFO_PRE info_pre;
  358. info_pre.t = p_dists_locate[0]->t;
  359. info_pre.detaT = 0;
  360. info_pre.sta_num = p_dists_locate[0]->reader_id - 1;
  361. info_pre.ant = p_dists_locate[0]->antenna_id;
  362. info_pre.dist = p_dists_locate[0]->d;
  363. /*POS *p = NULL;
  364. POS tmpPos;
  365. tmpPos.posx = 0;
  366. tmpPos.posy = 0;
  367. tmpPos.pos_radius = 0;*/
  368. if(info_pre.t == 5){
  369. int sss = 0;
  370. sss = info_pre.t;
  371. }
  372. //this->p_reader->readerCoveragePath
  373. for(int i = 1;i < nCount;i++){
  374. sta_num = p_dists_locate[i]->reader_id - 1;
  375. ant = p_dists_locate[i]->antenna_id;
  376. dist = p_dists_locate[i]->d;
  377. //p = TOFLocateAlgorithm::Pos(station,sta_num,ant,dist,info_pre);
  378. p = LocateAlgorithm::Pos(*(this->pReaderPathMap),sta_num,ant,dist,info_pre);
  379. if(p == NULL)
  380. {
  381. continue;
  382. }
  383. if(p->pos_radius < 2.0){
  384. if(i == 1){
  385. tmpPos.posx = p->posx;
  386. tmpPos.posy = p->posy;
  387. tmpPos.pos_radius = p->pos_radius;
  388. }else{
  389. double posx = (tmpPos.posx + p->posx)/2;
  390. double posy = (tmpPos.posy + p->posy)/2;
  391. double radius = (tmpPos.pos_radius + p->pos_radius)/2;
  392. tmpPos.posx = posx;
  393. tmpPos.posy = posy;
  394. tmpPos.pos_radius = radius;
  395. }
  396. }
  397. if(p){
  398. delete p;
  399. p = NULL;
  400. }
  401. }
  402. if(tmpPos.posx != 0 && tmpPos.posy != 0){
  403. int difT = info_pre.t - this->t;
  404. double newPosX = tmpPos.posx / (this->map_scale*1.0);
  405. double newPosY = tmpPos.posy / (this->map_scale*1.0);
  406. double d = sqrt(pow(newPosX - this->last_locate.x,2)+pow(newPosY - this->y,2))*this->map_scale;
  407. if(difT==0){
  408. this->last_locate.v = 0;
  409. }else{
  410. this->last_locate.v = d/difT;
  411. }
  412. this->last_locate.x = tmpPos.posx / (this->map_scale*1.0);
  413. this->last_locate.y = tmpPos.posy / (this->map_scale*1.0);
  414. this->last_locate.t = info_pre.t;
  415. }
  416. this->x = this->last_locate.x;
  417. this->y = this->last_locate.y;
  418. this->v = ((int)(this->last_locate.v*1000))/1000.0;
  419. this->z = 0;
  420. this->a = 0;
  421. #elif defined ALGORITHM_TYPE_TDOA
  422. if(cnt < 2){
  423. return;
  424. }
  425. //主要处理当相同卡的时间戳的数据中存在同步序号大于5的情况,如果有大于5的数据则丢弃此数据
  426. DistMap::iterator it = _dists.front().distmap.begin();
  427. int k = 0;
  428. int dst = 0;
  429. int st = 0;
  430. bool bRet = false;
  431. for(; it != _dists.front().distmap.end(); ++it,k++){
  432. if(k==0){
  433. st = it->second->st;
  434. }else{
  435. dst = abs(st - it->second->st);
  436. if(dst >= 5){
  437. bRet = true;
  438. break;
  439. //continue;
  440. }
  441. }
  442. p_dists_locate[k] = it->second;
  443. }
  444. this->x = 0;
  445. this->y = 0;
  446. this->z = 0;
  447. if(bRet){
  448. this->x = this->last_locate.x;
  449. this->y = this->last_locate.y;
  450. this->z = INVALID_COORDINATE;
  451. return;
  452. }
  453. //HeapSort(p_dists_locate,k);
  454. SelectSort(p_dists_locate,k);
  455. ReceiveDataMap pRdm;
  456. pRdm.clear();
  457. bRet = false;
  458. int maxSyncTimes = 0;
  459. int acce_state = 2;
  460. for(int i=0;i<k;i++){
  461. if(i==0){
  462. maxSyncTimes = p_dists_locate[i]->st;
  463. acce_state = p_dists_locate[i]->acce_state;
  464. }
  465. else{
  466. if(maxSyncTimes < p_dists_locate[i]->st){
  467. maxSyncTimes = p_dists_locate[i]->st;
  468. acce_state = p_dists_locate[i]->acce_state;
  469. }
  470. }
  471. ReceiveDataMap::iterator prdm_it = pRdm.find(p_dists_locate[i]->reader_id);
  472. if(prdm_it == pRdm.end()){
  473. if(p_dists_locate[i]->tt == LLONG_MAX ){
  474. bRet = true;
  475. break;
  476. }
  477. ReceiveData prd;
  478. prd.reader_id = p_dists_locate[i]->reader_id;
  479. prd.antenna_id = p_dists_locate[i]->antenna_id;
  480. prd.rec_time_stamp = p_dists_locate[i]->tt;
  481. prd.x = p_dists_locate[i]->x*this->map_scale;
  482. prd.y = p_dists_locate[i]->y*this->map_scale;
  483. prd.z = p_dists_locate[i]->z*this->map_scale;
  484. prd.special = p_dists_locate[i]->special;
  485. pRdm[prd.reader_id] = prd;
  486. }
  487. }
  488. acce_state = 2;
  489. bool bOutput = false;
  490. int nSize = 0;
  491. if(pTdoaReaderPathMap->size() > 0 && pRdm.size() > 1){
  492. this->m_nCalcSyncNum = maxSyncTimes;
  493. p = LocateAlgorithm::Pos(&pRdm,*pTdoaReaderPathMap);
  494. this->origin_locate.x = p->posx / (this->map_scale*1.0);
  495. this->origin_locate.y = p->posy / (this->map_scale*1.0);
  496. //TRACE(_T("%d,%f,%f"),this->time_stamp_cal,p->posx/this->map_scale,p->posy/this->map_scale);
  497. bool bRet = false;
  498. bRet = LocateAlgorithm::CheckPosInValid(p,&pRdm,this->map_scale);
  499. if(bRet){
  500. p->posx = INVALID_COORDINATE ;
  501. p->posy = INVALID_COORDINATE ;
  502. p->posz = -2 ;
  503. }
  504. double interval_time = this->p_reader->reader_interval_time;
  505. double deltaT = 0.0;
  506. double cvx = 0;
  507. double cvy = 0;
  508. double cv = 0;
  509. double cx = 0;
  510. double cy = 0;
  511. double cz = 0;
  512. int nSign = 1;
  513. sync_data sdNew;
  514. sync_data sd;
  515. cx = p->posx / (this->map_scale*1.0);
  516. cy = p->posy / (this->map_scale*1.0);
  517. cz = p->posz / (this->map_scale*1.0);
  518. #ifdef ALGORITHM_TYPE_INS
  519. int ins_state = 0;
  520. int uwb_state = 0;
  521. bool bUseKalman = false;
  522. bool bOriginLocateState = false;
  523. #endif
  524. if(p->posx != INVALID_COORDINATE && p->posy != INVALID_COORDINATE){
  525. #ifdef ALGORITHM_TYPE_INS
  526. bOriginLocateState = true;
  527. #endif
  528. //定位成功
  529. if(this->m_nLastLocateT == 0){
  530. sdNew.sync_num = maxSyncTimes;
  531. sdNew.x = cx;
  532. sdNew.y = cy;
  533. sdNew.vx = 0;
  534. sdNew.vy = 0;
  535. sdNew.update = false;
  536. m_syncNumList.push_back(sdNew);
  537. }
  538. else{
  539. //现在的关于同步序号的处理是这样的:
  540. //如果定位成功,就把这次定位成功的同步数据:同步序号,坐标;x,y方向的速度,扔到一个队列里,
  541. //后来定位成功的就会先根据同步序号差用加速度抛一次;
  542. //抛不掉,就用队列里的同步数据(从后往前找),找到第一个与当前同步序号相差大于5的同步数据来进行第二次计算速度以及加速度,
  543. //如果加速度大于5,就不要此次的定位数据,
  544. //如果通过加速度判断就将队列中从头开始到此同步数据的所有元素都丢弃,并插入新的此次同步数据
  545. this->m_dFirstDiff = p->dFirstDiff;
  546. this->m_dSecondDiff = p->dSecondDiff;
  547. //从队列尾部开始查找,找到第一个同步序号与当前计算卡的同步序号相差5个以上的数据
  548. //list<sync_data>::reverse_iterator it = m_syncNumList.rbegin();
  549. list<sync_data>::reverse_iterator it;
  550. bool bOverflow = false;
  551. for(it = m_syncNumList.rbegin();it!=m_syncNumList.rend();it++){
  552. if(maxSyncTimes - it->sync_num >= 5){
  553. sd = *it;
  554. break;
  555. }
  556. else{
  557. if(maxSyncTimes - it->sync_num < 0 && maxSyncTimes < 100){
  558. //如果最新同步号小于列表中的同步号则
  559. if(maxSyncTimes + 65536 - it->sync_num >=5 ){
  560. bOverflow = true;
  561. sd = *it;
  562. }
  563. }else{
  564. continue;
  565. }
  566. }
  567. }
  568. //根据溢出条件来计算deltaT
  569. if(bOverflow){
  570. deltaT = (maxSyncTimes + 65536 - sd.sync_num)*interval_time;
  571. }else{
  572. deltaT = (maxSyncTimes - sd.sync_num)*interval_time;
  573. }
  574. //使用间隔来修正速度
  575. if(deltaT - 1.0 >= 0){
  576. //转为m/s
  577. cvx = (cx - sd.x)*this->map_scale/deltaT;
  578. cvy = (cy - sd.y)*this->map_scale/deltaT;
  579. //速度正负的判断:以x轴,y轴正向运动为正
  580. //如果x相等,则y2 - y1 > 0为正
  581. //其他情况,则x2 - x1 > 0 为正
  582. if(cx == sd.x){
  583. if(cy > sd.y){
  584. nSign = 1;
  585. }else{
  586. nSign = -1;
  587. }
  588. }else{
  589. if(cx > sd.x){
  590. nSign = 1;
  591. }else{
  592. nSign = -1;
  593. }
  594. }
  595. cv = sqrt(pow(cvx,2) + pow(cvy,2));
  596. cv = cv*nSign;
  597. double avx = (cvx - sd.vx) / deltaT;
  598. double avy = (cvy - sd.vy) / deltaT;
  599. double av = sqrt(pow(avx,2) + pow(avy,2));
  600. //车卡的加速度
  601. switch(this->card_type){
  602. case CT_PERSON:
  603. if(av > 3){
  604. this->x = this->last_locate.x;
  605. this->y = this->last_locate.y;
  606. return;
  607. }
  608. break;
  609. case CT_VEHICLE:
  610. if(av > 5){
  611. //保留上次结果
  612. this->x = this->last_locate.x;
  613. this->y = this->last_locate.y;
  614. return;
  615. }
  616. break;
  617. }
  618. this->last_locate.acceleration = av;
  619. this->last_vx = cvx;
  620. this->last_vy = cvy;
  621. cv = cv*3.6;
  622. //删除第一个元素到tmp(含)之间的所有元素
  623. bool bStartDel = false;
  624. for(list<sync_data>::reverse_iterator tmp = m_syncNumList.rbegin();tmp != m_syncNumList.rend();)
  625. {
  626. if(bStartDel){
  627. tmp = list<sync_data>::reverse_iterator(m_syncNumList.erase((++tmp).base()));
  628. }else{
  629. if(*tmp == sd){
  630. bStartDel = true;
  631. }
  632. ++tmp;
  633. }
  634. }
  635. //更新值为当前值并插入队列
  636. //sync_data sdNew;
  637. sdNew.sync_num = maxSyncTimes;
  638. this->m_nSyncNumInList = sd.sync_num;
  639. //sdNew.x = cx;
  640. //sdNew.y = cy;
  641. sdNew.vx = cvx;
  642. sdNew.vy = cvy;
  643. //m_syncNumList.push_back(sdNew);
  644. sdNew.update = true;
  645. #ifdef ALGORITHM_TYPE_INS
  646. uwb_state = nSign == 1?1:-1; //uwb形式判断不出静止或者怠速
  647. #endif
  648. }else{
  649. //cv = this->last_locate.v;
  650. cv = this->origin_locate.v;
  651. #ifdef ALGORITHM_TYPE_INS
  652. uwb_state = this->accelerate_state_last;
  653. #endif
  654. }
  655. }
  656. }
  657. #ifdef ALGORITHM_TYPE_INS
  658. else{
  659. bOriginLocateState = false;
  660. uwb_state = this->accelerate_state_last;
  661. }
  662. #endif
  663. #ifdef ALGORITHM_TYPE_INS
  664. bRet = false;
  665. switch(acce_state){
  666. case 0: //熄火
  667. case 1: //怠速
  668. //如果是熄火或者怠速状态,则将x,y方向的速度置为0,并送到卡尔曼滤波中
  669. cvx = 0;
  670. cvy = 0;
  671. bRet = true;
  672. break;
  673. case 2: //前进
  674. //如果根据加速度状态判断为前进,则取1
  675. ins_state = 1;
  676. break;
  677. case 3: //倒退
  678. //如果根据加速度状态判断为后退,则取-1
  679. ins_state = -1;
  680. break;
  681. }
  682. if(!bRet){
  683. double cweight = 0;
  684. cweight = this->ins_weight * ins_state + this->uwb_weight*uwb_state;
  685. //如果计算出的权重在合适范围内,就逐渐降低惯导的权限
  686. //如果计算出的权重超过范围,则重置惯导的权重为90%
  687. if(cweight>=INS_WEIGHT*-1&&cweight<=INS_WEIGHT){
  688. this->ins_weight = abs(cweight);
  689. }else{
  690. this->ins_weight = INS_WEIGHT;
  691. }
  692. this->weight = cweight;
  693. if(bOriginLocateState){
  694. if(cweight*uwb_state <= 0){
  695. //惯导和uwb定位方向不一致,定位失败
  696. //增加一次判断,与上一次的进行比对,如果和上一次的一致,则送进卡尔曼滤波
  697. if(cweight*this->accelerate_state_last > 0){
  698. bUseKalman = true;
  699. }else{
  700. bUseKalman = false;
  701. }
  702. }else{
  703. //惯导和uwb定位方向一致,定位成功
  704. //送进卡尔曼滤波
  705. bUseKalman = true;
  706. }
  707. }else{
  708. if(k*uwb_state > 0){
  709. //方向一致,但原始定位失败了
  710. //送进卡尔曼滤波
  711. bUseKalman = true;
  712. }else{
  713. //方向不一致,而且定位也失败了
  714. //不送了,直接取上一次的结果
  715. bUseKalman = false;
  716. }
  717. }
  718. }
  719. else{
  720. //不送到卡尔曼滤波
  721. //bUseKalman = true;
  722. bUseKalman = false;
  723. }
  724. #endif
  725. if(m_nFilterType == FILTER_KALMAN){
  726. #ifdef ALGORITHM_TYPE_INS
  727. if(bUseKalman){
  728. #endif
  729. double kalman_detal_t = (maxSyncTimes - this->last_locate.st)*interval_time;
  730. //通过卡尔曼滤波处理
  731. if(p->posx == INVALID_COORDINATE && p->posy == INVALID_COORDINATE){
  732. this->m_pKalmanFilter->m_bFlag = false;
  733. if(this->m_pKalmanFilter->m_nCounts < 3 || this->m_pKalmanFilter->m_pCar->P(0,0) > 2 || kalman_detal_t > 3){
  734. //P(0,0):连续时间(大于2s)都定位失败
  735. //deltaT>3:距离上次成功定位时间间隔为3s
  736. this->x = this->last_locate.x;
  737. this->y = this->last_locate.y;
  738. this->z = -3;
  739. return;
  740. }
  741. if(this->m_pKalmanFilter->m_nCounts >= 3){
  742. //只有三次以上才允许使用kalman滤波以下的函数
  743. //this->m_pKalmanFilter->Predict(deltaT);
  744. this->m_pKalmanFilter->Predict(kalman_detal_t);
  745. //this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  746. //this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  747. this->z = -4;
  748. sdNew.update = true;
  749. }
  750. }else{
  751. this->m_pKalmanFilter->m_bFlag = true;
  752. this->m_pKalmanFilter->m_nCounts++;
  753. this->m_pKalmanFilter->m_pCar->z(0,0) = cx * this->map_scale;
  754. this->m_pKalmanFilter->m_pCar->z(1,0) = cvx;
  755. this->m_pKalmanFilter->m_pCar->z(2,0) = cy * this->map_scale;
  756. this->m_pKalmanFilter->m_pCar->z(3,0) = cvy;
  757. if(this->m_pKalmanFilter->m_nCounts == 1){
  758. //第一次直接赋值
  759. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  760. }
  761. if(this->m_pKalmanFilter->m_nCounts == 2){
  762. //两次处理
  763. this->m_pKalmanFilter->m_pCar->z(1, 0) = (this->m_pKalmanFilter->m_pCar->z(0, 0) - this->m_pKalmanFilter->m_pCar->x(0, 0))/deltaT;
  764. this->m_pKalmanFilter->m_pCar->z(3, 0) = (this->m_pKalmanFilter->m_pCar->z(2, 0) - this->m_pKalmanFilter->m_pCar->x(2, 0))/deltaT;
  765. this->m_pKalmanFilter->m_pCar->x = this->m_pKalmanFilter->m_pCar->z;
  766. }
  767. if(this->m_pKalmanFilter->m_nCounts >= 3){
  768. //只有三次以上才允许使用kalman滤波以下的函数
  769. //this->m_pKalmanFilter->Predict_Correct(deltaT);
  770. this->m_pKalmanFilter->Predict_Correct(kalman_detal_t);
  771. sdNew.update = true;
  772. if(deltaT!=0){
  773. /*this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - this->last_locate.x)/deltaT;
  774. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - this->last_locate.y)/deltaT;*/
  775. this->m_pKalmanFilter->m_pCar->x(1,0) = (this->m_pKalmanFilter->m_pCar->x(0,0) - sd.x*this->map_scale)/deltaT;
  776. this->m_pKalmanFilter->m_pCar->x(3,0) = (this->m_pKalmanFilter->m_pCar->x(2,0) - sd.y*this->map_scale)/deltaT;
  777. }
  778. }
  779. }
  780. if(p->nFirstReader == -1 && p->nSecondReader == -1){
  781. p->nFirstReader = last_s_locate_reader[0];
  782. p->nSecondReader = last_s_locate_reader[1];
  783. }
  784. //增加地图集的判定,判断定位结果是否在地图集上
  785. //如果不在地图集上,需要再次定位
  786. //需要带出定位结果的分站信息,
  787. //利用地图集中分站信息再次定位
  788. POS kalman_p;
  789. kalman_p.nFirstReader = p->nFirstReader;
  790. kalman_p.nSecondReader = p->nSecondReader;
  791. kalman_p.posx = this->m_pKalmanFilter->m_pCar->x(0,0);//* this->map_scale
  792. kalman_p.posy = this->m_pKalmanFilter->m_pCar->x(2,0);//* this->map_scale
  793. kalman_p.pos_radius = p->pos_radius;
  794. if(!LocateAlgorithm::IsOnMap(&kalman_p,*pTdoaReaderPathMap)){
  795. //再一次定位到地图集上
  796. POS* cp = LocateAlgorithm::Pos(&kalman_p,*pTdoaReaderPathMap);
  797. if(cp != NULL){
  798. this->m_pKalmanFilter->m_pCar->x(0,0) = cp->posx;
  799. this->m_pKalmanFilter->m_pCar->x(2,0) = cp->posy;
  800. }
  801. if(cp){
  802. delete cp;
  803. cp = NULL;
  804. }
  805. }
  806. this->last_locate.x = this->x = this->m_pKalmanFilter->m_pCar->x(0,0) / this->map_scale;
  807. this->last_locate.y = this->y = this->m_pKalmanFilter->m_pCar->x(2,0) / this->map_scale;
  808. this->last_locate.z = this->z;
  809. this->last_locate.st = maxSyncTimes;
  810. last_s_locate_reader[0] = p->nFirstReader;
  811. last_s_locate_reader[1] = p->nSecondReader;
  812. this->m_pKalmanFilter->m_pCar->t = this->m_nLastLocateT = maxSyncTimes;
  813. this->v = sqrt(pow(this->m_pKalmanFilter->m_pCar->x(1,0),2) + pow(this->m_pKalmanFilter->m_pCar->x(3,0),2)); //*this->map_scale
  814. this->v*=3.6; //转为km/h
  815. cvx = this->m_pKalmanFilter->m_pCar->x(1,0);
  816. cvy = this->m_pKalmanFilter->m_pCar->x(3,0);
  817. nSign = 1;
  818. if(this->m_pKalmanFilter->m_pCar->x(1,0) == 0){
  819. if(this->m_pKalmanFilter->m_pCar->x(3,0)>0){
  820. nSign = 1;
  821. }
  822. }else{
  823. if(this->m_pKalmanFilter->m_pCar->x(1,0) > 0){
  824. nSign = 1;
  825. }else{
  826. nSign = -1;
  827. }
  828. }
  829. this->v*=nSign;
  830. this->last_locate.v = this->v;
  831. this->origin_locate.v = cv;
  832. if(sdNew.update){
  833. sdNew.sync_num = maxSyncTimes;
  834. sdNew.x = this->x;
  835. sdNew.y = this->y;
  836. sdNew.vx = cvx;
  837. sdNew.vy = cvy;
  838. m_syncNumList.push_back(sdNew);
  839. }
  840. #ifdef ALGORITHM_TYPE_INS
  841. }else{
  842. //这组数据的处理方法是:
  843. //如果第一次成功定位,但状态是静止,就取第一次成功定位值(这是为了处理当第一次成功定位,但状态是静止的,此时取上一次定位值为零的问题),
  844. //后续如果不管定位成功还是失败,只要状态是静止的,就输出上一次成功定位值,并更新同步序号。
  845. //取上次结果
  846. if(this->last_locate.x == 0 && this->last_locate.y == 0){
  847. if(this->map_scale > 0){
  848. this->last_locate.x = this->x = p->posx / (this->map_scale*1.0);
  849. this->last_locate.y = this->y = p->posy / (this->map_scale*1.0);
  850. this->last_locate.z = this->z = p->posz / (this->map_scale*1.0);
  851. this->last_locate.v = this->v = cv;
  852. }
  853. }else{
  854. this->x = this->last_locate.x;
  855. this->y = this->last_locate.y;
  856. this->z = this->last_locate.z;
  857. this->v = this->last_locate.v;
  858. }
  859. this->m_nLastLocateT = this->m_nCalcSyncNum = this->last_locate.st = this->sync_num = maxSyncTimes;
  860. if(sdNew.update){
  861. sdNew.sync_num = maxSyncTimes;
  862. sdNew.x = this->x;
  863. sdNew.y = this->y;
  864. sdNew.vx = cvx;
  865. sdNew.vy = cvy;
  866. m_syncNumList.push_back(sdNew);
  867. this->m_nSyncNumInList = maxSyncTimes;
  868. }
  869. this->is_deal_by_algo = true;
  870. }
  871. #endif
  872. }else{
  873. //TRACE(_T("no kalman . \n"));
  874. //最新通过算法算出的结果
  875. if(p->posx == INVALID_COORDINATE || p->posy == INVALID_COORDINATE){
  876. this->x = this->last_locate.x;
  877. this->y = this->last_locate.y;
  878. this->z = this->last_locate.z;
  879. }else{
  880. if(this->map_scale > 0){
  881. this->x = p->posx / (this->map_scale*1.0);
  882. this->y = p->posy / (this->map_scale*1.0);
  883. this->z = p->posz / (this->map_scale*1.0);
  884. }
  885. this->v = cv;
  886. this->last_locate.x = this->x;
  887. this->last_locate.y = this->y;
  888. this->last_locate.z = this->z;
  889. this->last_locate.v = this->v;
  890. this->m_nLastLocateT = this->last_locate.st = maxSyncTimes;
  891. }
  892. this->a = 0;
  893. }
  894. }
  895. if(p){
  896. delete p;
  897. p = NULL;
  898. }
  899. pRdm.clear();
  900. #else
  901. #endif
  902. if(_isnan(this->x) || _isnan(this->y) || _isnan(this->z)){
  903. this->x = this->last_x;
  904. this->y = this->last_y;
  905. this->z = this->last_z;
  906. }
  907. }
  908. void Card::get_coordinate()
  909. {
  910. get_coordinate(get_effictive_dist_count());
  911. }
  912. std::string Card::get_state_text()
  913. {
  914. string ret = "";
  915. state = 0;
  916. state_biz = 0;
  917. if(status_help == STATUS_ERROR){
  918. state += STATUS_HELP;
  919. state_biz += STATUS_HELP;
  920. ret += "呼救,";
  921. }
  922. if(status_area_over_time == STATUS_ERROR){
  923. state += STATUS_AREA_OVER_TIME;
  924. state_biz += STATUS_AREA_OVER_TIME;
  925. ret += "区域超时,";
  926. }else if(status_area_over_time == STATUS_ERROR){
  927. state += STATUS_OVER_TIME;
  928. state_biz += STATUS_OVER_TIME;
  929. ret += "超时,";
  930. }
  931. if(status_area_over_speed == STATUS_ERROR){
  932. state += STATUS_AREA_OVER_SPEED;
  933. state_biz += STATUS_AREA_OVER_SPEED;
  934. ret += "区域超速,";
  935. }else if(status_over_speed == STATUS_ERROR){
  936. state += STATUS_OVER_SPEED;
  937. state_biz += STATUS_OVER_SPEED;
  938. ret += "超速,";
  939. }
  940. if(status_area_forbidden == STATUS_ERROR){
  941. state += STATUS_AREA_FORBIDDEN;
  942. state_biz += STATUS_AREA_FORBIDDEN;
  943. ret += "进入限制区域,";
  944. }
  945. if(status_call == STATUS_ERROR){
  946. state += STATUS_CALL;
  947. state_biz += STATUS_CALL;
  948. ret += "呼叫,";
  949. }
  950. if(status_lost == STATUS_ERROR){
  951. state += STATUS_LOST;
  952. state_biz += STATUS_LOST;
  953. ret += "进入盲区,";
  954. }
  955. if(power_state == STATUS_ERROR){
  956. state += STATUS_POWER_LOWER;
  957. ret += "电量低,";
  958. }else if(power_state == STATUS_ERROR_SERIOUS){
  959. state += STATUS_POWER_LOWER_SERIOUS;
  960. ret += "电量极低,";
  961. }
  962. if(ret.length() > 0){
  963. ret = ret.substr(0, ret.length() - 1);
  964. }else{
  965. ret = "正常";
  966. }
  967. return ret;
  968. }
  969. std::string Card::get_acc_text()
  970. {
  971. string ret = "";
  972. state_moving = (accelerate_state && 0x01)? STATUS_ERROR : STATUS_NORMAL;
  973. if(state_moving == STATUS_NORMAL){
  974. ret += "静止";
  975. }else if(state_moving == STATUS_ERROR){
  976. ret += "运动";
  977. }
  978. return ret;
  979. }
  980. std::string Card::concat( int reader_id, int ant_id )
  981. {
  982. char s[10];
  983. sprintf_s(s, "%d-%d", reader_id, ant_id);
  984. return s;
  985. }
  986. /*
  987. * 滤波功能设置
  988. *
  989. * param
  990. * nType ------ 滤波类型
  991. *
  992. * return
  993. * 无
  994. */
  995. void Card::EnableFilter(int nType)
  996. {
  997. //如果无滤波类型直接返回
  998. if(nType == NO_FILTER){
  999. return;
  1000. }
  1001. //开启滤波功能,设置滤波类型
  1002. m_bUseFilter = TRUE;
  1003. m_nFilterType = nType;
  1004. switch(nType){
  1005. case FILTER_KALMAN:
  1006. //分配卡尔曼滤波类型变量并初始化参数
  1007. if(m_pKalmanFilter == NULL){
  1008. m_pKalmanFilter = new CKalmanFilter();
  1009. m_pKalmanFilter->Initial(0.2);
  1010. m_pKalmanFilter->m_bFlag = false;
  1011. }
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. }
  1017. void Card::remove_dist_head()
  1018. {
  1019. DistMap tmp = _dists.front().distmap;
  1020. if(tmp.size() > 0 ){
  1021. DistMap::iterator it_mp_dist = tmp.begin();
  1022. while(it_mp_dist != tmp.end()){
  1023. delete it_mp_dist->second;
  1024. it_mp_dist->second = NULL;
  1025. tmp.erase(it_mp_dist++);
  1026. }
  1027. }
  1028. _dists.pop_front();
  1029. //_dists.erase(_dists.begin());
  1030. }
  1031. Reader::Reader(void)
  1032. {
  1033. rec_time = reader_time = lost_time = time(NULL);
  1034. map_scale = 1.0;
  1035. sync_level = 0xFF;
  1036. state = STATUS_DEVICE_NORMAL;
  1037. for(int i = 0;i < ANTENNA_COUNT;i++){
  1038. ant[i] = NULL;
  1039. }
  1040. for(int i = 0;i < ADHOC_COUNT;i++){
  1041. adhoc[i] = NULL;
  1042. }
  1043. bIsInitCoverage = false;
  1044. reader_interval_time = 0.0;
  1045. m_nIsSpecial = -1;
  1046. }
  1047. Reader::~Reader(void)
  1048. {
  1049. for(int i = 0;i < ANTENNA_COUNT;i++){
  1050. Antenna* tmp = NULL;
  1051. tmp = ant[i];
  1052. if(tmp){
  1053. delete tmp;
  1054. tmp = NULL;
  1055. ant[i] = NULL;
  1056. }
  1057. }
  1058. for(int i = 0; i < ADHOC_COUNT;i++){
  1059. Adhoc * tmp = NULL;
  1060. tmp = adhoc[i];
  1061. if(tmp){
  1062. delete tmp;
  1063. tmp = NULL;
  1064. adhoc[i] = NULL;
  1065. }
  1066. }
  1067. }
  1068. std::string Reader::get_state_text()
  1069. {
  1070. string ret = "";
  1071. if(state == STATUS_DEVICE_ERROR){
  1072. ret = "故障";
  1073. }else if(state == STATUS_DEVICE_NORMAL){
  1074. ret = "正常";
  1075. }
  1076. return ret;
  1077. }
  1078. Antenna::Antenna(void)
  1079. {
  1080. antenna_angle = 0;
  1081. antenna_id = 0;
  1082. antenna_x = 0;
  1083. antenna_y = 0;
  1084. antenna_z = 0;
  1085. }
  1086. Antenna::~Antenna(void)
  1087. {
  1088. }
  1089. Area::Area(void)
  1090. {
  1091. is_att = 1;
  1092. polygon_count = 0;
  1093. polygon = NULL;
  1094. map_id = area_id = area_type_id = 0 ;
  1095. area_name = area_type_name = path = "";
  1096. over_count_person = over_time_person = under_count_person = under_time_person = 0;
  1097. over_count_vehicle = over_time_vehicle = under_count_vehicle = under_time_vehicle = 0;
  1098. count_person = count_vehicle = count_card = 0;
  1099. is_area_over_time_person = is_area_over_time_vehicle = false;
  1100. count_area_over_time_person = count_area_over_time_vehicle = 0;
  1101. time_over_time_person = time_over_time_vehicle = time(NULL);
  1102. is_area_over_count_person = is_area_over_count_vehicle = false;
  1103. count_area_over_count_person = count_area_over_count_vehicle = 0;
  1104. time_over_count_person = time_over_count_vehicle = time(NULL);
  1105. is_area_forbidden_person = is_area_forbidden_vehicle = 0;
  1106. count_area_forbidden_person = count_area_forbidden_vehicle = 0;
  1107. time_forbidden_person = time_forbidden_vehicle = time(NULL);
  1108. }
  1109. Area::~Area(void)
  1110. {
  1111. if(polygon){
  1112. delete[] polygon;
  1113. polygon = NULL;
  1114. }
  1115. }
  1116. void Area::init_border(string sz_path)
  1117. {
  1118. //std::vector<std::string> vec = split(sz_path, ",");
  1119. //if(vec.size() >= 4){
  1120. // rect_left = atoi(vec[0].c_str());
  1121. // rect_top = atoi(vec[1].c_str());
  1122. // rect_right = atoi(vec[2].c_str());
  1123. // rect_bottom = atoi(vec[3].c_str());
  1124. //}else{
  1125. // rect_left = rect_right = rect_top = rect_bottom = 0;
  1126. //}
  1127. if(sz_path == ""){
  1128. return ;
  1129. }
  1130. std::vector<std::string> vec = split(sz_path, " ");
  1131. std::vector<std::string>::iterator it = vec.begin();
  1132. if(polygon){
  1133. delete[] polygon;
  1134. polygon = NULL;
  1135. }
  1136. polygon = new _point[vec.size()];
  1137. polygon_count = 0;
  1138. for(; it != vec.end(); ++it){
  1139. std::vector<std::string> subvec = split(it->c_str(), ",");
  1140. _point p;
  1141. p.x = get_vertex(subvec[0]);
  1142. p.y = get_vertex(subvec[1]);
  1143. p.z = 0;
  1144. polygon[polygon_count] = p;
  1145. polygon_count++;
  1146. }
  1147. }
  1148. std::vector<std::string> Area::split( std::string str,std::string pattern )
  1149. {
  1150. std::string::size_type pos;
  1151. std::vector<std::string> result;
  1152. str+=pattern;//扩展字符串以方便操作
  1153. unsigned int size=str.size();
  1154. for(unsigned int i=0; i<size; i++){
  1155. pos=str.find(pattern,i);
  1156. if(pos<size){
  1157. std::string s=str.substr(i,pos-i);
  1158. result.push_back(s);
  1159. i=pos+pattern.size()-1;
  1160. }
  1161. }
  1162. return result;
  1163. }
  1164. double Area::get_vertex( std::string src)
  1165. {
  1166. std::string dest = "";
  1167. for(unsigned int i = 0; i < src.length(); i++){
  1168. if((src[i] >= '0' && src[i]<='9') || src[i]=='-' || src[i] == '.'){
  1169. dest += src[i];
  1170. }
  1171. }
  1172. return atof(dest.c_str());
  1173. }
  1174. bool Area::is_in_polygon( _point p )
  1175. {
  1176. if(polygon == NULL){
  1177. return false;
  1178. }
  1179. int counter = 0;
  1180. int i;
  1181. double xinters;
  1182. _point p1,p2;
  1183. p1 = polygon[0];
  1184. for (int i=1;i<= polygon_count;i++) {
  1185. p2 = polygon[i % polygon_count];
  1186. if (p.y > MIN(p1.y,p2.y)) {
  1187. if (p.y <= MAX(p1.y,p2.y)) {
  1188. if (p.x <= MAX(p1.x,p2.x)) {
  1189. if (p1.y != p2.y) {
  1190. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  1191. if (p1.x == p2.x || p.x <= xinters)
  1192. counter++;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. p1 = p2;
  1198. }
  1199. //TRACE(_T("counter : %d \n"),counter);
  1200. return (counter % 2 == 0) ? false : true;
  1201. }
  1202. int Card::FindDistMap( int cardstamp )
  1203. {
  1204. int idx = -1;
  1205. for(int i = _dists.size() - 1; i >= 0; i--){
  1206. if(_dists[i].cardstamp == cardstamp ){
  1207. return i;
  1208. }
  1209. }
  1210. return idx;
  1211. }
  1212. bool Card::is_pos_state_pack_changed( int nval )
  1213. {
  1214. if(this->pos_state_park == this->pos_state_park_old){
  1215. if(this->pos_state_park_count < nval){
  1216. this->pos_state_park_count++;
  1217. }
  1218. return false;
  1219. }
  1220. //if(this->pos_state_park_count < nval ){ // 未达到确认次数
  1221. // this->pos_state_park_count++;
  1222. // return false;
  1223. //}
  1224. this->pos_state_park_old = this->pos_state_park;
  1225. this->pos_state_park_count = 1;
  1226. return true;
  1227. }
  1228. MapInfo::MapInfo( void )
  1229. {
  1230. }
  1231. MapInfo::~MapInfo( void )
  1232. {
  1233. }
  1234. Dept::Dept( int id, string name )
  1235. {
  1236. dept_id = id;
  1237. dept_name = name;
  1238. }
  1239. Dept::Dept()
  1240. {
  1241. }
  1242. Dept::~Dept()
  1243. {
  1244. }
  1245. Adhoc::Adhoc()
  1246. {
  1247. adhoc_id = 0;
  1248. x = 0;
  1249. y = 0;
  1250. z = 0;
  1251. idx = 0;
  1252. }
  1253. Adhoc::~Adhoc()
  1254. {
  1255. }