File: TestGraphLayoutStrategy.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (207 lines) | stat: -rw-r--r-- 6,563 bytes parent folder | download | duplicates (9)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestGraphLayoutStrategy.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.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkCircularLayoutStrategy.h"
#include "vtkEdgeListIterator.h"
#include "vtkFast2DLayoutStrategy.h"
#include "vtkForceDirectedLayoutStrategy.h"
#include "vtkGraphLayout.h"
#include "vtkMath.h"
#include "vtkPassThroughLayoutStrategy.h"
#include "vtkRandomGraphSource.h"
#include "vtkRandomLayoutStrategy.h"
#include "vtkSimple2DLayoutStrategy.h"
#include "vtkSmartPointer.h"
#include "vtkTestUtilities.h"
#include "vtkTreeLayoutStrategy.h"

#define VTK_CREATE(type, name) \
  vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

int TestGraphLayoutStrategy(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
  int errors = 0;

  // Create input graph
  vtkIdType numVert = 100;
  vtkIdType numEdges = 150;
  VTK_CREATE(vtkRandomGraphSource, source);
  source->SetNumberOfVertices(numVert);
  source->SetNumberOfEdges(numEdges);

  VTK_CREATE(vtkGraphLayout, layout);
  layout->SetInputConnection(source->GetOutputPort());
  vtkGraph *output = NULL;
  double pt[3] = {0.0, 0.0, 0.0};
  double pt2[3] = {0.0, 0.0, 0.0};
  double eps = 1.0e-6;
  double length = 0.0;
  double tol = 50.0;

  cerr << "Testing vtkCircularLayoutStrategy..." << endl;
  VTK_CREATE(vtkCircularLayoutStrategy, circular);
  layout->SetLayoutStrategy(circular);
  layout->Update();
  output = layout->GetOutput();
  for (vtkIdType i = 0; i < numVert; i++)
    {
    output->GetPoint(i, pt);
    double dist = pt[0]*pt[0] + pt[1]*pt[1] - 1.0;
    dist = dist > 0 ? dist : -dist;
    if (dist > eps || pt[2] != 0.0)
      {
      cerr << "ERROR: Point " << i << " is not on the unit circle." << endl;
      errors++;
      }
    }
  cerr << "...done." << endl;

  cerr << "Testing vtkFast2DLayoutStrategy..." << endl;
  VTK_CREATE(vtkFast2DLayoutStrategy, fast);
  fast->SetRestDistance(1.0f);
  length = fast->GetRestDistance();
  layout->SetLayoutStrategy(fast);
  layout->Update();
  output = layout->GetOutput();
  VTK_CREATE(vtkEdgeListIterator, edges);
  output->GetEdges(edges);
  while (edges->HasNext())
    {
    vtkEdgeType e = edges->Next();
    vtkIdType u = e.Source;
    vtkIdType v = e.Target;
    output->GetPoint(u, pt);
    output->GetPoint(v, pt2);
    double dist = sqrt(vtkMath::Distance2BetweenPoints(pt, pt2));
    if (dist < length/tol || dist > length*tol)
      {
      cerr << "ERROR: Edge " << u << "," << v << " distance is " << dist
           << " but resting distance is " << length << endl;
      errors++;
      }
    if (pt[2] != 0.0)
      {
      cerr << "ERROR: Point " << u << " not on the xy plane" << endl;
      errors++;
      }
    if (pt2[2] != 0.0)
      {
      cerr << "ERROR: Point " << v << " not on the xy plane" << endl;
      errors++;
      }
    }
  cerr << "...done." << endl;

  cerr << "Testing vtkForceDirectedLayoutStrategy..." << endl;
  VTK_CREATE(vtkForceDirectedLayoutStrategy, force);
  length = pow(1.0/numVert, 1.0/3.0);
  layout->SetLayoutStrategy(force);
  layout->Update();
  output = layout->GetOutput();
  output->GetEdges(edges);
  while (edges->HasNext())
    {
    vtkEdgeType e = edges->Next();
    vtkIdType u = e.Source;
    vtkIdType v = e.Target;
    output->GetPoint(u, pt);
    output->GetPoint(v, pt2);
    double dist = sqrt(vtkMath::Distance2BetweenPoints(pt, pt2));
    if (dist < length/tol || dist > length*tol)
      {
      cerr << "ERROR: Edge " << u << "," << v << " distance is " << dist
           << " but resting distance is " << length << endl;
      errors++;
      }
    }
  cerr << "...done." << endl;

  cerr << "Testing vtkPassThroughLayoutStrategy..." << endl;
  VTK_CREATE(vtkPassThroughLayoutStrategy, pass);
  layout->SetLayoutStrategy(pass);
  layout->Update();
  output = layout->GetOutput();
  for (vtkIdType i = 0; i < numVert; i++)
    {
    output->GetPoint(i, pt);
    if (pt[0] != 0.0 || pt[1] != 0.0 || pt[2] != 0.0)
      {
      cerr << "ERROR: Point " << i << " is not 0,0,0." << endl;
      errors++;
      }
    }
  cerr << "...done." << endl;

  cerr << "Testing vtkRandomLayoutStrategy..." << endl;
  VTK_CREATE(vtkRandomLayoutStrategy, random);
  double bounds[6] = {0, 0, 0, 0, 0, 0};
  random->GetGraphBounds(bounds);
  layout->SetLayoutStrategy(random);
  layout->Update();
  output = layout->GetOutput();
  for (vtkIdType i = 0; i < numVert; i++)
    {
    output->GetPoint(i, pt);
    if (pt[0] < bounds[0] || pt[0] > bounds[1]
     || pt[1] < bounds[2] || pt[1] > bounds[3]
     || pt[2] < bounds[4] || pt[2] > bounds[5])
      {
      cerr << "ERROR: Point " << i << " is not within the bounds." << endl;
      errors++;
      }
    }
  cerr << "...done." << endl;

  cerr << "Testing vtkSimple2DLayoutStrategy..." << endl;
  VTK_CREATE(vtkSimple2DLayoutStrategy, simple);
  simple->SetRestDistance(1.0f);
  length = simple->GetRestDistance();
  layout->SetLayoutStrategy(simple);
  layout->Update();
  output = layout->GetOutput();
  output->GetEdges(edges);
  while (edges->HasNext())
    {
    vtkEdgeType e = edges->Next();
    vtkIdType u = e.Source;
    vtkIdType v = e.Target;
    output->GetPoint(u, pt);
    output->GetPoint(v, pt2);
    double dist = sqrt(vtkMath::Distance2BetweenPoints(pt, pt2));
    if (dist < length/tol || dist > length*tol)
      {
      cerr << "ERROR: Edge " << u << "," << v << " distance is " << dist
           << " but resting distance is " << length << endl;
      errors++;
      }
    if (pt[2] != 0.0)
      {
      cerr << "ERROR: Point " << u << " not on the xy plane" << endl;
      errors++;
      }
    if (pt2[2] != 0.0)
      {
      cerr << "ERROR: Point " << v << " not on the xy plane" << endl;
      errors++;
      }
    }
  cerr << "...done." << endl;

  return errors;
}