Skip to content

Commit 2e4d2bc

Browse files
committed
Merge pull request #857 from SCIInstitute/brig_work
#855 fix and parts of new create standard color map
2 parents b212a53 + 6328139 commit 2e4d2bc

File tree

11 files changed

+430
-91
lines changed

11 files changed

+430
-91
lines changed

src/Core/Datatypes/ColorMap.cc

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@
3131

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

34-
ColorMap::ColorMap(const std::string& name) : name_(name)
35-
{
36-
37-
}
34+
ColorMap::ColorMap(const std::string& name, const size_t resolution, const double shift)
35+
: name_(name), resolution_(resolution), shift_(shift) {}
3836

3937
ColorMap* ColorMap::clone() const
4038
{
@@ -45,13 +43,62 @@ ColorMapHandle StandardColorMapFactory::create(const std::string& name)
4543
{
4644
if (name == "Rainbow")
4745
return ColorMapHandle(rainbow_.clone());
48-
if (name == "Gray")
46+
if (name == "Grayscale")
4947
return ColorMapHandle(grayscale_.clone());
5048
if (name == "Blackbody")
5149
return ColorMapHandle(blackbody_.clone());
5250
THROW_INVALID_ARGUMENT("Unknown standard colormap name: " + name);
5351
}
5452

53+
float ColorMap::Hue_2_RGB(float v1, float v2, float vH) {
54+
if ( vH < 0 ) vH += 1.;
55+
if ( vH > 1 ) vH -= 1.;
56+
if ( ( 6 * vH ) < 1 ) return ( v1 + ( v2 - v1 ) * 6. * vH );
57+
if ( ( 2 * vH ) < 1 ) return ( v2 );
58+
if ( ( 3 * vH ) < 2 ) return ( v1 + ( v2 - v1 ) * ( ( .666667 ) - vH ) * 6. );
59+
return ( v1 );
60+
}
61+
62+
ColorRGB ColorMap::hslToRGB(float h, float s, float l) {
63+
float r,g,b;
64+
if ( s == 0. ) {
65+
r = g = b = l ;
66+
} else {
67+
float var_1, var_2;
68+
if ( l < 0.5 ) var_2 = l * ( 1. + s );
69+
else var_2 = ( l + s ) - ( s * l );
70+
71+
var_1 = 2 * l - var_2;
72+
73+
r = Hue_2_RGB( var_1, var_2, h + ( .333333 ) );
74+
g = Hue_2_RGB( var_1, var_2, h );
75+
b = Hue_2_RGB( var_1, var_2, h - ( .333333 ) );
76+
}
77+
return ColorRGB(r,g,b);
78+
}
79+
80+
ColorRGB ColorMap::getColorMapVal(float v) {
81+
//@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);
85+
//apply the resolution
86+
v = static_cast<double>((static_cast<int>(v * static_cast<float>(resolution_)))) / static_cast<double>(resolution_ - 1);
87+
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.);
95+
else
96+
col = ColorRGB(1., 1., (v - 0.6666667) * 3.);
97+
} else if (name_ == "Grayscale")
98+
col = hslToRGB(0., 0., v);
99+
return col;
100+
}
101+
55102
ColorMap StandardColorMapFactory::rainbow_("Rainbow");
56103
ColorMap StandardColorMapFactory::grayscale_("Grayscale");
57104
ColorMap StandardColorMapFactory::blackbody_("Blackbody");

src/Core/Datatypes/ColorMap.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <Core/Datatypes/Datatype.h>
3333
#include <boost/noncopyable.hpp>
34+
#include <Core/Datatypes/Color.h>
3435
#include <Core/Datatypes/share.h>
3536

