-
Notifications
You must be signed in to change notification settings - Fork 3
/
gridslamprocessor_tree.cpp
279 lines (237 loc) · 7.1 KB
/
gridslamprocessor_tree.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
#include <string>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <fstream>
//#include <gsl/gsl_blas.h>
#include <utils/stat.h>
#include "gridslamprocessor.h"
namespace GMapping {
using namespace std;
GridSlamProcessor::TNode::TNode(const OrientedPoint& p, double w, TNode* n, unsigned int c){
pose=p;
weight=w;
childs=c;
parent=n;
reading=0;
gweight=0;
// 给父节点的子节点计数变量 ++
if (n){
n->childs++;
}
flag=0;
accWeight=0;
}
GridSlamProcessor::TNode::~TNode(){
if (parent && (--parent->childs)<=0)
delete parent;
assert(!childs);
}
//BEGIN State Save/Restore
GridSlamProcessor::TNodeVector GridSlamProcessor::getTrajectories() const{
TNodeVector v;
TNodeMultimap parentCache;
TNodeDeque border;
for (ParticleVector::const_iterator it=m_particles.begin(); it!=m_particles.end(); it++){
TNode* node=it->node;
while(node){
node->flag=false;
node=node->parent;
}
}
for (ParticleVector::const_iterator it=m_particles.begin(); it!=m_particles.end(); it++){
TNode* newnode=new TNode(* (it->node) );
v.push_back(newnode);
assert(newnode->childs==0);
if (newnode->parent){
parentCache.insert(make_pair(newnode->parent, newnode));
//cerr << __PRETTY_FUNCTION__ << ": node " << newnode->parent << " flag=" << newnode->parent->flag<< endl;
if (! newnode->parent->flag){
//cerr << __PRETTY_FUNCTION__ << ": node " << newnode->parent << " flag=" << newnode->parent->flag<< endl;
newnode->parent->flag=true;
border.push_back(newnode->parent);
}
}
}
//cerr << __PRETTY_FUNCTION__ << ": border.size(INITIAL)=" << border.size() << endl;
//cerr << __PRETTY_FUNCTION__ << ": parentCache.size()=" << parentCache.size() << endl;
while (! border.empty()){
//cerr << __PRETTY_FUNCTION__ << ": border.size(PREPROCESS)=" << border.size() << endl;
//cerr << __PRETTY_FUNCTION__ << ": parentCache.size(PREPROCESS)=" << parentCache.size() << endl;
const TNode* node=border.front();
//cerr << __PRETTY_FUNCTION__ << ": node " << node << endl;
border.pop_front();
if (! node)
continue;
TNode* newnode=new TNode(*node);
node->flag=false;
//update the parent of all of the referring childs
pair<TNodeMultimap::iterator, TNodeMultimap::iterator> p=parentCache.equal_range(node);
double childs=0;
for (TNodeMultimap::iterator it=p.first; it!=p.second; it++){
assert(it->second->parent==it->first);
(it->second)->parent=newnode;
//cerr << "PS(" << it->first << ", "<< it->second << ")";
childs++;
}
////cerr << endl;
parentCache.erase(p.first, p.second);
//cerr << __PRETTY_FUNCTION__ << ": parentCache.size(POSTERASE)=" << parentCache.size() << endl;
assert(childs==newnode->childs);
//unmark the node
if ( node->parent ){
parentCache.insert(make_pair(node->parent, newnode));
if(! node->parent->flag){
border.push_back(node->parent);
node->parent->flag=true;
}
}
//insert the parent in the cache
}
//cerr << __PRETTY_FUNCTION__ << " : checking cloned trajectories" << endl;
for (unsigned int i=0; i<v.size(); i++){
TNode* node= v[i];
while (node){
//cerr <<".";
node=node->parent;
}
//cerr << endl;
}
return v;
}
void GridSlamProcessor::integrateScanSequence(GridSlamProcessor::TNode* node){
//reverse the list
TNode* aux=node;
TNode* reversed=0;
double count=0;
while(aux!=0){
TNode * newnode=new TNode(*aux);
newnode->parent=reversed;
reversed=newnode;
aux=aux->parent;
count++;
}
//attach the path to each particle and compute the map;
if (m_infoStream )
m_infoStream << "Restoring State Nodes=" <<count << endl;
aux=reversed;
bool first=true;
double oldWeight=0;
OrientedPoint oldPose;
while (aux!=0){
if (first){
oldPose=aux->pose;
first=false;
oldWeight=aux->weight;
}
OrientedPoint dp=aux->pose-oldPose;
double dw=aux->weight-oldWeight;
oldPose=aux->pose;
double * plainReading = new double[m_beams];
for(unsigned int i=0; i<m_beams; i++)
plainReading[i]=(*(aux->reading))[i];
for (ParticleVector::iterator it=m_particles.begin(); it!=m_particles.end(); it++){
//compute the position relative to the path;
double s=sin(oldPose.theta-it->pose.theta),
c=cos(oldPose.theta-it->pose.theta);
it->pose.x+=c*dp.x-s*dp.y;
it->pose.y+=s*dp.x+c*dp.y;
it->pose.theta+=dp.theta;
it->pose.theta=atan2(sin(it->pose.theta), cos(it->pose.theta));
//register the scan
m_matcher.invalidateActiveArea();
m_matcher.computeActiveArea(it->map, it->pose, plainReading);
it->weight+=dw;
it->weightSum+=dw;
// this should not work, since it->weight is not the correct weight!
// it->node=new TNode(it->pose, it->weight, it->node);
it->node=new TNode(it->pose, 0.0, it->node);
//update the weight
}
delete [] plainReading;
aux=aux->parent;
}
//destroy the path
aux=reversed;
while (reversed){
aux=reversed;
reversed=reversed->parent;
delete aux;
}
}
//END State Save/Restore
//BEGIN
void GridSlamProcessor::updateTreeWeights(bool weightsAlreadyNormalized){
// 传入为 false
if (!weightsAlreadyNormalized) {
// 将位姿中的旋转归一化到 - pi ~ + pi
// 上一次看错了,该函数在 gridslamprocess.hxx 中实现的
//
normalize();
}
// 遍历粒子中的 nodes,计数、累计权重置 0
resetTree();
// 计算权重总和,但并没用到?
propagateWeights();
}
void GridSlamProcessor::resetTree(){
// don't calls this function directly, use updateTreeWeights(..) !
// 遍历粒子中的 nodes
for (ParticleVector::iterator it=m_particles.begin(); it!=m_particles.end(); it++){
TNode* n=it->node;
// 遍历 nodes,计数、累计权重置 0
while (n){
n->accWeight=0;
n->visitCounter=0;
n=n->parent;
}
}
}
double propagateWeight(GridSlamProcessor::TNode* n, double weight){
// 递归出口
if (!n)
return weight;
double w=0;
n->visitCounter++;
n->accWeight+=weight;
// 递归到第一个节点
if (n->visitCounter==n->childs){
w=propagateWeight(n->parent,n->accWeight);
}
assert(n->visitCounter<=n->childs);
return w;
}
double GridSlamProcessor::propagateWeights(){
// don't calls this function directly, use updateTreeWeights(..) !
// all nodes must be resetted to zero and weights normalized
// the accumulated weight of the root
double lastNodeWeight=0;
// sum of the weights in the leafs
double aw=0;
// 粒子对应的权重
std::vector<double>::iterator w=m_weights.begin();
// 遍历粒子集
for (ParticleVector::iterator it=m_particles.begin(); it!=m_particles.end(); it++){
// 粒子权重
double weight=*w;
// 所有粒子权重总和
aw+=weight;
// 当前粒子的叶子节点,即最新状态
TNode * n=it->node;
// 记录当前的权重
n->accWeight=weight;
// 记录所有粒子根节点的权重总和
lastNodeWeight+=propagateWeight(n->parent,n->accWeight);
w++;
}
if (fabs(aw-1.0) > 0.0001 || fabs(lastNodeWeight-1.0) > 0.0001) {
cerr << "ERROR: ";
cerr << "root->accWeight=" << lastNodeWeight << " sum_leaf_weights=" << aw << endl;
assert(0);
}
// 返回根节点权重总和
return lastNodeWeight;
}
};
//END