685 std::vector<WAIMapPoint*> mapPoints;
686 std::vector<WAIKeyFrame*> keyFrames;
687 std::map<int, int> parentIdMap;
688 std::map<int, std::vector<int>> loopEdgesMap;
689 std::map<int, WAIKeyFrame*> kfsMap;
690 int numLoopClosings = 0;
699 FILE* f = fopen(path.c_str(),
"rb");
703 fseek(f, 0, SEEK_END);
704 uint32_t contentSize = ftell(f);
707 uint8_t* fContent = (uint8_t*)malloc(
sizeof(uint8_t*) * contentSize);
708 uint8_t* fContentStart = fContent;
712 size_t readResult = fread(fContent, 1, contentSize, f);
713 if (readResult != contentSize)
718 MapInfo* mapInfo = (MapInfo*)fContent;
719 fContent +=
sizeof(MapInfo);
721 if (mapInfo->nodeOmSaved)
724 mapNodeOm = cvMat.clone();
727 std::map<int, std::vector<int>> bestCovisibleKeyFrameIdsMap;
728 std::map<int, std::vector<int>> bestCovisibleWeightsMap;
730 for (
int i = 0; i < mapInfo->kfCount; i++)
732 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::keyFrames");
734 KeyFrameInfo* kfInfo = (KeyFrameInfo*)fContent;
735 fContent +=
sizeof(KeyFrameInfo);
738 int parentId = kfInfo->parentId;
742 parentIdMap[id] = parentId;
748 if (kfInfo->loopEdgesCount > 0)
750 std::vector<int32_t> loopEdges =
751 loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->loopEdgesCount);
753 loopEdgesMap[id] = loopEdges;
757 std::vector<cv::KeyPoint> keyPtsUndist = loadVectorFromBinaryStream<cv::KeyPoint>(&fContent, kfInfo->kpCount);
759 float scaleFactor = kfInfo->scaleFactor;
760 int nScaleLevels = kfInfo->scaleLevels;
763 std::vector<float> vScaleFactor;
764 std::vector<float> vInvScaleFactor;
765 std::vector<float> vLevelSigma2;
766 std::vector<float> vInvLevelSigma2;
767 vScaleFactor.clear();
768 vLevelSigma2.clear();
769 vScaleFactor.resize(nScaleLevels);
770 vLevelSigma2.resize(nScaleLevels);
772 vScaleFactor[0] = 1.0f;
773 vLevelSigma2[0] = 1.0f;
774 for (
int j = 1; j < nScaleLevels; j++)
776 vScaleFactor[j] = vScaleFactor[j - 1] * scaleFactor;
777 vLevelSigma2[j] = vScaleFactor[j] * vScaleFactor[j];
780 vInvScaleFactor.resize(nScaleLevels);
781 vInvLevelSigma2.resize(nScaleLevels);
782 for (
int j = 0; j < nScaleLevels; j++)
784 vInvScaleFactor[j] = 1.0f / vScaleFactor[j];
785 vInvLevelSigma2[j] = 1.0f / vLevelSigma2[j];
788 float fx, fy, cx, cy;
789 fx = K.at<
float>(0, 0);
790 fy = K.at<
float>(1, 1);
791 cx = K.at<
float>(0, 2);
792 cy = K.at<
float>(1, 2);
795 float nMinX = kfInfo->minX;
796 float nMinY = kfInfo->minY;
797 float nMaxX = kfInfo->maxX;
798 float nMaxY = kfInfo->maxY;
822 if (kfInfo->bowVecSize > 0)
824 std::vector<int32_t> wordsId = loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->bowVecSize);
825 std::vector<float> tfIdf = loadVectorFromBinaryStream<float>(&fContent, kfInfo->bowVecSize);
834 ss << imgDir <<
"kf" <<
id <<
".jpg";
839 cv::Mat imgColor = cv::imread(ss.str());
840 cv::cvtColor(imgColor, newKf->
imgGray, cv::COLOR_BGR2GRAY);
844 keyFrames.push_back(newKf);
845 kfsMap[newKf->
mnId] = newKf;
847 std::vector<int32_t> bestCovisibleKeyFrameIds = loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->covisiblesCount);
848 std::vector<int32_t> bestCovisibleWeights = loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->covisiblesCount);
850 bestCovisibleKeyFrameIdsMap[newKf->
mnId] = bestCovisibleKeyFrameIds;
851 bestCovisibleWeightsMap[newKf->
mnId] = bestCovisibleWeights;
859 auto itParentId = parentIdMap.find(kf->mnId);
860 if (itParentId != parentIdMap.end())
862 int parentId = itParentId->second;
863 auto itParentKf = kfsMap.find(parentId);
864 if (itParentKf != kfsMap.end())
865 kf->ChangeParent(itParentKf->second);
867 cerr <<
"[WAIMapIO] loadKeyFrames: Parent does not exist of keyframe " << kf->mnId <<
"! FAIL" << endl;
870 cerr <<
"[WAIMapIO] loadKeyFrames: Parent does not exist of keyframe " << kf->mnId <<
"! FAIL" << endl;
874 int numberOfLoopClosings = 0;
878 auto it = loopEdgesMap.find(kf->mnId);
879 if (it != loopEdgesMap.end())
881 const auto& loopEdgeIds = it->second;
882 for (
int loopKfId : loopEdgeIds)
884 auto loopKfIt = kfsMap.find(loopKfId);
885 if (loopKfIt != kfsMap.end())
887 kf->AddLoopEdge(loopKfIt->second);
888 numberOfLoopClosings++;
891 cerr <<
"[WAIMapIO] loadKeyFrames: Loop keyframe id does not exist! FAIL" << endl;
895 numLoopClosings = numberOfLoopClosings / 2;
897 for (
int i = 0; i < mapInfo->mpCount; i++)
899 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::mapPoints");
901 MapPointInfo* mpInfo = (MapPointInfo*)fContent;
902 fContent +=
sizeof(MapPointInfo);
907 std::vector<int32_t> observingKfIds = loadVectorFromBinaryStream<int32_t>(&fContent, mpInfo->nObervations);
908 std::vector<int32_t> corrKpIndices = loadVectorFromBinaryStream<int32_t>(&fContent, mpInfo->nObervations);
920 int refKfId = (int)mpInfo->refKfId;
921 bool refKFFound =
false;
923 if (kfsMap.find(refKfId) != kfsMap.end())
925 newPt->
refKf(kfsMap[refKfId]);
930 cout <<
"no reference keyframe found!" << endl;
931 if (observingKfIds.size())
934 int kfId = observingKfIds[0];
935 if (kfsMap.find(kfId) != kfsMap.end())
937 newPt->
refKf(kfsMap[kfId]);
946 for (
int j = 0; j < observingKfIds.size(); j++)
948 const int kfId = observingKfIds[j];
949 if (kfsMap.find(kfId) != kfsMap.end())
956 mapPoints.push_back(newPt);
966 bool buildSpanningTree =
false;
969 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::updateConnections");
971 std::map<WAIKeyFrame*, int> keyFrameWeightMap;
973 std::vector<int> bestCovisibleKeyFrameIds = bestCovisibleKeyFrameIdsMap[kf->mnId];
974 std::vector<int> bestCovisibleWeights = bestCovisibleWeightsMap[kf->mnId];
976 for (
int i = 0; i < bestCovisibleKeyFrameIds.size(); i++)
978 int keyFrameId = bestCovisibleKeyFrameIds[i];
979 int weight = bestCovisibleWeights[i];
981 keyFrameWeightMap[covisibleKF] = weight;
990 else if (kf->GetParent() == NULL)
992 buildSpanningTree =
true;
996 wai_assert(firstKF &&
"Could not find keyframe with id 0\n");
999 if (buildSpanningTree)
1001 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::buildSpanningTree");
1004 using QueueElem = std::tuple<WAIKeyFrame*, WAIKeyFrame*, int>;
1005 auto cmpQueue = [](
const QueueElem& left,
const QueueElem& right)
1006 {
return (std::get<2>(left) < std::get<2>(right)); };
1007 auto cmpMap = [](
const pair<WAIKeyFrame*, int>& left,
const pair<WAIKeyFrame*, int>& right)
1008 {
return left.second < right.second; };
1009 std::set<WAIKeyFrame*> graph;
1010 std::set<WAIKeyFrame*> unconKfs;
1011 for (
auto& kf : keyFrames)
1012 unconKfs.insert(kf);
1015 graph.insert(firstKF);
1016 unconKfs.erase(firstKF);
1018 while (unconKfs.size())
1020 std::priority_queue<QueueElem, std::vector<QueueElem>, decltype(cmpQueue)> q(cmpQueue);
1022 for (
auto& unconKf : unconKfs)
1024 const std::map<WAIKeyFrame*, int>& weights = unconKf->GetConnectedKfWeights();
1025 for (
auto& graphKf : graph)
1027 auto it = weights.find(graphKf);
1028 if (it != weights.end())
1030 QueueElem newElem = std::make_tuple(unconKf, it->first, it->second);
1039 Utils::log(
"WAIMapStorage",
"Error in building spanning tree: There are %i unconnected keyframes!", unconKfs.size());
1045 QueueElem topElem = q.top();
1048 unconKfs.erase(newGraphKf);
1052 graph.insert(newGraphKf);
1059 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::addKeyFrame");
1061 if (kf->mBowVec.data.empty())
1063 std::cout <<
"kf->mBowVec.data empty" << std::endl;
1078 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::addMapPoint");
1085 free(fContentStart);
static cv::Mat loadCVMatFromBinaryStream(uint8_t **data, int rows, int cols, int type)