PlaneIntersector 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGUTIL_PLANEINTERSECTOR
  14. #define OSGUTIL_PLANEINTERSECTOR 1
  15. #include <osgUtil/IntersectionVisitor>
  16. #include <osg/CoordinateSystemNode>
  17. namespace osgUtil
  18. {
  19. /** Concrete class for implementing polytope intersections with the scene graph.
  20. * To be used in conjunction with IntersectionVisitor. */
  21. class OSGUTIL_EXPORT PlaneIntersector : public Intersector
  22. {
  23. public:
  24. /** Construct a PolytopeIntersector using speified polytope in MODEL coordinates.*/
  25. PlaneIntersector(const osg::Plane& plane, const osg::Polytope& boundingPolytope=osg::Polytope());
  26. /** Construct a PolytopeIntersector using speified polytope in specified coordinate frame.*/
  27. PlaneIntersector(CoordinateFrame cf, const osg::Plane& plane, const osg::Polytope& boundingPolytope=osg::Polytope());
  28. struct Intersection
  29. {
  30. Intersection() {}
  31. bool operator < (const Intersection& rhs) const
  32. {
  33. if (polyline < rhs.polyline) return true;
  34. if (rhs.polyline < polyline) return false;
  35. if (nodePath < rhs.nodePath) return true;
  36. if (rhs.nodePath < nodePath ) return false;
  37. return (drawable < rhs.drawable);
  38. }
  39. typedef std::vector<osg::Vec3d> Polyline;
  40. typedef std::vector<double> Attributes;
  41. osg::NodePath nodePath;
  42. osg::ref_ptr<osg::RefMatrix> matrix;
  43. osg::ref_ptr<osg::Drawable> drawable;
  44. Polyline polyline;
  45. Attributes attributes;
  46. };
  47. typedef std::vector<Intersection> Intersections;
  48. inline void insertIntersection(const Intersection& intersection) { getIntersections().push_back(intersection); }
  49. inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; }
  50. void setRecordHeightsAsAttributes(bool flag) { _recordHeightsAsAttributes = flag; }
  51. bool getRecordHeightsAsAttributes() const { return _recordHeightsAsAttributes; }
  52. void setEllipsoidModel(osg::EllipsoidModel* em) { _em = em; }
  53. const osg::EllipsoidModel* getEllipsoidModel() const { return _em.get(); }
  54. public:
  55. virtual Intersector* clone(osgUtil::IntersectionVisitor& iv);
  56. virtual bool enter(const osg::Node& node);
  57. virtual void leave();
  58. virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable);
  59. virtual void reset();
  60. virtual bool containsIntersections() { return !getIntersections().empty(); }
  61. protected:
  62. PlaneIntersector* _parent;
  63. bool _recordHeightsAsAttributes;
  64. osg::ref_ptr<osg::EllipsoidModel> _em;
  65. osg::Plane _plane;
  66. osg::Polytope _polytope;
  67. Intersections _intersections;
  68. };
  69. }
  70. #endif