contrib/gel/gmvl/gmvl_connection_cache.cxx
Go to the documentation of this file.
00001 // This is gel/gmvl/gmvl_connection_cache.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author crossge@crd.ge.com
00008 
00009 #include "gmvl_connection_cache.h"
00010 
00011 #include <vcl_cassert.h>
00012 #include <vcl_iostream.h>
00013 #include <vnl/vnl_math.h>
00014 
00015 // constructors / destructors
00016 
00017 gmvl_connection_cache::gmvl_connection_cache()
00018 {
00019 }
00020 
00021 gmvl_connection_cache::~gmvl_connection_cache()
00022 {
00023 }
00024 
00025 // simple accessors
00026 
00027 void gmvl_connection_cache::add(const gmvl_node_sptr node1, const gmvl_node_sptr node2)
00028 {
00029   if (node1.ptr()!= node2.ptr())
00030   {
00031     gmvl_connection_sptr c= new gmvl_connection(node1, node2);
00032 
00033     connections_.push_back(c);
00034 
00035     // and add to the cache
00036     assert (node1->ref_>= 0);
00037     assert (node2->ref_>= 0);
00038 
00039     if (node1->ref_>= int(cache_.size())) cache_.resize(node1->ref_+1);
00040     if (node2->ref_>= int(cache_.size())) cache_.resize(node2->ref_+1);
00041 
00042     unsigned int biggest = vnl_math_max(node1->ref_, node2->ref_);
00043 
00044     if (biggest>= cachebool_.rows())
00045     {
00046       cachebool_.enlarge((biggest+1)*2, (biggest+1)*2);
00047     }
00048 
00049     cache_[node1->ref_].push_back(node2->ref_);
00050     cache_[node2->ref_].push_back(node1->ref_);
00051 
00052     cachebool_.put(node1->ref_, node2->ref_, true);
00053     cachebool_.put(node2->ref_, node1->ref_, true);
00054   }
00055 }
00056 
00057 
00058 // clever accessors
00059 
00060 vcl_vector<int> gmvl_connection_cache::get_connected_nodes(const gmvl_node_sptr node1,
00061                                                            const gmvl_node_sptr node2) const
00062 {
00063   vcl_vector<int> c= get_connected_nodes(node1);
00064   vcl_vector<int> d;
00065 
00066   for (unsigned int i=0; i< c.size(); ++i)
00067     if (cachebool_(node2->ref_,c[i]))
00068       d.push_back(c[i]);
00069 
00070   return d;
00071 }
00072 
00073 vcl_vector<int> gmvl_connection_cache::get_connected_nodes(const gmvl_node_sptr node1,
00074                                                            const gmvl_node_sptr node2,
00075                                                            const gmvl_node_sptr node3) const
00076 {
00077   vcl_vector<int> c= get_connected_nodes(node1);
00078   vcl_vector<int> d;
00079 
00080   for (unsigned int i=0; i< c.size(); ++i)
00081     if (cachebool_(node2->ref_,c[i]) &&
00082         cachebool_(node3->ref_,c[i]))
00083       d.push_back(c[i]);
00084 
00085   return d;
00086 }
00087 
00088 vcl_vector<int> gmvl_connection_cache::get_connected_nodes(const vcl_vector<gmvl_node_sptr> nodes) const
00089 {
00090   vcl_vector<int> c= get_connected_nodes(nodes[0]);
00091   vcl_vector<int> d;
00092 
00093   for (unsigned int i=0; i< c.size(); ++i)
00094   {
00095     bool ok= true;
00096 
00097     for (unsigned int j=1; j< nodes.size() && ok; ++j)
00098     {
00099       if (!cachebool_(nodes[j]->ref_,c[i]))
00100         ok= false;
00101     }
00102 
00103     if (ok)
00104       d.push_back(c[i]);
00105   }
00106 
00107   return d;
00108 }
00109 
00110 // house-keeping
00111 
00112 void gmvl_connection_cache::rebuild()
00113 {
00114   cache_.clear();
00115   assert(false);
00116 
00117   for (unsigned int i=0; i< connections_.size(); ++i)
00118   {
00119     gmvl_node_sptr node1= connections_[i]->get_node1();
00120     gmvl_node_sptr node2= connections_[i]->get_node2();
00121 
00122     assert (node1->ref_>= 0);
00123     assert (node2->ref_>= 0);
00124 
00125     if (node1->ref_>= int(cache_.size())) cache_.resize(node1->ref_+1);
00126     if (node2->ref_>= int(cache_.size())) cache_.resize(node2->ref_+1);
00127 
00128     unsigned int biggest= vnl_math_max(node1->ref_, node2->ref_);
00129 
00130     if (biggest>= cachebool_.rows())
00131     {
00132       vbl_bit_array_2d temp(biggest+1, biggest+1, false);
00133 
00134       for (unsigned int ci=0; ci< cachebool_.rows(); ++ci)
00135         for (unsigned int cj=0; cj< cachebool_.cols(); ++cj)
00136           temp.put(ci,cj, cachebool_(ci,cj));
00137 
00138       cachebool_= temp;
00139     }
00140 
00141     cache_[node1->ref_].push_back(node2->ref_);
00142     cache_[node2->ref_].push_back(node1->ref_);
00143 
00144     cachebool_.put(node1->ref_, node2->ref_, true);
00145     cachebool_.put(node2->ref_, node1->ref_, true);
00146   }
00147 }
00148 
00149 // input / output
00150 
00151 vcl_ostream &operator<<(vcl_ostream &os, const gmvl_connection_cache &c)
00152 {
00153 #if 0
00154   for (int i=0; i< c.connections_.size(); i++)
00155     os << *c.connections_[i];
00156 #endif
00157   for (unsigned int i=0; i< c.cache_.size(); ++i)
00158   {
00159     os << '<' << i << " - ";
00160 
00161     for (unsigned int j=0; j< c.cache_[i].size(); ++j)
00162     {
00163       os << c.cache_[i][j];
00164       if (j+1 != c.cache_[i].size())
00165         os << ", ";
00166     }
00167     os << '>';
00168   }
00169 
00170   return os;
00171 }