indii/ml/aux/KDTreeNode.hpp

00001 #ifndef INDII_ML_AUX_KDTREENODE_HPP
00002 #define INDII_ML_AUX_KDTREENODE_HPP
00003 
00004 #include "PartitionTreeNode.hpp"
00005 #include "DiracMixturePdf.hpp"
00006 
00007 #include "boost/serialization/split_member.hpp"
00008 
00009 namespace indii {
00010   namespace ml {
00011     namespace aux {
00012 /**
00013  * Node of a \f$kd\f$ tree.
00014  *
00015  * @author Lawrence Murray <lawrence@indii.org>
00016  * @version $Rev: 570 $
00017  * @date $Date: 2008-09-16 16:49:33 +0100 (Tue, 16 Sep 2008) $
00018  *
00019  * @section KDTreeNode_serialization Serialization
00020  *
00021  * This class supports serialization through the Boost.Serialization
00022  * library.
00023  */
00024 class KDTreeNode : public PartitionTreeNode {
00025 public:
00026   /**
00027    * Default constructor.
00028    *
00029    * This should generally only be used when the object is to be
00030    * restored from a serialization.
00031    */
00032   KDTreeNode();
00033 
00034   /**
00035    * Construct leaf node.
00036    *
00037    * @param p Weighted sample set.
00038    * @param i Index of component in the weighted sample set.
00039    * @param depth Depth of the node in the tree.
00040    */
00041   KDTreeNode(DiracMixturePdf* p, unsigned int i, unsigned int depth);
00042 
00043   /**
00044    * Construct prune node.
00045    *
00046    * @param p Weighted sample set.
00047    * @param is Indices of components in the weighted sample set.
00048    * @param depth Depth of the node in the tree.
00049    */
00050   KDTreeNode(DiracMixturePdf* p, const std::vector<unsigned int>& is,
00051       const unsigned int depth);
00052 
00053   /**
00054    * Construct internal node.
00055    *
00056    * @param left Left child node. Caller releases ownership.
00057    * @param right Right child node. Caller releases ownership.
00058    * @param depth Depth of the node in the tree.
00059    */
00060   KDTreeNode(KDTreeNode* left, KDTreeNode* right, const unsigned int depth);
00061 
00062   /**
00063    * Copy constructor.
00064    */
00065   KDTreeNode(const KDTreeNode& o);
00066 
00067   /**
00068    * Destructor.
00069    */
00070   virtual ~KDTreeNode();
00071 
00072   /**
00073    * Assignment operator.
00074    */
00075   KDTreeNode& operator=(const KDTreeNode& o);
00076 
00077   virtual PartitionTreeNode* clone() const;
00078 
00079   /**
00080    * Get lower bound on the node.
00081    */
00082   const vector* getLower() const;
00083   
00084   /**
00085    * Get upper bound on the node.
00086    */
00087   const vector* getUpper() const;
00088 
00089   virtual void difference(const vector& x, vector& result) const;
00090 
00091   virtual void difference(const PartitionTreeNode& node, vector& result)
00092       const;
00093 
00094 private:  
00095   /**
00096    * The lower bound.
00097    */
00098   vector* lower;
00099   
00100   /**
00101    * The upper bound.
00102    */
00103   vector* upper;
00104   
00105   /**
00106    * Does object own the lower bound?
00107    */
00108   bool ownLower;
00109   
00110   /**
00111    * Does object own the upper bound?
00112    */
00113   bool ownUpper;
00114 
00115   /**
00116    * Set the lower bound on the node.
00117    *
00118    * @param lower Lower bound on the node.
00119    * @param own True if ownership of the object should be assumed.
00120    */
00121   void setLower(vector* lower, const bool own);
00122   
00123   /**
00124    * Set the upper bound on the node.
00125    *
00126    * @param upper Upper bound on the node.
00127    * @param own True if ownership of the object should be assumed.
00128    */
00129   void setUpper(vector* upper, const bool own);
00130 
00131   /**
00132    * Serialize.
00133    */
00134   template<class Archive>
00135   void save(Archive& ar, const unsigned int version) const;
00136 
00137   /**
00138    * Restore from serialization.
00139    */
00140   template<class Archive>
00141   void load(Archive& ar, const unsigned int version);
00142 
00143   /*
00144    * Boost.Serialization requirements.
00145    */
00146   BOOST_SERIALIZATION_SPLIT_MEMBER()
00147   friend class boost::serialization::access;
00148 
00149 };
00150 
00151     }
00152   }
00153 }
00154 
00155 #include "boost/serialization/base_object.hpp"
00156 
00157 inline const indii::ml::aux::vector* indii::ml::aux::KDTreeNode::getLower()
00158     const {
00159   return lower;
00160 }
00161 
00162 inline const indii::ml::aux::vector* indii::ml::aux::KDTreeNode::getUpper()
00163     const {
00164   return upper;
00165 }
00166 
00167 template<class Archive>
00168 void indii::ml::aux::KDTreeNode::save(Archive& ar,
00169     const unsigned int version) const {
00170   ar & boost::serialization::base_object<PartitionTreeNode>(*this);
00171 
00172   ar & ownLower;
00173   if (ownLower) {
00174     ar & lower;
00175   }
00176   ar & ownUpper;
00177   if (ownUpper) {
00178     ar & upper;
00179   }
00180 }
00181 
00182 template<class Archive>
00183 void indii::ml::aux::KDTreeNode::load(Archive& ar,
00184     const unsigned int version) {
00185   ar & boost::serialization::base_object<PartitionTreeNode>(*this);
00186     
00187   if (ownLower) {
00188     delete lower;
00189     lower = NULL;
00190   }
00191   if (ownUpper) {
00192     delete upper;
00193     upper = NULL;
00194   }
00195   
00196   ar & ownLower;
00197   if (ownLower) {
00198     ar & lower;
00199   }
00200   ar & ownUpper;
00201   if (ownUpper) {
00202     ar & upper;
00203   }
00204 
00205   /* tracking of indii::ml::aux::vector may be turned off, and commonly is,
00206      so restore non-owned bounds separately */
00207   KDTreeNode* left;
00208   KDTreeNode* right;
00209   if (getLeft() == NULL) {
00210     left = NULL;
00211   } else {
00212     left = dynamic_cast<KDTreeNode*>(getLeft());
00213   }
00214   if (getRight() == NULL) {
00215     right = NULL;
00216   } else {
00217     right = dynamic_cast<KDTreeNode*>(getRight());
00218   }
00219 
00220   if (!ownLower) {
00221     if (left->lower != NULL) {
00222       lower = left->lower;
00223     } else {
00224       lower = right->lower;
00225     }
00226   }
00227   if (!ownUpper) {
00228     if (isLeaf()) {
00229       upper = lower; // see constructor
00230     } else {
00231       if (right->upper != NULL) {
00232   upper = right->upper;
00233       } else {
00234   upper = left->upper;
00235       }
00236     }
00237   }
00238 }
00239 
00240 #endif
00241 

Generated on Wed Dec 17 15:11:57 2008 for dysii Dynamical Systems Library by  doxygen 1.5.3