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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestGeometryFilterCellData.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 that the proper amount of tuples exist in the
// point and cell data arrays after the vtkGeometryFilter
// is used.
#include <iostream>
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkGeometryFilter.h"
#include "vtkIdTypeArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include "vtkUnstructuredGrid.h"
int TestGeometryFilter( vtkUnstructuredGrid* ug );
int CheckDataSet( vtkDataSet* d );
int CheckFieldData( vtkIdType numGridEntities, vtkFieldData* fd );
// Creates a vtkUnstructuredGrid
class GridFactory
{
public:
GridFactory();
void AddVertexCells();
void AddLineCells();
void AddTriangleCells();
void AddTetraCells();
vtkUnstructuredGrid* Get();
private:
vtkSmartPointer<vtkUnstructuredGrid> Grid;
};
GridFactory::GridFactory() :
Grid ( vtkSmartPointer<vtkUnstructuredGrid>::New() )
{
// the points
static float x[8][3]={{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}};
std::cout << "Defining 8 points\n";
// Create the points
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->SetNumberOfPoints( 8 );
for ( int i = 0; i < 8; ++i )
{
points->SetPoint( i, x[i] );
}
this->Grid->SetPoints( points );
}
// Create 2 tetras
void GridFactory::AddTetraCells()
{
if ( ! this->Grid )
return;
std::cout << "Adding 2 tetra cells\n";
vtkIdType pts[4];
pts[0] = 0; pts[1] = 1; pts[2] = 2; pts[3] = 3;
this->Grid->InsertNextCell( VTK_TETRA, 4, pts );
pts[0] = 2; pts[1] = 3; pts[2] = 4; pts[3] = 5;
this->Grid->InsertNextCell( VTK_TETRA, 4, pts );
}
// Create 2 triangles
void GridFactory::AddTriangleCells()
{
if ( ! this->Grid )
return;
std::cout << "Adding 2 triangle cells\n";
vtkIdType pts[3];
pts[0] = 1; pts[1] = 3; pts[2] = 5;
this->Grid->InsertNextCell( VTK_TRIANGLE, 3, pts );
pts[0] = 2; pts[1] = 4; pts[2] = 6;
this->Grid->InsertNextCell( VTK_TRIANGLE, 3, pts );
}
// Create 2 lines
void GridFactory::AddLineCells()
{
if ( ! this->Grid )
return;
std::cout << "Adding 2 line cells\n";
vtkIdType pts[2];
pts[0] = 3; pts[1] = 7;
this->Grid->InsertNextCell( VTK_LINE, 2, pts );
pts[0] = 0; pts[1] = 4;
this->Grid->InsertNextCell( VTK_LINE, 2, pts );
}
// Create 2 points
void GridFactory::AddVertexCells()
{
if ( ! this->Grid )
return;
std::cout << "Adding 2 vertex cells\n";
vtkIdType pts[1];
pts[0] = 7;
this->Grid->InsertNextCell( VTK_VERTEX, 1, pts );
pts[0] = 6;
this->Grid->InsertNextCell( VTK_VERTEX, 1, pts );
}
// Add cell data and point data for all cells/points, and
// return the unstructured grid.
vtkUnstructuredGrid* GridFactory::Get()
{
if ( ! this->Grid )
return NULL;
// Create a point data array
const char* name = "foo";
int num = this->Grid->GetNumberOfPoints();
std::cout << "Adding point data array '"<<name<<"' with data for "<<num<<" points\n";
vtkSmartPointer<vtkIdTypeArray> pointDataArray = vtkSmartPointer<vtkIdTypeArray>::New();
pointDataArray->SetName( name );
// Creating data for 8 points
for ( int i = 0; i < num; ++i )
{
vtkIdType value = i+100;
pointDataArray->InsertNextTupleValue( &value );
}
this->Grid->GetPointData()->AddArray( pointDataArray );
// Create the cell data array
name = "bar";
num = this->Grid->GetNumberOfCells();
std::cout << "Adding cell data array '"<<name<<"' with data for "<<num<<" cells\n";
vtkSmartPointer<vtkIdTypeArray> cellDataArray = vtkSmartPointer<vtkIdTypeArray>::New();
cellDataArray->SetName( name );
cellDataArray->SetNumberOfComponents( 1 );
for ( int i = 0; i < num; ++i )
{
vtkIdType value = i+200;
cellDataArray->InsertNextTupleValue( &value );
}
this->Grid->GetCellData()->AddArray( cellDataArray );
return this->Grid;
}
int TestGeometryFilterCellData(int, char*[])
{
vtkSmartPointer<vtkUnstructuredGrid> ug;
// Build the unstructured grid
GridFactory g;
g.AddTetraCells();
g.AddTriangleCells();
g.AddLineCells();
g.AddVertexCells();
ug = g.Get();
// Run it through vtkGeometryFilter
int retVal = TestGeometryFilter( ug );
return retVal;
}
// Runs the unstructured grid through the vtkGeometryFilter and prints the
// output
int TestGeometryFilter( vtkUnstructuredGrid* ug )
{
// Print the input unstructured grid dataset
std::cout << "\nvtkGeometryFilter input:\n";
int retVal = CheckDataSet( ug );
// Do the filtering
vtkSmartPointer<vtkGeometryFilter> gf = vtkSmartPointer<vtkGeometryFilter>::New();
gf->SetInputData( ug );
gf->Update();
// Print the output poly data
std::cout << "\nvtkGeometryFilter output:\n";
vtkPolyData *poly = vtkPolyData::SafeDownCast( gf->GetOutput( ) );
retVal += CheckDataSet( poly );
return retVal;
}
int CheckDataSet( vtkDataSet* d )
{
if ( ! d )
{
std::cout << "No dataset\n";
return 1;
}
const char* name;
if ( vtkUnstructuredGrid::SafeDownCast( d ) )
name = "vtkUnstructuredGrid";
else if ( vtkPolyData::SafeDownCast( d ) )
name = "vtkPolyData";
else
name = "vtkDataSet";
std::cout << name
<< " dimensions: #cells="<<d->GetNumberOfCells()
<< " #points=" << d->GetNumberOfPoints()<< "\n";
int retVal = CheckFieldData( d->GetNumberOfPoints(), d->GetPointData() );
retVal += CheckFieldData( d->GetNumberOfCells(), d->GetCellData() );
return retVal;
}
int CheckFieldData( vtkIdType numGridEntities, vtkFieldData* fd )
{
int retVal = 0;
if ( ! fd )
{
std::cout << "No field data\n";
return 1;
}
const char* name;
if ( vtkCellData::SafeDownCast( fd ) )
{
name = "cell data";
}
else if ( vtkPointData::SafeDownCast( fd ) )
{
name = "point data";
}
else
{
name = "field data";
}
for ( int i = 0; i < fd->GetNumberOfArrays(); ++i )
{
vtkAbstractArray *a = fd->GetArray( i );
if(a->GetNumberOfTuples() != numGridEntities)
{
vtkGenericWarningMacro(<< name << " array '" << a->GetName() << "' has #tuples="<< a->GetNumberOfTuples()
<< " but should have " << numGridEntities);
retVal = 1;
}
}
return retVal;
}
|