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