00001
00002 #ifndef bmrf_network_h_
00003 #define bmrf_network_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00040 bmrf_network() {}
00041
00042
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
00049 ~bmrf_network() {}
00050
00051
00052
00053
00054
00055 bool add_node(const bmrf_node_sptr& node);
00056
00057
00058
00059
00060 bool remove_node(bmrf_node_sptr node);
00061
00062
00063 bool add_arc( const bmrf_node_sptr& n1, const bmrf_node_sptr& n2, neighbor_type type );
00064
00065
00066 bool add_arc( const bmrf_arc_sptr& arc, neighbor_type type );
00067
00068
00069 bool remove_arc( const bmrf_node_sptr& n1, const bmrf_node_sptr& n2, neighbor_type type = bmrf_node::ALL );
00070
00071
00072
00073
00074 bool purge();
00075
00076
00077
00078
00079 bmrf_node_sptr seg_to_node(const bmrf_epi_seg_sptr& seg, int frame = -1) const;
00080
00081
00082 int num_frames() const;
00083
00084
00085
00086 vcl_set<int> frame_numbers() const;
00087
00088
00089
00090 int size( int frame = -1 );
00091
00092
00093 double probability();
00094
00095
00096 void prune_by_probability(double threshold, bool relative = false);
00097
00098
00099 void prune_by_gamma(double min_gamma, double max_gamma);
00100
00101
00102 void prune_directed();
00103
00104
00105 void set_epipole(const bmrf_epipole& epipole, int frame);
00106
00107
00108 const bmrf_epipole& epipole(int frame) const;
00109
00110
00111
00112 seg_node_map::const_iterator begin(int frame = -1) const;
00113
00114
00115
00116 seg_node_map::const_iterator end(int frame = -1) const;
00117
00118
00119 void b_write(vsl_b_ostream &os) const;
00120
00121
00122 void b_read(vsl_b_istream &is);
00123
00124
00125 short version() const;
00126
00127
00128 void print_summary(vcl_ostream &os) const;
00129
00130 private:
00131
00132
00133 seg_node_map node_from_seg_;
00134
00135
00136 frame_node_map nodes_from_frame_;
00137
00138
00139 vcl_vector<bmrf_epipole> epipoles_;
00140
00141 public:
00142 class iterator
00143 {
00144 public:
00145
00146 iterator( bmrf_network* network, bmrf_node_sptr node ) : network_(network), curr_node_(node) {}
00147
00148
00149 virtual ~iterator() {}
00150
00151
00152 iterator& operator++ () { next_node(); return *this; }
00153
00154
00155 bmrf_node_sptr operator -> () const { return curr_node_; }
00156
00157 bmrf_node_sptr operator * () const { return curr_node_; }
00158
00159
00160 bool operator == (const iterator& rhs) const { return rhs.curr_node_ == this->curr_node_; }
00161
00162
00163 bool operator != (const iterator& rhs) const { return rhs.curr_node_ != this->curr_node_; }
00164
00165 protected:
00166
00167 virtual void next_node() = 0;
00168
00169 bmrf_network* network_;
00170 bmrf_node_sptr curr_node_;
00171 };
00172
00173
00174 class depth_iterator : public iterator
00175 {
00176 public:
00177
00178 depth_iterator( bmrf_network* network, bmrf_node_sptr node ) : iterator(network, node){ visited_.insert(node); }
00179
00180 protected:
00181
00182 void next_node();
00183
00184 vcl_deque<bmrf_node_sptr> eval_queue_;
00185 vcl_set<bmrf_node_sptr> visited_;
00186 };
00187
00188
00189 class breadth_iterator : public iterator
00190 {
00191 public:
00192
00193 breadth_iterator( bmrf_network* network, bmrf_node_sptr node ) : iterator(network, node){ visited_.insert(node); }
00194
00195 protected:
00196
00197 void next_node();
00198
00199 vcl_deque<bmrf_node_sptr> eval_queue_;
00200 vcl_set<bmrf_node_sptr> visited_;
00201 };
00202
00203
00204 depth_iterator depth_begin(bmrf_node_sptr node) { return depth_iterator(this, node); }
00205
00206 depth_iterator depth_end() { return depth_iterator(this, NULL); }
00207
00208
00209 breadth_iterator breadth_begin(bmrf_node_sptr node) { return breadth_iterator(this, node); }
00210
00211 breadth_iterator breadth_end() { return breadth_iterator(this, NULL); }
00212 };
00213
00214
00215
00216 void vsl_b_write(vsl_b_ostream &os, const bmrf_network* n);
00217
00218
00219 void vsl_b_read(vsl_b_istream &is, bmrf_network* &n);
00220
00221
00222 void vsl_print_summary(vcl_ostream &os, const bmrf_network* n);
00223
00224
00225 #endif // bmrf_network_h_