LCOV - code coverage report
Current view: top level - src/access - AdMapAccess.cpp (source / functions) Hit Total Coverage
Test: ad_map_access Lines: 110 113 97.3 %
Date: 2022-10-04 09:48:07 Functions: 11 11 100.0 %
Branches: 111 186 59.7 %

           Branch data     Line data    Source code
       1                 :            : // ----------------- BEGIN LICENSE BLOCK ---------------------------------
       2                 :            : //
       3                 :            : // Copyright (C) 2018-2021 Intel Corporation
       4                 :            : //
       5                 :            : // SPDX-License-Identifier: MIT
       6                 :            : //
       7                 :            : // ----------------- END LICENSE BLOCK -----------------------------------
       8                 :            : 
       9                 :            : #include <spdlog/sinks/stdout_color_sinks.h>
      10                 :            : 
      11                 :            : #include "AdMapAccess.hpp"
      12                 :            : #include "ad/map/opendrive/AdMapFactory.hpp"
      13                 :            : #include "ad/map/point/Operation.hpp"
      14                 :            : #include "ad/map/serialize/SerializerFileCRC32.hpp"
      15                 :            : 
      16                 :            : namespace ad {
      17                 :            : namespace map {
      18                 :            : namespace access {
      19                 :            : 
      20                 :            : typedef std::lock_guard<std::recursive_mutex> LockGuard;
      21                 :            : 
      22                 :    1592320 : AdMapAccess &AdMapAccess::getAdMapAccessInstance()
      23                 :            : {
      24   [ +  +  +  -  :    1592320 :   static AdMapAccess singleton;
             +  -  -  - ]
      25                 :    1592320 :   return singleton;
      26                 :            : }
      27                 :            : 
      28                 :     900508 : AdMapAccess &AdMapAccess::getInitializedInstance()
      29                 :            : {
      30         [ +  - ]:     900508 :   AdMapAccess &singleton = getAdMapAccessInstance();
      31         [ +  - ]:     900508 :   LockGuard guard(singleton.mMutex);
      32   [ +  -  +  +  :     900508 :   if (!singleton.mConfigFileHandler.isInitialized() && !singleton.mInitializedFromStore)
             -  +  -  + ]
      33                 :            :   {
      34         [ #  # ]:          0 :     throw std::runtime_error("Instance not initialized. Forgot to call AdMapAccess::initialize?");
      35                 :            :   }
      36                 :    1801020 :   return singleton;
      37                 :            : }
      38                 :            : 
      39         [ +  - ]:          1 : AdMapAccess::AdMapAccess()
      40                 :            : {
      41         [ +  - ]:          1 :   reset();
      42                 :            : 
      43   [ +  -  +  - ]:          1 :   mLogger = spdlog::stdout_color_mt("ad_map_access");
      44         [ +  - ]:          1 :   mLogger->set_level(spdlog::level::warn);
      45                 :          1 : }
      46                 :            : 
      47                 :          1 : AdMapAccess::~AdMapAccess()
      48                 :            : {
      49                 :          1 : }
      50                 :            : 
      51                 :        196 : bool AdMapAccess::initialize(std::string const &configFileName)
      52                 :            : {
      53         [ +  - ]:        392 :   LockGuard guard(mMutex);
      54         [ +  + ]:        196 :   if (mInitializedFromStore)
      55                 :            :   {
      56         [ +  - ]:          1 :     mLogger->error("AdMapAccess::initialize(config) failed; already initialized from store");
      57                 :          1 :     return false;
      58                 :            :   }
      59   [ +  -  +  + ]:        195 :   if (mConfigFileHandler.isInitialized())
      60                 :            :   {
      61   [ +  -  +  + ]:          2 :     if (!mConfigFileHandler.isInitializedWithFilename(configFileName))
      62                 :            :     {
      63         [ +  - ]:          2 :       mLogger->error(
      64                 :            :         "AdMapAccess::initialize(config) with file {} failed; already initialized with different config: {}",
      65                 :            :         configFileName,
      66         [ +  - ]:          1 :         mConfigFileHandler.configFileName());
      67                 :          1 :       return false;
      68                 :            :     }
      69                 :          1 :     return true;
      70                 :            :   }
      71   [ +  -  +  + ]:        193 :   if (!mConfigFileHandler.readConfig(configFileName))
      72                 :            :   {
      73         [ +  - ]:          6 :     mLogger->warn("AdMapAccess::initialize(config) Failed to open {}", configFileName);
      74                 :          3 :     return false;
      75                 :            :   }
      76                 :            : 
      77         [ +  - ]:        380 :   mLogger->info("AdMapAccess::initialize(config) Successfully opened {}", configFileName);
      78   [ +  -  +  -  :        190 :   if (!readMap(mConfigFileHandler.adMapEntry().filename))
                   +  + ]
      79                 :            :   {
      80   [ +  -  +  - ]:          6 :     mLogger->warn("Unable to read map {}", mConfigFileHandler.adMapEntry().filename);
      81         [ +  - ]:          3 :     reset();
      82                 :          3 :     return false;
      83                 :            :   }
      84   [ +  -  +  + ]:        187 :   if (mConfigFileHandler.defaultEnuReferenceAvailable())
      85                 :            :   {
      86   [ +  -  +  - ]:        141 :     setENUReferencePoint(mConfigFileHandler.defaultEnuReference());
      87                 :            :   }
      88                 :        187 :   return true;
      89                 :            : }
      90                 :            : 
      91                 :          6 : bool AdMapAccess::initializeFromOpenDriveContent(std::string const &openDriveContent,
      92                 :            :                                                  double const overlapMargin,
      93                 :            :                                                  intersection::IntersectionType const defaultIntersectionType,
      94                 :            :                                                  landmark::TrafficLightType const defaultTrafficLightType)
      95                 :            : {
      96         [ +  - ]:         12 :   LockGuard guard(mMutex);
      97   [ +  -  +  + ]:          6 :   if (mConfigFileHandler.isInitialized())
      98                 :            :   {
      99         [ +  - ]:          2 :     mLogger->error(
     100                 :            :       "AdMapAccess::initializeFromOpenDriveContent() failed; already initialized with different config: {}",
     101         [ +  - ]:          1 :       mConfigFileHandler.configFileName());
     102                 :          1 :     return false;
     103                 :            :   }
     104                 :            : 
     105                 :            :   auto const openDriveContentChecksum
     106         [ +  - ]:          5 :     = serialize::calculateCRC32Checksum(openDriveContent.c_str(), openDriveContent.size());
     107         [ +  + ]:          5 :   if (mInitializedFromStore)
     108                 :            :   {
     109         [ +  + ]:          2 :     if (openDriveContentChecksum == mInitializeFromOpenDriveContentChecksum)
     110                 :            :     {
     111                 :            :       // initialize twice from same store object is ok.
     112                 :          1 :       return true;
     113                 :            :     }
     114                 :            :     else
     115                 :            :     {
     116         [ +  - ]:          1 :       mLogger->error("AdMapAccess::initializeFromOpenDriveContent() failed; already initialized with different store "
     117                 :            :                      "object or content");
     118                 :          1 :       return false;
     119                 :            :     }
     120                 :            :   }
     121                 :            : 
     122         [ +  - ]:          6 :   auto store = std::make_shared<Store>();
     123         [ +  - ]:          3 :   opendrive::AdMapFactory factory(*store);
     124                 :            : 
     125                 :            :   bool result
     126         [ +  - ]:          3 :     = factory.createAdMapFromString(openDriveContent, overlapMargin, defaultIntersectionType, defaultTrafficLightType);
     127         [ +  + ]:          3 :   if (result)
     128                 :            :   {
     129                 :          2 :     mInitializeFromOpenDriveContentChecksum = openDriveContentChecksum;
     130                 :          2 :     mInitializedFromStore = true;
     131                 :          2 :     mStore = store;
     132                 :            :   }
     133                 :            :   else
     134                 :            :   {
     135         [ +  - ]:          1 :     mLogger->error("AdMapAccess::initializeFromOpenDriveContent() failed; error parsing OpenDRIVE content");
     136                 :            :   }
     137                 :          3 :   return result;
     138                 :            : }
     139                 :            : 
     140                 :         29 : bool AdMapAccess::initialize(Store::Ptr store)
     141                 :            : {
     142         [ +  - ]:         58 :   LockGuard guard(mMutex);
     143   [ +  +  +  -  :         29 :   if (!bool(store) || !store->isValid())
             +  +  +  + ]
     144                 :            :   {
     145         [ +  - ]:          2 :     mLogger->error("AdMapAccess::initialize(store) provided store is invalid");
     146                 :          2 :     return false;
     147                 :            :   }
     148   [ +  -  +  + ]:         27 :   if (mConfigFileHandler.isInitialized())
     149                 :            :   {
     150         [ +  - ]:          2 :     mLogger->error("AdMapAccess::initialize(store) failed; already initialized with different config {}",
     151         [ +  - ]:          1 :                    mConfigFileHandler.configFileName());
     152                 :          1 :     return false;
     153                 :            :   }
     154                 :            : 
     155         [ +  + ]:         26 :   if (mInitializedFromStore)
     156                 :            :   {
     157         [ -  + ]:          1 :     if (mStore != store)
     158                 :            :     {
     159         [ #  # ]:          0 :       mLogger->error("AdMapAccess::initialize(store) failed; already initialized with different store object");
     160                 :          0 :       return false;
     161                 :            :     }
     162                 :            :     else
     163                 :            :     {
     164                 :            :       // initialize twice from same store object is ok.
     165                 :          1 :       return true;
     166                 :            :     }
     167                 :            :   }
     168                 :         25 :   mInitializedFromStore = true;
     169                 :            : 
     170                 :         25 :   mStore = store;
     171         [ +  - ]:         25 :   auto boundingSphere = mStore->getBoundingSphere();
     172         [ +  - ]:         25 :   auto centerGeo = point::toGeo(boundingSphere.center);
     173         [ +  - ]:         25 :   setENUReferencePoint(centerGeo);
     174                 :            : 
     175         [ +  - ]:         25 :   mLogger->info("AdMapAccess::initialized from store");
     176                 :         25 :   return true;
     177                 :            : }
     178                 :            : 
     179                 :        447 : void AdMapAccess::reset()
     180                 :            : {
     181         [ +  - ]:        447 :   LockGuard guard(mMutex);
     182         [ +  - ]:        447 :   mStore = std::make_shared<Store>();
     183         [ +  - ]:        447 :   mConfigFileHandler.reset();
     184                 :        447 :   mInitializedFromStore = false;
     185         [ +  - ]:        447 :   mCoordinateTransform = std::make_shared<point::CoordinateTransform>();
     186                 :        447 : }
     187                 :            : 
     188                 :        190 : bool AdMapAccess::readMap(std::string const &mapName)
     189                 :            : {
     190         [ +  + ]:        190 :   if (ad::map::opendrive::AdMapFactory::isOpenDriveMap(mapName))
     191                 :            :   {
     192                 :         41 :     return readOpenDriveMap(mapName);
     193                 :            :   }
     194                 :            :   else
     195                 :            :   {
     196                 :        149 :     return readAdMap(mapName);
     197                 :            :   }
     198                 :            : }
     199                 :            : 
     200                 :         41 : bool AdMapAccess::readOpenDriveMap(std::string const &mapName)
     201                 :            : {
     202         [ +  - ]:         82 :   opendrive::AdMapFactory factory(*mStore);
     203         [ +  - ]:         41 :   return factory.createAdMap(mapName,
     204                 :         41 :                              static_cast<double>(mConfigFileHandler.adMapEntry().openDriveOverlapMargin),
     205         [ +  - ]:         41 :                              mConfigFileHandler.adMapEntry().openDriveDefaultIntersectionType,
     206   [ +  -  +  - ]:        123 :                              mConfigFileHandler.adMapEntry().openDriveDefaultTrafficLightType);
     207                 :            : }
     208                 :            : 
     209                 :        149 : bool AdMapAccess::readAdMap(std::string const &mapName)
     210                 :            : {
     211         [ +  - ]:        298 :   serialize::SerializerFileCRC32 serializer(false);
     212                 :        149 :   size_t version_major = 0;
     213                 :        149 :   size_t version_minor = 0;
     214   [ +  -  +  -  :        149 :   if (!serializer.open(mapName.c_str(), version_major, version_minor))
                   +  + ]
     215                 :            :   {
     216         [ +  - ]:          2 :     mLogger->warn("Unable to open map for reading {}", mapName);
     217                 :          1 :     return false;
     218                 :            :   }
     219         [ +  - ]:        296 :   mLogger->info("Opened map {}", mapName);
     220         [ +  - ]:        296 :   mLogger->trace("Version {}.{}", version_major, version_minor);
     221   [ +  -  +  + ]:        148 :   if (!mStore->load(serializer))
     222                 :            :   {
     223         [ +  - ]:          2 :     mLogger->warn("Unable to read map {}", mapName);
     224                 :          1 :     return false;
     225                 :            :   }
     226   [ +  -  +  + ]:        147 :   if (!serializer.close())
     227                 :            :   {
     228         [ +  - ]:          2 :     mLogger->warn("Map file is corrupt {}", mapName);
     229                 :          1 :     return false;
     230                 :            :   }
     231                 :            : 
     232                 :        146 :   return true;
     233                 :            : }
     234                 :            : 
     235                 :            : } // namespace access
     236                 :            : } // namespace map
     237                 :            : } // namespace ad

Generated by: LCOV version 1.14