00001 /*!@file Beobot/Graph.H basic graph class can be both directed 00002 and undirected */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Christian Siagian <siagian@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/Graph.H $ 00036 // $Id $ 00037 // 00038 00039 #ifndef BEOBOT_GRAPH_H_DEFINED 00040 #define BEOBOT_GRAPH_H_DEFINED 00041 00042 #include "Beobot/Node.H" 00043 #include "Beobot/Edge.H" 00044 #include "Image/Image.H" 00045 00046 //! basic graph class 00047 //! has all the access functions and shortest path 00048 //! no input/output file function because usually the file 00049 //! has to be filled with extra information 00050 class Graph 00051 { 00052 public: 00053 // ###################################################################### 00054 //! @name Constructor, assigment and destructor 00055 //@{ 00056 00057 //! Constructor: generate a blank graph 00058 Graph(); 00059 00060 //! Constructor: generate a graph with edges 00061 Graph(std::vector<rutz::shared_ptr<Node> > nodes, 00062 std::vector<rutz::shared_ptr<Edge> > edges); 00063 00064 //! Destructor 00065 ~Graph(); 00066 00067 //! add edges and nodes 00068 void addNode(rutz::shared_ptr<Node>); 00069 void addEdge(rutz::shared_ptr<Edge>); 00070 00071 //@} 00072 00073 // ###################################################################### 00074 //! @name Access functions 00075 //@{ 00076 00077 //! get node and edge size 00078 inline uint getNumNode(); 00079 inline uint getNumEdge(); 00080 00081 //! get node or edge of the passed in index 00082 inline rutz::shared_ptr<Node> getNode(uint index); 00083 inline rutz::shared_ptr<Edge> getEdge(uint index); 00084 00085 //@} 00086 00087 // ###################################################################### 00088 //! @name Compute functions 00089 //@{ 00090 00091 //! compute the adjecency list 00092 void computeAdjecencyList(); 00093 00094 //! compute all the shortcuts for shortest-distance related operations 00095 void computeDistances(); 00096 00097 //! get the shortest distance from node A to node B 00098 //! using Dijkstra shortest-path algorithm 00099 //! for the directed graph 00100 float getDirectedDistance 00101 (uint a, uint b, std::vector<uint> &edges); 00102 float getDirectedDistance(uint a, uint b); 00103 00104 //! get the shortest distance from node A to node B 00105 //! using Dijkstra shortest-path algorithm 00106 //! for the undirected graph 00107 float getUndirectedDistance 00108 (uint a, uint b, std::vector<uint> &edges); 00109 float getUndirectedDistance(uint a, uint b); 00110 00111 //! get the maximum distance of any 2 points in the map 00112 //! return -1.0F if the graph is unconnected 00113 //! for the directed graph 00114 float getMaxDirectedDistance(); 00115 00116 //! get the maximum distance of any 2 points in the map 00117 //! return -1.0F if the graph is unconnected 00118 //! for undirected graph 00119 float getMaxUndirectedDistance(); 00120 00121 //! get the directed path from node A to Note B 00122 std::vector<uint> getDirectedPath(uint a, uint b); 00123 00124 //! get the undirected path from node A to Note B 00125 std::vector<uint> getUndirectedPath(uint a, uint b); 00126 00127 //! get all the edge that connects to this node 00128 std::vector<uint> getEdges(rutz::shared_ptr<Node> n); 00129 00130 //! get the angle (in radians) between two edges 00131 //! they two edges do not have to have a common node 00132 //! angles are intuitive from the tip of 00133 //! the incoming (e1) edge perspective 00134 //! 0 degrees/radians means continue the same direction 00135 //! 180/-180 degrees or M_PI/-M_PI means turn around 00136 //! positive angle to the right, negative angle to the left 00137 float getAngle(rutz::shared_ptr<Edge> e1, rutz::shared_ptr<Edge> e2); 00138 00139 //@} 00140 00141 private: 00142 00143 //! all the nodes and edge information 00144 std::vector<rutz::shared_ptr<Node> > itsNodes; 00145 std::vector<rutz::shared_ptr<Edge> > itsEdges; 00146 00147 std::vector<std::vector<uint> > itsDirectedAdjecencyList; 00148 std::vector<std::vector<float> > itsDirectedAdjecencyCostList; 00149 std::vector<std::vector<uint> > itsDirectedAdjecencyInEdgeList; 00150 std::vector<std::vector<uint> > itsDirectedAdjecencyOutEdgeList; 00151 00152 std::vector<std::vector<uint> > itsUndirectedAdjecencyList; 00153 std::vector<std::vector<float> > itsUndirectedAdjecencyCostList; 00154 std::vector<std::vector<uint> > itsUndirectedAdjecencyEdgeList; 00155 00156 //! all the distances (pre-computed if 'computeDistances' is called) 00157 Image<float> itsDirectedDistanceMatrix; 00158 Image<float> itsUndirectedDistanceMatrix; 00159 00160 std::vector<std::vector<std::vector<uint> > > itsDirectedPathList; 00161 std::vector<std::vector<std::vector<uint> > > itsUndirectedPathList; 00162 00163 bool itsDistanceMatrixComputed; 00164 }; 00165 00166 // ###################################################################### 00167 // Implementation for Node inline functions 00168 // ###################################################################### 00169 00170 inline uint Graph::getNumNode() 00171 { 00172 return itsNodes.size(); 00173 } 00174 00175 inline uint Graph::getNumEdge() 00176 { 00177 return itsEdges.size(); 00178 } 00179 00180 inline rutz::shared_ptr<Node> Graph::getNode(uint index) 00181 { 00182 ASSERT(index < itsNodes.size()); 00183 return itsNodes[index]; 00184 } 00185 00186 inline rutz::shared_ptr<Edge> Graph::getEdge(uint index) 00187 { 00188 ASSERT(index < itsEdges.size()); 00189 return itsEdges[index]; 00190 } 00191 00192 #endif 00193 00194 // ###################################################################### 00195 /* So things look consistent in everyone's emacs... */ 00196 /* Local Variables: */ 00197 /* indent-tabs-mode: nil */ 00198 /* End: */