Skip to content

Commit 52d8e12

Browse files
authored
Merge branch 'master' into qt4-tag
2 parents ef7bb08 + 040a02b commit 52d8e12

File tree

12 files changed

+528
-522
lines changed

12 files changed

+528
-522
lines changed

src/Core/Datatypes/ColorMap.cc

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ using namespace SCIRun::Core::Geometry;
4242
using namespace SCIRun::Core::Logging;
4343

4444
ColorMap::ColorMap(ColorMapStrategyHandle color, const std::string& name, const size_t resolution, const double shift,
45-
const bool invert, const double rescale_scale, const double rescale_shift)
45+
const bool invert, const double rescale_scale, const double rescale_shift, const std::vector<double>& alphaPoints)
4646
: color_(color), nameInfo_(name), resolution_(resolution), shift_(shift),
47-
invert_(invert), rescale_scale_(rescale_scale), rescale_shift_(rescale_shift)
47+
invert_(invert), rescale_scale_(rescale_scale), rescale_shift_(rescale_shift),
48+
alphaLookup_(alphaPoints)
4849
{
4950

5051
}
@@ -116,7 +117,7 @@ namespace detail
116117

117118
ColorMapHandle StandardColorMapFactory::create(const std::string& name, const size_t &res,
118119
const double &shift, const bool &invert,
119-
const double &rescale_scale, const double &rescale_shift)
120+
const double &rescale_scale, const double &rescale_shift, const std::vector<double>& alphaPoints)
120121
{
121122
using namespace detail;
122123
ColorMapStrategyHandle color;
@@ -129,7 +130,7 @@ ColorMapHandle StandardColorMapFactory::create(const std::string& name, const si
129130
color.reset(new Rainbow);
130131
}
131132

132-
return boost::make_shared<ColorMap>(color, name, res, shift, invert, rescale_scale, rescale_shift);
133+
return boost::make_shared<ColorMap>(color, name, res, shift, invert, rescale_scale, rescale_shift, alphaPoints);
133134
}
134135

135136
StandardColorMapFactory::NameList StandardColorMapFactory::getList()
@@ -148,16 +149,6 @@ StandardColorMapFactory::NameList StandardColorMapFactory::getList()
148149
*/
149150
double ColorMap::getTransformedValue(double f) const
150151
{
151-
/////////////////////////////////////////////////
152-
//TODO: this seemingly useless code fixes a nasty crash bug on Windows. Don't delete it until a proper fix is implemented!
153-
static bool x = true;
154-
if (x)
155-
{
156-
std::cout << "";// this;// << " " << name_ << " " << resolution_ << " " << shift_ << " " << invert_ << std::endl;
157-
x = false;
158-
}
159-
/////////////////////////////////////////////////
160-
161152
const double rescaled01 = static_cast<double>((f + rescale_shift_) * rescale_scale_);
162153

163154
double v = std::min(std::max(0., rescaled01), 1.);
@@ -191,57 +182,83 @@ double ColorMap::getTransformedValue(double f) const
191182
ColorRGB ColorMap::getColorMapVal(double v) const
192183
{
193184
double f = getTransformedValue(v);
194-
//now grab the RGB
195185
auto colorWithoutAlpha = color_->getColorMapVal(f);
196-
//TODO:
197-
//return applyAlpha(f, colorWithoutAlpha);
198-
return colorWithoutAlpha;
186+
return applyAlpha(f, colorWithoutAlpha);
199187
}
200-
/*
201-
ColorRGB applyAlpha(double transformed, colorWithoutAlpha)
202-
...easy
203188

189+
ColorRGB ColorMap::applyAlpha(double transformed, ColorRGB colorWithoutAlpha) const
190+
{
191+
double a = alpha(transformed);
192+
return ColorRGB(colorWithoutAlpha.r(), colorWithoutAlpha.g(), colorWithoutAlpha.b(), a);
193+
}
204194

205-
double alpha(double transformedValue)
195+
double ColorMap::alpha(double transformedValue) const
206196
{
207-
// interpolate in alphaLookup_
197+
if(alphaLookup_.size() == 0) return 0.5;
198+
int i;
199+
for(i = 0; (i < alphaLookup_.size()) && (alphaLookup_[i] < transformedValue); i += 2);
200+
201+
double startx = 0.0f, starty, endx = 1.0f, endy;
202+
if(i == 0)
203+
{
204+
endx = alphaLookup_[0];
205+
starty = endy = alphaLookup_[1];
206+
}
207+
else if(i == alphaLookup_.size())
208+
{
209+
startx = alphaLookup_[i - 2];
210+
endy = starty = alphaLookup_[i - 1];
211+
}
212+
else
213+
{
214+
startx = alphaLookup_[i - 2];
215+
starty = alphaLookup_[i - 1];
216+
endx = alphaLookup_[i + 0];
217+
endy = alphaLookup_[i + 1];
218+
}
219+
220+
double interp = (transformedValue - startx) / (endx - startx);
221+
double value = ((1.0f - interp) * starty + (interp) * endy);
222+
return value;
208223
}
209-
*/
224+
210225

211226
/**
212227
* @name valueToColor
213228
* @brief Takes a scalar value and directly passes into getColorMap.
214229
* @param The raw data value as a scalar double.
215230
* @return The RGB value mapped from the scalar.
216231
*/
217-
ColorRGB ColorMap::valueToColor(double scalar) const {
218-
ColorRGB color = getColorMapVal(scalar);
219-
return color;
232+
ColorRGB ColorMap::valueToColor(double scalar) const
233+
{
234+
return getColorMapVal(scalar);
220235
}
236+
221237
/**
222238
* @name valueToColor
223239
* @brief Takes a tensor value and creates an RGB value based on the magnitude of the eigenvalues.
224240
* @param The raw data value as a tensor.
225241
* @return The RGB value mapped from the tensor.
226242
*/
227-
ColorRGB ColorMap::valueToColor(Tensor &tensor) const {
243+
ColorRGB ColorMap::valueToColor(Tensor &tensor) const
244+
{
228245
double eigen1, eigen2, eigen3;
229246
tensor.get_eigenvalues(eigen1, eigen2, eigen3);
230247
double magnitude = Vector(eigen1, eigen2, eigen3).length();
231-
ColorRGB color = getColorMapVal(magnitude);
232-
return color;
248+
return getColorMapVal(magnitude);
233249
}
250+
234251
/**
235252
* @name valueToColor
236253
* @brief Takes a vector value and creates an RGB value.
237254
* @param The raw data value as a vector.
238255
* @return The RGB value mapped from the vector.
239256
*/
240-
ColorRGB ColorMap::valueToColor(const Vector &vector) const {
257+
ColorRGB ColorMap::valueToColor(const Vector &vector) const
258+
{
241259
//TODO this is probably not implemented correctly.
242260
// return ColorRGB(getTransformedColor(fabs(vector.x())),getTransformedColor(fabs(vector.y())), getTransformedColor(fabs(vector.z())));
243-
ColorRGB color = getColorMapVal(vector.length());
244-
return color;
261+
return getColorMapVal(vector.length());
245262
}
246263

247264
// This Rainbow takes into account scientific visualization recommendations.

src/Core/Datatypes/ColorMap.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ namespace Datatypes {
5151
explicit ColorMap(ColorMapStrategyHandle color,
5252
const std::string& name = "Rainbow", const size_t resolution = 256,
5353
const double shift = 0.0, const bool invert = false,
54-
const double rescale_scale = .5, const double rescale_shift = 1.);
54+
const double rescale_scale = .5, const double rescale_shift = 1.0,
55+
const std::vector<double>& alphaPoints = {});
5556
//TODO cbright: pass in alpha vector
5657
virtual ColorMap* clone() const override;
5758

@@ -62,6 +63,7 @@ namespace Datatypes {
6263
bool getColorMapInvert() const;
6364
double getColorMapRescaleScale() const;
6465
double getColorMapRescaleShift() const;
66+
std::vector<double> getAlphaLookup() const {return alphaLookup_;}
6567

6668
ColorRGB valueToColor(double scalar) const;
6769
ColorRGB valueToColor(Core::Geometry::Tensor &tensor) const;
@@ -73,6 +75,8 @@ namespace Datatypes {
7375
///<< Internal functions.
7476
Core::Datatypes::ColorRGB getColorMapVal(double v) const;
7577
double getTransformedValue(double v) const;
78+
ColorRGB applyAlpha(double transformed, ColorRGB colorWithoutAlpha) const;
79+
double alpha(double transformedValue) const;
7680

7781
ColorMapStrategyHandle color_;
7882
///<< The colormap's name.
@@ -112,7 +116,8 @@ namespace Datatypes {
112116
// See explanation for defaults above in ColorMap Constructor
113117
static ColorMapHandle create(const std::string& name = "Rainbow", const size_t &resolution = 256,
114118
const double &shift = 0.0, const bool &invert = false,
115-
const double &rescale_scale = .5, const double &rescale_shift = 1.);
119+
const double &rescale_scale = .5, const double &rescale_shift = 1.0,
120+
const std::vector<double>& alphaPoints = {});
116121
typedef std::vector<std::string> NameList;
117122
static NameList getList();
118123
private:

src/Interface/Modules/Render/ES/shaders/Flat_ColorMap.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void main()
119119
else colorMapValue = texture2D(uTX0, vec2(vTexCoords.x, 0.0));
120120

121121
vec3 diffuseColor = colorMapValue.rgb;
122-
float transparency = uTransparency;
122+
float transparency = colorMapValue.a;
123123

124124
gl_FragColor = vec4(diffuseColor, transparency);
125125

src/Interface/Modules/Render/ES/shaders/Phong_ColorMap.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void main()
137137
vec3 diffuseColor = pow(colorMapValue.rgb, vec3(2.2));
138138
vec3 specularColor = uSpecularColor.rgb;
139139
vec3 ambientColor = uAmbientColor.rgb;
140-
float transparency = uTransparency;
140+
float transparency = colorMapValue.a;
141141

142142
vec3 normal = normalize(vNormal);
143143
if(gl_FrontFacing) normal = -normal;

src/Interface/Modules/Visualization/CreateStandardColorMapDialog.cc

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ using namespace SCIRun::Core::Datatypes;
3838

3939
typedef SCIRun::Modules::Visualization::CreateStandardColorMap CreateStandardColorMapModule;
4040

41+
namespace
42+
{
43+
const double colormapPreviewHeight = 83;
44+
const double colormapPreviewWidth = 365;
45+
const QRectF colorMapPreviewRect(0, 0, colormapPreviewWidth, colormapPreviewHeight);
46+
}
4147

4248
CreateStandardColorMapDialog::CreateStandardColorMapDialog(const std::string& name, ModuleStateHandle state,
4349
QWidget* parent /* = 0 */)
@@ -94,11 +100,10 @@ void CreateStandardColorMapDialog::pullSpecial()
94100
}
95101
else
96102
{
97-
previewColorMap_->addEndpoints();
98103
for (const auto& p : pointsVec)
99104
{
100105
auto pVec = p.toVector();
101-
previewColorMap_->addPoint(QPointF(pVec[0].toDouble(), pVec[1].toDouble()));
106+
previewColorMap_->addPoint(QPointF(pVec[0].toDouble() * colormapPreviewWidth, (1.0f - pVec[1].toDouble()) * colormapPreviewHeight));
102107
}
103108
}
104109
}
@@ -155,18 +160,10 @@ void CreateStandardColorMapDialog::onInvertCheck(bool b)
155160
AlphaFunctionManager::AlphaFunctionManager(const QPointF& start, const QPointF& end, ModuleStateHandle state, const boost::atomic<bool>& pulling) :
156161
state_(state),
157162
defaultStart_(start), defaultEnd_(end),
158-
alphaFunction_(ALPHA_VECTOR_LENGTH, DEFAULT_ALPHA),
159163
dialogPulling_(pulling)
160164
{
161165
}
162166

163-
namespace
164-
{
165-
const double colormapPreviewHeight = 83;
166-
const double colormapPreviewWidth = 365;
167-
const QRectF colorMapPreviewRect(0, 0, colormapPreviewWidth, colormapPreviewHeight);
168-
}
169-
170167
ColormapPreview::ColormapPreview(QGraphicsScene* scene, ModuleStateHandle state,
171168
const boost::atomic<bool>& pulling,
172169
QWidget* parent)
@@ -189,7 +186,6 @@ void ColormapPreview::mousePressEvent(QMouseEvent* event)
189186
}
190187

191188
//TODO: remove point if event & RightMouseButton
192-
193189
//TODO: points are movable!
194190
}
195191

@@ -201,13 +197,6 @@ void ColormapPreview::addDefaultLine()
201197
alphaPath_ = scene()->addLine(defaultStart_.x(), defaultStart_.y(),
202198
defaultEnd_.x(), defaultEnd_.y(),
203199
alphaLinePen);
204-
alphaManager_.insertEndpoints();
205-
}
206-
207-
void AlphaFunctionManager::insertEndpoints()
208-
{
209-
alphaPoints_.insert(defaultStart_);
210-
insert(defaultEnd_); // only update function and state after both endpoints are added
211200
}
212201

213202
void ColormapPreview::removeDefaultLine()
@@ -218,18 +207,13 @@ void ColormapPreview::removeDefaultLine()
218207

219208
void ColormapPreview::addPoint(const QPointF& point)
220209
{
221-
if (alphaManager_.alreadyExists(point))
222-
return;
210+
if (alphaManager_.alreadyExists(point)) return;
223211

224212
removeDefaultLine();
225213

226214
static QPen pointPen(Qt::white, 1);
227215
auto item = scene()->addEllipse(point.x() - 4, point.y() - 4, 8, 8, pointPen, QBrush(Qt::black));
228216
item->setZValue(1);
229-
// QString toolTip;
230-
// QDebug tt(&toolTip);
231-
// tt << "Alpha point " << point.x() << ", " << point.y() << " y% " << (1 - point.y() / sceneRect().height());
232-
// item->setToolTip(toolTip);
233217
alphaManager_.insert(point);
234218

235219
drawAlphaPolyline();
@@ -244,14 +228,12 @@ bool AlphaFunctionManager::alreadyExists(const QPointF& point) const
244228
void AlphaFunctionManager::insert(const QPointF& p)
245229
{
246230
alphaPoints_.insert(p);
247-
updateAlphaFunction();
248231
pushToState();
249232
}
250233

251234
void AlphaFunctionManager::clear()
252235
{
253236
alphaPoints_.clear();
254-
alphaFunction_.assign(ALPHA_VECTOR_LENGTH, DEFAULT_ALPHA);
255237
pushToState();
256238
}
257239

@@ -262,19 +244,15 @@ void AlphaFunctionManager::pushToState()
262244
if (!alphaPoints_.empty())
263245
{
264246
Variable::List alphaPointsVec;
265-
//strip endpoints before saving user-added points
266247
auto begin = alphaPoints_.begin(), end = alphaPoints_.end();
267-
std::advance(begin, 1);
268-
std::advance(end, -1);
269-
std::for_each(begin, end, [&](const QPointF& p) { alphaPointsVec.emplace_back(Name("alphaPoint"), makeAnonymousVariableList(p.x(), p.y())); });
248+
std::for_each(begin, end, [&](const QPointF& p) { alphaPointsVec.emplace_back(Name("alphaPoint"),
249+
makeAnonymousVariableList(p.x()/colormapPreviewWidth, 1.0f - p.y()/colormapPreviewHeight)); });
270250
state_->setValue(Parameters::AlphaUserPointsVector, alphaPointsVec);
271251
}
272252
else
273253
{
274254
state_->setValue(Parameters::AlphaUserPointsVector, Variable::List());
275255
}
276-
277-
state_->setTransientValue(Parameters::AlphaFunctionVector, alphaFunction_);
278256
}
279257
}
280258

