File: TestAdaptiveResampleToImage.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 (166 lines) | stat: -rw-r--r-- 4,863 bytes parent folder | download | duplicates (3)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkAdaptiveResampleToImage.h"
#include "vtkBoundingBox.h"
#include "vtkClipDataSet.h"
#include "vtkDataSet.h"
#include "vtkLogger.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkPartitionedDataSet.h"
#include "vtkRTAnalyticSource.h"

#if VTK_MODULE_ENABLE_VTK_ParallelMPI
#include "vtkMPIController.h"
#else
#include "vtkDummyController.h"
#endif

#include <numeric>
#include <vector>

namespace
{
bool ValidateDataset(vtkPartitionedDataSet* pds, vtkMultiProcessController* controller,
  int numLeaves, const vtkBoundingBox& gbox)
{
  numLeaves = vtkMath::NearestPowerOfTwo(numLeaves);

  const int numParts = static_cast<int>(pds->GetNumberOfPartitions());
  int allParts = numParts;
  controller->AllReduce(&numParts, &allParts, 1, vtkCommunicator::SUM_OP);

  if (allParts != numLeaves)
  {
    vtkLogF(ERROR, "Error: mismatched leaves. expected: %d, got %d", numLeaves, allParts);
    return false;
  }

  // validate all boxes same as the input dataset box.
  double bds[6];
  vtkMath::UninitializeBounds(bds);
  pds->GetBounds(bds);

  vtkBoundingBox bbox, allbbox;
  bbox.SetBounds(bds);
  controller->AllReduce(bbox, allbbox);

  if (allbbox != gbox)
  {
    vtkLogF(ERROR, "Error: mismatched bounds!");
    return false;
  }

  // validate no bounding boxes overlap.
  std::vector<int> parts(controller->GetNumberOfProcesses());
  parts[controller->GetLocalProcessId()] = numParts;

  controller->AllGather(&numParts, parts.data(), 1);

  std::vector<double> local_boxes(6 * numParts);
  for (int cc = 0; cc < numParts; ++cc)
  {
    vtkDataSet::SafeDownCast(pds->GetPartition(cc))->GetBounds(&local_boxes[6 * cc]);
  }
  std::vector<double> boxes(6 * std::accumulate(parts.begin(), parts.end(), 0));

  std::vector<vtkIdType> recvLengths(controller->GetNumberOfProcesses());
  std::vector<vtkIdType> offsets(controller->GetNumberOfProcesses());
  controller->AllGatherV(local_boxes.data(), boxes.data(),
    static_cast<vtkIdType>(local_boxes.size()), recvLengths.data(), offsets.data());
  if (controller->GetNumberOfProcesses() == 1)
  {
    boxes = local_boxes;
  }

  for (size_t i = 0; i < (boxes.size()) / 6; ++i)
  {
    const vtkBoundingBox boxA(&boxes[6 * i]);
    for (size_t j = i + 1; j < (boxes.size()) / 6; ++j)
    {
      const vtkBoundingBox boxB(&boxes[6 * j]);
      int overlap = 0;
      for (int dim = 0; dim < 3; ++dim)
      {
        if (boxB.GetMinPoint()[dim] > boxA.GetMinPoint()[dim] &&
          boxB.GetMinPoint()[dim] < boxA.GetMaxPoint()[dim])
        {
          overlap++;
        }
        else if (boxB.GetMaxPoint()[dim] > boxA.GetMinPoint()[dim] &&
          boxB.GetMaxPoint()[dim] < boxA.GetMaxPoint()[dim])
        {
          overlap++;
        }
      }
      if (overlap == 3)
      {
        vtkLogF(ERROR, "Error: boxes overlap!");
        abort();
      }
    }
  }

  return true;
}
}

int TestAdaptiveResampleToImage(int argc, char* argv[])
{
#if VTK_MODULE_ENABLE_VTK_ParallelMPI
  vtkMPIController* contr = vtkMPIController::New();
#else
  vtkDummyController* contr = vtkDummyController::New();
#endif
  contr->Initialize(&argc, &argv);
  vtkMultiProcessController::SetGlobalController(contr);

  int status = EXIT_SUCCESS;

  // Create Pipeline
  vtkNew<vtkRTAnalyticSource> wavelet;
  wavelet->SetWholeExtent(0, 63, 0, 63, 0, 63);
  wavelet->SetCenter(16, 16, 16);

  vtkNew<vtkClipDataSet> clip;
  clip->SetInputConnection(wavelet->GetOutputPort());
  clip->SetValue(157);

  vtkNew<vtkAdaptiveResampleToImage> resampler;
  resampler->SetNumberOfImages(4);
  resampler->SetInputConnection(clip->GetOutputPort());
  resampler->SetSamplingDimensions(8, 8, 8);
  resampler->UpdatePiece(contr->GetLocalProcessId(), contr->GetNumberOfProcesses(), 0);

  double bds[6];
  vtkDataSet::SafeDownCast(clip->GetOutputDataObject(0))->GetBounds(bds);
  vtkBoundingBox bbox(bds), allbbox;
  contr->AllReduce(bbox, allbbox);

  if (!ValidateDataset(
        vtkPartitionedDataSet::SafeDownCast(resampler->GetOutputDataObject(0)), contr, 4, allbbox))
  {
    status = EXIT_FAILURE;
  }

  resampler->SetNumberOfImages(6);
  resampler->UpdatePiece(contr->GetLocalProcessId(), contr->GetNumberOfProcesses(), 0);
  if (!ValidateDataset(
        vtkPartitionedDataSet::SafeDownCast(resampler->GetOutputDataObject(0)), contr, 6, allbbox))
  {
    status = EXIT_FAILURE;
  }

  resampler->SetNumberOfImages(3);
  resampler->UpdatePiece(contr->GetLocalProcessId(), contr->GetNumberOfProcesses(), 0);
  if (!ValidateDataset(
        vtkPartitionedDataSet::SafeDownCast(resampler->GetOutputDataObject(0)), contr, 3, allbbox))
  {
    status = EXIT_FAILURE;
  }

  vtkMultiProcessController::SetGlobalController(nullptr);
  contr->Finalize();
  contr->Delete();
  return status;
}