File: itkContourSpatialObjectTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (280 lines) | stat: -rw-r--r-- 9,706 bytes parent folder | download | duplicates (5)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#include "itkContourSpatialObject.h"
#include <iostream>
#include "itkMath.h"

/**
 * This is a test for itkContourSpatialObject.  It runs all methods and checks
 * the correctness of each result.  There are several potential issues
 * surrounding the IsInside method.  The IsEvaluable and ValueAt methods both
 * depend on IsInside and at the moment IsInside will always return false.  It
 * is unclear whether this is done intentionally to indicate that nothing can
 * be inside the contour or whether this is a bug that needs fixing.
 */
int itkContourSpatialObjectTest(int, char* [])
{

  //
  // Set up data
  //
  const unsigned int NumDimensions = 2;
  typedef itk::ContourSpatialObject<NumDimensions> SpatialObjectType;

  // contour is a unit square
  SpatialObjectType::ControlPointType pt1;
  pt1.SetPickedPoint(0,0);
  SpatialObjectType::ControlPointType  pt2;
  pt2.SetPickedPoint(1,0);
  SpatialObjectType::ControlPointType  pt3;
  pt3.SetPickedPoint(1,1);
  SpatialObjectType::ControlPointType  pt4;
  pt4.SetPickedPoint(0,1);

  SpatialObjectType::Pointer contour = SpatialObjectType::New();


  //
  // Test ComputeBoundingBox before data added
  //
  if (contour->ComputeLocalBoundingBox())
    {
    std::cout << "[FAILED] computed bounding box without data " << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] ComputeLocalBoundingBox before data" << std::endl;


  //
  // Test Control Points (SetControlPoints, GetControlPoints, GetNumberOfControlPoints,
  // GetControlPoint)
  //
  SpatialObjectType::ControlPointListType controlPointList;
  controlPointList.push_back(pt1);
  controlPointList.push_back(pt2);
  controlPointList.push_back(pt3);
  controlPointList.push_back(pt4);

  contour->SetControlPoints(controlPointList);

  // check number of points
  if (contour->GetNumberOfControlPoints() != 4)
    {
    std::cout << "[FAILED] Did not add the right number of control points" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] GetNumberOfControlPoints" << std::endl;

  // check values of points
  if (itk::Math::NotAlmostEquals( contour->GetControlPoints()[0].GetPickedPoint()[0], pt1.GetPickedPoint()[0] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[0].GetPickedPoint()[1], pt1.GetPickedPoint()[1] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[1].GetPickedPoint()[0], pt2.GetPickedPoint()[0] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[1].GetPickedPoint()[1], pt2.GetPickedPoint()[1] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[2].GetPickedPoint()[0], pt3.GetPickedPoint()[0] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[2].GetPickedPoint()[1], pt3.GetPickedPoint()[1] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[3].GetPickedPoint()[0], pt4.GetPickedPoint()[0] ) ||
      itk::Math::NotAlmostEquals( contour->GetControlPoints()[3].GetPickedPoint()[1], pt4.GetPickedPoint()[1] ))
    {
    std::cout << "[FAILED] Did not add/retrieve control point list correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] Set/GetControlPoints" << std::endl;

  // check retrieval of a single point
  if (itk::Math::NotAlmostEquals(contour->GetControlPoint(0)->GetPickedPoint()[0], pt1.GetPickedPoint()[0]) ||
      itk::Math::NotAlmostEquals(contour->GetControlPoint(0)->GetPickedPoint()[1], pt1.GetPickedPoint()[1]))
    {
    std::cout << "[FAILED] Did not retrieve single control point correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] GetControlPoint" << std::endl;


  //
  // Test Set/Get Closed
  //

  // first set to not closed and test
  contour->SetClosed(false);
  if (contour->GetClosed())
    {
    std::cout << "[FAILED] Did not set/retrieve closed property correctly" << std::endl;
    return EXIT_FAILURE;
    }

  // then set it to closed and test
  contour->SetClosed(true);
  if (!contour->GetClosed())
    {
    std::cout << "[FAILED] Did not set/retrieve closed property correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] Set/GetClosed" << std::endl;


  //
  // Test Set/Get DisplayOrientation
  //
  contour->SetDisplayOrientation(1);
  if (contour->GetDisplayOrientation() != 1)
    {
    std::cout << "[FAILED] Did not set/retrieve display orientation correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] Set/GetDisplayOrientation" << std::endl;


  //
  // Test Set/Get AttachedToSlice
  //

  // first test with no slice
  if (contour->GetAttachedToSlice() != -1)
    {
    std::cout << "[FAILED] Did not retrieve -1 when not slice" << std::endl;
    return EXIT_FAILURE;
    }

  // then test when attached to a slice
  contour->SetAttachedToSlice(1);
  if (contour->GetAttachedToSlice() != 1)
    {
    std::cout << "[FAILED] Did not set/retrieve proper slice" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] Set/GetAttachedToSlice" << std::endl;


  //
  // Test Set/Get InterpolationType
  //
  contour->SetInterpolationType(SpatialObjectType::LINEAR_INTERPOLATION);
  if (contour->GetInterpolationType() != SpatialObjectType::LINEAR_INTERPOLATION)
    {
    std::cout << "[FAILED] Did not set/retrieve interpolation type correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] Set/GetInterpolationType" << std::endl;


  //
  // Test Interpolation Points (SetInterpolationPoints, GetInterpolationPoints,
  // GetNumberOfInterpolationPoints, GetInterpolationPoint)
  //
  SpatialObjectType::InterpolatedPointType intPt1;
  intPt1.SetPosition(0,0.5);
  SpatialObjectType::InterpolatedPointType  intPt2;
  intPt2.SetPosition(0.5,0);

  SpatialObjectType::InterpolatedPointListType interpPointList;
  interpPointList.push_back(intPt1);
  interpPointList.push_back(intPt2);

  contour->SetInterpolatedPoints(interpPointList);

  // check number of points
  if (contour->GetNumberOfInterpolatedPoints() != 2)
    {
    std::cout << "[FAILED] Did not add the right number of interpolated points" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] GetNumberOfInterpolatedPoints" << std::endl;

  // check values of points
  if (itk::Math::NotAlmostEquals(contour->GetInterpolatedPoints()[0].GetPosition()[0], intPt1.GetPosition()[0]) ||
      itk::Math::NotAlmostEquals(contour->GetInterpolatedPoints()[0].GetPosition()[1], intPt1.GetPosition()[1]) ||
      itk::Math::NotAlmostEquals(contour->GetInterpolatedPoints()[1].GetPosition()[0], intPt2.GetPosition()[0]) ||
      itk::Math::NotAlmostEquals(contour->GetInterpolatedPoints()[1].GetPosition()[1], intPt2.GetPosition()[1]))
    {
    std::cout << "[FAILED] Did not add/retrieve interpolated point list correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] Set/GetInterpolatedPoints" << std::endl;

  // check retrieval of a single point
  if (itk::Math::NotAlmostEquals(contour->GetInterpolatedPoint(0)->GetPosition()[0], intPt1.GetPosition()[0]) ||
      itk::Math::NotAlmostEquals(contour->GetInterpolatedPoint(0)->GetPosition()[1], intPt1.GetPosition()[1]))
    {
    std::cout << "[FAILED] Did not retrieve single interpolated point correctly" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] GetInterpolatedPoint" << std::endl;


  //
  // Test ComputeLocalBoundingBox
  //
  if (!contour->ComputeLocalBoundingBox())
    {
    std::cout << "[FAILED] faild bounding box computation" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] ComputeLocalBoundingBox" << std::endl;


  //
  // Test IsInside (at this point, this should always return false)
  //
  SpatialObjectType::PointType testPoint;
  testPoint[0] = 0;
  testPoint[1] = 0;
  if (contour->IsInside(testPoint))
    {
    std::cout << "[FAILED] Somehow returned true for IsInside" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] IsInside" << std::endl;


  //
  // Test IsEvaluableAt (should always return false since IsInside always returns false)
  //
  if (contour->IsEvaluableAt(testPoint))
    {
    std::cout << "[FAILED] Somehow returned true for IsEvaluableAt" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] IsEvaluableAt" << std::endl;


  //
  // Test ValueAt (should always return false and val=0 since IsInside always returns false)
  //
  double val = -1;
  double* valPtr = &val;
  if (contour->ValueAt(testPoint, *valPtr) || itk::Math::NotExactlyEquals(val, contour->GetDefaultOutsideValue()))
    {
    std::cout << "[FAILED] Somehow returned true for ValueAt" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED] ValueAt" << std::endl;


  //
  // Run PrintSelf for the sake of coverage (and to make sure no segfault/exceptions arise)
  //
  itk::Indent idt;
  contour->Print(std::cout, idt);


  //
  // All tests executed successfully
  //
  return EXIT_SUCCESS;

}