Commit 4a2556b4 authored by Mark Olesen's avatar Mark Olesen

STYLE: avoid VTK InsertNextPoint if we already know the sizes

- avoids potentially issues if we reusing a vtkPoints array,
  and should be marginally faster without the additional
  range checking.
parent db67028a
......@@ -332,7 +332,7 @@ class vtkPVFoam
template<class PatchType>
vtkSmartPointer<vtkPolyData> patchVTKMesh
(
const word& name,
const string& name,
const PatchType& p
);
......
......@@ -602,15 +602,14 @@ void Foam::vtkPVFoam::convertPointField
const labelUList& addPointCellLabels = vtuData.additionalIds();
const labelUList& pointMap = vtuData.pointMap();
// use a pointMap or address directly into mesh
// Use a pointMap or address directly into mesh
const label nPoints = (pointMap.size() ? pointMap.size() : pfld.size());
vtkSmartPointer<vtkFloatArray> fldData =
vtkSmartPointer<vtkFloatArray>::New();
fldData->SetNumberOfTuples(nPoints + addPointCellLabels.size());
fldData->SetNumberOfComponents(nComp);
fldData->Allocate(nComp*(nPoints + addPointCellLabels.size()));
fldData->SetNumberOfTuples(nPoints + addPointCellLabels.size());
// Note: using the name of the original volField
// not the name generated by the interpolation "volPointInterpolate(<name>)"
......@@ -635,6 +634,7 @@ void Foam::vtkPVFoam::convertPointField
float vec[nComp];
label pointi = 0;
if (pointMap.size())
{
forAll(pointMap, i)
......@@ -646,7 +646,7 @@ void Foam::vtkPVFoam::convertPointField
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(i, vec);
fldData->SetTuple(pointi++, vec);
}
}
else
......@@ -660,12 +660,11 @@ void Foam::vtkPVFoam::convertPointField
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(i, vec);
fldData->SetTuple(pointi++, vec);
}
}
// continue insertion from here
label i = nPoints;
// Continue additional points
if (&vfld != &GeometricField<Type, fvPatchField, volMesh>::null())
{
......@@ -678,7 +677,7 @@ void Foam::vtkPVFoam::convertPointField
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(i++, vec);
fldData->SetTuple(pointi++, vec);
}
}
else
......@@ -692,7 +691,7 @@ void Foam::vtkPVFoam::convertPointField
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(i++, vec);
fldData->SetTuple(pointi++, vec);
}
}
......@@ -766,22 +765,21 @@ Foam::vtkPVFoam::convertFieldToVTK
vtkSmartPointer<vtkFloatArray> fldData =
vtkSmartPointer<vtkFloatArray>::New();
fldData->SetNumberOfTuples(fld.size());
fldData->SetNumberOfComponents(nComp);
fldData->Allocate(nComp*fld.size());
fldData->SetNumberOfTuples(fld.size());
fldData->SetName(name.c_str());
float vec[nComp];
forAll(fld, i)
forAll(fld, idx)
{
const Type& t = fld[i];
const Type& t = fld[idx];
for (direction d=0; d<nComp; ++d)
{
vec[d] = component(t, d);
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(i, vec);
fldData->SetTuple(idx, vec);
}
return fldData;
......@@ -814,18 +812,17 @@ Foam::vtkPVFoam::convertFaceFieldToVTK
vtkSmartPointer<vtkFloatArray> fldData =
vtkSmartPointer<vtkFloatArray>::New();
fldData->SetNumberOfTuples(faceLabels.size());
fldData->SetNumberOfComponents(nComp);
fldData->Allocate(nComp*faceLabels.size());
fldData->SetNumberOfTuples(faceLabels.size());
fldData->SetName(fld.name().c_str());
float vec[nComp];
// for interior faces: average owner/neighbour
// for boundary faces: owner
forAll(faceLabels, facei)
forAll(faceLabels, idx)
{
const label faceNo = faceLabels[facei];
const label faceNo = faceLabels[idx];
if (faceNo < nInternalFaces)
{
Type t = 0.5*(fld[faceOwner[faceNo]] + fld[faceNeigh[faceNo]]);
......@@ -845,7 +842,7 @@ Foam::vtkPVFoam::convertFaceFieldToVTK
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(facei, vec);
fldData->SetTuple(idx, vec);
}
return fldData;
......@@ -866,9 +863,8 @@ Foam::vtkPVFoam::convertVolFieldToVTK
vtkSmartPointer<vtkFloatArray> fldData =
vtkSmartPointer<vtkFloatArray>::New();
fldData->SetNumberOfTuples(cellMap.size());
fldData->SetNumberOfComponents(nComp);
fldData->Allocate(nComp*cellMap.size());
fldData->SetNumberOfTuples(cellMap.size());
fldData->SetName(fld.name().c_str());
if (debug)
......@@ -891,7 +887,7 @@ Foam::vtkPVFoam::convertVolFieldToVTK
}
foamPvFields::remapTuple<Type>(vec);
fldData->InsertTuple(i, vec);
fldData->SetTuple(i, vec);
}
return fldData;
......
......@@ -575,17 +575,21 @@ void Foam::vtkPVFoam::convertMeshPointZones
continue;
}
const pointField& meshPoints = mesh.points();
const labelUList& pointLabels = zMesh[zoneId];
vtkSmartPointer<vtkPoints> vtkpoints =
vtkSmartPointer<vtkPoints>::New();
vtkpoints->Allocate(pointLabels.size());
vtkpoints->SetNumberOfPoints(pointLabels.size());
const pointField& meshPoints = mesh.points();
forAll(pointLabels, pointi)
{
vtkpoints->InsertNextPoint(meshPoints[pointLabels[pointi]].v_);
vtkpoints->SetPoint
(
pointi,
meshPoints[pointLabels[pointi]].v_
);
}
vtkSmartPointer<vtkPolyData> vtkmesh =
......@@ -647,17 +651,21 @@ void Foam::vtkPVFoam::convertMeshPointSets
Info<< "Creating VTK mesh for pointSet=" << partName << endl;
}
const pointSet pSet(mesh, partName);
const pointField& meshPoints = mesh.points();
const labelList pointLabels = pointSet(mesh, partName).sortedToc();
vtkSmartPointer<vtkPoints> vtkpoints =
vtkSmartPointer<vtkPoints>::New();
vtkpoints->Allocate(pSet.size());
vtkpoints->SetNumberOfPoints(pointLabels.size());
const pointField& meshPoints = mesh.points();
forAllConstIters(pSet, iter)
forAll(pointLabels, pointi)
{
vtkpoints->InsertNextPoint(meshPoints[iter.key()].v_);
vtkpoints->SetPoint
(
pointi,
meshPoints[pointLabels[pointi]].v_
);
}
vtkSmartPointer<vtkPolyData> vtkmesh =
......
......@@ -81,15 +81,16 @@ vtkSmartPointer<vtkPolyData> Foam::vtkPVFoam::lagrangianVTKMesh
vtkSmartPointer<vtkCellArray> vtkcells =
vtkSmartPointer<vtkCellArray>::New();
vtkpoints->Allocate(parcels.size());
vtkcells->Allocate(parcels.size());
vtkpoints->SetNumberOfPoints(parcels.size());
vtkcells->Allocate(2*parcels.size());
// If reusing memory, ensure insert always starts from 0
vtkcells->Reset();
vtkIdType particleId = 0;
forAllConstIters(parcels, iter)
{
vtkpoints->InsertNextPoint(iter().position().v_);
vtkcells->InsertNextCell(1, &particleId);
vtkpoints->SetPoint(particleId, iter().position().v_);
vtkcells->InsertNextCell(1, &particleId); // VTK_VERTEX
particleId++;
}
......
......@@ -40,7 +40,7 @@ License
template<class PatchType>
vtkSmartPointer<vtkPolyData> Foam::vtkPVFoam::patchVTKMesh
(
const word& name,
const string& name,
const PatchType& p
)
{
......@@ -59,20 +59,29 @@ vtkSmartPointer<vtkPolyData> Foam::vtkPVFoam::patchVTKMesh
vtkSmartPointer<vtkPoints> vtkpoints =
vtkSmartPointer<vtkPoints>::New();
vtkpoints->Allocate(points.size());
vtkpoints->SetNumberOfPoints(points.size());
forAll(points, i)
{
vtkpoints->InsertNextPoint(points[i].v_);
vtkpoints->SetPoint(i, points[i].v_);
}
vtkmesh->SetPoints(vtkpoints);
// Add faces as polygons
const faceList& faces = p.localFaces();
label nAlloc = faces.size();
forAll(faces, facei)
{
nAlloc += faces[facei].size();
}
vtkSmartPointer<vtkCellArray> vtkcells =
vtkSmartPointer<vtkCellArray>::New();
vtkcells->Allocate(faces.size());
vtkcells->Allocate(nAlloc);
// If reusing memory, ensure insert always starts from 0
vtkcells->Reset();
forAll(faces, facei)
{
const face& f = faces[facei];
......
......@@ -40,26 +40,6 @@ License
#include "vtkSmartPointer.h"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
//! \cond fileScope
inline static void insertNextPoint
(
vtkPoints *points,
const Foam::point& p,
const Foam::scalar scaleFactor
)
{
points->InsertNextPoint
(
p.x()*scaleFactor,
p.y()*scaleFactor,
p.z()*scaleFactor
);
}
//! \endcond
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::vtkPVblockMesh::convertMeshBlocks
......@@ -68,22 +48,20 @@ void Foam::vtkPVblockMesh::convertMeshBlocks
int& blockNo
)
{
if (debug)
{
Info<< "<beg> convertMeshBlocks" << endl;
}
vtkDataArraySelection* selection = reader_->GetBlockSelection();
arrayRange& range = rangeBlocks_;
range.block(blockNo); // set output block
label datasetNo = 0; // restart at dataset 0
const blockMesh& blkMesh = *meshPtr_;
const Foam::pointField& blockPoints = blkMesh.vertices();
if (debug)
{
Info<< "<beg> convertMeshBlocks" << endl;
}
const pointField blkPoints(blkMesh.vertices() * blkMesh.scaleFactor());
int blockI = 0;
const scalar scaleFactor = blkMesh.scaleFactor();
for
(
auto iter = range.cbegin();
......@@ -98,26 +76,22 @@ void Foam::vtkPVblockMesh::convertMeshBlocks
}
const blockDescriptor& blockDef = blkMesh[blockI];
const labelList& blockLabels = blockDef.blockShape();
// Convert OpenFOAM mesh vertices to VTK
vtkSmartPointer<vtkPoints> vtkpoints =
vtkSmartPointer<vtkPoints>::New();
vtkpoints->Allocate(blockDef.nPoints());
const labelList& blockLabels = blockDef.blockShape();
vtkpoints->SetNumberOfPoints(blockLabels.size());
vtkIdType nodeIds[8];
forAll(blockLabels, ptI)
forAll(blockLabels, pointi)
{
insertNextPoint
vtkpoints->SetPoint
(
vtkpoints,
blockPoints[blockLabels[ptI]],
scaleFactor
pointi,
blkPoints[blockLabels[pointi]].v_
);
nodeIds[ptI] = ptI;
nodeIds[pointi] = pointi;
}
vtkSmartPointer<vtkUnstructuredGrid> vtkmesh =
......@@ -138,7 +112,6 @@ void Foam::vtkPVblockMesh::convertMeshBlocks
output, vtkmesh, range, datasetNo,
selection->GetArrayName(partId)
);
++datasetNo;
}
......@@ -170,10 +143,9 @@ void Foam::vtkPVblockMesh::convertMeshEdges
const blockMesh& blkMesh = *meshPtr_;
const blockEdgeList& edges = blkMesh.edges();
int edgeI = 0;
const scalar scaleFactor = blkMesh.scaleFactor();
int edgeI = 0;
for
(
auto iter = range.cbegin();
......@@ -214,27 +186,24 @@ void Foam::vtkPVblockMesh::convertMeshEdges
{
const List<point>& edgePoints = edgesPoints[foundEdgeI];
vtkSmartPointer<vtkPolyData> vtkmesh =
vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPoints> vtkpoints =
vtkSmartPointer<vtkPoints>::New();
vtkpoints->Allocate( edgePoints.size() );
vtkmesh->Allocate(1);
vtkpoints->SetNumberOfPoints(edgePoints.size());
vtkIdType pointIds[edgePoints.size()];
forAll(edgePoints, ptI)
forAll(edgePoints, pointi)
{
insertNextPoint
(
vtkpoints,
edgePoints[ptI],
scaleFactor
);
pointIds[ptI] = ptI;
const point p = edgePoints[pointi] * scaleFactor;
vtkpoints->SetPoint(pointi, p.v_);
pointIds[pointi] = pointi;
}
vtkSmartPointer<vtkPolyData> vtkmesh =
vtkSmartPointer<vtkPolyData>::New();
vtkmesh->Allocate(1);
vtkmesh->InsertNextCell
(
VTK_POLY_LINE,
......@@ -249,8 +218,7 @@ void Foam::vtkPVblockMesh::convertMeshEdges
output, vtkmesh, range, datasetNo,
selection->GetArrayName(partId)
);
datasetNo++;
++datasetNo;
break;
}
......@@ -281,8 +249,7 @@ void Foam::vtkPVblockMesh::convertMeshCorners
range.block(blockNo); // set output block
label datasetNo = 0; // restart at dataset 0
const pointField& blockPoints = meshPtr_->vertices();
const scalar& scaleFactor = meshPtr_->scaleFactor();
const pointField blkPoints(meshPtr_->vertices() * meshPtr_->scaleFactor());
if (debug)
{
......@@ -300,20 +267,16 @@ void Foam::vtkPVblockMesh::convertMeshCorners
vtkSmartPointer<vtkCellArray> vtkcells =
vtkSmartPointer<vtkCellArray>::New();
vtkpoints->Allocate( blockPoints.size() );
vtkcells->Allocate( blockPoints.size() );
vtkpoints->SetNumberOfPoints(blkPoints.size());
vtkcells->Allocate(2*blkPoints.size());
// If reusing memory, ensure insert always starts from 0
vtkcells->Reset();
vtkIdType pointId = 0;
forAll(blockPoints, ptI)
forAll(blkPoints, pointi)
{
insertNextPoint
(
vtkpoints,
blockPoints[ptI],
scaleFactor
);
vtkcells->InsertNextCell(1, &pointId); // VTK_VERTEX
vtkpoints->SetPoint(pointi, blkPoints[pointi].v_);
vtkcells->InsertNextCell(1, &pointId); // VTK_VERTEX
pointId++;
}
......
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