contrib/mul/mbl/mbl_cluster_tree.h
Go to the documentation of this file.
00001 #ifndef mbl_cluster_tree_h_
00002 #define mbl_cluster_tree_h_
00003 //:
00004 // \file
00005 // \brief  Record trees of clusters of data, for faster neighbour finding
00006 // \author Tim Cootes
00007 
00008 #include <mbl/mbl_clusters.h>
00009 #include <vcl_iosfwd.h>
00010 
00011 //: Record trees of clusters of data, for fast neighbour finding
00012 //  Used to record clusters of objects of type T.
00013 //  D::d(T t1, T t2) is a measure of distance between two objects.
00014 //  It must obey the triangle inequality: D::d(t1,t2)<=D::d(t1,t3)+D::d(t2,t3).
00015 //
00016 //  The class is designed to allow fast location of the nearest 
00017 //  example in a set objects to a given new object.
00018 //  It represents the data as a
00019 //  set of key point positions, together with a list of indices into
00020 //  the external data for each cluster.  Each cluster is in turn
00021 //  assigned to a large cluster at a higher level.
00022 //
00023 //  Thus to find the nearest neighbour, we first check for proximity
00024 //  to the keypoints, and only consider objects in the clusters
00025 //  which are sufficiently close.
00026 template<class T, class D>
00027 class mbl_cluster_tree
00028 {
00029  private:
00030   //: Storage for objects
00031   vcl_vector<T> data_;
00032 
00033   //: Clusters
00034   vcl_vector<mbl_clusters<T,D> > cluster_;
00035 
00036   //: Indicate which cluster each object is assigned to.
00037   //  parent_[0][i] indicates which cluster in cluster_[0] data_[i]
00038   //  is assigned to.
00039   //  parent_[j][i] (j>0) indicates which cluster in level above
00040   //  cluster_[j-1].p()[i] is assigned to.
00041   vcl_vector<vcl_vector<unsigned> > parent_;
00042 
00043   //: Empty clusters
00044   void empty();
00045 
00046   //: Append new object with index i and assign to clusters.
00047   //  Assumes that new object data()[i] is available. 
00048   //  Deduce which clusters belongs to and add it.
00049   //  Create new clusters if further than max_r() from any.
00050   void add_object(unsigned i);
00051 
00052  public:
00053   mbl_cluster_tree();
00054 
00055   //: Define number of levels and max radius of clusters at each level
00056   void set_max_r(const vcl_vector<double>& r);
00057 
00058   //: Copy in data
00059   //  Empty existing clusters, then process every element of data
00060   //  to create clusters, by calling add_object() 
00061   void set_data(const vcl_vector<T>& data);
00062 
00063   //: Add an extra element to data()
00064   void push_back(const T& t);
00065 
00066   //: List of objects
00067   const vcl_vector<T>& data() const { return data_; }
00068 
00069   //: Return index of nearest object in data() to t
00070   //  Nearest object in data() to t is given by data()[nearest(t,d)];
00071   //  The distance to the point is d
00072   unsigned nearest(const T& t, double& d) const;
00073 
00074   //: Print ancestry of every element
00075   void print_tree(vcl_ostream& os) const;
00076 
00077   //: Version number for I/O
00078   short version_no() const;
00079 
00080   //: Print class to os
00081   void print_summary(vcl_ostream& os) const;
00082 
00083   //: Save class to binary file stream.
00084   void b_write(vsl_b_ostream& bfs) const;
00085 
00086   //: Load class from binary file stream
00087   void b_read(vsl_b_istream& bfs);
00088 };
00089 
00090 //: Binary file stream output operator for class reference
00091 template<class T, class D>
00092 void vsl_b_write(vsl_b_ostream& bfs, const mbl_cluster_tree<T,D>& c);
00093 
00094 //: Binary file stream input operator for class reference
00095 template<class T, class D>
00096 void vsl_b_read(vsl_b_istream& bfs, mbl_cluster_tree<T,D>& c);
00097 
00098 //: Stream output operator for class reference
00099 template<class T, class D>
00100 vcl_ostream& operator<<(vcl_ostream& os,const mbl_cluster_tree<T,D>& c);
00101 
00102 #endif // mbl_cluster_tree_h_