File: TestImageDataFindCell.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 181; javascript: 165; objc: 153; tcl: 59
file content (205 lines) | stat: -rw-r--r-- 6,753 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

// .NAME Test FindCell methods for image data
// .SECTION Description
// This program tests the FindCell methods for vtkImageData to
// ensure that they give correct results near the boundaries and
// to ensure that tolerance is handled properly.  Even when the
// tolerance is zero, points on the boundary must be considered
// to be inside the dataset.

#include "vtkDebugLeaks.h"
#include "vtkImageData.h"
#include "vtkMatrix4x4.h"
#include "vtkSmartPointer.h"

inline int DoTest(int extent[6], double origin[3], double spacing[3], double direction[9])
{
  vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
  image->SetExtent(extent);
  image->SetOrigin(origin);
  image->SetSpacing(spacing);
  image->SetDirectionMatrix(direction);
  image->AllocateScalars(VTK_DOUBLE, 1);

  double bounds[6];
  image->GetBounds(bounds);

  int subId = 0;
  double pcoords[3];
  double weights[8];
  double ijk[3], x[3];
  vtkIdType cellId;

  double tol = 1e-4;

  for (int i = 0; i < 3; i++)
  {
    ijk[0] = 0.5 * (extent[0] + extent[1]);
    ijk[1] = 0.5 * (extent[2] + extent[3]);
    ijk[2] = 0.5 * (extent[4] + extent[5]);
    for (int j = 0; j < 2; j++)
    {
      // test point right on the boundary with zero tolerance
      ijk[i] = extent[2 * i + j];

      image->TransformContinuousIndexToPhysicalPoint(ijk, x);
      cellId = image->FindCell(x, nullptr, 0, 0.0, subId, pcoords, weights);

      if (cellId < 0)
      {
        cerr << "point (" << x[0] << ", " << x[1] << ", " << x[2] << ")"
             << " should be in bounds (" << bounds[0] << ", " << bounds[1] << ", " << bounds[2]
             << ", " << bounds[3] << ", " << bounds[4] << ", " << bounds[5] << ") with tol 0.0\n";
        return 1;
      }

      // test point just outside boundary with zero tolerance
      double offset = ((j == 0) ? (-tol * 0.5) : (tol * 0.5));
      ijk[i] = extent[2 * i + j] + offset;

      image->TransformContinuousIndexToPhysicalPoint(ijk, x);
      cellId = image->FindCell(x, nullptr, 0, 0.0, subId, pcoords, weights);

      if (cellId >= 0)
      {
        cerr << "point (" << x[0] << ", " << x[1] << ", " << x[2] << ")"
             << " should be out of bounds (" << bounds[0] << ", " << bounds[1] << ", " << bounds[2]
             << ", " << bounds[3] << ", " << bounds[4] << ", " << bounds[5] << ") with tol 0.0\n";
        return 1;
      }

      // test point just outside boundary with nonzero tolerance
      ijk[i] = extent[2 * i + j] + ((j == 0) ? (-tol * 0.5) : (tol * 0.5));

      image->TransformContinuousIndexToPhysicalPoint(ijk, x);
      cellId = image->FindCell(x, nullptr, 0, tol * tol, subId, pcoords, weights);

      if (cellId < 0)
      {
        cerr << "point (" << x[0] << ", " << x[1] << ", " << x[2] << ")"
             << " should be inside bounds (" << bounds[0] << ", " << bounds[1] << ", " << bounds[2]
             << ", " << bounds[3] << ", " << bounds[4] << ", " << bounds[5] << ") with tol " << tol
             << "\n";
        return 1;
      }

      // check pcoords at boundaries
      int isUpperBound = (j == 1);
      int isOnePixelThick = (extent[2 * i] == extent[2 * i + 1]);
      if (isUpperBound && !isOnePixelThick)
      {
        if (pcoords[i] != 1.0)
        {
          cerr << "at upper bounds, pcoord should be 1, but is " << pcoords[i] << "\n";
          return 1;
        }
      }
      else
      {
        if (pcoords[i] != 0.0)
        {
          cerr << "at lower bounds and for 0,1,2D cells, pcoord should be 0, "
               << "but is " << pcoords[i] << "\n";
          return 1;
        }
      }

      // validate the cellId
      ijk[i] = extent[2 * i + j];
      image->TransformContinuousIndexToPhysicalPoint(ijk, x);
      double pcoords2[3];
      int idx[3];
      if (image->ComputeStructuredCoordinates(x, idx, pcoords2) == 0)
      {
        cerr << "ComputeStructuredCoordinates failed for "
             << "point (" << x[0] << ", " << x[1] << ", " << x[2] << ")"
             << " and bounds (" << bounds[0] << ", " << bounds[1] << ", " << bounds[2] << ", "
             << bounds[3] << ", " << bounds[4] << ", " << bounds[5] << ")\n";
        return 1;
      }

      if (image->ComputeCellId(idx) != cellId)
      {
        cerr << "cellId = " << cellId << ", should be " << image->ComputeCellId(idx) << "\n";
        return 1;
      }

      // validate the pcoords, allow a tolerance
      double dist = pcoords[i] - pcoords2[i];
      if (dist * dist > 1e-29)
      {
        cerr << "pcoords[" << i << "] = " << pcoords[i] << ", should be " << pcoords2[i] << "\n";
        return 1;
      }

      // Test with bigger tolerance
      double y[3];
      y[0] = x[0] + 1e-5;
      y[1] = x[1];
      y[2] = x[2];
      if (image->ComputeStructuredCoordinates(y, idx, pcoords2, 1e-8) == 0)
      {
        cerr << "ComputeStructuredCoordinates with tolerance failed for "
             << "point (" << y[0] << ", " << y[1] << ", " << y[2] << ")"
             << " and bounds (" << bounds[0] << ", " << bounds[1] << ", " << bounds[2] << ", "
             << bounds[3] << ", " << bounds[4] << ", " << bounds[5] << ")\n";
        return 1;
      }
    }
  }

  return 0;
}

