shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/node.hpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * M a t h e m a g i x
00003  *****************************************************************************
00004  * kdnode
00005  * 2009-05-18
00006  * Julien Wintz & Bernard Mourrain
00007  *****************************************************************************
00008  *               Copyright (C) 2009 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 # ifndef shape_node_hpp
00013 # define shape_node_hpp
00014 
00015 # include <iostream>
00016 # include <vector>
00017 # define Node node<Object, CELL>
00018 //====================================================================
00019 namespace mmx {
00020 namespace shape {
00021 
00022 template <class _Object, class _CELL> class node
00023 {
00024 public:
00025   enum NODE_TYPE { LEFT, RIGHT } ;
00026   typedef _Object Object;
00027   typedef _CELL   CELL;
00028   typedef _CELL   Cell;
00029 
00030 public:
00031   node(void) ;
00032   node(Object object, CELL cl) ;
00033   node(node<Object, CELL>* parent, CELL & cl, NODE_TYPE nodeType, int v=0) ;
00034   node(node<Object, CELL>* left, node<Object, CELL> * right, CELL cl, int v=0) ;
00035 
00036 protected:
00037   node(Node& node) ;
00038 
00039 public:    
00040   inline void set_cell(const CELL & c) { m_cell = c ; }
00041   inline void set_parent(Node * n)     { m_parent = n ; }
00042   
00043   inline void set_leftchild(node<Object, CELL> * n)  { m_left = n ; }
00044   inline void set_rightchild(node<Object, CELL> * n) { m_right = n ; }
00045   
00046   inline const CELL& get_cell(void) const { return m_cell; } 
00047   inline       CELL& get_cell(void)       { return m_cell ; }
00048   
00049   inline Object object(void) { return m_objects.front() ; }
00050   
00051   inline NODE_TYPE type(void) const { return m_type ; }
00052   inline int split_dir(void) const { return m_var; }
00053   
00054   inline node<Object, CELL> * left  (void) { return m_left ; }
00055   inline node<Object, CELL> * right (void) { return m_right ; }
00056   inline node<Object, CELL> * parent(void) { return m_parent ; }
00057   
00058   inline const node<Object, CELL> * left  (void) const { return m_left ; }
00059   inline const node<Object, CELL> * right (void) const { return m_right ; }
00060   inline const node<Object, CELL> * parent(void) const { return m_parent ; }
00061     
00062   int    var(void) const {return this->m_var;}
00063   bool   is_leaf(void) const;
00064   size_t leaf_distance() const;
00065 
00066 public:   
00067   CELL                m_cell ;
00068   std::vector<Object> m_objects ;
00069   NODE_TYPE           m_type ;
00070   int                 m_var ;
00071     
00072   Node * m_parent  ;   
00073   Node * m_left ;
00074   Node * m_right ;
00075 
00076   int depth ;
00077   int index ;
00078 } ;
00079 
00080 //--------------------------------------------------------------------
00081 template<class Object, class CELL> node<Object, CELL>::node(void)
00082 { 
00083     m_type   = LEFT ;
00084     m_var    = 0 ;
00085 
00086     m_parent = NULL ;
00087     m_left   = NULL;
00088     m_right  = NULL;
00089 
00090     m_cell   = NULL ;
00091 
00092 
00093     depth = 0;
00094     index = 0;
00095 } 
00096 
00097 template<class Object, class CELL> node<Object, CELL>::node(Object object, CELL cl)
00098 { 
00099     this->m_type   = LEFT ;
00100     this->m_var    = 0 ;
00101 
00102     this->m_parent = NULL ;
00103     this->m_left   = NULL;
00104     this->m_right  = NULL;
00105 
00106     this->m_cell   = cl ;
00107     m_objects.push_back(object) ;
00108 
00109     depth = 0;
00110     index = 0;
00111 } 
00112 
00113 template <class Object, class CELL> node<Object, CELL>::node(node<Object, CELL> * left, node<Object, CELL> * right, CELL cl, int v) {
00114 
00115     this->m_parent = NULL ;
00116     this->m_cell   = cl ;
00117     this->m_var    = v ;
00118 
00119     left->type  = LEFT ;  left->parent = this ; m_left = left; 
00120     right->type = RIGHT; right->parent = this ; m_right= right;
00121 
00122     depth = left->depth-1 ;
00123 }
00124 
00125   template <class Object, class CELL> node<Object, CELL>::node(node<Object, CELL> * parent, CELL & cl, NODE_TYPE type, int v)
00126 {  
00127     this->m_cell   = cl ;
00128     this->m_type   = type ; 
00129     this->m_var    = v ;
00130     this->m_parent = parent ;
00131 
00132     this->m_left   = NULL;
00133     this->m_right  = NULL;
00134 
00135     depth = parent->depth+1 ;
00136 
00137     switch(type) { 
00138     case LEFT : parent->set_leftchild(this) ; break ; 
00139     case RIGHT: parent->set_rightchild(this) ; break ;  
00140     default: std::cerr << "Error : the node's type isn't appropriate \n" ; break ;
00141     }
00142 }
00143 
00144 template<class Object, class CELL> bool node<Object, CELL>::is_leaf(void) const
00145 {
00146     if((m_left == NULL) && (m_right == NULL) )
00147         return true; 
00148     return false; 
00149 } 
00150 
00151 template<class Object, class CELL> size_t node<Object, CELL>::leaf_distance(void) const
00152 {
00153         if ( this->is_leaf() )
00154                 return 0;
00155 
00156         struct inner {
00157                 size_t operator()( const Node* node ) {
00158                         if ( node == 0 )
00159                                 return 0;
00160                         else
00161                                 return node->leaf_distance();
00162                 }
00163         } I;
00164 
00165         size_t d = 0;
00166         d = std::min( d, I(m_left)  );
00167         d = std::min( d, I(m_right) );
00168 
00169         return d+1;
00170 }
00171 
00172 
00173 } ; // namespace shape
00174 } ; // namespace mmx
00175 //====================================================================
00176 # undef Node
00177 # endif // shape_node_hpp