-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMapPoint.cpp
293 lines (225 loc) · 6.21 KB
/
MapPoint.cpp
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
//
// Created by buyi on 17-11-4.
//
#include "MapPoint.h"
namespace DSDTM
{
unsigned long MapPoint::mlNextId = 0;
MapPoint::MapPoint()
{
mbOutlier = false;
mdStaticWeight = 1.0;
}
MapPoint::MapPoint(Eigen::Vector3d &_pose, KeyFrame *_frame, Map *tMap):
mPose(_pose), mFirstFrameId(_frame->mlId), mnFound(0),
mlLocalBAKFId(0), mMap(tMap), mObsNum(0), mRefKframe(_frame),
mnVisible(0)
{
{
std::unique_lock<std::mutex> lock(mMap->mMutexMPCreation);
mlID = mlNextId++;
}
mbOutlier = false;
mdStaticWeight = 1.0;
}
void MapPoint::Set_Pose(Eigen::Vector3d tPose)
{
std::unique_lock<std::mutex> lock(mMutexPos);
mPose = tPose;
}
Eigen::Vector3d MapPoint::Get_Pose() const
{
std::unique_lock<std::mutex> lock(mMutexPos);
return mPose;
}
void MapPoint::Add_Observation(KeyFrame *tKFrame, size_t tFID)
{
std::unique_lock<std::mutex> lock(mMutexObs);
if(mObservations.count(tKFrame))
return;
else
mObservations[tKFrame] = tFID;
mObsNum++;
}
void MapPoint::Erase_Observation(KeyFrame *tKFrame)
{
bool bBad = false;
{
std::unique_lock<std::mutex> lock(mMutexObs);
if (mObservations.count(tKFrame))
{
mObsNum--;
mObservations.erase(tKFrame);
if (tKFrame == mRefKframe) {
mRefKframe = mObservations.begin()->first;
mFirstFrameId = mRefKframe->mlId;
}
if (mObsNum <= 1)
bBad = true;
}
}
if(bBad)
SetBadFlag();
}
std::map<KeyFrame*, size_t> MapPoint::Get_Observations()
{
std::unique_lock<std::mutex> lock(mMutexObs);
return mObservations;
}
void MapPoint::SetBadFlag()
{
std::unique_lock<std::mutex> lock(mMutexObs);
std::unique_lock<std::mutex> lock2(mMutexPos);
mbOutlier = true;
std::map<KeyFrame*, size_t> obs = mObservations;
mObservations.clear();
for (auto &iter : obs)
{
KeyFrame *tKf = iter.first;
tKf->Erase_MapPointMatch(iter.second);
}
LOG(INFO)<< "MapPoint " << this->mlID << " deleted" <<std::endl;
mMap->EraseMapPoint(this);
}
bool MapPoint::IsBad() const
{
std::unique_lock<std::mutex> lock(mMutexObs);
std::unique_lock<std::mutex> lock2(mMutexPos);
return mbOutlier;
}
int MapPoint::Get_ObserveNums() const
{
std::unique_lock<std::mutex> lock(mMutexObs);
return mObsNum;
}
int MapPoint::Get_FoundNums() const
{
std::unique_lock<std::mutex> lock(mMutexFounds);
return mnFound;
}
bool MapPoint::Get_ClosetObs(const Frame *tFrame, Feature *&tFeature, KeyFrame *&tKframe) const
{
Eigen::Vector3d tPt_frame, tPose;
std::map<KeyFrame*, size_t> tObservations;
{
std::unique_lock<std::mutex> lock2(mMutexObs);
std::unique_lock<std::mutex> lock(mMutexPos);
tPose = mPose;
tObservations = mObservations;
}
tPt_frame = tFrame->Get_CameraCnt() - tPose;
tPt_frame.normalize();
double tMin_angle = 0;
auto min_it = tObservations.begin();
for (auto iter = tObservations.begin(); iter != tObservations.end(); iter++)
{
Eigen::Vector3d tPt_refer = iter->first->Get_CameraCnt() - tPose;
tPt_refer.normalize();
double tCos_angle = tPt_refer.dot(tPt_frame);
if(tCos_angle > tMin_angle)
{
tMin_angle = tCos_angle;
min_it = iter;
}
}
{
std::unique_lock<std::mutex> lock2(mMutexObs);
std::unique_lock<std::mutex> lock(mMutexFounds);
tFeature = min_it->first->mvFeatures[min_it->second];
tKframe = min_it->first;
}
if(tMin_angle < 0.5)
return false;
return true;
}
void MapPoint::IncreaseFound(int n)
{
std::unique_lock<std::mutex> lock(mMutexFounds);
mnFound = mnFound + n;
}
void MapPoint::EraseFound(int n)
{
//std::unique_lock<std::mutex> lock(mMutexFounds);
bool tbBad = false;
{
std::unique_lock<std::mutex> lock(mMutexObs);
std::unique_lock<std::mutex> lock2(mMutexFounds);
mnFound = mnFound - n;
if (mnFound <= 0)
tbBad = true;
}
if(tbBad)
SetBadFlag();
}
void MapPoint::IncreaseVisible(int n)
{
std::unique_lock<std::mutex> lock(mMutexObs);
mnVisible += n;
}
int MapPoint::Get_IndexInKeyFrame(KeyFrame *tKf)
{
std::unique_lock<std::mutex> lock(mMutexObs);
std::unique_lock<std::mutex> lock2(mMutexPos);
if(mObservations.count(tKf))
return mObservations[tKf];
else
return -1;
}
void MapPoint::UpdateNormalAndDepth()
{
std::map<KeyFrame*, size_t> tObservations;
Eigen::Vector3d tPose;
KeyFrame *tRefKframe;
{
std::unique_lock<std::mutex> lock(mMutexObs);
std::unique_lock<std::mutex> lock2(mMutexPos);
tObservations = mObservations;
tPose = mPose;
tRefKframe = mRefKframe;
}
if(tObservations.empty())
return;
Eigen::Vector3d normal = Eigen::MatrixXd::Zero(3,1);
int n = 0;
for(const auto &it : tObservations)
{
Eigen::Vector3d tVec_C2P = tPose - it.first->Get_CameraCnt();
tVec_C2P.normalize();
normal = normal + tVec_C2P;
n++;
}
Eigen::Vector3d tVec_P2Ref = tPose - tRefKframe->Get_CameraCnt();
const float dist = tVec_P2Ref.norm();
const int level = tObservations[tRefKframe];
const float tLevelFactor = 1<<level;
const int tLevels = 4;
const float tMinLevelFactor = 1.0/(1<<tLevels);
{
std::unique_lock<std::mutex> lock2(mMutexPos);
mfMaxDistance = dist*tLevelFactor;
mfMinDistance = mfMaxDistance*tMinLevelFactor;
mNoramlVector = normal/n;
}
}
float MapPoint::Get_MaxObserveDistance()
{
std::unique_lock<std::mutex> lock2(mMutexPos);
return mfMaxDistance*1.2;
}
float MapPoint::Get_MinObserveDistance()
{
std::unique_lock<std::mutex> lock2(mMutexPos);
return mfMinDistance*0.8;
}
Eigen::Vector3d MapPoint::Get_NormalVector()
{
std::unique_lock<std::mutex> lock2(mMutexPos);
return mNoramlVector;
}
float MapPoint::Get_FoundRatio()
{
std::unique_lock<std::mutex> lock(mMutexObs);
std::unique_lock<std::mutex> lock2(mMutexFounds);
return (1.0*mnFound/mnVisible);
}
} // namespace DSDTM