1 /*---------------------------------------------------------------------------*\
2 * OpenSG NURBS Library *
5 * Copyright (C) 2001-2006 by the University of Bonn, Computer Graphics Group*
7 * http://cg.cs.uni-bonn.de/ *
9 * contact: edhellon@cs.uni-bonn.de, guthe@cs.uni-bonn.de, rk@cs.uni-bonn.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
41 // default constructor
42 template <typename T0, typename T1>
43 DirectedGraph<T0, T1>::DirectedGraph() :
52 template <typename T0, typename T1>
53 int DirectedGraph<T0, T1>::AddNode( T0 &n )
57 nodes.push_back( nn );
58 return Int32(nodes.size()) - 1;
61 // add a new (possibly directed) edge
62 // returns edge index or -1
63 template <typename T0, typename T1>
64 int DirectedGraph<T0, T1>::AddEdge( T1 &t, int from, int to, bool direction)
66 if ( from < 0 || from >= int(nodes.size()) ||
67 to < 0 || to >= int(nodes.size()) ) return -1;
74 e.direction = direction;
75 e.valid = true; //DEBUG
77 eidx = Int32(edges.size()) - 1;
79 nodes[ from ].edges.push_back( eidx );
80 nodes[ to ].edges.push_back( eidx );
84 // delete edge specified by index
85 template <typename T0, typename T1>
86 int DirectedGraph<T0, T1>::DeleteEdge( int edgeidx )
88 if ( edgeidx < 0 || edgeidx >= int(edges.size()) )
91 // note: we don't need to actually erase from here
92 // edges.erase( edgeidx );
94 edges[ edgeidx ].valid = false;
96 DirectedNode<T0> &fromnode = nodes[ edges [ edgeidx ].from ];
97 DCTPivector::iterator nee = fromnode.edges.end();
98 DCTPivector::iterator i;
99 for( i = fromnode.edges.begin(); i != nee; ++i )
100 if ( *i == edgeidx ) {
101 fromnode.edges.erase( i );
105 DirectedNode<T0> &tonode = nodes[ edges [ edgeidx ].to ];
106 nee = tonode.edges.end();
107 for( i = tonode.edges.begin(); i != nee; ++i )
108 if ( *i == edgeidx ) {
109 tonode.edges.erase( i );
119 template <typename T0, typename T1>
120 DCTPivector & DirectedGraph<T0, T1>::getEdges( int n ) // get all edges (indexes) from a node
126 template <typename T0, typename T1>
127 DirectedNode<T0>& DirectedGraph<T0, T1>::getNode( int nodeindex, int &error )
130 if ( nodeindex < 0 || nodeindex >= nodes.size() ) {
134 return &nodes[ nodeindex ] ;
138 template <typename T0, typename T1>
139 DirectedEdge<T1>& DirectedGraph<T0, T1>::getEdge( int edgeindex, int &error )
142 if ( edgeindex < 0 || edgeindex >= edges.size() ) {
146 return &edges[ edgeindex ] ;
149 // get one edge's direction
150 template <typename T0, typename T1>
151 bool DirectedGraph<T0, T1>::getEdgeDirection( int edgeindex, int &error )
154 if ( edgeindex < 0 || edgeindex >= edges.size() ) {
159 return edges[ edgeindex ].orientation;
162 // set the direction of an edge
163 template <typename T0, typename T1>
164 int DirectedGraph<T0, T1>::setEdgeDirection( int edgeindex, int to )
166 if ( edgeindex < 0 || edgeindex >= int(edges.size()) ) return -1;
168 if ( edges[ edgeindex ].from != to && edges[ edgeindex ].to != to )
170 if ( edges[ edgeindex ].to != to ) {
171 int tmp = edges[ edgeindex ].to;
172 edges[ edgeindex ].to = to;
173 edges[ edgeindex ].from = tmp;
175 edges[ edgeindex ].direction = true;
179 // change (invert) the direction of an edge
180 template <typename T0, typename T1>
181 int DirectedGraph<T0, T1>::changeEdgeDirection( int edgeindex )
183 if ( edgeindex < 0 || edgeindex >= edges.size() )
185 if ( !edges[ edgeindex ].orientation )
188 int tmp = edges[ edgeindex ].to;
189 edges[ edgeindex ].to = edges[ edgeindex ].from;
190 edges[ edgeindex ].from = tmp;
195 template <typename T0, typename T1>
196 bool DirectedGraph<T0, T1>::isInvalid( void )
201 template <typename T0, typename T1>
202 int DirectedGraph<T0, T1>::FindNode( T0 &nodeinfo )
204 for( unsigned int i = 0; i < nodes.size( ); ++i )
206 if( nodes[ i ].nodeinfo == nodeinfo ) return i;
212 template <typename T0, typename T1>
213 int DirectedGraph<T0, T1>::FindEdge( int from, int to )
215 for( unsigned int i = 0; i < edges.size( ); ++i )
217 // std::cerr << "checking edge from " << edges[ i ].from << " to " << edges[ i ].to << std::endl;
218 if( ( edges[ i ].from == from ) &&
219 ( edges[ i ].to == to ) ) return i;
220 if( ( edges[ i ].from == to ) &&
221 ( edges[ i ].to == from ) ) return i;