TAPs 0.7.7.3
TAPsDirectedGraph.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsDirectedGraph.cpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (09/12/2005)
00009 UPDATE          (09/09/2010)
00010 ******************************************************************************/
00011 #include "TAPsDirectedGraph.hpp"
00012 // Using Inclusion Model (i.e. definitions are included in declarations)
00013 //                       (this name.cpp is included in name.hpp)
00014 // Each friend is defined directly inside its declaration.
00015 
00016 BEGIN_NAMESPACE_TAPs__DS
00017 //=============================================================================
00018 // Constructors and Destructor
00019 //-----------------------------------------------------------------------------
00020 template <typename T>
00021 DirectedGraph<T>::DirectedGraph ()
00022 {}
00023 //-----------------------------------------------------------------------------
00024 template <typename T>
00025 DirectedGraph<T>::~DirectedGraph ()
00026 {}
00027 //-----------------------------------------------------------------------------
00028 //=============================================================================
00029 // Graph Operations
00030 //-----------------------------------------------------------------------------
00031 // Create a graph node
00032 template <typename T>
00033 void DirectedGraph<T>::NewGraphNode ( T & t )
00034 {
00035     m_vNodes.push_back( node( t, static_cast<int>(m_vNodes.size()) ) );
00036 
00037     #if defined(__gl_h_) || defined(__GL_H__)
00038     if ( m_ListOfNodePos.size() > 0 ) {
00039         m_ListOfNodePos.push_back( m_ListOfNodePos[m_ListOfNodePos.size()-1] + Vector3<float>( 0,-1,0 ) );
00040     }
00041     else {
00042         m_ListOfNodePos.push_back( Vector3<float>(0,0,0) );
00043     }
00044     #endif
00045 }
00046 //-----------------------------------------------------------------------------
00047 // Direct node 1 to node 2
00048 template <typename T>
00049 void DirectedGraph<T>::SetDirection ( unsigned int n1, unsigned int n2 )
00050 {
00051     m_vNodes[n1].RefToListOfOutPaths().push_back( GraphNode<T>::Edge(n2) );
00052     m_vNodes[n2].RefToListOfInPaths().push_back( GraphNode<T>::Edge(n1) );
00053 }
00054 //-----------------------------------------------------------------------------
00055 // Bidirect node 1 and node 2
00056 template <typename T>
00057 void DirectedGraph<T>::SetBidirection ( unsigned int n1, unsigned int n2 )
00058 {
00059     m_vNodes[n1].RefToListOfOutPaths().push_back( GraphNode<T>::Edge(n2) );
00060     m_vNodes[n2].RefToListOfOutPaths().push_back( GraphNode<T>::Edge(n1) );
00061     m_vNodes[n1].RefToListOfInPaths().push_back( GraphNode<T>::Edge(n2) );
00062     m_vNodes[n2].RefToListOfInPaths().push_back( GraphNode<T>::Edge(n1) );
00063 }
00064 //-----------------------------------------------------------------------------
00065 template <typename T>
00066 GraphNode<T> const & DirectedGraph<T>::RefNodeNumber ( unsigned int number ) const
00067 {
00068     assert( 0 <= number && number < GetNumOfNodes() );
00069     return m_vNodes[number];
00070 }
00071 //-----------------------------------------------------------------------------
00072 template <typename T>
00073 GraphNode<T> & DirectedGraph<T>::RefNodeNumber ( unsigned int number )
00074 {
00075     assert( 0 <= number && number < GetNumOfNodes() );
00076     return m_vNodes[number];
00077 }
00078 //-----------------------------------------------------------------------------
00079 template <typename T>
00080 T const & DirectedGraph<T>::RefDataInNodeNumber ( unsigned int number ) const
00081 {
00082     assert( 0 <= number && number < GetNumOfNodes() );
00083     return m_vNodes[number].RefValue();
00084 }
00085 //-----------------------------------------------------------------------------
00086 template <typename T>
00087 T & DirectedGraph<T>::RefDataInNodeNumber ( unsigned int number )
00088 {
00089     assert( 0 <= number && number < GetNumOfNodes() );
00090     return m_vNodes[number].RefValue();
00091 }
00092 //-----------------------------------------------------------------------------
00093 #if defined(__gl_h_) || defined(__GL_H__)
00094 //=============================================================================
00095 // DrawByOpenGL
00096 //-----------------------------------------------------------------------------
00097 template <typename T>
00098 void DirectedGraph<T>::Draw () const
00099 {
00100     glPushAttrib( GL_ALL_ATTRIB_BITS );
00101     const_iterator it = m_vNodes.begin();
00102     int n = 0;
00103     glLineWidth( 1 );
00104     glDisable( GL_LIGHTING );
00105     while ( it != m_vNodes.end() ) {
00106 
00107         // Draw the current node
00108         glPushMatrix();
00109             glColor3f( 0.25, 0.25, 0.50 );
00110             glTranslatef( m_ListOfNodePos[n][0], m_ListOfNodePos[n][1], m_ListOfNodePos[n][2] );
00111             it->Draw();
00112         glPopMatrix();
00113 
00114         glBegin( GL_LINES );
00115             /*
00116             // Draw all in-paths of the current node
00117             for ( unsigned int e = 0; e < m_vNodes[n].GetNumOfInPaths(); ++e ) {
00118                 glColor3f( 0, 0, 1 );
00119                 glVertex3fv( m_ListOfNodePos[n].GetDataFloat() );
00120                 glColor3f( 0, 1, 0 );
00121                 glVertex3fv( m_ListOfNodePos[m_vNodes[n].GetNodeIdOfInPathsNumber(e)].GetDataFloat() );
00122             }
00123             //*/
00124             // Draw all out-paths of the current node
00125             static float offset = 0.02f;
00126             int id;
00127             for ( unsigned int e = 0; e < m_vNodes[n].GetNumOfOutPaths(); ++e ) {
00128                 id = m_vNodes[n].GetNodeIdOfOutPathsNumber(e);
00129                 glPushMatrix();
00130                 if ( m_vNodes[n].GetID() < RefNodeNumber( id ).GetID() ) {
00131                     glColor3f( 1, 0, 1 );
00132                     glVertex3f( m_ListOfNodePos[n][0]-offset, m_ListOfNodePos[n][1], m_ListOfNodePos[n][2] );
00133                     glColor3f( 1, 1, 0 );
00134                     glVertex3f( m_ListOfNodePos[id][0]-offset, m_ListOfNodePos[id][1], m_ListOfNodePos[id][2] );
00135                 }
00136                 else {
00137                     glColor3f( 1, 0, 1 );
00138                     glVertex3f( m_ListOfNodePos[n][0]+offset, m_ListOfNodePos[n][1], m_ListOfNodePos[n][2] );
00139                     glColor3f( 1, 1, 0 );
00140                     glVertex3f( m_ListOfNodePos[id][0]+offset, m_ListOfNodePos[id][1], m_ListOfNodePos[id][2] );
00141                 }
00142                 glPopMatrix();
00143             }
00144         glEnd();
00145         ++it;
00146         ++n;
00147     }
00148     glPopAttrib();
00149 }
00150 //-----------------------------------------------------------------------------
00151 #endif
00152 //=============================================================================
00153 END_NAMESPACE_TAPs__DS
00154 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00155 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines