text.C 4.91 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
     \\/     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 "text.H"
28
#include "stringOps.H"
29
#include "fvMesh.H"
30 31 32
#include "runTimePostProcessing.H"

// VTK includes
33
#include "vtkCoordinate.h"
34 35 36 37 38
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkTextActor.h"
#include "vtkTextProperty.h"

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

const Foam::Enum
<
    Foam::functionObjects::runTimePostPro::text::halignType
>
Foam::functionObjects::runTimePostPro::text::halignTypeNames
({
    { halignType::LEFT, "left" },
    { halignType::CENTER, "center" },
    { halignType::CENTER, "centre" },
    { halignType::RIGHT, "right" },
});


54 55
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

56
Foam::functionObjects::runTimePostPro::text::text
57 58 59
(
    const runTimePostProcessing& parent,
    const dictionary& dict,
60
    const HashPtrTable<Function1<vector>>& colours
61 62 63
)
:
    geometryBase(parent, dict, colours),
64
    string_(dict.get<string>("string")),
65
    positions_(),
66
    size_(dict.get<scalar>("size")),
Andrew Heather's avatar
Andrew Heather committed
67
    colour_(nullptr),
68 69
    halign_
    (
70
        halignTypeNames.getOrDefault("halign", dict, halignType::LEFT)
71
    ),
72
    bold_(dict.get<bool>("bold")),
73 74 75
    italic_(dict.getOrDefault("italic", false)),
    shadow_(dict.getOrDefault("shadow", false)),
    timeStamp_(dict.getOrDefault("timeStamp", false))
76
{
77 78 79 80 81 82 83 84 85 86 87 88 89 90
    if (!dict.readIfPresent("positions", positions_))
    {
        positions_.resize(1);
        dict.readEntry("position", positions_.first());
    }

    // Additional safety
    if (positions_.empty())
    {
        positions_.resize(1);
        positions_.first() = {0, 0};
    }

    stringOps::inplaceExpand(string_, dict, true, true);
91

92 93
    if (dict.found("colour"))
    {
94
        colour_.reset(Function1<vector>::New("colour", dict));
95 96 97 98 99 100 101 102 103 104
    }
    else
    {
        colour_.reset(colours["text"]->clone().ptr());
    }
}


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

105
Foam::functionObjects::runTimePostPro::text::~text()
106 107 108 109 110
{}


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

111
void Foam::functionObjects::runTimePostPro::text::addGeometryToScene
112 113 114 115 116
(
    const scalar position,
    vtkRenderer* renderer
)
{
117
    if (!visible_ || !renderer || !Pstream::master())
118
    {
119
        // Add text on master only!
120 121 122
        return;
    }

123
    DebugInfo << "    Add text: " << string_ << nl;
124

125
    // Concatenate string with timeStamp if true
126
    string str = string_;
127 128
    if (timeStamp_)
    {
129
        str += " " + geometryBase::parent_.mesh().time().timeName();
130
    }
131

132
    const vector textColour = colour_->value(position);
133

134
    const scalar textOpacity = opacity(position);
135

136 137 138
    for (const auto& textPosition : positions_)
    {
        auto actor = vtkSmartPointer<vtkTextActor>::New();
139

140
        actor->SetInput(str.c_str());
141

142 143 144 145 146 147 148 149 150 151 152 153
        vtkTextProperty* prop = actor->GetTextProperty();

        prop->SetFontFamilyToArial();
        prop->SetFontSize(size_);
        prop->SetJustification(int(halign_));
        prop->SetVerticalJustificationToBottom();
        prop->SetBold(bold_);
        prop->SetItalic(italic_);
        prop->SetShadow(shadow_);

        prop->SetColor(textColour[0], textColour[1], textColour[2]);
        prop->SetOpacity(textOpacity);
154

155 156 157 158 159 160 161 162 163 164
        // Positioning
        {
            vtkCoordinate* coord = actor->GetPositionCoordinate();

            coord->SetCoordinateSystemToNormalizedViewport();
            coord->SetValue(textPosition.first(), textPosition.second());
        }

        renderer->AddActor2D(actor);
    }
165 166 167
}


168 169 170 171
void Foam::functionObjects::runTimePostPro::text::updateActors
(
    const scalar position
)
172
{
173 174 175 176 177 178 179
    // Do nothing - all handled by addGeometryToScene
}


bool Foam::functionObjects::runTimePostPro::text::clear()
{
    return true;
180 181 182 183
}


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