contrib/brl/bseg/brip/brip_quadtree_node.h
Go to the documentation of this file.
00001 #ifndef brip_quadtree_h
00002 #define brip_quadtree_h
00003 //-----------------------------------------------------------------
00004 //:
00005 // \file
00006 // \author  J.L. Mundy March 8, 2008
00007 // \brief A quadtree structure for 2-d arrays (not necessarily an image)
00008 //
00009 // \verbatim
00010 //  a quadtree node has the structure:
00011 //      ( node )
00012 //    ./  |  |  \.
00013 //  q00 q01 q10 q11  (indexed by i, j as qij)
00014 // \endverbatim
00015 // The quadtree can hold any datatype, not necessarily image data.
00016 //
00017 // \verbatim
00018 // Modifications:
00019 //   None
00020 // \endverbatim
00021 //------------------------------------------------------------------
00022 
00023 #include <vcl_vector.h>
00024 #include <vbl/vbl_ref_count.h>
00025 #include <brip/brip_quadtree_node_base_sptr.h>
00026 
00027 //: the base class representing the quadtree datastructure - holds no data
00028 class brip_quadtree_node_base : public vbl_ref_count
00029 {
00030  public:
00031   brip_quadtree_node_base()
00032   : iul_(0), jul_(0), ilr_(0), jlr_(0), valid_(false),
00033     children_(vcl_vector<brip_quadtree_node_base_sptr>(4)) {}
00034 
00035   brip_quadtree_node_base(unsigned iul, unsigned jul,
00036                           unsigned ilr, unsigned jlr, bool valid = false)
00037   : iul_(iul), jul_(jul), ilr_(ilr), jlr_(jlr), valid_(valid),
00038     children_(vcl_vector<brip_quadtree_node_base_sptr>(4)) {}
00039 
00040   ~brip_quadtree_node_base() {}
00041 
00042   bool data_valid() const { return valid_; }
00043 
00044   //: return the child, qij
00045   brip_quadtree_node_base_sptr child(unsigned i, unsigned j)
00046   { return children_[2*j+i]; }
00047 
00048   //: set the child, qij
00049   void set_child(unsigned i, unsigned j,
00050                  brip_quadtree_node_base_sptr const& child)
00051   { children_[2*j+i] = child; }
00052 
00053   //: the number of non-null child nodes
00054   unsigned n_children() const
00055   { unsigned n=0; for (unsigned i=0; i<4; ++i) if (children_[i]) ++n; return n;}
00056 
00057   //: the parent node to *this node
00058   brip_quadtree_node_base_sptr parent() { return parent_; }
00059 
00060   //: set the parent
00061   void set_parent(brip_quadtree_node_base_sptr const& parent) {parent_=parent;}
00062 
00063   //: the region in the array represented by *this node
00064   void region(unsigned& iul, unsigned& jul, unsigned& ilr, unsigned& jlr) const
00065   { iul = iul_; jul = jul_; ilr = ilr_; jlr = jlr_; }
00066 
00067   //: set the region in the array represented by *this node
00068   void set_region(unsigned iul, unsigned jul, unsigned ilr, unsigned jlr)
00069   { iul_ = iul; jul_ = jul; ilr_ = ilr; jlr_ = jlr;}
00070 
00071  protected:
00072   unsigned iul_, jul_;
00073   unsigned ilr_, jlr_;
00074   bool valid_; // data valid
00075   brip_quadtree_node_base_sptr parent_;
00076   vcl_vector<brip_quadtree_node_base_sptr> children_;
00077 };
00078 
00079 //: the templated subclass that holds data values, T,  in a quadtree structure
00080 template <class T>
00081 class brip_quadtree_node : public brip_quadtree_node_base
00082 {
00083   // PUBLIC INTERFACE-----------------------------------------------
00084 
00085  public:
00086 
00087   // Constructors/Initializers/Destructors--------------------------
00088   brip_quadtree_node()
00089   : data_(T(0)) {}
00090 
00091   brip_quadtree_node(unsigned iul, unsigned jul, unsigned ilr, unsigned jlr)
00092   : brip_quadtree_node_base(iul, jul, ilr, jlr, false) {}
00093 
00094   brip_quadtree_node(unsigned iul, unsigned jul, unsigned ilr,
00095                      unsigned jlr, T data)
00096   : brip_quadtree_node_base(iul, jul, ilr, jlr, true),data_(data){}
00097 
00098   ~brip_quadtree_node(){}
00099 
00100   // Data Access----------------------------------------------------
00101 
00102   T data() const { return data_; }
00103 
00104   // Utility Methods
00105 
00106  protected:
00107   T data_;// pixel value
00108 };
00109 
00110 VCL_DEFINE_SPECIALIZATION
00111 class brip_quadtree_node<float> : public brip_quadtree_node_base
00112 {
00113  public:
00114   // Constructors/Initializers/Destructors--------------------------
00115   brip_quadtree_node()
00116   : data_(float(0)) {}
00117 
00118   brip_quadtree_node(unsigned iul, unsigned jul, unsigned ilr, unsigned jlr)
00119   : brip_quadtree_node_base(iul, jul, ilr, jlr, false) {}
00120 
00121   brip_quadtree_node(unsigned iul, unsigned jul, unsigned ilr,
00122                      unsigned jlr, float data)
00123   : brip_quadtree_node_base(iul, jul, ilr, jlr, true), data_(data) {}
00124 
00125   ~brip_quadtree_node() {}
00126 
00127   // Data Access----------------------------------------------------
00128 
00129   float data() const { return data_; }
00130 
00131  protected:
00132   float data_; // pixel value
00133 };
00134 
00135 #endif // brip_quadtree_h