changed: gcc8 base update
[opensg.git] / Source / System / NodeCores / Drawables / Nurbs / Internal / OSGDirectedGraph.inl
blobb9cc8234b86da426358791ce329324d8e9b364dd
1 /*---------------------------------------------------------------------------*\
2  *                           OpenSG NURBS Library                            *
3  *                                                                           *
4  *                                                                           *
5  * Copyright (C) 2001-2006 by the University of Bonn, Computer Graphics Group*
6  *                                                                           *
7  *                         http://cg.cs.uni-bonn.de/                         *
8  *                                                                           *
9  * contact: edhellon@cs.uni-bonn.de, guthe@cs.uni-bonn.de, rk@cs.uni-bonn.de *
10  *                                                                           *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13  *                                License                                    *
14  *                                                                           *
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.                               *
18  *                                                                           *
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.                          *
23  *                                                                           *
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.                 *
27  *                                                                           *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30  *                                Changes                                    *
31  *                                                                           *
32  *                                                                           *
33  *                                                                           *
34  *                                                                           *
35  *                                                                           *
36  *                                                                           *
37 \*---------------------------------------------------------------------------*/
39 OSG_BEGIN_NAMESPACE
41 // default constructor
42 template <typename T0, typename T1>
43 DirectedGraph<T0, T1>::DirectedGraph() :
44     nodes  (    ),
45     edges  (    ),
46     invalid(true)
50 // add a new node
51 // returns node index 
52 template <typename T0, typename T1>
53 int DirectedGraph<T0, T1>::AddNode( T0 &n )
55   DirectedNode<T0> nn;
56   nn.nodeinfo = n;
57   nodes.push_back( nn );
58   return Int32(nodes.size()) - 1;
59
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;
69   DirectedEdge<T1> e;
70   int eidx;
71   e.edgeinfo = t;
72   e.from = from;
73   e.to = to;
74   e.direction = direction;
75   e.valid = true; //DEBUG
76   edges.push_back( e );
77   eidx = Int32(edges.size()) - 1;
79   nodes[ from ].edges.push_back( eidx );
80   nodes[ to ].edges.push_back( eidx );  
81   return 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()) )
89     return -1;
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 );
102       break;
103     }
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 );
110       break;
111     }
113   return 0;
119 template <typename T0, typename T1>
120 DCTPivector & DirectedGraph<T0, T1>::getEdges( int n ) // get all edges (indexes) from a node
122   return &nodes.edges;
125 // get one node
126 template <typename T0, typename T1>
127 DirectedNode<T0>& DirectedGraph<T0, T1>::getNode( int nodeindex, int &error )
129   error = 0;
130   if ( nodeindex < 0 || nodeindex >= nodes.size() ) {
131     error = -1;
132     return NULL;
133   }
134   return &nodes[ nodeindex ] ;
137 // get one edge
138 template <typename T0, typename T1>
139 DirectedEdge<T1>& DirectedGraph<T0, T1>::getEdge( int edgeindex, int &error )
141   error = 0;
142   if ( edgeindex < 0 || edgeindex >= edges.size() ) {
143     error = -1;
144     return NULL;
145   }
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 )
153   error = 0;
154   if ( edgeindex < 0 || edgeindex >= edges.size() ) {
155     error = -1;
156     return false;
157   }
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 )
169     return -1;
170   if ( edges[ edgeindex ].to != to ) {
171     int tmp = edges[ edgeindex ].to;
172     edges[ edgeindex ].to = to;
173     edges[ edgeindex ].from = tmp;
174   }
175   edges[ edgeindex ].direction = true;
176   return 0;
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() )
184     return -1;
185   if ( !edges[ edgeindex ].orientation )
186     return -1;
188   int tmp = edges[ edgeindex ].to;
189   edges[ edgeindex ].to = edges[ edgeindex ].from;
190   edges[ edgeindex ].from = tmp;
192   return 0;
195 template <typename T0, typename T1>
196 bool DirectedGraph<T0, T1>::isInvalid( void )
198   return invalid;
201 template <typename T0, typename T1>
202 int DirectedGraph<T0, T1>::FindNode( T0 &nodeinfo )
204         for( unsigned int i = 0; i < nodes.size( ); ++i )
205         {
206                 if( nodes[ i ].nodeinfo == nodeinfo ) return i;
207         }
209         return -1;
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 )
216         {
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;
222         }
224         return -1;
227 OSG_END_NAMESPACE