locate_algorithm.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  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::Pos(std::shared_ptr<TOFReaderPathMap> trpm,int reader_id,int antenna_idx,double dist,TOF_REFER_DATA refer_data)
  542. {
  543. std::shared_ptr<POS> p = std::make_shared<POS>();
  544. double d[MAX_READER_TDOA_PATH_NUMS] = {0};
  545. double xcross[MAX_READER_TDOA_PATH_NUMS] = {0};
  546. double ycross[MAX_READER_TDOA_PATH_NUMS] = {0};
  547. int seg_num = 0;
  548. seg_num = trpm->find(reader_id)->second->nRealCalcPoints - 1;
  549. //判断路径集的点数是否大于两个点,两点才可以构成线段路径
  550. if(seg_num < 0 ){
  551. return nullptr;
  552. }
  553. int j = 0;
  554. int m = 0;
  555. int nCounts = 0;
  556. //对每一个线段所在的直线和以天线为圆心的圆求解,最多为两个解
  557. for(int i = 0;i < seg_num;i++){
  558. //如果线段两端点的坐标不存在,则寻找下一个线段
  559. if(trpm->find(reader_id)->second->px[i] == -1 || trpm->find(reader_id)->second->px[i+1] == -1)
  560. {
  561. continue;
  562. }
  563. //求线段所在直线和分站readerid的antenna_idx天线为圆心,半径为dist的圆的两个交点
  564. std::shared_ptr<SOLUTION> r = GetPos(trpm,reader_id,antenna_idx,dist,i);
  565. //如果解不存在,则继续寻找下一个线段
  566. if(r == nullptr || r->nCount <= 0){
  567. continue;
  568. }
  569. int k = -1;
  570. double c = 999999.9;
  571. //如果解存在,则保存两个解
  572. for(j = 0;j < r->nCount;j++){
  573. //保存解
  574. xcross[j] = r->x[j];
  575. ycross[j] = r->y[j];
  576. //计算解与参考分站中的天线坐标的距离distance
  577. double d1 = xcross[j] - trpm->find(refer_data.nReaderId)->second->x[refer_data.nAntennaIndex];
  578. double d2 = ycross[j] - trpm->find(refer_data.nReaderId)->second->y[refer_data.nAntennaIndex];
  579. double distance = sqrt(pow(d1,2) + pow(d2,2));
  580. d[j] = distance;
  581. }
  582. for (j = 0;j < r->nCount; j++)
  583. {
  584. if (d[j] < c)
  585. {
  586. c = d[j];
  587. k = j;
  588. }
  589. }
  590. if(c < p->pos_radius){
  591. p->posx = xcross[k];
  592. p->posy = ycross[k];
  593. p->pos_radius = c;
  594. }
  595. }
  596. return p;
  597. }
  598. std::shared_ptr<POS> LocateAlgorithm::TdoaLocate2d(std::shared_ptr<ReceiveDataMap> pRdm)
  599. {
  600. if (pRdm->size() < 3)
  601. {
  602. return nullptr;
  603. }
  604. std::shared_ptr<POS> p = std::make_shared<POS>();
  605. //2D定位
  606. std::vector<double> vtk; //保存ki
  607. std::vector<double> vtd; //保存di
  608. std::vector<_coordinate> vtc; //保存xi,1 yi,1
  609. std::vector<_point> vtp; //保存三角形的顶点坐标
  610. std::vector<int> vts;
  611. vtk.resize(0);
  612. vtd.resize(0);
  613. vtc.resize(0);
  614. vts.resize(0);
  615. _coordinate tc;
  616. int i = 0;
  617. unsigned long long time_stamp = 0; //保存第一次的插值时间戳
  618. for (ReceiveDataMap::iterator it = pRdm->begin();it != pRdm->end()&&i<3;++it)
  619. {
  620. double k = 0 ;
  621. k = pow(it->second->x,2) + pow(it->second->y,2) ;//+ pow(it->second->z,2)
  622. vtk.push_back(k);
  623. if (i == 0 )
  624. {
  625. time_stamp = it->second->rec_time_stamp;
  626. tc.x = it->second->x;
  627. tc.y = it->second->y;
  628. //tc.z = it->second->z;
  629. }
  630. long long diff_time = it->second->rec_time_stamp - time_stamp;
  631. double d = 0;
  632. d = CFunctions::getDistance(diff_time,CFunctions::TDOA);
  633. if (i>0)
  634. {
  635. if (d>0)
  636. {
  637. vts.push_back(1);
  638. }
  639. else
  640. {
  641. vts.push_back(-1);
  642. }
  643. }
  644. vtd.push_back(d);
  645. _coordinate dtc;
  646. dtc.x = it->second->x - tc.x;
  647. dtc.y = it->second->y - tc.y;
  648. //dtc.z = it->second->z - tc.z;
  649. vtc.push_back(dtc);
  650. _point p;
  651. p.x = it->second->x;
  652. p.y = it->second->y;
  653. vtp.push_back(p);
  654. i++;
  655. }
  656. double a[20] = {0};
  657. double dt = 0;
  658. dt = vtd[1]*vtc[2].y - vtd[2]*vtc[1].y;
  659. if (fabs(dt) < 1E-5)
  660. {
  661. return nullptr;
  662. }
  663. a[0] = (vtd[2]*vtc[1].x - vtd[1]*vtc[2].x)/dt;
  664. 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;
  665. if (fabs(vtd[1]) < 1E-5)
  666. {
  667. return nullptr;
  668. }
  669. a[2] = 0.5*(vtk[1] - vtk[0] - pow(vtd[1],2) - 2*vtc[1].y*a[1])/vtd[1];
  670. a[3] = -1*(vtc[1].x + vtc[1].y*a[0])/vtd[1];
  671. double A = 0, B = 0, C = 0;
  672. A = pow(a[3],2) - 1 - pow(a[0],2);
  673. B = 2*(a[2]*a[3] + tc.x + a[0]*(tc.y - a[1]));
  674. C = pow(a[2],2) - pow(tc.x,2) - pow(tc.y-a[1],2);
  675. _point pos[MAX_READER_TDOA_PATH_NUMS];
  676. double delta = 0.0;
  677. int count = 0;
  678. delta = pow(B,2) - 4*A*C;
  679. if (delta > 0)
  680. {
  681. pos[0].x = ((-1)*B + sqrt(delta))/(2*A);
  682. pos[0].y = a[0]*pos[0].x + a[1];
  683. pos[1].x = ((-1)*B - sqrt(delta))/(2*A);
  684. pos[1].y = a[0]*pos[1].x + a[1];
  685. count = 2;
  686. }else{
  687. return nullptr;
  688. }
  689. int idx = -1;
  690. int points = 0;
  691. for (int i=0;i<count;i++)
  692. {
  693. if (IsInTriangle(vtp,pos[i]))
  694. {
  695. bool condition[2] = {false,false};
  696. double d1 = 0,d2 = 0;
  697. //对两解进行判断,需要同时满足两个条件
  698. //1.解到点1和点2的距离差的方向性和之前的参数相同
  699. //2.解到点1和点3的距离差的方向性和之前的参数相同
  700. 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));
  701. 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));
  702. if ((d1<0&&vts[0]==-1)||(d1>0&&vts[0] == 1))
  703. {
  704. condition[0] = true;
  705. }
  706. if ((d2<0&&vts[1]==-1)||(d2>0&&vts[1] == 1))
  707. {
  708. condition[1] =true;
  709. }
  710. if (condition[0]&&condition[1])
  711. {
  712. idx = i;
  713. }
  714. //idx = i;
  715. }else{
  716. }
  717. }
  718. if (points == 2)
  719. {
  720. idx = -1;
  721. }
  722. if (idx != -1)
  723. {
  724. p->posx = pos[idx].x;
  725. p->posy = pos[idx].y;
  726. }else{
  727. return nullptr;
  728. }
  729. return p;
  730. }
  731. /*
  732. * TDOA算法实现
  733. * 函数名:LocatePos,此函数与Pos函数的区别是本函数两两遍历求坐标
  734. * 遍历求解
  735. * param
  736. * pRdm ------ 存放参与计算的分站信息,主要信息包含同步后的时间戳
  737. * trpm ------ 地图集
  738. *
  739. * return
  740. * 返回最终结果坐标
  741. *
  742. */
  743. std::unique_ptr<POS> LocateAlgorithm::LocatePos(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm)
  744. {
  745. std::unique_ptr<POS> pos(new POS);
  746. int nNoReaderPathIdx = 0;
  747. int nFirstReader[MAX_READER_TDOA_PATH_NUMS] = {-1};
  748. int nSecondReader[MAX_READER_TDOA_PATH_NUMS] ={-1};
  749. _coordinate r;
  750. int r_idx = 0;
  751. r.reason = 0;
  752. for (ReceiveDataMap::iterator first = pRdm->begin();first != pRdm->end();++first)
  753. {
  754. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  755. TDOAReaderPathMap::iterator rdm_it = trpm->find(first->second->reader_id);
  756. if(rdm_it == trpm->end()){
  757. continue;
  758. }
  759. double ref_dist = 0.0; //两分站之间无路径的距离差
  760. _coordinate res[MAX_READER_TDOA_PATH_NUMS];
  761. int res_idx = 0;
  762. for(int i = 0;i < MAX_READER_TDOA_PATH_NUMS;i++){
  763. res[i].x = INVALID_COORDINATE;
  764. res[i].y = INVALID_COORDINATE;
  765. res[i].z = INVALID_COORDINATE;
  766. }
  767. //存储无路径的两分站的id和坐标
  768. ReceiveData tmp_reader[MAX_READER_TDOA_PATH_NUMS];
  769. //存储和第一条分站存在路径的分站信息
  770. ReceiveData tmp_dist_reader[MAX_READER_TDOA_PATH_NUMS];
  771. //和第一个分站存在地图集的分站个数,在丁字或十字路口,数量可能为3个或4个
  772. int nDistReaders = 0;
  773. //获取第一个时间戳
  774. ReceiveData f1;
  775. f1.antenna_id = first->second->antenna_id;
  776. f1.reader_id = first->second->reader_id;
  777. f1.rec_time_stamp = first->second->rec_time_stamp;
  778. f1.x = first->second->x;
  779. f1.y = first->second->y;
  780. f1.z = first->second->z;
  781. ReceiveDataMap::iterator second = first;
  782. std::advance(second,1);
  783. //从第二个开始遍历
  784. for(;second != pRdm->end();++second){
  785. //获取第二个时间戳
  786. ReceiveData f2;
  787. f2.antenna_id = second->second->antenna_id;
  788. f2.reader_id = second->second->reader_id;
  789. f2.rec_time_stamp = second->second->rec_time_stamp;
  790. f2.x = second->second->x;
  791. f2.y = second->second->y;
  792. f2.z = second->second->z;
  793. //时间戳异常
  794. if(f1.rec_time_stamp == LLONG_MAX || f2.rec_time_stamp == LLONG_MAX){
  795. continue;
  796. }
  797. if(f1.reader_id == f2.reader_id){
  798. continue;
  799. }
  800. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  801. if(rpm_it == trpm->find(f1.reader_id)->second->end()){
  802. continue;
  803. }
  804. //根据距离的正负,后续判断计算位置取舍时使用
  805. int nSign = 1;
  806. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  807. //计算位置
  808. double dist = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  809. double readers_dist = sqrt(pow(f1.x - f2.x,2) + pow(f1.y - f2.y,2));
  810. if(fabs(dist) - readers_dist > 0){
  811. continue;
  812. }
  813. //如果和第一条分站存在地图集
  814. tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  815. tmp_dist_reader[nDistReaders].x = f2.x;
  816. tmp_dist_reader[nDistReaders].y = f2.y;
  817. tmp_dist_reader[nDistReaders].z = f2.z;
  818. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  819. nDistReaders++;
  820. std::shared_ptr<ReaderPath> pRP = trpm->find(f1.reader_id)->second->find(f2.reader_id)->second;
  821. tmp_reader[nNoReaderPathIdx].reader_id = f2.reader_id;
  822. tmp_reader[nNoReaderPathIdx].x = f2.x;
  823. tmp_reader[nNoReaderPathIdx].y = f2.y;
  824. tmp_reader[nNoReaderPathIdx].z = f2.z;
  825. tmp_reader[nNoReaderPathIdx].rec_time_stamp = f2.rec_time_stamp;
  826. nNoReaderPathIdx++;
  827. //两分站之间的线段个数
  828. int seg_num = pRP->nRealCalcPoints - 1;
  829. if(seg_num == 0 || seg_num > 100){
  830. continue;
  831. }
  832. //因为双曲线与分站之间第i条线段或者第j条线段分别有两焦点
  833. //或者分站之间就一条直线,有两焦点
  834. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  835. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  836. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  837. int nIdx = 0;
  838. //根据线段个数开始计算
  839. for(int i = 0;i < seg_num;i++){
  840. //计算位置坐标,双曲线和线段相交的交点
  841. std::unique_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pRP,dist,i);
  842. //无解或解无效
  843. if(r == nullptr ){
  844. continue;
  845. }
  846. if(r->nCount == 0){
  847. continue;
  848. }
  849. if(r->nCount == 1){
  850. xcross[nIdx] = r->x[0];
  851. ycross[nIdx] = r->y[0];
  852. res[res_idx].x = xcross[nIdx];
  853. res[res_idx].y = ycross[nIdx];
  854. res[res_idx].z = zcross[nIdx];
  855. nIdx++;
  856. res_idx++;
  857. }
  858. if(r->nCount == 2){
  859. for(int j = 0; j < 2;j++){
  860. xcross[j] = r->x[j];
  861. ycross[j] = r->y[j];
  862. nIdx++;
  863. }
  864. }
  865. if(nIdx == 2){
  866. //解到两焦点之间的距离
  867. double deltad[2] = {0};
  868. for(int j = 0; j < 2;j ++){
  869. double d[2] = {0};
  870. double dx1 = xcross[j] - pRP->x[0];
  871. double dy1 = ycross[j] - pRP->y[0];
  872. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  873. double dx2 = xcross[j] - pRP->x[1];
  874. double dy2 = ycross[j] - pRP->y[1];
  875. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  876. deltad[j] = d[0] - d[1];
  877. }
  878. int idx = 0;
  879. //应该和之前计算的dist同方向
  880. if(dist > 0){
  881. for(int j = 0;j < 2;j++){
  882. if(deltad[j] > 0){
  883. idx = j;
  884. break;
  885. }
  886. }
  887. }else{
  888. for(int j = 0;j < 2;j++){
  889. if(deltad[j] < 0){
  890. idx = j;
  891. break;
  892. }
  893. }
  894. }
  895. nFirstReader[res_idx] = f1.reader_id;
  896. nSecondReader[res_idx] = f2.reader_id;
  897. res[res_idx].x = xcross[idx];
  898. res[res_idx].y = ycross[idx];
  899. res[res_idx].z = zcross[idx];
  900. res_idx++;
  901. }
  902. continue;
  903. }
  904. }
  905. if(res_idx == 1){
  906. bool bValid = true;
  907. //如果定位坐标在分站附近,需要判断此分站是否为special,如果为special则接受此定位结果
  908. for(ReceiveDataMap::iterator it = pRdm->begin();it!=pRdm->end();++it){
  909. double dist = sqrt(pow(res[0].x - it->second->x,2) + pow(res[0].y - it->second->y,2));
  910. if(dist<4){
  911. if(it->second->special == 0){
  912. r.reason = ALGO_FAILED_CONDITION_12;
  913. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_12);
  914. bValid = false;
  915. }
  916. }
  917. }
  918. if(pRdm->size()>=3&&bValid){
  919. double dist[2] = {0};
  920. //更改选取分站方式:
  921. //选取和第一个分站有地图集的两个分站来参与判别,
  922. //例如:分站顺序是2,1,3,其中(1,2)和(2,3)之间有地图集,那么就选取1,3来参与判别
  923. //如果分站顺序是1,2,3,其中(1,2)有地图集,(1,3)之间无地图集,那么此时就不使用以下方法进行判别
  924. if(nDistReaders == 2){
  925. dist[0] = sqrt(pow(res[0].x - tmp_dist_reader[0].x,2) + pow(res[0].y - tmp_dist_reader[0].y,2));
  926. dist[1] = sqrt(pow(res[0].x - tmp_dist_reader[1].x,2) + pow(res[0].y - tmp_dist_reader[1].y,2));
  927. double dif1 = fabs(dist[0] - dist[1]);
  928. long long dt = tmp_dist_reader[0].rec_time_stamp - tmp_dist_reader[1].rec_time_stamp;
  929. double dif2 = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  930. double dif = fabs(dif1 - dif2);
  931. if(dif>10){
  932. r.reason = ALGO_FAILED_CONDITION_13;
  933. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_13);
  934. bValid = false;
  935. }
  936. }
  937. }
  938. if(bValid){
  939. r.x = res[0].x;
  940. r.y = res[0].y;
  941. r.z = res[0].z;
  942. for (int i=0;i<MAX_READER_TDOA_PATH_NUMS;i++)
  943. {
  944. pos->dDiff[i] = 0;
  945. }
  946. }
  947. }else if(res_idx >= 2){
  948. double d = 99999.9;
  949. long long dt = tmp_reader[0].rec_time_stamp - tmp_reader[1].rec_time_stamp;
  950. //计算位置
  951. double ref_dist = abs(CFunctions::getDistance(dt,CFunctions::TDOA));
  952. for(int i = 0;i<res_idx;i++){
  953. 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)));
  954. double d_diff = abs(d_tmp - ref_dist);
  955. pos->dDiff[i] = d_diff;
  956. if(d_diff < d){
  957. d = d_diff;
  958. r_idx = i;
  959. }
  960. }
  961. if(pRdm->size()>=3){
  962. if(pos->dDiff[r_idx] < 4){
  963. r.x = res[r_idx].x;
  964. r.y = res[r_idx].y;
  965. r.z = res[r_idx].z;
  966. }else{
  967. r.reason = ALGO_FAILED_CONDITION_14;
  968. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_14);
  969. }
  970. }
  971. }else{
  972. //解的个数<=0
  973. continue;
  974. }
  975. //表示找到解了,后面的数据都不可信
  976. if (r.x != INVALID_COORDINATE && r.y != INVALID_COORDINATE)
  977. {
  978. break;
  979. }
  980. }
  981. //最终得到的结果解
  982. pos->posx = r.x;
  983. pos->posy = r.y;
  984. pos->posz = 0;
  985. pos->reason = r.reason;
  986. if(pos->posx != INVALID_COORDINATE && pos->posy != INVALID_COORDINATE){
  987. pos->nFirstReader = nFirstReader[r_idx];
  988. pos->nSecondReader = nSecondReader[r_idx];
  989. }
  990. return pos;
  991. }
  992. SOLUTION* LocateAlgorithm::GetPos(ReaderPathMap rpm,int sta_num,int ant,double dist,int i)
  993. {
  994. double x1 = 0.0;
  995. double y1 = 0.0;
  996. double x2 = 0.0;
  997. double y2 = 0.0;
  998. double deta = 0.0; //b^2 - 4*a*c;
  999. int count = 0; //方程解的个数
  1000. if((rpm.find(sta_num)->second->px[i] - rpm.find(sta_num)->second->px[i+1]) == 0){
  1001. //方程一的解
  1002. x1 = rpm.find(sta_num)->second->px[i];
  1003. y1 = rpm.find(sta_num)->second->y[ant] - dist;
  1004. x2 = rpm.find(sta_num)->second->px[i];
  1005. y2 = dist + rpm.find(sta_num)->second->y[ant];
  1006. count = 2;
  1007. }else{
  1008. //计算斜率
  1009. 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]);
  1010. //方程二
  1011. //确保有两解的情况
  1012. //圆心为s[sta_num].x[ant],s[sta_num].y[ant];
  1013. double a = pow(k,2) + 1;
  1014. 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]);
  1015. double d = rpm.find(sta_num)->second->py[i] - k * rpm.find(sta_num)->second->px[i] - rpm.find(sta_num)->second->y[ant];
  1016. double c = pow(rpm.find(sta_num)->second->x[ant],2) + pow(d,2) - pow(dist,2);
  1017. //根据deta = b^2 - 4*a*c 判断方程是否有解
  1018. deta = pow(b,2) - 4*a*c; //
  1019. if(deta>0){
  1020. count = 2;
  1021. }else if(deta == 0){
  1022. count = 1;
  1023. }
  1024. else{
  1025. count = 0;
  1026. }
  1027. if(count == 0){
  1028. return NULL;
  1029. }
  1030. //得两解
  1031. x1 = -(b + sqrt(deta))/(2*a);
  1032. x2 = (-b + sqrt(deta) )/(2*a);
  1033. y1 = k*x1 + rpm.find(sta_num)->second->py[i] - k*rpm.find(sta_num)->second->px[i];
  1034. y2 = k*x2 + rpm.find(sta_num)->second->py[i] - k*rpm.find(sta_num)->second->px[i];
  1035. }
  1036. double x[3] = {0,0,0};
  1037. double y[3] = {0,0,0};
  1038. x[0] = x1;
  1039. x[1] = x2;
  1040. y[0] = y1;
  1041. y[1] = y2;
  1042. if(count>0){
  1043. for(int t = 0;t<count;t++){
  1044. if(x[t] < min(rpm.find(sta_num)->second->px[i],rpm.find(sta_num)->second->px[i+1]) - 0.01
  1045. ||x[t] > max(rpm.find(sta_num)->second->px[i],rpm.find(sta_num)->second->px[i+1]) + 0.01
  1046. ||y[t] < min(rpm.find(sta_num)->second->py[i],rpm.find(sta_num)->second->py[i+1])
  1047. ||y[t] > max(rpm.find(sta_num)->second->py[i],rpm.find(sta_num)->second->py[i+1])){
  1048. x[t] = 0;
  1049. y[t] = 0;
  1050. }
  1051. }
  1052. }
  1053. SOLUTION* result = new SOLUTION;
  1054. memset(result->x,0,3*sizeof(double));
  1055. memset(result->y,0,3*sizeof(double));
  1056. memset(result->z,0,3*sizeof(double));
  1057. result->x[0] = x[0];
  1058. result->x[1] = x[1];
  1059. result->y[0] = y[0];
  1060. result->y[1] = y[1];
  1061. result->z[0] = 0;
  1062. result->z[1] = 0;
  1063. return result;
  1064. }
  1065. /*
  1066. *
  1067. * param
  1068. * trpm
  1069. * sta_num
  1070. * ant
  1071. * dist
  1072. * i
  1073. *
  1074. * return
  1075. *
  1076. */
  1077. std::shared_ptr<SOLUTION> LocateAlgorithm::GetPos(std::shared_ptr<TOFReaderPathMap> trpm,int reader_id,int antenna_idx,double dist,int seg_idx)
  1078. {
  1079. std::shared_ptr<SOLUTION> s = std::make_shared<SOLUTION>();
  1080. double x1 = 0.0;
  1081. double y1 = 0.0;
  1082. double x2 = 0.0;
  1083. double y2 = 0.0;
  1084. double deta = 0.0; //b^2 - 4*a*c;
  1085. int count = 0; //方程解的个数
  1086. if(abs(trpm->find(reader_id)->second->px[seg_idx] - trpm->find(reader_id)->second->px[seg_idx + 1]) < ZERO_PRECISION){
  1087. //方程一的解
  1088. x1 = trpm->find(reader_id)->second->px[seg_idx];
  1089. y1 = trpm->find(reader_id)->second->y[antenna_idx] - dist;
  1090. x2 = trpm->find(reader_id)->second->px[seg_idx];
  1091. y2 = dist + trpm->find(reader_id)->second->y[antenna_idx];
  1092. count = 2;
  1093. }/*else if(abs(trpm->find(sta_num)->second->py[i] - trpm->find(sta_num)->second->py[i+1]) < ZERO_PRECISION){
  1094. y1 = y2 = trpm->find(sta_num)->second->py[i];
  1095. x1 = trpm->find(sta_num)->second->y[ant] - dist;
  1096. x2 = trpm->find(sta_num)->second->y[ant] + dist;
  1097. count = 2;
  1098. }*/else{
  1099. //计算斜率
  1100. double k = (trpm->find(reader_id)->second->py[seg_idx] - trpm->find(reader_id)->second->py[seg_idx + 1])/(trpm->find(reader_id)->second->px[seg_idx] - trpm->find(reader_id)->second->px[seg_idx + 1]);
  1101. //double b = trpm->find(reader_id)->second->y[antenna_idx] - k*trpm->find(reader_id)->second->x[antenna_idx];
  1102. double b = trpm->find(reader_id)->second->py[seg_idx] - k*trpm->find(reader_id)->second->px[seg_idx];
  1103. double A = pow(k,2) + 1;
  1104. double B = 2*k*(b - trpm->find(reader_id)->second->y[antenna_idx]) - 2*trpm->find(reader_id)->second->x[antenna_idx];
  1105. double C = pow(trpm->find(reader_id)->second->x[antenna_idx],2) + pow(b - trpm->find(reader_id)->second->y[antenna_idx],2) - pow(dist,2);
  1106. deta = pow(B,2) - 4*A*C;
  1107. //方程二
  1108. //确保有两解的情况
  1109. //圆心为s[sta_num].x[ant],s[sta_num].y[ant];
  1110. //double a = pow(k,2) + 1;
  1111. //double b = 2*(k*(trpm->find(sta_num)->second->py[i] - k*trpm->find(sta_num)->second->px[i] - trpm->find(sta_num)->second->y[ant])- trpm->find(sta_num)->second->x[ant]);
  1112. //double d = trpm->find(sta_num)->second->py[i] - k * trpm->find(sta_num)->second->px[i] - trpm->find(sta_num)->second->y[ant];
  1113. ////double b = 2*k*d - 2*trpm->find(sta_num)->second->px[i];
  1114. //double c = pow(trpm->find(sta_num)->second->x[ant],2) + pow(d,2) - pow(dist,2);
  1115. ////根据deta = b^2 - 4*a*c 判断方程是否有解
  1116. //deta = pow(b,2) - 4*a*c; //
  1117. if(deta>0){
  1118. count = 2;
  1119. }else if(deta == 0){
  1120. count = 1;
  1121. }
  1122. else{
  1123. count = 0;
  1124. }
  1125. if(count == 0){
  1126. return NULL;
  1127. }
  1128. //得两解
  1129. x1 = -(B + sqrt(deta))/(2*A);
  1130. x2 = (-B + sqrt(deta) )/(2*A);
  1131. //x1 = -(b + sqrt(deta))/(2*a);
  1132. //x2 = (-b + sqrt(deta) )/(2*a);
  1133. y1 = k*x1 + trpm->find(reader_id)->second->py[seg_idx] - k*trpm->find(reader_id)->second->px[seg_idx];
  1134. y2 = k*x2 + trpm->find(reader_id)->second->py[seg_idx] - k*trpm->find(reader_id)->second->px[seg_idx];
  1135. }
  1136. double x[3] = {0,0,0};
  1137. double y[3] = {0,0,0};
  1138. x[0] = x1;
  1139. x[1] = x2;
  1140. y[0] = y1;
  1141. y[1] = y2;
  1142. int nValidCount = count;
  1143. if(count > 0){
  1144. for(int t = 0;t < count;t++){
  1145. if(x[t] < min(trpm->find(reader_id)->second->px[seg_idx],trpm->find(reader_id)->second->px[seg_idx+1])
  1146. ||x[t] > max(trpm->find(reader_id)->second->px[seg_idx],trpm->find(reader_id)->second->px[seg_idx+1])
  1147. ||y[t] < min(trpm->find(reader_id)->second->py[seg_idx],trpm->find(reader_id)->second->py[seg_idx+1])
  1148. ||y[t] > max(trpm->find(reader_id)->second->py[seg_idx],trpm->find(reader_id)->second->py[seg_idx+1])){
  1149. x[t] = INVALID_COORDINATE;
  1150. y[t] = INVALID_COORDINATE;
  1151. nValidCount--;
  1152. }
  1153. }
  1154. }
  1155. s->nCount = nValidCount;
  1156. switch (s->nCount)
  1157. {
  1158. case 1:
  1159. for (int i = 0;i < count;i++)
  1160. {
  1161. if (x[i] != INVALID_COORDINATE && y[i] != INVALID_COORDINATE)
  1162. {
  1163. s->x[0] = x[i];
  1164. s->y[0] = y[i];
  1165. break;
  1166. }
  1167. }
  1168. break;
  1169. case 2:
  1170. for (int i = 0;i < s->nCount;i++)
  1171. {
  1172. s->x[i] = x[i];
  1173. s->y[i] = y[i];
  1174. }
  1175. break;
  1176. default:
  1177. break;
  1178. }
  1179. return s;
  1180. }
  1181. /*
  1182. * TDOA算法实现
  1183. * 函数名:Pos
  1184. * 遍历求解
  1185. * param
  1186. * pRdm ------ 存放参与计算的分站信息,主要信息包含同步后的时间戳
  1187. * trpm ------ 地图集
  1188. *
  1189. * return
  1190. * 返回最终结果坐标
  1191. *
  1192. */
  1193. std::unique_ptr<POS> LocateAlgorithm::Pos(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm)
  1194. {
  1195. std::unique_ptr<POS> pos(new POS);
  1196. double a = 0;
  1197. double ref_dist = 0.0; //两分站之间无路径的距离差
  1198. _coordinate res[MAX_READER_TDOA_PATH_NUMS];
  1199. int res_idx = 0;
  1200. for(int i = 0;i < MAX_READER_TDOA_PATH_NUMS;i++){
  1201. res[i].x = INVALID_COORDINATE;
  1202. res[i].y = INVALID_COORDINATE;
  1203. res[i].z = INVALID_COORDINATE;
  1204. }
  1205. bool bFirst = true;
  1206. int nNoReaderPathIdx = 0;
  1207. int nFirstReader[MAX_READER_TDOA_PATH_NUMS] = {-1};
  1208. int nSecondReader[MAX_READER_TDOA_PATH_NUMS] ={-1};
  1209. //存储无路径的两分站的id和坐标
  1210. ReceiveData tmp_reader[MAX_READER_TDOA_PATH_NUMS];
  1211. //存储和第一条分站存在路径的分站信息
  1212. ReceiveData tmp_dist_reader[MAX_READER_TDOA_PATH_NUMS];
  1213. int nDistReaders = 0;
  1214. ReceiveDataMap::iterator first = pRdm->begin();
  1215. ReceiveDataMap::iterator second = first;
  1216. //偏移到第二个元素
  1217. std::advance(second,1);
  1218. //获取第一个时间戳
  1219. ReceiveData f1;
  1220. f1.antenna_id = first->second->antenna_id;
  1221. f1.reader_id = first->second->reader_id;
  1222. f1.rec_time_stamp = first->second->rec_time_stamp;
  1223. f1.x = first->second->x;
  1224. f1.y = first->second->y;
  1225. f1.z = first->second->z;
  1226. //从第二个开始遍历
  1227. for(;second != pRdm->end();++second){
  1228. //获取第二个时间戳
  1229. ReceiveData f2;
  1230. f2.antenna_id = second->second->antenna_id;
  1231. f2.reader_id = second->second->reader_id;
  1232. f2.rec_time_stamp = second->second->rec_time_stamp;
  1233. f2.x = second->second->x;
  1234. f2.y = second->second->y;
  1235. f2.z = second->second->z;
  1236. //时间戳异常
  1237. if(f1.rec_time_stamp == LLONG_MAX || f2.rec_time_stamp == LLONG_MAX){
  1238. continue;
  1239. }
  1240. if(f1.reader_id == f2.reader_id){
  1241. continue;
  1242. }
  1243. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1244. TDOAReaderPathMap::iterator rdm_it = trpm->find(f1.reader_id);
  1245. if(rdm_it == trpm->end()){
  1246. continue;
  1247. }
  1248. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  1249. if(rpm_it == trpm->find(f1.reader_id)->second->end()){
  1250. continue;
  1251. }else{
  1252. //如果和第一条分站存在地图集
  1253. tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  1254. tmp_dist_reader[nDistReaders].x = f2.x;
  1255. tmp_dist_reader[nDistReaders].y = f2.y;
  1256. tmp_dist_reader[nDistReaders].z = f2.z;
  1257. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  1258. nDistReaders++;
  1259. }
  1260. //根据距离的正负,后续判断计算位置取舍时使用
  1261. int nSign = 1;
  1262. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  1263. //计算位置
  1264. double dist = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  1265. double readers_dist = sqrt(pow(f1.x - f2.x,2) + pow(f1.y - f2.y,2));
  1266. if(fabs(dist) - readers_dist > 0){
  1267. continue;
  1268. }
  1269. std::shared_ptr<ReaderPath> pRP = trpm->find(f1.reader_id)->second->find(f2.reader_id)->second;
  1270. //如果和第一条分站存在地图集
  1271. tmp_reader[nNoReaderPathIdx].reader_id = f2.reader_id;
  1272. tmp_reader[nNoReaderPathIdx].x = f2.x;
  1273. tmp_reader[nNoReaderPathIdx].y = f2.y;
  1274. tmp_reader[nNoReaderPathIdx].z = f2.z;
  1275. tmp_reader[nNoReaderPathIdx].rec_time_stamp = f2.rec_time_stamp;
  1276. nNoReaderPathIdx++;
  1277. //两分站之间的线段个数
  1278. int seg_num = pRP->nRealCalcPoints - 1;
  1279. if(seg_num == 0 || seg_num > 100){
  1280. continue;
  1281. }
  1282. //因为双曲线与分站之间第i条线段或者第j条线段分别有两焦点
  1283. //或者分站之间就一条直线,有两焦点
  1284. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1285. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1286. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1287. int nIdx = 0;
  1288. //根据线段个数开始计算
  1289. for(int i = 0;i < seg_num;i++){
  1290. //计算位置坐标,双曲线和线段相交的交点
  1291. std::unique_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pRP,dist,i);
  1292. //无解或解无效
  1293. if(r == nullptr ||r->nCount == 0){
  1294. continue;
  1295. }
  1296. if(r->nCount == 1){
  1297. xcross[nIdx] = r->x[0];
  1298. ycross[nIdx] = r->y[0];
  1299. res[res_idx].x = xcross[nIdx];
  1300. res[res_idx].y = ycross[nIdx];
  1301. res[res_idx].z = zcross[nIdx];
  1302. nIdx++;
  1303. res_idx++;
  1304. }
  1305. if(r->nCount == 2){
  1306. for(int j = 0; j < 2;j++){
  1307. xcross[j] = r->x[j];
  1308. ycross[j] = r->y[j];
  1309. nIdx++;
  1310. }
  1311. }
  1312. if(nIdx == 2){
  1313. //解到两焦点之间的距离
  1314. double deltad[2] = {0};
  1315. for(int j = 0; j < 2;j ++){
  1316. double d[2] = {0};
  1317. double dx1 = xcross[j] - pRP->x[0];
  1318. double dy1 = ycross[j] - pRP->y[0];
  1319. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  1320. double dx2 = xcross[j] - pRP->x[1];
  1321. double dy2 = ycross[j] - pRP->y[1];
  1322. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  1323. deltad[j] = d[0] - d[1];
  1324. }
  1325. int idx = 0;
  1326. //应该和之前计算的dist同方向
  1327. if(dist > 0){
  1328. for(int j = 0;j < 2;j++){
  1329. if(deltad[j] > 0){
  1330. idx = j;
  1331. break;
  1332. }
  1333. }
  1334. }else{
  1335. for(int j = 0;j < 2;j++){
  1336. if(deltad[j] < 0){
  1337. idx = j;
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. nFirstReader[res_idx] = f1.reader_id;
  1343. nSecondReader[res_idx] = f2.reader_id;
  1344. res[res_idx].x = xcross[idx];
  1345. res[res_idx].y = ycross[idx];
  1346. res[res_idx].z = zcross[idx];
  1347. res_idx++;
  1348. }
  1349. continue;
  1350. }
  1351. }
  1352. _coordinate r;
  1353. r.x = INVALID_COORDINATE;
  1354. r.y = INVALID_COORDINATE;
  1355. r.z = INVALID_COORDINATE;
  1356. if(res_idx == 1){
  1357. bool bValid = true;
  1358. //如果定位坐标在分站附近,需要判断此分站是否为special,如果为special则接受此定位结果
  1359. for(ReceiveDataMap::iterator it = pRdm->begin();it!=pRdm->end();++it){
  1360. double dist = sqrt(pow(res[0].x - it->second->x,2) + pow(res[0].y - it->second->y,2));
  1361. if(dist < 4){
  1362. if(it->second->special == 0){
  1363. r.reason = ALGO_FAILED_CONDITION_12;
  1364. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_12);
  1365. bValid = false;
  1366. }
  1367. }
  1368. }
  1369. if(pRdm->size()>=3&&bValid){
  1370. double dist[2] = {0};
  1371. //更改选取分站方式:
  1372. //选取和第一个分站有地图集的两个分站来参与判别,
  1373. //例如:分站顺序是2,1,3,其中(1,2)和(2,3)之间有地图集,那么就选取1,3来参与判别
  1374. //如果分站顺序是1,2,3,其中(1,2)有地图集,(1,3)之间无地图集,那么此时就不使用以下方法进行判别
  1375. if(nDistReaders == 2){
  1376. //如果第二个分站与这两个分站无地图集则不该使用如下逻辑
  1377. dist[0] = sqrt(pow(res[0].x - tmp_dist_reader[0].x,2) + pow(res[0].y - tmp_dist_reader[0].y,2));
  1378. dist[1] = sqrt(pow(res[0].x - tmp_dist_reader[1].x,2) + pow(res[0].y - tmp_dist_reader[1].y,2));
  1379. double dif1 = fabs(dist[0] - dist[1]);
  1380. long long dt = tmp_dist_reader[0].rec_time_stamp - tmp_dist_reader[1].rec_time_stamp;
  1381. double dif2 = fabs(CFunctions::getDistance(dt,CFunctions::TDOA));
  1382. double dif = fabs(dif1 - dif2);
  1383. if(dif > 10){//4
  1384. r.reason = ALGO_FAILED_CONDITION_13;
  1385. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_13);
  1386. bValid = false;
  1387. }
  1388. }
  1389. }
  1390. if(bValid){
  1391. r.x = res[0].x;
  1392. r.y = res[0].y;
  1393. r.z = res[0].z;
  1394. for (int i=0;i<MAX_READER_TDOA_PATH_NUMS;i++)
  1395. {
  1396. pos->dDiff[i] = 0;
  1397. }
  1398. }
  1399. }
  1400. int r_idx = 0;
  1401. if(res_idx >= 2){
  1402. double d = 99999.9;
  1403. long long dt = tmp_reader[0].rec_time_stamp - tmp_reader[1].rec_time_stamp;
  1404. //计算位置
  1405. double ref_dist = abs(CFunctions::getDistance(dt,CFunctions::TDOA));
  1406. for(int i = 0;i<res_idx;i++){
  1407. 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)));
  1408. double d_diff = abs(d_tmp - ref_dist);
  1409. pos->dDiff[i] = d_diff;
  1410. if(d_diff < d){
  1411. d = d_diff;
  1412. r_idx = i;
  1413. }
  1414. }
  1415. if(pRdm->size() >= 3){
  1416. if(pos->dDiff[r_idx] < NEAR_READER){
  1417. r.x = res[r_idx].x;
  1418. r.y = res[r_idx].y;
  1419. r.z = res[r_idx].z;
  1420. }else{
  1421. r.reason = ALGO_FAILED_CONDITION_14;
  1422. ALGORITHM_FAILED(ALGO_FAILED_CONDITION_14);
  1423. }
  1424. }
  1425. }
  1426. //最终得到的结果解
  1427. pos->posx = r.x;
  1428. pos->posy = r.y;
  1429. pos->posz = 0;
  1430. pos->reason = r.reason;
  1431. if(pos->posx != INVALID_COORDINATE && pos->posy != INVALID_COORDINATE){
  1432. pos->nFirstReader = nFirstReader[r_idx];
  1433. pos->nSecondReader = nSecondReader[r_idx];
  1434. }
  1435. return pos;
  1436. }
  1437. std::shared_ptr<POS> LocateAlgorithm::Pos(std::shared_ptr<POS> pos,std::shared_ptr<TDOAReaderPathMap> trpm)
  1438. {
  1439. if(trpm->find(pos->nFirstReader) == trpm->end()){
  1440. return nullptr;
  1441. }
  1442. if(trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)==trpm->find(pos->nFirstReader)->second->end()){
  1443. return nullptr;
  1444. }
  1445. std::shared_ptr<POS> p = std::make_shared<POS>();
  1446. _coordinate res[2];
  1447. int res_idx = 0;
  1448. for(int i = 0;i < 2;i++){
  1449. res[i].x = INVALID_COORDINATE;
  1450. res[i].y = INVALID_COORDINATE;
  1451. res[i].z = INVALID_COORDINATE;
  1452. }
  1453. //获得地图集
  1454. std::shared_ptr<ReaderPath> pRP = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second;
  1455. //距离差
  1456. double distance = 0.0;
  1457. distance = sqrt(pow(pos->posx - pRP->x[0],2 ) + pow(pos->posy - pRP->y[0],2 )) -
  1458. sqrt(pow(pos->posx - pRP->x[1],2 ) + pow(pos->posy - pRP->y[1],2 ));
  1459. //两分站之间的线段个数
  1460. int seg_num = pRP->nRealCalcPoints - 1;
  1461. if(seg_num == 0){
  1462. return nullptr;
  1463. }
  1464. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1465. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1466. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  1467. int nIdx = 0;
  1468. int nResIdx = 0;
  1469. for(int i = 0;i < seg_num;i++){
  1470. //计算位置坐标,双曲线和线段相交的交点
  1471. std::unique_ptr<SOLUTION> r = GetPos(pRP,distance,i);
  1472. //无解或解无效
  1473. if(r == nullptr){
  1474. continue;
  1475. }
  1476. if(r->nCount == 0){
  1477. continue;
  1478. }
  1479. if(r->nCount == 1){
  1480. xcross[nIdx] = r->x[0];
  1481. ycross[nIdx] = r->y[0];
  1482. nIdx++;
  1483. }
  1484. if(r->nCount == 2){
  1485. for(int j = 0; j < 2;j++){
  1486. xcross[j] = r->x[j];
  1487. ycross[j] = r->y[j];
  1488. nIdx++;
  1489. }
  1490. }
  1491. if(nIdx == 2){
  1492. //解到两焦点之间的距离
  1493. double deltad[2] = {0};
  1494. for(int j = 0; j < 2;j ++){
  1495. double d[2] = {0};
  1496. double dx1 = xcross[j] - pRP->x[0];
  1497. double dy1 = ycross[j] - pRP->y[0];
  1498. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  1499. double dx2 = xcross[j] - pRP->x[1];
  1500. double dy2 = ycross[j] - pRP->y[1];
  1501. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  1502. deltad[j] = d[0] - d[1];
  1503. }
  1504. int idx = 0;
  1505. //应该和之前计算的dist同方向
  1506. if(distance > 0){
  1507. for(int j = 0;j < 2;j++){
  1508. if(deltad[j] > 0){
  1509. idx = j;
  1510. break;
  1511. }
  1512. }
  1513. }else{
  1514. for(int j = 0;j < 2;j++){
  1515. if(deltad[j] < 0){
  1516. idx = j;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. res[nResIdx].x = xcross[idx];
  1522. res[nResIdx].y = ycross[idx];
  1523. res[nResIdx].z = zcross[idx];
  1524. }
  1525. continue;
  1526. }
  1527. if (res[nResIdx].x == INVALID_COORDINATE && res[nResIdx].y == INVALID_COORDINATE)
  1528. {
  1529. return nullptr;
  1530. }
  1531. p->posx = res[nResIdx].x;
  1532. p->posy = res[nResIdx].y;
  1533. p->posz = res[nResIdx].z;
  1534. return p;
  1535. }
  1536. /*
  1537. * TDOA算法求解实现
  1538. * 函数名:GetPos
  1539. * 实现直线和双曲线求交点
  1540. * param
  1541. * pRP ------ 分站路径
  1542. * dist ------ 距离差
  1543. * i ------ 第i条线段
  1544. *
  1545. * return
  1546. * 满足条件的解,最多两个
  1547. *
  1548. */
  1549. //SOLUTION* LocateAlgorithm::GetPos(ReaderPath * pRP,double dist,int i)
  1550. typedef struct
  1551. {
  1552. double x;
  1553. double y;
  1554. double z;
  1555. }PositionStr;
  1556. int calc_card_position(ReceiveData stationFirst, ReceiveData stationSecond, double distanceDiff, PositionStr& result)
  1557. {
  1558. double station_distance = sqrt(pow((stationFirst.x - stationSecond.x), 2) + pow((stationFirst.y - stationSecond.y),2));
  1559. double diff_check = abs(abs(station_distance) - abs(distanceDiff));
  1560. if(abs(distanceDiff) > station_distance)
  1561. {
  1562. //debug_print_syslog(0, "calc_card_position, distanceDiff is longer than stationDistance, read_id_first: %d,read_id_second: %d, diff_check: %f,\
  1563. // first_time: %I64u, second_time: %I64u, station_distance: %f, distanceDiff: %f",
  1564. // stationFirst.reader_id, stationSecond.reader_id,
  1565. // diff_check, stationFirst.rec_time_stamp, stationSecond.rec_time_stamp, station_distance, distanceDiff);
  1566. return -1;
  1567. }
  1568. if(diff_check < 4)
  1569. {
  1570. //debug_print_syslog(0, "calc_card_position, read_id_first: %d,read_id_second: %d, diff_check: %f,\
  1571. // first_time: %I64u, second_time: %I64u, station_distance: %f, distanceDiff: %f",
  1572. // stationFirst.reader_id, stationSecond.reader_id,
  1573. // diff_check, stationFirst.rec_time_stamp, stationSecond.rec_time_stamp, station_distance, distanceDiff);
  1574. return -1;
  1575. }
  1576. double m = station_distance + distanceDiff;
  1577. double n = station_distance - distanceDiff;
  1578. result.x = ((m * stationSecond.x) + (n * stationFirst.x))/(m + n);
  1579. result.y = ((m * stationSecond.y) + (n * stationFirst.y))/(m + n);
  1580. result.z = 0;
  1581. return 0;
  1582. }
  1583. std::unique_ptr<POS> LocateAlgorithm::CalcCardPosition(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm)
  1584. {
  1585. std::unique_ptr<POS> pos(new POS);
  1586. double a = 0;
  1587. double ref_dist = 0.0; //两分站之间无路径的距离差
  1588. const int Totals = 10;
  1589. _coordinate res;
  1590. int res_idx = 0;
  1591. bool bFirst = true;
  1592. int nNoReaderPathIdx = 0;
  1593. int nFirstReader[Totals] = {-1};
  1594. int nSecondReader[Totals] ={-1};
  1595. pos->posx = INVALID_COORDINATE;
  1596. pos->posy = INVALID_COORDINATE;
  1597. pos->posz = INVALID_COORDINATE;
  1598. pos->reason = ALGO_FAILED_CONDITION_1;
  1599. //存储无路径的两分站的id和坐标
  1600. ReceiveData tmp_reader[Totals];
  1601. //存储和第一条分站存在路径的分站信息
  1602. ReceiveData tmp_dist_reader[Totals];
  1603. int nDistReaders = 0;
  1604. for(ReceiveDataMap::iterator it_tmp = pRdm->begin(); it_tmp != pRdm->end(); it_tmp++)
  1605. {
  1606. //debug_print_syslog(0, "Calculate data, read_id_second: %d, rec_time_stamp: %I64u",
  1607. // it_tmp->second->reader_id, it_tmp->second->rec_time_stamp);
  1608. }
  1609. ReceiveDataMap::iterator first = pRdm->begin();
  1610. ReceiveDataMap::iterator second = first;
  1611. //偏移到第二个元素
  1612. std::advance(second,1);
  1613. //获取第一个时间戳
  1614. ReceiveData f1;
  1615. f1.antenna_id = first->second->antenna_id;
  1616. f1.reader_id = first->second->reader_id;
  1617. f1.rec_time_stamp = first->second->rec_time_stamp;
  1618. f1.x = first->second->x;
  1619. f1.y = first->second->y;
  1620. f1.z = first->second->z;
  1621. //从第二个开始遍历
  1622. for(; second != pRdm->end(); ++second)
  1623. {
  1624. //获取第二个时间戳
  1625. ReceiveData f2;
  1626. f2.antenna_id = second->second->antenna_id;
  1627. f2.reader_id = second->second->reader_id;
  1628. f2.rec_time_stamp = second->second->rec_time_stamp;
  1629. f2.x = second->second->x;
  1630. f2.y = second->second->y;
  1631. f2.z = second->second->z;
  1632. //时间戳异常
  1633. if(f1.rec_time_stamp == LLONG_MAX || f2.rec_time_stamp == LLONG_MAX){
  1634. continue;
  1635. }
  1636. if(f1.reader_id == f2.reader_id){
  1637. continue;
  1638. }
  1639. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1640. TDOAReaderPathMap::iterator rdm_it = trpm->find(f1.reader_id);
  1641. if(rdm_it == trpm->end()){
  1642. continue;
  1643. }
  1644. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  1645. if(rpm_it == trpm->find(f1.reader_id)->second->end()){
  1646. continue;
  1647. }else{
  1648. //如果和第一条分站存在地图集
  1649. tmp_dist_reader[nDistReaders].reader_id = f2.reader_id;
  1650. tmp_dist_reader[nDistReaders].x = f2.x;
  1651. tmp_dist_reader[nDistReaders].y = f2.y;
  1652. tmp_dist_reader[nDistReaders].z = f2.z;
  1653. tmp_dist_reader[nDistReaders].rec_time_stamp = f2.rec_time_stamp;
  1654. nDistReaders++;
  1655. }
  1656. //根据距离的正负,后续判断计算位置取舍时使用
  1657. int nSign = 1;
  1658. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  1659. //计算位置
  1660. double distDiff = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  1661. PositionStr stationFirst;
  1662. stationFirst.x = f1.x;
  1663. stationFirst.y = f1.y;
  1664. PositionStr stationSecond;
  1665. stationSecond.x = f2.x;
  1666. stationSecond.y = f2.y;
  1667. PositionStr result;
  1668. int rt_val = 0;
  1669. rt_val = calc_card_position(f1, f2, distDiff, result);
  1670. if(0 == rt_val)
  1671. {
  1672. res.x = result.x;
  1673. res.y = result.y;
  1674. res.z = result.z;
  1675. res.reason = ALGO_LOC_SUCCESSED;
  1676. res_idx++;
  1677. pos->posx = res.x;
  1678. pos->posy = res.y;
  1679. pos->posz = res.z;
  1680. pos->reason = res.reason;
  1681. return pos;
  1682. }
  1683. }
  1684. return pos;
  1685. }
  1686. std::unique_ptr<SOLUTION> LocateAlgorithm::GetPos(std::shared_ptr<ReaderPath> pRP,double dist,int i)
  1687. {
  1688. //解的个数
  1689. int count = 0;
  1690. //解的坐标
  1691. double x[2];
  1692. double y[2];
  1693. //double z[2];
  1694. for (int t=0;t<2;t++)
  1695. {
  1696. x[t] = y[t] = INVALID_COORDINATE;
  1697. }
  1698. //双曲线的两个焦点分别是
  1699. double x1 = pRP->x[0];
  1700. double y1 = pRP->y[0];
  1701. double z1 = pRP->z[0];
  1702. double x2 = pRP->x[1];
  1703. double y2 = pRP->y[1];
  1704. double z2 = pRP->z[1];
  1705. if(pRP->px[i] - pRP->px[i+1] == 0){
  1706. //x相等,双曲线在Y轴上
  1707. x[0] = x[1] = pRP->px[i];
  1708. y[0] = (y1 + y2 + dist)/2.0;
  1709. y[1] = (y1 + y2 - dist)/2.0;
  1710. count = 2;
  1711. }
  1712. else{
  1713. //双曲线和直线相交
  1714. //直线常数求解
  1715. double k = (pRP->py[i+1] - pRP->py[i])/(pRP->px[i+1] - pRP->px[i]);
  1716. double kb = pRP->py[i] - k*pRP->px[i];
  1717. //求解的常数中间量
  1718. double d1 = 2*(x2 - x1);//m
  1719. double d2 = 2*(y2 - y1);//n
  1720. double d3 = pow(y1,2) + pow(x1,2)- pow(dist,2) - pow(y2,2) - pow(x2,2);//p
  1721. //方程ax^2 + bx + c = 0
  1722. double a = pow(d1 + d2*k,2) - 4*pow(dist,2)*(pow(k,2) + 1);
  1723. double b = 2*((d3 + d2*kb)*(d1 + d2*k) - 4*pow(dist,2)*(k*(kb - y2) - x2));
  1724. double c = pow(d3 + d2*kb,2) - 4*pow(dist,2)*(pow(x2,2) + pow(kb-y2,2));
  1725. double delta = pow(b,2) - 4*a*c;
  1726. if(delta > 0){
  1727. count = 2;
  1728. }
  1729. else if(delta == 0){
  1730. count = 1;
  1731. }
  1732. else{
  1733. count = 0;
  1734. }
  1735. if(count == 0){
  1736. return nullptr;
  1737. }
  1738. //计算解
  1739. x[0] = -(b + sqrt(delta))/(2*a);
  1740. x[1] = (-b + sqrt(delta) )/(2*a);
  1741. y[0] = k*x[0] + kb;
  1742. y[1] = k*x[1] + kb;
  1743. }
  1744. int nIdx = 0;
  1745. int nValidCount = count;
  1746. //引入误差
  1747. double deviation = 0.0;
  1748. //判断两解是否在线段范围内
  1749. if(count > 0){
  1750. for(int t = 0; t < count ;t++){
  1751. //两分站之间第i条线段的两端点
  1752. if(x[t] < min(pRP->px[i],pRP->px[i+1]) - deviation||
  1753. x[t] > max(pRP->px[i],pRP->px[i+1]) + deviation||
  1754. y[t] < min(pRP->py[i],pRP->py[i+1]) - deviation||
  1755. y[t] > max(pRP->py[i],pRP->py[i+1]) + deviation
  1756. )
  1757. {
  1758. x[t] = INVALID_COORDINATE;
  1759. y[t] = INVALID_COORDINATE;
  1760. nValidCount--;
  1761. nIdx = t;
  1762. }
  1763. }
  1764. }
  1765. std::unique_ptr<SOLUTION> s(new SOLUTION);
  1766. s->nCount = nValidCount;
  1767. nIdx = -1;
  1768. switch(nValidCount){
  1769. case 1:
  1770. for(int i = 0;i<2;i++){
  1771. if(x[i] != INVALID_COORDINATE && y[i] != INVALID_COORDINATE){
  1772. nIdx = i;
  1773. }
  1774. }
  1775. s->x[0] = x[nIdx];
  1776. s->y[0] = y[nIdx];
  1777. break;
  1778. case 2:
  1779. s->x[0] = x[0];
  1780. s->x[1] = x[1];
  1781. s->y[0] = y[0];
  1782. s->y[1] = y[1];
  1783. break;
  1784. }
  1785. return s;
  1786. }
  1787. /*
  1788. * 判断坐标在地图集上
  1789. *
  1790. * param
  1791. * pos ------ 定位的结果
  1792. * trpm ------ 地图集
  1793. *
  1794. * return
  1795. * false,不在地图集上,true在地图集上
  1796. *
  1797. */
  1798. bool LocateAlgorithm::IsOnMap(std::shared_ptr<POS>& pos,std::shared_ptr<TDOAReaderPathMap> trpm)
  1799. {
  1800. //如果地图集中都找不到此分站开始的地图集,就返回false
  1801. //if(trpm->find(pos->nFirstReader) == trpm->end()){
  1802. // return false;
  1803. //}
  1804. ////如果地图集找不到此两分站的地图集,就返回false
  1805. //if(trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader) == trpm->find(pos->nFirstReader)->second->end())
  1806. //{
  1807. // return false;
  1808. //}
  1809. ////保存两分站的坐标
  1810. //double x[2] = {0};
  1811. //double y[2] = {0};
  1812. //for(int i=0;i<2;i++){
  1813. // x[i] = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second->x[i];
  1814. // y[i] = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second->y[i];
  1815. //}
  1816. //if(x[0] == x[1]){
  1817. // //误差1cm
  1818. // if(abs(pos->posx - x[0]) > 1E-2){
  1819. // return false;
  1820. // }
  1821. //}else{
  1822. // double k = (y[1] - y[0])/(x[1] - x[0]);
  1823. // double b = y[1] - k*x[1];
  1824. // double calc_y = k*pos->posx + b;
  1825. // if(abs(calc_y - pos->posy) > 1E-2){
  1826. // return false;
  1827. // }
  1828. //}
  1829. bool bExist = false;
  1830. for (TDOAReaderPathMap::iterator first = trpm->begin();first!=trpm->end();++first)
  1831. {
  1832. for (ReaderPathMap::iterator second = first->second->begin();second != first->second->end();++second)
  1833. {
  1834. _point p,start_p,end_p;
  1835. p.x = pos->posx;
  1836. p.y = pos->posy;
  1837. start_p.x = second->second->px[0];
  1838. start_p.y = second->second->py[0];
  1839. end_p.x = second->second->px[1];
  1840. end_p.y = second->second->py[1];
  1841. bExist = LocateAlgorithm::IsInLine(p,start_p,end_p);
  1842. if (bExist)
  1843. {
  1844. pos->nFirstReader = first->first;
  1845. pos->nSecondReader = second->first;
  1846. return bExist;
  1847. }
  1848. ////保存两分站的坐标
  1849. //double x[2] = {0};
  1850. //double y[2] = {0};
  1851. //for(int i=0;i<2;i++){
  1852. // x[i] = second->second->x[i];
  1853. // y[i] = second->second->y[i];
  1854. //}
  1855. //if(abs(x[0] - x[1]) < 1E-4 && abs(pos->posx - x[0]) < 1E-4){
  1856. // //误差1cm
  1857. // if ((pos->posy > y[0] && pos->posy < y[1])||(pos->posy > y[1] && pos->posy < y[0]))
  1858. // {
  1859. // bExist = true;
  1860. // return bExist;
  1861. // }
  1862. //}else{
  1863. // double k = (y[1] - y[0])/(x[1] - x[0]);
  1864. // double b = y[1] - k*x[1];
  1865. // double calc_y = k*pos->posx + b;
  1866. // if(abs(calc_y - pos->posy) < 1){
  1867. // bExist = true;
  1868. // return bExist;
  1869. // }
  1870. //}
  1871. }
  1872. }
  1873. return bExist;
  1874. }
  1875. bool LocateAlgorithm::IsInTriangle(std::vector<_point> vtp,_point p)
  1876. {
  1877. double sabc = 0,sadb = 0,sbdc = 0,sadc = 0;
  1878. sabc = GetTriangleArea(vtp[0],vtp[1],vtp[2]);
  1879. sadb = GetTriangleArea(vtp[0],p,vtp[1]);
  1880. sbdc = GetTriangleArea(vtp[1],p,vtp[2]);
  1881. sadc = GetTriangleArea(vtp[0],p,vtp[2]);
  1882. double sum = 0.0;
  1883. sum = sadb + sbdc + sadc;
  1884. if ((sabc - sum) > -1E-5 && (sabc - sum) < 1E-5)
  1885. {
  1886. return true;
  1887. }
  1888. else
  1889. {
  1890. return false;
  1891. }
  1892. }
  1893. double LocateAlgorithm::GetTriangleArea(_point p0,_point p1,_point p2)
  1894. {
  1895. _point ab,bc;
  1896. ab.x = p1.x - p0.x;
  1897. ab.y = p1.y - p0.y;
  1898. bc.x = p2.x - p1.x;
  1899. bc.y = p2.y - p1.y;
  1900. return abs(ab.x*bc.y - ab.y*bc.x)/2.0;
  1901. }
  1902. /*
  1903. * 点P是否在以start_p和end_p的线段上
  1904. *
  1905. * param
  1906. * p 识别点
  1907. * start_p 线段端点
  1908. * end_p 线段终点
  1909. *
  1910. * return
  1911. * 在线段上返回true,否则返回false
  1912. *
  1913. */
  1914. bool LocateAlgorithm::IsInLine(_point p,_point start_p,_point end_p)
  1915. {
  1916. double d1 = 0, d2 = 0, d3 = 0;
  1917. d1 = sqrt(pow(p.x - start_p.x,2) + pow(p.y - start_p.y,2));
  1918. d2 = sqrt(pow(p.x - end_p.x,2) + pow(p.y - end_p.y,2));
  1919. d3 = sqrt(pow(start_p.x - end_p.x,2) + pow(start_p.y - end_p.y,2));
  1920. double d4 = 0;
  1921. d4 = abs(d3 - (d1 + d2));
  1922. if (d4 < OFFSET_THRE_IN_LINE)
  1923. {
  1924. //增加点是否在start_p和end_p为顶点的矩形内
  1925. if (abs(start_p.x - end_p.x) < ZERO_PRECISION)
  1926. {
  1927. if (p.y > min(start_p.y,end_p.y) && p.y < max(start_p.y,end_p.y))
  1928. {
  1929. return true;
  1930. }else{
  1931. return false;
  1932. }
  1933. }else if(abs(start_p.y - end_p.y) < ZERO_PRECISION){
  1934. if(p.x > min(start_p.x,end_p.x) && p.x < max(start_p.x,end_p.x)) {
  1935. return true;
  1936. }else{
  1937. return false;
  1938. }
  1939. }else{
  1940. if(p.x > min(start_p.x,end_p.x) && p.x < max(start_p.x,end_p.x)
  1941. && p.y > min(start_p.y,end_p.y) && p.y < max(start_p.y,end_p.y))
  1942. {
  1943. return true;
  1944. }else{
  1945. return false;
  1946. }
  1947. }
  1948. }
  1949. return false;
  1950. }
  1951. bool LocateAlgorithm::PointIsInRect(point p,point tp,point lp,point bp,point rp)
  1952. {
  1953. //任意四边形有4个顶点
  1954. int nCount = 4;
  1955. point RectPoints[4] = { tp, lp, bp, rp };
  1956. int nCross = 0;
  1957. for (int i = 0; i < nCount; i++)
  1958. {
  1959. //依次取相邻的两个点
  1960. point pStart = RectPoints[i];
  1961. point pEnd = RectPoints[(i + 1) % nCount];
  1962. //相邻的两个点是平行于x轴的,肯定不相交,忽略
  1963. if ( pStart.y == pEnd.y )
  1964. continue;
  1965. //交点在pStart,pEnd的延长线上,pCur肯定不会与pStart.pEnd相交,忽略
  1966. if ( p.y < min(pStart.y, pEnd.y) || p.y > max( pStart.y, pEnd.y ) )
  1967. continue;
  1968. //求当前点和x轴的平行线与pStart,pEnd直线的交点的x坐标
  1969. double x = (double)(p.y - pStart.y) * (double)(pEnd.x - pStart.x) / (double)(pEnd.y - pStart.y) + pStart.x;
  1970. //若x坐标大于当前点的坐标,则有交点
  1971. if ( x > p.x )
  1972. nCross++;
  1973. }
  1974. // 单边交点为偶数,点在多边形之外
  1975. return (nCross % 2 == 1);
  1976. }
  1977. int LocateAlgorithm::CalcTdoaPosition(std::shared_ptr<ReceiveDataMap> pRdm,std::shared_ptr<TDOAReaderPathMap> trpm,std::vector<std::shared_ptr<POS>>& udm_pos)
  1978. {
  1979. //这部分应该放到组装数据后检测里
  1980. //一:选择基准分站,条件如下:
  1981. //1.基准分站选择:当前分站和后一个分站有地图集,则选为基准分站
  1982. //2.当前分站和后续所有分站无地图集,则当前分站不是基准分站
  1983. //3.重复1,2过程
  1984. for (ReceiveDataMap::iterator first = pRdm->begin();first != pRdm->end();)
  1985. {
  1986. ReceiveDataMap::iterator second = first;
  1987. //second偏移到第二个元素
  1988. std::advance(second,1);
  1989. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  1990. TDOAReaderPathMap::iterator rdm_it = trpm->find(first->second->reader_id);
  1991. if(rdm_it == trpm->end()){
  1992. //表示地图集中不存在第一个分站的路径集,则删除此分站,并继续循环
  1993. pRdm->erase(first);
  1994. first = pRdm->begin();
  1995. continue;
  1996. }
  1997. //统计first分站和后续分站的地图集数量
  1998. int nCount = 0;
  1999. for (;second!= pRdm->end();++second)
  2000. {
  2001. //确认第一个和第二个分站之间是否有地图集
  2002. ReaderPathMap::iterator rpm_it = trpm->find(first->second->reader_id)->second->find(second->second->reader_id);
  2003. if(rpm_it == trpm->find(first->second->reader_id)->second->end()){
  2004. continue;
  2005. }else{
  2006. nCount++;
  2007. break;
  2008. }
  2009. }
  2010. //如果first分站和后续分站都无地图集,则删除first分站,并重置迭代器
  2011. if (nCount == 0)
  2012. {
  2013. pRdm->erase(first);
  2014. first = pRdm->begin();
  2015. }else{
  2016. first = second;
  2017. break;
  2018. }
  2019. }
  2020. //定位数据条数不够2条
  2021. if (pRdm->size() < 2)
  2022. {
  2023. return ALGO_CALC_SOLUTION;
  2024. }
  2025. //先做第一遍筛选,
  2026. //根据前三个分站计算坐标,如果有解,直接返回;如果无解,做第二遍筛选
  2027. bool bNoSolution = true;
  2028. std::unique_ptr<POS> first_pos = LocateAlgorithm::Pos(pRdm,trpm);
  2029. if (first_pos->posx != INVALID_COORDINATE && first_pos->posy != INVALID_COORDINATE)
  2030. {
  2031. std::shared_ptr<POS> sp = std::make_shared<POS>();
  2032. *sp = *first_pos;
  2033. udm_pos.push_back(sp);
  2034. bNoSolution = false;
  2035. return 0;
  2036. }
  2037. //如果无解且数据条数小于等于2,则返回选择失败
  2038. //主要是筛选两条数据,但无法定位的情况
  2039. if (bNoSolution && pRdm->size() <= 2)
  2040. {
  2041. return ALGO_CALC_NO_SOLUTION_WITH_TWO_DATA;
  2042. }
  2043. //开始执行第二遍筛选
  2044. ReceiveDataMap::iterator first = pRdm->begin();
  2045. ReceiveDataMap::iterator second = first;
  2046. if (bNoSolution)
  2047. {
  2048. //因为first和后两个分站已经通过上步判断无解了
  2049. //如果按3偏移,存在一个bug,
  2050. //即在在如下情况下103,104,301,102,105,106情况下
  2051. //在第一步时,当103和104,103和102的结果被判定不合格而无解
  2052. //此处偏移3,即会导致301被跳过
  2053. //所以改为重新对后续求所有解
  2054. //偏移到第二个元素
  2055. std::advance(second,1);
  2056. }
  2057. //检查偏移后的元素是否存在
  2058. if (second == pRdm->end())
  2059. {
  2060. //偏移后找不到元素,返回错误
  2061. return ALGO_CALC_ONE_DATA;
  2062. }
  2063. //获取第一个元素中的数据
  2064. ReceiveData f1;
  2065. f1.antenna_id = first->second->antenna_id;
  2066. f1.reader_id = first->second->reader_id;
  2067. f1.rec_time_stamp = first->second->rec_time_stamp;
  2068. f1.x = first->second->x;
  2069. f1.y = first->second->y;
  2070. f1.z = first->second->z;
  2071. for(;second != pRdm->end();++second){
  2072. //获取第二个数据中的元素
  2073. ReceiveData f2;
  2074. f2.antenna_id = second->second->antenna_id;
  2075. f2.reader_id = second->second->reader_id;
  2076. f2.rec_time_stamp = second->second->rec_time_stamp;
  2077. f2.x = second->second->x;
  2078. f2.y = second->second->y;
  2079. f2.z = second->second->z;
  2080. bool bExistPath = false;
  2081. //如果第一级都不存在,则继续找下一个元素
  2082. TDOAReaderPathMap::iterator rdm_it = trpm->find(f1.reader_id);
  2083. if(rdm_it == trpm->end()){
  2084. continue;
  2085. }
  2086. //如果两级都能找到才运行继续后续操作,否则,表明没有此路径地图集
  2087. ReaderPathMap::iterator rpm_it = trpm->find(f1.reader_id)->second->find(f2.reader_id);
  2088. if(rpm_it != trpm->find(f1.reader_id)->second->end()){
  2089. bExistPath = true;
  2090. }
  2091. //根据距离的正负,后续判断计算位置取舍时使用
  2092. int nSign = 1;
  2093. long long diffTime = f1.rec_time_stamp - f2.rec_time_stamp;
  2094. //计算位置
  2095. double dist = CFunctions::getDistance(diffTime,CFunctions::TDOA);
  2096. double readers_dist = sqrt(pow(f1.x - f2.x,2) + pow(f1.y - f2.y,2));
  2097. if(fabs(dist) - readers_dist > 0){
  2098. continue;
  2099. }
  2100. //保存解信息
  2101. std::shared_ptr<POS> pos = std::make_shared<POS>();
  2102. if (bExistPath)
  2103. {
  2104. //如果存在地图集
  2105. std::shared_ptr<ReaderPath> pRP = trpm->find(f1.reader_id)->second->find(f2.reader_id)->second;
  2106. //两分站之间的线段个数
  2107. int seg_num = pRP->nRealCalcPoints - 1;
  2108. if(seg_num == 0 || seg_num > 100){
  2109. continue;
  2110. }
  2111. //因为双曲线与分站之间第i条线段或者第j条线段分别有两焦点
  2112. //或者分站之间就一条直线,有两焦点
  2113. double xcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  2114. double ycross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  2115. double zcross[2] = {INVALID_COORDINATE,INVALID_COORDINATE};
  2116. int nIdx = 0;
  2117. for(int i = 0;i < seg_num;i++){
  2118. //计算位置坐标,双曲线和线段相交的交点
  2119. std::unique_ptr<SOLUTION> r = LocateAlgorithm::GetPos(pRP,dist,i);
  2120. //无解或解无效
  2121. if(r == nullptr || r->nCount == 0){
  2122. continue;
  2123. }
  2124. int nPosCounts = 0;
  2125. for (int i=0;i<r->nCount;i++)
  2126. {
  2127. xcross[nIdx] = r->x[i];
  2128. ycross[nIdx] = r->y[i];
  2129. nIdx++;
  2130. }
  2131. switch (nIdx)
  2132. {
  2133. case 1:
  2134. pos->posx = xcross[0];
  2135. pos->posy = ycross[0];
  2136. pos->posz = zcross[0];
  2137. udm_pos.push_back(pos);
  2138. break;
  2139. case 2:
  2140. //筛选解:从两解中选出一个
  2141. //解到两焦点之间的距离
  2142. double deltad[2] = {0};
  2143. for(int j = 0; j < 2;j ++){
  2144. double d[2] = {0};
  2145. double dx1 = xcross[j] - pRP->x[0];
  2146. double dy1 = ycross[j] - pRP->y[0];
  2147. d[0] = sqrt(pow(dx1,2) + pow(dy1,2));
  2148. double dx2 = xcross[j] - pRP->x[1];
  2149. double dy2 = ycross[j] - pRP->y[1];
  2150. d[1] = sqrt(pow(dx2,2) + pow(dy2,2));
  2151. deltad[j] = d[0] - d[1];
  2152. }
  2153. int idx = 0;
  2154. //应该和之前计算的dist同方向
  2155. if(dist > 0){
  2156. for(int j = 0;j < 2;j++){
  2157. if(deltad[j] > 0){
  2158. idx = j;
  2159. break;
  2160. }
  2161. }
  2162. }else{
  2163. for(int j = 0;j < 2;j++){
  2164. if(deltad[j] < 0){
  2165. idx = j;
  2166. break;
  2167. }
  2168. }
  2169. }
  2170. pos->nFirstReader = f1.reader_id;
  2171. pos->nSecondReader = f2.reader_id;
  2172. pos->posx = xcross[idx];
  2173. pos->posy = ycross[idx];
  2174. pos->posz = zcross[idx];
  2175. udm_pos.push_back(pos);
  2176. break;
  2177. }
  2178. }
  2179. }
  2180. else
  2181. {
  2182. //如果不存在地图集,需要检测计算结果是否在地图集上,
  2183. //根据距离的正负,后续判断计算位置取舍时使用
  2184. //无地图集的解求坐标方法如下:
  2185. double offset_d = (dist + readers_dist)/2;
  2186. double calc_x =0.0, calc_y = 0.0;
  2187. if (f1.x - f2.x < 1E-5)
  2188. {
  2189. //在y轴上
  2190. calc_x = f1.x;
  2191. if (f1.y > f2.y)
  2192. {
  2193. calc_y = f1.y - offset_d;
  2194. }
  2195. else
  2196. {
  2197. calc_y = f1.y + offset_d;
  2198. }
  2199. }else if (f1.y - f2.y < 1E-5)
  2200. {
  2201. //在x轴上
  2202. if (f1.x < f2.x)
  2203. {
  2204. calc_x = f1.x + offset_d;
  2205. }
  2206. else
  2207. {
  2208. calc_x = f1.x - offset_d;
  2209. }
  2210. calc_y = f1.y;
  2211. }
  2212. else
  2213. {
  2214. //在有斜率的地方
  2215. double arg = atan((f2.y - f1.y)/(f2.x - f1.x));
  2216. if (f1.x < f2.x && f1.y < f2.y)
  2217. {
  2218. calc_x = f1.x + cos(arg)*offset_d;
  2219. calc_y = f1.y + sin(arg)*offset_d;
  2220. }
  2221. else
  2222. {
  2223. calc_x = f1.x - cos(arg)*offset_d;
  2224. calc_y = f1.y - sin(arg)*offset_d;
  2225. }
  2226. }
  2227. pos->posx = calc_x;
  2228. pos->posy = calc_y;
  2229. pos->nFirstReader = f1.reader_id;
  2230. pos->nSecondReader = f2.reader_id;
  2231. //如果不在,则此解无效,如果存在,则此解有效
  2232. if (IsOnMap(pos,trpm))
  2233. {
  2234. udm_pos.push_back(pos);
  2235. }
  2236. }
  2237. }
  2238. if (udm_pos.size() == 0)
  2239. {
  2240. return ALGO_CALC_SOLUTION;
  2241. }
  2242. return 0;
  2243. }
  2244. int LocateAlgorithm::round(double value)
  2245. {
  2246. return (value > 0.0)?floor(value + 0.5):ceil(value - 0.5);
  2247. }
  2248. bool LocateAlgorithm::CheckPosInValid(POS* pos,ReceiveDataMap* pRdm,double dScale)
  2249. {
  2250. if(dScale <= 0.0){
  2251. return false;
  2252. }
  2253. if(pos->posx == INVALID_COORDINATE || pos->posy == INVALID_COORDINATE){
  2254. return false;
  2255. }
  2256. //判断解的无效性
  2257. int nSize = 0;
  2258. bool bRet = false;
  2259. nSize = pRdm->size();
  2260. double dist[2] = {0.0};
  2261. if(nSize == 2){
  2262. int i = 0;
  2263. for(ReceiveDataMap::iterator it = pRdm->begin();it!=pRdm->end();++it,i++){
  2264. dist[i] = sqrt(pow(pos->posx - it->second->x,2)+pow(pos->posy - it->second->y,2));
  2265. if(fabs(dist[i]) < NEAR_READER){
  2266. //误差在范围内,判断此分站是否属于特殊分站
  2267. if(it->second->special){
  2268. bRet = false;
  2269. }else{
  2270. bRet = true;
  2271. }
  2272. }
  2273. }
  2274. }
  2275. return bRet;
  2276. }
  2277. std::shared_ptr<POS> LocateAlgorithm::MappingToPath( std::shared_ptr<POS> pos, std::shared_ptr<TDOAReaderPathMap> trpm )
  2278. {
  2279. if(trpm->find(pos->nFirstReader) == trpm->end()){
  2280. return nullptr;
  2281. }
  2282. if(trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)==trpm->find(pos->nFirstReader)->second->end()){
  2283. return nullptr;
  2284. }
  2285. std::shared_ptr<POS> p = std::make_shared<POS>();
  2286. //获得地图集
  2287. std::shared_ptr<ReaderPath> pRP = trpm->find(pos->nFirstReader)->second->find(pos->nSecondReader)->second;
  2288. // 计算两点间斜率
  2289. // 垂直
  2290. if(abs(pRP->x[0] - pRP->x[1]) < 1E-6)
  2291. {
  2292. p->posx = pRP->x[0];
  2293. p->posy = pos->posy;
  2294. }
  2295. // 水平
  2296. else if(abs(pRP->y[0] - pRP->y[1]) < 1E-6)
  2297. {
  2298. p->posx = pos->posx;
  2299. p->posy = pRP->y[0];
  2300. }
  2301. // 有斜率
  2302. else
  2303. {
  2304. // 原理 y = kx + b, y' = (-1/k) x' + b'
  2305. double k1= (pRP->y[1] - pRP->y[0]) / (pRP->x[1] - pRP->x[0]);
  2306. double k2 = -1.0 / k1;
  2307. double b1 = pRP->y[0] - k1 * pRP->x[0];
  2308. double b2 = pos->posy - k2 * pos->posx;
  2309. p->posx = (b1 - b2)/(k2 - k1);
  2310. p->posy = k2 * p->posx + b2;
  2311. }
  2312. return p;
  2313. }
  2314. void HeapSort(_coordinate** pCoordinateArray,int nLen)
  2315. {
  2316. BuildMaxHeap(pCoordinateArray,nLen);
  2317. for(int i = nLen - 1;i > 0;i--){
  2318. SwapElement(pCoordinateArray[0],pCoordinateArray[i]);
  2319. AdjustMaxHeap(pCoordinateArray,0,i-1);
  2320. }
  2321. }
  2322. void BuildMaxHeap(_coordinate** pCoordinateArray,int nLen)
  2323. {
  2324. for(int i = nLen/2 - 1;i > 0;i--){
  2325. AdjustMaxHeap(pCoordinateArray,i,nLen-1);
  2326. }
  2327. }
  2328. void AdjustMaxHeap(_coordinate** pCoordinateArray,int n,int nHeapSize)
  2329. {
  2330. int l = (n+1)*2-1;
  2331. int r = (n+1)*2;
  2332. int max;
  2333. if(l <= nHeapSize && pCoordinateArray[l]->tt > pCoordinateArray[n]->tt){
  2334. max = l;
  2335. }else{
  2336. max = n;
  2337. }
  2338. if(r <= nHeapSize && pCoordinateArray[r]->tt > pCoordinateArray[max]->tt){
  2339. max = r;
  2340. }
  2341. if(max != n){
  2342. SwapElement(pCoordinateArray[n],pCoordinateArray[max]);
  2343. AdjustMaxHeap(pCoordinateArray,max,nHeapSize);
  2344. }
  2345. }
  2346. void SwapElement(_coordinate* a,_coordinate*b)
  2347. {
  2348. _coordinate tmp;
  2349. tmp = *a;
  2350. *a = *b;
  2351. *b = tmp;
  2352. }
  2353. void SelectSort(_coordinate** pCoordinateArray,int nLen)
  2354. {
  2355. for (int i=0; i<nLen; i++)
  2356. {
  2357. int k = i;
  2358. unsigned long long key = pCoordinateArray[i]->tt;
  2359. for (int j=i+1; j<nLen; j++)
  2360. {
  2361. if (pCoordinateArray[j]->tt < key)
  2362. {
  2363. k = j;
  2364. key = pCoordinateArray[j]->tt;
  2365. }
  2366. }
  2367. if (k!=i)
  2368. SwapElement(pCoordinateArray[i], pCoordinateArray[k]);
  2369. }
  2370. }