Rename layout variables in legends to make their intent and use clearer

This commit is contained in:
Jacob Støren 2018-04-20 09:23:19 +02:00
parent c3ae45e748
commit d48c2b7aa9
4 changed files with 103 additions and 93 deletions

View File

@ -95,10 +95,10 @@ bool CategoryLegend::pick(int oglXCoord, int oglYCoord, const Vec2i& position, c
layoutInfo(&layoutInViewPortCoords);
Vec2i legendBarOrigin = oglRect.min();
legendBarOrigin.x() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().x());
legendBarOrigin.y() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().y());
legendBarOrigin.x() += static_cast<uint>(layoutInViewPortCoords.colorBarRect.min().x());
legendBarOrigin.y() += static_cast<uint>(layoutInViewPortCoords.colorBarRect.min().y());
Recti legendBarRect = Recti(legendBarOrigin, static_cast<uint>(layoutInViewPortCoords.legendRect.width()), static_cast<uint>(layoutInViewPortCoords.legendRect.height()));
Recti legendBarRect = Recti(legendBarOrigin, static_cast<uint>(layoutInViewPortCoords.colorBarRect.width()), static_cast<uint>(layoutInViewPortCoords.colorBarRect.height()));
if ((oglXCoord > legendBarRect.min().x()) && (oglXCoord < legendBarRect.max().x()) &&
(oglYCoord > legendBarRect.min().y()) && (oglYCoord < legendBarRect.max().y()))
@ -162,7 +162,7 @@ void CategoryLegend::renderGeneric(OpenGLContext* oglContext,
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
OverlayColorLegendLayoutInfo* layout,
const OverlayColorLegendLayoutInfo* layout,
float* maxLegendRightPos)
{
if (m_categoryMapper.isNull())
@ -179,7 +179,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
m_visibleCategoryLabels.clear();
const float textX = layout->tickX + 5;
const float textX = layout->tickEndX + 5;
const float overlapTolerance = 1.2f * layout->charHeight;
float lastVisibleTextY = 0.0;
@ -187,11 +187,11 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
CVF_ASSERT(m_categoryMapper.notNull());
size_t numLabels = m_categoryMapper->categoryCount();
float categoryHeight = static_cast<float>(layout->legendRect.height() / numLabels);
float categoryHeight = static_cast<float>(layout->colorBarRect.height() / numLabels);
for (size_t it = 0; it < numLabels; it++)
{
float textY = static_cast<float>(layout->legendRect.min().y() + it * categoryHeight + categoryHeight / 2);
float textY = static_cast<float>(layout->colorBarRect.min().y() + it * categoryHeight + categoryHeight / 2);
// Always draw first and last tick label. For all others, skip drawing if text ends up
// on top of the previous label.
@ -204,7 +204,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
}
// Make sure it does not overlap the last tick as well
float lastTickY = static_cast<float>(layout->legendRect.max().y());
float lastTickY = static_cast<float>(layout->colorBarRect.max().y());
if (cvf::Math::abs(textY - lastTickY) < overlapTolerance)
{
@ -276,8 +276,8 @@ void CategoryLegend::renderLegendUsingShaders(OpenGLContext* oglContext,
float* v4 = &vertexArray[12];
// Constant coordinates
v0[0] = v3[0] = layout->x0;
v1[0] = v4[0] = layout->x1;
v0[0] = v3[0] = layout->tickStartX;
v1[0] = v4[0] = layout->tickMidX;
// Connects
static const ushort trianglesConnects[] = { 0, 1, 4, 0, 4, 3 };
@ -297,15 +297,15 @@ void CategoryLegend::renderLegendUsingShaders(OpenGLContext* oglContext,
// Render color bar as one colored quad per pixel
int legendHeightPixelCount = static_cast<int>(layout->legendRect.height());
int legendHeightPixelCount = static_cast<int>(layout->colorBarRect.height());
if (m_categoryMapper.notNull())
{
int iPx;
for (iPx = 0; iPx < legendHeightPixelCount; iPx++)
{
const Color3ub& clr = m_categoryMapper->mapToColor(m_categoryMapper->domainValue((iPx + 0.5) / legendHeightPixelCount));
float y0 = static_cast<float>(layout->legendRect.min().y() + iPx);
float y1 = static_cast<float>(layout->legendRect.min().y() + iPx + 1);
float y0 = static_cast<float>(layout->colorBarRect.min().y() + iPx);
float y1 = static_cast<float>(layout->colorBarRect.min().y() + iPx + 1);
// Dynamic coordinates for rectangle
v0[1] = v1[1] = y0;
@ -331,10 +331,10 @@ void CategoryLegend::renderLegendUsingShaders(OpenGLContext* oglContext,
bool isRenderingFrame = true;
if (isRenderingFrame)
{
v0[0] = v2[0] = layout->legendRect.min().x() - 0.5f;
v1[0] = v3[0] = layout->legendRect.max().x() - 0.5f;
v0[1] = v1[1] = layout->legendRect.min().y() - 0.5f;
v2[1] = v3[1] = layout->legendRect.max().y() - 0.5f;
v0[0] = v2[0] = layout->colorBarRect.min().x() - 0.5f;
v1[0] = v3[0] = layout->colorBarRect.max().x() - 0.5f;
v0[1] = v1[1] = layout->colorBarRect.min().y() - 0.5f;
v2[1] = v3[1] = layout->colorBarRect.max().y() - 0.5f;
static const ushort frameConnects[] = { 0, 1, 1, 3, 3, 2, 2, 0 };
UniformFloat uniformColor("u_color", Color4f(this->lineColor()));
@ -400,20 +400,20 @@ void CategoryLegend::renderLegendImmediateMode(OpenGLContext* oglContext, Overla
float* v4 = &vertexArray[12];
// Constant coordinates
v0[0] = v3[0] = layout->x0;
v1[0] = v4[0] = layout->x1;
v0[0] = v3[0] = layout->tickStartX;
v1[0] = v4[0] = layout->tickMidX;
// Render color bar as one colored quad per pixel
int legendHeightPixelCount = static_cast<int>(layout->legendRect.height());
int legendHeightPixelCount = static_cast<int>(layout->colorBarRect.height());
if (m_categoryMapper.notNull())
{
int iPx;
for (iPx = 0; iPx < legendHeightPixelCount; iPx++)
{
const Color3ub& clr = m_categoryMapper->mapToColor(m_categoryMapper->domainValue((iPx + 0.5) / legendHeightPixelCount));
float y0 = static_cast<float>(layout->legendRect.min().y() + iPx);
float y1 = static_cast<float>(layout->legendRect.min().y() + iPx + 1);
float y0 = static_cast<float>(layout->colorBarRect.min().y() + iPx);
float y1 = static_cast<float>(layout->colorBarRect.min().y() + iPx + 1);
// Dynamic coordinates for rectangle
v0[1] = v1[1] = y0;
@ -436,10 +436,10 @@ void CategoryLegend::renderLegendImmediateMode(OpenGLContext* oglContext, Overla
bool isRenderingFrame = true;
if (isRenderingFrame)
{
v0[0] = v2[0] = layout->legendRect.min().x() - 0.5f;
v1[0] = v3[0] = layout->legendRect.max().x() - 0.5f;
v0[1] = v1[1] = layout->legendRect.min().y() - 0.5f;
v2[1] = v3[1] = layout->legendRect.max().y() - 0.5f;
v0[0] = v2[0] = layout->colorBarRect.min().x() - 0.5f;
v1[0] = v3[0] = layout->colorBarRect.max().x() - 0.5f;
v0[1] = v1[1] = layout->colorBarRect.min().y() - 0.5f;
v2[1] = v3[1] = layout->colorBarRect.max().y() - 0.5f;
glColor3fv(this->textColor().ptr());
glBegin(GL_LINES);
@ -472,23 +472,29 @@ void CategoryLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
{
CVF_TIGHT_ASSERT(layout);
ref<Glyph> glyph = this->font()->getGlyph(L'A');
layout->charHeight = static_cast<float>(glyph->height());
ref<Glyph> glyph = this->font()->getGlyph(L'A');
layout->charHeight = static_cast<float>(glyph->height());
layout->lineSpacing = layout->charHeight*1.5f;
layout->margins = Vec2f(8.0f, 8.0f);
layout->margins = Vec2f(8.0f, 8.0f);
float legendWidth = 25.0f;
float legendHeight = static_cast<float>(layout->size.y()) - 2 * layout->margins.y() - static_cast<float>(this->titleStrings().size())*layout->lineSpacing - layout->lineSpacing;
layout->legendRect = Rectf(layout->margins.x(), layout->margins.y() + layout->charHeight / 2.0f, legendWidth, legendHeight);
float colorBarWidth = 25.0f;
float colorBarHeight = static_cast<float>(layout->size.y())
- 2 * layout->margins.y()
- static_cast<float>(this->titleStrings().size()) * layout->lineSpacing
- layout->lineSpacing;
layout->colorBarRect = Rectf(layout->margins.x(),
layout->margins.y() + layout->charHeight / 2.0f,
colorBarWidth,
colorBarHeight);
if (layout->legendRect.width() < 1 || layout->legendRect.height() < 1)
if (layout->colorBarRect.width() < 1 || layout->colorBarRect.height() < 1)
{
return;
}
layout->x0 = layout->margins.x();
layout->x1 = layout->margins.x() + layout->legendRect.width();
layout->tickX = layout->x1 + 5;
layout->tickStartX = layout->margins.x();
layout->tickMidX = layout->margins.x() + layout->colorBarRect.width();
layout->tickEndX = layout->tickMidX + 5;
}
//--------------------------------------------------------------------------------------------------

View File

@ -45,9 +45,9 @@ protected:
charHeight = 0.0f;
lineSpacing = 0.0f;
margins = cvf::Vec2f::ZERO;
tickX = 0.0f;
x0 = 0.0f;
x1 = 0.0f;
tickEndX = 0.0f;
tickStartX = 0.0f;
tickMidX = 0.0f;
position = pos;
size = setSize;
@ -56,10 +56,9 @@ protected:
float charHeight;
float lineSpacing;
cvf::Vec2f margins;
float tickX;
float x0, x1;
float tickStartX, tickMidX, tickEndX;
cvf::Rectf legendRect;
cvf::Rectf colorBarRect;
cvf::Vec2i position;
cvf::Vec2ui size;
@ -72,7 +71,7 @@ protected:
const cvf::Vec2ui& size,
bool software);
void setupTextDrawer(cvf::TextDrawer* textDrawer,
OverlayColorLegendLayoutInfo* layout,
const OverlayColorLegendLayoutInfo* layout,
float* maxLegendRightPos);
void renderLegendUsingShaders(cvf::OpenGLContext* oglContext,
OverlayColorLegendLayoutInfo* layout,

View File

@ -151,10 +151,10 @@ bool OverlayScalarMapperLegend::pick(int oglXCoord, int oglYCoord, const Vec2i&
layoutInfo(&layoutInViewPortCoords);
Vec2i legendBarOrigin = oglRect.min();
legendBarOrigin.x() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().x());
legendBarOrigin.y() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().y());
legendBarOrigin.x() += static_cast<uint>(layoutInViewPortCoords.colorBarRect.min().x());
legendBarOrigin.y() += static_cast<uint>(layoutInViewPortCoords.colorBarRect.min().y());
Recti legendBarRect = Recti(legendBarOrigin, static_cast<uint>(layoutInViewPortCoords.legendRect.width()), static_cast<uint>(layoutInViewPortCoords.legendRect.height()));
Recti legendBarRect = Recti(legendBarOrigin, static_cast<uint>(layoutInViewPortCoords.colorBarRect.width()), static_cast<uint>(layoutInViewPortCoords.colorBarRect.height()));
if ((oglXCoord > legendBarRect.min().x()) && (oglXCoord < legendBarRect.max().x()) &&
(oglYCoord > legendBarRect.min().y()) && (oglYCoord < legendBarRect.max().y()))
@ -220,7 +220,7 @@ void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const Ov
m_visibleTickLabels.clear();
const float textX = layout->tickX + 5;
const float textX = layout->tickEndX + 5;
const float overlapTolerance = 1.2f * layout->charHeight;
float lastVisibleTextY = 0.0;
@ -229,7 +229,7 @@ void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const Ov
size_t it;
for (it = 0; it < numTicks; it++)
{
float textY = static_cast<float>(layout->legendRect.min().y() + layout->tickPixelPos->get(it));
float textY = static_cast<float>(layout->colorBarRect.min().y() + layout->tickYPixelPos->get(it));
// Always draw first and last tick label. For all others, skip drawing if text ends up
// on top of the previous label.
@ -242,7 +242,7 @@ void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const Ov
}
// Make sure it does not overlap the last tick as well
float lastTickY = static_cast<float>(layout->legendRect.max().y() );
float lastTickY = static_cast<float>(layout->colorBarRect.max().y() );
if (cvf::Math::abs(textY - lastTickY) < overlapTolerance)
{
@ -327,8 +327,8 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
float* v4 = &vertexArray[12];
// Constant coordinates
v0[0] = v3[0] = layout->x0;
v1[0] = v4[0] = layout->x1;
v0[0] = v3[0] = layout->tickStartX;
v1[0] = v4[0] = layout->tickMidX;
// Connects
static const ushort trianglesConnects[] = { 0, 1, 4, 0, 4, 3 };
@ -348,15 +348,15 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
// Render color bar as one colored quad per pixel
int legendHeightPixelCount = static_cast<int>(layout->tickPixelPos->get(m_tickValues.size()-1) - layout->tickPixelPos->get(0) + 0.01);
int legendHeightPixelCount = static_cast<int>(layout->tickYPixelPos->get(m_tickValues.size()-1) - layout->tickYPixelPos->get(0) + 0.01);
if (m_scalarMapper.notNull())
{
int iPx;
for (iPx = 0; iPx < legendHeightPixelCount; iPx++)
{
const Color3ub& clr = m_scalarMapper->mapToColor(m_scalarMapper->domainValue((iPx+0.5)/legendHeightPixelCount));
float y0 = static_cast<float>(layout->legendRect.min().y() + iPx);
float y1 = static_cast<float>(layout->legendRect.min().y() + iPx + 1);
float y0 = static_cast<float>(layout->colorBarRect.min().y() + iPx);
float y1 = static_cast<float>(layout->colorBarRect.min().y() + iPx + 1);
// Dynamic coordinates for rectangle
v0[1] = v1[1] = y0;
@ -382,10 +382,10 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
bool isRenderingFrame = true;
if (isRenderingFrame)
{
v0[0] = v2[0] = layout->legendRect.min().x()-0.5f;
v1[0] = v3[0] = layout->legendRect.max().x()-0.5f;
v0[1] = v1[1] = layout->legendRect.min().y()-0.5f;
v2[1] = v3[1] = layout->legendRect.max().y()-0.5f;
v0[0] = v2[0] = layout->colorBarRect.min().x()-0.5f;
v1[0] = v3[0] = layout->colorBarRect.max().x()-0.5f;
v0[1] = v1[1] = layout->colorBarRect.min().y()-0.5f;
v2[1] = v3[1] = layout->colorBarRect.max().y()-0.5f;
static const ushort frameConnects[] = { 0, 1, 1, 3, 3, 2, 2, 0};
UniformFloat uniformColor("u_color", Color4f(this->lineColor()));
@ -404,11 +404,11 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
if (isRenderingTicks)
{
// Constant coordinates
v0[0] = layout->x0;
v1[0] = layout->x1 - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v2[0] = layout->x1;
v3[0] = layout->tickX - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v4[0] = layout->tickX;
v0[0] = layout->tickStartX;
v1[0] = layout->tickMidX - 0.5f*(layout->tickEndX - layout->tickMidX) - 0.5f;
v2[0] = layout->tickMidX;
v3[0] = layout->tickEndX - 0.5f*(layout->tickEndX - layout->tickMidX) - 0.5f;
v4[0] = layout->tickEndX;
static const ushort tickLinesWithLabel[] = { 0, 4 };
static const ushort tickLinesWoLabel[] = { 2, 3 };
@ -416,7 +416,7 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
size_t ic;
for (ic = 0; ic < m_tickValues.size(); ic++)
{
float y0 = static_cast<float>(layout->legendRect.min().y() + layout->tickPixelPos->get(ic) - 0.5f);
float y0 = static_cast<float>(layout->colorBarRect.min().y() + layout->tickYPixelPos->get(ic) - 0.5f);
// Dynamic coordinates for tickmarks-lines
v0[1] = v1[1] = v2[1] = v3[1] = v4[1] = y0;
@ -495,20 +495,20 @@ void OverlayScalarMapperLegend::renderLegendImmediateMode(OpenGLContext* oglCont
float* v4 = &vertexArray[12];
// Constant coordinates
v0[0] = v3[0] = layout->x0;
v1[0] = v4[0] = layout->x1;
v0[0] = v3[0] = layout->tickStartX;
v1[0] = v4[0] = layout->tickMidX;
// Render color bar as one colored quad per pixel
int legendHeightPixelCount = static_cast<int>(layout->tickPixelPos->get(m_tickValues.size() - 1) - layout->tickPixelPos->get(0) + 0.01);
int legendHeightPixelCount = static_cast<int>(layout->tickYPixelPos->get(m_tickValues.size() - 1) - layout->tickYPixelPos->get(0) + 0.01);
if (m_scalarMapper.notNull())
{
int iPx;
for (iPx = 0; iPx < legendHeightPixelCount; iPx++)
{
const Color3ub& clr = m_scalarMapper->mapToColor(m_scalarMapper->domainValue((iPx+0.5)/legendHeightPixelCount));
float y0 = static_cast<float>(layout->legendRect.min().y() + iPx);
float y1 = static_cast<float>(layout->legendRect.min().y() + iPx + 1);
float y0 = static_cast<float>(layout->colorBarRect.min().y() + iPx);
float y1 = static_cast<float>(layout->colorBarRect.min().y() + iPx + 1);
// Dynamic coordinates for rectangle
v0[1] = v1[1] = y0;
@ -531,10 +531,10 @@ void OverlayScalarMapperLegend::renderLegendImmediateMode(OpenGLContext* oglCont
bool isRenderingFrame = true;
if (isRenderingFrame)
{
v0[0] = v2[0] = layout->legendRect.min().x()-0.5f;
v1[0] = v3[0] = layout->legendRect.max().x()-0.5f;
v0[1] = v1[1] = layout->legendRect.min().y()-0.5f;
v2[1] = v3[1] = layout->legendRect.max().y()-0.5f;
v0[0] = v2[0] = layout->colorBarRect.min().x()-0.5f;
v1[0] = v3[0] = layout->colorBarRect.max().x()-0.5f;
v0[1] = v1[1] = layout->colorBarRect.min().y()-0.5f;
v2[1] = v3[1] = layout->colorBarRect.max().y()-0.5f;
glColor3fv(this->textColor().ptr());
glBegin(GL_LINES);
@ -556,16 +556,16 @@ void OverlayScalarMapperLegend::renderLegendImmediateMode(OpenGLContext* oglCont
if (isRenderingTicks)
{
// Constant coordinates
v0[0] = layout->x0;
v1[0] = layout->x1 - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v2[0] = layout->x1;
v3[0] = layout->tickX - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v4[0] = layout->tickX;
v0[0] = layout->tickStartX;
v1[0] = layout->tickMidX - 0.5f*(layout->tickEndX - layout->tickMidX) - 0.5f;
v2[0] = layout->tickMidX;
v3[0] = layout->tickEndX - 0.5f*(layout->tickEndX - layout->tickMidX) - 0.5f;
v4[0] = layout->tickEndX;
size_t ic;
for (ic = 0; ic < m_tickValues.size(); ic++)
{
float y0 = static_cast<float>(layout->legendRect.min().y() + layout->tickPixelPos->get(ic) - 0.5f);
float y0 = static_cast<float>(layout->colorBarRect.min().y() + layout->tickYPixelPos->get(ic) - 0.5f);
// Dynamic coordinates for tickmarks-lines
v0[1] = v1[1] = v2[1] = v3[1] = v4[1] = y0;
@ -608,22 +608,28 @@ void OverlayScalarMapperLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
layout->lineSpacing = layout->charHeight*1.5f;
layout->margins = Vec2f(8.0f, 8.0f);
float legendWidth = 25.0f;
float legendHeight = static_cast<float>(layout->size.y()) - 2*layout->margins.y() - static_cast<float>(this->titleStrings().size())*layout->lineSpacing - layout->lineSpacing;
layout->legendRect = Rectf(layout->margins.x(), layout->margins.y() + layout->charHeight/2.0f, legendWidth, legendHeight);
float colorBarWidth = 25.0f;
float colorBarHeight = static_cast<float>(layout->size.y())
- 2*layout->margins.y()
- static_cast<float>(this->titleStrings().size()) * layout->lineSpacing
- layout->lineSpacing;
layout->colorBarRect = Rectf(layout->margins.x(),
layout->margins.y() + layout->charHeight/2.0f,
colorBarWidth,
colorBarHeight);
if (layout->legendRect.width() < 1 || layout->legendRect.height() < 1)
if (layout->colorBarRect.width() < 1 || layout->colorBarRect.height() < 1)
{
return;
}
layout->x0 = layout->margins.x();
layout->x1 = layout->margins.x() + layout->legendRect.width();
layout->tickX = layout->x1 + 5;
layout->tickStartX = layout->margins.x();
layout->tickMidX = layout->margins.x() + layout->colorBarRect.width();
layout->tickEndX = layout->tickMidX + 5;
// Build array containing the pixel positions of all the ticks
size_t numTicks = m_tickValues.size();
layout->tickPixelPos = new DoubleArray(numTicks);
layout->tickYPixelPos = new DoubleArray(numTicks);
size_t i;
for (i = 0; i < numTicks; i++)
@ -634,11 +640,11 @@ void OverlayScalarMapperLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
t = Math::clamp(t, 0.0, 1.1);
if (i != numTicks -1)
{
layout->tickPixelPos->set(i, t*layout->legendRect.height());
layout->tickYPixelPos->set(i, t*layout->colorBarRect.height());
}
else
{
layout->tickPixelPos->set(i, layout->legendRect.height()); // Make sure we get a value at the top even if the scalarmapper range is zero
layout->tickYPixelPos->set(i, layout->colorBarRect.height()); // Make sure we get a value at the top even if the scalarmapper range is zero
}
}
}

View File

@ -102,12 +102,11 @@ protected:
float charHeight;
float lineSpacing;
Vec2f margins;
float tickX;
float x0, x1;
float tickStartX, tickMidX, tickEndX;
Rectf legendRect;
Rectf colorBarRect;
cvf::ref<DoubleArray> tickPixelPos;
cvf::ref<DoubleArray> tickYPixelPos;
Vec2i position;
Vec2ui size;