locate_algorithm.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. #include "stdafx.h"
  2. #include "locate_algorithm.h"
  3. #include <math.h>
  4. #include <algorithm>
  5. #include "../common/matrix/_Matrix.h"
  6. #include "../common/Functions/Functions.h"
  7. #include "ProcessRemodule.h"
  8. #include "log_process_module.h"
  9. using namespace std;
  10. // ant 天线, tar定位目标, d 测距
  11. void AOAformula( struct _coordinate* ant, double angle, struct _coordinate* tar, bool is_line /*= false*/, double z_offset /*= 0*/ )
  12. {
  13. double aa = sin(angle);
  14. double bb = cos(angle);
  15. double h = ant->z;
  16. if(is_line){
  17. h = z_offset;
  18. }
  19. tar->x = ant->x + sqrt(ant->d * ant->d - h * h) * cos(angle);
  20. tar->y = ant->y + sqrt(ant->d * ant->d - h * h) * sin(angle);
  21. tar->z = ant->z; // 高度
  22. tar->a = ant->a; // 角度
  23. tar->t = ant->t; // 时间戳
  24. tar->d_offset = ant->d_offset;
  25. tar->reader_id = ant->reader_id;
  26. tar->d = ant->d;
  27. }
  28. // ant 天线, last 上次坐标
  29. double Distanceformula( struct _coordinate* ant, struct _coordinate* last )
  30. {
  31. double x0,y0;
  32. double tan_diff = tan(last->a) - tan(ant->a);
  33. double ret = 0;
  34. if(tan_diff == 0){
  35. ret = sqrt((ant->x - last->x) * (ant->x - last->x) + (ant->y - last->y) * (ant->y - last->y));
  36. }else{
  37. x0 = (last->x * tan(last->a) - ant->x * tan(ant->a) + ant->y - last->y) / (tan(last->a) - tan(ant->a));
  38. y0 = (((last->x - ant->x) * tan(last->a) * tan(ant->a)) + ant->y * tan(last->a) - last->y * tan(ant->a)) / (tan(last->a) - tan(ant->a));
  39. ret = sqrt((x0 - ant->x) * (x0 - ant->x) + (y0 - ant->y) * (y0 - ant->y))
  40. + sqrt((x0 - last->x) * (x0 - last->x) + (y0 - last->y) * (y0 - last->y));
  41. }
  42. // n = Nodeformula( n1, n2);
  43. return ret;
  44. }
  45. // 接收到的新数据
  46. void algorithm_locate( struct _coordinate** coor_list, int coor_size, struct _coordinate* hist, struct _coordinate* dest, double dist_limit, double z_offset)
  47. {
  48. double d0; //历史定位坐标与实时定位坐标之间的距离;
  49. double dN; //利用速率V和时间戳理论计算出行进距离;
  50. double Angle1,Angle2,Angle3; //三个坐标时,任何2坐标的角度
  51. double Angle; //两个坐标之间的角度
  52. double dLong;
  53. double dLenth;
  54. int position; //定位点位置
  55. switch(coor_size){
  56. case 1: // 1条定位数据,5.3.1
  57. {
  58. if(coor_list[0]->d > dist_limit){ // 如果测距的距离大于某个固定值,此时判断此位置与天线角度一致
  59. //coor_list[0]->d -= coor_list[0]->d_offset;
  60. AOAformula(coor_list[0], coor_list[0]->a, dest, true, z_offset);
  61. }else { // 测距距离小于某值
  62. //利用速率V和时间戳理论计算出行进距离
  63. dN = coor_list[0]->v * (coor_list[0]->t - hist->t);
  64. //计算实时定位分站(天线)坐标和历史定位坐标之间的距离
  65. d0 = Distanceformula(coor_list[0], hist);
  66. if(hist->x - coor_list[0]->x == 0){
  67. Angle = (coor_list[0]->y >= hist->y) ? 90 : 270;
  68. Angle = Angle * M_PI / 180;
  69. }else{
  70. //计算历史坐标与实时分站坐标的角度,用来表明历史坐标位于定位坐标的方向
  71. Angle = atan((hist->y - coor_list[0]->y)/(hist->x - coor_list[0]->x));
  72. }
  73. // 与历史坐标同向
  74. //如果定位距离小于两个坐标之间的距离,说明定位位置在两个坐标之内
  75. if(d0 >= coor_list[0]->d){
  76. if(dN >= coor_list[0]->d){
  77. Angle += M_PI; //与历史坐标反方向
  78. }
  79. }else{
  80. //如果定位距离大于两个坐标之间的距离,说明定位位置在两个坐标之外,接下来只要判断在哪个yu坐标的位置即可
  81. //如果理论进行距离大于两者坐标之间的距离,则说明与历史坐标在同一方向,反之在反方向
  82. if(dN < coor_list[0]->d){
  83. Angle += M_PI; //与历史坐标反方向
  84. }
  85. }
  86. //coor_list[0]->d -= coor_list[0]->d_offset;
  87. AOAformula(coor_list[0], Angle, dest, true, z_offset); //最终定位坐标计算
  88. }
  89. break;
  90. }
  91. case 2: //2条定位数据,5.3.2
  92. {
  93. //2条定位数据中分站(天线)坐标与历史定位坐标之间的角度,以此推出这三个坐标是否在一条直线上或者呈三角形
  94. if(coor_list[0]->x == hist->x ){
  95. Angle1 = (coor_list[0]->y == hist->y) ? 0 : ((coor_list[0]->y > hist->y) ? 90 : 270);
  96. Angle1 = Angle1 * M_PI / 180;
  97. }else{
  98. Angle1 = atan((coor_list[0]->y - hist->y) / (coor_list[0]->x - hist->x));
  99. }
  100. if(coor_list[1]->x == hist->x ){
  101. Angle2 = (coor_list[1]->y == hist->y) ? 0 : ((coor_list[1]->y > hist->y)? 90 : 270);
  102. Angle2 = Angle2 * M_PI / 180;
  103. }else{
  104. Angle2 = atan((coor_list[1]->y - hist->y) / (coor_list[1]->x - hist->x));
  105. }
  106. if(coor_list[1]->x == coor_list[0]->x ){
  107. Angle3 = (coor_list[1]->y >= coor_list[0]->y) ? 90 : 270; // 必须在不同点
  108. Angle3 = Angle3 * M_PI / 180;
  109. }else{
  110. Angle3 = atan((coor_list[1]->y - coor_list[0]->y) / (coor_list[1]->x - coor_list[0]->x));
  111. if(Angle3 > M_PI){
  112. Angle3 -= M_PI;
  113. }
  114. /*if(coor_list[0]->x > coor_list[1]->x)
  115. Angle3 += M_PI;*/
  116. }
  117. //2个角度相同或者相差180度说明在一条直线上
  118. //if(coor_list[0]->a == coor_list[1]->a || coor_list[0]->a == coor_list[1]->a + M_PI || coor_list[0]->a == coor_list[1]->a - M_PI){
  119. if(Angle1 == Angle2 || Angle1 == Angle2 + M_PI || Angle1 == Angle2 - M_PI){
  120. //取两个距离之间的最大值,保存最短距离的数据作为定位点
  121. dLong = max(coor_list[0]->d, coor_list[1]->d);
  122. position = (coor_list[0]->d > coor_list[1]->d) ? 1 : 0;
  123. dLenth = Distanceformula(coor_list[0], coor_list[1]);
  124. if(dLong > dLenth){
  125. Angle = Angle3 + ((position + 1)%2) * M_PI;
  126. }else{
  127. Angle = Angle3 + position * M_PI;
  128. }
  129. //2个坐标的距离小于其2个距离中的最大值,则说明定位点在2个坐标的外侧,
  130. //且在距离短的那条作为定位坐标,定位角度与此相同
  131. //2个坐标的距离大于这两条距离中的任何一个,则说明定位点在两个坐标之间,取任意一条作为定位点,定位角度与其角度相反(+180)
  132. // AOAformula(coor_list[position], Angle, dest); //最终定位坐标计算
  133. // 根据测量的结果,调整距离d
  134. // x2 - (d2 - ((d1+d2) - (x2-x1))/2) 靠近x2 x2 > x1,d1 > d2
  135. // x1 + (d1 - ((d1+d2) - (x2-x1))/2) 靠近x1 x2 > x1,d1 < d2
  136. // 化简得到 (x1 + x2 + d1 - d2)/2
  137. // 显示的d = ((d1 + d2) - abs(x2-x1))/2
  138. double d_offset;
  139. if(fabs(coor_list[0]->x - coor_list[1]->x) > fabs(coor_list[0]->y - coor_list[1]->y)){ // 水平方向
  140. d_offset = (coor_list[0]->d + coor_list[1]->d - fabs(coor_list[0]->x - coor_list[1]->x))/2;
  141. }else{ // 垂直方向
  142. d_offset = (coor_list[0]->d + coor_list[1]->d - fabs(coor_list[0]->y - coor_list[1]->y))/2;
  143. }
  144. dest->d_offset = d_offset;
  145. coor_list[0]->d_offset = d_offset;
  146. coor_list[1]->d_offset = d_offset;
  147. coor_list[position]->d -= d_offset;
  148. AOAformula(coor_list[position], Angle, dest, true, z_offset);
  149. }
  150. //岔路口形式
  151. else {
  152. dLenth = Distanceformula(coor_list[0], coor_list[1]); //2个天线坐标之间的距离
  153. if(dLenth > (coor_list[0]->d + coor_list[1]->d)) { //取一条作为定位参考数据,采用1条定位数据算法,参考5.3.1
  154. //如果测距的距离大于某个固定值,此时判断此位置与天线角度一致,
  155. if(coor_list[0]->d > dist_limit){
  156. AOAformula(coor_list[0], coor_list[0]->a, dest); //最终定位坐标计算
  157. }
  158. //测距距离小于某值
  159. else{
  160. dN = coor_list[0]->v * ( hist->t - coor_list[0]->t); //利用速率V和时间戳理论计算出行进距离;
  161. d0 = Distanceformula(coor_list[0], hist); //计算实时定位分站(天线)坐标和历史定位坐标之间的距离
  162. if(hist->x == coor_list[0]->x){
  163. Angle = (hist->y >= coor_list[0]->y) ? 90 : 270; //计算历史坐标与实时分站坐标的角度,用来表明历史坐标位于定位坐标的方向
  164. Angle = Angle * M_PI / 180;
  165. }else{
  166. Angle = atan((hist->y - coor_list[0]->y) / (hist->x - coor_list[0]->x));
  167. }
  168. //如果定位距离小于两个坐标之间的距离,说明定位位置在两个坐标之内
  169. if(d0 > coor_list[0]->d){
  170. if(dN >= coor_list[0]->d){
  171. Angle += M_PI; //与历史坐标反方向
  172. }
  173. }
  174. else{
  175. //如果定位距离大于两个坐标之间的距离,说明定位位置在两个坐标之外,接下来只要判断在哪个坐标的位置即可
  176. //如果理论进行距离大于两者坐标之间的距离,则说明与历史坐标在同一方向,反之在反方向
  177. if(dN < coor_list[0]->d){
  178. Angle += M_PI;
  179. }
  180. }
  181. AOAformula(coor_list[0], Angle, dest, true, z_offset); //最终定位坐标计算
  182. }
  183. }
  184. else { //采用2条定位数据算法,采用5.3.2
  185. //取两个距离之间的最大值,保存最短距离的数据作为定位点
  186. dLong = max(coor_list[0]->d, coor_list[1]->d);
  187. position = (coor_list[0]->d > coor_list[1]->d) ? 1 : 0;
  188. dLenth = Distanceformula(coor_list[0], coor_list[1]);
  189. //2个坐标的距离小于其2个距离中的最大值,则说明定位点在2个坐标的外侧,
  190. //且在距离短的那条作为定位坐标,定位角度与此相同
  191. //2个坐标的距离大于这两条距离中的任何一个,则说明定位点在两个坐标之间,取任意一条作为定位点,定位角度与其角度相反
  192. if(dLong > dLenth){
  193. Angle = Angle3 + ((position + 1)%2) * M_PI;
  194. }else{
  195. Angle = Angle3 + position * M_PI;
  196. }
  197. AOAformula(coor_list[position], Angle, dest, true, z_offset); //最终定位坐标计算
  198. }
  199. }
  200. break;
  201. }
  202. case 3: // 3条定位数据,三点定位法
  203. {
  204. // 在同一直线上不能使用三点定位
  205. locatebycordinate(coor_list, dest);
  206. break;
  207. }
  208. case 4: // 4条数据,取前3条
  209. {
  210. locatebycordinate(coor_list, dest);
  211. break;
  212. }
  213. default:
  214. {
  215. dest->x = -1;
  216. dest->y = -1;
  217. dest->a = 0;
  218. break;
  219. }
  220. }
  221. }
  222. void locatebycordinate( struct _coordinate** coor_list, struct _coordinate* dest )
  223. {
  224. double h = 0;
  225. int i = 0;
  226. int j = 0;
  227. double temp = 0;
  228. double x[3]; // 坐标
  229. double y[3]; // 坐标
  230. double z[3];
  231. double d[3];
  232. double p[2];
  233. _Matrix pi;
  234. matrix_set(&pi,3,2);
  235. matrix_init(&pi);
  236. for(i=0;i<3;i++){
  237. x[i] = coor_list[i]->x;
  238. y[i] = coor_list[i]->y;
  239. z[i] = coor_list[i]->z;
  240. d[i] = coor_list[i]->d; // d * CM_PIX * 100;
  241. }
  242. //获得坐标
  243. matrix_write(&pi,0,0,x[0]);
  244. matrix_write(&pi,0,1,y[0]);
  245. h = z[0];
  246. d[0] = sqrt(d[0] * d[0] - h * h);
  247. matrix_write(&pi,1,0,x[1]);
  248. matrix_write(&pi,1,1,y[1]);
  249. h = z[1];
  250. d[1] = sqrt(d[1] * d[1]- h * h);
  251. matrix_write(&pi,2,0,x[2]);
  252. matrix_write(&pi,2,1,y[2]);
  253. h = z[2];
  254. d[2] = sqrt(d[2] * d[2] - h * h);
  255. if (locate(&pi,d,p) > 0)
  256. {
  257. dest->x = p[0];
  258. dest->y = p[1];
  259. }else
  260. {
  261. dest->x = -1;
  262. dest->y = -1;
  263. }
  264. }
  265. void algorithm_locate_ex( _coordinate** coor_list, int coor_size, _coordinate* hist, _coordinate* dest, double dist_limit )
  266. {
  267. switch (coor_size)
  268. {
  269. case 1:
  270. {
  271. if(!hist){ // 初次计算,只有一个分站数据的时候跳出,不计算坐标
  272. return;
  273. }
  274. // 获取历史节点与当前分站的角度,判断是否仍在同一直线
  275. double angle = getAngleByAngle(coor_list[0], hist);
  276. if(0 == tan(angle)){ // 在同一直线
  277. double dist_temp = getDistance(hist, coor_list[0]); // 获得上一次的距离
  278. // 获得移动的距离
  279. double dist_mv = fabs(dist_temp - coor_list[0]->d);
  280. // 沿着原移动方向运动
  281. calcCoordinate(hist, dist_mv, hist->a, dest); //
  282. }else{ // 有角度,找交点
  283. _coordinate* pt_cross = getCross(hist, coor_list[0]);
  284. double dist_temp = getDistance(pt_cross, coor_list[0]);
  285. if(dist_temp >= coor_list[0]->d){
  286. // 避免动画穿墙,先将该点定位到交点上
  287. dest->x = pt_cross->x;
  288. dest->y = pt_cross->y;
  289. dest->z = coor_list[0]->z;
  290. dest->a = getAngleByCoordinate(pt_cross, coor_list[0]); // 运行方向
  291. }else{ // 在当前方向上运行, 理论上不存在
  292. dist_temp = (getDistance(hist, pt_cross) - (coor_list[0]->d - dist_temp)); // 位移
  293. calcCoordinate(hist, dist_temp, hist->a, dest);
  294. }
  295. }
  296. break;
  297. }
  298. case 2:
  299. {
  300. double angle_anc = getAngleByAngle(coor_list[0], coor_list[1]);
  301. if(0 == tan(angle_anc)){ // 在同一直线
  302. double dist_anc = getDistance(coor_list[0], coor_list[1]);
  303. double dist_offset;
  304. if(dist_anc < coor_list[0]->d || dist_anc < coor_list[1]->d ) {//分站到标识卡的距离大于分站之间的距离,同一侧
  305. dist_offset = fabs(coor_list[0]->d - coor_list[1]->d) - dist_anc;
  306. double angle_anc_run = getAngleByCoordinate(coor_list[0], coor_list[1]); // 分站2一侧
  307. if(coor_list[0]->d < coor_list[1]->d){ // 分站1一侧
  308. angle_anc_run += M_PI;
  309. }
  310. calcCoordinate(coor_list[0], coor_list[0]->d - dist_offset / 2, angle_anc_run, dest);
  311. }else{ //在分站之间
  312. dist_offset = coor_list[0]->d + coor_list[1]->d - dist_anc;
  313. calcCoordinate(coor_list[0], coor_list[0]->d + dist_offset / 2, getAngleByCoordinate(coor_list[0], coor_list[1]), dest);
  314. }
  315. }else{ // 有夹角, 一定在两分站之间
  316. if(hist){
  317. double angle_anc1 = getAngleByAngle(coor_list[0], hist);
  318. double angle_anc2 = getAngleByAngle(coor_list[1], hist);
  319. // 找到交点
  320. _coordinate* pt_cross = getCross(coor_list[0], coor_list[1]);
  321. // 交点到分站的距离
  322. double dist_anc1 = getDistance(pt_cross, coor_list[0]);
  323. double dist_anc2 = getDistance(pt_cross, coor_list[1]);
  324. if(0 == tan(angle_anc1)){
  325. if(dist_anc1 >= coor_list[0]->d){ // 以直线上为准
  326. calcCoordinate(hist, fabs(dist_anc1 - coor_list[0]->d), hist->a, dest);
  327. //calcCoordinate(coor_list[0], coor_list[0]->d, getAngleByCoordinate(coor_list[0], hist), dest);
  328. }else{ // 超过直接定位到交点上
  329. dest->x = pt_cross->x;
  330. dest->y = pt_cross->y;
  331. dest->a = getAngleByCoordinate(pt_cross, coor_list[1]);
  332. }
  333. }else if(0 == tan(angle_anc2)){ // 与分站1在同一直线
  334. if(dist_anc2 >= coor_list[1]->d){ // 以直线上为准
  335. calcCoordinate(hist, fabs(dist_anc2 - coor_list[1]->d), hist->a, dest);
  336. //calcCoordinate(coor_list[1], coor_list[1]->d, getAngleByCoordinate(coor_list[1], hist), dest);
  337. }else{ // 超过直接定位到交点上
  338. dest->x = pt_cross->x;
  339. dest->y = pt_cross->y;
  340. dest->a = getAngleByCoordinate(pt_cross, coor_list[0]);
  341. }
  342. }else { // 三叉口
  343. // 找到交点
  344. _coordinate* pt_cross1 = getCross(hist, coor_list[0]);
  345. _coordinate* pt_cross2 = getCross(hist, coor_list[1]);
  346. if(coor_list[0]->d >= coor_list[1]->d){
  347. dest->x = pt_cross2->x;
  348. dest->y = pt_cross2->y;
  349. dest->z = coor_list[1]->z;
  350. dest->a = coor_list[1]->a;
  351. }else{
  352. dest->x = pt_cross1->x;
  353. dest->y = pt_cross1->y;
  354. dest->z = coor_list[0]->z;
  355. dest->a = coor_list[0]->a;
  356. }
  357. }
  358. }else{ // 先定位到交点,避免穿墙
  359. _coordinate* pt_cross = getCross(coor_list[0], coor_list[1]);
  360. dest->x = pt_cross->x;
  361. dest->y = pt_cross->y;
  362. // 比较距离,定位到距离近的方向上
  363. if(fabs(getDistance(pt_cross, coor_list[0]) - coor_list[0]->d) >= fabs(getDistance(pt_cross, coor_list[1]) - coor_list[1]->d)){
  364. dest->z = coor_list[1]->z;
  365. dest->a = coor_list[1]->a;
  366. }else{
  367. dest->z = coor_list[0]->z;
  368. dest->a = coor_list[0]->a;
  369. }
  370. }
  371. }
  372. break;
  373. }
  374. case 3:
  375. {
  376. // 判断分站是否在同一直线上
  377. if(is_all_inline(coor_list, coor_size)){
  378. algorithm_locate_ex(coor_list, 2, hist, dest, dist_limit);
  379. break;
  380. }
  381. locatebycordinate(coor_list, dest);
  382. break;
  383. }
  384. case 4:
  385. {
  386. // 判断分站是否在同一直线上
  387. if(is_all_inline(coor_list, coor_size)){
  388. algorithm_locate_ex(coor_list, 2, hist, dest, dist_limit);
  389. break;
  390. }
  391. locatebycordinate(coor_list, dest);
  392. break;
  393. }
  394. default:
  395. break;
  396. }
  397. }
  398. double getAngleByAngle( _coordinate* p1, _coordinate* p2 )
  399. {
  400. if(p1->x == p2->x) return 0;
  401. if(p1->y == p2->y) return 0;
  402. return double(p2->a - p1->a);
  403. }
  404. double getAngleByCoordinate( _coordinate* p1, _coordinate* p2 )
  405. {
  406. if(p1->x == p2->x && p1->y == p2->y){ // 如果坐标相等,则跨越,不考虑折返情况
  407. return p1->a;
  408. }
  409. if(p1->x == p2->x){
  410. if(p1->y >= p2->y){
  411. return M_PI_2 * 3;
  412. }
  413. return M_PI_2;
  414. }
  415. if(p1->y == p2->y){
  416. if(p1->x > p2->x){
  417. return M_PI;
  418. }
  419. return 0;
  420. }
  421. return atan((p2->y - p1->y) / (p2->x - p1->x));
  422. }
  423. _coordinate* getCross( _coordinate* p1, _coordinate* p2 )
  424. {
  425. _coordinate* p = new _coordinate;
  426. double b1 = p1->y - tan(p1->a) * p1->x;
  427. double b2 = p2->y - tan(p2->a) * p2->x;
  428. if(p1->a == 0 || p1->a == M_PI){ // 第一个点水平
  429. p->y = p1->y;
  430. if(p2->a == M_PI_2 || p2->a == M_PI_2 * 3){ // 第二个点垂直
  431. p->x = p2->x;
  432. }else{ // 第二个点有角度
  433. p->x = (p->y - b2) / tan(p2->a);
  434. }
  435. return p;
  436. }else if(p1->a == M_PI_2 || p1->a == M_PI_2 * 3){ // 第一个点垂直
  437. p->x = p1->x;
  438. if(p2->a == 0 || p2->a == M_PI){ // 第二个点水平
  439. p->y = p2->y;
  440. }else{ // 第二个点有角度
  441. p->y = tan(p2->a) * p->x + b2;
  442. }
  443. return p;
  444. }else { // 有角度
  445. if(p2->a == 0 || p2->a == M_PI){ // 第二点水平
  446. p->y = p2->y;
  447. p->x = (p->y - b1) / tan(p1->a);
  448. return p;
  449. }else if(p2->a == M_PI_2 || p2->a == M_PI_2 * 3){ // 第二点垂直
  450. p->x = p2->x;
  451. p->y = p->x * tan(p1->a) + b1;
  452. return p;
  453. }
  454. }
  455. p->x = (b1-b2)/(tan(p2->a) - tan(p1->a));
  456. p->y = tan(p1->a) * p->x + b1;
  457. return p;
  458. }
  459. // 不考虑折返
  460. void calcCoordinate( _coordinate* p1, double dist, double angle, _coordinate* dest )
  461. {
  462. dest->x = p1->x;
  463. dest->y = p1->y;
  464. dest->z = p1->z;
  465. dest->x += dist * cos(angle);
  466. dest->y += dist * sin(angle);
  467. dest->a = angle;
  468. }
  469. double getDistance( _coordinate* p1, _coordinate *p2 )
  470. {
  471. return sqrt(pow(p1->x - p2->x, 2) + pow(p1->y - p2->y, 2));
  472. }
  473. bool is_all_inline( _coordinate** coor_list, int cnt )
  474. {
  475. bool ret = false;
  476. if(cnt < 3) return ret;
  477. if(coor_list[0]->x == coor_list[1]->x && coor_list[0]->x == coor_list[2]->x){
  478. ret = true;
  479. }else if(coor_list[0]->y == coor_list[1]->y && coor_list[0]->y == coor_list[2]->y){
  480. ret = true;
  481. }else{
  482. ret = false;
  483. }
  484. return ret;
  485. }
  486. POS* LocateAlgorithm::Pos(ReaderPathMap rpm,int sta_num,int ant,double dist,INFO_PRE info_pre)
  487. {
  488. POS* p = new POS;
  489. p->posx = 0;
  490. p->posy = 0;
  491. p->pos_radius = 999999.9; //inf在matlab中表示负无穷
  492. double d[2];
  493. double xcross[2];
  494. double ycross[2];
  495. int seg_num = 0;
  496. seg_num = rpm.find(sta_num)->second->nRealCalcPoints - 1;
  497. if(seg_num < 0 ){
  498. return NULL;
  499. }
  500. int j = 0;
  501. for(int i =0;i<seg_num;i++){
  502. if(rpm.find(sta_num)->second->px[i]==-1||rpm.find(sta_num)->second->px[i+1]==-1)
  503. {
  504. continue;
  505. }
  506. SOLUTION *r = NULL;
  507. r = GetPos(rpm,sta_num,ant,dist,i);
  508. if(r == NULL){
  509. continue;
  510. }
  511. //x的列数count取2,是因为解最多为2
  512. int count = 2;
  513. for(j =0;j<2;j++){
  514. xcross[j] = r->x[j];
  515. ycross[j] = r->y[j];
  516. }
  517. for(j = 0;j < count;j++){
  518. double d1 = xcross[j] - rpm.find(info_pre.sta_num)->second->x[info_pre.ant];
  519. double d2 = ycross[j] - rpm.find(info_pre.sta_num)->second->y[info_pre.ant];
  520. d[j] = sqrt(pow(d1,2) + pow(d2,2));
  521. }
  522. for(j = 0;j < count;j++){
  523. d[j] = fabs(d[j] - info_pre.dist);
  524. }
  525. double c = 999999.9;
  526. int k = -1;
  527. for(j = 0;j < count;j++){
  528. if(d[j] < c){
  529. c = d[j];
  530. k = j;
  531. }
  532. }
  533. if(c<p->pos_radius){
  534. p->posx = xcross[k];
  535. p->posy = ycross[k];
  536. p->pos_radius = c;
  537. }
  538. }
  539. return p;
  540. }
  541. std::shared_ptr<POS> LocateAlgorithm::TdoaLocate2d(std::shared_ptr<ReceiveDataMap> pRdm)
  542. {
  543. if (pRdm->size() < 3)
  544. {
  545. return nullptr;
  546. }
  547. std::shared_ptr<POS> p = std::make_shared<POS>();
  548. //2D定位
  549. std::vector<double> vtk; //保存ki
  550. std::vector<double> vtd; //保存di
  551. std::vector<_coordinate> vtc; //保存xi,1 yi,1
  552. std::vector<_point> vtp; //保存三角形的顶点坐标
  553. std::vector<int> vts;
  554. vtk.resize(0);
  555. vtd.resize(0);
  556. vtc.resize(0);
  557. vts.resize(0);
  558. _coordinate tc;
  559. int i = 0;
  560. unsigned long long time_stamp = 0; //保存第一次的插值时间戳
  561. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end()&&i<3;++it)
  562. {
  563. double k = 0 ;
  564. k = pow(it->second->x,2) + pow(it->second->y,2) ;//+ pow(it->second->z,2)
  565. vtk.push_back(k);
  566. if (i == 0 )
  567. {
  568. time_stamp = it->second->rec_time_stamp;
  569. tc.x = it->second->x;
  570. tc.y = it->second->y;
  571. //tc.z = it->second->z;
  572. }
  573. long long diff_time = it->second->rec_time_stamp - time_stamp;
  574. double d = 0;
  575. d = CFunctions::getDistance(diff_time,CFunctions::TDOA);
  576. if (i>0)
  577. {
  578. if (d>0)
  579. {
  580. vts.push_back(1);
  581. }
  582. else
  583. {
  584. vts.push_back(-1);
  585. }
  586. }
  587. vtd.push_back(d);
  588. _coordinate dtc;
  589. dtc.x = it->second->x - tc.x;
  590. dtc.y = it->second->y - tc.y;
  591. //dtc.z = it->second->z - tc.z;
  592. vtc.push_back(dtc);
  593. _point p;
  594. p.x = it->second->x;
  595. p.y = it->second->y;
  596. vtp.push_back(p);
  597. i++;
  598. }
  599. double a[20] = {0};
  600. double dt = 0;
  601. dt = vtd[1]*vtc[2].y - vtd[2]*vtc[1].y;
  602. if (fabs(dt) < 1E-5)
  603. {
  604. return nullptr;
  605. }
  606. a[0] = (vtd[2]*vtc[1].x - vtd[1]*vtc[2].x)/dt;
  607. a[1] = (-1)*((vtk[1] - vtk[0] - pow(vtd[1],2))*vtd[2] - (vtk[2] - vtk[0] - pow(vtd[2],2))*vtd[1])*0.5/dt;
  608. if (fabs(vtd[1]) < 1E-5)
  609. {
  610. return nullptr;
  611. }
  612. a[2] = 0.5*(vtk[1] - vtk[0] - pow(vtd[1],2) - 2*vtc[1].y*a[1])/vtd[1];
  613. a[3] = -1*(vtc[1].x + vtc[1].y*a[0])/vtd[1];
  614. double A = 0, B = 0, C = 0;
  615. A = pow(a[3],2) - 1 - pow(a[0],2);
  616. B = 2*(a[2]*a[3] + tc.x + a[0]*(tc.y - a[1]));
  617. C = pow(a[2],2) - pow(tc.x,2) - pow(tc.y-a[1],2);
  618. _point pos[MAX_READER_TDOA_PATH_NUMS];
  619. double delta = 0.0;
  620. int count = 0;
  621. delta = pow(B,2) - 4*A*C;
  622. if (delta > 0)
  623. {
  624. pos[0].x = ((-1)*B + sqrt(delta))/(2*A);
  625. pos[0].y = a[0]*pos[0].x + a[1];
  626. //TRACE(_T("x1: %.2f , y1: %.2f \r\n"),pos[0].x,pos[0].y);
  627. pos[1].x = ((-1)*B - sqrt(delta))/(2*A);
  628. pos[1].y = a[0]*pos[1].x + a[1];
  629. //TRACE(_T("x2: %.2f , y2: %.2f \r\n"),pos[1].x,pos[1].y);
  630. count = 2;
  631. }else{
  632. return nullptr;
  633. }
  634. int idx = -1;
  635. int points = 0;
  636. for (int i=0;i<count;i++)
  637. {
  638. if (IsInTriangle(vtp,pos[i]))
  639. {
  640. //TRACE(_T("point is in triangle. the index is : %d \r\n"),i);
  641. bool condition[2] = {false,false};
  642. double d1 = 0,d2 = 0;
  643. //对两解进行判断,需要同时满足两个条件
  644. //1.解到点1和点2的距离差的方向性和之前的参数相同
  645. //2.解到点1和点3的距离差的方向性和之前的参数相同
  646. d1 = sqrt(pow(vtp[1].x - pos[i].x,2) + pow(vtp[1].y - pos[i].y,2)) - sqrt(pow(vtp[0].x - pos[i].x,2) + pow(vtp[0].y - pos[i].y,2));
  647. d2 = sqrt(pow(vtp[2].x - pos[i].x,2) + pow(vtp[2].y - pos[i].y,2)) - sqrt(pow(vtp[0].x - pos[i].x,2) + pow(vtp[0].y - pos[i].y,2));
  648. if ((d1<0&&vts[0]==-1)||(d1>0&&vts[0] == 1))
  649. {
  650. condition[0] = true;
  651. }
  652. if ((d2<0&&vts[1]==-1)||(d2>0&&vts[1] == 1))
  653. {
  654. condition[1] =true;
  655. }
  656. if (condition[0]&&condition[1])
  657. {
  658. idx = i;
  659. }
  660. //idx = i;
  661. }else{
  662. //TRACE(_T("point is not in triangle. the index is : %d \r\n"),i);
  663. }
  664. }
  665. if (points == 2)
  666. {
  667. idx = -1;
  668. //TRACE(_T("There hava 2 point.\r\n"));
  669. }
  670. if (idx != -1)
  671. {
  672. //TRACE(_T("the idx is : %d \r\n"),idx);
  673. p->posx = pos[idx].x;
  674. p->posy = pos[idx].y;
  675. }else{
  676. return nullptr;
  677. }
  678. return p;
  679. }
  680. /*
  681. * TDOA算法实现
  682. * 函数名:LocatePos,此函数与Pos函数的区别是本函数两两遍历求坐标
  683. * 遍历求解
  684. * param
  685. * pRdm ------ 存放参与计算的分站信息,主要信息包含同步后的时间戳
  686. * trpm ------ 地图集
  687. *
  688. * return
  689. * 返回最终结果坐标
  690. *
  691. */
  692. std::unique_ptr<POS> LocateAlgorithm::LocatePos(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm)
  693. {
  694. std::unique_ptr<POS> pos(new POS);
  695. int nNoReaderPathIdx = 0;
  696. int nFirstReader[MAX_READER_TDOA_PATH_NUMS] = {-1};
  697. int nSecondReader[MAX_READER_TDOA_PATH_NUMS] ={-1};
  698. _coordinate r;
  699. int r_idx = 0;
  700. r.reason = 0;
  701. for (ReceiveDataMap::iterator first = pRdm->begin();first != pRdm->end();++first)
  702. {
  703. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  704. TDOAReaderPathMap::iterator rdm_it = trpm->find(first->second->reader_id);
  705. if(rdm_it == trpm->end()){
  706. continue;
  707. }
  708. double ref_dist = 0.0; //两分站之间无路径的距离差
  709. _coordinate res[MAX_READER_TDOA_PATH_NUMS];
  710. int res_idx = 0;
  711. for(int i = 0;i < MAX_READER_TDOA_PATH_NUMS;i++){
  712. res[0].x = INVALID_COORDINATE;
  713. res[0].y = INVALID_COORDINATE;
  714. res[0].z = INVALID_COORDINATE;
  715. }
  716. //存储无路径的两分站的id和坐标
  717. ReceiveData tmp_reader[MAX_READER_TDOA_PATH_NUMS];
  718. //存储和第一条分站存在路径的分站信息
  719. ReceiveData tmp_dist_reader[MAX_READER_TDOA_PATH_NUMS];
  720. //和第一个分站存在地图集的分站个数,在丁字或十字路口,数量可能为3个或4个
  721. int nDistReaders = 0;
  722. //获取第一个时间戳
  723. ReceiveData f1;
  724. f1.antenna_id = first->second->antenna_id;
  725. f1.reader_id = first->second->reader_id;
  726. f1.rec_time_stamp = first->second->rec_time_stamp;
  727. f1.x = first->second->x;
  728. f1.y = first->second->y;
  729. f1.z = first->second->z;
  730. ReceiveDataMap::iterator second = first;
  731. std::advance(second,1);
  732. //从第二个开始遍历
  733. for(;second != pRdm->end();++second){
  734. //获取第二个时间戳
  735. ReceiveData f2;
  736. f2.antenna_id = second->second->antenna_id;
  737. f2.reader_id = second->second->reader_id;
  738. f2.rec_time_stamp = second->second->rec_time_stamp;
  739. f2.x = second->second->x;
  740. f2.y = second->second->y;
  741. f2.z = second->second->z;
  742. //时间戳异常
  743. if(f1.rec_time_stamp == LLONG_MAX || f2.rec_time_stamp == LLONG_MAX){
  744. continue;
  745. }
  746. if(f1.reader_id == f2.reader_id){
  747. continue;
  748. }
  749. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  750. if(rpm_it == trpm->find(f1.reader_id)->second->end()){
  751. continue;
  752. }
  753. //根据距离的正负,后续判断计算位置取舍时使用
  754. int nSign = 1;
  755. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  756. //计算位置
  757. double dist = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  758. double readers_dist = sqrt(pow(f1.x - f2.x,2) + pow(f1.y - f2.y,2));
  759. if(fabs(dist) - readers_dist > 0){
  760. continue;
  761. }
  762. //如果和第一条分站存在地图集
  763. tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  764. tmp_dist_reader[nDistReaders].x = f2.x;
  765. tmp_dist_reader[nDistReaders].y = f2.y;
  766. tmp_dist_reader[nDistReaders].z = f2.z;
  767. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  768. nDistReaders++;
  769. std::shared_ptr<ReaderPath> pRP = trpm->find(f1.reader_id)->second->find(f2.reader_id)->second;
  770. tmp_reader[nNoReaderPathIdx].reader_id = f2.reader_id;
  771. tmp_reader[nNoReaderPathIdx].x = f2.x;
  772. tmp_reader[nNoReaderPathIdx].y = f2.y;
  773. tmp_reader[nNoReaderPathIdx].z = f2.z;
  774. tmp_reader[nNoReaderPathIdx].rec_time_stamp = f2.rec_time_stamp;
  775. nNoReaderPathIdx++;
  776. //两分站之间的线段个数
  777. int seg_num = pRP->nRealCalcPoints - 1;
  778. if(seg_num == 0 || seg_num > 100){
  779. continue;
  780. }
  781. //因为双曲线与分站之间第i条线段或者第j条线段分别有两焦点
  782. //或者分站之间就一条直线,有两焦点
  783. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  784. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  785. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  786. int nIdx = 0;
  787. //根据线段个数开始计算
  788. for(int i = 0;i < seg_num;i++){
  789. //计算位置坐标,双曲线和线段相交的交点
  790. std::unique_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pRP,dist,i);
  791. //无解或解无效
  792. if(r == nullptr ){
  793. continue;
  794. }
  795. if(r->nCount == 0){
  796. continue;
  797. }
  798. if(r->nCount == 1){
  799. xcross[nIdx] = r->x[0];
  800. ycross[nIdx] = r->y[0];
  801. res[res_idx].x = xcross[nIdx];
  802. res[res_idx].y = ycross[nIdx];
  803. res[res_idx].z = zcross[nIdx];
  804. nIdx++;
  805. res_idx++;
  806. }
  807. if(r->nCount == 2){
  808. for(int j = 0; j < 2;j++){
  809. xcross[j] = r->x[j];
  810. ycross[j] = r->y[j];
  811. nIdx++;
  812. }
  813. }
  814. if(nIdx == 2){
  815. //解到两焦点之间的距离
  816. double deltad[2] = {0};
  817. for(int j = 0; j < 2;j ++){
  818. double d[2] = {0};
  819. double dx1 = xcross[j] - pRP->x[0];
  820. double dy1 = ycross[j] - pRP->y[0];
  821. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  822. double dx2 = xcross[j] - pRP->x[1];
  823. double dy2 = ycross[j] - pRP->y[1];
  824. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  825. deltad[j] = d[0] - d[1];
  826. }
  827. int idx = 0;
  828. //应该和之前计算的dist同方向
  829. if(dist > 0){
  830. for(int j = 0;j < 2;j++){
  831. if(deltad[j] > 0){
  832. idx = j;
  833. break;
  834. }
  835. }
  836. }else{
  837. for(int j = 0;j < 2;j++){
  838. if(deltad[j] < 0){
  839. idx = j;
  840. break;
  841. }
  842. }
  843. }
  844. nFirstReader[res_idx] = f1.reader_id;
  845. nSecondReader[res_idx] = f2.reader_id;
  846. res[res_idx].x = xcross[idx];
  847. res[res_idx].y = ycross[idx];
  848. res[res_idx].z = zcross[idx];
  849. res_idx++;
  850. }
  851. continue;
  852. }
  853. }
  854. if(res_idx == 1){
  855. bool bValid = true;
  856. //如果定位坐标在分站附近,需要判断此分站是否为special,如果为special则接受此定位结果
  857. for(ReceiveDataMap::iterator it = pRdm->begin();it!=pRdm->end();++it){
  858. double dist = sqrt(pow(res[0].x - it->second->x,2) + pow(res[0].y - it->second->y,2));
  859. if(dist<4){
  860. if(it->second->special == 0){
  861. r.reason = ALGO_FAILED_CONDITION_12;
  862. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_12);
  863. bValid = false;
  864. }
  865. }
  866. }
  867. if(pRdm->size()>=3&&bValid){
  868. double dist[2] = {0};
  869. //更改选取分站方式:
  870. //选取和第一个分站有地图集的两个分站来参与判别,
  871. //例如:分站顺序是2,1,3,其中(1,2)和(2,3)之间有地图集,那么就选取1,3来参与判别
  872. //如果分站顺序是1,2,3,其中(1,2)有地图集,(1,3)之间无地图集,那么此时就不使用以下方法进行判别
  873. if(nDistReaders == 2){
  874. dist[0] = sqrt(pow(res[0].x - tmp_dist_reader[0].x,2) + pow(res[0].y - tmp_dist_reader[0].y,2));
  875. dist[1] = sqrt(pow(res[0].x - tmp_dist_reader[1].x,2) + pow(res[0].y - tmp_dist_reader[1].y,2));
  876. double dif1 = fabs(dist[0] - dist[1]);
  877. long long dt = tmp_dist_reader[0].rec_time_stamp - tmp_dist_reader[1].rec_time_stamp;
  878. double dif2 = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  879. double dif = fabs(dif1 - dif2);
  880. if(dif>10){
  881. r.reason = ALGO_FAILED_CONDITION_13;
  882. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_13);
  883. bValid = false;
  884. }
  885. }
  886. }
  887. if(bValid){
  888. r.x = res[0].x;
  889. r.y = res[0].y;
  890. r.z = res[0].z;
  891. for (int i=0;i<MAX_READER_TDOA_PATH_NUMS;i++)
  892. {
  893. pos->dDiff[i] = 0;
  894. }
  895. }
  896. }else if(res_idx >= 2){
  897. double d = 99999.9;
  898. long long dt = tmp_reader[0].rec_time_stamp - tmp_reader[1].rec_time_stamp;
  899. //计算位置
  900. double ref_dist = abs(CFunctions::getDistance(dt,CFunctions::TDOA));
  901. for(int i = 0;i<res_idx;i++){
  902. double d_tmp = abs(sqrt(pow(res[i].x - tmp_reader[0].x,2) + pow(res[i].y - tmp_reader[0].y,2)) - sqrt(pow(res[i].x - tmp_reader[1].x,2) + pow(res[i].y - tmp_reader[1].y,2)));
  903. double d_diff = abs(d_tmp - ref_dist);
  904. pos->dDiff[i] = d_diff;
  905. if(d_diff < d){
  906. d = d_diff;
  907. r_idx = i;
  908. }
  909. }
  910. if(pRdm->size()>=3){
  911. if(pos->dDiff[r_idx] < 4){
  912. r.x = res[r_idx].x;
  913. r.y = res[r_idx].y;
  914. r.z = res[r_idx].z;
  915. }else{
  916. r.reason = ALGO_FAILED_CONDITION_14;
  917. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_14);
  918. }
  919. }
  920. }else{
  921. //解的个数<=0
  922. continue;
  923. }
  924. //表示找到解了,后面的数据都不可信
  925. if (r.x != INVALID_COORDINATE && r.y != INVALID_COORDINATE)
  926. {
  927. break;
  928. }
  929. }
  930. //最终得到的结果解
  931. pos->posx = r.x;
  932. pos->posy = r.y;
  933. pos->posz = 0;
  934. pos->reason = r.reason;
  935. if(pos->posx != INVALID_COORDINATE && pos->posy != INVALID_COORDINATE){
  936. pos->nFirstReader = nFirstReader[r_idx];
  937. pos->nSecondReader = nSecondReader[r_idx];
  938. }
  939. return pos;
  940. }
  941. SOLUTION* LocateAlgorithm::GetPos(ReaderPathMap rpm,int sta_num,int ant,double dist,int i)
  942. {
  943. double x1 = 0.0;
  944. double y1 = 0.0;
  945. double x2 = 0.0;
  946. double y2 = 0.0;
  947. double deta = 0.0; //b^2 - 4*a*c;
  948. int count = 0; //方程解的个数
  949. if((rpm.find(sta_num)->second->px[i] - rpm.find(sta_num)->second->px[i+1]) == 0){
  950. //方程一的解
  951. x1 = rpm.find(sta_num)->second->px[i];
  952. y1 = rpm.find(sta_num)->second->y[ant] - dist;
  953. x2 = rpm.find(sta_num)->second->px[i];
  954. y2 = dist + rpm.find(sta_num)->second->y[ant];
  955. count = 2;
  956. }else{
  957. //计算斜率
  958. double k = (rpm.find(sta_num)->second->py[i] - rpm.find(sta_num)->second->py[i+1])/(rpm.find(sta_num)->second->px[i] - rpm.find(sta_num)->second->px[i+1]);
  959. //方程二
  960. //确保有两解的情况
  961. //圆心为s[sta_num].x[ant],s[sta_num].y[ant];
  962. double a = pow(k,2) + 1;
  963. double b = 2*(k*(rpm.find(sta_num)->second->py[i] - k*rpm.find(sta_num)->second->px[i] - rpm.find(sta_num)->second->y[ant])- rpm.find(sta_num)->second->x[ant]);
  964. double d = rpm.find(sta_num)->second->py[i] - k * rpm.find(sta_num)->second->px[i] - rpm.find(sta_num)->second->y[ant];
  965. double c = pow(rpm.find(sta_num)->second->x[ant],2) + pow(d,2) - pow(dist,2);
  966. //根据deta = b^2 - 4*a*c 判断方程是否有解
  967. deta = pow(b,2) - 4*a*c; //
  968. if(deta>0){
  969. count = 2;
  970. }else if(deta == 0){
  971. count = 1;
  972. }
  973. else{
  974. count = 0;
  975. }
  976. if(count == 0){
  977. return NULL;
  978. }
  979. //得两解
  980. x1 = -(b + sqrt(deta))/(2*a);
  981. x2 = (-b + sqrt(deta) )/(2*a);
  982. y1 = k*x1 + rpm.find(sta_num)->second->py[i] - k*rpm.find(sta_num)->second->px[i];
  983. y2 = k*x2 + rpm.find(sta_num)->second->py[i] - k*rpm.find(sta_num)->second->px[i];
  984. }
  985. double x[3] = {0,0,0};
  986. double y[3] = {0,0,0};
  987. x[0] = x1;
  988. x[1] = x2;
  989. y[0] = y1;
  990. y[1] = y2;
  991. if(count>0){
  992. for(int t = 0;t<count;t++){
  993. if(x[t] < min(rpm.find(sta_num)->second->px[i],rpm.find(sta_num)->second->px[i+1]) - 0.01
  994. ||x[t] > max(rpm.find(sta_num)->second->px[i],rpm.find(sta_num)->second->px[i+1]) + 0.01
  995. ||y[t] < min(rpm.find(sta_num)->second->py[i],rpm.find(sta_num)->second->py[i+1])
  996. ||y[t] > max(rpm.find(sta_num)->second->py[i],rpm.find(sta_num)->second->py[i+1])){
  997. x[t] = 0;
  998. y[t] = 0;
  999. }
  1000. }
  1001. }
  1002. SOLUTION* result = new SOLUTION;
  1003. memset(result->x,0,3*sizeof(double));
  1004. memset(result->y,0,3*sizeof(double));
  1005. memset(result->z,0,3*sizeof(double));
  1006. result->x[0] = x[0];
  1007. result->x[1] = x[1];
  1008. result->y[0] = y[0];
  1009. result->y[1] = y[1];
  1010. result->z[0] = 0;
  1011. result->z[1] = 0;
  1012. return result;
  1013. }
  1014. /*
  1015. * TDOA算法实现
  1016. * 函数名:Pos
  1017. * 遍历求解
  1018. * param
  1019. * pRdm ------ 存放参与计算的分站信息,主要信息包含同步后的时间戳
  1020. * trpm ------ 地图集
  1021. *
  1022. * return
  1023. * 返回最终结果坐标
  1024. *
  1025. */
  1026. std::unique_ptr<POS> LocateAlgorithm::Pos(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm)
  1027. {
  1028. std::unique_ptr<POS> pos(new POS);
  1029. double a = 0;
  1030. double ref_dist = 0.0; //两分站之间无路径的距离差
  1031. _coordinate res[MAX_READER_TDOA_PATH_NUMS];
  1032. int res_idx = 0;
  1033. for(int i = 0;i < MAX_READER_TDOA_PATH_NUMS;i++){
  1034. res[i].x = INVALID_COORDINATE;
  1035. res[i].y = INVALID_COORDINATE;
  1036. res[i].z = INVALID_COORDINATE;
  1037. }
  1038. bool bFirst = true;
  1039. int nNoReaderPathIdx = 0;
  1040. int nFirstReader[MAX_READER_TDOA_PATH_NUMS] = {-1};
  1041. int nSecondReader[MAX_READER_TDOA_PATH_NUMS] ={-1};
  1042. //存储无路径的两分站的id和坐标
  1043. ReceiveData tmp_reader[MAX_READER_TDOA_PATH_NUMS];
  1044. //存储和第一条分站存在路径的分站信息
  1045. ReceiveData tmp_dist_reader[MAX_READER_TDOA_PATH_NUMS];
  1046. int nDistReaders = 0;
  1047. ReceiveDataMap::iterator first = pRdm->begin();
  1048. ReceiveDataMap::iterator second = first;
  1049. //偏移到第二个元素
  1050. std::advance(second,1);
  1051. //获取第一个时间戳
  1052. ReceiveData f1;
  1053. f1.antenna_id = first->second->antenna_id;
  1054. f1.reader_id = first->second->reader_id;
  1055. f1.rec_time_stamp = first->second->rec_time_stamp;
  1056. f1.x = first->second->x;
  1057. f1.y = first->second->y;
  1058. f1.z = first->second->z;
  1059. //从第二个开始遍历
  1060. for(;second != pRdm->end();++second){
  1061. //获取第二个时间戳
  1062. ReceiveData f2;
  1063. f2.antenna_id = second->second->antenna_id;
  1064. f2.reader_id = second->second->reader_id;
  1065. f2.rec_time_stamp = second->second->rec_time_stamp;
  1066. f2.x = second->second->x;
  1067. f2.y = second->second->y;
  1068. f2.z = second->second->z;
  1069. //时间戳异常
  1070. if(f1.rec_time_stamp == LLONG_MAX || f2.rec_time_stamp == LLONG_MAX){
  1071. continue;
  1072. }
  1073. if(f1.reader_id == f2.reader_id){
  1074. continue;
  1075. }
  1076. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1077. TDOAReaderPathMap::iterator rdm_it = trpm->find(f1.reader_id);
  1078. if(rdm_it == trpm->end()){
  1079. continue;
  1080. }
  1081. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  1082. if(rpm_it == trpm->find(f1.reader_id)->second->end()){
  1083. continue;
  1084. }else{
  1085. //如果和第一条分站存在地图集
  1086. tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  1087. tmp_dist_reader[nDistReaders].x = f2.x;
  1088. tmp_dist_reader[nDistReaders].y = f2.y;
  1089. tmp_dist_reader[nDistReaders].z = f2.z;
  1090. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  1091. nDistReaders++;
  1092. }
  1093. //根据距离的正负,后续判断计算位置取舍时使用
  1094. int nSign = 1;
  1095. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  1096. //计算位置
  1097. double dist = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  1098. double readers_dist = sqrt(pow(f1.x - f2.x,2) + pow(f1.y - f2.y,2));
  1099. if(fabs(dist) - readers_dist > 0){
  1100. continue;
  1101. }
  1102. std::shared_ptr<ReaderPath> pRP = trpm->find(f1.reader_id)->second->find(f2.reader_id)->second;
  1103. //如果和第一条分站存在地图集
  1104. /*tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  1105. tmp_dist_reader[nDistReaders].x = f2.x;
  1106. tmp_dist_reader[nDistReaders].y = f2.y;
  1107. tmp_dist_reader[nDistReaders].z = f2.z;
  1108. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  1109. nDistReaders++;*/
  1110. tmp_reader[nNoReaderPathIdx].reader_id = f2.reader_id;
  1111. tmp_reader[nNoReaderPathIdx].x = f2.x;
  1112. tmp_reader[nNoReaderPathIdx].y = f2.y;
  1113. tmp_reader[nNoReaderPathIdx].z = f2.z;
  1114. tmp_reader[nNoReaderPathIdx].rec_time_stamp = f2.rec_time_stamp;
  1115. nNoReaderPathIdx++;
  1116. //两分站之间的线段个数
  1117. int seg_num = pRP->nRealCalcPoints - 1;
  1118. if(seg_num == 0 || seg_num > 100){
  1119. continue;
  1120. }
  1121. //因为双曲线与分站之间第i条线段或者第j条线段分别有两焦点
  1122. //或者分站之间就一条直线,有两焦点
  1123. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1124. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1125. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1126. int nIdx = 0;
  1127. //根据线段个数开始计算
  1128. for(int i = 0;i < seg_num;i++){
  1129. //计算位置坐标,双曲线和线段相交的交点
  1130. std::unique_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pRP,dist,i);
  1131. //无解或解无效
  1132. if(r == nullptr ||r->nCount == 0){
  1133. continue;
  1134. }
  1135. if(r->nCount == 1){
  1136. xcross[nIdx] = r->x[0];
  1137. ycross[nIdx] = r->y[0];
  1138. res[res_idx].x = xcross[nIdx];
  1139. res[res_idx].y = ycross[nIdx];
  1140. res[res_idx].z = zcross[nIdx];
  1141. nIdx++;
  1142. res_idx++;
  1143. }
  1144. if(r->nCount == 2){
  1145. for(int j = 0; j < 2;j++){
  1146. xcross[j] = r->x[j];
  1147. ycross[j] = r->y[j];
  1148. nIdx++;
  1149. }
  1150. }
  1151. if(nIdx == 2){
  1152. //解到两焦点之间的距离
  1153. double deltad[2] = {0};
  1154. for(int j = 0; j < 2;j ++){
  1155. double d[2] = {0};
  1156. double dx1 = xcross[j] - pRP->x[0];
  1157. double dy1 = ycross[j] - pRP->y[0];
  1158. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  1159. double dx2 = xcross[j] - pRP->x[1];
  1160. double dy2 = ycross[j] - pRP->y[1];
  1161. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  1162. deltad[j] = d[0] - d[1];
  1163. }
  1164. int idx = 0;
  1165. //应该和之前计算的dist同方向
  1166. if(dist > 0){
  1167. for(int j = 0;j < 2;j++){
  1168. if(deltad[j] > 0){
  1169. idx = j;
  1170. break;
  1171. }
  1172. }
  1173. }else{
  1174. for(int j = 0;j < 2;j++){
  1175. if(deltad[j] < 0){
  1176. idx = j;
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. nFirstReader[res_idx] = f1.reader_id;
  1182. nSecondReader[res_idx] = f2.reader_id;
  1183. res[res_idx].x = xcross[idx];
  1184. res[res_idx].y = ycross[idx];
  1185. res[res_idx].z = zcross[idx];
  1186. res_idx++;
  1187. }
  1188. continue;
  1189. }
  1190. }
  1191. _coordinate r;
  1192. r.x = INVALID_COORDINATE;
  1193. r.y = INVALID_COORDINATE;
  1194. r.z = INVALID_COORDINATE;
  1195. if(res_idx == 1){
  1196. bool bValid = true;
  1197. //如果定位坐标在分站附近,需要判断此分站是否为special,如果为special则接受此定位结果
  1198. for(ReceiveDataMap::iterator it = pRdm->begin();it!=pRdm->end();++it){
  1199. double dist = sqrt(pow(res[0].x - it->second->x,2) + pow(res[0].y - it->second->y,2));
  1200. if(dist<4){
  1201. if(it->second->special == 0){
  1202. r.reason = ALGO_FAILED_CONDITION_12;
  1203. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_12);
  1204. bValid = false;
  1205. }
  1206. }
  1207. }
  1208. if(pRdm->size()>=3&&bValid){
  1209. double dist[2] = {0};
  1210. //更改选取分站方式:
  1211. //选取和第一个分站有地图集的两个分站来参与判别,
  1212. //例如:分站顺序是2,1,3,其中(1,2)和(2,3)之间有地图集,那么就选取1,3来参与判别
  1213. //如果分站顺序是1,2,3,其中(1,2)有地图集,(1,3)之间无地图集,那么此时就不使用以下方法进行判别
  1214. if(nDistReaders == 2){
  1215. //如果第二个分站与这两个分站无地图集则不该使用如下逻辑
  1216. dist[0] = sqrt(pow(res[0].x - tmp_dist_reader[0].x,2) + pow(res[0].y - tmp_dist_reader[0].y,2));
  1217. dist[1] = sqrt(pow(res[0].x - tmp_dist_reader[1].x,2) + pow(res[0].y - tmp_dist_reader[1].y,2));
  1218. double dif1 = fabs(dist[0] - dist[1]);
  1219. long long dt = tmp_dist_reader[0].rec_time_stamp - tmp_dist_reader[1].rec_time_stamp;
  1220. double dif2 = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  1221. double dif = fabs(dif1 - dif2);
  1222. if(dif>10){//4
  1223. r.reason = ALGO_FAILED_CONDITION_13;
  1224. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_13);
  1225. bValid = false;
  1226. }
  1227. }
  1228. }
  1229. if(bValid){
  1230. r.x = res[0].x;
  1231. r.y = res[0].y;
  1232. r.z = res[0].z;
  1233. for (int i=0;i<MAX_READER_TDOA_PATH_NUMS;i++)
  1234. {
  1235. pos->dDiff[i] = 0;
  1236. }
  1237. }
  1238. }
  1239. int r_idx = 0;
  1240. if(res_idx >= 2){
  1241. double d = 99999.9;
  1242. long long dt = tmp_reader[0].rec_time_stamp - tmp_reader[1].rec_time_stamp;
  1243. //计算位置
  1244. double ref_dist = abs(CFunctions::getDistance(dt,CFunctions::TDOA));
  1245. for(int i = 0;i<res_idx;i++){
  1246. double d_tmp = abs(sqrt(pow(res[i].x - tmp_reader[0].x,2) + pow(res[i].y - tmp_reader[0].y,2)) - sqrt(pow(res[i].x - tmp_reader[1].x,2) + pow(res[i].y - tmp_reader[1].y,2)));
  1247. double d_diff = abs(d_tmp - ref_dist);
  1248. pos->dDiff[i] = d_diff;
  1249. if(d_diff < d){
  1250. d = d_diff;
  1251. r_idx = i;
  1252. }
  1253. }
  1254. if(pRdm->size()>=3){
  1255. if(pos->dDiff[r_idx] < 6){ //4
  1256. r.x = res[r_idx].x;
  1257. r.y = res[r_idx].y;
  1258. r.z = res[r_idx].z;
  1259. }else{
  1260. r.reason = ALGO_FAILED_CONDITION_14;
  1261. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_14);
  1262. }
  1263. }
  1264. }
  1265. //最终得到的结果解
  1266. pos->posx = r.x;
  1267. pos->posy = r.y;
  1268. pos->posz = 0;
  1269. pos->reason = r.reason;
  1270. if(pos->posx != INVALID_COORDINATE && pos->posy != INVALID_COORDINATE){
  1271. pos->nFirstReader = nFirstReader[r_idx];
  1272. pos->nSecondReader = nSecondReader[r_idx];
  1273. }
  1274. return pos;
  1275. }
  1276. std::shared_ptr<POS> LocateAlgorithm::Pos(std::shared_ptr<POS> pos,std::shared_ptr<TDOAReaderPathMap> trpm)
  1277. {
  1278. if(trpm->find(pos->nFirstReader) == trpm->end()){
  1279. return nullptr;
  1280. }
  1281. if(trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)==trpm->find(pos->nFirstReader)->second->end()){
  1282. return nullptr;
  1283. }
  1284. std::shared_ptr<POS> p = std::make_shared<POS>();
  1285. _coordinate res[2];
  1286. int res_idx = 0;
  1287. for(int i = 0;i < 2;i++){
  1288. res[i].x = INVALID_COORDINATE;
  1289. res[i].y = INVALID_COORDINATE;
  1290. res[i].z = INVALID_COORDINATE;
  1291. }
  1292. //获得地图集
  1293. std::shared_ptr<ReaderPath> pRP = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second;
  1294. //距离差
  1295. double distance = 0.0;
  1296. distance = sqrt(pow(pos->posx - pRP->x[0],2 ) + pow(pos->posy - pRP->y[0],2 )) -
  1297. sqrt(pow(pos->posx - pRP->x[1],2 ) + pow(pos->posy - pRP->y[1],2 ));
  1298. //两分站之间的线段个数
  1299. int seg_num = pRP->nRealCalcPoints - 1;
  1300. if(seg_num == 0){
  1301. return nullptr;
  1302. }
  1303. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1304. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1305. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1306. int nIdx = 0;
  1307. int nResIdx = 0;
  1308. for(int i = 0;i < seg_num;i++){
  1309. //计算位置坐标,双曲线和线段相交的交点
  1310. std::unique_ptr<SOLUTION> r = GetPos(pRP,distance,i);
  1311. //无解或解无效
  1312. if(r == nullptr){
  1313. continue;
  1314. }
  1315. if(r->nCount == 0){
  1316. continue;
  1317. }
  1318. if(r->nCount == 1){
  1319. xcross[nIdx] = r->x[0];
  1320. ycross[nIdx] = r->y[0];
  1321. nIdx++;
  1322. }
  1323. if(r->nCount == 2){
  1324. for(int j = 0; j < 2;j++){
  1325. xcross[j] = r->x[j];
  1326. ycross[j] = r->y[j];
  1327. nIdx++;
  1328. }
  1329. }
  1330. if(nIdx == 2){
  1331. //解到两焦点之间的距离
  1332. double deltad[2] = {0};
  1333. for(int j = 0; j < 2;j ++){
  1334. double d[2] = {0};
  1335. double dx1 = xcross[j] - pRP->x[0];
  1336. double dy1 = ycross[j] - pRP->y[0];
  1337. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  1338. double dx2 = xcross[j] - pRP->x[1];
  1339. double dy2 = ycross[j] - pRP->y[1];
  1340. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  1341. deltad[j] = d[0] - d[1];
  1342. }
  1343. int idx = 0;
  1344. //应该和之前计算的dist同方向
  1345. if(distance > 0){
  1346. for(int j = 0;j < 2;j++){
  1347. if(deltad[j] > 0){
  1348. idx = j;
  1349. break;
  1350. }
  1351. }
  1352. }else{
  1353. for(int j = 0;j < 2;j++){
  1354. if(deltad[j] < 0){
  1355. idx = j;
  1356. break;
  1357. }
  1358. }
  1359. }
  1360. res[nResIdx].x = xcross[idx];
  1361. res[nResIdx].y = ycross[idx];
  1362. res[nResIdx].z = zcross[idx];
  1363. }
  1364. continue;
  1365. }
  1366. if (res[nResIdx].x == INVALID_COORDINATE && res[nResIdx].y == INVALID_COORDINATE)
  1367. {
  1368. return nullptr;
  1369. }
  1370. p->posx = res[nResIdx].x;
  1371. p->posy = res[nResIdx].y;
  1372. p->posz = res[nResIdx].z;
  1373. return p;
  1374. }
  1375. /*
  1376. * TDOA算法求解实现
  1377. * 函数名:GetPos
  1378. * 实现直线和双曲线求交点
  1379. * param
  1380. * pRP ------ 分站路径
  1381. * dist ------ 距离差
  1382. * i ------ 第i条线段
  1383. *
  1384. * return
  1385. * 满足条件的解,最多两个
  1386. *
  1387. */
  1388. //SOLUTION* LocateAlgorithm::GetPos(ReaderPath * pRP,double dist,int i)
  1389. typedef struct
  1390. {
  1391. double x;
  1392. double y;
  1393. double z;
  1394. }PositionStr;
  1395. int calc_card_position(ReceiveData stationFirst, ReceiveData stationSecond, double distanceDiff, PositionStr& result)
  1396. {
  1397. double station_distance = sqrt(pow((stationFirst.x - stationSecond.x), 2) + pow((stationFirst.y - stationSecond.y),2));
  1398. double diff_check = abs(abs(station_distance) - abs(distanceDiff));
  1399. if(abs(distanceDiff) > station_distance)
  1400. {
  1401. //debug_print_syslog(0, "calc_card_position, distanceDiff is longer than stationDistance, read_id_first: %d,read_id_second: %d, diff_check: %f,\
  1402. // first_time: %I64u, second_time: %I64u, station_distance: %f, distanceDiff: %f",
  1403. // stationFirst.reader_id, stationSecond.reader_id,
  1404. // diff_check, stationFirst.rec_time_stamp, stationSecond.rec_time_stamp, station_distance, distanceDiff);
  1405. return -1;
  1406. }
  1407. if(diff_check < 4)
  1408. {
  1409. //debug_print_syslog(0, "calc_card_position, read_id_first: %d,read_id_second: %d, diff_check: %f,\
  1410. // first_time: %I64u, second_time: %I64u, station_distance: %f, distanceDiff: %f",
  1411. // stationFirst.reader_id, stationSecond.reader_id,
  1412. // diff_check, stationFirst.rec_time_stamp, stationSecond.rec_time_stamp, station_distance, distanceDiff);
  1413. return -1;
  1414. }
  1415. double m = station_distance + distanceDiff;
  1416. double n = station_distance - distanceDiff;
  1417. result.x = ((m * stationSecond.x) + (n * stationFirst.x))/(m + n);
  1418. result.y = ((m * stationSecond.y) + (n * stationFirst.y))/(m + n);
  1419. result.z = 0;
  1420. return 0;
  1421. }
  1422. std::unique_ptr<POS> LocateAlgorithm::CalcCardPosition(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm)
  1423. {
  1424. std::unique_ptr<POS> pos(new POS);
  1425. double a = 0;
  1426. double ref_dist = 0.0; //两分站之间无路径的距离差
  1427. const int Totals = 10;
  1428. _coordinate res;
  1429. int res_idx = 0;
  1430. bool bFirst = true;
  1431. int nNoReaderPathIdx = 0;
  1432. int nFirstReader[Totals] = {-1};
  1433. int nSecondReader[Totals] ={-1};
  1434. pos->posx = INVALID_COORDINATE;
  1435. pos->posy = INVALID_COORDINATE;
  1436. pos->posz = INVALID_COORDINATE;
  1437. pos->reason = ALGO_FAILED_CONDITION_1;
  1438. //存储无路径的两分站的id和坐标
  1439. ReceiveData tmp_reader[Totals];
  1440. //存储和第一条分站存在路径的分站信息
  1441. ReceiveData tmp_dist_reader[Totals];
  1442. int nDistReaders = 0;
  1443. for(ReceiveDataMap::iterator it_tmp = pRdm->begin(); it_tmp != pRdm->end(); it_tmp++)
  1444. {
  1445. //debug_print_syslog(0, "Calculate data, read_id_second: %d, rec_time_stamp: %I64u",
  1446. // it_tmp->second->reader_id, it_tmp->second->rec_time_stamp);
  1447. }
  1448. ReceiveDataMap::iterator first = pRdm->begin();
  1449. ReceiveDataMap::iterator second = first;
  1450. //偏移到第二个元素
  1451. std::advance(second,1);
  1452. //获取第一个时间戳
  1453. ReceiveData f1;
  1454. f1.antenna_id = first->second->antenna_id;
  1455. f1.reader_id = first->second->reader_id;
  1456. f1.rec_time_stamp = first->second->rec_time_stamp;
  1457. f1.x = first->second->x;
  1458. f1.y = first->second->y;
  1459. f1.z = first->second->z;
  1460. //从第二个开始遍历
  1461. for(; second != pRdm->end(); ++second)
  1462. {
  1463. //获取第二个时间戳
  1464. ReceiveData f2;
  1465. f2.antenna_id = second->second->antenna_id;
  1466. f2.reader_id = second->second->reader_id;
  1467. f2.rec_time_stamp = second->second->rec_time_stamp;
  1468. f2.x = second->second->x;
  1469. f2.y = second->second->y;
  1470. f2.z = second->second->z;
  1471. //时间戳异常
  1472. if(f1.rec_time_stamp == LLONG_MAX || f2.rec_time_stamp == LLONG_MAX){
  1473. continue;
  1474. }
  1475. if(f1.reader_id == f2.reader_id){
  1476. continue;
  1477. }
  1478. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1479. TDOAReaderPathMap::iterator rdm_it = trpm->find(f1.reader_id);
  1480. if(rdm_it == trpm->end()){
  1481. continue;
  1482. }
  1483. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  1484. if(rpm_it == trpm->find(f1.reader_id)->second->end()){
  1485. continue;
  1486. }else{
  1487. //如果和第一条分站存在地图集
  1488. tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  1489. tmp_dist_reader[nDistReaders].x = f2.x;
  1490. tmp_dist_reader[nDistReaders].y = f2.y;
  1491. tmp_dist_reader[nDistReaders].z = f2.z;
  1492. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  1493. nDistReaders++;
  1494. }
  1495. //根据距离的正负,后续判断计算位置取舍时使用
  1496. int nSign = 1;
  1497. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  1498. //计算位置
  1499. double distDiff = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  1500. PositionStr stationFirst;
  1501. stationFirst.x = f1.x;
  1502. stationFirst.y = f1.y;
  1503. PositionStr stationSecond;
  1504. stationSecond.x = f2.x;
  1505. stationSecond.y = f2.y;
  1506. PositionStr result;
  1507. int rt_val = 0;
  1508. rt_val = calc_card_position(f1, f2, distDiff, result);
  1509. if(0 == rt_val)
  1510. {
  1511. res.x = result.x;
  1512. res.y = result.y;
  1513. res.z = result.z;
  1514. res.reason = ALGO_LOC_SUCCESSED;
  1515. res_idx++;
  1516. pos->posx = res.x;
  1517. pos->posy = res.y;
  1518. pos->posz = res.z;
  1519. pos->reason = res.reason;
  1520. return pos;
  1521. }
  1522. }
  1523. return pos;
  1524. }
  1525. std::unique_ptr<SOLUTION> LocateAlgorithm::GetPos(std::shared_ptr<ReaderPath> pRP,double dist,int i)
  1526. {
  1527. //解的个数
  1528. int count = 0;
  1529. //解的坐标
  1530. double x[2];
  1531. double y[2];
  1532. //double z[2];
  1533. for (int t=0;t<2;t++)
  1534. {
  1535. x[t] = y[t] = INVALID_COORDINATE;
  1536. }
  1537. //双曲线的两个焦点分别是
  1538. double x1 = pRP->x[0];
  1539. double y1 = pRP->y[0];
  1540. double z1 = pRP->z[0];
  1541. double x2 = pRP->x[1];
  1542. double y2 = pRP->y[1];
  1543. double z2 = pRP->z[1];
  1544. if(pRP->px[i] - pRP->px[i+1] == 0){
  1545. //x相等,双曲线在Y轴上
  1546. x[0] = x[1] = pRP->px[i];
  1547. y[0] = (y1 + y2 + dist)/2.0;
  1548. y[1] = (y1 + y2 - dist)/2.0;
  1549. count = 2;
  1550. }
  1551. else{
  1552. //双曲线和直线相交
  1553. //直线常数求解
  1554. double k = (pRP->py[i+1] - pRP->py[i])/(pRP->px[i+1] - pRP->px[i]);
  1555. double kb = pRP->py[i] - k*pRP->px[i];
  1556. //求解的常数中间量
  1557. double d1 = 2*(x2 - x1);//m
  1558. double d2 = 2*(y2 - y1);//n
  1559. double d3 = pow(y1,2) + pow(x1,2)- pow(dist,2) - pow(y2,2) - pow(x2,2);//p
  1560. //方程ax^2 + bx + c = 0
  1561. double a = pow(d1 + d2*k,2) - 4*pow(dist,2)*(pow(k,2) + 1);
  1562. double b = 2*((d3 + d2*kb)*(d1 + d2*k) - 4*pow(dist,2)*(k*(kb - y2) - x2));
  1563. double c = pow(d3 + d2*kb,2) - 4*pow(dist,2)*(pow(x2,2) + pow(kb-y2,2));
  1564. double delta = pow(b,2) - 4*a*c;
  1565. if(delta > 0){
  1566. count = 2;
  1567. }
  1568. else if(delta == 0){
  1569. count = 1;
  1570. }
  1571. else{
  1572. count = 0;
  1573. }
  1574. if(count == 0){
  1575. return nullptr;
  1576. }
  1577. //计算解
  1578. x[0] = -(b + sqrt(delta))/(2*a);
  1579. x[1] = (-b + sqrt(delta) )/(2*a);
  1580. y[0] = k*x[0] + kb;
  1581. y[1] = k*x[1] + kb;
  1582. }
  1583. int nIdx = 0;
  1584. int nValidCount = count;
  1585. //引入误差
  1586. double deviation = 0.0;
  1587. //判断两解是否在线段范围内
  1588. if(count > 0){
  1589. for(int t = 0; t < count ;t++){
  1590. //两分站之间第i条线段的两端点
  1591. if(x[t] < min(pRP->px[i],pRP->px[i+1]) - deviation||
  1592. x[t] > max(pRP->px[i],pRP->px[i+1]) + deviation||
  1593. y[t] < min(pRP->py[i],pRP->py[i+1]) - deviation||
  1594. y[t] > max(pRP->py[i],pRP->py[i+1]) + deviation
  1595. )
  1596. {
  1597. x[t] = INVALID_COORDINATE;
  1598. y[t] = INVALID_COORDINATE;
  1599. nValidCount--;
  1600. nIdx = t;
  1601. }
  1602. }
  1603. }
  1604. std::unique_ptr<SOLUTION> s(new SOLUTION);
  1605. s->nCount = nValidCount;
  1606. nIdx = -1;
  1607. switch(nValidCount){
  1608. case 1:
  1609. for(int i = 0;i<2;i++){
  1610. if(x[i] != INVALID_COORDINATE && y[i] != INVALID_COORDINATE){
  1611. nIdx = i;
  1612. }
  1613. }
  1614. s->x[0] = x[nIdx];
  1615. s->y[0] = y[nIdx];
  1616. break;
  1617. case 2:
  1618. s->x[0] = x[0];
  1619. s->x[1] = x[1];
  1620. s->y[0] = y[0];
  1621. s->y[1] = y[1];
  1622. break;
  1623. }
  1624. return s;
  1625. }
  1626. /*
  1627. * 判断坐标在地图集上
  1628. *
  1629. * param
  1630. * pos ------ 定位的结果
  1631. * trpm ------ 地图集
  1632. *
  1633. * return
  1634. * false,不在地图集上,true在地图集上
  1635. *
  1636. */
  1637. bool LocateAlgorithm::IsOnMap(std::shared_ptr<POS>& pos,std::shared_ptr<TDOAReaderPathMap> trpm)
  1638. {
  1639. //如果地图集中都找不到此分站开始的地图集,就返回false
  1640. //if(trpm->find(pos->nFirstReader) == trpm->end()){
  1641. // return false;
  1642. //}
  1643. ////如果地图集找不到此两分站的地图集,就返回false
  1644. //if(trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader) == trpm->find(pos->nFirstReader)->second->end())
  1645. //{
  1646. // return false;
  1647. //}
  1648. ////保存两分站的坐标
  1649. //double x[2] = {0};
  1650. //double y[2] = {0};
  1651. //for(int i=0;i<2;i++){
  1652. // x[i] = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second->x[i];
  1653. // y[i] = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second->y[i];
  1654. //}
  1655. //if(x[0] == x[1]){
  1656. // //误差1cm
  1657. // if(abs(pos->posx - x[0]) > 1E-2){
  1658. // return false;
  1659. // }
  1660. //}else{
  1661. // double k = (y[1] - y[0])/(x[1] - x[0]);
  1662. // double b = y[1] - k*x[1];
  1663. // double calc_y = k*pos->posx + b;
  1664. // if(abs(calc_y - pos->posy) > 1E-2){
  1665. // return false;
  1666. // }
  1667. //}
  1668. bool bExist = false;
  1669. for (TDOAReaderPathMap::iterator first = trpm->begin();first!=trpm->end();++first)
  1670. {
  1671. for (ReaderPathMap::iterator second = first->second->begin();second != first->second->end();++second)
  1672. {
  1673. _point p,start_p,end_p;
  1674. p.x = pos->posx;
  1675. p.y = pos->posy;
  1676. start_p.x = second->second->px[0];
  1677. start_p.y = second->second->py[0];
  1678. end_p.x = second->second->px[1];
  1679. end_p.y = second->second->py[1];
  1680. bExist = LocateAlgorithm::IsInLine(p,start_p,end_p);
  1681. if (bExist)
  1682. {
  1683. pos->nFirstReader = first->first;
  1684. pos->nSecondReader = second->first;
  1685. return bExist;
  1686. }
  1687. ////保存两分站的坐标
  1688. //double x[2] = {0};
  1689. //double y[2] = {0};
  1690. //for(int i=0;i<2;i++){
  1691. // x[i] = second->second->x[i];
  1692. // y[i] = second->second->y[i];
  1693. //}
  1694. //if(abs(x[0] - x[1]) < 1E-4 && abs(pos->posx - x[0]) < 1E-4){
  1695. // //误差1cm
  1696. // if ((pos->posy > y[0] && pos->posy < y[1])||(pos->posy > y[1] && pos->posy < y[0]))
  1697. // {
  1698. // bExist = true;
  1699. // return bExist;
  1700. // }
  1701. //}else{
  1702. // double k = (y[1] - y[0])/(x[1] - x[0]);
  1703. // double b = y[1] - k*x[1];
  1704. // double calc_y = k*pos->posx + b;
  1705. // if(abs(calc_y - pos->posy) < 1){
  1706. // bExist = true;
  1707. // return bExist;
  1708. // }
  1709. //}
  1710. }
  1711. }
  1712. return bExist;
  1713. }
  1714. bool LocateAlgorithm::IsInTriangle(std::vector<_point> vtp,_point p)
  1715. {
  1716. double sabc = 0,sadb = 0,sbdc = 0,sadc = 0;
  1717. sabc = GetTriangleArea(vtp[0],vtp[1],vtp[2]);
  1718. sadb = GetTriangleArea(vtp[0],p,vtp[1]);
  1719. sbdc = GetTriangleArea(vtp[1],p,vtp[2]);
  1720. sadc = GetTriangleArea(vtp[0],p,vtp[2]);
  1721. double sum = 0.0;
  1722. sum = sadb + sbdc + sadc;
  1723. if ((sabc - sum) > -1E-5 && (sabc - sum) < 1E-5)
  1724. {
  1725. return true;
  1726. }
  1727. else
  1728. {
  1729. return false;
  1730. }
  1731. }
  1732. double LocateAlgorithm::GetTriangleArea(_point p0,_point p1,_point p2)
  1733. {
  1734. _point ab,bc;
  1735. ab.x = p1.x - p0.x;
  1736. ab.y = p1.y - p0.y;
  1737. bc.x = p2.x - p1.x;
  1738. bc.y = p2.y - p1.y;
  1739. return abs(ab.x*bc.y - ab.y*bc.x)/2.0;
  1740. }
  1741. /*
  1742. * 点P是否在以start_p和end_p的线段上
  1743. *
  1744. *
  1745. * return
  1746. * 在线段上返回true,否则返回false
  1747. */
  1748. bool LocateAlgorithm::IsInLine(_point p,_point start_p,_point end_p)
  1749. {
  1750. double d1 = 0, d2 = 0, d3 = 0;
  1751. d1 = sqrt(pow(p.x - start_p.x,2) + pow(p.y - start_p.y,2));
  1752. d2 = sqrt(pow(p.x - end_p.x,2) + pow(p.y - end_p.y,2));
  1753. d3 = sqrt(pow(start_p.x - end_p.x,2) + pow(start_p.y - end_p.y,2));
  1754. if (fabs(d3 - (d1 + d2)) < 1)
  1755. {
  1756. return true;
  1757. }
  1758. return false;
  1759. }
  1760. bool LocateAlgorithm::PointIsInRect(point p,point tp,point lp,point bp,point rp)
  1761. {
  1762. //任意四边形有4个顶点
  1763. int nCount = 4;
  1764. point RectPoints[4] = { tp, lp, bp, rp };
  1765. int nCross = 0;
  1766. for (int i = 0; i < nCount; i++)
  1767. {
  1768. //依次取相邻的两个点
  1769. point pStart = RectPoints[i];
  1770. point pEnd = RectPoints[(i + 1) % nCount];
  1771. //相邻的两个点是平行于x轴的,肯定不相交,忽略
  1772. if ( pStart.y == pEnd.y )
  1773. continue;
  1774. //交点在pStart,pEnd的延长线上,pCur肯定不会与pStart.pEnd相交,忽略
  1775. if ( p.y < min(pStart.y, pEnd.y) || p.y > max( pStart.y, pEnd.y ) )
  1776. continue;
  1777. //求当前点和x轴的平行线与pStart,pEnd直线的交点的x坐标
  1778. double x = (double)(p.y - pStart.y) * (double)(pEnd.x - pStart.x) / (double)(pEnd.y - pStart.y) + pStart.x;
  1779. //若x坐标大于当前点的坐标,则有交点
  1780. if ( x > p.x )
  1781. nCross++;
  1782. }
  1783. // 单边交点为偶数,点在多边形之外
  1784. return (nCross % 2 == 1);
  1785. }
  1786. int LocateAlgorithm::CalcTdoaPosition(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm,std::vector<std::shared_ptr<POS>>& udm_pos)
  1787. {
  1788. //这部分应该放到组装数据后检测里
  1789. //一:选择基准分站,条件如下:
  1790. //1.基准分站选择:当前分站和后一个分站有地图集,则选为基准分站
  1791. //2.当前分站和后续所有分站无地图集,则当前分站不是基准分站
  1792. //3.重复1,2过程
  1793. for (ReceiveDataMap::iterator first = pRdm->begin();first != pRdm->end();)
  1794. {
  1795. ReceiveDataMap::iterator second = first;
  1796. //second偏移到第二个元素
  1797. std::advance(second,1);
  1798. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1799. TDOAReaderPathMap::iterator rdm_it = trpm->find(first->second->reader_id);
  1800. if(rdm_it == trpm->end()){
  1801. //表示地图集中不存在第一个分站的路径集,则删除此分站,并继续循环
  1802. pRdm->erase(first);
  1803. first = pRdm->begin();
  1804. continue;
  1805. }
  1806. //统计first分站和后续分站的地图集数量
  1807. int nCount = 0;
  1808. for (;second!= pRdm->end();++second)
  1809. {
  1810. //确认第一个和第二个分站之间是否有地图集
  1811. ReaderPathMap::iterator rpm_it = trpm->find(first->second->reader_id)->second->find(second->second->reader_id);
  1812. if(rpm_it == trpm->find(first->second->reader_id)->second->end()){
  1813. continue;
  1814. }else{
  1815. nCount++;
  1816. break;
  1817. }
  1818. }
  1819. //如果first分站和后续分站都无地图集,则删除first分站,并重置迭代器
  1820. if (nCount == 0)
  1821. {
  1822. pRdm->erase(first);
  1823. first = pRdm->begin();
  1824. }else{
  1825. first = second;
  1826. break;
  1827. }
  1828. }
  1829. if (pRdm->size() < 2)
  1830. {
  1831. return ALGO_CALC_SOLUTION;
  1832. }
  1833. //先做第一遍筛选,根据前三个分站计算坐标,如果无解,做第二遍筛选
  1834. bool bNoSolution = true;
  1835. std::unique_ptr<POS> first_pos = LocateAlgorithm::Pos(pRdm,trpm);
  1836. if (first_pos->posx != INVALID_COORDINATE && first_pos->posy != INVALID_COORDINATE)
  1837. {
  1838. std::shared_ptr<POS> sp = std::make_shared<POS>();
  1839. *sp = *first_pos;
  1840. udm_pos.push_back(sp);
  1841. bNoSolution = false;
  1842. return 0;
  1843. }
  1844. if (bNoSolution && pRdm->size() <= 2)
  1845. {
  1846. return ALGO_CALC_NO_SOLUTION_WITH_TWO_DATA;
  1847. }
  1848. //执行第二遍筛选
  1849. ReceiveDataMap::iterator first = pRdm->begin();
  1850. ReceiveDataMap::iterator second = first;
  1851. if (bNoSolution)
  1852. {
  1853. //因为first和后两个分站已经通过上步判断无解了
  1854. //如果按3偏移,存在一个bug,
  1855. //即在在如下情况下103,104,301,102,105,106情况下
  1856. //在第一步时,当103和104,103和102的结果被判定不合格而无解
  1857. //此处偏移3,即会导致301被跳过
  1858. //所以改为重新对后续求所有解
  1859. std::advance(second,1);
  1860. }
  1861. //偏移到第二个元素
  1862. //std::advance(second,1);
  1863. if (second == pRdm->end())
  1864. {
  1865. //偏移后找不到元素
  1866. return ALGO_CALC_ONE_DATA;
  1867. }
  1868. //获取第一个元素中的数据
  1869. ReceiveData f1;
  1870. f1.antenna_id = first->second->antenna_id;
  1871. f1.reader_id = first->second->reader_id;
  1872. f1.rec_time_stamp = first->second->rec_time_stamp;
  1873. f1.x = first->second->x;
  1874. f1.y = first->second->y;
  1875. f1.z = first->second->z;
  1876. for(;second != pRdm->end();++second){
  1877. //获取第二个数据中的元素
  1878. ReceiveData f2;
  1879. f2.antenna_id = second->second->antenna_id;
  1880. f2.reader_id = second->second->reader_id;
  1881. f2.rec_time_stamp = second->second->rec_time_stamp;
  1882. f2.x = second->second->x;
  1883. f2.y = second->second->y;
  1884. f2.z = second->second->z;
  1885. bool bExistPath = false;
  1886. //如果第一级都不存在,则继续找下一个元素
  1887. TDOAReaderPathMap::iterator rdm_it = trpm->find(f1.reader_id);
  1888. if(rdm_it == trpm->end()){
  1889. continue;
  1890. }
  1891. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1892. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  1893. if(rpm_it != trpm->find(f1.reader_id)->second->end()){
  1894. bExistPath = true;
  1895. }
  1896. //根据距离的正负,后续判断计算位置取舍时使用
  1897. int nSign = 1;
  1898. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  1899. //计算位置
  1900. double dist = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  1901. double readers_dist = sqrt(pow(f1.x - f2.x,2) + pow(f1.y - f2.y,2));
  1902. if(fabs(dist) - readers_dist > 0){
  1903. continue;
  1904. }
  1905. //保存解信息
  1906. std::shared_ptr<POS> pos = std::make_shared<POS>();
  1907. if (bExistPath)
  1908. {
  1909. //如果存在地图集
  1910. std::shared_ptr<ReaderPath> pRP = trpm->find(f1.reader_id)->second->find(f2.reader_id)->second;
  1911. //两分站之间的线段个数
  1912. int seg_num = pRP->nRealCalcPoints - 1;
  1913. if(seg_num == 0 || seg_num > 100){
  1914. continue;
  1915. }
  1916. //因为双曲线与分站之间第i条线段或者第j条线段分别有两焦点
  1917. //或者分站之间就一条直线,有两焦点
  1918. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1919. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1920. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1921. int nIdx = 0;
  1922. for(int i = 0;i < seg_num;i++){
  1923. //计算位置坐标,双曲线和线段相交的交点
  1924. std::unique_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pRP,dist,i);
  1925. //无解或解无效
  1926. if(r == nullptr || r->nCount == 0){
  1927. continue;
  1928. }
  1929. int nPosCounts = 0;
  1930. for (int i=0;i<r->nCount;i++)
  1931. {
  1932. xcross[nIdx] = r->x[i];
  1933. ycross[nIdx] = r->y[i];
  1934. nIdx++;
  1935. }
  1936. switch (nIdx)
  1937. {
  1938. case 1:
  1939. pos->posx = xcross[0];
  1940. pos->posy = ycross[0];
  1941. pos->posz = zcross[0];
  1942. udm_pos.push_back(pos);
  1943. break;
  1944. case 2:
  1945. //筛选解:从两解中选出一个
  1946. //解到两焦点之间的距离
  1947. double deltad[2] = {0};
  1948. for(int j = 0; j < 2;j ++){
  1949. double d[2] = {0};
  1950. double dx1 = xcross[j] - pRP->x[0];
  1951. double dy1 = ycross[j] - pRP->y[0];
  1952. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  1953. double dx2 = xcross[j] - pRP->x[1];
  1954. double dy2 = ycross[j] - pRP->y[1];
  1955. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  1956. deltad[j] = d[0] - d[1];
  1957. }
  1958. int idx = 0;
  1959. //应该和之前计算的dist同方向
  1960. if(dist > 0){
  1961. for(int j = 0;j < 2;j++){
  1962. if(deltad[j] > 0){
  1963. idx = j;
  1964. break;
  1965. }
  1966. }
  1967. }else{
  1968. for(int j = 0;j < 2;j++){
  1969. if(deltad[j] < 0){
  1970. idx = j;
  1971. break;
  1972. }
  1973. }
  1974. }
  1975. pos->nFirstReader = f1.reader_id;
  1976. pos->nSecondReader = f2.reader_id;
  1977. pos->posx = xcross[idx];
  1978. pos->posy = ycross[idx];
  1979. pos->posz = zcross[idx];
  1980. udm_pos.push_back(pos);
  1981. break;
  1982. }
  1983. }
  1984. }
  1985. else
  1986. {
  1987. //如果不存在地图集,需要检测计算结果是否在地图集上,
  1988. //根据距离的正负,后续判断计算位置取舍时使用
  1989. //无地图集的解求坐标方法如下:
  1990. double offset_d = (dist + readers_dist)/2;
  1991. double calc_x =0.0, calc_y = 0.0;
  1992. if (f1.x - f2.x < 1E-5)
  1993. {
  1994. //在y轴上
  1995. calc_x = f1.x;
  1996. if (f1.y > f2.y)
  1997. {
  1998. calc_y = f1.y - offset_d;
  1999. }
  2000. else
  2001. {
  2002. calc_y = f1.y + offset_d;
  2003. }
  2004. }else if (f1.y - f2.y < 1E-5)
  2005. {
  2006. //在x轴上
  2007. if (f1.x < f2.x)
  2008. {
  2009. calc_x = f1.x + offset_d;
  2010. }
  2011. else
  2012. {
  2013. calc_x = f1.x - offset_d;
  2014. }
  2015. calc_y = f1.y;
  2016. }
  2017. else
  2018. {
  2019. //在有斜率的地方
  2020. double arg = atan((f2.y - f1.y)/(f2.x - f1.x));
  2021. if (f1.x < f2.x && f1.y < f2.y)
  2022. {
  2023. calc_x = f1.x + cos(arg)*offset_d;
  2024. calc_y = f1.y + sin(arg)*offset_d;
  2025. }
  2026. else
  2027. {
  2028. calc_x = f1.x - cos(arg)*offset_d;
  2029. calc_y = f1.y - sin(arg)*offset_d;
  2030. }
  2031. }
  2032. pos->posx = calc_x;
  2033. pos->posy = calc_y;
  2034. pos->nFirstReader = f1.reader_id;
  2035. pos->nSecondReader = f2.reader_id;
  2036. //如果不在,则此解无效,如果存在,则此解有效
  2037. if (IsOnMap(pos,trpm))
  2038. {
  2039. udm_pos.push_back(pos);
  2040. }
  2041. }
  2042. }
  2043. if (udm_pos.size() == 0)
  2044. {
  2045. return ALGO_CALC_SOLUTION;
  2046. }
  2047. return 0;
  2048. }
  2049. bool LocateAlgorithm::CheckPosInValid(POS* pos,ReceiveDataMap* pRdm,double dScale)
  2050. {
  2051. if(dScale <= 0.0){
  2052. return false;
  2053. }
  2054. if(pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE){
  2055. return false;
  2056. }
  2057. //判断解的无效性
  2058. int nSize = 0;
  2059. bool bRet = false;
  2060. nSize = pRdm->size();
  2061. double dist[2] = {0.0};
  2062. if(nSize == 2){
  2063. int i = 0;
  2064. for(ReceiveDataMap::iterator it = pRdm->begin();it!=pRdm->end();++it,i++){
  2065. dist[i] = sqrt(pow(pos->posx - it->second->x,2)+pow(pos->posy - it->second->y,2));
  2066. if(fabs(dist[i]) < 4){
  2067. //误差在范围内,判断此分站是否属于特殊分站
  2068. if(it->second->special){
  2069. bRet = false;
  2070. }else{
  2071. bRet = true;
  2072. }
  2073. }
  2074. }
  2075. }
  2076. return bRet;
  2077. }
  2078. std::shared_ptr<POS> LocateAlgorithm::MappingToPath( std::shared_ptr<POS> pos, std::shared_ptr<TDOAReaderPathMap> trpm )
  2079. {
  2080. if(trpm->find(pos->nFirstReader) == trpm->end()){
  2081. return nullptr;
  2082. }
  2083. if(trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)==trpm->find(pos->nFirstReader)->second->end()){
  2084. return nullptr;
  2085. }
  2086. std::shared_ptr<POS> p = std::make_shared<POS>();
  2087. p = pos;
  2088. //获得地图集
  2089. std::shared_ptr<ReaderPath> pRP = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second;
  2090. // 计算两点间斜率
  2091. // 垂直
  2092. if(pRP->x[0] - pRP->x[1] < 1E-6)
  2093. {
  2094. p->posx = pRP->x[0];
  2095. }
  2096. // 水平
  2097. else if(pRP->y[0] - pRP->y[1] < 1E-6)
  2098. {
  2099. p->posy = pRP->y[0];
  2100. }
  2101. // 有斜率
  2102. else
  2103. {
  2104. // 原理 y = kx + b, y' = (-1/k) x' + b'
  2105. double k1= (pRP->y[1] - pRP->y[0]) / (pRP->x[1] - pRP->x[0]);
  2106. double k2 = -1.0 / k1;
  2107. double b1 = pRP->y[0] - k1 * pRP->x[0];
  2108. double b2 = pos->cy - k2 * pos->cx;
  2109. p->posx = (b1 - b2)/(k2 - k1);
  2110. p->posy = k2 * p->posx + b2;
  2111. }
  2112. p->posz = 0;
  2113. return p;
  2114. }
  2115. void HeapSort(_coordinate** pCoordinateArray,int nLen)
  2116. {
  2117. BuildMaxHeap(pCoordinateArray,nLen);
  2118. for(int i = nLen - 1;i > 0;i--){
  2119. SwapElement(pCoordinateArray[0],pCoordinateArray[i]);
  2120. AdjustMaxHeap(pCoordinateArray,0,i-1);
  2121. }
  2122. }
  2123. void BuildMaxHeap(_coordinate** pCoordinateArray,int nLen)
  2124. {
  2125. for(int i = nLen/2 - 1;i > 0;i--){
  2126. AdjustMaxHeap(pCoordinateArray,i,nLen-1);
  2127. }
  2128. }
  2129. void AdjustMaxHeap(_coordinate** pCoordinateArray,int n,int nHeapSize)
  2130. {
  2131. int l = (n+1)*2-1;
  2132. int r = (n+1)*2;
  2133. int max;
  2134. if(l <= nHeapSize && pCoordinateArray[l]->tt > pCoordinateArray[n]->tt){
  2135. max = l;
  2136. }else{
  2137. max = n;
  2138. }
  2139. if(r <= nHeapSize && pCoordinateArray[r]->tt > pCoordinateArray[max]->tt){
  2140. max = r;
  2141. }
  2142. if(max != n){
  2143. SwapElement(pCoordinateArray[n],pCoordinateArray[max]);
  2144. AdjustMaxHeap(pCoordinateArray,max,nHeapSize);
  2145. }
  2146. }
  2147. void SwapElement(_coordinate* a,_coordinate*b)
  2148. {
  2149. _coordinate tmp;
  2150. tmp = *a;
  2151. *a = *b;
  2152. *b = tmp;
  2153. }
  2154. void SelectSort(_coordinate** pCoordinateArray,int nLen)
  2155. {
  2156. for (int i=0; i<nLen; i++)
  2157. {
  2158. int k = i;
  2159. unsigned long long key = pCoordinateArray[i]->tt;
  2160. for (int j=i+1; j<nLen; j++)
  2161. {
  2162. if (pCoordinateArray[j]->tt < key)
  2163. {
  2164. k = j;
  2165. key = pCoordinateArray[j]->tt;
  2166. }
  2167. }
  2168. if (k!=i)
  2169. SwapElement(pCoordinateArray[i], pCoordinateArray[k]);
  2170. }
  2171. }