colourTable.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) 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 #include "colourTable.H"
29 #include "colourTools.H"
30 #include "ListOps.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const Foam::Enum
35 <
37 >
39 ({
40  { interpolationType::RGB, "rgb" },
41  { interpolationType::HSV, "hsv" },
42  { interpolationType::DIVERGING, "diverging" },
43 });
44 
45 
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47 
49 (
51  const interpolationType interp
52 )
53 :
54  table_(values),
55  interp_(interp)
56 {}
57 
58 
60 (
62  const interpolationType interp
63 )
64 :
65  table_(std::move(values)),
66  interp_(interp)
67 {}
68 
69 
71 (
72  const dictionary& dict,
73  const interpolationType interp
74 )
75 :
76  table_(),
77  interp_(interp)
78 {
79  dict.readEntry("table", table_);
80  interpolationTypeNames.readIfPresent("interpolate", dict, interp_);
81 }
82 
83 
84 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
85 
87 {
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
95 {
96  if (x <= 0)
97  {
98  return table_.first().second();
99  }
100 
101  if (x >= 1)
102  {
103  return table_.last().second();
104  }
105 
106 
107  label idx = findLower
108  (
109  table_, x, 0,
110  [](const pair_type& pr, const scalar& val)
111  {
112  // Test first element
113  return (pr.first() <= val);
114  }
115  );
116 
117  if (idx == -1)
118  {
119  // Use first element only
120  return table_.first().second();
121  }
122  else if (idx == table_.size()-1)
123  {
124  // Use last element only
125  return table_.last().second();
126  }
127 
128  const scalar t0 = table_[idx].first();
129  const scalar t1 = table_[idx+1].first();
130 
131  const scalar s = (x - t0)/(t1 - t0);
132 
133  const vector& rgb0 = table_[idx].second();
134  const vector& rgb1 = table_[idx+1].second();
135 
136  if (interp_ == DIVERGING)
137  {
138  return colourTools::interpolateDiverging(s, rgb0, rgb1);
139  }
140  else if (interp_ == HSV)
141  {
142  return colourTools::interpolateHSV(s, rgb0, rgb1);
143  }
144 
145  return colourTools::interpolateRGB(s, rgb0, rgb1);
146 }
147 
148 
150 Foam::colourTable::table(const label nColours) const
151 {
152  List<Tuple2<scalar, vector>> lut(nColours);
153 
154  for (label i=0; i < nColours; ++i)
155  {
156  const scalar x = scalar(i)/scalar(nColours-1);
157 
158  lut[i] = pair_type(x, value(x));
159  }
160 
161  return lut;
162 }
163 
164 
166 {
167  os.beginBlock();
168  os.writeEntry("interpolate", interpolationTypeNames[interp_]);
169  os.writeEntry("table", table_);
170  os.endBlock();
171 
172  return os;
173 }
174 
175 
177 {
178  tbl.writeDict(os);
179 
180  return os;
181 }
182 
183 
184 // ************************************************************************* //
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::colourTable::colourTable
colourTable(const List< Tuple2< scalar, vector >> &values, const interpolationType interp=interpolationType::RGB)
Copy construct from table values.
Definition: colourTable.C:49
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::colourTable::New
static autoPtr< colourTable > New(Istream &is)
Read as dictionary content.
Definition: colourTable.C:86
Foam::colourTable::interpolationTypeNames
static const Enum< interpolationType > interpolationTypeNames
Enumeration names for interpolationType.
Definition: colourTable.H:105
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
Foam::colourTable::value
vector value(const scalar x) const
Return the colour at x (within 0-1 range)
Definition: colourTable.C:94
Foam::colourTable::interpolationType
interpolationType
Internal interpolation type.
Definition: colourTable.H:84
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
colourTools.H
Foam::findLower
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::colourTable::table
List< Tuple2< scalar, vector > > table(const label nColours) const
Return a discrete lookup table of colours.
Definition: colourTable.C:150
colourTable.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:109
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::colourTable::writeDict
Ostream & writeDict(Ostream &os) const
Write as dictionary format.
Definition: colourTable.C:165
Foam::colourTools::interpolateRGB
vector interpolateRGB(scalar s, const vector &rgb1, const vector &rgb2)
Interpolate RGB values in RGB colourspace.
Definition: colourTools.H:196
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::colourTable
Base class for generating a colour table from node points.
Definition: colourTable.H:79
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:236
ListOps.H
Various functions to operate on Lists.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::colourTools::interpolateDiverging
void interpolateDiverging(scalar s, const vector &rgb1, const vector &rgb2, vector &result)
Interpolate RGB values with diverging color map.
Definition: colourTools.C:403
Foam::Tuple2::first
const T1 & first() const noexcept
Return first.
Definition: Tuple2.H:118
Foam::colourTools::interpolateHSV
void interpolateHSV(scalar s, const vector &rgb1, const vector &rgb2, vector &result)
Interpolate RGB values in HSV colourspace.
Definition: colourTools.C:467