MapInterface.cpp 8.75 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
#include "MapInterface.hpp"

#include <cmath>
#include <iostream>
#include <limits>
namespace jf {
MapInterface::MapInterface() {  }

MapInterface::~MapInterface() {}

void MapInterface::Init(const std::string& strPrjPath, const std::string strCfgPath)
{
    m_mlMapLib.Init(strPrjPath, strCfgPath);
}

bool MapInterface::GetMapData(const Point &ptInLoc, double dCarAngle, int &nOutLaneCnt, int &nOutLaneNum, LaneType &nOutLaneType, EdgeCrossType &nOutLeftEdgeCrossType, EdgeCrossType &nOutRightEdgeCrossType, int &nOutSpeedLimit, double &dOutLaneAngle,
                              Point &ptOutFoot) {
    long lRoadId;
    std::vector<long> vctlPreRoadId = {};
    std::vector<long> vctlNxtRoadId = {};
oscar's avatar
oscar committed
21 22 23
    int isInCross = 0;
    return GetMapData(ptInLoc, dCarAngle, lRoadId, vctlPreRoadId, vctlNxtRoadId, nOutLaneCnt, nOutLaneNum, nOutLaneType, nOutLeftEdgeCrossType, nOutRightEdgeCrossType, nOutSpeedLimit, dOutLaneAngle, ptOutFoot, isInCross);
}
oscar's avatar
oscar committed
24

oscar's avatar
oscar committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//  The function will return YES if the point x,y is inside the polygon, or
//  NO if it is not.  If the point is exactly on the edge of the polygon,
//  then the function may return YES or NO.
bool IsPointInPolygon(std::vector<std::vector<float>>& poly, Point& pt)
{
    int i, j;
    bool c = false;
    for (i = 0, j = poly.size() - 1; i < poly.size(); j = i++)
    {
        if ((((poly[i][1] <= pt.dLat) && (pt.dLat < poly[j][1])) ||
            ((poly[j][1] <= pt.dLat) && (pt.dLat < poly[i][1])))
            && (pt.dLon < (poly[j][0] - poly[i][0]) * (pt.dLat - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0]))
        {
            c = !c;
        }
    }
    return c;
oscar's avatar
oscar committed
42 43 44
}

bool MapInterface::GetMapData(const Point &ptInLoc, double dCarAngle, long &lOutRaodId, std::vector<long> &vctlOutPreRoadId, std::vector<long> &vctlOutNxtRoadId, int &nOutLaneCnt, int &nOutLaneNum, LaneType &nOutLaneType,
oscar's avatar
oscar committed
45
                              EdgeCrossType &nOutLeftEdgeCrossType, EdgeCrossType &nOutRightEdgeCrossType, int &nOutSpeedLimit, double &dOutLaneAngle, Point &ptOutFoot,int &isInCross) {
oscar's avatar
oscar committed
46 47 48
    std::vector<HdLinkId> vcthlLinkIds = {};
    std::vector<HdLaneId> vcthlLaneIds = {};
    // Locate lane failed
oscar's avatar
oscar committed
49
    isInCross = 0;
oscar's avatar
oscar committed
50 51 52 53 54 55 56 57 58 59 60 61
    if (false == m_mlMapLib.GetLocateLanes(ptInLoc, vcthlLinkIds, vcthlLaneIds)) {
        nOutLaneCnt = 0;
        nOutLaneNum = 0;
        nOutLaneType = LaneType::NotInvestigated;
        nOutLeftEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = ptInLoc;
        //std::cout << "MapInterface::GetMapData(), Locate lane failed." << std::endl;
        return false;
    }
oscar's avatar
oscar committed
62
    std::vector<HdLinkId> vcthlLinkNearby = {};
oscar's avatar
oscar committed
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
    if (false == m_mlMapLib.GetDataNearby(ptInLoc, 10.0f, 10.0f, vcthlLinkNearby))
    {
        nOutLaneCnt = 0;
        nOutLaneNum = 0;
        nOutLaneType = LaneType::NotInvestigated;
        nOutLeftEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = ptInLoc;
        return false;
    }
    for (int i = 0; i < vcthlLinkNearby.size(); i++)
    {
        const HdLinkId& hlLinkId = vcthlLinkNearby[i];
        const HdLink* cphlLink = m_mlMapLib.GetLinkData(hlLinkId);
        if (cphlLink)
        {
            for (int j = 0; j < cphlLink->vctcrCrossing; j++)
            {
                if (IsPointInPolygon(cphlLink->vctcrCrossing[j]->vctvdGeoArea, ptInLoc))
                {
                    isInCross = 1;
                    break;
                }
            }
        }
        if (isInCross == 1)
            break;
    }
oscar's avatar
oscar committed
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
    int nIndex = 0;
    double dMinAngleDiff = std::numeric_limits<double>::max();
    auto func_GetAngleDiff = [](double dAngle1, double dAngle2) { return std::min(std::abs(dAngle1 - dAngle2), 360 - std::abs((dAngle1 - dAngle2))); };
    for (int i = 0; i < vcthlLinkIds.size(); i++) {
        const HdLinkId &hlLinkId = vcthlLinkIds[i];
        const HdLaneId &hlLaneId = vcthlLaneIds[i];
        double dAzimuth = 0;
        if (vcthlLinkIds.size() == 1) {
            nIndex = i;
        } else {
            if (true == m_mlMapLib.GetLaneAzimuth(hlLinkId, hlLaneId, ptInLoc, dAzimuth) || true == m_mlMapLib.GetLinkStartAngle(hlLinkId, dAzimuth)) {
                double dAngleDiff = func_GetAngleDiff(dCarAngle, dAzimuth);
                if (dMinAngleDiff >= dAngleDiff) {
                    dMinAngleDiff = dAngleDiff;
                    nIndex = i;
                }
            }
        }
    }

    lOutRaodId = vcthlLinkIds[nIndex].lValue;
    std::vector<HdLinkId> vctliPLinkIds = {};
    std::vector<HdLinkId> vctliNLinkIds = {};
    m_mlMapLib.GetLinkPreNxt(vcthlLinkIds[nIndex], vctliPLinkIds, vctliNLinkIds);
    for (int i = 0; i < vctliPLinkIds.size(); ++i) {
        vctlOutPreRoadId.push_back(vctliPLinkIds.at(i).lValue);
    }
    for (int i = 0; i < vctliNLinkIds.size(); ++i) {
        vctlOutNxtRoadId.push_back(vctliNLinkIds.at(i).lValue);
    }

    if (false == m_mlMapLib.GetLaneCnt(vcthlLinkIds[nIndex], nOutLaneCnt)) {
        nOutLaneCnt = 0;
        nOutLaneNum = 0;
        nOutLaneType = LaneType::NotInvestigated;
        nOutLeftEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get lane count failed." << std::endl;
        return false;
    }

    if (false == m_mlMapLib.GetLaneNum(vcthlLinkIds[nIndex], vcthlLaneIds[nIndex], nOutLaneNum)) {
        nOutLaneNum = 0;
        nOutLaneType = LaneType::NotInvestigated;
        nOutLeftEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get lane num failed." << std::endl;
        return false;
    }

    if (false == m_mlMapLib.GetLaneType(vcthlLinkIds[nIndex], nOutLaneNum, (int &)nOutLaneType)) {
        nOutLaneType = LaneType::NotInvestigated;
        nOutLeftEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get lane type failed." << std::endl;
        return false;
    }

    if (false == m_mlMapLib.GetLaneEdgeCrossType(vcthlLinkIds[nIndex], nOutLaneNum, true, (int &)nOutLeftEdgeCrossType)) {
        nOutLeftEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get left lane edge cross failed." << std::endl;
        return false;
    }

    if (false == m_mlMapLib.GetLaneEdgeCrossType(vcthlLinkIds[nIndex], nOutLaneNum, false, (int &)nOutRightEdgeCrossType)) {
        nOutRightEdgeCrossType = EdgeCrossType::NotInvestigated;
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get right lane cross failed." << std::endl;
        return false;
    }

    if (false == m_mlMapLib.GetLinkSpeedLimit(vcthlLinkIds[nIndex], nOutSpeedLimit)) {
        nOutSpeedLimit = 0;
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get link speed failed." << std::endl;
        return false;
    }

    if (false == m_mlMapLib.GetLaneAzimuth(vcthlLinkIds[nIndex], vcthlLaneIds[nIndex], ptInLoc, dOutLaneAngle)) {
        dOutLaneAngle = 0.0;
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get lane angle failed." << std::endl;
        return false;
    }

    int nSeqNum = 0;
    if (false == m_mlMapLib.GetLaneNum(vcthlLinkIds[nIndex], vcthlLaneIds[nIndex], nSeqNum)) {
        ptOutFoot = {};
        std::cout << "MapInterface::GetMapData(), get lane num failed." << std::endl;
        return false;
    } else {
        std::vector<std::vector<double>> vctvdGeoLink = {};
        double dDistance = 0;
        if (false == m_mlMapLib.GetLaneGeoLink(vcthlLinkIds[nIndex], nSeqNum, vctvdGeoLink)) {
            ptOutFoot = {};
            //std::cout << "MapInterface::GetMapData(), get lane geolink failed." << std::endl;
            return false;
        } else {
            if (false == m_mlMapLib.GetPerpendicularFoot(ptInLoc, vctvdGeoLink, ptOutFoot, dDistance)) {
                ptOutFoot = {};
                std::cout << "MapInterface::GetMapData(), get lane foot point failed." << std::endl;
                return false;
            }
        };
    }

    return true;
}

}  // namespace jf