<commit_before>/*
 the tracker is used for tracking the identities of a collection of objects that
 change slightly over time. example applications are in contour tracking and
 face tracking. when using a tracker, the two most important things to know are
 the maximumAge and maximumDistance. maximumAge determines how many frames an
 object can last without being seen until the tracker forgets about it.
 maximumDistance determines how far an object can move until the tracker
 considers it a new object.
 
 the default trackers are for cv::Rect and cv::Point2f (RectTracker and
 PointTracker). to create a new kind of tracker, you need to add a
 trackingDistance() function that returns the distance between two tracked
 objects.
 
 the tracking algorithm calls the distance function approximately n^2 times.
 it then filters the distances using maximumDistance, which can significantly
 reduce the possible matches. then it sorts the distance using std::sort, which
 runs in nlogn time. the primary bottleneck for most data is the distance
 function. in practical terms, the tracker can become non-realtime when
 tracking more than a few hundred objects. to optimize the tracker, consider
 avoiding usage of sqrt() in the trackingDistance() function.
 
 this tracker doesn't find a global minimum, but a local minimum. for example,
 when a dense set of points moves farther than the average point-to-point radius
 (like a line of points 5 pixels apart moving up and to the right 5 pixels). it
 also fails to model the data, so two objects might be swapped if they cross
 paths quickly.
 */

#pragma once

#include "opencv2/opencv.hpp"

namespace ofxCv {
	
	using namespace cv;
	
	template <class T>
	class TrackedObject {
	protected:
		unsigned int age, label;
		int index;
	public:
		T object;
		
		TrackedObject(const T& object, unsigned int label, int index)
		:object(object)
		,label(label)
		,index(index)
		,age(0) {
		}
		TrackedObject(const T& object, const TrackedObject<T>& previous, int index)
		:object(object)
		,label(previous.label)
		,index(index)
		,age(0) {
		}
		TrackedObject(const TrackedObject<T>& old)
		:object(old.object)
		,label(old.label)
		,index(-1)
		,age(old.age) {
		}
		void timeStep() {
			age++;
		}
		unsigned int getAge() const {
			return age;
		}
		unsigned int getLabel() const {
			return label;
		}
		int getIndex() const {
			return index;
		}
	};
	
	struct bySecond {
		template <class First, class Second>
		bool operator()(pair<First, Second> const &a, pair<First, Second> const &b) { 
			return a.second < b.second;
		}
	};
	
	template <class T>
	class Tracker {
	protected:		
		vector<TrackedObject<T> > previous, current;
		vector<unsigned int> currentLabels, previousLabels, newLabels, deadLabels;
		map<unsigned int, T*> previousLabelMap, currentLabelMap;
		
		float maximumDistance;
		unsigned int maximumAge;
		unsigned int curLabel;
		unsigned int getNewLabel() {
			return curLabel++;
		}
		
	public:
		Tracker<T>()
		:curLabel(0)
		,maximumAge(4)
		,maximumDistance(32) {
		}
		void setMaximumAge(unsigned int maximumAge);
		void setMaximumDistance(float maximumDistance);
		vector<unsigned int>& track(const vector<T>& objects);
		
		// organized in the order received by track()
		vector<unsigned int>& getCurrentLabels();
		vector<unsigned int>& getPreviousLabels();
		vector<unsigned int>& getNewLabels();
		vector<unsigned int>& getDeadLabels();
		unsigned int getLabelFromIndex(unsigned int i) const;
		
		// organized by label
		int getIndexFromLabel(unsigned int label) const;
		T& getPrevious(unsigned int label) const;
		T& getCurrent(unsigned int label) const;
		bool existsCurrent(unsigned int label) const;
		bool existsPrevious(unsigned int label) const;
	};
	
	template <class T>
	void Tracker<T>::setMaximumAge(unsigned int maximumAge) {
		this->maximumAge = maximumAge;
	}
	
	template <class T>
	void Tracker<T>::setMaximumDistance(float maximumDistance) {
		this->maximumDistance = maximumDistance;
	}
	
