Refactored the way that margin is applied to the text buounding box to prevent growth of the bounding box each time Text::setPosition() is called.

This commit is contained in:
Robert Osfield 2018-04-06 20:05:26 +01:00
parent ba4624c437
commit 6bfca1582e
4 changed files with 139 additions and 148 deletions

View File

@ -282,9 +282,6 @@ protected:
virtual void computePositionsImplementation();
void computeBackdropBoundingBox();
void computeBoundingBoxMargin();
void computeColorGradients();
void computeColorGradientsOverall();
void computeColorGradientsPerCharacter();

View File

@ -337,6 +337,7 @@ protected:
osg::Vec3 _offset;
osg::Vec3 _normal;
osg::BoundingBox _textBB;
osg::BoundingBox _textBBWithMargin;
mutable osg::Matrix _matrix;

View File

@ -785,168 +785,158 @@ bool Text::computeAverageGlyphWidthAndHeight(float& avg_width, float& avg_height
void Text::computePositionsImplementation()
{
// TextBase::computePositionsImplementation(); computes basic positions and maps the _textBB to _textBBWithMargin
TextBase::computePositionsImplementation();
computeBackdropBoundingBox();
computeBoundingBoxMargin();
}
if (!_textBBWithMargin.valid()) return;
// This method adjusts the bounding box to account for the expanded area caused by the backdrop.
// This assumes that the bounding box has already been computed for the text without the backdrop.
void Text::computeBackdropBoundingBox()
{
if(_backdropType == NONE)
if (_drawMode & (BOUNDINGBOX | FILLEDBOUNDINGBOX))
{
return;
_textBBWithMargin.set(
_textBBWithMargin.xMin() - _textBBMargin,
_textBBWithMargin.yMin() - _textBBMargin,
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax() + _textBBMargin,
_textBBWithMargin.yMax() + _textBBMargin,
_textBBWithMargin.zMax()
);
}
float avg_width = 0.0f;
float avg_height = 0.0f;
bool is_valid_size;
// FIXME: OPTIMIZE: It is possible that this value has already been computed before
// from previous calls to this function. This might be worth optimizing.
is_valid_size = computeAverageGlyphWidthAndHeight(avg_width, avg_height);
// Finally, we have one more issue to deal with.
// Now that the text takes more space, we need
// to adjust the size of the bounding box.
if((!_textBB.valid() || !is_valid_size))
if (_backdropType != NONE)
{
return;
}
float avg_width = 0.0f;
float avg_height = 0.0f;
bool is_valid_size;
// Finally, we have one more issue to deal with.
// Now that the text takes more space, we need
// to adjust the size of the bounding box.
switch(_backdropType)
{
case DROP_SHADOW_BOTTOM_RIGHT:
// FIXME: OPTIMIZE: It is possible that this value has already been computed before
// from previous calls to this function. This might be worth optimizing.
is_valid_size = computeAverageGlyphWidthAndHeight(avg_width, avg_height);
// Finally, we have one more issue to deal with.
// Now that the text takes more space, we need
// to adjust the size of the bounding box.
if (!is_valid_size)
{
return;
}
// Finally, we have one more issue to deal with.
// Now that the text takes more space, we need
// to adjust the size of the bounding box.
switch(_backdropType)
{
case DROP_SHADOW_BOTTOM_RIGHT:
{
_textBB.set(
_textBB.xMin(),
_textBB.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax(),
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin(),
_textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMax(),
_textBBWithMargin.zMax()
);
break;
}
case DROP_SHADOW_CENTER_RIGHT:
case DROP_SHADOW_CENTER_RIGHT:
{
_textBB.set(
_textBB.xMin(),
_textBB.yMin(),
_textBB.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax(),
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin(),
_textBBWithMargin.yMin(),
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMax(),
_textBBWithMargin.zMax()
);
break;
}
case DROP_SHADOW_TOP_RIGHT:
case DROP_SHADOW_TOP_RIGHT:
{
_textBB.set(
_textBB.xMin(),
_textBB.yMin(),
_textBB.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin(),
_textBBWithMargin.yMin(),
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMax()
);
break;
}
case DROP_SHADOW_BOTTOM_CENTER:
case DROP_SHADOW_BOTTOM_CENTER:
{
_textBB.set(
_textBB.xMin(),
_textBB.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(),
_textBB.xMax(),
_textBB.yMax(),
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin(),
_textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax(),
_textBBWithMargin.yMax(),
_textBBWithMargin.zMax()
);
break;
}
case DROP_SHADOW_TOP_CENTER:
case DROP_SHADOW_TOP_CENTER:
{
_textBB.set(
_textBB.xMin(),
_textBB.yMin(),
_textBB.zMin(),
_textBB.xMax(),
_textBB.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin(),
_textBBWithMargin.yMin(),
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax(),
_textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMax()
);
break;
}
case DROP_SHADOW_BOTTOM_LEFT:
case DROP_SHADOW_BOTTOM_LEFT:
{
_textBB.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(),
_textBB.xMax(),
_textBB.yMax(),
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax(),
_textBBWithMargin.yMax(),
_textBBWithMargin.zMax()
);
break;
}
case DROP_SHADOW_CENTER_LEFT:
case DROP_SHADOW_CENTER_LEFT:
{
_textBB.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin(),
_textBB.zMin(),
_textBB.xMax(),
_textBB.yMax(),
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMin(),
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax(),
_textBBWithMargin.yMax(),
_textBBWithMargin.zMax()
); break;
}
case DROP_SHADOW_TOP_LEFT:
case DROP_SHADOW_TOP_LEFT:
{
_textBB.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin(),
_textBB.zMin(),
_textBB.xMax(),
_textBB.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMin(),
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax(),
_textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMax()
);
break;
}
case OUTLINE:
case OUTLINE:
{
_textBB.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax()
_textBBWithMargin.set(
_textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBBWithMargin.zMax()
);
break;
}
default: // error
default: // error
{
break;
}
}
}
// This method expands the bounding box to a settable margin when a bounding box drawing mode is active.
void Text::computeBoundingBoxMargin()
{
if(_drawMode & (BOUNDINGBOX | FILLEDBOUNDINGBOX)){
_textBB.set(
_textBB.xMin() - _textBBMargin,
_textBB.yMin() - _textBBMargin,
_textBB.zMin(),
_textBB.xMax() + _textBBMargin,
_textBB.yMax() + _textBBMargin,
_textBB.zMax()
);
}
}
}

