1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGraph2.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 // .NAME
17 // .SECTION Description
18 // This program tests functions in vtkGraph
19 
20 #include "vtkMutableUndirectedGraph.h"
21 #include "vtkMutableDirectedGraph.h"
22 #include <limits>
23 #include "vtkSmartPointer.h"
24 
25 #include <vector>
26 
27 template<class A>
fuzzyCompare(A a,A b)28 bool fuzzyCompare(A a, A b) {
29   return fabs(a - b) < std::numeric_limits<A>::epsilon();
30 }
31 
32 int TestGetEdgeId();
33 int TestToDirectedGraph();
34 int TestToUndirectedGraph();
35 
TestGraph2(int,char * [])36 int TestGraph2(int,char *[])
37 {
38   std::vector<int> results;
39 
40   results.push_back(TestGetEdgeId());
41   results.push_back(TestToDirectedGraph());
42   results.push_back(TestToUndirectedGraph());
43 
44   for(unsigned int i = 0; i < results.size(); i++)
45   {
46     if(results[i] == EXIT_FAILURE)
47     {
48       return EXIT_FAILURE;
49     }
50   }
51 
52   return EXIT_SUCCESS;
53 }
54 
TestGetEdgeId()55 int TestGetEdgeId()
56 {
57   // Create a graph
58   vtkSmartPointer<vtkMutableUndirectedGraph> g =
59     vtkSmartPointer<vtkMutableUndirectedGraph>::New();
60   vtkIdType v0 = g->AddVertex();
61   vtkIdType v1 = g->AddVertex();
62   vtkIdType v2 = g->AddVertex();
63 
64   vtkEdgeType e0 = g->AddEdge(v0, v1);
65   vtkEdgeType e1 = g->AddEdge(v1, v2);
66 
67   // Test to make sure both edges (in either orientation) are found
68   if(g->GetEdgeId(v0, v1) != e0.Id || g->GetEdgeId(v1, v0) != e0.Id)
69   {
70     return EXIT_FAILURE;
71   }
72 
73   if(g->GetEdgeId(v1, v2) != e1.Id || g->GetEdgeId(v2, v1) != e1.Id)
74   {
75     return EXIT_FAILURE;
76   }
77 
78   // Test to make sure -1 is returned if the edge does not exist
79   if(g->GetEdgeId(v1, 3) != -1)
80   {
81     return EXIT_FAILURE;
82   }
83 
84   return EXIT_SUCCESS;
85 }
86 
TestToDirectedGraph()87 int TestToDirectedGraph()
88 {
89   // Create an undirected graph
90   vtkSmartPointer<vtkMutableUndirectedGraph> ug =
91     vtkSmartPointer<vtkMutableUndirectedGraph>::New();
92   vtkIdType v0 = ug->AddVertex();
93   vtkIdType v1 = ug->AddVertex();
94   vtkIdType v2 = ug->AddVertex();
95 
96   ug->AddEdge(v0, v1);
97   ug->AddEdge(v1, v2);
98 
99   // Convert it to a directed graph
100   vtkSmartPointer<vtkMutableDirectedGraph> dg =
101     vtkSmartPointer<vtkMutableDirectedGraph>::New();
102   ug->ToDirectedGraph(dg);
103 
104   // Check that the number of vertices and edges is unchanged
105   if(ug->GetNumberOfVertices() != dg->GetNumberOfVertices() ||
106      ug->GetNumberOfEdges() != dg->GetNumberOfEdges())
107   {
108     return EXIT_FAILURE;
109   }
110 
111   return EXIT_SUCCESS;
112 }
113 
TestToUndirectedGraph()114 int TestToUndirectedGraph()
115 {
116   // Create a directed graph
117   vtkSmartPointer<vtkMutableDirectedGraph> dg =
118     vtkSmartPointer<vtkMutableDirectedGraph>::New();
119   vtkIdType v0 = dg->AddVertex();
120   vtkIdType v1 = dg->AddVertex();
121   vtkIdType v2 = dg->AddVertex();
122 
123   dg->AddEdge(v0, v1);
124   dg->AddEdge(v1, v2);
125 
126   // Convert it to an undirected graph
127   vtkSmartPointer<vtkMutableUndirectedGraph> ug =
128     vtkSmartPointer<vtkMutableUndirectedGraph>::New();
129   dg->ToUndirectedGraph(ug);
130 
131   // Check that the number of vertices and edges is unchanged
132   if(ug->GetNumberOfVertices() != dg->GetNumberOfVertices() ||
133      ug->GetNumberOfEdges() != dg->GetNumberOfEdges())
134   {
135     return EXIT_FAILURE;
136   }
137 
138   return EXIT_SUCCESS;
139 }
140