20 #define WIN32_LEAN_AND_MEAN
30 : mMap(m), mCamera(cam)
64 #define CHECK_RESET if(mbResetRequested) {Reset(); continue;};
164 cout <<
"Waiting for mapmaker to die.." << endl;
165 LOGV(
LOG_TAG,
"Info: %s",
"Waiting for mapmaker to die..");
167 cout <<
" .. mapmaker has died." << endl;
168 LOGV(
LOG_TAG,
"Info: %s",
".. mapmaker has died.");
176 PDash.slice<0,0,3,3>() = se3AfromB.
get_rotation().get_matrix();
180 A[0][0] = -1.0; A[0][1] = 0.0; A[0][2] = v2B[0]; A[0][3] = 0.0;
181 A[1][0] = 0.0; A[1][1] = -1.0; A[1][2] = v2B[1]; A[1][3] = 0.0;
182 A[2] = v2A[0] * PDash[2] - PDash[0];
183 A[3] = v2A[1] * PDash[2] - PDash[1];
187 if(v4Smallest[3] == 0.0)
188 v4Smallest[3] = 0.00001;
198 vector<pair<ImageRef, ImageRef> > &vTrailMatches,
199 SE3<> &se3TrackerPose)
206 vector<HomographyMatch> vMatches;
207 for(
unsigned int i=0; i<vTrailMatches.size(); i++)
213 vMatches.push_back(m);
219 bGood = HomographyInit.
Compute(vMatches, 5.0, se3);
222 cout <<
" Could not init from stereo pair, try again." << endl;
223 LOGV(
LOG_TAG,
"Info: %s",
"Could not init from stereo pair, try again.");
231 cout <<
" Estimated zero baseline from stereo pair, try again." << endl;
232 LOGV(
LOG_TAG,
"Info: %s",
" Estimated zero baseline from stereo pair, try again.");
253 for(
unsigned int i=0; i<vMatches.size(); i++)
261 p->
irCenter = vTrailMatches[i].first;
315 for(
int i=0; i<5; i++)
355 cout <<
" MapMaker: made initial map with " <<
mMap.
vpPoints.size() <<
" points." << endl;
358 tmp <<
"MapMaker: made initial map with "<<
mMap.
vpPoints.size()<<
" points."<< endl;
372 vector<Candidate> vCGood;
373 vector<ImageRef> irBusyLevelPos;
378 if(!(it->second.nLevel == nLevel || it->second.nLevel == nLevel + 1))
384 unsigned int nMinMagSquared = 10*10;
385 for(
unsigned int i=0; i<vCSrc.size(); i++)
389 for(
unsigned int j=0; j<irBusyLevelPos.size(); j++)
392 if((irB - irC).mag_squared() < nMinMagSquared)
399 vCGood.push_back(vCSrc[i]);
415 for(
unsigned int i = 0; i<l.
vCandidates.size(); i++)
478 it->first->pMMData->sMeasurementKFs.insert(pK);
503 static bool bMadeCache =
false;
530 Vector<3> v3RayStart_TC = v3CamCenter_TC + dStartDepth * v3LineDirn_TC;
531 Vector<3> v3RayEnd_TC = v3CamCenter_TC + dEndDepth * v3LineDirn_TC;
534 if(v3RayEnd_TC[2] <= v3RayStart_TC[2])
536 if(v3RayEnd_TC[2] <= 0.0 )
return false;
537 if(v3RayStart_TC[2] <= 0.0)
538 v3RayStart_TC += v3LineDirn_TC * (0.001 - v3RayStart_TC[2] / v3LineDirn_TC[2]);
542 Vector<2> v2AlongProjectedLine = v2A-v2B;
544 if(v2AlongProjectedLine * v2AlongProjectedLine < 0.00000001)
546 cout <<
"v2AlongProjectedLine too small." << endl;
551 v2Normal[0] = v2AlongProjectedLine[1];
552 v2Normal[1] = -v2AlongProjectedLine[0];
554 double dNormDist = v2A * v2Normal;
558 double dMinLen = min(v2AlongProjectedLine * v2A, v2AlongProjectedLine * v2B) - 0.05;
559 double dMaxLen = max(v2AlongProjectedLine * v2A, v2AlongProjectedLine * v2B) + 0.05;
560 if(dMinLen < -2.0) dMinLen = -2.0;
561 if(dMaxLen < -2.0) dMaxLen = -2.0;
562 if(dMinLen > 2.0) dMinLen = 2.0;
563 if(dMaxLen > 2.0) dMaxLen = 2.0;
574 for(
unsigned int i=0; i<vIR.size(); i++)
580 int nBestZMSSD = Finder.
mnMaxSSD + 1;
582 double dMaxDistSq = dMaxDistDiff * dMaxDistDiff;
584 for(
unsigned int i=0; i<vv2Corners.size(); i++)
587 double dDistDiff = dNormDist - v2Im * v2Normal;
588 if(dDistDiff * dDistDiff > dMaxDistSq)
continue;
589 if(v2Im * v2AlongProjectedLine < dMinLen)
continue;
590 if(v2Im * v2AlongProjectedLine > dMaxLen)
continue;
592 if(nZMSSD < nBestZMSSD)
599 if(nBest == -1)
return false;
605 if(!bSubPixConverges)
655 Vector<3> v3Diff = v3KF2_CamPos - v3KF1_CamPos;
656 double dDist = sqrt(v3Diff * v3Diff);
662 vector<pair<double, KeyFrame* > > vKFandScores;
670 if(N > vKFandScores.size())
671 N = vKFandScores.size();
672 partial_sort(vKFandScores.begin(), vKFandScores.begin() + N, vKFandScores.end());
674 vector<KeyFrame*> vResult;
675 for(
unsigned int i=0; i<N; i++)
676 vResult.push_back(vKFandScores[i].second);
682 double dClosestDist = 9999999999.9;
689 if(dDist < dClosestDist)
691 dClosestDist = dDist;
695 assert(nClosest != -1);
723 set<KeyFrame*> sFixed;
730 set<MapPoint*> sMapPoints;
749 set<KeyFrame*> sAdjustSet;
751 sAdjustSet.insert(pkfNewest);
753 for(
int i=0; i<4; i++)
754 if(vClosest[i]->bFixed ==
false)
755 sAdjustSet.insert(vClosest[i]);
758 set<MapPoint*> sMapPoints;
759 for(set<KeyFrame*>::iterator iter = sAdjustSet.begin();
760 iter!=sAdjustSet.end();
763 map<MapPoint*,Measurement> &mKFMeas = (*iter)->mMeasurements;
764 for(
meas_it jiter = mKFMeas.begin(); jiter!= mKFMeas.end(); jiter++)
765 sMapPoints.insert(jiter->first);
769 set<KeyFrame*> sFixedSet;
772 if(sAdjustSet.count(*it))
774 bool bInclude =
false;
775 for(
meas_it jiter = (*it)->mMeasurements.begin(); jiter!= (*it)->mMeasurements.end(); jiter++)
776 if(sMapPoints.count(jiter->first))
782 sFixedSet.insert(*it);
789 void MapMaker::BundleAdjust(set<KeyFrame*> sAdjustSet, set<KeyFrame*> sFixedSet, set<MapPoint*> sMapPoints,
bool bRecent)
797 map<MapPoint*, int> mPoint_BundleID;
798 map<int, MapPoint*> mBundleID_Point;
799 map<KeyFrame*, int> mView_BundleID;
800 map<int, KeyFrame*> mBundleID_View;
803 for(set<KeyFrame*>::iterator it = sAdjustSet.begin(); it!= sAdjustSet.end(); it++)
805 int nBundleID = b.
AddCamera((*it)->se3CfromW, (*it)->bFixed);
806 mView_BundleID[*it] = nBundleID;
807 mBundleID_View[nBundleID] = *it;
809 for(set<KeyFrame*>::iterator it = sFixedSet.begin(); it!= sFixedSet.end(); it++)
811 int nBundleID = b.
AddCamera((*it)->se3CfromW,
true);
812 mView_BundleID[*it] = nBundleID;
813 mBundleID_View[nBundleID] = *it;
817 for(set<MapPoint*>::iterator it = sMapPoints.begin(); it!=sMapPoints.end(); it++)
819 int nBundleID = b.
AddPoint((*it)->v3WorldPos);
820 mPoint_BundleID[*it] = nBundleID;
821 mBundleID_Point[nBundleID] = *it;
835 if(mPoint_BundleID.count(it->first) == 0)
837 int nPoint_BundleID = mPoint_BundleID[it->first];
850 cout <<
"!! MapMaker: Cholesky failure in bundle adjust. " << endl
851 <<
" The map is probably corrupt: Ditching the map. " << endl;
860 for(map<MapPoint*,int>::iterator itr = mPoint_BundleID.begin();
861 itr!=mPoint_BundleID.end();
863 itr->first->v3WorldPos = b.
GetPoint(itr->second);
865 for(map<KeyFrame*,int>::iterator itr = mView_BundleID.begin();
866 itr!=mView_BundleID.end();
868 itr->first->se3CfromW = b.
GetCamera(itr->second);
886 for(
unsigned int i=0; i<vOutliers_PC_pair.size(); i++)
888 MapPoint *pp = mBundleID_Point[vOutliers_PC_pair[i].first];
889 KeyFrame *pk = mBundleID_View[vOutliers_PC_pair[i].second];
941 if(v2Image[0] < 0 || v2Image[1] < 0 || v2Image[0] > irImageSize[0] || v2Image[1] > irImageSize[1])
994 vector<MapPoint*> vToFind;
999 for(
unsigned int i=0; i<vToFind.size(); i++)
1036 vector<pair<KeyFrame*, MapPoint*> >::iterator it;
1057 cout <<
" MapMaker: CalcPlane: too few points to calc plane." << endl;
1064 double dBestDistSquared = 9999999999999999.9;
1066 for(
int i=0; i<nRansacs; i++)
1068 int nA = rand()%nPoints;
1072 nB = rand()%nPoints;
1073 while(nC == nA || nC==nB)
1074 nC = rand()%nPoints;
1083 if(v3Normal * v3Normal == 0)
1087 double dSumError = 0.0;
1088 for(
unsigned int i=0; i<nPoints; i++)
1091 double dDistSq = v3Diff * v3Diff;
1094 double dNormDist = fabs(v3Diff * v3Normal);
1096 if(dNormDist > 0.05)
1098 dSumError += dNormDist;
1100 if(dSumError < dBestDistSquared)
1102 dBestDistSquared = dSumError;
1103 v3BestMean = v3Mean;
1104 v3BestNormal = v3Normal;
1109 vector<Vector<3> > vv3Inliers;
1110 for(
unsigned int i=0; i<nPoints; i++)
1113 double dDistSq = v3Diff * v3Diff;
1116 double dNormDist = fabs(v3Diff * v3BestNormal);
1117 if(dNormDist < 0.05)
1123 for(
unsigned int i=0; i<vv3Inliers.size(); i++)
1124 v3MeanOfInliers+=vv3Inliers[i];
1125 v3MeanOfInliers *= (1.0 / vv3Inliers.size());
1128 for(
unsigned int i=0; i<vv3Inliers.size(); i++)
1130 Vector<3> v3Diff = vv3Inliers[i] - v3MeanOfInliers;
1131 m3Cov += v3Diff.as_col() * v3Diff.as_row();
1143 m3Rot[2] = v3Normal;
1144 m3Rot[0] = m3Rot[0] - (v3Normal * (m3Rot[0] * v3Normal));
1146 m3Rot[1] = m3Rot[2] ^ m3Rot[0];
1150 Vector<3> v3RMean = se3Aligner * v3MeanOfInliers;
1161 double dSumDepth = 0.0;
1162 double dSumDepthSquared = 0.0;
1168 dSumDepth += v3PosK[2];
1169 dSumDepthSquared += v3PosK[2] * v3PosK[2];
1183 ((
MapMaker*) ptr)->mvQueuedCommands.push_back(c);
1188 if(sCommand==
"SaveMap")
1190 cout <<
" MapMaker: Saving the map.... " << endl;
1191 ofstream ofs(
"map.dump");
1202 ost1 <<
"keyframes/" << i <<
".jpg";
1206 ost2 <<
"keyframes/" << i <<
".info";
1208 ofs2.open(ost2.str().c_str());
1212 cout <<
" ... done saving map." << endl;
1216 cout <<
"! MapMaker::GUICommandHandler: unhandled command "<< sCommand << endl;
void normalize(Vector< Size, Precision, Base > v)
bool mbBundleConverged_Recent
const int CounterForConvergenceLimit
KeyFrame * ClosestKeyFrame(KeyFrame &k)
std::map< MapPoint *, Measurement >::iterator meas_it
bool Compute(std::vector< HomographyMatch > vMatches, double dMaxPixelError, SE3<> &se3SecondCameraPose)
int LevelScale(int nLevel)
bool IsDistanceToNearestKeyFrameExcessive(KeyFrame &kCurrent)
int AddPoint(Vector< 3 > v3Pos)
void GUICommandHandler(std::string sCommand, std::string sParams)
#define LOGV(LOG_TAG,...)
std::queue< MapPoint * > mqNewQueue
Vector< 3 > ReprojectPoint(SE3<> se3AfromB, const Vector< 2 > &v2A, const Vector< 2 > &v2B)
void AddSomeMapPoints(int nLevel)
int nMEstimatorOutlierCount
std::vector< KeyFrame * > NClosestKeyFrames(KeyFrame &k, unsigned int N)
int Compute(bool *pbAbortSignal)
bool FindPatchCoarse(CVD::ImageRef ir, KeyFrame &kf, unsigned int nRange)
std::vector< Command > mvQueuedCommands
SO3< Precision > & get_rotation()
Returns the rotation part of the transformation as a SO3.
static Operator< Internal::Identity< Internal::One > > Identity
int counterForConvergenceLimit
void resize(const ImageRef &size)
bool AddPointEpipolar(KeyFrame &kSrc, KeyFrame &kTarget, int nLevel, int nCandidate)
void SetSendMessageToUnity(SendMessage sm)
Vector< 2 > Project(const Vector< 2 > &camframe)
SendMessage sendStringToUnity
void MakeTemplateCoarseNoWarp(MapPoint &p)
Vector< 3 > v3OneDownFromCenter_NC
void stop()
Tell the thread to stop.
TooN::Vector< 2 > vec(const ImageRef &ir)
CVD::Image< CVD::byte > im
ImageRef ir_rounded(const TooN::Vector< 2 > &v)
void MakeSubPixTemplate()
void SetSubPixPos(Vector< 2 > v2)
Vector<(Size==Dynamic?Dynamic:Size+1), Precision > unproject(const Vector< Size, Precision, Base > &v)
void SendStringToUnity(const char *st)
Send string to Unity3D
bool NeedNewKeyFrame(KeyFrame &kCurrent)
const int MapMakerPlaneAlignerRansacs
bool ReFind_Common(KeyFrame &k, MapPoint &p)
void AddKeyFrame(KeyFrame &k)
double mdWiggleScaleDepthNormalized
enum Measurement::@4 Source
Vector< 2 > GetSubPixPos()
bool mbBundleRunningIsRecent
void start(Runnable *runnable=0)
Start execution of "run" method in separate thread.
Vector< 2 > UnProject(const Vector< 2 > &imframe)
Matrix< Size, Size, Precision > & get_evectors()
std::vector< MapPoint * > vpPoints
virtual void run()
Override this method to do whatever it is the thread should do.
int AddCamera(SE3<> se3CamFromWorld, bool bFixed)
Vector< 2 > GetCoarsePosAsVector()
bool bImplaneCornersCached
MapMaker(Map &m, const ATANCamera &cam)
const double MapMakerWiggleScale
void BundleAdjustRecent()
void ApplyGlobalTransformationToMap(SE3<> se3NewFromOld)
bool IterateSubPixToConvergence(KeyFrame &kf, int nMaxIts)
void ReFindFromFailureQueue()
bool next(const ImageRef &max)
std::vector< CVD::ImageRef > vCorners
void AddMeas(int nCam, int nPoint, Vector< 2 > v2Pos, double dSigmaSquared)
Matrix< 2 > m2PixelProjectionJac
std::vector< Vector< 2 > > vImplaneCorners
std::vector< KeyFrame * > vpKeyFrames
std::vector< std::pair< KeyFrame *, MapPoint * > > mvFailureQueue
bool InitFromStereo(KeyFrame &kFirst, KeyFrame &kSecond, std::vector< std::pair< CVD::ImageRef, CVD::ImageRef > > &vMatches, SE3<> &se3CameraPos)
std::set< KeyFrame * > sMeasurementKFs
int ReFindInSingleKeyFrame(KeyFrame &k)
ImageRef size() const
What is the size of this image?
bool mbBundleConverged_Full
bool mbBundleAbortRequested
Vector< 3 > v3OneRightFromCenter_NC
ImageRef ir(const TooN::Vector< 2 > &v)
int nMEstimatorInlierCount
double DistToNearestKeyFrame(KeyFrame &kCurrent)
void ThinCandidates(KeyFrame &k, int nLevel)
int ZMSSDAtPoint(CVD::BasicImage< CVD::byte > &im, const CVD::ImageRef &ir)
void RefreshSceneDepth(KeyFrame *pKF)
Vector< 3, Precision > & get_translation()
Returns the translation part of the transformation as a Vector.
std::map< MapPoint *, Measurement > mMeasurements
Vector< 1 > makeVector(double x1)
Matrix< Min_Dim, Cols, Precision, Reference::RowMajor > get_VT()
KeyFrame * pPatchSourceKF
void SetImageSize(Vector< 2 > v2ImageSize)
Vector< 2 > v2CamPlaneFirst
static void GUICommandCallBack(void *ptr, std::string sCommand, std::string sParams)
bool shouldStop() const
Returns true if the stop() method been called, false otherwise.
Vector< 3 > GetPoint(int n)
double LargestRadiusInImage()
std::vector< KeyFrame * > mvpKeyFrameQueue
void MakeTemplateCoarse(MapPoint &p, SE3<> se3CFromW, Matrix< 2 > &m2CamDerivs)
void AddKeyFrameFromTopOfQueue()
double LevelZeroPos(double dLevelPos, int nLevel)
void ApplyGlobalScaleToMap(double dScale)
void MoveBadPointsToTrash()
std::vector< Candidate > vCandidates
int counterForConvergence
Matrix< 2, 2 > GetProjectionDerivs()
void RefreshPixelVectors()
std::set< KeyFrame * > sNeverRetryKFs
double KeyFrameLinearDist(KeyFrame &k1, KeyFrame &k2)
static Operator< Internal::Zero > Zeros
Vector<(Size==Dynamic?Dynamic:Size-1), Precision > project(const Vector< Size, Precision, Base > &v)
void BundleAdjust(std::set< KeyFrame * >, std::set< KeyFrame * >, std::set< MapPoint * >, bool)
Vector< 2 > v2CamPlaneSecond
void join()
This blocks until the thread has actually terminated.
static void sleep(unsigned int milli)
Tell the current thread to sleep for milli milliseconds.
std::vector< std::pair< int, int > > GetOutlierMeasurements()
const double MapMakerMaxKFDistWiggleMult