Skip to content

Commit b98ba21

Browse files
committed
Merge pull request #860 from SCIInstitute/brig_work
Completed color map options applied to geometry.
2 parents c989fae + e6eb56c commit b98ba21

File tree

19 files changed

+481
-248
lines changed

19 files changed

+481
-248
lines changed

src/Core/Datatypes/ColorMap.cc

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,24 @@
3131

3232
using namespace SCIRun::Core::Datatypes;
3333

34-
ColorMap::ColorMap(const std::string& name, const size_t resolution, const double shift)
35-
: name_(name), resolution_(resolution), shift_(shift) {}
34+
ColorMap::ColorMap(const std::string& name, const size_t resolution, const double shift, const bool invert)
35+
: name_(name), resolution_(resolution), shift_(shift), invert_(invert) {
36+
if (!(name_ == "Rainbow" ||
37+
name_ == "Blackbody" ||
38+
name_ == "Grayscale" ))
39+
throw InvalidArgumentException();
40+
}
3641

3742
ColorMap* ColorMap::clone() const
3843
{
39-
return new ColorMap(name_);
44+
return new ColorMap(name_,resolution_,shift_,invert_);
4045
}
4146

42-
ColorMapHandle StandardColorMapFactory::create(const std::string& name)
47+
ColorMapHandle StandardColorMapFactory::create(const std::string& name, const size_t &resolution,
48+
const double &shift, const bool &invert)
4349
{
44-
if (name == "Rainbow")
45-
return ColorMapHandle(rainbow_.clone());
46-
if (name == "Grayscale")
47-
return ColorMapHandle(grayscale_.clone());
48-
if (name == "Blackbody")
49-
return ColorMapHandle(blackbody_.clone());
50-
THROW_INVALID_ARGUMENT("Unknown standard colormap name: " + name);
50+
cm_ = ColorMap(name,resolution,shift,invert);
51+
return ColorMapHandle(cm_.clone());
5152
}
5253

5354
float ColorMap::Hue_2_RGB(float v1, float v2, float vH) {
@@ -74,31 +75,52 @@ ColorRGB ColorMap::hslToRGB(float h, float s, float l) {
7475
g = Hue_2_RGB( var_1, var_2, h );
7576
b = Hue_2_RGB( var_1, var_2, h - ( .333333 ) );
7677
}
77-
return ColorRGB(r,g,b);
78+
return ColorRGB(std::min(std::max(r,0.f),1.f),
79+
std::min(std::max(g,0.f),1.f),
80+
std::min(std::max(b,0.f),1.f));
7881
}
7982

80-
ColorRGB ColorMap::getColorMapVal(float v) {
83+
84+
float ColorMap::getTransformedColor(float f) {
8185
//@todo this will not be needed with rescale color map.
82-
v = std::min(std::max(0.f,v),1.f);
83-
float shift = (static_cast<float>(shift_)+1.f)/2.f;
84-
v = (v + shift) / (shift + 0.5f);
86+
float v = std::min(std::max(0.f,f),1.f);
87+
double shift = shift_;
88+
if (invert_) {
89+
v = 1.f - v;
90+
shift *= -1.;
91+
}
8592
//apply the resolution
86-
v = static_cast<double>((static_cast<int>(v * static_cast<float>(resolution_)))) / static_cast<double>(resolution_ - 1);
93+
v = static_cast<double>((static_cast<int>(v *
94+
static_cast<float>(resolution_)))) /
95+
static_cast<double>(resolution_ - 1);
96+
// the shift is a gamma.
97+
float denom = std::tan(M_PI_2 * ( 0.5f - std::min(std::max(shift,-0.99),0.99) * 0.5f));
98+
// make sure we don't hit divide by zero
99+
if (std::isnan(denom)) denom = 0.f;
100+
denom = std::max(denom, 0.001f);
101+
v = std::pow(v,(1.f/denom));
102+
return v;
103+
}
104+
105+
ColorRGB ColorMap::getColorMapVal(float v) {
106+
float f = getTransformedColor(v);
107+
//now grab the RGB
87108
ColorRGB col;
88-
if (name_ == "Rainbow")
89-
col = hslToRGB((1.0-v) * 0.8, 0.95, 0.5);
90-
else if (name_ == "Blackbody") {
91-
if (v < 0.333333)
92-
col = ColorRGB(v * 3., 0., 0.);
93-
else if (v < 0.6666667)
94-
col = ColorRGB(1.,(v - 0.333333) * 3., 0.);
109+
if (name_ == "Rainbow") {
110+
// spread out the thin colors
111+
//if (v < 0.7 && v > 0.3)
112+
// v = v - 0.05f * std::sin((v - 0.3) * 6.f * M_PI / 0.8);
113+
col = hslToRGB((1. - f) * 0.7, 0.95, 0.5);
114+
} else if (name_ == "Blackbody") {
115+
if (f < 0.333333)
116+
col = ColorRGB(std::min(std::max(f * 3.,0.),1.), 0., 0.);
117+
else if (f < 0.6666666)
118+
col = ColorRGB(1.,std::min(std::max((f - 0.333333) * 3.,0.),1.), 0.);
95119
else
96-
col = ColorRGB(1., 1., (v - 0.6666667) * 3.);
120+
col = ColorRGB(1., 1., std::min(std::max((f - 0.6666666) * 3.,0.),1.));
97121
} else if (name_ == "Grayscale")
98-
col = hslToRGB(0., 0., v);
122+
col = hslToRGB(0., 0., f);
99123
return col;
100124
}
101125

102-
ColorMap StandardColorMapFactory::rainbow_("Rainbow");
103-
ColorMap StandardColorMapFactory::grayscale_("Grayscale");
104-
ColorMap StandardColorMapFactory::blackbody_("Blackbody");
126+
ColorMap StandardColorMapFactory::cm_("Rainbow");

src/Core/Datatypes/ColorMap.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,22 @@ namespace Datatypes {
4141
class SCISHARE ColorMap : public Datatype
4242
{
4343
public:
44-
explicit ColorMap(const std::string& name, const size_t resolution = 256, const double shift = 0.0);
44+
explicit ColorMap(const std::string& name, const size_t resolution = 256,
45+
const double shift = 0.0, const bool invert = false);
4546

4647
virtual ColorMap* clone() const;
4748

4849
std::string getColorMapName() const {return name_;}
50+
size_t getColorMapResolution() const {return resolution_;}
51+
double getColorMapShift() const {return shift_;}
52+
bool getColorMapInvert() const {return invert_;}
4953
Core::Datatypes::ColorRGB getColorMapVal(float v);
54+
float getTransformedColor(float v);
5055
private:
5156
std::string name_;
5257
size_t resolution_;
5358
double shift_;
59+
bool invert_;
5460
boost::shared_ptr<class ColorMapImpl> impl_;
5561
float Hue_2_RGB(float v1, float v2, float vH);
5662
Core::Datatypes::ColorRGB hslToRGB(float h, float s, float l);
@@ -59,12 +65,11 @@ namespace Datatypes {
5965
class SCISHARE StandardColorMapFactory : boost::noncopyable
6066
{
6167
public:
62-
static ColorMapHandle create(const std::string& name);
68+
static ColorMapHandle create(const std::string& name, const size_t &resolution,
69+
const double &shift, const bool &invert);
6370
private:
6471
StandardColorMapFactory();
65-
static ColorMap rainbow_;
66-
static ColorMap grayscale_;
67-
static ColorMap blackbody_;
72+
static ColorMap cm_;
6873
};
6974

7075
}}}

0 commit comments

Comments
 (0)