Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

bmrf_network.h

Go to the documentation of this file.
00001 // This is brl/bseg/bmrf/bmrf_network.h
00002 #ifndef bmrf_network_h_
00003 #define bmrf_network_h_
00004 //:
00005 // \file
00006 // \brief A Markov Random Field (MRF) network
00007 // \author Matt Leotta, (mleotta@lems.brown.edu)
00008 // \date 1/13/04
00009 //
00010 // The MRF network object maintains pointers to all of the nodes
00011 // in the network.  It also handle the creation and destruction of
00012 // arcs between nodes.  The network object also contains search
00013 // iterators to iterate through the nodes along arcs using search
00014 // algorithms such as depth-first or breadth-first.
00015 //
00016 // \verbatim
00017 //  Modifications
00018 // \endverbatim
00019 
00020 #include <vcl_deque.h>
00021 #include <vcl_set.h>
00022 #include <vsl/vsl_binary_io.h>
00023 #include <vbl/vbl_ref_count.h>
00024 #include "bmrf_node_sptr.h"
00025 #include "bmrf_node.h"
00026 #include "bmrf_network_sptr.h"
00027 #include "bmrf_epi_seg_sptr.h"
00028 #include "bmrf_epipole.h"
00029 
00030 //: The MRF network
00031 class bmrf_network : public vbl_ref_count
00032 {
00033  public:
00034   typedef vcl_map<bmrf_epi_seg_sptr, bmrf_node_sptr> seg_node_map;
00035   typedef vcl_map<int, seg_node_map > frame_node_map;
00036 
00037   typedef bmrf_node::neighbor_type neighbor_type;
00038 
00039   //: Constructor
00040   bmrf_network() {}
00041 
00042   //: Copy constructor
00043   bmrf_network(bmrf_network const& n) : vbl_ref_count(),
00044                                         node_from_seg_(n.node_from_seg_),
00045                                         nodes_from_frame_(n.nodes_from_frame_),
00046                                         epipoles_(n.epipoles_) {}
00047 
00048   //: Destructor
00049   ~bmrf_network() {}
00050 
00051   //: Adds a new to the network
00052   // \retval true if the node was added
00053   // \retval false if the node could not be added
00054   // \note every node in the network must have a unique epi_segment
00055   bool add_node(const bmrf_node_sptr& node);
00056 
00057   //: Deletes a node in the network
00058   // \retval true if the node was deleted
00059   // \retval false if the node was not found in the network
00060   bool remove_node(bmrf_node_sptr node);
00061 
00062   //: Add an arc between \p n1 and \p n2 of type \p type
00063   bool add_arc( const bmrf_node_sptr& n1, const bmrf_node_sptr& n2, neighbor_type type );
00064 
00065   //: Add an arc \p arc of type \p type
00066   bool add_arc( const bmrf_arc_sptr& arc, neighbor_type type );
00067 
00068   //: Add an arc between \p n1 and \p n2 of type \p type
00069   bool remove_arc( const bmrf_node_sptr& n1, const bmrf_node_sptr& n2, neighbor_type type = bmrf_node::ALL );
00070 
00071   //: Remove all arcs to NULL nodes and node not found in this network
00072   // \retval true if any arcs have been purged
00073   // \retval false if all arcs were found to be valid
00074   bool purge();
00075 
00076   //: Look up the node corresponding to an epi-segment
00077   // \return a null smart pointer if no node exists
00078   // \note if the optional paramater \p frame is positive the search is restricted to that frame
00079   bmrf_node_sptr seg_to_node(const bmrf_epi_seg_sptr& seg, int frame = -1) const;
00080 
00081   //: Returns the number of frames in the network
00082   int num_frames() const;
00083 
00084   //: Returns the set of active frame numbers
00085   // \note frame_numbers().size() == num_frames() but the numbers do not start at zero in general
00086   vcl_set<int> frame_numbers() const;
00087 
00088   //: Returns the number of nodes in the network
00089   // \ note if the optional parameter \p frame is positive then the size is of that frame
00090   int size( int frame = -1 );
00091 
00092   //: Returns the probability that the entire network is correct
00093   double probability();
00094 
00095   //: Remove all nodes and arcs with probability less than \p threshold
00096   void prune_by_probability(double threshold, bool relative = false);
00097 
00098   //: Prune nodes with a mean gamma outside this range
00099   void prune_by_gamma(double min_gamma, double max_gamma);
00100 
00101   //: Prune directed arcs leaving the undirected subset of the network
00102   void prune_directed();
00103 
00104   //: Set the epipole for frame \p frame
00105   void set_epipole(const bmrf_epipole& epipole, int frame);
00106 
00107   //: Access the epipole for frame \p frame
00108   const bmrf_epipole& epipole(int frame) const;
00109 
00110   //: Returns the beginning const iterator to the map of nodes in frame \p frame
00111   // \note if \p frame is negative the iterator will cover all frames
00112   seg_node_map::const_iterator begin(int frame = -1) const;
00113 
00114   //: Returns the end const iterator to the map of nodes in frame \p frame
00115   // \note if \p frame is negative the iterator will cover all frames
00116   seg_node_map::const_iterator end(int frame = -1) const;
00117 
00118   //: Binary save self to stream.
00119   void b_write(vsl_b_ostream &os) const;
00120 
00121   //: Binary load self from stream.
00122   void b_read(vsl_b_istream &is);
00123 
00124   //: Return IO version number;
00125   short version() const;
00126 
00127   //: Print an ascii summary to the stream
00128   void print_summary(vcl_ostream &os) const;
00129 
00130  private:
00131   //: The map from epi_seg pointers to nodes in the network
00132   // \note indexed by epi_seg pointers for quick reverse lookup
00133   seg_node_map node_from_seg_;
00134 
00135   //: The map from frame number to list of nodes in that frame
00136   frame_node_map nodes_from_frame_;
00137 
00138   //: The vector of epipoles (for each frame)
00139   vcl_vector<bmrf_epipole> epipoles_;
00140 
00141  public:
00142   class iterator
00143   {
00144    public:
00145     //: Constructor
00146     iterator( bmrf_network* network, bmrf_node_sptr node ) : network_(network), curr_node_(node) {}
00147 
00148     //: Destructor
00149     virtual ~iterator() {}
00150 
00151     //: Increment
00152     iterator& operator++ () { next_node(); return *this; }
00153 
00154     //: Dereference
00155     bmrf_node_sptr operator -> () const { return curr_node_; }
00156     //: Dereference
00157     bmrf_node_sptr operator * () const { return curr_node_; }
00158 
00159     //: Equality comparison
00160     bool operator == (const iterator& rhs) const { return rhs.curr_node_ == this->curr_node_; }
00161 
00162     //: Inequality comparison
00163     bool operator != (const iterator& rhs) const { return rhs.curr_node_ != this->curr_node_; }
00164 
00165    protected:
00166     //: Increment the current node
00167     virtual void next_node() = 0;
00168 
00169     bmrf_network* network_;
00170     bmrf_node_sptr curr_node_;
00171   };
00172 
00173   // Depth first search iterator
00174   class depth_iterator : public iterator
00175   {
00176    public:
00177     //: Constructor
00178     depth_iterator( bmrf_network* network, bmrf_node_sptr node ) : iterator(network, node){ visited_.insert(node); }
00179 
00180    protected:
00181     //: Increment the current node
00182     void next_node();
00183 
00184     vcl_deque<bmrf_node_sptr> eval_queue_;
00185     vcl_set<bmrf_node_sptr> visited_;
00186   };
00187 
00188   // Breadth first search iterator
00189   class breadth_iterator : public iterator
00190   {
00191    public:
00192     //: Constructor
00193     breadth_iterator( bmrf_network* network, bmrf_node_sptr node ) : iterator(network, node){ visited_.insert(node); }
00194 
00195    protected:
00196     //: Increment the current node
00197     void next_node();
00198 
00199     vcl_deque<bmrf_node_sptr> eval_queue_;
00200     vcl_set<bmrf_node_sptr> visited_;
00201   };
00202 
00203   //: Depth first search begin iterator
00204   depth_iterator depth_begin(bmrf_node_sptr node) { return depth_iterator(this, node); }
00205   //: Depth first search end iterator
00206   depth_iterator depth_end()   { return depth_iterator(this, NULL); }
00207 
00208   //: Breadth first search begin iterator
00209   breadth_iterator breadth_begin(bmrf_node_sptr node) { return breadth_iterator(this, node); }
00210   //: Breadth first search end iterator
00211   breadth_iterator breadth_end()   { return breadth_iterator(this, NULL); }
00212 };
00213 
00214 
00215 //: Binary save bmrf_network* to stream.
00216 void vsl_b_write(vsl_b_ostream &os, const bmrf_network* n);
00217 
00218 //: Binary load bmrf_network* from stream.
00219 void vsl_b_read(vsl_b_istream &is, bmrf_network* &n);
00220 
00221 //: Print an ASCII summary to the stream
00222 void vsl_print_summary(vcl_ostream &os, const bmrf_network* n);
00223 
00224 
00225 #endif // bmrf_network_h_

Generated on Thu Jan 10 14:51:53 2008 for contrib/brl/bseg/bmrf by  doxygen 1.4.4