	template <class T>
	vector<unsigned int>& Tracker<T>::track(const vector<T>& objects) {
		previous = current;
		int n = objects.size();
		int m = previous.size();
		
		// build NxM distance matrix
		typedef pair<int, int> MatchPair;
		typedef pair<MatchPair, float> MatchDistancePair;
		vector<MatchDistancePair> all;
		for(int i = 0; i < n; i++) {<commit_msg>fixed compilation issue with Tracker referring to std::pair and std::map<commit_after>/*
 the tracker is used for tracking the identities of a collection of objects that
 change slightly over time. example applications are in contour tracking and
 face tracking. when using a tracker, the two most important things to know are
 the maximumAge and maximumDistance. maximumAge determines how many frames an
 object can last without being seen until the tracker forgets about it.
 maximumDistance determines how far an object can move until the tracker
 considers it a new object.
 
 the default trackers are for cv::Rect and cv::Point2f (RectTracker and
 PointTracker). to create a new kind of tracker, you need to add a
 trackingDistance() function that returns the distance between two tracked
 objects.
 
 the tracking algorithm calls the distance function approximately n^2 times.
 it then filters the distances using maximumDistance, which can significantly
 reduce the possible matches. then it sorts the distance using std::sort, which
 runs in nlogn time. the primary bottleneck for most data is the distance
 function. in practical terms, the tracker can become non-realtime when
 tracking more than a few hundred objects. to optimize the tracker, consider
 avoiding usage of sqrt() in the trackingDistance() function.
 
 this tracker doesn't find a global minimum, but a local minimum. for example:
 when a dense set of points moves farther than the average point-to-point radius
 (like a line of points 5 pixels apart moving up and to the right 5 pixels). it
 also fails to model the data, so two objects might be swapped if they cross
 paths quickly.
 */

#pragma once

#include "opencv2/opencv.hpp"
#include <utility>
#include <map>

namespace ofxCv {
	
	using namespace cv;
	
	template <class T>
	class TrackedObject {
	protected:
		unsigned int age, label;
		int index;
	public:
		T object;
		
		TrackedObject(const T& object, unsigned int label, int index)
		:object(object)
		,label(label)
		,index(index)
		,age(0) {
		}
		TrackedObject(const T& object, const TrackedObject<T>& previous, int index)
		:object(object)
		,label(previous.label)
		,index(index)
		,age(0) {
		}
		TrackedObject(const TrackedObject<T>& old)
		:object(old.object)
		,label(old.label)
		,index(-1)
		,age(old.age) {
		}
		void timeStep() {
			age++;
		}
		unsigned int getAge() const {
			return age;
		}
		unsigned int getLabel() const {
			return label;
		}
		int getIndex() const {
			return index;
		}
	};
	
	struct bySecond {
		template <class First, class Second>
		bool operator()(std::pair<First, Second> const &a, std::pair<First, Second> const &b) { 
			return a.second < b.second;
		}
	};
	
	template <class T>
	class Tracker {
	protected:		
		vector<TrackedObject<T> > previous, current;
		vector<unsigned int> currentLabels, previousLabels, newLabels, deadLabels;
		std::map<unsigned int, T*> previousLabelMap, currentLabelMap;
		
		float maximumDistance;
		unsigned int maximumAge;
		unsigned int curLabel;
		unsigned int getNewLabel() {
			return curLabel++;
		}
		
	public:
		Tracker<T>()
		:curLabel(0)
		,maximumAge(4)
		,maximumDistance(32) {
		}
		void setMaximumAge(unsigned int maximumAge);
		void setMaximumDistance(float maximumDistance);
		vector<unsigned int>& track(const vector<T>& objects);
		
		// organized in the order received by track()
		vector<unsigned int>& getCurrentLabels();
		vector<unsigned int>& getPreviousLabels();
		vector<unsigned int>& getNewLabels();
		vector<unsigned int>& getDeadLabels();
		unsigned int getLabelFromIndex(unsigned int i) const;
		
		// organized by label
		int getIndexFromLabel(unsigned int label) const;
		T& getPrevious(unsigned int label) const;
		T& getCurrent(unsigned int label) const;
		bool existsCurrent(unsigned int label) const;
		bool existsPrevious(unsigned int label) const;
	};
	
	template <class T>
	void Tracker<T>::setMaximumAge(unsigned int maximumAge) {
		this->maximumAge = maximumAge;
	}
	
	template <class T>
	void Tracker<T>::setMaximumDistance(float maximumDistance) {
		this->maximumDistance = maximumDistance;
	}
	
	template <class T>
	vector<unsigned int>& Tracker<T>::track(const vector<T>& objects) {
		previous = current;
		int n = objects.size();
		int m = previous.size();
		
		// build NxM distance matrix
		typedef std::pair<int, int> MatchPair;
		typedef std::pair<MatchPair, float> MatchDistancePair;
		vector<MatchDistancePair> all;
		for(int i = 0; i < n; i++) {
{'fraction_non_alphanumeric': 0.08764759309718438, 'fraction_numerical': 0.0029518619436875566, 'mean_word_length': 4.274850299401198, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6291639', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2479, 'n_words': 1005}