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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkExtractRectilinearGrid.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.
=========================================================================*/
// VTK includes
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkExtractRectilinearGrid.h"
#include "vtkMathUtilities.h"
#include "vtkPointData.h"
#include "vtkRectilinearGrid.h"
#include "vtkRectilinearGridWriter.h"
#include "vtkStructuredData.h"
// C/C++ includes
#include <cassert>
#include <cmath>
#include <sstream>
//#define DEBUG
double exponential_distribution(const int i, const double beta)
{
double xi = ( ( exp( i*beta ) - 1 ) /( exp( beta ) - 1 ) );
return( xi );
}
//------------------------------------------------------------------------------
void WriteGrid(vtkRectilinearGrid* grid, const std::string& file)
{
assert( "pre: input grid instance is NULL!" && (grid != NULL) );
std::ostringstream oss;
oss << file << ".vtk";
vtkRectilinearGridWriter* writer = vtkRectilinearGridWriter::New();
writer->SetFileName( oss.str().c_str() );
writer->SetInputData( grid );
writer->Write();
writer->Delete();
}
//------------------------------------------------------------------------------
int CheckGrid( vtkRectilinearGrid* grid )
{
int rc = 0;
vtkPointData* PD = grid->GetPointData();
if( !PD->HasArray("xyz") )
{
++rc;
return( rc );
}
vtkDoubleArray* xyz_data = vtkArrayDownCast<vtkDoubleArray>(PD->GetArray("xyz"));
double* xyz = static_cast<double*>( xyz_data->GetVoidPointer(0));
vtkIdType npoints = grid->GetNumberOfPoints();
for( vtkIdType pntIdx=0; pntIdx < npoints; ++pntIdx )
{
double* pnt = grid->GetPoint( pntIdx );
if( !vtkMathUtilities::NearlyEqual(pnt[0],xyz[pntIdx*3],1.e-9) ||
!vtkMathUtilities::NearlyEqual(pnt[1],xyz[pntIdx*3+1],1.e-9) ||
!vtkMathUtilities::NearlyEqual(pnt[2],xyz[pntIdx*3+2],1.e-9) )
{
std::cerr << "ERROR: point=(" << pnt[0] << ", ";
std::cerr << pnt[1] << ", ";
std::cerr << pnt[2] << ") ";
std::cerr << "data = (" << xyz[pntIdx*3] << ", ";
std::cerr << xyz[pntIdx*3+1] << ", ";
std::cerr << xyz[pntIdx*3+2] << ") ";
++rc;
} // END if
} // END for all points
return( rc );
}
//------------------------------------------------------------------------------
void GenerateGrid( vtkRectilinearGrid* grid, int ext[6] )
{
assert( "pre: input grid instance is NULL!" && (grid != NULL) );
grid->Initialize();
grid->SetExtent(ext);
vtkDataArray* coords[3];
int dims[3];
int dataDesc = vtkStructuredData::GetDataDescriptionFromExtent(ext);
vtkStructuredData::GetDimensionsFromExtent(ext,dims,dataDesc);
// compute & populate coordinate vectors
double beta = 0.05; /* controls the intensity of the stretching */
for(int i=0; i < 3; ++i)
{
coords[i] = vtkDataArray::CreateDataArray(VTK_DOUBLE);
if( dims[i] == 0 )
{
continue;
}
coords[i]->SetNumberOfTuples(dims[i]);
double prev = 0.0;
for(int j=0; j < dims[i]; ++j)
{
double val = prev + ( (j==0)? 0.0 : exponential_distribution(j,beta) );
coords[ i ]->SetTuple( j, &val );
prev = val;
} // END for all points along this dimension
} // END for all dimensions
grid->SetXCoordinates( coords[0] );
grid->SetYCoordinates( coords[1] );
grid->SetZCoordinates( coords[2] );
coords[0]->Delete();
coords[1]->Delete();
coords[2]->Delete();
// compute & populate XYZ field
vtkIdType npoints = vtkStructuredData::GetNumberOfPoints(ext,dataDesc);
vtkDoubleArray* xyz = vtkDoubleArray::New();
xyz->SetName( "xyz" );
xyz->SetNumberOfComponents(3);
xyz->SetNumberOfTuples( npoints );
for(vtkIdType pntIdx=0; pntIdx < npoints; ++pntIdx )
{
xyz->SetTuple(pntIdx, grid->GetPoint(pntIdx) );
} // END for all points
grid->GetPointData()->AddArray( xyz );
xyz->Delete();
}
//------------------------------------------------------------------------------
int TestExtractRectilinearGrid( int argc, char* argv[])
{
int rc = 0;
// silence compiler warnings
static_cast<void>(argc);
static_cast<void>(argv);
int ext[6] = {0,49,0,49,0,0};
vtkRectilinearGrid* grid = vtkRectilinearGrid::New();
GenerateGrid( grid, ext );
#ifdef DEBUG
WriteGrid( grid, "initial" );
#endif
int sub_ext[6] = {0,35,0,35,0,0};
vtkExtractRectilinearGrid* extractFilter = vtkExtractRectilinearGrid::New();
extractFilter->SetInputData( grid );
extractFilter->SetVOI(sub_ext);
extractFilter->SetSampleRate(2,2,1);
extractFilter->IncludeBoundaryOn();
extractFilter->Update();
vtkRectilinearGrid* subGrid = extractFilter->GetOutput();
#ifdef DEBUG
WriteGrid( subGrid, "sub-grid" );
#endif
rc += CheckGrid( subGrid );
extractFilter->Delete();
grid->Delete();
return( rc );
}
|