scene.C 11 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2015-2019 OpenCFD Ltd.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
     \\/     M anipulation  |
-------------------------------------------------------------------------------
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 "scene.H"
#include "Constant.H"

// VTK includes
#include "vtkCamera.h"
#include "vtkCubeSource.h"
#include "vtkLightKit.h"
#include "vtkPolyDataMapper.h"
#include "vtkPNGWriter.h"
#include "vtkRenderer.h"
#include "vtkRendererCollection.h"
#include "vtkRenderWindow.h"
#include "vtkWindowToImageFilter.h"

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

43 44 45 46
void Foam::functionObjects::runTimePostPro::scene::readCamera
(
    const dictionary& dict
)
47
{
48 49 50 51 52 53
    nFrameTotal_ = dict.getCheckOrDefault<label>
    (
        "nFrameTotal",
        1,
        labelMinMax::ge(1)
    );
54

55 56 57 58 59 60 61 62 63
    if
    (
        dict.readCheckIfPresent
        (
            "startPosition",
            startPosition_,
            scalarMinMax::zero_one()
        )
    )
64
    {
65
        position_ = startPosition_;
66 67 68 69
    }

    if (nFrameTotal_ > 1)
    {
70 71 72 73 74 75 76
        scalar endPosition = dict.getCheckOrDefault<scalar>
        (
            "endPosition",
            1,
            scalarMinMax::zero_one()
        );

77
        dPosition_ = (endPosition - startPosition_)/scalar(nFrameTotal_ - 1);
78 79
    }

80 81 82
    cameraPosition_ = Function1<vector>::New("position", dict);
    cameraFocalPoint_ = Function1<point>::New("focalPoint", dict);
    cameraUp_ = Function1<vector>::New("up", dict);
83

84
    dict.readIfPresent("clipBox", clipBox_);
85
    parallelProjection_ = dict.getOrDefault("parallelProjection", true);
86
    if (!parallelProjection_)
87
    {
88
        if (dict.found("viewAngle"))
89
        {
90
            cameraViewAngle_ = Function1<scalar>::New("viewAngle", dict);
91
        }
92
        else
93
        {
94
            cameraViewAngle_.reset
95
            (
96
                new Function1Types::Constant<scalar>("viewAngle", 35.0)
97 98 99 100
            );
        }
    }

101 102 103 104 105 106 107 108 109 110 111
    if (dict.found("zoom"))
    {
        cameraZoom_ = Function1<scalar>::New("zoom", dict);
    }
    else
    {
        cameraZoom_.reset
        (
            new Function1Types::Constant<scalar>("zoom", 1.0)
        );
    }
112 113 114
}


115 116 117 118
void Foam::functionObjects::runTimePostPro::scene::readColours
(
    const dictionary& dict
)
119
{
120 121
    const wordList colours(dict.toc());
    for (const word& c : colours)
122
    {
123
        colours_.insert(c, Function1<vector>::New(c, dict));
124 125 126 127
    }
}


128 129 130 131 132 133
void Foam::functionObjects::runTimePostPro::scene::setActorVisibility
(
    vtkRenderer* renderer,
    const bool visible
) const
{
134 135
    if (!renderer) return;

136 137 138 139 140 141 142 143 144
    vtkActorCollection *actors = renderer->GetActors();
    for (int i = 0; i < actors->GetNumberOfItems(); ++i)
    {
        vtkActor *actor = vtkActor::SafeDownCast(actors->GetItemAsObject(i));
        actor->SetVisibility(visible);
    }
}


