Commit db67028a authored by Mark Olesen's avatar Mark Olesen

ENH: use smart pointers for paraview readers

- adds flexiblity and reduces risk of memory leaks as we add/change
  features

STYLE: adjust naming for paraview internal polyDecomp
parent 6d73c8a9
......@@ -39,6 +39,7 @@ License
#include "vtkSMRenderViewProxy.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include "vtkSmartPointer.h"
// OpenFOAM includes
#include "vtkPVFoam.H"
......@@ -69,11 +70,13 @@ vtkPVFoamReader::vtkPVFoamReader()
#ifdef VTKPVFOAM_DUALPORT
// Add second output for the Lagrangian
this->SetNumberOfOutputPorts(2);
vtkMultiBlockDataSet *lagrangian = vtkMultiBlockDataSet::New();
vtkSmartPointer<vtkMultiBlockDataSet> lagrangian =
vtkSmartPointer<vtkMultiBlockDataSet>::New();
lagrangian->ReleaseData();
this->GetExecutive()->SetOutputData(1, lagrangian);
lagrangian->Delete();
#endif
TimeStepRange[0] = 0;
......@@ -218,7 +221,7 @@ int vtkPVFoamReader::RequestInformation
{
vtkErrorMacro("could not find valid OpenFOAM mesh");
// delete foamData and flag it as fatal error
// delete backend handler and flag it as fatal error
delete backend_;
backend_ = nullptr;
return 0;
......@@ -326,7 +329,8 @@ int vtkPVFoamReader::RequestData
{
vtkInformation *outInfo = outputVector->GetInformationObject(infoI);
int nsteps = outInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
const int nsteps =
outInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
if
(
......@@ -419,7 +423,7 @@ int vtkPVFoamReader::RequestData
(
vtkMultiBlockDataSet::DATA_OBJECT()
)
);
)
);
#else
backend_->Update(output, output);
......
......@@ -46,6 +46,7 @@ SourceFiles
// VTK forward declarations
class vtkDataArraySelection;
class vtkCallbackCommand;
template<class T> class vtkSmartPointer;
// OpenFOAM forward declarations
namespace Foam
......
......@@ -37,6 +37,7 @@ License
#include "vtkRenderer.h"
#include "vtkTextActor.h"
#include "vtkTextProperty.h"
#include "vtkSmartPointer.h"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -48,29 +49,37 @@ namespace Foam
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
vtkTextActor* Foam::vtkPVFoam::createTextActor
(
const std::string& s,
const point& pt
)
namespace Foam
{
vtkTextActor* txt = vtkTextActor::New();
txt->SetInput(s.c_str());
// Set text properties
vtkTextProperty* tprop = txt->GetTextProperty();
tprop->SetFontFamilyToArial();
tprop->BoldOn();
tprop->ShadowOff();
tprop->SetLineSpacing(1.0);
tprop->SetFontSize(14);
tprop->SetColor(1.0, 0.0, 1.0);
tprop->SetJustificationToCentered();
txt->GetPositionCoordinate()->SetCoordinateSystemToWorld();
txt->GetPositionCoordinate()->SetValue(pt.x(), pt.y(), pt.z());
return txt;
// file-scope
//- Create a text actor
vtkSmartPointer<vtkTextActor> createTextActor
(
const std::string& s,
const Foam::point& pt
)
{
vtkSmartPointer<vtkTextActor> txt =
vtkSmartPointer<vtkTextActor>::New();
txt->SetInput(s.c_str());
// Set text properties
vtkTextProperty* tprop = txt->GetTextProperty();
tprop->SetFontFamilyToArial();
tprop->BoldOn();
tprop->ShadowOff();
tprop->SetLineSpacing(1.0);
tprop->SetFontSize(14);
tprop->SetColor(1.0, 0.0, 1.0);
tprop->SetJustificationToCentered();
txt->GetPositionCoordinate()->SetCoordinateSystemToWorld();
txt->GetPositionCoordinate()->SetValue(pt.x(), pt.y(), pt.z());
return txt;
}
}
......@@ -93,19 +102,19 @@ void Foam::vtkPVFoam::resetCounters()
void Foam::vtkPVFoam::reduceMemory()
{
forAll(regionPolyDecomp_, i)
forAll(regionVtus_, i)
{
regionPolyDecomp_[i].clear();
regionVtus_[i].clear();
}
forAll(zonePolyDecomp_, i)
forAll(zoneVtus_, i)
{
zonePolyDecomp_[i].clear();
zoneVtus_[i].clear();
}
forAll(csetPolyDecomp_, i)
forAll(csetVtus_, i)
{
csetPolyDecomp_[i].clear();
csetVtus_[i].clear();
}
if (!reader_->GetCacheMesh())
......@@ -640,12 +649,11 @@ void Foam::vtkPVFoam::renderPatchNames
{
// always remove old actors first
forAll(patchTextActorsPtrs_, patchi)
forAll(patchTextActors_, patchi)
{
renderer->RemoveViewProp(patchTextActorsPtrs_[patchi]);
patchTextActorsPtrs_[patchi]->Delete();
renderer->RemoveViewProp(patchTextActors_[patchi]);
}
patchTextActorsPtrs_.clear();
patchTextActors_.clear();
if (show && meshPtr_)
{
......@@ -750,7 +758,7 @@ void Foam::vtkPVFoam::renderPatchNames
}
// Set the size of the patch labels to max number of zones
patchTextActorsPtrs_.setSize(displayZoneI);
patchTextActors_.setSize(displayZoneI);
if (debug)
{
......@@ -783,7 +791,7 @@ void Foam::vtkPVFoam::renderPatchNames
}
// Into a list for later removal
patchTextActorsPtrs_[displayZoneI++] = createTextActor
patchTextActors_[displayZoneI++] = createTextActor
(
pp.name(),
zoneCentre[patchi][globalZoneI]
......@@ -792,13 +800,13 @@ void Foam::vtkPVFoam::renderPatchNames
}
// Resize the patch names list to the actual number of patch names added
patchTextActorsPtrs_.setSize(displayZoneI);
patchTextActors_.setSize(displayZoneI);
}
// Add text to each renderer
forAll(patchTextActorsPtrs_, actori)
forAll(patchTextActors_, actori)
{
renderer->AddViewProp(patchTextActorsPtrs_[actori]);
renderer->AddViewProp(patchTextActors_[actori]);
}
}
......
......@@ -71,6 +71,8 @@ class vtkPolyData;
class vtkUnstructuredGrid;
class vtkIndent;
template<class T> class vtkSmartPointer;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -105,9 +107,9 @@ class vtkPVFoam
// hide in extra pointMap (cellSet/cellZone) for now
class polyDecomp
{
labelList superCells_;
labelList addPointCellLabels_;
labelList cellMap_;
labelList pointMap_;
labelList additionalIds_;
public:
......@@ -115,27 +117,15 @@ class vtkPVFoam
{}
//- Label of original cell for decomposed cells
labelList& superCells()
labelList& cellMap()
{
return superCells_;
return cellMap_;
}
//- Label of original cell for decomposed cells
const labelList& superCells() const
{
return superCells_;
}
//- Cell-centre labels for additional points of decomposed cells
labelList& addPointCellLabels()
const labelList& cellMap() const
{
return addPointCellLabels_;
}
//- Cell-centre labels for additional points of decomposed cells
const labelList& addPointCellLabels() const
{
return addPointCellLabels_;
return cellMap_;
}
//- Point labels for subsetted meshes
......@@ -151,15 +141,29 @@ class vtkPVFoam
}
//- Cell-centre labels for additional points of decomposed cells
labelList& additionalIds()
{
return additionalIds_;
}
//- Cell-centre labels for additional points of decomposed cells
const labelList& additionalIds() const
{
return additionalIds_;
}
//- Clear
void clear()
{
superCells_.clear();
addPointCellLabels_.clear();
cellMap_.clear();
pointMap_.clear();
additionalIds_.clear();
}
};
typedef polyDecomp foamVtuData;
// Private Data
......@@ -208,27 +212,20 @@ class vtkPVFoam
//- Decomposed cells information (mesh regions)
// TODO: regions
List<polyDecomp> regionPolyDecomp_;
List<foamVtuData> regionVtus_;
//- Decomposed cells information (cellZone meshes)
List<polyDecomp> zonePolyDecomp_;
List<foamVtuData> zoneVtus_;
//- Decomposed cells information (cellSet meshes)
List<polyDecomp> csetPolyDecomp_;
List<foamVtuData> csetVtus_;
//- List of patch names for rendering to window
List<vtkTextActor*> patchTextActorsPtrs_;
List<vtkSmartPointer<vtkTextActor>> patchTextActors_;
// Private Member Functions
//- Create a text actor
static vtkTextActor* createTextActor
(
const std::string& s,
const point& pt
);
//- Reset data counters
void resetCounters();
......@@ -318,14 +315,14 @@ class vtkPVFoam
// Add mesh functions
//- Volume meshes as vtkUnstructuredGrid
vtkUnstructuredGrid* volumeVTKMesh
vtkSmartPointer<vtkUnstructuredGrid> volumeVTKMesh
(
const fvMesh& mesh,
polyDecomp& decompInfo
foamVtuData& vtuData
);
//- Lagrangian positions as vtkPolyData
vtkPolyData* lagrangianVTKMesh
vtkSmartPointer<vtkPolyData> lagrangianVTKMesh
(
const polyMesh& mesh,
const word& cloudName
......@@ -333,7 +330,7 @@ class vtkPVFoam
//- Patches (mesh or primitive) as vtkPolyData
template<class PatchType>
vtkPolyData* patchVTKMesh
vtkSmartPointer<vtkPolyData> patchVTKMesh
(
const word& name,
const PatchType& p
......@@ -344,7 +341,7 @@ class vtkPVFoam
//- Convert Field to VTK field
template<class Type>
vtkFloatArray* convertFieldToVTK
vtkSmartPointer<vtkFloatArray> convertFieldToVTK
(
const word& name,
const Field<Type>& fld
......@@ -352,7 +349,7 @@ class vtkPVFoam
//- Face set/zone field
template<class Type>
vtkFloatArray* convertFaceFieldToVTK
vtkSmartPointer<vtkFloatArray> convertFaceFieldToVTK
(
const GeometricField<Type, fvPatchField, volMesh>& fld,
const labelUList& faceLabels
......@@ -360,10 +357,10 @@ class vtkPVFoam
//- Volume field
template<class Type>
vtkFloatArray* convertVolFieldToVTK
vtkSmartPointer<vtkFloatArray> convertVolFieldToVTK
(
const GeometricField<Type, fvPatchField, volMesh>& fld,
const polyDecomp& decompInfo
const foamVtuData& vtuData
);
......@@ -416,7 +413,7 @@ class vtkPVFoam
autoPtr<GeometricField<Type, pointPatchField, pointMesh>>& ptfPtr,
vtkMultiBlockDataSet* output,
const arrayRange& range,
const List<polyDecomp>& decompLst
const List<foamVtuData>& vtuDataList
);
//- Lagrangian fields - all types
......@@ -444,7 +441,7 @@ class vtkPVFoam
const GeometricField<Type, pointPatchField, pointMesh>& pfld,
vtkMultiBlockDataSet* output,
const arrayRange& range,
const List<polyDecomp>& decompLst
const List<foamVtuData>& vtuDataList
);
//- Point field
......@@ -454,7 +451,7 @@ class vtkPVFoam
vtkUnstructuredGrid* vtkmesh,
const GeometricField<Type, pointPatchField, pointMesh>& pfld,
const GeometricField<Type, fvPatchField, volMesh>& vfld,
const polyDecomp& decomp
const foamVtuData& vtuData
);
......
......@@ -42,6 +42,7 @@ InClass
#include "vtkFloatArray.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//
......@@ -84,7 +85,7 @@ void Foam::vtkPVFoam::convertVolField
ptfPtr,
output,
rangeVolume_,
regionPolyDecomp_
regionVtus_
);
// Convert activated cellZones
......@@ -94,7 +95,7 @@ void Foam::vtkPVFoam::convertVolField
ptfPtr,
output,
rangeCellZones_,
zonePolyDecomp_
zoneVtus_
);
// Convert activated cellSets
......@@ -104,7 +105,7 @@ void Foam::vtkPVFoam::convertVolField
ptfPtr,
output,
rangeCellSets_,
csetPolyDecomp_
csetVtus_
);
......@@ -151,38 +152,44 @@ void Foam::vtkPVFoam::convertVolField
fvPatchField<Type>(p, fld).patchInternalField()
);
vtkFloatArray* cdata = convertFieldToVTK(fld.name(), tpptf());
vtkSmartPointer<vtkFloatArray> cdata =
convertFieldToVTK
(
fld.name(),
tpptf()
);
vtkmesh->GetCellData()->AddArray(cdata);
cdata->Delete();
if (patchId < patchInterpList.size())
{
vtkFloatArray* pdata = convertFieldToVTK
vtkSmartPointer<vtkFloatArray> pdata = convertFieldToVTK
(
fld.name(),
patchInterpList[patchId].faceToPointInterpolate(tpptf)()
);
vtkmesh->GetPointData()->AddArray(pdata);
pdata->Delete();
}
}
else
{
vtkFloatArray* cdata = convertFieldToVTK(fld.name(), ptf);
vtkSmartPointer<vtkFloatArray> cdata =
convertFieldToVTK
(
fld.name(),
ptf
);
vtkmesh->GetCellData()->AddArray(cdata);
cdata->Delete();
if (patchId < patchInterpList.size())
{
vtkFloatArray* pdata = convertFieldToVTK
vtkSmartPointer<vtkFloatArray> pdata = convertFieldToVTK
(
fld.name(),
patchInterpList[patchId].faceToPointInterpolate(ptf)()
);
vtkmesh->GetPointData()->AddArray(pdata);
pdata->Delete();
}
}
}
......@@ -215,14 +222,13 @@ void Foam::vtkPVFoam::convertVolField
if (vtkmesh)
{
vtkFloatArray* cdata = convertFaceFieldToVTK
vtkSmartPointer<vtkFloatArray> cdata = convertFaceFieldToVTK
(
fld,
zMesh[zoneId]
);
vtkmesh->GetCellData()->AddArray(cdata);
cdata->Delete();
}
// TODO: points
......@@ -253,14 +259,13 @@ void Foam::vtkPVFoam::convertVolField
const faceSet fSet(mesh, selectName);
vtkFloatArray* cdata = convertFaceFieldToVTK
vtkSmartPointer<vtkFloatArray> cdata = convertFaceFieldToVTK
(
fld,
fSet.sortedToc()
);
vtkmesh->GetCellData()->AddArray(cdata);
cdata->Delete();
// TODO: points
}
......@@ -369,7 +374,7 @@ void Foam::vtkPVFoam::convertVolFieldBlock
autoPtr<GeometricField<Type, pointPatchField, pointMesh>>& ptfPtr,
vtkMultiBlockDataSet* output,
const arrayRange& range,
const List<polyDecomp>& decompLst
const List<foamVtuData>& vtuDataList
)
{
for (auto partId : range)
......@@ -389,18 +394,16 @@ void Foam::vtkPVFoam::convertVolFieldBlock
continue;
}
vtkFloatArray* cdata = convertVolFieldToVTK
vtkSmartPointer<vtkFloatArray> cdata = convertVolFieldToVTK
(
fld,
decompLst[datasetNo]
vtuDataList[datasetNo]
);
vtkmesh->GetCellData()->AddArray(cdata);
cdata->Delete();
if (ptfPtr.valid())
{
convertPointField(vtkmesh, ptfPtr(), fld, decompLst[datasetNo]);
convertPointField(vtkmesh, ptfPtr(), fld, vtuDataList[datasetNo]);
}
}
}
......@@ -449,7 +452,7 @@ void Foam::vtkPVFoam::convertPointFields
pfld,
output,
rangeVolume_,
regionPolyDecomp_
regionVtus_
);
// Convert activated cellZones
......@@ -458,7 +461,7 @@ void Foam::vtkPVFoam::convertPointFields
pfld,
output,
rangeCellZones_,
zonePolyDecomp_
zoneVtus_
);
// Convert activated cellSets
......@@ -467,7 +470,7 @@ void Foam::vtkPVFoam::convertPointFields
pfld,
output,
rangeCellSets_,
csetPolyDecomp_
csetVtus_
);
......@@ -492,14 +495,13 @@ void Foam::vtkPVFoam::convertPointFields
if (vtkmesh)
{
vtkFloatArray* pdata = convertFieldToVTK
vtkSmartPointer<vtkFloatArray> pdata = convertFieldToVTK
(
fieldName,
pfld.boundaryField()[patchId].patchInternalField()()
);
vtkmesh->GetPointData()->AddArray(pdata);
pdata->Delete();
}
}
......@@ -531,10 +533,14 @@ void Foam::vtkPVFoam::convertPointFields
mesh.faceZones()[zoneId]().meshPoints()
);
vtkFloatArray* pdata = convertFieldToVTK(fieldName, znfld);
vtkSmartPointer<vtkFloatArray> pdata =
convertFieldToVTK
(
fieldName,
znfld
);
vtkmesh->GetPointData()->AddArray(pdata);
pdata->Delete();
}
}
}
......@@ -547,7 +553,7 @@ void Foam::vtkPVFoam::convertPointFieldBlock
const GeometricField<Type, pointPatchField, pointMesh>& pfld,
vtkMultiBlockDataSet* output,
const arrayRange& range,
const List<polyDecomp>& decompLst
const List<foamVtuData>& vtuDataList
)
{
for (auto partId : range)
......@@ -571,7 +577,7 @@ void Foam::vtkPVFoam::convertPointFieldBlock
vtkmesh,
pfld,
GeometricField<Type, fvPatchField, volMesh>::null(),
decompLst[datasetNo]
vtuDataList[datasetNo]
);
}
}
......@@ -584,7 +590,7 @@ void Foam::vtkPVFoam::convertPointField
vtkUnstructuredGrid* vtkmesh,
const GeometricField<Type, pointPatchField, pointMesh>& pfld,
const GeometricField<Type, fvPatchField, volMesh>& vfld,
const polyDecomp& decomp
const foamVtuData& vtuData
)
{
if (!vtkmesh)
......@@ -593,13 +599,15 @@ void Foam::vtkPVFoam::convertPointField
}
const label nComp = pTraits<Type>::nComponents;
const labelUList& addPointCellLabels = decomp.addPointCellLabels();
const labelUList& pointMap = decomp.pointMap();