@@ -284,15 +262,16 @@ void ColormapPreview::drawAlphaPolyline()
284262
auto pathItem = new QGraphicsPathItem();
285263
alphaPath_ = pathItem;
286264
pathItem->setPen(alphaLinePen);
265+
287266
QPainterPath path;
288-
QPointF from = defaultStart_;
289-
path.moveTo(from);
267+
auto start = alphaManager_.begin();
268+
auto end = alphaManager_.end(); std::advance(end, -1);
269+
QPointF from = QPointF(defaultStart_.x(), start->y());
270+
QPointF to = QPointF(defaultEnd_.x(), end->y());
290271

291-
for (const auto& point : alphaManager_)
292-
{
293-
path.lineTo(point);
294-
path.moveTo(point);
295-
}
272+
path.moveTo(from);
273+
for (const auto& point : alphaManager_) path.lineTo(point);
274+
path.lineTo(to);
296275

297276
pathItem->setPath(path);
298277
pathItem->setZValue(0);
@@ -310,27 +289,6 @@ void ColormapPreview::clearAlphaPointGraphics()
310289
addDefaultLine();
311290
}
312291

313-
void AlphaFunctionManager::updateAlphaFunction()
314-
{
315-
//from v4, color endpoints (0 and 1) are fixed at alpha = 0.5.
316-
// alphaFunction_ will sample from in between these endpoints, evenly spaced throughout open interval (0,1)
317-
318-
for (int i = 0; i < static_cast<int>(alphaFunction_.size()); ++i)
319-
{
320-
if (i > 0 && i < alphaFunction_.size() - 1)
321-
{
322-
double color = i / static_cast<double>(ALPHA_SAMPLES + 1);
323-
auto between = alphaLineEndpointsAtColor(color);
324-
alphaFunction_[i] = interpolateAlphaLineValue(between.first, between.second, color);
325-
// qDebug() << "Color: " << color << "Alpha: " << alphaFunction_[i] << "between points" << between.first << between.second;
326-
}
327-
else
328-
{
329-
alphaFunction_[i] = DEFAULT_ALPHA;
330-
}
331-
}
332-
}
333-
334292
std::pair<QPointF,QPointF> AlphaFunctionManager::alphaLineEndpointsAtColor(double color) const
335293
{
336294
auto rightIter = alphaPoints_.upper_bound(colorToPoint(color));
@@ -353,7 +311,7 @@ double AlphaFunctionManager::interpolateAlphaLineValue(const QPointF& leftEndpoi
353311

354312
double AlphaFunctionManager::pointYToAlpha(double y) const
355313
{
356-
return 1 - y / colorMapPreviewRect.height();
314+
return 1.0f - y / colorMapPreviewRect.height();
357315
}
358316

359317
QPointF AlphaFunctionManager::colorToPoint(double color) const

0 commit comments

Comments
 (0)