/* This file is part of the OpenLB library
*
* Copyright (C) 2006, 2007 Jonas Latt
* E-mail contact: info@openlb.net
* The most recent release of OpenLB can be downloaded at
*
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef COLORMAPS_HH
#define COLORMAPS_HH
#include "colormaps.h"
#include
namespace olb {
namespace graphics {
template
LinearFunction::LinearFunction(T x1_, T x2_, T y1_, T y2_)
: x1(x1_), x2(x2_), y1(y1_), y2(y2_)
{ }
template
T LinearFunction::operator() (T x) const
{
return ( (y2-y1) * x + x2*y1-x1*y2 )/(x2-x1);
}
template
LinearFunction* LinearFunction::clone() const
{
return new LinearFunction(*this);
}
template
PowerLawFunction::PowerLawFunction(T x1_, T x2_, T y1_, T y2_, T b_)
: x1(std::pow(x1_,b_)), x2(std::pow(x2_,b_)), y1(y1_), y2(y2_), b(b_)
{ }
template
T PowerLawFunction::operator() (T x) const
{
return ( (y2-y1) * std::pow(x,b) + x2*y1-x1*y2 )/(x2-x1);
}
template
PowerLawFunction* PowerLawFunction::clone() const
{
return new PowerLawFunction(*this);
}
template
PiecewiseFunction::PiecewiseFunction(PiecewiseFunction const& rhs)
: pieces(rhs.pieces),
functions(rhs.functions.size())
{
for (unsigned iF=0; iFclone();
}
}
template
PiecewiseFunction& PiecewiseFunction::operator=(PiecewiseFunction const& rhs)
{
PiecewiseFunction(rhs).swap(*this);
return *this;
}
template
PiecewiseFunction::~PiecewiseFunction()
{
for (unsigned iF=0; iF
void PiecewiseFunction::swap(PiecewiseFunction& rhs)
{
pieces.swap(rhs.pieces);
functions.swap(rhs.functions);
}
template
void PiecewiseFunction::addPiece(Piece piece, ScalarFunction* f)
{
typename std::vector >::iterator pieceIt = pieces.begin();
typename std::vector*>::iterator fIt = functions.begin();
while (pieceIt != pieces.end() && piece.closedBegin >= pieceIt->closedBegin) {
++pieceIt;
++fIt;
}
pieces.insert(pieceIt, piece);
functions.insert(fIt, f);
}
template
T PiecewiseFunction::operator() (T x) const
{
if (pieces.empty() || x= pieces[iPiece].openEnd) {
++iPiece;
}
if (iPiece == pieces.size() || x < pieces[iPiece].closedBegin) {
return T();
}
return (*functions[iPiece])(x);
}
template
PiecewiseFunction* PiecewiseFunction::clone() const
{
return new PiecewiseFunction(*this);
}
template
ColorMap::ColorMap(PiecewiseFunction const& red_,
PiecewiseFunction const& green_,
PiecewiseFunction const& blue_)
: red(red_), green(green_), blue(blue_)
{ }
template
rgb ColorMap::get(T x) const
{
return rgb( red(x), green(x), blue(x) );
}
namespace mapGenerators {
template
PiecewiseFunction generateEarthRed()
{
T p0 = (T) 0.;
T p1 = (T) 3./8.;
T p2 = (T) 6./8.;
T p3 = (T) 1.;
PiecewiseFunction earthRed;
earthRed.addPiece (Piece(p0, p1), new PowerLawFunction(p0, p1, (T)0., (T)0.8, (T)0.6) );
earthRed.addPiece (Piece(p1, p2), new PowerLawFunction(p1, p2, (T)0.8, (T)0.9, (T)0.9) );
earthRed.addPiece (Piece(p2, p3), new PowerLawFunction(p2, p3, (T)0.9, (T)1.0, (T)0.2) );
return earthRed;
}
template
PiecewiseFunction generateEarthGreen()
{
T p0 = (T) 0.;
T p1 = (T) 3./8.;
T p2 = (T) 6./8.;
T p3 = (T) 1.;
PiecewiseFunction earthGreen;
earthGreen.addPiece (Piece(p0, p1), new PowerLawFunction(p0, p1, (T)0., (T)0.5, (T)0.6) );
earthGreen.addPiece (Piece(p1, p2), new PowerLawFunction(p1, p2, (T)0.5, (T)0.9, (T)0.2) );
earthGreen.addPiece (Piece(p2, p3), new PowerLawFunction(p2, p3, (T)0.9, (T)1.0, (T)0.2) );
return earthGreen;
}
template
PiecewiseFunction generateEarthBlue()
{
T p0 = (T) 0.;
T p1 = (T) 3./8.;
T p2 = (T) 6./8.;
T p3 = (T) 1.;
PiecewiseFunction earthBlue;
earthBlue.addPiece (Piece(p0, p1), new PowerLawFunction(p0, p1, (T)0., (T)0.5, (T)0.6) );
earthBlue.addPiece (Piece(p1, p2), new PowerLawFunction(p1, p2, (T)0.5, (T)0.7, (T)0.2) );
earthBlue.addPiece (Piece(p2, p3), new PowerLawFunction(p2, p3, (T)0.7, (T)1.0, (T)0.2) );
return earthBlue;
}
template
PiecewiseFunction generateWaterRed()
{
T p0 = (T) 0.;
T p1 = (T) 3./8.;
T p2 = (T) 6./8.;
T p3 = (T) 1.;
PiecewiseFunction waterRed;
waterRed.addPiece (Piece(p0, p1), new PowerLawFunction(p0, p1, (T)0., (T)0.5, (T)0.6) );
waterRed.addPiece (Piece(p1, p2), new PowerLawFunction(p1, p2, (T)0.5, (T)0.7, (T)0.2) );
waterRed.addPiece (Piece(p2, p3), new PowerLawFunction(p2, p3, (T)0.7, (T)1.0, (T)0.2) );
return waterRed;
}
template
PiecewiseFunction generateWaterGreen()
{
T p0 = (T) 0.;
T p1 = (T) 3./8.;
T p2 = (T) 6./8.;
T p3 = (T) 1.;
PiecewiseFunction waterGreen;
waterGreen.addPiece (Piece(p0, p1), new PowerLawFunction(p0, p1, (T)0., (T)0.5, (T)0.6) );
waterGreen.addPiece (Piece(p1, p2), new PowerLawFunction(p1, p2, (T)0.5, (T)0.9, (T)0.2) );
waterGreen.addPiece (Piece(p2, p3), new PowerLawFunction(p2, p3, (T)0.9, (T)1.0, (T)0.2) );
return waterGreen;
}
template
PiecewiseFunction generateWaterBlue()
{
T p0 = (T) 0.;
T p1 = (T) 3./8.;
T p2 = (T) 6./8.;
T p3 = (T) 1.;
PiecewiseFunction waterBlue;
waterBlue.addPiece (Piece(p0, p1), new PowerLawFunction(p0, p1, (T)0., (T)0.8, (T)0.6) );
waterBlue.addPiece (Piece(p1, p2), new PowerLawFunction(p1, p2, (T)0.8, (T)0.9, (T)0.9) );
waterBlue.addPiece (Piece(p2, p3), new PowerLawFunction(p2, p3, (T)0.9, (T)1.0, (T)0.2) );
return waterBlue;
}
template
PiecewiseFunction generateAirRed()
{
T p0 = (T) 0.;
T p1 = (T) 1.;
PiecewiseFunction airRed;
airRed.addPiece (Piece(p0, p1), new LinearFunction(p0, p1, (T)0., (T)1.) );
return airRed;
}
template
PiecewiseFunction generateAirGreen()
{
T p0 = (T) 0.;
T p1 = (T) 1.;
PiecewiseFunction airGreen;
airGreen.addPiece (Piece(p0, p1), new LinearFunction(p0, p1, (T)1., (T)0.) );
return airGreen;
}
template
PiecewiseFunction generateAirBlue()
{
T p0 = (T) 0.;
T p1 = (T) 1.;
PiecewiseFunction airBlue;
airBlue.addPiece (Piece(p0, p1), new LinearFunction(p0, p1, (T)1., (T)1.) );
return airBlue;
}
template
PiecewiseFunction generateFireRed()
{
T p0 = (T) 0.;
T p1 = (T) 0.36;
T p3 = (T) 1.;
PiecewiseFunction fireRed;
fireRed.addPiece (Piece(p0, p1), new LinearFunction(p0, p1, (T)0., (T)1.) );
fireRed.addPiece (Piece(p1, p3), new LinearFunction(p1, p3, (T)1., (T)1.) );
return fireRed;
}
template
PiecewiseFunction generateFireGreen()
{
T p0 = (T) 0.;
T p1 = (T) 0.36;
T p2 = (T) 0.75;
T p3 = (T) 1.;
PiecewiseFunction fireGreen;
fireGreen.addPiece (Piece(p0, p1), new LinearFunction(p0, p1, (T)0., (T)0.) );
fireGreen.addPiece (Piece(p1, p2), new LinearFunction(p1, p2, (T)0., (T)1.) );
fireGreen.addPiece (Piece(p2, p3), new LinearFunction(p2, p3, (T)1., (T)1.) );
return fireGreen;
}
template
PiecewiseFunction generateFireBlue()
{
T p0 = (T) 0.;
T p2 = (T) 0.75;
T p3 = (T) 1.;
PiecewiseFunction fireBlue;
fireBlue.addPiece (Piece(p0, p2), new LinearFunction(p0, p2, (T)0., (T)0.) );
fireBlue.addPiece (Piece(p2, p3), new LinearFunction(p2, p3, (T)0., (T)1.) );
return fireBlue;
}
template
PiecewiseFunction generateLeeLooRed()
{
T p0 = (T) 0.;
T p2 = (T) 3./8.;
T p3 = (T) 5./8.;
T p4 = (T) 7./8.;
T p5 = (T) 1.;
T p6 = (T) 9./8.;
PiecewiseFunction leeLooRed;
leeLooRed.addPiece (Piece(p0, p2), new LinearFunction(p0, p2, (T)0., (T)0.) );
leeLooRed.addPiece (Piece(p2, p3), new LinearFunction(p2, p3, (T)0., (T)1.) );
leeLooRed.addPiece (Piece(p3, p4), new LinearFunction(p3, p4, (T)1., (T)1.) );
leeLooRed.addPiece (Piece(p4, p5), new LinearFunction(p4, p6, (T)1., (T)0.) );
return leeLooRed;
}
template
PiecewiseFunction generateLeeLooGreen()
{
T p0 = (T) 0.;
T p1 = (T) 1./8.;
T p2 = (T) 3./8.;
T p3 = (T) 5./8.;
T p4 = (T) 7./8.;
T p5 = (T) 1.;
T p6 = (T) 9/8;
PiecewiseFunction leeLooGreen;
leeLooGreen.addPiece (Piece(p0, p1), new LinearFunction(p0, p1, (T)0., (T)0.) );
leeLooGreen.addPiece (Piece(p1, p2), new LinearFunction(p1, p2, (T)0., (T)1.) );
leeLooGreen.addPiece (Piece(p2, p3), new LinearFunction(p2, p3, (T)1., (T)1.) );
leeLooGreen.addPiece (Piece(p3, p4), new LinearFunction(p3, p4, (T)1., (T)0.) );
leeLooGreen.addPiece (Piece(p4, p5), new LinearFunction(p4, p6, (T)0., (T)0.) );
return leeLooGreen;
}
template
PiecewiseFunction generateLeeLooBlue()
{
T pm1 = (T) -1./8.;
T p0 = (T) 0.;
T p1 = (T) 1./8.;
T p2 = (T) 3./8.;
T p3 = (T) 5./8.;
T p5 = (T) 1.;
PiecewiseFunction leeLooBlue;
leeLooBlue.addPiece (Piece(p0, p1), new LinearFunction(pm1, p1, (T)0., (T)1.) );
leeLooBlue.addPiece (Piece(p1, p2), new LinearFunction(p1, p2, (T)1., (T)1.) );
leeLooBlue.addPiece (Piece(p2, p3), new LinearFunction(p2, p3, (T)1., (T)0.) );
leeLooBlue.addPiece (Piece(p3, p5), new LinearFunction(p3, p5, (T)0., (T)0.) );
return leeLooBlue;
}
template
ColorMap generateMap(std::string mapName)
{
if (mapName == "earth") {
return ColorMap (
generateEarthRed(),
generateEarthGreen(),
generateEarthBlue() );
} else if (mapName == "water") {
return ColorMap (
generateWaterRed(),
generateWaterGreen(),
generateWaterBlue() );
} else if (mapName == "air") {
return ColorMap (
generateAirRed(),
generateAirGreen(),
generateAirBlue() );
} else if (mapName == "fire") {
return ColorMap (
generateFireRed(),
generateFireGreen(),
generateFireBlue() );
} else if (mapName == "leeloo") {
return ColorMap (
generateLeeLooRed(),
generateLeeLooGreen(),
generateLeeLooBlue() );
}
return ColorMap (
generateLeeLooRed(),
generateLeeLooGreen(),
generateLeeLooBlue() );
}
} // namespace mapGenerators
} // namespace graphics
} // namespace olb
#endif