| 22 | // The idea is to compute a bounding box with a factor x of the first step we compute the bounding box |

| 23 | class RigComputeBoundingBoxCallback : public osg::Drawable::ComputeBoundingBoxCallback |

| 24 | { |

| 25 | public: |

| 26 | RigComputeBoundingBoxCallback(double factor = 2.0) : _computed(false), _factor(factor) {} |

| 27 | void reset() { _computed = false; } |

| 28 | virtual osg::BoundingBox computeBound(const osg::Drawable& drawable) const |

| 29 | { |

| 30 | const osgAnimation::RigGeometry& rig = dynamic_cast<const osgAnimation::RigGeometry&>(drawable); |

| 31 | |

| 32 | // if a valid inital bounding box is set we use it without asking more |

| 33 | if (rig.getInitialBound().valid()) |

| 34 | return rig.getInitialBound(); |

| 35 | |

| 36 | if (_computed) |

| 37 | return _boundingBox; |

| 38 | |

| 39 | // if the computing of bb is invalid (like no geometry inside) |

| 40 | // then dont tag the bounding box as computed |

| 41 | osg::BoundingBox bb = rig.computeBound(); |

| 42 | if (!bb.valid()) |

| 43 | return bb; |

| 44 | |

| 45 | |

| 46 | _boundingBox.expandBy(bb); |

| 47 | osg::Vec3 center = _boundingBox.center(); |

| 48 | osg::Vec3 vec = (_boundingBox._max-center)*_factor; |

| 49 | _boundingBox.expandBy(center + vec); |

| 50 | _boundingBox.expandBy(center - vec); |

| 51 | _computed = true; |

| 52 | // osg::notify(osg::NOTICE) << "build the bounding box for RigGeometry " << rig.getName() << " " << _boundingBox._min << " " << _boundingBox._max << std::endl; |

| 53 | return _boundingBox; |

| 54 | } |

| 55 | protected: |

| 56 | mutable bool _computed; |

| 57 | double _factor; |

| 58 | mutable osg::BoundingBox _boundingBox; |

| 59 | }; |

| 60 | |