145 146 147 148 149
void Foam::functionObjects::runTimePostPro::scene::initialise
(
    vtkRenderer* renderer,
    const word& outputName
)
150 151
{
    currentFrameI_ = 0;
152
    position_ = startPosition_;
153 154 155

    outputName_ = outputName;

156 157 158
    if (!renderer) return;


159
    // Set the background
160 161 162
    const vector bgColour = colours_["background"]->value(position_);

    renderer->SetBackground(bgColour.x(), bgColour.y(), bgColour.z());
163 164 165 166

    // Apply gradient background if "background2" defined
    if (colours_.found("background2"))
    {
167 168
        const vector bg2Colour = colours_["background2"]->value(position_);

169
        renderer->GradientBackgroundOn();
170 171 172 173 174 175 176
        renderer->SetBackground2(bg2Colour.x(), bg2Colour.y(), bg2Colour.z());
    }
    else if (Pstream::parRun())
    {
        // Oddly enough we seem a gradient background for parallel rendering,
        // otherwise the colours look quite funny.
        // Doesn't seem to matter if we use SetBackground2() though
177

178 179
        renderer->GradientBackgroundOn();
        renderer->SetBackground2(bgColour.x(), bgColour.y(), bgColour.z());
180 181 182 183 184 185 186 187
    }

    // Depth peeling
    renderer->SetUseDepthPeeling(true);
    renderer->SetMaximumNumberOfPeels(4);
    renderer->SetOcclusionRatio(0);

    // Set the camera
188
    auto camera = vtkSmartPointer<vtkCamera>::New();
189 190 191
    camera->SetParallelProjection(parallelProjection_);
    renderer->SetActiveCamera(camera);

192
    // Add the lights
193
    auto lightKit = vtkSmartPointer<vtkLightKit>::New();
194 195
    lightKit->AddLightsToRenderer(renderer);

196
    if (!clipBox_.empty())
197 198 199
    {
        const point& min = clipBox_.min();
        const point& max = clipBox_.max();
200
        auto clipBox = vtkSmartPointer<vtkCubeSource>::New();
201 202 203 204 205 206 207 208 209
        clipBox->SetXLength(max.x() - min.x());
        clipBox->SetYLength(max.y() - min.y());
        clipBox->SetZLength(max.z() - min.z());
        clipBox->SetCenter
        (
            min.x() + 0.5*(max.x() - min.x()),
            min.y() + 0.5*(max.y() - min.y()),
            min.z() + 0.5*(max.z() - min.z())
        );
210
        auto clipMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
211 212
        clipMapper->SetInputConnection(clipBox->GetOutputPort());

213 214 215 216
        clipBoxActor_ = vtkSmartPointer<vtkActor>::New();
        clipBoxActor_->SetMapper(clipMapper);
        clipBoxActor_->VisibilityOff();
        renderer->AddActor(clipBoxActor_);
217 218 219 220
    }
}


221 222 223 224
void Foam::functionObjects::runTimePostPro::scene::setCamera
(
    vtkRenderer* renderer
) const
225
{
226 227
    if (!renderer) return;

228 229 230
    vtkCamera* camera = renderer->GetActiveCamera();

    if (parallelProjection_)
231
    {
232 233
        // Restore parallel scale to allow application of zoom (later)
        camera->SetParallelScale(1);
234
    }
235
    else
236 237 238
    {
        // Restore viewAngle (it might be reset by clipping)
        camera->SetViewAngle(cameraViewAngle_->value(position_));
239
    }
240 241 242 243 244 245 246 247 248 249 250 251 252 253

    const vector up = cameraUp_->value(position_);
    const vector pos = cameraPosition_->value(position_);
    const point focalPoint = cameraFocalPoint_->value(position_);
    const scalar zoom = cameraZoom_->value(position_);

    camera->SetViewUp(up.x(), up.y(), up.z());
    camera->SetPosition(pos.x(), pos.y(), pos.z());
    camera->SetFocalPoint(focalPoint.x(), focalPoint.y(), focalPoint.z());


    // Apply clipping if required
    // Note: possible optimisation - if the camera is static, this only needs
    //       to be done once on initialisation
254
    if (!clipBox_.empty())
255
    {
256
        setActorVisibility(renderer, false);
257
        clipBoxActor_->VisibilityOn();
258 259

        // Call ResetCamera() to fit clip box in view
260
        renderer->ResetCamera();
261 262

        setActorVisibility(renderer, true);
263 264 265 266 267 268 269
        clipBoxActor_->VisibilityOff();
    }

    // Zoom applied after all other operations
    camera->Zoom(zoom);

    camera->Modified();
270 271 272
}


