Commit 14c8487e authored by Kenneth Moreland's avatar Kenneth Moreland Committed by Kyle Lutz
Browse files

Safer fake coordinates for structured from unstructured CF grids

The vtkNetCDFCFReader allows you to specify the output type and makes
the best attempt to represent the data in that type.  Even though
sometimes the representation makes little sense (like storing an
unstructured grid in an extent-based data set like structured grids),
the reader should at least continue to be stable (see ParaView bug

There was a corner case in one of these nonsensical conversions where a
data array was read one past its end.  This change makes a different way
to create nonsense and avoids the bad array access.

Change-Id: Idc30bb9aa8474c1a67c36b22f05aa99428b1b37e
parent bc424e27
......@@ -32,6 +32,7 @@
#include "vtkIntArray.h"
#include "vtkMath.h"
#include "vtkMergePoints.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkRectilinearGrid.h"
......@@ -1071,7 +1072,17 @@ int vtkNetCDFCFReader::RequestData(vtkInformation *request,
= vtkRectilinearGrid::GetData(outputVector);
if (rectilinearOutput)
{
this->AddRectilinearCoordinates(rectilinearOutput);
switch (this->CoordinateType(this->LoadingDimensions))
{
case COORDS_EUCLIDEAN_PSIDED_CELLS:
case COORDS_SPHERICAL_PSIDED_CELLS:
// There is no sensible way to store p-sided cells in a structured grid.
// Just fake some coordinates (related to ParaView bug #11543).
this->FakeRectilinearCoordinates(rectilinearOutput);
break;
default:
this->AddRectilinearCoordinates(rectilinearOutput);
}
}
vtkStructuredGrid *structuredOutput
......@@ -1098,9 +1109,8 @@ int vtkNetCDFCFReader::RequestData(vtkInformation *request,
case COORDS_EUCLIDEAN_PSIDED_CELLS:
case COORDS_SPHERICAL_PSIDED_CELLS:
// There is no sensible way to store p-sided cells in a structured grid.
// Just store them as a rectilinear grid, which should at least not
// crash (bug #11543).
this->Add1DRectilinearCoordinates(structuredOutput);
// Just fake some coordinates (ParaView bug #11543).
this->FakeStructuredCoordinates(structuredOutput);
break;
default:
vtkErrorMacro("Internal error: unknown coordinate type.");
......@@ -1244,6 +1254,34 @@ void vtkNetCDFCFReader::AddRectilinearCoordinates(
case 0: rectilinearOutput->SetXCoordinates(coords); break;
case 1: rectilinearOutput->SetYCoordinates(coords); break;
case 2: rectilinearOutput->SetZCoordinates(coords); break;
default: vtkErrorMacro("Sanity check failed!"); break;
}
}
}
//-----------------------------------------------------------------------------
void vtkNetCDFCFReader::FakeRectilinearCoordinates(
vtkRectilinearGrid *rectilinearOutput)
{
int dimensionSizes[3];
rectilinearOutput->GetDimensions(dimensionSizes);
for (int dim = 0; dim < 3; dim++)
{
vtkNew<vtkDoubleArray> coordinate;
coordinate->SetNumberOfComponents(1);
coordinate->SetNumberOfTuples(dimensionSizes[dim]);
for (int index = 0; index < dimensionSizes[dim]; index++)
{
coordinate->SetComponent(index, 0, static_cast<double>(index));
}
switch(dim)
{
case 0: rectilinearOutput->SetXCoordinates(coordinate.GetPointer());break;
case 1: rectilinearOutput->SetYCoordinates(coordinate.GetPointer());break;
case 2: rectilinearOutput->SetZCoordinates(coordinate.GetPointer());break;
default: vtkErrorMacro("Sanity check failed!"); break;
}
}
}
......@@ -1378,6 +1416,33 @@ void vtkNetCDFCFReader::Add2DRectilinearCoordinates(
structuredOutput->SetPoints(points);
}
//-----------------------------------------------------------------------------
void vtkNetCDFCFReader::FakeStructuredCoordinates(
vtkStructuredGrid *structuredOutput)
{
int extent[6];
structuredOutput->GetExtent(extent);
vtkNew<vtkPoints> points;
points->SetDataTypeToDouble();
points->Allocate( (extent[1]-extent[0]+1)
* (extent[3]-extent[2]+1)
* (extent[5]-extent[4]+1) );
for (int kIndex = extent[4]; kIndex <= extent[5]; kIndex++)
{
for (int jIndex = extent[2]; jIndex <= extent[3]; jIndex++)
{
for (int iIndex = extent[0]; iIndex <= extent[1]; iIndex++)
{
points->InsertNextPoint(iIndex, jIndex, kIndex);
}
}
}
structuredOutput->SetPoints(points.GetPointer());
}
//-----------------------------------------------------------------------------
void vtkNetCDFCFReader::Add1DRectilinearCoordinates(
vtkUnstructuredGrid *unstructuredOutput,
......
......@@ -252,10 +252,12 @@ protected:
// Internal methods for setting rectilinear coordinates.
void AddRectilinearCoordinates(vtkImageData *imageOutput);
void AddRectilinearCoordinates(vtkRectilinearGrid *rectilinearOutput);
void FakeRectilinearCoordinates(vtkRectilinearGrid *rectilinearOutput);
void Add1DRectilinearCoordinates(vtkPoints *points, const int extent[6]);
void Add2DRectilinearCoordinates(vtkPoints *points, const int extent[6]);
void Add1DRectilinearCoordinates(vtkStructuredGrid *structuredOutput);
void Add2DRectilinearCoordinates(vtkStructuredGrid *structuredOutput);
void FakeStructuredCoordinates(vtkStructuredGrid *structuredOutput);
void Add1DRectilinearCoordinates(vtkUnstructuredGrid *unstructuredOutput,
const int extent[6]);
void Add2DRectilinearCoordinates(vtkUnstructuredGrid *unstructuredOutput,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment