indii/ml/aux/PartitionTreeNode.hpp

00001 #ifndef INDII_ML_AUX_PARTITIONTREENODE_HPP
00002 #define INDII_ML_AUX_PARTITIONTREENODE_HPP
00003 
00004 #include "vector.hpp"
00005 
00006 #include "boost/serialization/split_member.hpp"
00007 
00008 namespace indii {
00009   namespace ml {
00010     namespace aux {
00011 /**
00012  * Node of a spatial partition tree.
00013  *
00014  * @author Lawrence Murray <lawrence@indii.org>
00015  * @version $Rev: 590 $
00016  * @date $Date: 2008-12-17 15:09:40 +0000 (Wed, 17 Dec 2008) $
00017  *
00018  * @section PartitionTreeNode_serialization Serialization
00019  *
00020  * This class supports serialization through the Boost.Serialization
00021  * library.
00022  */
00023 class PartitionTreeNode {
00024 public:
00025   /**
00026    * Constructor.
00027    *
00028    * This should generally only be used when the object is to be
00029    * restored from a serialization.
00030    */
00031   PartitionTreeNode();
00032 
00033   /**
00034    * Constructor for leaf node.
00035    *
00036    * @param i Index of component in the weighted sample set.
00037    * @param depth Depth of the node in that tree.
00038    */
00039   PartitionTreeNode(const unsigned int i, const unsigned int depth);
00040    
00041   /**
00042    * Constructor for prune node.
00043    *
00044    * @param is Indices of components in the weighted sample set.
00045    * @param depth Depth of the node in that tree.
00046    */
00047   PartitionTreeNode(const std::vector<unsigned int>& is,
00048       const unsigned int depth);
00049   
00050   /**
00051    * Constructor for internal node.
00052    *
00053    * @param left Left child node. Caller releases ownership.
00054    * @param right Right child node. Caller releases ownership.
00055    * @param depth Depth of the node in that tree.
00056    */
00057   PartitionTreeNode(PartitionTreeNode* left, PartitionTreeNode* right,
00058       const unsigned int depth);
00059 
00060   /**
00061    * Copy constructor.
00062    */
00063   PartitionTreeNode(const PartitionTreeNode& o);
00064   
00065   /**
00066    * Destructor.
00067    */
00068   virtual ~PartitionTreeNode();
00069 
00070   /**
00071    * Assignment operator.
00072    */
00073   PartitionTreeNode& operator=(const PartitionTreeNode& o);
00074 
00075   /**
00076    * Clone node.
00077    *
00078    * @return Clone of node. Caller has ownership.
00079    */
00080   virtual PartitionTreeNode* clone() const = 0;
00081 
00082   /**
00083    * Get the depth of the node in its tree.
00084    *
00085    * @return The depth of the node.
00086    */
00087   unsigned int getDepth() const;
00088 
00089   /**
00090    * Is the node a leaf node?
00091    *
00092    * @return True if the node is a leaf node, false otherwise.
00093    */
00094   bool isLeaf() const;
00095   
00096   /**
00097    * Is the node a pruned node?
00098    *
00099    * @return True if the node is a pruned node, false otherwise.
00100    */
00101   bool isPrune() const;
00102   
00103   /**
00104    * Is the node an internal node?
00105    *
00106    * @return True if the node is an internal node, false otherwise.
00107    */
00108   bool isInternal() const;
00109 
00110   /**
00111    * Get the number of components encompassed by the node.
00112    *
00113    * @return The number of components encompassed by the node.
00114    */
00115   unsigned int getSize() const;
00116 
00117   /**
00118    * Get the component index of a leaf node.
00119    *
00120    * @return The component index, if a leaf node.
00121    */
00122   unsigned int getIndex() const;
00123 
00124   /**
00125    * Get the component indices of a pruned node.
00126    *
00127    * @return The component indices, if a pruned node.
00128    */
00129   const std::vector<unsigned int>& getIndices() const;
00130 
00131   /**
00132    * Get the left child of the node.
00133    *
00134    * @return The left child of an internal node.
00135    */
00136   PartitionTreeNode* getLeft();
00137   
00138   /**
00139    * Get the right child of the node.
00140    *
00141    * @return The right child of an internal node.
00142    */
00143   PartitionTreeNode* getRight();
00144 
00145   /**
00146    * Find the coordinate difference of the node from a single point.
00147    *
00148    * @param x Query point.
00149    * @param result After return, difference between the query point and
00150    * the nearest point within the volume contained by the node.
00151    *
00152    * Note that the difference may contain negative values. Usually a norm
00153    * would subsequently be applied to obtain a scalar distance.
00154    */
00155   virtual void difference(const vector& x, vector& result) const = 0;
00156 
00157   /**
00158    * Find the coordinate difference of the node from another node.
00159    *
00160    * @param node Query node.
00161    * @param result After return, difference between the closest two points
00162    * in the volumes contained by the nodes.
00163    *
00164    * Note that the difference may contain negative values. Usually a norm
00165    * would subsequently be applied to obtain a scalar distance.
00166    */
00167   virtual void difference(const PartitionTreeNode& node, vector& result)
00168       const = 0;
00169   
00170 private:
00171   /**
00172    * Is the node a leaf node?
00173    */
00174   bool flagLeaf;
00175   
00176   /**
00177    * Is the node a pruned node?
00178    */
00179   bool flagPrune;
00180   
00181   /**
00182    * Is the node an internal node?
00183    */
00184   bool flagInternal;
00185 
00186   /**
00187    * Depth of the node.
00188    */
00189   unsigned int depth;
00190 
00191   /**
00192    * Number of components encompassed by the node.
00193    */
00194   unsigned int size;
00195 
00196   /**
00197    * Component index for leaf node.
00198    */
00199   unsigned int i;
00200 
00201   /**
00202    * Component indices for prune node.
00203    */
00204   std::vector<unsigned int> is;
00205 
00206   /**
00207    * The left child for an internal node.
00208    */
00209   PartitionTreeNode* left;
00210   
00211   /**
00212    * The right child for an internal node.
00213    */
00214   PartitionTreeNode* right;
00215 
00216   /**
00217    * Serialize.
00218    */
00219   template<class Archive>
00220   void save(Archive& ar, const unsigned int version) const;
00221 
00222   /**
00223    * Restore from serialization.
00224    */
00225   template<class Archive>
00226   void load(Archive& ar, const unsigned int version);
00227 
00228   /*
00229    * Boost.Serialization requirements.
00230    */
00231   BOOST_SERIALIZATION_SPLIT_MEMBER()
00232   friend class boost::serialization::access;
00233 
00234 };
00235 
00236     }
00237   }
00238 }
00239 
00240 inline unsigned int indii::ml::aux::PartitionTreeNode::getIndex() const {
00241   /* pre-condition */
00242   assert (flagLeaf);
00243   
00244   return i;
00245 }
00246 
00247 inline const std::vector<unsigned int>&
00248     indii::ml::aux::PartitionTreeNode::getIndices() const {
00249   return is;
00250 }
00251 
00252 inline indii::ml::aux::PartitionTreeNode*
00253     indii::ml::aux::PartitionTreeNode::getLeft() { 
00254   return left;
00255 }
00256   
00257 inline indii::ml::aux::PartitionTreeNode*
00258     indii::ml::aux::PartitionTreeNode::getRight() {
00259   return right;
00260 }
00261 
00262 inline unsigned int indii::ml::aux::PartitionTreeNode::getDepth() const {
00263   return depth;
00264 }
00265 
00266 inline unsigned int indii::ml::aux::PartitionTreeNode::getSize() const {
00267   return size;
00268 }
00269 
00270 inline bool indii::ml::aux::PartitionTreeNode::isLeaf() const {
00271   return flagLeaf;
00272 }
00273 
00274 inline bool indii::ml::aux::PartitionTreeNode::isPrune() const {
00275   return flagPrune;
00276 }
00277 
00278 inline bool indii::ml::aux::PartitionTreeNode::isInternal() const {
00279   return flagInternal;
00280 }
00281 
00282 template<class Archive>
00283 void indii::ml::aux::PartitionTreeNode::load(Archive& ar,
00284     const unsigned int version) {
00285   delete left;
00286   delete right;
00287 
00288   ar & flagLeaf;
00289   ar & flagPrune;
00290   ar & flagInternal;
00291   ar & depth;
00292   ar & size;
00293   ar & i;
00294   ar & is;
00295   if (flagInternal) {
00296     ar & left;
00297     ar & right;
00298   } else {
00299     left = NULL;
00300     right = NULL;
00301   }
00302 }
00303 
00304 template<class Archive>
00305 void indii::ml::aux::PartitionTreeNode::save(Archive& ar,
00306     const unsigned int version) const {   
00307   ar & flagLeaf;
00308   ar & flagPrune;
00309   ar & flagInternal;
00310   ar & depth;
00311   ar & size;
00312   ar & i;
00313   ar & is;
00314   if (flagInternal) {
00315     ar & left;
00316     ar & right;
00317   }
00318 }
00319 
00320 #endif
00321 

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