From 9338f0b8600d1ba5f0b24f79a83c63111e90675b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 24 Jan 2020 13:01:38 +0100 Subject: [PATCH] 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. --- src/conversion/vtk/adaptor/foamVtkTools.H | 13 +- src/conversion/vtk/adaptor/foamVtkToolsI.H | 81 ++++-- .../vtk/adaptor/foamVtkToolsTemplates.C | 103 +++++-- .../vtk/adaptor/foamVtkVtuAdaptor.H | 11 +- .../vtk/adaptor/foamVtkVtuAdaptorI.H | 124 +++++--- src/fileFormats/vtk/part/foamVtkMeshMaps.H | 39 ++- src/fileFormats/vtk/part/foamVtkMeshMapsI.H | 10 +- src/fileFormats/vtk/part/foamVtuCells.C | 26 +- src/fileFormats/vtk/part/foamVtuCells.H | 31 +- src/fileFormats/vtk/part/foamVtuCellsI.H | 3 +- src/fileFormats/vtk/part/foamVtuSizing.C | 269 +++++++----------- src/fileFormats/vtk/part/foamVtuSizing.H | 151 +++++----- src/fileFormats/vtk/part/foamVtuSizingI.H | 15 +- .../vtk/part/foamVtuSizingTemplates.C | 233 ++++++++++++--- 14 files changed, 648 insertions(+), 461 deletions(-) diff --git a/src/conversion/vtk/adaptor/foamVtkTools.H b/src/conversion/vtk/adaptor/foamVtkTools.H index 3748e3f029..9f279076e2 100644 --- a/src/conversion/vtk/adaptor/foamVtkTools.H +++ b/src/conversion/vtk/adaptor/foamVtkTools.H @@ -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 asUList - ( - vtkCellArray* cells, - const label nCells, - const label size - ); - - //- Return a list of points as vtkPoints inline vtkSmartPointer Points ( diff --git a/src/conversion/vtk/adaptor/foamVtkToolsI.H b/src/conversion/vtk/adaptor/foamVtkToolsI.H index 17342d417b..aa2da4eeb2 100644 --- a/src/conversion/vtk/adaptor/foamVtkToolsI.H +++ b/src/conversion/vtk/adaptor/foamVtkToolsI.H @@ -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 + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // inline Foam::UList Foam::vtk::Tools::asUList @@ -53,19 +55,6 @@ inline Foam::UList Foam::vtk::Tools::asUList } -inline Foam::UList Foam::vtk::Tools::asUList -( - vtkCellArray* cells, - const label nCells, - const label size -) -{ - cells->GetData()->SetNumberOfTuples(size); - - return UList(cells->WritePointer(nCells, size), size); -} - - inline vtkSmartPointer Foam::vtk::Tools::Points(const UList& pts) { @@ -150,20 +139,72 @@ inline vtkSmartPointer Foam::vtk::Tools::identityVertices const label size ) { - // VTK_VERTEX: need 2 values (size=1 and index=id) per vertex auto cells = vtkSmartPointer::New(); - UList list = asUList(cells, size, 2*size); + #ifdef VTK_CELL_ARRAY_V2 + + // Offsets + // [0, n1, n1+n2, n1+n2+n3... ] + + auto offsets = vtkSmartPointer::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::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; }; diff --git a/src/conversion/vtk/adaptor/foamVtkToolsTemplates.C b/src/conversion/vtk/adaptor/foamVtkToolsTemplates.C index 2133485395..4ce843d8d0 100644 --- a/src/conversion/vtk/adaptor/foamVtkToolsTemplates.C +++ b/src/conversion/vtk/adaptor/foamVtkToolsTemplates.C @@ -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 vtkSmartPointer Foam::vtk::Tools::Faces(const UList& faces) { - label nAlloc = faces.size(); - for (const auto& f : faces) + auto cells = vtkSmartPointer::New(); + + #ifdef VTK_CELL_ARRAY_V2 + + // Offsets + // [0, n1, n1+n2, n1+n2+n3... ] + + const vtkIdType nOffsets(faces.size()+1); + + auto offsets = vtkSmartPointer::New(); + + vtkIdType nConnect(0); { - nAlloc += f.size(); - } + offsets->SetNumberOfTuples(nOffsets); - auto vtkcells = vtkSmartPointer::New(); + vtkIdType* iter = offsets->WritePointer(0, nOffsets); - UList list = asUList(vtkcells, faces.size(), nAlloc); + // Assign offsets, determine overall connectivity size - // Cell connectivity for polygons - // [size, verts..., size, verts... ] - auto iter = list.begin(); - for (const auto& f : faces) - { - *(iter++) = f.size(); - - for (const label verti : f) + *iter = 0; + for (const auto& f : faces) { - *(iter++) = verti; + nConnect += f.size(); + + *(++iter) = nConnect; } } - return vtkcells; + + // Cell connectivity for polygons + // [verts..., verts... ] + + auto connect = vtkSmartPointer::New(); + + { + 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; + } + } + } + + // Move into a vtkCellArray + + cells->SetData(offsets, connect); + + #else + + // In VTK-8.2.0 and older, + // sizes are interwoven (prefixed) in the connectivity + + // Cell connectivity for polygons + // [n1, verts..., n2, verts... ] + + + const vtkIdType nElem(faces.size()); + + // Connectivity size, with prefixed size information + vtkIdType nConnect(faces.size()); + for (const auto& f : faces) + { + nConnect += f.size(); + } + + { + cells->GetData()->SetNumberOfTuples(nConnect); + + vtkIdType* iter = cells->WritePointer(nElem, nConnect); + + // Fill in the connectivity array, with prefixed size information + + for (const auto& f : faces) + { + *(iter++) = f.size(); + + for (const label verti : f) + { + *(iter++) = verti; + } + } + } + + #endif + + return cells; } diff --git a/src/conversion/vtk/adaptor/foamVtkVtuAdaptor.H b/src/conversion/vtk/adaptor/foamVtkVtuAdaptor.H index 3e941ad6ef..57a624256c 100644 --- a/src/conversion/vtk/adaptor/foamVtkVtuAdaptor.H +++ b/src/conversion/vtk/adaptor/foamVtkVtuAdaptor.H @@ -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 diff --git a/src/conversion/vtk/adaptor/foamVtkVtuAdaptorI.H b/src/conversion/vtk/adaptor/foamVtkVtuAdaptorI.H index 39443d3156..e1ddefa703 100644 --- a/src/conversion/vtk/adaptor/foamVtkVtuAdaptorI.H +++ b/src/conversion/vtk/adaptor/foamVtkVtuAdaptorI.H @@ -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::New(); + auto cellTypes = vtkSmartPointer::New(); + UList cellTypesUL + ( + vtk::Tools::asUList(cellTypes, sizing.nFieldCells()) + ); + auto cells = vtkSmartPointer::New(); auto faces = vtkSmartPointer::New(); - auto cellLocations = vtkSmartPointer::New(); + auto cellOffsets = vtkSmartPointer::New(); auto faceLocations = vtkSmartPointer::New(); - UList cellTypesUL = - vtk::Tools::asUList(cellTypes, sizing.nFieldCells()); + const auto nConnect + ( + sizing.sizeOf(output, vtk::vtuSizing::slotType::CELLS) + ); - UList cellsUL = + UList cellOffsetsUL + ( vtk::Tools::asUList ( - cells, - sizing.nFieldCells(), - sizing.sizeInternal(vtk::vtuSizing::slotType::CELLS) - ); + cellOffsets, + sizing.sizeOf(output, vtk::vtuSizing::slotType::CELLS_OFFSETS) + ) + ); - UList cellLocationsUL = - vtk::Tools::asUList - ( - cellLocations, - sizing.sizeInternal(vtk::vtuSizing::slotType::CELLS_OFFSETS) - ); + #ifdef VTK_CELL_ARRAY_V2 - UList facesUL = + auto cellConnect = vtkSmartPointer::New(); + + UList cellsUL + ( + vtk::Tools::asUList(cellConnect, nConnect) + ); + + #else + + cells->GetData()->SetNumberOfTuples(sizing.nFieldCells()); + + UList cellsUL + ( + cells->WritePointer(sizing.nFieldCells(), nConnect), + nConnect + ); + + #endif + + UList facesUL + ( vtk::Tools::asUList ( faces, - sizing.sizeInternal(vtk::vtuSizing::slotType::FACES) - ); + sizing.sizeOf(output, vtk::vtuSizing::slotType::FACES) + ) + ); - UList faceLocationsUL = + UList 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(*this) + cellsUL, cellOffsetsUL, + facesUL, faceLocationsUL, + static_cast(*this), + output ); - auto vtkmesh = vtkSmartPointer::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; } diff --git a/src/fileFormats/vtk/part/foamVtkMeshMaps.H b/src/fileFormats/vtk/part/foamVtkMeshMaps.H index 95650b0bf6..9ef2c6a204 100644 --- a/src/fileFormats/vtk/part/foamVtkMeshMaps.H +++ b/src/fileFormats/vtk/part/foamVtkMeshMaps.H @@ -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