File: TestImageToAMR.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (139 lines) | stat: -rw-r--r-- 4,057 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
/*=========================================================================

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

=========================================================================*/
// Test vtkImageToAMR filter.

#include "vtkPointData.h"
#include "vtkCellData.h"

#include "vtkImageToAMR.h"
#include "vtkRTAnalyticSource.h"
#include "vtkNew.h"
#include "vtkDataObject.h"
#include "vtkOverlappingAMR.h"
#include "vtkAMRBox.h"
#include "vtkImageData.h"
#include "vtkUniformGrid.h"
#include "vtkIdFilter.h"
#include "vtkVector.h"
#include "vtkIdTypeArray.h"

#include <vector>
#define VTK_SUCCESS 0
#define VTK_FAILURE 1

namespace
{
  //
  int ComputeNumCells(vtkOverlappingAMR* amr)
  {
    int n(0);
    for(unsigned int level=0; level<amr->GetNumberOfLevels();level++)
    {
      int numDataSets = amr->GetNumberOfDataSets(level);
      for(int i=0; i<numDataSets; i++)
      {
        vtkUniformGrid* grid = amr->GetDataSet(level,i);
        int numCells = grid->GetNumberOfCells();
        for(int cellId =0; cellId<numCells; cellId++)
        {
          n+= grid->IsCellVisible(cellId)? 1 :0;
        }
      }
    }
    return n;
  }


  vtkIdType FindCell( vtkImageData* image, double point[3])
  {
    double pcoords[3]; int subid=0;
    return image->vtkImageData::FindCell( point, NULL, -1, 0.1, subid, pcoords, NULL);
  }
};

int TestImageToAMR(int, char *[])
{
  vtkNew<vtkRTAnalyticSource> imageSource;
  imageSource->SetWholeExtent(0, 0, -128, 128, -128, 128);

  vtkNew<vtkIdFilter> idFilter;
  idFilter->SetInputConnection(imageSource->GetOutputPort());

  vtkNew<vtkImageToAMR> amrConverter;
  amrConverter->SetInputConnection(idFilter->GetOutputPort());
  amrConverter->SetNumberOfLevels(4);

  std::vector<vtkVector3d> samples;
  for(int i=-118; i<122; i+=10)
  {
    samples.push_back( vtkVector3d(0.0, (double)i, (double) i));
  }

  for(unsigned int numLevels=1; numLevels<=4; numLevels++)
  {
    for(int maxBlocks=10; maxBlocks<=50; maxBlocks+=10)
    {
      amrConverter->SetNumberOfLevels(numLevels);
      amrConverter->SetMaximumNumberOfBlocks(maxBlocks);
      amrConverter->Update();
      vtkImageData* image = vtkImageData::SafeDownCast(idFilter->GetOutputDataObject(0));
      vtkOverlappingAMR* amr = vtkOverlappingAMR::SafeDownCast(amrConverter->GetOutputDataObject(0));
      amr->Audit();
      // cout<<amr->GetTotalNumberOfBlocks()<<" "<<maxBlocks<<endl;
      if(amr->GetNumberOfLevels()!=numLevels)
      {
        return VTK_FAILURE;
      }
      if(maxBlocks< static_cast<int>(amr->GetTotalNumberOfBlocks()))
      {
        return VTK_FAILURE;
      }
      if(ComputeNumCells(amr)!=image->GetNumberOfCells())
      {
        return VTK_FAILURE;
      }

      vtkIdTypeArray* cd = vtkArrayDownCast<vtkIdTypeArray>(image->GetCellData()->GetArray("vtkIdFilter_Ids"));
      assert(cd);
      for(std::vector<vtkVector3d>::iterator itr=samples.begin(); itr!=samples.end();itr++)
      {
        double* x =(*itr).GetData();
        vtkIdType cellId = FindCell(image,x);
        vtkIdType value = cd->GetValue(cellId);
        assert(cellId==value);

        unsigned int level,id;
        if(amr->FindGrid(x,level,id))
        {
          vtkUniformGrid* grid =amr->GetDataSet(level,id);
          vtkIdTypeArray* cd1 = vtkArrayDownCast<vtkIdTypeArray>(grid->GetCellData()->GetArray("vtkIdFilter_Ids"));
          vtkIdType cellId1 = FindCell(grid,x);
          vtkIdType value1 = cd1->GetValue(cellId1);
          if(value1!=value)
          {
            return VTK_FAILURE;
          }
        }
        else
        {
          return VTK_FAILURE;
        }

      }
    }
  }

  return VTK_SUCCESS;
}