15 #ifndef __SemanticClustering_H 16 #define __SemanticClustering_H 18 #include <mrpt/config.h> 54 std::map<unsigned, std::vector<unsigned> >
groups;
56 std::map<unsigned, std::vector<unsigned> >
vicinity;
68 planesVicinity_order.clear();
71 neighborGroups = vicinity[currentSemanticGroup];
72 neighborGroups.push_back(currentSemanticGroup);
74 for(
unsigned i=0; i < neighborGroups.size(); i++)
77 neigSize += groups[neighborGroups[i] ].size();
78 planesVicinity_order.insert(planesVicinity_order.end(), groups[neighborGroups[i] ].begin(), groups[neighborGroups[i] ].end());
83 assert(neigSize <= mPbMap.
vPlanes.size());
84 connectivity_matrix.resize(neigSize,neigSize);
85 connectivity_matrix.zeros();
86 for(
unsigned i=0; i < planesVicinity_order.size(); i++)
88 unsigned plane_i = planesVicinity_order[i];
89 for(
unsigned j=i+1; j < planesVicinity_order.size(); j++)
91 unsigned plane_j = planesVicinity_order[j];
92 if(mPbMap.
vPlanes[plane_i].nearbyPlanes.count(plane_j))
93 connectivity_matrix(i,j) = connectivity_matrix(j,i) = 1;
111 cout <<
"connectivity_matrix matrix\n" << connectivity_matrix << endl;
121 planesVicinity_order.clear();
124 neighborGroups = vicinity[currentSemanticGroup];
125 neighborGroups.push_back(currentSemanticGroup);
126 cout <<
"neighborGroups: ";
127 for(
unsigned i=0; i < neighborGroups.size(); i++)
129 cout << neighborGroups[i] <<
" ";
130 neigSize += groups[neighborGroups[i] ].size();
131 planesVicinity_order.insert(planesVicinity_order.end(), groups[neighborGroups[i] ].begin(), groups[neighborGroups[i] ].end());
136 assert(neigSize <= mPbMap.
vPlanes.size());
137 connectivity_matrix.resize(neigSize,neigSize);
138 connectivity_matrix.zeros();
139 for(
unsigned i=0; i < planesVicinity_order.size(); i++)
141 unsigned plane_i = planesVicinity_order[i];
142 for(
unsigned j=i+1; j < planesVicinity_order.size(); j++)
144 unsigned plane_j = planesVicinity_order[j];
145 if(mPbMap.
vPlanes[plane_i].neighborPlanes.count(plane_j))
148 float sso = (float)mPbMap.
vPlanes[plane_i].neighborPlanes[plane_j] /
std::min(mPbMap.
vPlanes[plane_i].numObservations, mPbMap.
vPlanes[plane_j].numObservations);
149 connectivity_matrix(i,j) = connectivity_matrix(j,i) = sso;
154 cout <<
"Planes in the vicinity: ";
155 for(
unsigned i=0; i < planesVicinity_order.size(); i++)
156 cout << planesVicinity_order[i] <<
" ";
168 cout <<
"connectivity_matrix matrix\n" << connectivity_matrix << endl;
178 int group_diff = parts.size() - neighborGroups.size();
179 std::map<unsigned, std::vector<unsigned> > newGroups;
183 for(
unsigned i=0; i < neighborGroups.size(); i++)
185 for(
int i=0; i < group_diff; i++)
189 else if(group_diff < 0)
191 for(
unsigned i=0; i < parts.size(); i++)
196 for(
unsigned i=0; i < neighborGroups.size(); i++)
200 std::vector<unsigned> group_limits(1,0);
205 for(
unsigned i=0; i < neighborGroups.size(); i++)
206 group_limits.push_back(group_limits.back() + groups[neighborGroups[i]].size());
213 for(
unsigned i=0; i < parts.size(); i++)
215 for(
unsigned j=0; j < parts[i].size(); j++)
217 if(i < neighborGroups.size())
219 if(parts[i][j] < group_limits[i] || parts[i][j] >= group_limits[i+1])
221 for(
unsigned k=0; k < neighborGroups.size() && i != k; k++)
222 if(parts[i][j] >= group_limits[k] || parts[i][j] < group_limits[k+1])
224 assert(groups[neighborGroups[k]][parts[i][j]-group_limits[k]] == planesVicinity_order[parts[i][j]]);
226 newGroups[neighborGroups[k]].push_back(mPbMap.
vPlanes[groups[neighborGroups[k]][parts[i][j]-group_limits[k]]].id);
227 mPbMap.
vPlanes[groups[neighborGroups[k]][parts[i][j]-group_limits[k]]].semanticGroup = neighborGroups[k];
233 newGroups[neighborGroups[i]].push_back(mPbMap.
vPlanes[groups[neighborGroups[i]][parts[i][j]-group_limits[i]]].id);
239 newGroups[groups.size()+i-neighborGroups.size()].push_back(planesVicinity_order[parts[i][j]]);
240 mPbMap.
vPlanes[planesVicinity_order[parts[i][j]]].semanticGroup = groups.size()+i-neighborGroups.size();
247 for(map<
unsigned,vector<unsigned> >::
iterator it=newGroups.begin(); it != newGroups.end(); it++)
248 groups[it->first] = it->second;
252 int sizeVicinity = neighborGroups.size();
253 for(
int i=-1; i >= group_diff; i--)
255 if(neighborGroups[sizeVicinity+i] == groups.size()-1)
256 groups.erase(neighborGroups[sizeVicinity+i]);
259 for(
unsigned j=0; j < mPbMap.
vPlanes.size(); j++)
260 if(mPbMap.
vPlanes[j].semanticGroup > neighborGroups[sizeVicinity+i])
261 mPbMap.
vPlanes[j].semanticGroup--;
262 for(
unsigned j=neighborGroups[sizeVicinity+i]; j < groups.size()-1; j++)
263 groups[j] = groups[j+1];
264 groups.erase(groups.size()-1);
281 for(std::map<
unsigned, std::vector<unsigned> >::
iterator it1=newGroups.begin(); it1 != newGroups.end(); it1++)
284 for(std::map<
unsigned, std::vector<unsigned> >::
iterator it1=newGroups.begin(); it1 != newGroups.end(); it1++)
286 std::map<unsigned, std::vector<unsigned> >
::iterator it2 = it1;
287 for( it2++; it2 != newGroups.end(); it2++)
288 for(
unsigned i=0; i < it1->second.size(); i++)
291 for(
unsigned j=0; j < it2->second.size(); j++)
293 if(mPbMap.
vPlanes[it1->second[i]].neighborPlanes.count(mPbMap.
vPlanes[it2->second[j]].id))
295 vicinity[it1->first].push_back(it2->first);
296 vicinity[it2->first].push_back(it1->first);
316 currentSemanticGroup(0),
333 unsigned current_group_votes = 0;
334 map<unsigned, unsigned> observed_group;
336 if(observed_group.count(mPbMap.
vPlanes[*it].semanticGroup))
337 observed_group[mPbMap.
vPlanes[*it].semanticGroup ]++;
339 observed_group[mPbMap.
vPlanes[*it].semanticGroup ] = 1;
341 if(it->second > current_group_votes)
343 currentSemanticGroup = it->first;
344 current_group_votes = it->second;
349 buildProximityMatrix();
350 std::vector<mrpt::vector_uint> parts;
355 if(neighborGroups.size() == 1 && parts.size() == 1)
359 bool different_partition =
false;
360 if(neighborGroups.size() != parts.size())
361 different_partition =
true;
364 unsigned prev_size = 0;
365 for(
unsigned i=0; i < neighborGroups.size(); i++)
367 if(groups[neighborGroups[i] ].
size() != parts[i].size())
369 different_partition =
true;
373 for(
unsigned j=0; j < parts[i].size(); j++)
375 if(parts[i][j] != j+prev_size)
377 different_partition =
true;
381 prev_size += parts[i].size();
385 if(!different_partition)
388 arrangeNewGroups(parts);
391 current_group_votes = 0;
392 observed_group.clear();
394 if(observed_group.count(mPbMap.
vPlanes[*it].semanticGroup))
395 observed_group[mPbMap.
vPlanes[*it].semanticGroup ]++;
397 observed_group[mPbMap.
vPlanes[*it].semanticGroup ] = 1;
399 if(it->second > current_group_votes)
401 currentSemanticGroup = it->first;
402 current_group_votes = it->second;
static void RecursiveSpectralPartition(GRAPH_MATRIX &in_A, std::vector< vector_uint > &out_parts, num_t threshold_Ncut=1, bool forceSimetry=true, bool useSpectralBisection=true, bool recursive=true, unsigned minSizeClusters=1, const bool verbose=false)
Performs the spectral recursive partition into K-parts for a given graph.
std::map< unsigned, std::vector< unsigned > > vicinity
SemanticClustering(PbMap &mPbM)
mrpt::math::CMatrix connectivity_matrix
std::vector< unsigned > planesVicinity_order
std::map< unsigned, std::vector< unsigned > > groups
std::vector< unsigned > neighborGroups
static std::vector< unsigned > DEFAULT_VECTOR_U
void buildCoVisibilityMatrix()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void arrangeNewGroups(std::vector< mrpt::vector_uint > &parts)
int evalPartition(std::set< unsigned > &observedPlanes)
std::vector< Plane > vPlanes
This class is a "CSerializable" wrapper for "CMatrixFloat".
unsigned currentSemanticGroup
A class used to store a Plane-based Map (PbMap).
void buildProximityMatrix()