Foam::tmp<Foam::volScalarField> Foam::sampledIsoSurface::average
(
    const fvMesh& mesh,
    const pointScalarField& pfld
) const
{
    tmp<volScalarField> tcellAvg
    (
        new volScalarField
        (
            IOobject
            (
                "cellAvg",
                mesh.time().timeName(),
                mesh,
                IOobject::NO_READ,
                IOobject::NO_WRITE,
                false
            ),
            mesh,
            dimensionedScalar("zero", dimless, scalar(0.0))
        )
    );
    volScalarField& cellAvg = tcellAvg();

    labelField nPointCells(mesh.nCells(), 0);
    {
        for (label pointI = 0; pointI < mesh.nPoints(); pointI++)
        {
            const labelList& pCells = mesh.pointCells(pointI);

            forAll(pCells, i)
            {
                label cellI = pCells[i];

                cellAvg[cellI] += pfld[pointI];
                nPointCells[cellI]++;
            }
        }
    }
void Foam::vtkFoam::addInternalMesh
(
    const fvMesh& mesh,
    vtkUnstructuredGrid* vtkMesh
)
{
    SetName(vtkMesh, "Internal Mesh");

    // Number of additional points needed by the decomposition of polyhedra
    label nAddPoints = 0;

    // Number of additional cells generated by the decomposition of polyhedra
    label nAddCells = 0;

    const cellModel& tet = *(cellModeller::lookup("tet"));
    const cellModel& pyr = *(cellModeller::lookup("pyr"));
    const cellModel& prism = *(cellModeller::lookup("prism"));
    const cellModel& wedge = *(cellModeller::lookup("wedge"));
    const cellModel& tetWedge = *(cellModeller::lookup("tetWedge"));
    const cellModel& hex = *(cellModeller::lookup("hex"));

    // Scan for cells which need to be decomposed and count additional points
    // and cells
    if (debug)
    {
        Info<< "building cell-shapes" << endl;
    }
    const cellShapeList& cellShapes = mesh.cellShapes();

    if (debug)
    {
        Info<< "scanning" << endl;
    }

    forAll(cellShapes, cellI)
    {
        const cellModel& model = cellShapes[cellI].model();

        if
        (
            model != hex
         && model != wedge
         && model != prism
         && model != pyr
         && model != tet
         && model != tetWedge
        )
        {
            const cell& cFaces = mesh.cells()[cellI];

            forAll(cFaces, cFaceI)
            {
                const face& f = mesh.faces()[cFaces[cFaceI]];

                label nFacePoints = f.size();

                label nQuads = (nFacePoints - 2)/2;
                label nTris = (nFacePoints - 2)%2;
                nAddCells += nQuads + nTris;
            }

            nAddCells--;
            nAddPoints++;
        }
    }

    // Set size of additional point addressing array
    // (from added point to original cell)
    addPointCellLabels_.setSize(nAddPoints);

    // Set size of additional cells mapping array
    // (from added cell to original cell)
    superCells_.setSize(mesh.nCells() + nAddCells);

    if (debug)
    {
        Info<< "converting points" << endl;
    }

    // Convert Foam mesh vertices to VTK
    vtkPoints *vtkpoints = vtkPoints::New();
    vtkpoints->Allocate(mesh.nPoints() + nAddPoints);

    const Foam::pointField& points = mesh.points();

    forAll(points, i)
    {
        vtkFoamInsertNextPoint(vtkpoints, points[i]);
    }