MapInfo.cpp 19.9 KB
Newer Older
oscar's avatar
oscar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
#include "MapInfo.hpp"

namespace jf {

    MapInfo::MapInfo(){}

    MapInfo::~MapInfo() {}

/**
 * 获取车辆实际行驶的link和lane
 *
 * @param pointIn  车辆GPS坐标点
 * @param Agdir  车辆航向角
 * @param hdLinkId
 * @param hdLaneId
 * @return 主要排除问题:(1)坐标点位于多个link的交叉区域的时候判定车辆真正行驶的link
 *                    (2)车辆拐弯的时候暂时偏离实际想走的车道
 */
    bool MapInfo::locateLinkandLane(Point pointIn, float Agdir, HdLinkId &hdLinkId, HdLaneId &hdLaneId) {

        static HdLinkId s_liLastLinkId = {};
        static HdLaneId s_liLastLaneId = {}; //记录车辆上一个周期经过的link和lane

        std::vector<HdLinkId> vctliLocateLinkIds = {};
        std::vector<HdLaneId> vctliLocateLaneIds = {};
        if (false == m_spHdLib->GetLocateLanes(pointIn, vctliLocateLinkIds, vctliLocateLaneIds)) { //定位失败,那么使用上次的结果

            if (s_liLastLaneId.lValue == 0) {  // 上次结果不可用
                return false;
            } else {   // 上次结果可以用
                vctliLocateLinkIds.push_back(s_liLastLinkId);
                vctliLocateLaneIds.push_back(s_liLastLaneId);
            }
        }

        if (vctliLocateLaneIds.size() >= 2) {
            // 首先,选择航向角匹配的车道
            std::vector<int> vctnAngleMatchedLanes = {};
            for (int i = 0; i < vctliLocateLaneIds.size(); ++i) {
                double dAzimuth = 0.0;
                m_spHdLib->GetLaneAzimuth(vctliLocateLinkIds.at(i), vctliLocateLaneIds.at(i), pointIn, dAzimuth);
                double dAngleSub = std::min(std::abs(dAzimuth - Agdir), std::abs(360 - (dAzimuth - Agdir)));
                if (dAngleSub < 15) {
                    vctnAngleMatchedLanes.push_back(i);
                }
            }
            // 多个车道符合条件或者没有符合条件的车道
            if (vctnAngleMatchedLanes.size() >= 2 || vctnAngleMatchedLanes.size() == 0) {
                // 应该尝试使用INS的高程作为判断,但INS的高程并不是很精确,所以不使用
                hdLinkId = s_liLastLinkId;
                hdLaneId = s_liLastLaneId;
            } else {
                hdLinkId = vctliLocateLinkIds.at(vctnAngleMatchedLanes.at(0));
                hdLaneId = vctliLocateLaneIds.at(vctnAngleMatchedLanes.at(0));
            }
        } else {
//            // 即使只有一条车道符合条件,也可能存在坐标点与实际车道不符合的情况
//            double dAzimuth = 0.0;
//            m_spHdLib->GetLaneAzimuth(vctliLocateLinkIds.at(0), vctliLocateLaneIds.at(0), pointIn, dAzimuth);
//            double dAngleSub = std::min(std::abs(dAzimuth - Agdir), std::abs(360 - (dAzimuth - Agdir)));
//            if (dAngleSub < 15) {
                hdLinkId = vctliLocateLinkIds.at(0);
                hdLaneId = vctliLocateLaneIds.at(0);
//            }
//                // 角度不匹配,使用上次结果
//            else {
//                hdLinkId = s_liLastLinkId;
//                hdLaneId = s_liLastLaneId;
//            }
        }
        s_liLastLinkId = hdLinkId;
        s_liLastLaneId = hdLaneId;
        return true;
    }

    /**
     * 输出自车当前车道的道路信息
     *
     * 信息包括:
     *  (1)当前车道的限速信息
     *  (2)左右相邻车道的车到边界线ID
     *  (3)是否能变道到左右相邻车道
     * @return
     */