3637
namespace SCIRun {
@@ -40,14 +41,19 @@ namespace Datatypes {
4041
class SCISHARE ColorMap : public Datatype
4142
{
4243
public:
43-
explicit ColorMap(const std::string& name);
44+
explicit ColorMap(const std::string& name, const size_t resolution = 256, const double shift = 0.0);
4445

4546
virtual ColorMap* clone() const;
4647

4748
std::string getColorMapName() const {return name_;}
49+
Core::Datatypes::ColorRGB getColorMapVal(float v);
4850
private:
4951
std::string name_;
52+
size_t resolution_;
53+
double shift_;
5054
boost::shared_ptr<class ColorMapImpl> impl_;
55+
float Hue_2_RGB(float v1, float v2, float vH);
56+
Core::Datatypes::ColorRGB hslToRGB(float h, float s, float l);
5157
};
5258

5359
class SCISHARE StandardColorMapFactory : boost::noncopyable

src/Interface/Modules/Base/ModuleDialogGeneric.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,36 @@ void ModuleDialogGeneric::addDynamicLabelManager(QLabel* label, const AlgorithmP
582582
{
583583
addWidgetSlotManager(boost::make_shared<DynamicLabelSlotManager>(state_, *this, stateKey, label));
584584
}
585+
/*
586+
class SliderSlotManager : public WidgetSlotManager
587+
{
588+
public:
589+
SliderSlotManager(ModuleStateHandle state, ModuleDialogGeneric& dialog, const AlgorithmParameterName& stateKey, QSlider* slider) :
590+
WidgetSlotManager(state, dialog), stateKey_(stateKey), slider_(slider)
591+
{
592+
}
593+
virtual void pull() override
594+
{
595+
auto newValue = state_->getValue(stateKey_).toInt();
596+
if (newValue != slider_->value())
597+
{
598+
LOG_DEBUG("In new version of pull code for QSlider: " << newValue);
599+
slider_->setValue(newValue);
600+
}
601+
}
602+
virtual void pushImpl() override
603+
{
604+
}
605+
private:
606+
AlgorithmParameterName stateKey_;
607+
QSlider* slider_;
608+
};
585609
610+
void ModuleDialogGeneric::addSliderManager(QSlider* slider, const AlgorithmParameterName& stateKey)
611+
{
612+
addWidgetSlotManager(boost::make_shared<DynamicLabelSlotManager>(state_, *this, stateKey, slider));
613+
}
614+
*/
586615
class RadioButtonGroupSlotManager : public WidgetSlotManager
587616
{
588617
public:

src/Interface/Modules/Base/ModuleDialogGeneric.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ namespace Gui {
112112
void addTwoChoiceBooleanComboBoxManager(QComboBox* comboBox, const Core::Algorithms::AlgorithmParameterName& stateKey);
113113
void addDynamicLabelManager(QLabel* label, const Core::Algorithms::AlgorithmParameterName& stateKey);
114114
void addRadioButtonGroupManager(std::initializer_list<QRadioButton*> radioButtons, const Core::Algorithms::AlgorithmParameterName& stateKey);
115+
//void addSliderManager(QSlider* slider, const Core::Algorithms::AlgorithmParameterName& stateKey);
115116
private:
116117
void addWidgetSlotManager(WidgetSlotManagerPtr ptr);
117118
void createExecuteAction();

src/Interface/Modules/Render/ES/SRInterface.cc

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ DEALINGS IN THE SOFTWARE.
7171
#include "systems/RenderColorMapSys.h"
7272
#include "systems/RenderTransBasicSys.h"
7373
#include "systems/RenderTransColorMapSys.h"
74+
#include <Core/Datatypes/ColorMap.h>
75+
76+
using namespace SCIRun::Core::Datatypes;
7477

7578
using namespace std::placeholders;
7679

@@ -913,60 +916,16 @@ namespace SCIRun {
913916
}
914917
}
915918

916-
float SRInterface::Hue_2_RGB(float v1, float v2, float vH) {
917-
if ( vH < 0 ) vH += 1.;
918-
if ( vH > 1 ) vH -= 1.;
919-
if ( ( 6 * vH ) < 1 ) return ( v1 + ( v2 - v1 ) * 6. * vH );
920-
if ( ( 2 * vH ) < 1 ) return ( v2 );
921-
if ( ( 3 * vH ) < 2 ) return ( v1 + ( v2 - v1 ) * ( ( .666667 ) - vH ) * 6. );
922-
return ( v1 );
923-
}
924-
925-
Core::Datatypes::ColorRGB SRInterface::hslToRGB(float h, float s, float l) {
926-
float r,g,b;
927-
if ( s == 0. ) {
928-
r = g = b = l ;
929-
} else {
930-
float var_1, var_2;
931-
if ( l < 0.5 ) var_2 = l * ( 1. + s );
932-
else var_2 = ( l + s ) - ( s * l );
933-
934-
var_1 = 2 * l - var_2;
935-
936-
r = Hue_2_RGB( var_1, var_2, h + ( .333333 ) );
937-
g = Hue_2_RGB( var_1, var_2, h );
938-
b = Hue_2_RGB( var_1, var_2, h - ( .333333 ) );
939-
}
940-
return Core::Datatypes::ColorRGB(r,g,b);
941-
}
942-
943-
Core::Datatypes::ColorRGB SRInterface::getColorMapVal(float v, std::string which) {
944-
Core::Datatypes::ColorRGB col;
945-
std::string str = which;
946-
if (str == "Rainbow")
947-
col = hslToRGB((1.0-v) * 0.8, 0.95, 0.5);
948-
else if (str == "Blackbody") {
949-
if (v < 0.333333)
950-
col = Core::Datatypes::ColorRGB(v * 3., 0., 0.);
951-
else if (v < 0.6666667)
952-
col = Core::Datatypes::ColorRGB(1.,(v - 0.333333) * 3., 0.);
953-
else
954-
col = Core::Datatypes::ColorRGB(1., 1., (v - 0.6666667) * 3.);
955-
} else if (str == "Grayscale")
956-
col = hslToRGB(0., 0., v);
957-
return col;
958-
}
959-
960919
// Create default colormaps.
961920
void SRInterface::generateColormaps()
962921
{
963922
size_t resolution = 1000;
964923
float step = 1.f / static_cast<float>(resolution);
965-
924+
ColorMap cm("Rainbow");
966925
std::vector<uint8_t> rainbow;
967926
rainbow.reserve(resolution * 3);
968927
for (float i = 0.f; i < 1.f; i+=step) {
969-
Core::Datatypes::ColorRGB col = getColorMapVal(i, "Rainbow");
928+
ColorRGB col = cm.getColorMapVal(i);
970929
rainbow.push_back(static_cast<uint8_t>(col.r() * 255.0f));
971930
rainbow.push_back(static_cast<uint8_t>(col.g() * 255.0f));
972931
rainbow.push_back(static_cast<uint8_t>(col.b() * 255.0f));
@@ -987,11 +946,12 @@ Core::Datatypes::ColorRGB SRInterface::getColorMapVal(float v, std::string which
987946
GL_RGBA,
988947
GL_UNSIGNED_BYTE, &rainbow[0]));
989948

949+
cm = ColorMap("Grayscale");
990950
// build grayscale texture.
991951
std::vector<uint8_t> grayscale;
992952
grayscale.reserve(resolution * 3);
993953
for (float i = 0.f; i < 1.f; i+=step) {
994-
Core::Datatypes::ColorRGB col = getColorMapVal(i, "Grayscale");
954+
ColorRGB col = cm.getColorMapVal(i);
995955
grayscale.push_back(static_cast<uint8_t>(col.r() * 255.0f));
996956
grayscale.push_back(static_cast<uint8_t>(col.g() * 255.0f));
997957
grayscale.push_back(static_cast<uint8_t>(col.b() * 255.0f));
@@ -1012,11 +972,12 @@ Core::Datatypes::ColorRGB SRInterface::getColorMapVal(float v, std::string which
1012972
GL_RGBA,
1013973
GL_UNSIGNED_BYTE, &grayscale[0]));
1014974

975+
cm = ColorMap("Blackbody");
1015976
//blackbody texture
1016977
std::vector<uint8_t> blackbody;
1017978
blackbody.reserve(resolution * 3);
1018979
for (float i = 0.f; i < 1.f; i+=step) {
1019-
Core::Datatypes::ColorRGB col = getColorMapVal(i, "Blackbody");
980+
ColorRGB col = cm.getColorMapVal(i);
1020981
blackbody.push_back(static_cast<uint8_t>(col.r() * 255.0f));
1021982
blackbody.push_back(static_cast<uint8_t>(col.g() * 255.0f));
1022983
blackbody.push_back(static_cast<uint8_t>(col.b() * 255.0f));

src/Interface/Modules/Render/ES/SRInterface.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ namespace SCIRun {
136136

137137
/// Set the Background Color
138138
void setBackgroundColor(QColor color);
139-
140-
//choose a color value for color mapping based on value and type of color map
141-
static Core::Datatypes::ColorRGB getColorMapVal(float v, std::string which);
142139

143140
private:
144141

@@ -185,11 +182,6 @@ namespace SCIRun {
185182

186183
int mPort;
187184
};
188-
//internal color map options
189-
190-
static float Hue_2_RGB(float v1, float v2, float vH) ;
191-
static Core::Datatypes::ColorRGB hslToRGB(float h, float s, float l) ;
192-
193185
// Sets up ESCore.
194186
void setupCore();
195187

0 commit comments

Comments
 (0)