-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestPartialVolumeModeller.cxx
201 lines (164 loc) · 6.27 KB
/
TestPartialVolumeModeller.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPartialVolumeModeller.h
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 <iostream>
#include <vtkCommand.h>
#include <vtkDoubleArray.h>
#include <vtkImageData.h>
#include <vtkImageShiftScale.h>
#include <vtkPNGWriter.h>
#include <vtkPoints.h>
#include <vtkRectilinearGridToTetrahedra.h>
#include <vtkTetra.h>
#include <vtkType.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVoxelModeller.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include "vtkPartialVolumeModeller.h"
class vtkProgressCommand : public vtkCommand
{
public:
static vtkProgressCommand *New(){
return new vtkProgressCommand;
}
virtual void Execute(vtkObject *caller, unsigned long, void *callData){
double progress = *(static_cast<double*>(callData));
fprintf(stderr, "\rFilter progress: %5.1f\n", 100.0 * progress);
std::cerr.flush();
}
};
int main(int argc, char* argv[])
{
// Create a box partitioned into tetrahedra.
double spacing = 5.0;
double boxVoxelsCoveredX = 20;
double boxVoxelsCoveredY = 30;
double boxVoxelsCoveredZ = 15;
double boxWidth = spacing * boxVoxelsCoveredX;
double boxHeight = spacing * boxVoxelsCoveredY;
double boxDepth = spacing * boxVoxelsCoveredZ;
// Node numbering scheme
// 7-----6
// /| /|
// 4-+---5 |
// | 3---+-2
// |/ |/
// 0-----1
double tetPoints[8][3] = {
{0.0, 0.0, boxDepth},
{boxWidth, 0.0, boxDepth},
{boxWidth, 0.0, 0.0},
{0.0, 0.0, 0.0},
{0.0, boxHeight, boxDepth},
{boxWidth, boxHeight, boxDepth},
{boxWidth, boxHeight, 0.0},
{0.0, boxHeight, 0.0}};
vtkIdType tetIndices[6][4] = {
{1, 3, 0, 4},
{4, 1, 3, 5},
{4, 7, 5, 3},
{5, 7, 6, 3},
{1, 3, 5, 2},
{3, 2, 6, 5}
};
vtkUnstructuredGrid *grid = vtkUnstructuredGrid::New();
vtkPoints* gridPoints = vtkPoints::New();
for (int i = 0; i < 8; i++)
{
gridPoints->InsertNextPoint(tetPoints[i]);
}
grid->SetPoints(gridPoints);
for (int i = 0; i < 6; i++)
{
grid->InsertNextCell(VTK_TETRA, 4, tetIndices[i]);
}
vtkXMLUnstructuredGridWriter *gridWriter = vtkXMLUnstructuredGridWriter::New();
gridWriter->SetFileName("TetrahedralGrid.vtu");
gridWriter->SetInput(grid);
gridWriter->Write();
// Set up partial volume modeller to create an image larger than the
// extent of the box.
double padding = 3 * spacing;
double off = 0.0; // An offset applied to all modeller bounds
double xmin = -padding + off;
double xmax = boxWidth + padding + off;
double ymin = -padding + off;
double ymax = boxHeight + padding + off;
double zmin = -padding + off;
double zmax = boxDepth + padding + off;
int imageDimsX = static_cast<int>((xmax - xmin) / spacing) + 1;
int imageDimsY = static_cast<int>((ymax - ymin) / spacing) + 1;
int imageDimsZ = static_cast<int>((zmax - zmin) / spacing) + 1;
std::cout << "Creating image of dimensions (" << imageDimsX << ", " << imageDimsY << ", "
<< imageDimsZ << ")" << std::endl;
vtkProgressCommand * pobserver = vtkProgressCommand::New();
// Try out the vtkPartialVolumeModeller
vtkPartialVolumeModeller *partialVolumeModeller = vtkPartialVolumeModeller::New();
partialVolumeModeller->SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax);
partialVolumeModeller->SetSampleDimensions(imageDimsX, imageDimsY, imageDimsZ);
partialVolumeModeller->SetInput(grid);
partialVolumeModeller->AddObserver(vtkCommand::ProgressEvent, pobserver);
partialVolumeModeller->Update();
pobserver->Delete();
// Check out the results at the side edge of the box. Should be 0.25.
double edgeValue = partialVolumeModeller->GetOutput()->
GetScalarComponentAsDouble(3, 3, 4, 0);
if (fabs(edgeValue - 0.25) > 1e-5)
{
std::cerr << "Expected value at voxel (3, 3, 4) should be 0.25, got "
<< edgeValue << std::endl;
return EXIT_FAILURE;
}
// Check out the results at the corner of the box. Should be 0.125.
double cornerValue = partialVolumeModeller->GetOutput()->
GetScalarComponentAsDouble(3, 3, 3, 0);
if (fabs(cornerValue - 0.125) > 1e-5)
{
std::cerr << "Expected value at voxel (3, 3, 3) to be 0.125, got "
<< cornerValue << std::endl;
return EXIT_FAILURE;
}
vtkXMLImageDataWriter* imageWriter = vtkXMLImageDataWriter::New();
imageWriter->SetFileName("PartialVolumeImageData.vti");
imageWriter->SetInputConnection(partialVolumeModeller->GetOutputPort());
imageWriter->Write();
// Scale values to range necessary for image file formats.
vtkImageShiftScale *shiftScale = vtkImageShiftScale::New();
shiftScale->SetShift(0.0);
shiftScale->SetScale(255.0);
shiftScale->SetOutputScalarTypeToUnsignedChar();
shiftScale->SetInputConnection(partialVolumeModeller->GetOutputPort());
// Save as a PNG.
vtkPNGWriter *writer = vtkPNGWriter::New();
writer->SetFilePattern("PartialVolumeModellerImage%04d.png");
writer->SetFileDimensionality(2);
writer->SetInputConnection(shiftScale->GetOutputPort());
writer->Write();
// Try the vtkVoxelModeller for comparison
vtkVoxelModeller *voxelModeller = vtkVoxelModeller::New();
voxelModeller->SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax);
voxelModeller->SetSampleDimensions(imageDimsX, imageDimsY, imageDimsZ);
voxelModeller->SetScalarTypeToFloat();
voxelModeller->SetInput(grid);
voxelModeller->Update();
imageWriter->SetFileName("VoxelModellerImageData.vti");
imageWriter->SetInputConnection(partialVolumeModeller->GetOutputPort());
imageWriter->Write();
shiftScale->SetInputConnection(voxelModeller->GetOutputPort());
writer->SetFilePattern("VoxelModellerImage%04d.png");
writer->Write();
writer->Delete();
imageWriter->Delete();
shiftScale->Delete();
voxelModeller->Delete();
partialVolumeModeller->Delete();
return EXIT_SUCCESS;
}