surface.C 8.2 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
    Copyright (C) 2015-2019 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

// OpenFOAM includes
#include "surface.H"
#include "runTimePostProcessing.H"

32 33 34 35
#include "foamVtkTools.H"
#include "polySurfaceFields.H"
#include "polySurfacePointFields.H"

36 37
// VTK includes
#include "vtkActor.h"
38
#include "vtkCompositeDataGeometryFilter.h"
39
#include "vtkFeatureEdges.h"
40
#include "vtkMultiPieceDataSet.h"
41 42 43 44 45 46 47 48 49 50
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"

// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

namespace Foam
{
51 52 53 54
namespace functionObjects
{
namespace runTimePostPro
{
55
    defineTypeName(surface);
56 57
    defineRunTimeSelectionTable(surface, dictionary);
}
58 59
}
}
60

61 62

const Foam::Enum
63
<
64
    Foam::functionObjects::runTimePostPro::surface::representationType
65
>
66
Foam::functionObjects::runTimePostPro::surface::representationTypeNames
Mark Olesen's avatar
Mark Olesen committed
67
({
68
    { representationType::rtNone, "none" },
69
    { representationType::rtGlyph, "glyph" },
70 71 72
    { representationType::rtWireframe, "wireframe" },
    { representationType::rtSurface, "surface" },
    { representationType::rtSurfaceWithEdges, "surfaceWithEdges" },
Mark Olesen's avatar
Mark Olesen committed
73
});
74 75


76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// * * * * * * * * * * * * * * * Specializations * * * * * * * * * * * * * * //

// These need to shift elsewhere

vtkCellData* Foam::vtk::Tools::GetCellData(vtkDataSet* dataset)
{
    if (dataset) return dataset->GetCellData();
    return nullptr;
}

vtkPointData* Foam::vtk::Tools::GetPointData(vtkDataSet* dataset)
{
    if (dataset) return dataset->GetPointData();
    return nullptr;
}


93 94
// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

95 96 97 98
void Foam::functionObjects::runTimePostPro::surface::setRepresentation
(
    vtkActor* actor
) const
99 100 101 102 103 104 105 106 107 108 109 110
{
    geometryBase::initialiseActor(actor);

    switch (representation_)
    {
        case rtNone:
        {
            actor->VisibilityOff();
            break;
        }
        case rtWireframe:
        {
111
            // Note: colour is set using general SetColor, not SetEdgeColor
112 113 114 115 116 117
            actor->GetProperty()->SetRepresentationToWireframe();
            break;
        }
        case rtGlyph:
        case rtSurface:
        {
118 119
            actor->GetProperty()->SetBackfaceCulling(backFaceCulling_);
            actor->GetProperty()->SetFrontfaceCulling(frontFaceCulling_);
120 121 122 123 124
            actor->GetProperty()->SetRepresentationToSurface();
            break;
        }
        case rtSurfaceWithEdges:
        {
125 126
            actor->GetProperty()->SetBackfaceCulling(backFaceCulling_);
            actor->GetProperty()->SetFrontfaceCulling(frontFaceCulling_);
127 128 129 130 131 132 133 134
            actor->GetProperty()->SetRepresentationToSurface();
            actor->GetProperty()->EdgeVisibilityOn();
            break;
        }
    }
}


135
void Foam::functionObjects::runTimePostPro::surface::addFeatureEdges
136 137
(
    vtkRenderer* renderer,
138
    vtkFeatureEdges* featureEdges
139 140
) const
{
141
    if (!featureEdges)
142 143 144 145 146 147 148 149
    {
        return;
    }

    featureEdges->BoundaryEdgesOn();
    featureEdges->FeatureEdgesOn();
    featureEdges->ManifoldEdgesOff();
    featureEdges->NonManifoldEdgesOff();
150
    /// featureEdges->SetFeatureAngle(60);
151 152 153
    featureEdges->ColoringOff();
    featureEdges->Update();

154
    auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
155 156 157 158 159 160 161 162 163 164 165 166
    mapper->SetInputConnection(featureEdges->GetOutputPort());
    mapper->ScalarVisibilityOff();

    edgeActor_->GetProperty()->SetSpecular(0);
    edgeActor_->GetProperty()->SetSpecularPower(20);
    edgeActor_->GetProperty()->SetRepresentationToWireframe();
    edgeActor_->SetMapper(mapper);

    renderer->AddActor(edgeActor_);
}


