Commit 2e9b8d1c authored by Mark Olesen's avatar Mark Olesen

ENH: add colour for x3d surface writer (#1057)

- can be used for outputing sampled surfaces in x3d format for direct
  import in other rendering tools.
parent cc405548
Test-colourTables.C
EXE = $(FOAM_USER_APPBIN)/Test-colourTables
EXE_INC = \
-I$(LIB_SRC)/fileFormats/lnInclude
EXE_LIBS = \
-lfileFormats
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ 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/>.
\*---------------------------------------------------------------------------*/
#include "colourTable.H"
#include "IOstreams.H"
using namespace Foam;
void dumpTable(const colourTable& tbl, const label n=128)
{
Info<< tbl.table(n) << nl;
}
void dumpTable(const colourTable* tbl, const label n=128)
{
if (tbl)
{
Info<< tbl->table(n) << nl;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main()
{
// dumpTable(colourTable::ptr(colourTable::RAINBOW));
dumpTable(colourTable::ptr(colourTable::COOL_WARM));
// forAllConstIters(colourTable::tables(), iter)
// {
// Info<< nl << iter.key() << nl;
// dumpTable(iter.val());
// }
Info<< "\nDone\n";
return 0;
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v1812 |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
// An OpenFOAM dictionary of colourTables.
// The names should match those in the colourTables class.
6
(
coolToWarm
{
// ParaView: "Cool To Warm"
interpolate diverging;
table
(
( 0.0 ( 0.231372 0.298039 0.752941 ) )
( 0.5 ( 0.865003 0.865003 0.865003 ) )
( 1.0 ( 0.705882 0.0156863 0.14902 ) )
);
}
coldAndHot
{
// ParaView : "Cold and Hot"
interpolate diverging;
table
(
( 0 ( 0 1 1 ) )
( 0.45 ( 0 0 1 ) )
( 0.5 ( 0 0 0.5019608) )
( 0.55 ( 1 0 0 ) )
( 1 ( 1 1 0 ) )
);
}
fire
{
// ParaView: Black-Body Radiation
interpolate rbg;
table
(
( 0 ( 0 0 0 ) )
( 0.4 ( 0.901961 0 0 ) )
( 0.8 ( 0.901961 0.901961 0 ) )
( 1 ( 1 1 1 ) )
);
}
rainbow
{
interpolate hsv;
table
(
( 0 ( 0 0 1 ) )
( 0.5 ( 0 1 0 ) )
( 1 ( 1 0 0 ) )
);
}
greyscale
{
// ParaView: grayscale
interpolate rbg;
table
(
( 0 ( 0 0 0 ) )
( 1 ( 1 1 1 ) )
);
}
xray
{
// ParaView: "X ray"
interpolate rbg;
table
(
( 0 ( 1 1 1 ) )
( 1 ( 0 0 0 ) )
);
}
)
// ************************************************************************* //
colours/colourTable.C
colours/colourTables.C
colours/colourTools.C
ensight/file/ensightCase.C
ensight/file/ensightCaseOptions.C
ensight/file/ensightFile.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ 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/>.
\*---------------------------------------------------------------------------*/
#include "colourTable.H"
#include "colourTools.H"
#include "ListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::Enum
<
Foam::colourTable::interpolationType
>
Foam::colourTable::interpolationTypeNames
({
{ interpolationType::RGB, "rgb" },
{ interpolationType::HSV, "hsv" },
{ interpolationType::DIVERGING, "diverging" },
});
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::colourTable::colourTable
(
const List<Tuple2<scalar, vector>>& values,
const interpolationType interp
)
:
table_(values),
interp_(interp)
{}
Foam::colourTable::colourTable
(
List<Tuple2<scalar, vector>>&& values,
const interpolationType interp
)
:
table_(std::move(values)),
interp_(interp)
{}
Foam::colourTable::colourTable
(
const dictionary& dict,
const interpolationType interp
)
:
table_(),
interp_(interp)
{
dict.readEntry("table", table_);
interpolationTypeNames.readIfPresent("interpolate", dict, interp_);
}
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
Foam::autoPtr<Foam::colourTable> Foam::colourTable::New(Istream& is)
{
return autoPtr<colourTable>::New(dictionary(is));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::vector Foam::colourTable::value(const scalar x) const
{
if (x <= 0)
{
return table_.first().second();
}
if (x >= 1)
{
return table_.last().second();
}
label idx = findLower
(
table_, x, 0,
[](const pair_type& pr, const scalar& val)
{
// Test first element
return (pr.first() <= val);
}
);
if (idx == -1)
{
// Use first element only
return table_.first().second();
}
else if (idx == table_.size()-1)
{
// Use last element only
return table_.last().second();
}
const scalar t0 = table_[idx].first();
const scalar t1 = table_[idx+1].first();
const scalar s = (x - t0)/(t1 - t0);
const vector& rgb0 = table_[idx].second();
const vector& rgb1 = table_[idx+1].second();
if (interp_ == DIVERGING)
{
return colourTools::interpolateDiverging(s, rgb0, rgb1);
}
else if (interp_ == HSV)
{
return colourTools::interpolateHSV(s, rgb0, rgb1);
}
return colourTools::interpolateRGB(s, rgb0, rgb1);
}
Foam::List<Foam::Tuple2<Foam::scalar, Foam::vector>>
Foam::colourTable::table(const label nColours) const
{
List<Tuple2<scalar, vector>> lut(nColours);
for (label i=0; i < nColours; ++i)
{
const scalar x = scalar(i)/scalar(nColours-1);
lut[i] = pair_type(x, value(x));
}
return lut;
}
Foam::Ostream& Foam::colourTable::writeDict(Ostream& os) const
{
os.beginBlock();
os.writeEntry("interpolate", interpolationTypeNames[interp_]);
os.writeEntry("table", table_);
os.endBlock();
return os;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const colourTable& tbl)
{
tbl.writeDict(os);
return os;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ 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/>.
Class
Foam::colourTable
Description
Base class for generating a colour table from node points.
Dictionary definition
\table
Property | Description | Required | Default
interpolate | rgb/hsv/diverging | no | rgb
table | Node points for the colour table | yes |
\endtable
Predefined colour tables (in "etc/colourTables") include
"coolToWarm", "coldAndHot", "fire", "rainbow", "greyscale", "xray".
SourceFiles
colourTable.C
\*---------------------------------------------------------------------------*/
#ifndef colourTable_H
#define colourTable_H
#include "Enum.H"
#include "List.H"
#include "Tuple2.H"
#include "vector.H"
#include "HashPtrTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class colourTable Declaration
\*---------------------------------------------------------------------------*/
class colourTable
{
public:
//- Internal interpolation type
enum interpolationType
{
RGB,
HSV,
DIVERGING
};
//- Enumeration of commonly used colour tables.
// The indices must match those in "etc/colourTables"
enum predefinedType
{
COOL_WARM, //!< "coolToWarm"
COLD_HOT, //!< "coldAndHot"
FIRE, //!< "fire" - ParaView "Black-Body Radiation"
RAINBOW, //!< "rainbow"
GREYSCALE, //!< greyscale - ParaView "Grayscale"
XRAY //!< "xray" - ParaView "X Ray"
};
//- Enumeration names for interpolationType
static const Enum<interpolationType> interpolationTypeNames;
//- Enumeration names for predefinedType
static const Enum<predefinedType> predefinedNames;
//- The data lookup type
typedef Tuple2<scalar, vector> pair_type;
// Lookup Colour Tables
//- Look up pointer to colourTable by name, or nullptr on failure.
static const colourTable* ptr(const word& tableName);
//- Look up pointer to colourTable by type, or nullptr on failure.
static const colourTable* ptr(const predefinedType tbl);
//- Look up pointer to colourTable by name. Fatal on failure
static const colourTable& ref(const word& tableName);
//- Look up pointer to colourTable by type. Fatal on failure
static const colourTable& ref(const predefinedType tbl);
private:
// Private Static Data
//- Predefined tables
static HashPtrTable<colourTable> tables_;
// Private Data
//- The table control points
List<pair_type> table_;
//- Interpolator type
interpolationType interp_;
// Private Member Functions
//- Construct from central "etc/colourTables" file.
static void constructTables();
public:
// Constructors
//- Copy construct from table values
explicit colourTable
(
const List<Tuple2<scalar, vector>>& values,
const interpolationType interp = interpolationType::RGB
);
//- Copy construct from table values
explicit colourTable
(
List<Tuple2<scalar, vector>>&& values,
const interpolationType interp = interpolationType::RGB
);
//- Read construct from dictionary
explicit colourTable
(
const dictionary& dict,
const interpolationType interp = interpolationType::RGB
);
// Selectors
//- Read as dictionary content
static autoPtr<colourTable> New(Istream& is);
//- Destructor
virtual ~colourTable() = default;
// Member Functions
// Access
//- Predefined tables
static const HashPtrTable<colourTable>& tables();
//- Return the colour at x (within 0-1 range)
vector value(const scalar x) const;
//- Return a discrete lookup table of colours
List<Tuple2<scalar, vector>> table(const label nColours) const;
// IO
//- Write as dictionary format
Ostream& writeDict(Ostream& os) const;
};
//- Write as dictionary format
Ostream& operator<<(Ostream& os, const colourTable& tbl);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ 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/>.
\*---------------------------------------------------------------------------*/
#include "colourTable.H"
#include "etcFiles.H"
#include "IFstream.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
Foam::HashPtrTable<Foam::colourTable> Foam::colourTable::tables_;
const Foam::Enum
<
Foam::colourTable::predefinedType
>
Foam::colourTable::predefinedNames
({
{ predefinedType::COOL_WARM, "coolToWarm" },
{ predefinedType::COLD_HOT, "coldAndHot" },
{ predefinedType::FIRE, "fire" },
{ predefinedType::RAINBOW, "rainbow" },
{ predefinedType::GREYSCALE, "greyscale" },
{ predefinedType::XRAY, "xray" },
});
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::colourTable::constructTables()
{
if (tables_.size())
{
FatalErrorInFunction
<< "attempt to re-construct colourTables when they already exist"
<< exit(FatalError);
}
IFstream is(findEtcFile("colourTables", true)); // Mandatory file
HashPtrTable<colourTable> newEntries(is);
tables_.swap(newEntries);
Info<< "loaded " << tables_.sortedToc()
<< " from etc/colourTable" << endl;
Info<< "== " << tables_ << nl;
}
const Foam::HashPtrTable<Foam::colourTable>& Foam::colourTable::tables()
{
if (tables_.empty())
{
constructTables();
}
return tables_;