26 std::vector<ClusterPtr> &clustersLeft,
const std::vector<ClusterPtr> &allClusters,
29 build(pattern, clusterId, reference, clustersLeft, allClusters, ism, naive);
36 unsigned clusterId = 0;
37 std::stringstream subPatternNameStream;
38 subPatternNameStream<<pattern;
39 std::string subPatternName = subPatternNameStream.str();
41 std::vector<ClusterPtr> copy = clusters;
42 std::vector<ClusterPtr> allClusters = clusters;
49 build(subPatternName, clusterId, (*clusters.begin())->
getReference(), copy, allClusters,
ism, naive);
52 for (std::map<std::string, std::vector<VoteSpecifierPtr> >::iterator it = ism->voteSpecifiersPerObject.begin();
53 it != ism->voteSpecifiersPerObject.end();
56 if (it->first.find(
"_sub") != std::string::npos)
58 for (
unsigned int i = 0; i < it->second.size(); ++i)
60 it->second[i]->observedId =
"";
72 std::vector<ClusterPtr>& clustersLeft,
const std::vector<ClusterPtr>& allClusters,
80 std::vector<ClusterPtr>::iterator clusterIt;
81 for (clusterIt = clustersLeft.begin(); clusterIt != clustersLeft.end(); ++clusterIt)
83 if (reference->type == (*clusterIt)->getReference()->type &&
84 reference->observedId == (*clusterIt)->getReference()->observedId)
87 clustersLeft.erase(clusterIt);
93 std::stringstream subPatternNameStream;
94 if (pattern.find(
"_sub") != std::string::npos)
96 subPatternNameStream<<pattern<<clusterId;
97 pattern = subPatternNameStream.str();
103 subPatternNameStream<<
"_sub";
104 pattern = subPatternNameStream.str();
112 subPatternNameStream<<pattern<<
"_sub";
113 pattern = subPatternNameStream.str();
115 for (
TrackPtr& child : cluster->getChildren()->tracks)
121 for (
const ClusterPtr& clusterChecker : allClusters)
123 if (clusterChecker == cluster)
128 for (
TrackPtr& childChecker : clusterChecker->getChildren()->tracks)
130 if (childChecker->type == child->type && childChecker->observedId == child->observedId)
141 subTree =
TreePtr(
new Tree(pattern, clusterId, child, clustersLeft, allClusters, ism, naive));
145 std::vector<ClusterPtr> dummyClusters;
148 subTree =
TreePtr(
new Tree(pattern, clusterId, child, dummyClusters, allClusters, ism, naive));
150 std::vector<VoteSpecifierPtr> votes =
151 cluster->getVotersByVotersTypeAndObservedId(child->type, child->observedId);
154 if (subTree->getHeight() + 1 > this->
getHeight())
156 this->
setHeight(subTree->getHeight() + 1);
161 vote->observedId = child->observedId;
162 vote->objectType = subTree->getReference()->type;
163 ism->voteSpecifiersPerObject[subTree->getReference()->type].push_back(vote);
165 if (subTree->getWeight() > 1)
172 ism->patternDefinitions.insert(std::make_pair(newPattern->name, newPattern));
177 +
" and height is " + std::to_string(this->
getHeight())
186 std::vector<ClusterPtr> clusters;
187 std::vector<std::pair<TrackPtr, ClusterPtr> > referencesWithParent;
189 std::pair<std::map<TrackPtr, int>, std::vector<std::pair<int, TrackPtr> > > objectOccurenceInfo =
getMostCommonObjects(topology);
190 std::map<TrackPtr, int> objectOccurences = objectOccurenceInfo.first;
191 std::vector<std::pair<int, TrackPtr> > mostCommonObjects = objectOccurenceInfo.second;
193 referencesWithParent.push_back(std::make_pair(mostCommonObjects.begin()->second, sentinel));
196 for (
size_t tracksIt = 0; tracksIt < referencesWithParent.size(); ++tracksIt)
198 for (
const std::pair<TrackPtr, int>& it : objectOccurences)
200 assert(it.second >= 0); (void)it;
202 ClusterPtr currentCluster(
new Cluster(std::make_pair(
TrackPtr(
new Track(*referencesWithParent[tracksIt].first)), referencesWithParent[tracksIt].second), topology, mostCommonObjects, objectOccurences));
204 std::ostringstream s;
206 s <<
"Current cluster contains " << currentCluster->getChildren()->tracks.size() <<
" objects: "<< std::endl;
207 for (
TrackPtr trackIt : currentCluster->getChildren()->tracks)
209 s << trackIt->type <<
" with ID " << trackIt->observedId <<
", ";
211 s << std::endl <<
"Reference is : " << currentCluster->getReference()->type << std::endl;
214 if (currentCluster->getChildren()->tracks.size() > 0)
216 clusters.push_back(currentCluster);
219 for (
const std::pair<TrackPtr, int>& objectIt : objectOccurences)
221 if (objectIt.second > 0)
223 std::pair<ISM::TrackPtr, ClusterPtr> subReference =
getReference(mostCommonObjects, clusters, topology);
224 assert (subReference.first != 0);
225 referencesWithParent.push_back(subReference);
230 for (
const std::pair<TrackPtr, int>& it : objectOccurences)
232 assert(it.second == 0); (void)it;
241 std::pair<std::map<ISM::TrackPtr, int> , std::vector<std::pair<int, ISM::TrackPtr> > >
244 std::map<ISM::TrackPtr, int> objectOccurences;
245 std::vector<std::pair<int, ISM::TrackPtr > > mostCommon;
246 for (
const std::pair<unsigned int, ObjectRelationPtr> relationIt : topology)
248 TrackPtr objectA = relationIt.second->getTrackA();
249 TrackPtr objectB = relationIt.second->getTrackB();
250 if (objectOccurences[objectA])
252 ++objectOccurences[objectA];
256 objectOccurences[objectA] = 1;
258 if (objectOccurences[objectB])
260 ++objectOccurences[objectB];
264 objectOccurences[objectB] = 1;
268 for (
const std::pair<TrackPtr, int>& it : objectOccurences)
270 mostCommon.push_back(std::make_pair(it.second, it.first));
275 bool operator()(
const std::pair<int, ISM::TrackPtr > &firstElem,
const std::pair<int, ISM::TrackPtr > &secondElem) {
276 if (firstElem.first == secondElem.first)
278 std::string objectIdentifierFirstElem = firstElem.second->type + firstElem.second->observedId;
279 std::string objectIdentifierSecondElem = secondElem.second->type + secondElem.second->observedId;
280 return objectIdentifierFirstElem.compare(objectIdentifierSecondElem) < 0;
283 return firstElem.first > secondElem.first;
287 std::sort(mostCommon.begin(), mostCommon.end(), compare());
288 std::pair<std::map<TrackPtr, int>, std::vector<std::pair<int, ISM::TrackPtr > > > ret = std::make_pair(objectOccurences, mostCommon);
292 std::pair<TrackPtr, ClusterPtr>
Tree::getReference(std::vector<std::pair<int, TrackPtr> >& objectTracks,
293 const std::vector<ClusterPtr>& currentClusters,
const ObjectRelations& topology)
const 295 std::vector<TrackPtr> orderedObjectsInRelations;
296 for (std::pair<int, TrackPtr>& tracksIt : objectTracks)
298 for (
const std::pair<unsigned int, ObjectRelationPtr>& relationIt : topology)
300 if (relationIt.second->containsObject(tracksIt.second->type, tracksIt.second->observedId))
302 orderedObjectsInRelations.push_back(tracksIt.second);
309 for (
TrackPtr& tracksIt : orderedObjectsInRelations)
311 if (recommendedParent != 0)
315 for (
const ClusterPtr& clusterIt : currentClusters)
317 if (recommendedParent != 0)
320 LogHelper::logMessage(std::to_string(recommendedParent->getRecommendedLevel()) +
" vs " + std::to_string(clusterIt->getRecommendedLevel()),
LOG_DEBUG);
322 if (recommendedParent != 0 && recommendedParent->getRecommendedLevel() < clusterIt->getRecommendedLevel())
326 for (
TrackPtr& childIt : clusterIt->getChildren()->tracks)
328 if ((childIt->observedId == tracksIt->observedId) && (childIt->type == tracksIt->type))
330 assert (tracksIt != 0);
331 recommendedParent = clusterIt;
332 reference = tracksIt;
338 return std::make_pair(reference, recommendedParent);
343 return one->getRecommendedLevel() < two->getRecommendedLevel();
347 const std::pair<unsigned, T>& secondElem)
349 return firstElem.first > secondElem.first;
377 void Tree::addVotes(
const std::string& voterType,
const std::vector<VoteSpecifierPtr>& votes)
379 std::vector<VoteSpecifierPtr> copy = votes;
std::vector< boost::shared_ptr< Tree > > children
void build(std::string pattern, unsigned clusterId, const TrackPtr reference, std::vector< ClusterPtr > &clustersLeft, const std::vector< ClusterPtr > &allClusters, IsmPtr &ism, bool naive)
void addVotes(const std::string &voterType, const std::vector< VoteSpecifierPtr > &votes)
void addChild(const boost::shared_ptr< Tree > &child)
static bool pairCompare(const std::pair< unsigned, T > &firstElem, const std::pair< unsigned, T > &secondElem)
static void logLine(LogLevel logLevel=LOG_INFO)
std::vector< VoteSpecifierPtr > getVotesByType(const std::string &type)
boost::shared_ptr< Cluster > ClusterPtr
const char * LOG_COLOR_CLUSTER
static bool compareClusters(const ClusterPtr &one, const ClusterPtr &two)
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
std::map< unsigned int, ISM::ObjectRelationPtr, std::less< unsigned > > ObjectRelations
boost::shared_ptr< ImplicitShapeModel > IsmPtr
boost::shared_ptr< Pattern > PatternPtr
boost::shared_ptr< VoteSpecifier > VoteSpecifierPtr
boost::shared_ptr< Tree > TreePtr
const char * LOG_COLOR_TREE
void setHeight(unsigned height)
std::map< std::string, std::vector< VoteSpecifierPtr > > votesByVotersType
void setWeight(unsigned weight)
boost::shared_ptr< Track > TrackPtr
std::pair< std::map< ISM::TrackPtr, int >, std::vector< std::pair< int, ISM::TrackPtr > > > getMostCommonObjects(const ISM::ObjectRelations &topology) const
this namespace contains all generally usable classes.
std::vector< ClusterPtr > buildClusters(ISM::ObjectRelations) const