contrib/gel/gmvl/gmvl_database.cxx
Go to the documentation of this file.
00001 // This is gel/gmvl/gmvl_database.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_database.h"
00010 #include <gmvl/gmvl_tag_node.h>
00011 #include <vcl_iostream.h>
00012 
00013 // specific node references
00014 
00015 void gmvl_database::add_node( const gmvl_node_sptr node)
00016 {
00017   nodecache_.add( node);
00018 }
00019 
00020 void gmvl_database::remove_node( const gmvl_node_sptr node)
00021 {
00022   nodecache_.remove( node);
00023   connectioncache_.rebuild();
00024 }
00025 
00026 // returns the first occurrence if there are multiple tags of the same type
00027 gmvl_node_sptr gmvl_database::find_tag( const vcl_string &string) const
00028 {
00029   vcl_vector<gmvl_node_sptr> tags= nodecache_.get( "gmvl_tag_node");
00030 
00031   for (unsigned int i=0; i< tags.size(); i++)
00032   {
00033     gmvl_node *ptr= tags[i].ptr();
00034 
00035     if (((gmvl_tag_node*)ptr)->get()== string)
00036       return tags[i];
00037   }
00038 
00039   return gmvl_node_sptr(0);
00040 }
00041 
00042 // specific connection references
00043 
00044 void gmvl_database::add_connection( const gmvl_node_sptr node1, const gmvl_node_sptr node2)
00045 {
00046   if (!nodecache_.cached( node1))
00047     add_node( node1);
00048 
00049   if (!nodecache_.cached( node2))
00050     add_node( node2);
00051 
00052   connectioncache_.add( node1, node2);
00053 }
00054 
00055 void gmvl_database::add_connections( const gmvl_node_sptr node1, vcl_vector<gmvl_node_sptr> nodes)
00056 {
00057   for (unsigned int i=0; i< nodes.size(); i++)
00058     add_connection( node1, nodes[i]);
00059 }
00060 
00061 // clever accessors
00062 vcl_vector<gmvl_node_sptr> gmvl_database::get_nodes( const vcl_string type) const
00063 {
00064   return nodecache_.get( type);
00065 }
00066 
00067 
00068 // one way connection
00069 vcl_vector<gmvl_node_sptr> gmvl_database::get_connected_nodes( const gmvl_node_sptr node) const
00070 {
00071   vcl_vector<int> c= connectioncache_.get_connected_nodes( node);
00072   vcl_vector<gmvl_node_sptr> l;
00073 
00074   for (unsigned int i=0; i< c.size(); i++)
00075   {
00076     l.push_back( nodecache_.get( c[i]));
00077   }
00078 
00079   return l;
00080 }
00081 
00082 vcl_vector<gmvl_node_sptr> gmvl_database::get_connected_nodes( const gmvl_node_sptr node, const vcl_string type) const
00083 {
00084   vcl_vector<gmvl_node_sptr> l= get_connected_nodes( node);
00085   vcl_vector<gmvl_node_sptr> m;
00086 
00087   for (unsigned int i=0; i< l.size(); i++)
00088   {
00089     if (l[i]->type()== type)
00090       m.push_back( l[i]);
00091   }
00092 
00093   return m;
00094 }
00095 
00096 // two way connection
00097 vcl_vector<gmvl_node_sptr> gmvl_database::get_connected_nodes( const gmvl_node_sptr node1,
00098                                                               const gmvl_node_sptr node2) const
00099 {
00100   vcl_vector<int> c= connectioncache_.get_connected_nodes( node1, node2);
00101   vcl_vector<gmvl_node_sptr> l;
00102 
00103   for (unsigned int i=0; i< c.size(); i++)
00104   {
00105     l.push_back( nodecache_.get( c[i]));
00106   }
00107 
00108   return l;
00109 }
00110 
00111 // three way connection
00112 vcl_vector<gmvl_node_sptr> gmvl_database::get_connected_nodes( const gmvl_node_sptr node1,
00113                                                               const gmvl_node_sptr node2,
00114                                                               const gmvl_node_sptr node3) const
00115 {
00116   vcl_vector<int> c= connectioncache_.get_connected_nodes( node1, node2, node3);
00117   vcl_vector<gmvl_node_sptr> l;
00118 
00119   for (unsigned int i=0; i< c.size(); i++)
00120   {
00121     l.push_back( nodecache_.get( c[i]));
00122   }
00123 
00124   return l;
00125 }
00126 
00127 // n way connection
00128 vcl_vector<gmvl_node_sptr> gmvl_database::get_connected_nodes( const vcl_vector<gmvl_node_sptr> nodes) const
00129 {
00130   vcl_vector<int> c= connectioncache_.get_connected_nodes( nodes);
00131   vcl_vector<gmvl_node_sptr> l;
00132 
00133   for (unsigned int i=0; i< c.size(); i++)
00134   {
00135     l.push_back( nodecache_.get( c[i]));
00136   }
00137 
00138   return l;
00139 }
00140 
00141 vcl_vector<gmvl_node_sptr> gmvl_database::get_connected_nodes( const vcl_vector<gmvl_node_sptr> nodes, const vcl_string type) const
00142 {
00143   vcl_vector<int> c= connectioncache_.get_connected_nodes( nodes);
00144   vcl_vector<gmvl_node_sptr> l;
00145 
00146   for (unsigned int i=0; i< c.size(); i++)
00147   {
00148     l.push_back( nodecache_.get( c[i]));
00149   }
00150 
00151   vcl_vector<gmvl_node_sptr> m;
00152 
00153   for (unsigned int i=0; i< l.size(); i++)
00154   {
00155     if (l[i]->type()== type)
00156       m.push_back( l[i]);
00157   }
00158 
00159   return m;
00160 }
00161 
00162 
00163 // input / output
00164 
00165 vcl_ostream &operator<<( vcl_ostream &os, const gmvl_database &db)
00166 {
00167   return
00168   os << "gmvl_database:\n  nodes:\n    " << db.nodecache_
00169      << "\n  connections:\n    " << db.connectioncache_ << '\n';
00170 }