167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
void Foam::functionObjects::runTimePostPro::surface::addFeatureEdges
(
    vtkRenderer* renderer,
    vtkPolyData* data
) const
{
    if (featureEdges_)
    {
        auto featureEdges = vtkSmartPointer<vtkFeatureEdges>::New();
        featureEdges->SetInputData(data);

        addFeatureEdges(renderer, featureEdges);
    }
}


void Foam::functionObjects::runTimePostPro::surface::addFeatureEdges
(
    vtkRenderer* renderer,
    vtkCompositeDataGeometryFilter* input
) const
{
    if (featureEdges_)
    {
        auto featureEdges = vtkSmartPointer<vtkFeatureEdges>::New();
        featureEdges->SetInputConnection(input->GetOutputPort());

        addFeatureEdges(renderer, featureEdges);
    }
}


199 200
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

201
Foam::functionObjects::runTimePostPro::surface::surface
202 203 204
(
    const runTimePostProcessing& parent,
    const dictionary& dict,
205
    const HashPtrTable<Function1<vector>>& colours
206 207 208 209 210
)
:
    geometryBase(parent, dict, colours),
    representation_
    (
Mark Olesen's avatar
Mark Olesen committed
211
        representationTypeNames.get("representation", dict)
212
    ),
213
    featureEdges_(dict.getOrDefault("featureEdges", false)),
214 215
    backFaceCulling_(dict.getOrDefault("backFaceCulling", false)),
    frontFaceCulling_(dict.getOrDefault("frontFaceCulling", true)),
Andrew Heather's avatar
Andrew Heather committed
216 217
    surfaceColour_(nullptr),
    edgeColour_(nullptr),
218 219
    surfaceActor_(),
    edgeActor_(),
220
    maxGlyphLength_(0)
221 222 223 224 225 226
{
    surfaceActor_ = vtkSmartPointer<vtkActor>::New();
    edgeActor_ = vtkSmartPointer<vtkActor>::New();

    if (dict.found("surfaceColour"))
    {
227
        surfaceColour_.reset(Function1<vector>::New("surfaceColour", dict));
228 229 230 231 232 233 234 235
    }
    else
    {
        surfaceColour_.reset(colours["surface"]->clone().ptr());
    }

    if (dict.found("edgeColour"))
    {
236
        edgeColour_.reset(Function1<vector>::New("edgeColour", dict));
237 238 239 240 241 242 243 244
    }
    else
    {
        edgeColour_.reset(colours["edge"]->clone().ptr());
    }

    if (representation_ == rtGlyph)
    {
245
        dict.readEntry("maxGlyphLength", maxGlyphLength_);
246 247 248 249 250 251
    }
}


// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //

252 253
Foam::autoPtr<Foam::functionObjects::runTimePostPro::surface>
Foam::functionObjects::runTimePostPro::surface::New
254 255 256
(
    const runTimePostProcessing& parent,
    const dictionary& dict,
257
    const HashPtrTable<Function1<vector>>& colours,
258 259 260
    const word& surfaceType
)
{
261
    DebugInfo << "Selecting surface " << surfaceType << endl;
262

263
    auto cstrIter = dictionaryConstructorTablePtr_->cfind(surfaceType);
264

265
    if (!cstrIter.found())
266
    {
267 268 269 270 271 272 273
        FatalIOErrorInLookup
        (
            dict,
            "surface",
            surfaceType,
            *dictionaryConstructorTablePtr_
        ) << exit(FatalIOError);
274 275 276 277 278 279 280 281
    }

    return autoPtr<surface>(cstrIter()(parent, dict, colours));
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

282
Foam::functionObjects::runTimePostPro::surface::~surface()
283 284 285 286 287
{}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

288 289 290 291
void Foam::functionObjects::runTimePostPro::surface::updateActors
(
    const scalar position
)
292 293 294 295 296 297
{
    if (!featureEdges_)
    {
        return;
    }

298
    vtkProperty* edgeProp = edgeActor_->GetProperty();
299

300 301 302 303 304 305
    edgeProp->SetLineWidth(2);
    edgeProp->SetOpacity(opacity(position));

    const vector ec = edgeColour_->value(position);
    edgeProp->SetColor(ec[0], ec[1], ec[2]);
    edgeProp->SetEdgeColor(ec[0], ec[1], ec[2]);
306 307 308 309
}


// ************************************************************************* //