Commit 9338f0b8 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: adjust wrapping routines for new vtkCellArray definition

- the vtkCellArray internal structure was still largely oriented on
  the VTK legacy format, but has now been revised.
  https://gitlab.kitware.com/vtk/vtk/merge_requests/5682

  The `VTK_CELL_ARRAY_V2` define from vtkCellArray.h indicates
  that the newer version is being used.

* In VTK-8.2.0 and older, sizes are interwoven (prefixed) in the
  connectivity.

  Connectivity: [n1, verts..., n2, verts... ]

  When using these in vtkUnstructuredGrid, also needed a secondary
  list of offsets for each of the starting locations.

* The update version now resembles a CompactListList. For example

  Connectivity: [verts..., verts... ]
  Offsets:      [0, n1, n1+n2, n1+n2+n3... ]

  The offsets are properly handled within vtkCellArray, and dropped as
  an additional input for vtkUnstructuredGrid.
parent f4ee841c
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -70,8 +70,6 @@ class vtkDataSet;
class vtkCellData;
class vtkPointData;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace vtk
......@@ -185,15 +183,6 @@ namespace Tools
const label size
);
//- Wrap vtkCellArray as a UList
inline UList<vtkIdType> asUList
(
vtkCellArray* cells,
const label nCells,
const label size
);
//- Return a list of points as vtkPoints
inline vtkSmartPointer<vtkPoints> Points
(
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,6 +25,8 @@ License
\*---------------------------------------------------------------------------*/
#include <numeric>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
inline Foam::UList<uint8_t> Foam::vtk::Tools::asUList
......@@ -53,19 +55,6 @@ inline Foam::UList<vtkIdType> Foam::vtk::Tools::asUList
}
inline Foam::UList<vtkIdType> Foam::vtk::Tools::asUList
(
vtkCellArray* cells,
const label nCells,
const label size
)
{
cells->GetData()->SetNumberOfTuples(size);
return UList<vtkIdType>(cells->WritePointer(nCells, size), size);
}
inline vtkSmartPointer<vtkPoints>
Foam::vtk::Tools::Points(const UList<point>& pts)
{
......@@ -150,20 +139,72 @@ inline vtkSmartPointer<vtkCellArray> Foam::vtk::Tools::identityVertices
const label size
)
{
// VTK_VERTEX: need 2 values (size=1 and index=id) per vertex
auto cells = vtkSmartPointer<vtkCellArray>::New();
UList<vtkIdType> list = asUList(cells, size, 2*size);
#ifdef VTK_CELL_ARRAY_V2
// Offsets
// [0, n1, n1+n2, n1+n2+n3... ]
auto offsets = vtkSmartPointer<vtkIdTypeArray>::New();
{
const vtkIdType nOffsets(size+1);
offsets->SetNumberOfTuples(nOffsets);
vtkIdType* iter = offsets->WritePointer(0, nOffsets);
std::iota(iter, (iter + nOffsets), vtkIdType(0));
}
auto connect = vtkSmartPointer<vtkIdTypeArray>::New();
// Connectivity
{
const vtkIdType nConnect(size);
connect->SetNumberOfTuples(nConnect);
vtkIdType* iter = connect->WritePointer(0, nConnect);
std::iota(iter, (iter + nConnect), vtkIdType(0));
}
// Move into a vtkCellArray
cells->SetData(offsets, connect);
#else
// In VTK-8.2.0 and older,
// sizes are interwoven (prefixed) in the connectivity
// Connectivity size, with prefixed size information
// Cell connectivity for vertex
// [size, ids.., size, ids...] -> therefore [1, id, 1, id, ...]
auto iter = list.begin();
for (label id=0; id < size; ++id)
const vtkIdType nElem(size);
const vtkIdType nConnect(2*size);
{
*(iter++) = 1;
*(iter++) = id;
cells->GetData()->SetNumberOfTuples(nConnect);
vtkIdType* iter = cells->WritePointer(nElem, nConnect);
// Fill in the connectivity array, with prefixed size information
for (vtkIdType id = 0; id < nElem; ++id)
{
*(iter++) = 1;
*(iter++) = id;
}
}
#endif
return cells;
};
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,30 +43,99 @@ template<class Face>
vtkSmartPointer<vtkCellArray>
Foam::vtk::Tools::Faces(const UList<Face>& faces)
{
label nAlloc = faces.size();
for (const auto& f : faces)
auto cells = vtkSmartPointer<vtkCellArray>::New();
#ifdef VTK_CELL_ARRAY_V2
// Offsets
// [0, n1, n1+n2, n1+n2+n3... ]
const vtkIdType nOffsets(faces.size()+1);
auto offsets = vtkSmartPointer<vtkIdTypeArray>::New();
vtkIdType nConnect(0);
{
offsets->SetNumberOfTuples(nOffsets);
vtkIdType* iter = offsets->WritePointer(0, nOffsets);
// Assign offsets, determine overall connectivity size
*iter = 0;
for (const auto& f : faces)
{
nConnect += f.size();
*(++iter) = nConnect;
}
}
// Cell connectivity for polygons
// [verts..., verts... ]
auto connect = vtkSmartPointer<vtkIdTypeArray>::New();
{
nAlloc += f.size();
connect->SetNumberOfTuples(nConnect);
vtkIdType* iter = connect->WritePointer(0, nConnect);
// Fill in the connectivity array
for (const auto& f : faces)
{
for (const label verti : f)
{
*(iter++) = verti;
}
}
}
auto vtkcells = vtkSmartPointer<vtkCellArray>::New();
// Move into a vtkCellArray
UList<vtkIdType> list = asUList(vtkcells, faces.size(), nAlloc);
cells->SetData(offsets, connect);
#else
// In VTK-8.2.0 and older,
// sizes are interwoven (prefixed) in the connectivity
// Cell connectivity for polygons
// [size, verts..., size, verts... ]
auto iter = list.begin();
// [n1, verts..., n2, verts... ]
const vtkIdType nElem(faces.size());
// Connectivity size, with prefixed size information
vtkIdType nConnect(faces.size());
for (const auto& f : faces)
{
*(iter++) = f.size();
nConnect += f.size();
}
{
cells->GetData()->SetNumberOfTuples(nConnect);
vtkIdType* iter = cells->WritePointer(nElem, nConnect);
for (const label verti : f)
// Fill in the connectivity array, with prefixed size information
for (const auto& f : faces)
{
*(iter++) = verti;
*(iter++) = f.size();
for (const label verti : f)
{
*(iter++) = verti;
}
}
}
return vtkcells;
#endif
return cells;
}
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -52,20 +52,15 @@ SourceFiles
#include "foamVtkMeshMaps.H"
#include "foamVtuSizing.H"
#include "vtkSmartPointer.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkUnstructuredGrid.h"
#include "vtkMultiBlockDataSet.h"
// * * * * * * * * * * * * * Forward Declarations * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
class vtkCellArray;
// Forward Declarations
class vtkDataSet;
class vtkFloatArray;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace vtk
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -103,89 +103,127 @@ Foam::vtk::vtuAdaptor::internal
const bool decompPoly
)
{
const vtk::vtuSizing::contentType output
(
#ifdef VTK_CELL_ARRAY_V2
vtk::vtuSizing::contentType::INTERNAL2
#else
vtk::vtuSizing::contentType::INTERNAL1
#endif
);
vtk::vtuSizing sizing(mesh, decompPoly);
auto vtkmesh = vtkSmartPointer<vtkUnstructuredGrid>::New();
auto cellTypes = vtkSmartPointer<vtkUnsignedCharArray>::New();
UList<uint8_t> cellTypesUL
(
vtk::Tools::asUList(cellTypes, sizing.nFieldCells())
);
auto cells = vtkSmartPointer<vtkCellArray>::New();
auto faces = vtkSmartPointer<vtkIdTypeArray>::New();
auto cellLocations = vtkSmartPointer<vtkIdTypeArray>::New();
auto cellOffsets = vtkSmartPointer<vtkIdTypeArray>::New();
auto faceLocations = vtkSmartPointer<vtkIdTypeArray>::New();
UList<uint8_t> cellTypesUL =
vtk::Tools::asUList(cellTypes, sizing.nFieldCells());
const auto nConnect
(
sizing.sizeOf(output, vtk::vtuSizing::slotType::CELLS)
);
UList<vtkIdType> cellsUL =
UList<vtkIdType> cellOffsetsUL
(
vtk::Tools::asUList
(
cells,
sizing.nFieldCells(),
sizing.sizeInternal(vtk::vtuSizing::slotType::CELLS)
);
cellOffsets,
sizing.sizeOf(output, vtk::vtuSizing::slotType::CELLS_OFFSETS)
)
);
UList<vtkIdType> cellLocationsUL =
vtk::Tools::asUList
(
cellLocations,
sizing.sizeInternal(vtk::vtuSizing::slotType::CELLS_OFFSETS)
);
#ifdef VTK_CELL_ARRAY_V2
auto cellConnect = vtkSmartPointer<vtkIdTypeArray>::New();
UList<vtkIdType> cellsUL
(
vtk::Tools::asUList(cellConnect, nConnect)
);
#else
cells->GetData()->SetNumberOfTuples(sizing.nFieldCells());
UList<vtkIdType> cellsUL
(
cells->WritePointer(sizing.nFieldCells(), nConnect),
nConnect
);
#endif
UList<vtkIdType> facesUL =
UList<vtkIdType> facesUL
(
vtk::Tools::asUList
(
faces,
sizing.sizeInternal(vtk::vtuSizing::slotType::FACES)
);
sizing.sizeOf(output, vtk::vtuSizing::slotType::FACES)
)
);
UList<vtkIdType> faceLocationsUL =
UList<vtkIdType> faceLocationsUL
(
vtk::Tools::asUList
(
faceLocations,
sizing.sizeInternal(vtk::vtuSizing::slotType::FACES_OFFSETS)
);
sizing.sizeOf(output, vtk::vtuSizing::slotType::FACES_OFFSETS)
)
);
sizing.populateInternal
(
mesh,
cellTypesUL,
cellsUL,
cellLocationsUL,
facesUL,
faceLocationsUL,
static_cast<foamVtkMeshMaps&>(*this)
cellsUL, cellOffsetsUL,
facesUL, faceLocationsUL,
static_cast<foamVtkMeshMaps&>(*this),
output
);
auto vtkmesh = vtkSmartPointer<vtkUnstructuredGrid>::New();
// Convert OpenFOAM mesh vertices to VTK
// - can only do this *after* populating the decompInfo with cell-ids
// for any additional points (ie, mesh cell-centres)
vtkmesh->SetPoints(this->points(mesh));
#ifdef VTK_CELL_ARRAY_V2
// Move into a vtkCellArray
cells->SetData(cellOffsets, cellConnect);
if (facesUL.size())
{
vtkmesh->SetCells
(
cellTypes,
cellLocations,
cells,
faceLocations,
faces
);
vtkmesh->SetCells(cellTypes, cells, faceLocations, faces);
}
else
{
vtkmesh->SetCells
(
cellTypes,
cellLocations,
cells,
nullptr,
nullptr
);
vtkmesh->SetCells(cellTypes, cells, nullptr, nullptr);
}
#else
if (facesUL.size())
{
vtkmesh->SetCells(cellTypes, cellOffsets, cells, faceLocations, faces);
}
else
{
vtkmesh->SetCells(cellTypes, cellOffsets, cells, nullptr, nullptr);
}
#endif
return vtkmesh;
}
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -37,6 +37,7 @@ Description
for additional points of decomposed cells
SourceFiles
foamVtkMeshMaps.C
foamVtkMeshMapsI.H
\*---------------------------------------------------------------------------*/
......@@ -74,12 +75,11 @@ public:
// Constructors
//- Construct null
inline explicit foamVtkMeshMaps(const label size = 0);
//- Default construct: zero-sized, no reserved size
inline foamVtkMeshMaps();
//- Destructor
~foamVtkMeshMaps() = default;
//- Construct with reserved size
inline explicit foamVtkMeshMaps(const label size);
// Member Functions
......@@ -92,17 +92,26 @@ public:
// cells this becomes a useful means of mapping from the original mesh.
inline const labelList& cellMap() const;
//- Write access to original cell ids
inline DynamicList<label>& cellMap();
//- Point labels for subsetted meshes
inline const labelList& pointMap() const;
//- Write access to point labels for subsetted meshes
inline DynamicList<label>& pointMap();
//- Any additional (user) labels.
// Eg, cell-centre labels for additional points of decomposed cells
inline const labelList& additionalIds() const;
//- Write access to additional (user) labels.
inline DynamicList<label>& additionalIds();
// Edit
//- Clear
//- Clear sizing
inline void clear();
//- Renumber cell ids (cellMap and additionalIds) to account for
......@@ -111,22 +120,6 @@ public:
//- Renumber point ids (pointMap) to account for subset meshes
void renumberPoints(const labelUList& mapping);
//- Original cell ids for all cells (regular and decomposed).
// For a regular mesh comprising only primitive cell types, this
// will simply be an identity list. However, for subsetted meshes
// and decomposed cells this becomes a useful means of mapping from
// the original mesh.
inline DynamicList<label>& cellMap();
//- Point labels for subsetted meshes
inline DynamicList<label>& pointMap();
//- Any additional (user) labels.
// Eg, cell-centre labels for additional points of decomposed cells
inline DynamicList<label>& additionalIds();
};
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,6 +29,14 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::foamVtkMeshMaps::foamVtkMeshMaps()
:
cellMap_(0),
pointMap_(0),
additionalIds_(0)
{}
inline Foam::foamVtkMeshMaps::foamVtkMeshMaps(const label size)
:
cellMap_(size),
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -108,15 +108,16 @@ void Foam::vtk::vtuCells::repopulate(const polyMesh& mesh)
{
// vtuSizing::reset() called prior to this method
cellTypes_.setSize(nFieldCells());
vertLabels_.setSize(sizeOf(output_, slotType::CELLS));
vertOffset_.setSize(sizeOf(output_, slotType::CELLS_OFFSETS));
faceLabels_.setSize(sizeOf(output_, slotType::FACES));
faceOffset_.setSize(sizeOf(output_, slotType::FACES_OFFSETS));
cellTypes_.resize(nFieldCells());
vertLabels_.resize(sizeOf(output_, slotType::CELLS));
vertOffset_.resize(sizeOf(output_, slotType::CELLS_OFFSETS));
faceLabels_.resize(sizeOf(output_, slotType::FACES));
faceOffset_.resize(sizeOf(output_, slotType::FACES_OFFSETS));
switch (output_)
{
case contentType::LEGACY:
{
populateLegacy
(
mesh,
......@@ -125,7 +126,10 @@ void Foam::vtk::vtuCells::repopulate(const polyMesh& mesh)