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(); virtual void computePositionsImplementation();
void computeBackdropBoundingBox();
void computeBoundingBoxMargin();
void computeColorGradients(); void computeColorGradients();
void computeColorGradientsOverall(); void computeColorGradientsOverall();
void computeColorGradientsPerCharacter(); void computeColorGradientsPerCharacter();

View File

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

View File

@ -785,21 +785,25 @@ bool Text::computeAverageGlyphWidthAndHeight(float& avg_width, float& avg_height
void Text::computePositionsImplementation() void Text::computePositionsImplementation()
{ {
// TextBase::computePositionsImplementation(); computes basic positions and maps the _textBB to _textBBWithMargin
TextBase::computePositionsImplementation(); TextBase::computePositionsImplementation();
computeBackdropBoundingBox(); if (!_textBBWithMargin.valid()) return;
computeBoundingBoxMargin();
}
// This method adjusts the bounding box to account for the expanded area caused by the backdrop. if (_drawMode & (BOUNDINGBOX | FILLEDBOUNDINGBOX))
// This assumes that the bounding box has already been computed for the text without the backdrop.
void Text::computeBackdropBoundingBox()
{
if(_backdropType == NONE)
{ {
return; _textBBWithMargin.set(
_textBBWithMargin.xMin() - _textBBMargin,
_textBBWithMargin.yMin() - _textBBMargin,
_textBBWithMargin.zMin(),
_textBBWithMargin.xMax() + _textBBMargin,
_textBBWithMargin.yMax() + _textBBMargin,
_textBBWithMargin.zMax()
);
} }
if (_backdropType != NONE)
{
float avg_width = 0.0f; float avg_width = 0.0f;
float avg_height = 0.0f; float avg_height = 0.0f;
bool is_valid_size; bool is_valid_size;
@ -811,7 +815,7 @@ void Text::computeBackdropBoundingBox()
// Finally, we have one more issue to deal with. // Finally, we have one more issue to deal with.
// Now that the text takes more space, we need // Now that the text takes more space, we need
// to adjust the size of the bounding box. // to adjust the size of the bounding box.
if((!_textBB.valid() || !is_valid_size)) if (!is_valid_size)
{ {
return; return;
} }
@ -823,108 +827,108 @@ void Text::computeBackdropBoundingBox()
{ {
case DROP_SHADOW_BOTTOM_RIGHT: case DROP_SHADOW_BOTTOM_RIGHT:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin(), _textBBWithMargin.xMin(),
_textBB.yMin() - avg_height * _backdropVerticalOffset, _textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax(), _textBBWithMargin.yMax(),
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case DROP_SHADOW_CENTER_RIGHT: case DROP_SHADOW_CENTER_RIGHT:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin(), _textBBWithMargin.xMin(),
_textBB.yMin(), _textBBWithMargin.yMin(),
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax(), _textBBWithMargin.yMax(),
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case DROP_SHADOW_TOP_RIGHT: case DROP_SHADOW_TOP_RIGHT:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin(), _textBBWithMargin.xMin(),
_textBB.yMin(), _textBBWithMargin.yMin(),
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax() + avg_height * _backdropVerticalOffset, _textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case DROP_SHADOW_BOTTOM_CENTER: case DROP_SHADOW_BOTTOM_CENTER:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin(), _textBBWithMargin.xMin(),
_textBB.yMin() - avg_height * _backdropVerticalOffset, _textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax(), _textBBWithMargin.xMax(),
_textBB.yMax(), _textBBWithMargin.yMax(),
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case DROP_SHADOW_TOP_CENTER: case DROP_SHADOW_TOP_CENTER:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin(), _textBBWithMargin.xMin(),
_textBB.yMin(), _textBBWithMargin.yMin(),
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax(), _textBBWithMargin.xMax(),
_textBB.yMax() + avg_height * _backdropVerticalOffset, _textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case DROP_SHADOW_BOTTOM_LEFT: case DROP_SHADOW_BOTTOM_LEFT:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin() - avg_height * _backdropVerticalOffset, _textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax(), _textBBWithMargin.xMax(),
_textBB.yMax(), _textBBWithMargin.yMax(),
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case DROP_SHADOW_CENTER_LEFT: case DROP_SHADOW_CENTER_LEFT:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin(), _textBBWithMargin.yMin(),
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax(), _textBBWithMargin.xMax(),
_textBB.yMax(), _textBBWithMargin.yMax(),
_textBB.zMax() _textBBWithMargin.zMax()
); break; ); break;
} }
case DROP_SHADOW_TOP_LEFT: case DROP_SHADOW_TOP_LEFT:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin(), _textBBWithMargin.yMin(),
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax(), _textBBWithMargin.xMax(),
_textBB.yMax() + avg_height * _backdropVerticalOffset, _textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
case OUTLINE: case OUTLINE:
{ {
_textBB.set( _textBBWithMargin.set(
_textBB.xMin() - avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMin() - avg_width * _backdropHorizontalOffset,
_textBB.yMin() - avg_height * _backdropVerticalOffset, _textBBWithMargin.yMin() - avg_height * _backdropVerticalOffset,
_textBB.zMin(), _textBBWithMargin.zMin(),
_textBB.xMax() + avg_width * _backdropHorizontalOffset, _textBBWithMargin.xMax() + avg_width * _backdropHorizontalOffset,
_textBB.yMax() + avg_height * _backdropVerticalOffset, _textBBWithMargin.yMax() + avg_height * _backdropVerticalOffset,
_textBB.zMax() _textBBWithMargin.zMax()
); );
break; break;
} }
@ -933,20 +937,6 @@ void Text::computeBackdropBoundingBox()
break; 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; return;
_textBBMargin = margin; _textBBMargin = margin;
computeGlyphRepresentation();
computePositions();
} }
@ -394,16 +395,16 @@ osg::BoundingBox TextBase::computeBoundingBox() const
return bbox; return bbox;
#endif #endif
if (_textBB.valid()) if (_textBBWithMargin.valid())
{ {
bbox.expandBy(_textBB.corner(0)*_matrix); bbox.expandBy(_textBBWithMargin.corner(0)*_matrix);
bbox.expandBy(_textBB.corner(1)*_matrix); bbox.expandBy(_textBBWithMargin.corner(1)*_matrix);
bbox.expandBy(_textBB.corner(2)*_matrix); bbox.expandBy(_textBBWithMargin.corner(2)*_matrix);
bbox.expandBy(_textBB.corner(3)*_matrix); bbox.expandBy(_textBBWithMargin.corner(3)*_matrix);
bbox.expandBy(_textBB.corner(4)*_matrix); bbox.expandBy(_textBBWithMargin.corner(4)*_matrix);
bbox.expandBy(_textBB.corner(5)*_matrix); bbox.expandBy(_textBBWithMargin.corner(5)*_matrix);
bbox.expandBy(_textBB.corner(6)*_matrix); bbox.expandBy(_textBBWithMargin.corner(6)*_matrix);
bbox.expandBy(_textBB.corner(7)*_matrix); bbox.expandBy(_textBBWithMargin.corner(7)*_matrix);
#if 0 #if 0
if (!bbox.valid()) if (!bbox.valid())
@ -444,6 +445,8 @@ osg::BoundingBox TextBase::computeBoundingBox() const
void TextBase::computePositions() void TextBase::computePositions()
{ {
_textBBWithMargin = _textBB;
computePositionsImplementation(); computePositionsImplementation();
osg::Matrix matrix; osg::Matrix matrix;
@ -745,12 +748,12 @@ void TextBase::setupDecoration()
osg::Vec2 default_texcoord(-1.0, -1.0); 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 c000(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c100(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()); osg::Vec3 c100(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c110(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()); osg::Vec3 c110(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c010(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); osg::Vec3 c010(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
unsigned int base = _coords->size(); unsigned int base = _coords->size();
@ -779,14 +782,14 @@ void TextBase::setupDecoration()
primitives->dirty(); 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 c000(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c100(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()); osg::Vec3 c100(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c110(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()); osg::Vec3 c110(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c010(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); osg::Vec3 c010(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
unsigned int base = _coords->size(); unsigned int base = _coords->size();
@ -815,15 +818,15 @@ void TextBase::setupDecoration()
} }
else else
{ {
osg::Vec3 c000(_textBB.xMin(),_textBB.yMin(),_textBB.zMin()); osg::Vec3 c000(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c100(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()); osg::Vec3 c100(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMin());
osg::Vec3 c110(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()); osg::Vec3 c110(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c010(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); osg::Vec3 c010(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMin());
osg::Vec3 c001(_textBB.xMin(),_textBB.yMin(),_textBB.zMax()); osg::Vec3 c001(_textBBWithMargin.xMin(),_textBBWithMargin.yMin(),_textBBWithMargin.zMax());
osg::Vec3 c101(_textBB.xMax(),_textBB.yMin(),_textBB.zMax()); osg::Vec3 c101(_textBBWithMargin.xMax(),_textBBWithMargin.yMin(),_textBBWithMargin.zMax());
osg::Vec3 c111(_textBB.xMax(),_textBB.yMax(),_textBB.zMax()); osg::Vec3 c111(_textBBWithMargin.xMax(),_textBBWithMargin.yMax(),_textBBWithMargin.zMax());
osg::Vec3 c011(_textBB.xMin(),_textBB.yMax(),_textBB.zMax()); osg::Vec3 c011(_textBBWithMargin.xMin(),_textBBWithMargin.yMax(),_textBBWithMargin.zMax());
unsigned int base = _coords->size(); unsigned int base = _coords->size();