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

bcal_camera_graph.h

Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief a graph with source
00004 // \author Kongbin Kang (Kongbin_Kang@Brown.edu)
00005 // \date   4/3/2003
00006 
00007 #ifndef AFX_CAMERAGRAPH_H__8810B4C3_E6C7_42CE_8C7F_D9F8F201F6F6__INCLUDED_
00008 #define AFX_CAMERAGRAPH_H__8810B4C3_E6C7_42CE_8C7F_D9F8F201F6F6__INCLUDED_
00009 
00010 #if defined(_MSC_VER) && ( _MSC_VER > 1000 )
00011 #pragma once
00012 #endif // _MSC_VER > 1000
00013 
00014 #include <vcl_cassert.h>
00015 #include <vcl_iostream.h>
00016 #include <vcl_vector.h>
00017 
00018 template<class S, class V, class E>
00019 class bcal_camera_graph
00020 {
00021  protected:
00022   struct vertex_node
00023   {
00024    public:
00025     int id_;
00026     V *v_; // distinguish source with other node
00027     S *s_;
00028    public:
00029     vertex_node(int id)
00030     {
00031       id_ = id;
00032       v_ = 0;
00033       s_ = 0;
00034     }
00035 
00036     ~vertex_node()
00037     {
00038       delete v_; v_ = 0;
00039       delete s_; s_ = 0;
00040     }
00041   };
00042 
00043 
00044   struct edge_node
00045   {
00046     vertex_node* v_; // recode which vertex attached with this edge
00047     E* e_;
00048    public:
00049     edge_node(vertex_node* v) { v_ = v; e_ = new E;}
00050     virtual ~edge_node() { delete e_; }
00051   };
00052 
00053  protected:
00054   int init_graph()
00055   {
00056     source_ = 0;
00057     num_vertice_ = 0;
00058     // add a source
00059     add_vertex(0);
00060 
00061     source_ = vertice_[0]->s_;
00062     return 0;
00063   }
00064 
00065   vertex_node* malloc_new_node(bool is_source)
00066   {
00067     vertex_node *v = new vertex_node(num_vertice_++);
00068 
00069     if (is_source){
00070       v->s_ = new S;
00071       v->v_ = 0;
00072     }
00073     else{
00074       v->s_ = 0;
00075       v->v_ = new V;
00076     }
00077 
00078     return v;
00079   }
00080 
00081  public:
00082 #if 0
00083   class iterator
00084   {
00085    public:
00086     iterator() : ptr_(0), pos_(0) {}
00087     iterator(vcl_vector<vertex_node*> *p, int pos = 0) : ptr_(p), pos_(pos) {}
00088     iterator(const iterator& x) : ptr_( x.ptr_ ), pos_(x.pos_) {}
00089     V& operator*() const {return *((*ptr_)[pos_]->v_); }
00090     V* operator->() const {return &(* *this); }
00091 
00092     iterator& operator=(iterator const& x)
00093     {
00094       pos_ = x.pos_;
00095       ptr_ = x.ptr_;
00096       return *this;
00097     }
00098 
00099     iterator& operator++()
00100     {
00101       increment();
00102       return *this;
00103     }
00104 
00105     iterator operator++(int)
00106     {
00107       iterator t = *this;
00108       increment();
00109       return t;
00110     }
00111 
00112     iterator& operator--()
00113     {
00114       decrement();
00115       return *this;
00116     }
00117 
00118     iterator operator--(int )
00119     {
00120       iterator t = *this;
00121       decrement();
00122       return t;
00123     }
00124 
00125     bool operator==(const iterator& x) const { return ptr_ == x.ptr_ && pos_ = x.pos_; }
00126 
00127     bool operator!=(const iterator& x) const { return !(*this == x); }
00128 
00129     void decrement() { if (pos_>=0)  pos_--; }
00130     void increment() { pos_++; }
00131 
00132     vertex_node* node() const { return (*ptr_)[pos]; }
00133 
00134     int get_vertex_id() const { return (*ptr_)[pos_]->id_; }
00135 
00136    protected:
00137     vcl_vector<vertex_node*>* ptr_;
00138     int pos_;
00139   };
00140 #endif // 0
00141 
00142  public: // constructor and destructor
00143 
00144   bcal_camera_graph() { init_graph(); }
00145 
00146   virtual ~bcal_camera_graph() { erase_graph(); }
00147 
00148  public: // operations
00149   S* get_source() {  return source_;}
00150   int get_source_id() { return 0;}
00151 
00152   // get vertex at position of
00153   V* get_vertex_from_pos(int pos)
00154   {
00155     assert(num_vertice_ > pos);
00156     return vertice_[pos + 1]->v_;
00157   }
00158 
00159   V* get_vertex_from_id(int id)
00160   {
00161     assert(id > 0 && id <= num_vertice_);
00162     return get_vertex_from_pos(id - 1);
00163   }
00164 
00165   // return a id of vertex at position pos;
00166   int get_vertex_id(int pos)
00167   {
00168     assert(pos >= 0 && pos < num_vertice_);
00169     return pos + 1;
00170   }
00171 
00172   // add a new vertex and edge from the neighbour v to it
00173   // it return the position of the new vertex in the array
00174   int add_vertex(int neighbour = 0)
00175   {
00176     // currently only edge from source can be added
00177     assert(neighbour == 0);
00178 
00179     // the neighbour should exist already
00180     assert(neighbour <= num_vertice_);
00181 
00182     vertex_node *v;
00183     if (source_){
00184       v = malloc_new_node(false); // allocate an ordinary vertex
00185     }
00186     else
00187       v = malloc_new_node(true); // allocate a source vertex
00188 
00189     vertice_.push_back(v);
00190 
00191     // add a neighbour list into edges
00192     vcl_vector<edge_node*> *neighbour_list = new vcl_vector<edge_node*>;
00193     edges_.push_back(neighbour_list);
00194 
00195     // update old adjacent neighbour list
00196     if (source_){ // if not source vertex is added
00197       edge_node *e = new edge_node(v);
00198       edges_[neighbour]->push_back(e);
00199     }
00200 
00201     return v->id_;
00202   }
00203 
00204   // id is the same of it is position in array
00205   inline V* get_vertex(int i)
00206   {
00207     assert(i>0);
00208     return vertice_[i]->v_;
00209   }
00210 
00211 #if 0
00212   // return a iterator pointer to first vertex
00213   iterator begin() { return iterator(&vertice_, 1); }
00214 #endif // 0
00215 
00216   // get edge from v1 to v2
00217   inline E* get_edge(int v1, int v2)
00218   {
00219     assert(v1 == 0 && v2 <= num_vertice_); // only from souce to camera is avaible
00220     vcl_vector<edge_node*>* plist = edges_[v1];
00221     for (unsigned int i=0; i < plist->size(); i++){
00222       edge_node *e = (*plist)[i];
00223       vertex_node *v = e->v_;
00224       assert(v != 0); // no single edge exist
00225 
00226       if (v->id_ == v2){
00227         return e->e_;
00228       }
00229       else
00230         continue;
00231     }
00232 
00233     return 0; // cannot find edge
00234   }
00235 
00236   int num_vertice() { return vertice_.size() - 1; }
00237 
00238 
00239   // for debug
00240   void print(vcl_ostream& out = vcl_cerr)
00241   {
00242     out<<"print graph\n";
00243     for (int i=0; i<num_vertice_; i++){
00244       out<<"vertex id is: "<<vertice_[i]->id_<<"\t v is: "
00245          <<vertice_[i]->v_ <<"\ts is: "<<vertice_[i]->s_<<'\n';
00246 
00247       int num_neighbours = edges_[i]->size();
00248       for (int j=0; j< num_neighbours; j++){
00249         (*edges_[i])[j]->e_->print(out);
00250       }
00251     }
00252 
00253     //print edge
00254   }
00255 
00256   int erase_graph()
00257   {
00258     num_vertice_ = vertice_.size();
00259 
00260     if (num_vertice_ == 0) // empty graph
00261       return 0; // no error
00262 
00263     for (int i=0; i<num_vertice_; i++){
00264       // delete each vertex
00265       delete vertice_[i]; vertice_[i] = 0;
00266 
00267       // delete each edge node
00268       if (edges_[i])
00269       {
00270         int list_length = edges_[i]->size();
00271         for (int j=0; j<list_length; j++)
00272           delete (*(edges_[i]))[j];
00273 
00274         delete edges_[i];
00275       }
00276       edges_[i] = 0;
00277     }
00278     vertice_.clear();
00279     edges_.clear();
00280 
00281     source_ = 0;
00282 
00283     return 0; // no error
00284   }
00285 
00286  private:
00287   vcl_vector<vertex_node*> vertice_;
00288   vcl_vector<vcl_vector<edge_node*>* > edges_; // adjacent neigbhour list
00289   S* source_;
00290   int num_vertice_;
00291 };
00292 
00293 #endif // AFX_CAMERAGRAPH_H__8810B4C3_E6C7_42CE_8C7F_D9F8F201F6F6__INCLUDED_

Generated on Thu Jan 10 14:53:50 2008 for contrib/brl/bmvl/bcal by  doxygen 1.4.4