]> git.mxchange.org Git - simgear.git/commitdiff
Canvas: do not write bounding box to property tree.
authorThomas Geymayer <tomgey@gmail.com>
Mon, 31 Mar 2014 11:24:33 +0000 (13:24 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Mon, 31 Mar 2014 11:28:03 +0000 (13:28 +0200)
 - Prevent writing to property tree in wrong thread.
 - Add Element::getBoundingBox and
   Element::getTightBoundingBox as uniform way to
   retrieve bounding boxes of all canvas elements.
 - Update path bounding boxe in update traversal.

simgear/canvas/Canvas.cxx
simgear/canvas/elements/CanvasElement.cxx
simgear/canvas/elements/CanvasElement.hxx
simgear/canvas/elements/CanvasImage.cxx
simgear/canvas/elements/CanvasPath.cxx
simgear/canvas/elements/CanvasText.cxx

index 5f13e28c8537eac8eaae13145afc9b2c19bba431..9873ba6d09c8b40fc88eef0a5a74efaef31286c8 100644 (file)
@@ -452,8 +452,7 @@ namespace canvas
   //----------------------------------------------------------------------------
   void Canvas::valueChanged(SGPropertyNode* node)
   {
-    if(    boost::starts_with(node->getNameString(), "status")
-        || node->getParent()->getNameString() == "bounding-box" )
+    if( boost::starts_with(node->getNameString(), "status") )
       return;
     _render_dirty = true;
 
index 373632fc4fbd4cbb79bcbbcd9bd3b482041046ea..bdbcf902572e68428725ae79c2539f2999b3b398 100644 (file)
@@ -189,6 +189,7 @@ namespace canvas
     // Trigger matrix update
     getMatrix();
 
+    // TODO limit bounding box to scissor
     if( _attributes_dirty & SCISSOR_COORDS )
     {
       if( _scissor && _scissor->_coord_reference != GLOBAL )
@@ -296,9 +297,11 @@ namespace canvas
     // Drawables have a bounding box...
     if( _drawable )
       return _drawable->getBound().contains(osg::Vec3f(local_pos, 0));
-    else
+    else if( _transform.valid() )
       // ... for other elements, i.e. groups only a bounding sphere is available
       return _transform->getBound().contains(osg::Vec3f(parent_pos, 0));
+    else
+      return false;
   }
 
 
@@ -534,22 +537,23 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  void Element::setBoundingBox(const osg::BoundingBox& bb)
+  osg::BoundingBox Element::getBoundingBox() const
   {
-    if( _bounding_box.empty() )
-    {
-      SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
-      _bounding_box.resize(4);
-      _bounding_box[0] = bb_node->getChild("min-x", 0, true);
-      _bounding_box[1] = bb_node->getChild("min-y", 0, true);
-      _bounding_box[2] = bb_node->getChild("max-x", 0, true);
-      _bounding_box[3] = bb_node->getChild("max-y", 0, true);
-    }
+    if( _drawable )
+      return _drawable->getBound();
+
+    osg::BoundingBox bb;
 
-    _bounding_box[0]->setFloatValue(bb._min.x());
-    _bounding_box[1]->setFloatValue(bb._min.y());
-    _bounding_box[2]->setFloatValue(bb._max.x());
-    _bounding_box[3]->setFloatValue(bb._max.y());
+    if( _transform.valid() )
+      bb.expandBy(_transform->getBound());
+
+    return bb;
+  }
+
+  //----------------------------------------------------------------------------
+  osg::BoundingBox Element::getTightBoundingBox() const
+  {
+    return getTransformedBounds(getMatrix());
   }
 
   //----------------------------------------------------------------------------
@@ -569,6 +573,9 @@ namespace canvas
   //----------------------------------------------------------------------------
   osg::Matrix Element::getMatrix() const
   {
+    if( !_transform )
+      return osg::Matrix::identity();
+
     if( !(_attributes_dirty & TRANSFORM) )
       return _transform->getMatrix();
 
index dc5e1bf7592985d125b588bc26470c5d4e770ead..63649a861f56caf35a5103a6b4a0c4a4ce31f2e3 100644 (file)
@@ -164,9 +164,16 @@ namespace canvas
       void setClipFrame(ReferenceFrame rf);
 
       /**
-       * Write the given bounding box to the property tree
+       * Get bounding box (may not be as tight as bounding box returned by
+       * #getTightBounds)
        */
-      void setBoundingBox(const osg::BoundingBox& bb);
+      osg::BoundingBox getBoundingBox() const;
+
+      /**
+       * Get tight bounding box (child points are transformed to elements
+       * coordinate space before calculating the bounding box).
+       */
+      osg::BoundingBox getTightBoundingBox() const;
 
       /**
        * Get bounding box with children/drawables transformed by passed matrix
@@ -227,9 +234,8 @@ namespace canvas
       osg::observer_ptr<osg::MatrixTransform> _transform;
       std::vector<TransformType>              _transform_types;
 
-      Style                             _style;
-      std::vector<SGPropertyNode_ptr>   _bounding_box;
-      RelativeScissor                  *_scissor;
+      Style             _style;
+      RelativeScissor  *_scissor;
 
       typedef std::vector<EventListener> Listener;
       typedef std::map<Event::Type, Listener> ListenerMap;
@@ -237,7 +243,7 @@ namespace canvas
       ListenerMap _listener;
 
       typedef std::map<std::string, StyleInfo> StyleSetters;
-      static StyleSetters       _style_setters;
+      static StyleSetters _style_setters;
 
       static void staticInit();
 
index 84d64573144dcd36652960968c3b66358673d645..28fd2f498e3466ca11d01f7a29a3cffcae418d76 100644 (file)
@@ -271,7 +271,6 @@ namespace canvas
       _vertices->dirty();
       _attributes_dirty &= ~DEST_SIZE;
       _geom->dirtyBound();
-      setBoundingBox(_geom->getBound());
     }
 
     if( _attributes_dirty & SRC_RECT )
index 27c847a7ef9b328cfaebe07f548099ebcee90a11..07d06babef88c9fb48ef71800c026c9820f90f99 100644 (file)
@@ -381,14 +381,11 @@ namespace canvas
         // vgPathBounds doesn't take stroke width into account
         float ext = 0.5 * _stroke_width;
 
-        osg::BoundingBox bb
+        return osg::BoundingBox
         (
           min[0] - ext,           min[1] - ext,           -0.1,
           min[0] + size[0] + ext, min[1] + size[1] + ext,  0.1
         );
-        _path_element->setBoundingBox(bb);
-
-        return bb;
       }
 
     private:
@@ -463,7 +460,12 @@ namespace canvas
         }
 
         if( _attributes_dirty & BOUNDING_BOX )
+        {
           dirtyBound();
+
+          // Recalculate bounding box now (prevent race condition)
+          getBound();
+        }
       }
 
       struct PathUpdateCallback:
index f73a6ce8ccc2d7d14f352fad1294e061e00dbcd5..eb399e169c2fa594882a4c9a383cca93a6d6891c 100644 (file)
@@ -179,18 +179,17 @@ namespace canvas
   osg::BoundingBox Text::TextOSG::computeBound() const
   {
     osg::BoundingBox bb = osgText::Text::computeBound();
-    if( !bb.valid() )
-      return bb;
 
 #if OSG_VERSION_LESS_THAN(3,1,0)
-    // TODO bounding box still doesn't seem always right (eg. with center
-    //      horizontal alignment not completely accurate)
-    bb._min.y() += _offset.y();
-    bb._max.y() += _offset.y();
+    if( bb.valid() )
+    {
+      // TODO bounding box still doesn't seem always right (eg. with center
+      //      horizontal alignment not completely accurate)
+      bb._min.y() += _offset.y();
+      bb._max.y() += _offset.y();
+    }
 #endif
 
-    _text_element->setBoundingBox(bb);
-
     return bb;
   }