int TestImageDataFindCell(int, char*[])
{
  // test 0D, 1D, 2D, 3D data with various extents, spacings, origins
  static int dims[4][3] = { { 1, 1, 1 }, { 3, 1, 1 }, { 3, 3, 1 }, { 3, 3, 3 } };
  static int starts[4][3] = { { 0, 0, 0 }, { -1, 0, -1 }, { 2, 3, 6 }, { -10, 0, 5 } };
  static double spacings[4][3] = { { 1, 1, 1 }, { 1.0 / 7, 1, 1 }, { 1, -1, 1 },
    { -1, 1, -1 / 13.0 } };
  static double origins[4][3] = { { 0, 0, 0 }, { 1.0 / 13, 0, 0 }, { 0, -1, 0 },
    { -1, 0, -1 / 7.0 } };
  static double directions[4][9] = { { 1, 0, 0, 0, 1, 0, 0, 0, 1 }, { -1, 0, 0, 0, -1, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, -1, 0, 1, 0, 0, 0, 0, 1 } };

  int extent[6];
  double* spacing;
  double* origin;
  double* direction;

  int failed = 0;

  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      for (int k = 0; k < 4; k++)
      {
        spacing = spacings[k];
        for (int l = 0; l < 4; l++)
        {
          origin = origins[l];
          for (int ii = 0; ii < 3; ii++)
          {
            extent[2 * ii] = starts[i][ii];
            extent[2 * ii + 1] = starts[i][ii] + dims[j][ii] - 1;
          }

          for (int jj = 0; jj < 4; jj++)
          {
            direction = directions[jj];
            if (DoTest(extent, origin, spacing, direction))
            {
              failed = 1;
            }
          }
        }
      }
    }
  }

  return failed;
}