273 274
Foam::string
Foam::functionObjects::runTimePostPro::scene::frameIndexStr() const
275 276 277 278 279 280 281 282 283 284
{
    string str = Foam::name(currentFrameI_);
    str.insert(0, 4 - str.length(), '0');

    return str;
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

285 286 287 288 289
Foam::functionObjects::runTimePostPro::scene::scene
(
    const objectRegistry& obr,
    const word& name
)
290 291 292 293
:
    obr_(obr),
    name_(name),
    colours_(),
Andrew Heather's avatar
Andrew Heather committed
294 295 296 297
    cameraPosition_(nullptr),
    cameraFocalPoint_(nullptr),
    cameraUp_(nullptr),
    cameraViewAngle_(nullptr),
298
    cameraZoom_(nullptr),
299
    clipBox_(boundBox::invertedBox),
300
    clipBoxActor_(),
301 302
    parallelProjection_(true),
    nFrameTotal_(1),
303
    startPosition_(0),
304 305 306 307 308 309 310 311 312
    position_(0),
    dPosition_(0),
    currentFrameI_(0),
    outputName_("unknown")
{}


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

313
Foam::functionObjects::runTimePostPro::scene::~scene()
314 315 316 317 318
{}


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

319
const Foam::HashPtrTable<Foam::Function1<Foam::vector>, Foam::word>&
320
Foam::functionObjects::runTimePostPro::scene::colours() const
321 322 323 324 325
{
    return colours_;
}


326
Foam::label Foam::functionObjects::runTimePostPro::scene::frameIndex() const
327 328 329 330 331
{
    return currentFrameI_;
}


332
Foam::scalar Foam::functionObjects::runTimePostPro::scene::position() const
333 334 335 336 337
{
    return position_;
}


338 339 340 341
void Foam::functionObjects::runTimePostPro::scene::read
(
    const dictionary& dict
)
342 343 344 345 346 347
{
    readCamera(dict.subDict("camera"));
    readColours(dict.subDict("colours"));
}


348
bool Foam::functionObjects::runTimePostPro::scene::loop(vtkRenderer* renderer)
349 350
{
    static bool initialised = false;
351
    setCamera(renderer);
352 353 354 355 356 357 358

    if (!initialised)
    {
        initialised = true;
        return true;
    }

359 360 361 362 363 364
    if (renderer)
    {

        // Ensure that all objects can be seen without clipping
        // Note: can only be done after all objects have been added!
        renderer->ResetCameraClippingRange();
365

366 367 368
        // Save image from last iteration
        saveImage(renderer->GetRenderWindow());
    }
369

370
    ++currentFrameI_;
371

372
    position_ = startPosition_ + currentFrameI_*dPosition_;
373

374 375 376 377 378 379
    if (currentFrameI_ < nFrameTotal_)
    {
        return true;
    }
    else
    {
380
        initialised = false;
381 382 383 384 385
        return false;
    }
}


386 387 388 389
void Foam::functionObjects::runTimePostPro::scene::saveImage
(
    vtkRenderWindow* renderWindow
) const
390 391 392 393 394 395
{
    if (!renderWindow)
    {
        return;
    }

396 397
    const Time& runTime = obr_.time();

398
    const fileName fName
399
    (
400 401 402 403
        runTime.globalPath()
      / functionObject::outputPrefix
      / name_
      / runTime.timeName()
404
      / outputName_ + '.' + frameIndexStr() + ".png"
405
    );
406

407 408 409
    renderWindow->Render();

    // Set up off-screen rendering
410
    auto windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
411 412 413 414 415 416 417 418 419 420 421 422

    windowToImageFilter->SetInput(renderWindow);

    //// Add alpha channel for transparency
    // windowToImageFilter->SetInputBufferTypeToRGBA();
    windowToImageFilter->SetInputBufferTypeToRGB();

//    windowToImageFilter->ReadFrontBufferOff();
    windowToImageFilter->Update();

    // Save the image

423 424 425 426 427 428 429 430 431
    if (Pstream::master())
    {
        mkDir(fName.path());

        auto writer = vtkSmartPointer<vtkPNGWriter>::New();
        writer->SetFileName(fName.c_str());
        writer->SetInputConnection(windowToImageFilter->GetOutputPort());

        Info<< "    Generating image: " << runTime.relativePath(fName) << endl;
432

433 434
        writer->Write();
    }
435 436 437
}


438

439
// ************************************************************************* //