View File

@ -383,7 +383,8 @@ void TextBase::setBoundingBoxMargin(float margin)
return;
_textBBMargin = margin;
computeGlyphRepresentation();
computePositions();
}
@ -394,16 +395,16 @@ osg::BoundingBox TextBase::computeBoundingBox() const
return bbox;
#endif
if (_textBB.valid())
if (_textBBWithMargin.valid())
{
bbox.expandBy(_textBB.corner(0)*_matrix);
bbox.expandBy(_textBB.corner(1)*_matrix);
bbox.expandBy(_textBB.corner(2)*_matrix);
bbox.expandBy(_textBB.corner(3)*_matrix);
bbox.expandBy(_textBB.corner(4)*_matrix);
bbox.expandBy(_textBB.corner(5)*_matrix);
bbox.expandBy(_textBB.corner(6)*_matrix);
bbox.expandBy(_textBB.corner(7)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(0)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(1)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(2)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(3)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(4)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(5)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(6)*_matrix);
bbox.expandBy(_textBBWithMargin.corner(7)*_matrix);
#if 0
if (!bbox.valid())
@ -444,6 +445,8 @@ osg::BoundingBox TextBase::computeBoundingBox() const
void TextBase::computePositions()
{
_textBBWithMargin = _textBB;
computePositionsImplementation();
osg::Matrix matrix;
@ -745,12 +748,12 @@ void TextBase::setupDecoration()
osg::Vec2 default_texcoord(-1.0, -1.0);
if ((_drawMode & FILLEDBOUNDINGBOX)!=0 && _textBB.valid())
if ((_drawMode & FILLEDBOUNDINGBOX)!=0 && _textBBWithMargin.valid())
{
osg::Vec3 c000(_textBB.xMin(),_textBB.yMin(),_textBB.zMin());
osg::Vec3 c100(_textBB.xMax(),_textBB.yMin(),_textBB.zMin());
osg::Vec3 c110(_textBB.xMax(),_textBB.yMax(),_textBB.zMin());
osg::Vec3 c010(_textBB.xMin(),_textBB.yMax(),_textBB.zMin());
osg::Vec3 c000(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c100(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c110(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c010(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
unsigned int base = _coords->size();
@ -779,14 +782,14 @@ void TextBase::setupDecoration()
primitives->dirty();
}
if ((_drawMode & BOUNDINGBOX)!=0 && _textBB.valid())
if ((_drawMode & BOUNDINGBOX)!=0 && _textBBWithMargin.valid())
{
if (_textBB.zMin()==_textBB.zMax())
if (_textBBWithMargin.zMin()==_textBBWithMargin.zMax())
{
osg::Vec3 c000(_textBB.xMin(),_textBB.yMin(),_textBB.zMin());
osg::Vec3 c100(_textBB.xMax(),_textBB.yMin(),_textBB.zMin());
osg::Vec3 c110(_textBB.xMax(),_textBB.yMax(),_textBB.zMin());
osg::Vec3 c010(_textBB.xMin(),_textBB.yMax(),_textBB.zMin());
osg::Vec3 c000(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c100(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c110(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c010(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
unsigned int base = _coords->size();
@ -815,15 +818,15 @@ void TextBase::setupDecoration()
}
else
{
osg::Vec3 c000(_textBB.xMin(),_textBB.yMin(),_textBB.zMin());
osg::Vec3 c100(_textBB.xMax(),_textBB.yMin(),_textBB.zMin());
osg::Vec3 c110(_textBB.xMax(),_textBB.yMax(),_textBB.zMin());
osg::Vec3 c010(_textBB.xMin(),_textBB.yMax(),_textBB.zMin());
osg::Vec3 c000(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c100(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c110(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c010(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c001(_textBB.xMin(),_textBB.yMin(),_textBB.zMax());
osg::Vec3 c101(_textBB.xMax(),_textBB.yMin(),_textBB.zMax());
osg::Vec3 c111(_textBB.xMax(),_textBB.yMax(),_textBB.zMax());
osg::Vec3 c011(_textBB.xMin(),_textBB.yMax(),_textBB.zMax());
osg::Vec3 c001(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMax());
osg::Vec3 c101(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMax());
osg::Vec3 c111(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMax());
osg::Vec3 c011(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMax());
unsigned int base = _coords->size();