    bool MapInfo::getCurrentLocationInfo(Point pointIn, float Agdir, HdLinkId &hdLinkId, HdLaneId &hdLaneId,
                                              int& speedLimitInfo,
                                              int64_t& LeftLaneEdgeId1, int64_t& LeftLaneEdgeId2, bool& LeftLane_Crossable,
                                              int64_t& RightLaneEdgeId1, int64_t& RightLaneEdgeId2, bool& RightLane_Crossable){

        const HdLink *linkPtr = m_spHdLib->GetLinkData(hdLinkId);
        int CurrentLaneNum;
        for(HdLane hdLane :linkPtr->vcthlLane){
            if(hdLane.liId.lValue == hdLaneId.lValue){
                speedLimitInfo = hdLane.nMaxSpeed; //限速信息输出
                CurrentLaneNum = hdLane.nSeqNum;
                break;
            }
        }

        for (const HdLane hdLane : linkPtr->vcthlLane) {
            if(CurrentLaneNum - 1 > 0){
                if (hdLane.nSeqNum == CurrentLaneNum-1) {
                    LeftLaneEdgeId1 = hdLane.lLeftEdgeId;
                    LeftLaneEdgeId2 = hdLane.lRightEdgeId;
                    LeftLane_Crossable =  m_spHdLib->IsReachable(linkPtr, hdLaneId,hdLane.liId);
                }
            }else if(CurrentLaneNum + 1 <= linkPtr->vcthlLane.size()){
                if(hdLane.nSeqNum == CurrentLaneNum + 1){
                    RightLaneEdgeId1 =  hdLane.lLeftEdgeId;
                    RightLaneEdgeId2 =  hdLane.lRightEdgeId;
                    RightLane_Crossable = m_spHdLib->IsReachable(linkPtr, hdLaneId,hdLane.liId);
                }
            }

        }

        return true;
    }


/**
 *  计算需要输出车道边界线的Link
 *
 * std::deque<HdLinkId> hdLinkIdQueue;                          //最终需要维护的数据结构1
 * std::map<HdLinkId, std::map<int, Point>> hdLinkIdQueueTable; //最终需要维护的数据结构2
 * @return
 */
    bool MapInfo::CreateMapInfo_First( const HdLinkId &hdLinkId, const HdLaneId &hdLaneId, const Point pointIn,
                                            std::deque<HdLinkId>& hdLinkIdQueue, std::map<HdLinkId, std::map<int, Point>>& hdLinkIdQueueTable) {

        double BehindDistanceTarget = 10.0; //车辆后方10m
        double FontDistanceTarget = 200; //车辆前方100m
        const HdLink *hdLinkPtr = m_spHdLib->GetLinkData(hdLinkId);//自车所在的Link
        static std::queue<HdLinkId> hdLinkIdLastQueue; //自辆经过的上一个Link和本周期的link队列
        Point footPointOfCar; //自车垂足点
        double distanceTmp;
        int indexOfCarPoint; //自车所在的Index
        HdLibMath::GetPerpendicularFoot(pointIn, hdLinkPtr->vctvdGeoLink, footPointOfCar, distanceTmp, indexOfCarPoint);

        /**
         * 生成车前的信息
         */
        auto createFont = [this](Point pointInGeoLink, const std::vector<std::vector<double>> &vctvdGeoLink,
                                 int indexOfPoint,
                                 double &distanceTarget, std::map<int, Point> &resultGeoMap) {
            double dRealLength = 0; //本周期累加距离总和
            static double dRealLengthLast = 0; //上一周期累加距离总和
            for (int i = indexOfPoint; i < vctvdGeoLink.size(); i++) {
                if (i == indexOfPoint) {
                    dRealLength += PointsDis(Point(vctvdGeoLink[i]), pointInGeoLink);
                } else {
                    dRealLength += PointsDis(Point(vctvdGeoLink[i]), Point(vctvdGeoLink[i - 1]));
                }
                if (dRealLength >= distanceTarget) { //如果累加距离超过了目标距离
                    std::vector<Point> vctPointTmp = jf::InterP(vctvdGeoLink[i - 1], vctvdGeoLink[i],distanceTarget - dRealLengthLast);
                    resultGeoMap[i] = vctPointTmp[1];
                    return true;
                }
                dRealLengthLast = dRealLength;
            }
            distanceTarget = distanceTarget - dRealLength;
            return false;
        };

        /**
         * 生成车后的信息
         */
        auto createBehind = [this](Point pointInGeoLink, const std::vector<std::vector<double>> &vctvdGeoLink,
                                   int indexOfPoint,
                                   double &distanceTarget, std::map<int, Point> &resultGeoMap) {
            double dRealLength = 0; //本周期累加距离总和
            static double dRealLengthLast = 0; //上一周期累加距离总和
            for (int i = indexOfPoint - 1; i >= 0; i--) {
                if (i == indexOfPoint - 1) {
                    dRealLength += PointsDis(Point(vctvdGeoLink[i]), pointInGeoLink);
                } else {
                    dRealLength += PointsDis(Point(vctvdGeoLink[i]), Point(vctvdGeoLink[i + 1]));
                }
                if (dRealLength >= distanceTarget) { //如果累加距离超过了目标距离
                    std::vector<Point> vctPointTmp = jf::InterP(vctvdGeoLink[i + 1], vctvdGeoLink[i],
                                                                distanceTarget - dRealLengthLast);
                    resultGeoMap[i] = vctPointTmp[1];
                    distanceTarget = 0;
                    return true;
                }
                dRealLengthLast = dRealLength;
            }
            distanceTarget = distanceTarget - dRealLength;
            return false;
        };

        /**
        * 递归寻找前方具有联通关系的Link
        */
        auto findFontLinksFunc = [this, &createFont, &hdLinkIdQueue, &hdLinkIdQueueTable](HdLinkId hdLinkId1,
                                                                                          double FontDistanceTarget) {
            std::function<void(HdLinkId, double)> func;
            func = [this, &createFont, &hdLinkIdQueue, &hdLinkIdQueueTable, &func](HdLinkId hdLinkId1,
                                                                                   double FontDistanceTarget) {
                const HdLink *hdLinkPtrTmp = m_spHdLib->GetLinkData(hdLinkId1);
                for (HdLinkId hdLinkIdTmp: hdLinkPtrTmp->vctliNxtLinkId) {
                    std::map<int, Point> resultGeoMap_FontTmp;
                    double FontDistanceTargetTmp1 = FontDistanceTarget;
                    const HdLink *hdLinkPtrTmp = m_spHdLib->GetLinkData(hdLinkIdTmp);
                    if (!createFont(hdLinkPtrTmp->vctvdGeoLink[0], hdLinkPtrTmp->vctvdGeoLink, 1,
                                    FontDistanceTargetTmp1, resultGeoMap_FontTmp)) {
                        hdLinkIdQueue.push_front(hdLinkPtrTmp->liId);
                        func(hdLinkPtrTmp->liId, FontDistanceTargetTmp1);
                    } else {
                        hdLinkIdQueue.push_front(hdLinkPtrTmp->liId);
                        hdLinkIdQueueTable[hdLinkPtrTmp->liId] = resultGeoMap_FontTmp;
                    }
                }
            };
            return func(hdLinkId1, FontDistanceTarget);
        };

        /******车辆后方*******/
        std::map<int, Point> resultGeoMap_behind;
        double BehindDistanceTargetTmp = BehindDistanceTarget;
        if (createBehind(footPointOfCar, hdLinkPtr->vctvdGeoLink, indexOfCarPoint, BehindDistanceTargetTmp,
                         resultGeoMap_behind)) { //计算自车link
            hdLinkIdQueue.push_back(hdLinkPtr->liId);
            hdLinkIdQueueTable[hdLinkPtr->liId] = resultGeoMap_behind;
        } else {//计算以前走过的link
            hdLinkIdQueue.push_back(hdLinkPtr->liId);
            HdLinkId hdLinkIdLast;
            if (hdLinkIdLastQueue.size() < 2) { //如果没有以前的linkId记录
                if (!hdLinkPtr->vctliPreLinkId.empty()) {
                    hdLinkIdLast = hdLinkPtr->vctliPreLinkId[0];
                }
            } else { //如果有以前的linkId记录
                hdLinkIdLast = hdLinkIdLastQueue.front();
            }
            if (hdLinkIdLast.lValue != 0) {
                const HdLink *hdLinkLastPtr = m_spHdLib->GetLinkData(hdLinkIdLast);
                if (createBehind(hdLinkLastPtr->vctvdGeoLink[hdLinkLastPtr->vctvdGeoLink.size() - 1],
                                 hdLinkLastPtr->vctvdGeoLink, hdLinkLastPtr->vctvdGeoLink.size() - 1,
                                 BehindDistanceTargetTmp, resultGeoMap_behind)) {
                    hdLinkIdQueue.push_back(hdLinkLastPtr->liId);
                    hdLinkIdQueueTable[hdLinkLastPtr->liId] = resultGeoMap_behind;
                } else {
                    hdLinkIdQueue.push_back(hdLinkLastPtr->liId);
                }
            }
        }
        if (hdLinkIdLastQueue.empty()) {
            hdLinkIdLastQueue.push(hdLinkPtr->liId);
        } else if (hdLinkIdLastQueue.size() == 1) {
            if (hdLinkIdLastQueue.back().lValue != hdLinkPtr->liId.lValue) {
                hdLinkIdLastQueue.push(hdLinkPtr->liId);
            }
        } else {
            if (hdLinkIdLastQueue.back().lValue != hdLinkPtr->liId.lValue) {
                hdLinkIdLastQueue.push(hdLinkPtr->liId);
            }
            hdLinkIdLastQueue.pop();
        }

        /******车辆前方*******/
        std::map<int, Point> resultGeoMap_Font;
        double FontDistanceTargetTmp = FontDistanceTarget;
        if (createFont(footPointOfCar, hdLinkPtr->vctvdGeoLink, indexOfCarPoint, FontDistanceTargetTmp,
                       resultGeoMap_Font)) { //计算自车link
            hdLinkIdQueueTable[hdLinkPtr->liId].insert(resultGeoMap_Font.begin(), resultGeoMap_Font.end());
        } else {//计算前方具有联通关系的link
            findFontLinksFunc(hdLinkPtr->liId, FontDistanceTargetTmp);
        }
        return true;
    }

/**
 * 计算需要输出的地图信息
 * @return
 */
    bool MapInfo::CreateMapInfo_Second(const HdLinkId &hdLinkId, const HdLaneId &hdLaneId, const Point pointIn,
                                            std::deque<HdLinkId> &hdLinkIdQueue,
                                            std::map<HdLinkId, std::map<int, Point>> &hdLinkIdQueueTable,
                                            std::vector<HdEdge>& vctheEdgeResult) {

        //(1)队列尾部第一个元素 特殊处理:从Index开始向数组尾部方向输出边界线
        if((hdLinkIdQueueTable.count(hdLinkIdQueue.back())!=0)&&
           hdLinkIdQueueTable[hdLinkIdQueue.back()].size()==1){

            Point pointTmp;
            for(auto it:hdLinkIdQueueTable[hdLinkIdQueue.back()]){
                pointTmp = it.second;
            }
            const HdLink *hdLinkPtr = m_spHdLib->GetLinkData(hdLinkIdQueue.back());
            for(HdEdge hdEdge : hdLinkPtr->vctheEdge){
                Point footPoint;
                double dis;
                int footIndex;
                HdLibMath::GetPerpendicularFoot(pointTmp,hdEdge.vctvdGeoLink,footPoint,dis,footIndex);
                int targetEdgePointSize = hdEdge.vctvdGeoLink.size()-footIndex;
                //删除数组中前面的元素,一直到元素个数到达目标个数。
                for(std::vector<std::vector<double>>::iterator it = hdEdge.vctvdGeoLink.begin();hdEdge.vctvdGeoLink.size()>targetEdgePointSize;){
                    it = hdEdge.vctvdGeoLink.erase(it);
                }
                std::vector<double> Vectpoint = {footPoint.dLon,footPoint.dLat,footPoint.dAlt};
                hdEdge.vctvdGeoLink.insert(hdEdge.vctvdGeoLink.begin(),Vectpoint);
                vctheEdgeResult.push_back(hdEdge);
            }
            hdLinkIdQueue.pop_back();
        }
        //(2)遍历队列中所有link
        while(!hdLinkIdQueue.empty()){
            /**table里面没有记录信息,则表示link的全部的边界线都要**/
            if(hdLinkIdQueueTable.find(hdLinkIdQueue.back())==hdLinkIdQueueTable.end()){

                const HdLink *hdLinkPtr = m_spHdLib->GetLinkData(hdLinkIdQueue.back());
                for(HdEdge hdEdge : hdLinkPtr->vctheEdge){
                    vctheEdgeResult.push_back(hdEdge);
                }
                hdLinkIdQueue.pop_back();

            }
                /**table里面记录一条数据,则从Index开始向数组头部方向输出边界线**/
            else if(hdLinkIdQueueTable[hdLinkIdQueue.back()].size()==1){

                Point pointTmp;
                for(auto it:hdLinkIdQueueTable[hdLinkIdQueue.back()]){
                    pointTmp = it.second;
                }
                const HdLink *hdLinkPtr = m_spHdLib->GetLinkData(hdLinkIdQueue.back());
                for(HdEdge hdEdge : hdLinkPtr->vctheEdge){
                    Point footPoint;
                    double dis;
                    int footIndex;
                    HdLibMath::GetPerpendicularFoot(pointTmp,hdEdge.vctvdGeoLink,footPoint,dis,footIndex);
                    int targetEdgePointSize = footIndex;
                    //删除数组中后面的元素,一直到元素个数到达目标个数。
                    while( hdEdge.vctvdGeoLink.size()>targetEdgePointSize){
                        hdEdge.vctvdGeoLink.pop_back();
                    }
                    std::vector<double> Vectpoint = {footPoint.dLon,footPoint.dLat,footPoint.dAlt};
                    hdEdge.vctvdGeoLink.push_back(Vectpoint);
                    vctheEdgeResult.push_back(hdEdge);
                }
                hdLinkIdQueue.pop_back();

            }
                /**table里面记录两条数据,则从小Index向大Index方向输出边界线**/
            else{

                std::vector<Point>  VctpointTmp;
                for(auto it:hdLinkIdQueueTable[hdLinkIdQueue.back()]){
                    VctpointTmp.push_back(it.second);
                }
                const HdLink *hdLinkPtr = m_spHdLib->GetLinkData(hdLinkIdQueue.back());
                for(HdEdge hdEdge : hdLinkPtr->vctheEdge){
                    Point footPoint1,footPoint2;
                    double dis1,dis2;
                    int footIndex1,footIndex2;

                    HdLibMath::GetPerpendicularFoot(VctpointTmp[0],hdEdge.vctvdGeoLink,footPoint1,dis1,footIndex1);
                    HdLibMath::GetPerpendicularFoot(VctpointTmp[1],hdEdge.vctvdGeoLink,footPoint2,dis2,footIndex2);

                    if(footIndex1>=footIndex2){
                        /**首先删除尾部元素*/
                        int targetEdgePointSize = footIndex1;
                        //删除数组中后面的元素,一直到元素个数到达目标个数。
                        while( hdEdge.vctvdGeoLink.size()>targetEdgePointSize){
                            hdEdge.vctvdGeoLink.pop_back();
                        }
                        std::vector<double> Vectpoint = {footPoint1.dLon,footPoint1.dLat,footPoint1.dAlt};
                        hdEdge.vctvdGeoLink.push_back(Vectpoint);
                        /***删除头部元素****/
                        int targetEdgePointSize2 = footIndex2;
                        for(std::vector<std::vector<double>>::iterator it = hdEdge.vctvdGeoLink.begin();targetEdgePointSize2 > 0;){
                            it = hdEdge.vctvdGeoLink.erase(it);
                            targetEdgePointSize2--;
                        }
                        std::vector<double> Vectpoint2 = {footPoint2.dLon,footPoint2.dLat,footPoint2.dAlt};
                        hdEdge.vctvdGeoLink.insert(hdEdge.vctvdGeoLink.begin(),Vectpoint2);
                        vctheEdgeResult.push_back(hdEdge);

                    }else{

                        /**首先删除尾部元素*/
                        int targetEdgePointSize = footIndex2;
                        //删除数组中后面的元素,一直到元素个数到达目标个数。
                        while( hdEdge.vctvdGeoLink.size()>targetEdgePointSize){
                            hdEdge.vctvdGeoLink.pop_back();
                        }
                        std::vector<double> Vectpoint = {footPoint2.dLon,footPoint2.dLat,footPoint2.dAlt};
                        hdEdge.vctvdGeoLink.push_back(Vectpoint);
                        /***删除头部元素****/
                        int targetEdgePointSize2 = footIndex1;
                        for(std::vector<std::vector<double>>::iterator it = hdEdge.vctvdGeoLink.begin();targetEdgePointSize2 > 0;){
                            it = hdEdge.vctvdGeoLink.erase(it);
                            targetEdgePointSize2--;
                        }
                        std::vector<double> Vectpoint2 = {footPoint1.dLon,footPoint1.dLat,footPoint1.dAlt};
                        hdEdge.vctvdGeoLink.insert(hdEdge.vctvdGeoLink.begin(),Vectpoint2);
                        vctheEdgeResult.push_back(hdEdge);
                    }
                }
                hdLinkIdQueue.pop_back();

            }
        }
        return true;
    }


}  // namespace jf