File: TestRect.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (267 lines) | stat: -rw-r--r-- 7,095 bytes parent folder | download | duplicates (4)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*=========================================================================

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

=========================================================================*/
#include "vtkRect.h"

namespace
{

//------------------------------------------------------------------------------
template <class T>
int TestAddPoint(vtkRect<T>& expandRect, T x, T y, const vtkRect<T>& expected)
{
  int returnValue = 0;

  std::cout << "Adding point (" << x << ", " << y << ") to rect " << expandRect << " ... ";

  expandRect.AddPoint(x, y);

  if (expandRect.GetX() != expected.GetX())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddPoint()/GetX() ";
  }

  if (expandRect.GetY() != expected.GetY())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddPoint()/GetY() ";
  }

  if (expandRect.GetWidth() != expected.GetWidth())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddPoint()/GetWidth() ";
  }

  if (expandRect.GetHeight() != expected.GetHeight())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddPoint()/GetHeight() ";
  }

  if (returnValue != EXIT_SUCCESS)
  {
    std::cout << "failed. Expected " << expected << ", got " << expandRect << "." << std::endl;
  }
  else
  {
    std::cout << "passed." << std::endl;
  }

  return returnValue;
}

//------------------------------------------------------------------------------
template <class T>
int TestAddRect(vtkRect<T>& expandRect, vtkRect<T>& addRect, const vtkRect<T>& expected)
{
  int returnValue = 0;

  std::cout << "Adding rect " << addRect << " to " << expandRect << " ... ";

  expandRect.AddRect(addRect);

  if (expandRect.GetX() != expected.GetX())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddRect()/GetX() ";
  }

  if (expandRect.GetY() != expected.GetY())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddRect()/GetY() ";
  }

  if (expandRect.GetWidth() != expected.GetWidth())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddRect()/GetWidth() ";
  }

  if (expandRect.GetHeight() != expected.GetHeight())
  {
    returnValue = EXIT_FAILURE;
    std::cout << "AddRect()/GetHeight() ";
  }

  if (returnValue != EXIT_SUCCESS)
  {
    std::cout << "failed. Expected " << expected << ", got " << expandRect << "." << std::endl;
  }
  else
  {
    std::cout << "passed." << std::endl;
  }

  return returnValue;
}

} // end anonymous namespace

//------------------------------------------------------------------------------
int TestRect(int, char*[])
{
  int result = 0;

  // Test constructor/getter agreement ---------------------------------------
  vtkRectf rectf(2.0f, 3.0f, 4.0f, 5.0f);
  if (rectf.GetX() != 2.0f)
  {
    std::cout << "GetX() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  if (rectf.GetY() != 3.0f)
  {
    std::cout << "GetY() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  if (rectf.GetWidth() != 4.0f)
  {
    std::cout << "GetWidth() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  if (rectf.GetHeight() != 5.0f)
  {
    std::cout << "GetHeight() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  // Test Setters/getters ----------------------------------------------------
  rectf.SetX(1.0f);
  if (rectf.GetX() != 1.0f)
  {
    std::cout << "SetX()/GetX() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  rectf.SetY(8.0f);
  if (rectf.GetY() != 8.0f)
  {
    std::cout << "SetY()/GetY() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  rectf.SetWidth(7.0f);
  if (rectf.GetWidth() != 7.0f)
  {
    std::cout << "SetWidth()/GetWidth() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  rectf.SetHeight(9.0f);
  if (rectf.GetHeight() != 9.0f)
  {
    std::cout << "SetHeight()/GetHeight() failed\n";
    std::cout << rectf << std::endl;
    return EXIT_FAILURE;
  }

  // Test AddPoint() ----------------------------------------------------------
  vtkRectd expectedRect;
  vtkRectd expandRect = vtkRectd(0.0, 0.0, 0.0, 0.0);

  expectedRect = vtkRectd(-1.0, 0.0, 1.0, 1.0);
  result += TestAddPoint(expandRect, -1.0, 1.0, expectedRect);

  expectedRect = vtkRectd(-1.0, -3.0, 3.0, 4.0);
  result += TestAddPoint(expandRect, 2.0, -3.0, expectedRect);

  // Test AddRect() -----------------------------------------------------------
  vtkRectd addRect;

  // These five cases should exercise all the branches in vtkRect::AddRect().
  expandRect = vtkRectd(0, 0, 4, 4);
  addRect = vtkRectd(-1, 3, 2, 2);
  expectedRect = vtkRectd(-1, 0, 5, 5);
  result += TestAddRect(expandRect, addRect, expectedRect);

  expandRect = vtkRectd(0, 0, 4, 4);
  addRect = vtkRectd(3, 0, 2, 4);
  expectedRect = vtkRectd(0, 0, 5, 4);
  result += TestAddRect(expandRect, addRect, expectedRect);

  expandRect = vtkRectd(0, 0, 4, 4);
  addRect = vtkRectd(0, -1, 4, 2);
  expectedRect = vtkRectd(0, -1, 4, 5);
  result += TestAddRect(expandRect, addRect, expectedRect);

  expandRect = vtkRectd(0, 0, 4, 4);
  addRect = vtkRectd(1, 1, 2, 2);
  expectedRect = vtkRectd(0, 0, 4, 4);
  result += TestAddRect(expandRect, addRect, expectedRect);

  // Test IntersectsWith() -----------------------------------------------------
  vtkRecti recti(2, 3, 2, 1);
  vtkRecti doesntIntersect(-1, -2, 3, 4);
  if (recti.IntersectsWith(doesntIntersect) || recti.Intersect(doesntIntersect))
  {
    std::cout << "Should not have intersected\n";
    std::cout << "recti:\n";
    std::cout << recti << "\n";
    std::cout << "doesntIntersect:\n";
    std::cout << doesntIntersect << "\n";
    return EXIT_FAILURE;
  }

  vtkRecti intersects(3, 2, 3, 4);
  if (!recti.IntersectsWith(intersects))
  {
    std::cout << "Should have intersected\n";
    std::cout << "recti:\n";
    std::cout << recti << "\n";
    std::cout << "intersect:\n";
    std::cout << intersects << "\n";
    return EXIT_FAILURE;
  }

  vtkRecti rectiIntersected = recti;
  if (!rectiIntersected.Intersect(intersects))
  {
    std::cout << "Should have intersected\n";
    std::cout << "recti:\n";
    std::cout << recti << "\n";
    std::cout << "intersect:\n";
    std::cout << intersects << "\n";
    return EXIT_FAILURE;
  }

  if (rectiIntersected != vtkRecti(3, 3, 1, 1))
  {
    std::cout << "Incorrect intersection\n";
    std::cout << "recti:       " << recti << "\n";
    std::cout << "intersect:   " << intersects << "\n";
    std::cout << "intersected: " << rectiIntersected << "\n";
    std::cout << "expected:    " << vtkRecti(3, 3, 1, 1) << "\n";
    return EXIT_FAILURE;
  }

  if (result != EXIT_SUCCESS)
  {
    result = EXIT_FAILURE;
  }

  return result;
}