text.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015-2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // OpenFOAM includes
29 #include "text.H"
30 #include "stringOps.H"
31 #include "fvMesh.H"
32 #include "runTimePostProcessing.H"
33 
34 // VTK includes
35 #include "vtkCoordinate.h"
36 #include "vtkRenderer.h"
37 #include "vtkSmartPointer.h"
38 #include "vtkTextActor.h"
39 #include "vtkTextProperty.h"
40 
41 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
42 
43 const Foam::Enum
44 <
46 >
48 ({
49  { halignType::LEFT, "left" },
50  { halignType::CENTER, "center" },
51  { halignType::CENTER, "centre" },
52  { halignType::RIGHT, "right" },
53 });
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
59 (
60  const runTimePostProcessing& parent,
61  const dictionary& dict,
62  const HashPtrTable<Function1<vector>>& colours
63 )
64 :
65  geometryBase(parent, dict, colours),
66  string_(dict.get<string>("string")),
67  positions_(),
68  size_(dict.get<scalar>("size")),
69  colour_(nullptr),
70  halign_
71  (
72  halignTypeNames.getOrDefault("halign", dict, halignType::LEFT)
73  ),
74  bold_(dict.get<bool>("bold")),
75  italic_(dict.getOrDefault("italic", false)),
76  shadow_(dict.getOrDefault("shadow", false)),
77  timeStamp_(dict.getOrDefault("timeStamp", false))
78 {
79  if (!dict.readIfPresent("positions", positions_))
80  {
81  positions_.resize(1);
82  dict.readEntry("position", positions_.first());
83  }
84 
85  // Additional safety
86  if (positions_.empty())
87  {
88  positions_.resize(1);
89  positions_.first() = {0, 0};
90  }
91 
92  stringOps::inplaceExpand(string_, dict, true, true);
93 
94  if (dict.found("colour"))
95  {
96  colour_.reset(Function1<vector>::New("colour", dict));
97  }
98  else
99  {
100  colour_.reset(colours["text"]->clone().ptr());
101  }
102 }
103 
104 
105 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
106 
108 {}
109 
110 
111 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
112 
114 (
115  const scalar position,
116  vtkRenderer* renderer
117 )
118 {
119  if (!visible_ || !renderer || !Pstream::master())
120  {
121  // Add text on master only!
122  return;
123  }
124 
125  DebugInfo << " Add text: " << string_ << nl;
126 
127  // Concatenate string with timeStamp if true
128  string str = string_;
129  if (timeStamp_)
130  {
131  str += " " + geometryBase::parent_.mesh().time().timeName();
132  }
133 
134  const vector textColour = colour_->value(position);
135 
136  const scalar textOpacity = opacity(position);
137 
138  for (const auto& textPosition : positions_)
139  {
140  auto actor = vtkSmartPointer<vtkTextActor>::New();
141 
142  actor->SetInput(str.c_str());
143 
144  vtkTextProperty* prop = actor->GetTextProperty();
145 
146  prop->SetFontFamilyToArial();
147  prop->SetFontSize(size_);
148  prop->SetJustification(int(halign_));
149  prop->SetVerticalJustificationToBottom();
150  prop->SetBold(bold_);
151  prop->SetItalic(italic_);
152  prop->SetShadow(shadow_);
153 
154  prop->SetColor(textColour[0], textColour[1], textColour[2]);
155  prop->SetOpacity(textOpacity);
156 
157  // Positioning
158  {
159  vtkCoordinate* coord = actor->GetPositionCoordinate();
160 
161  coord->SetCoordinateSystemToNormalizedViewport();
162  coord->SetValue(textPosition.first(), textPosition.second());
163  }
164 
165  renderer->AddActor2D(actor);
166  }
167 }
168 
169 
171 (
172  const scalar position
173 )
174 {
175  // Do nothing - all handled by addGeometryToScene
176 }
177 
178 
180 {
181  return true;
182 }
183 
184 
185 // ************************************************************************* //
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:51
Foam::functionObjects::runTimePostPro::text::halignTypeNames
static const Enum< halignType > halignTypeNames
Horizontal alignment names (includes "center" and "centre")
Definition: text.H:192
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:764
Foam::functionObjects::runTimePostPro::text::updateActors
virtual void updateActors(const scalar position)
Update actors.
Definition: text.C:171
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:56
Foam::functionObjects::runTimePostProcessing
Generate images during run-time.
Definition: runTimePostProcessing.H:170
text.H
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Definition: stringOps.C:752
Foam::functionObjects::runTimePostPro::text::text
text(const text &)=delete
No copy construct.
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
fvMesh.H
Foam::functionObjects::runTimePostPro::text::~text
virtual ~text()
Destructor.
Definition: text.C:107
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::functionObjects::runTimePostPro::text::clear
virtual bool clear()
Clear files used to create the object(s) - no-op.
Definition: text.C:179
Foam::functionObjects::runTimePostPro::geometryBase
Base class for surface, text handling etc.
Definition: geometryBase.H:101
runTimePostProcessing.H
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>.
Definition: HashPtrTable.H:54
Foam::Vector< scalar >
Foam::functionObjects::runTimePostPro::text::halignType
halignType
Horizontal alignment type. Values to match VTK definitions.
Definition: text.H:184
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:246
Foam::functionObjects::runTimePostPro::text::addGeometryToScene
virtual void addGeometryToScene(const scalar position, vtkRenderer *renderer)
Add text to scene.
Definition: text.C:114
Foam::functionObjects::runTimePostPro::geometryBase::parent_
const runTimePostProcessing & parent_
Reference to the parent function object.
Definition: geometryBase.H:124
stringOps.H
Foam::functionObjects::runTimePostProcessing::mesh
const fvMesh & mesh() const
Reference to the underlying OpenFOAM mesh.
Definition: runTimePostProcessing.H:259