Skip to content
Snippets Groups Projects
Commit c8bec1f2 authored by David C. Lonie's avatar David C. Lonie
Browse files

Handle polyhedra in vtkUnstructuredGridWriter.

Change-Id: I6c20eb8650e2b079c8b1f581c9727a3d8205087a
parent 42b0733e
No related branches found
No related tags found
No related merge requests found
......@@ -16,10 +16,17 @@
#include "vtkByteSwap.h"
#include "vtkCellArray.h"
#include "vtkCellIterator.h"
#include "vtkErrorCode.h"
#include "vtkInformation.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkUnstructuredGrid.h"
#include <algorithm>
#include <iterator>
#include <vector>
#if !defined(_WIN32) || defined(__CYGWIN__)
# include <unistd.h> /* unlink */
#else
......@@ -69,12 +76,29 @@ void vtkUnstructuredGridWriter::WriteData()
unlink(this->FileName);
return;
}
if (!this->WriteCells(fp, input->GetCells(),"CELLS"))
// Write cells. Check for faces so that we can handle them if present:
if (input->GetFaces() != NULL)
{
vtkErrorMacro("Ran out of disk space; deleting file: " << this->FileName);
this->CloseVTKFile(fp);
unlink(this->FileName);
return;
// Handle face data:
if (!this->WriteCellsAndFaces(fp, input, "CELLS"))
{
vtkErrorMacro("Ran out of disk space; deleting file: " << this->FileName);
this->CloseVTKFile(fp);
unlink(this->FileName);
return;
}
}
else
{
// Fall back to superclass:
if (!this->WriteCells(fp, input->GetCells(),"CELLS"))
{
vtkErrorMacro("Ran out of disk space; deleting file: " << this->FileName);
this->CloseVTKFile(fp);
unlink(this->FileName);
return;
}
}
//
......@@ -124,6 +148,81 @@ void vtkUnstructuredGridWriter::WriteData()
this->CloseVTKFile(fp);
}
int vtkUnstructuredGridWriter::WriteCellsAndFaces(
ostream *fp, vtkUnstructuredGrid *grid, const char *label)
{
if (!grid->GetCells())
{
return 1;
}
// Create a copy of the cell data with the face streams expanded.
// Do this before writing anything so that we know the size.
// Use ints to represent vtkIdTypes, since that's what the superclass does.
std::vector<int> cells;
cells.reserve(grid->GetNumberOfCells() * grid->GetMaxCellSize());
vtkSmartPointer<vtkCellIterator> it =
vtkSmartPointer<vtkCellIterator>::Take(grid->NewCellIterator());
for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell())
{
if (it->GetCellType() != VTK_POLYHEDRON)
{
vtkIdType cellSize = it->GetNumberOfPoints();
cells.push_back(static_cast<int>(cellSize));
std::copy(it->GetPointIds()->GetPointer(0),
it->GetPointIds()->GetPointer(cellSize),
std::back_inserter(cells));
}
else
{
vtkIdType cellSize = it->GetFaces()->GetNumberOfIds();
cells.push_back(static_cast<int>(cellSize));
std::copy(it->GetFaces()->GetPointer(0),
it->GetFaces()->GetPointer(cellSize),
std::back_inserter(cells));
}
}
if (cells.empty())
{ // Nothing to do.
return 1;
}
*fp << label << " " << grid->GetNumberOfCells() << " "
<< cells.size() << "\n";
if ( this->FileType == VTK_ASCII )
{ // Write each cell out to a separate line, must traverse:
std::vector<int>::const_iterator cellStart = cells.begin();
std::vector<int>::const_iterator cellEnd;
vtkIdType nCells = grid->GetNumberOfCells();
while (nCells-- > 0)
{
cellEnd = cellStart + (*cellStart + 1);
while (cellStart != cellEnd)
*fp << static_cast<int>(*cellStart++) << " ";
*fp << "\n";
}
}
else
{
// Just dump the cell data
vtkByteSwap::SwapWrite4BERange(&cells[0], cells.size(), fp);
*fp << "\n";
}
fp->flush();
if (fp->fail())
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return 0;
}
return 1;
}
int vtkUnstructuredGridWriter::FillInputPortInformation(int,
vtkInformation *info)
{
......
......@@ -44,6 +44,9 @@ protected:
void WriteData();
int WriteCellsAndFaces(ostream *fp, vtkUnstructuredGrid *grid,
const char *label);
virtual int FillInputPortInformation(int port, vtkInformation *info);
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment