From 6bfca1582efae9606e18883971c95eb734d70058 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 6 Apr 2018 20:05:26 +0100 Subject: [PATCH] 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. --- include/osgText/Text | 3 - include/osgText/TextBase | 1 + src/osgText/Text.cpp | 222 +++++++++++++++++++-------------------- src/osgText/TextBase.cpp | 61 ++++++----- 4 files changed, 139 insertions(+), 148 deletions(-) diff --git a/include/osgText/Text b/include/osgText/Text index b2ae9c695..5e13bbefc 100644 --- a/include/osgText/Text +++ b/include/osgText/Text @@ -282,9 +282,6 @@ protected: virtual void computePositionsImplementation(); - void computeBackdropBoundingBox(); - void computeBoundingBoxMargin(); - void computeColorGradients(); void computeColorGradientsOverall(); void computeColorGradientsPerCharacter(); diff --git a/include/osgText/TextBase b/include/osgText/TextBase index a674e3bd4..aa7d8d64b 100644 --- a/include/osgText/TextBase +++ b/include/osgText/TextBase @@ -337,6 +337,7 @@ protected: osg::Vec3 _offset; osg::Vec3 _normal; osg::BoundingBox _textBB; + osg::BoundingBox _textBBWithMargin; mutable osg::Matrix _matrix; diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 6a1f6e13c..8d45da517 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -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() - ); + } } } diff --git a/src/osgText/TextBase.cpp b/src/osgText/TextBase.cpp index afc49dc57..f8636fea7 100644 --- a/src/osgText/TextBase.cpp +++ b/src/osgText/TextBase.cpp @@ -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();