/* -*-c++-*- */ /* osgEarth - Geospatial SDK for OpenSceneGraph * Copyright 2020 Pelican Mapping * http://osgearth.org * * osgEarth is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see */ #ifndef OSGEARTH_PRIMITIVE_INTERSECTOR_H #define OSGEARTH_PRIMITIVE_INTERSECTOR_H 1 #include #include namespace osgEarth { namespace Util { /** Concrete class for implementing line intersections with the scene graph. * To be used in conjunction with IntersectionVisitor. */ class OSGEARTH_EXPORT PrimitiveIntersector : public osgUtil::Intersector { public: /** Convenience constructor for supporting picking in WINDOW, or PROJECTION coordinates * In WINDOW coordinates creates a start value of (x,y,0) and end value of (x,y,1). * In PROJECTION coordinates (clip space cube) creates a start value of (x,y,-1) and end value of (x,y,1).*/ PrimitiveIntersector(CoordinateFrame cf, double x, double y, double thickness); /** Constructor for initializing with full start and end vectors. */ PrimitiveIntersector(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& end, double thickness, bool overlayIgnore=false); struct OSGEARTH_EXPORT Intersection { Intersection(): ratio(-1.0), primitiveIndex(0) {} Intersection(const Intersection &rhs) { ratio = rhs.ratio; nodePath = rhs.nodePath; drawable = rhs.drawable; matrix = rhs.matrix; localIntersectionPoint = rhs.localIntersectionPoint; localIntersectionNormal = rhs.localIntersectionNormal; indexList = rhs.indexList; ratioList = rhs.ratioList; primitiveIndex = rhs.primitiveIndex; } bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; } typedef std::vector IndexList; typedef std::vector RatioList; double ratio; osg::NodePath nodePath; osg::ref_ptr drawable; osg::ref_ptr matrix; osg::Vec3d localIntersectionPoint; osg::Vec3d localIntersectionNormal; IndexList indexList; RatioList ratioList; unsigned int primitiveIndex; const osg::Vec3d& getLocalIntersectPoint() const { return localIntersectionPoint; } osg::Vec3d getWorldIntersectPoint() const { return matrix.valid() ? localIntersectionPoint * (*matrix) : localIntersectionPoint; } const osg::Vec3d& getLocalIntersectNormal() const { return localIntersectionNormal; } osg::Vec3d getWorldIntersectNormal() const { return matrix.valid() ? osg::Matrix::transform3x3(osg::Matrix::inverse(*matrix),localIntersectionNormal) : localIntersectionNormal; } }; typedef std::multiset Intersections; inline void insertIntersection(const Intersection& intersection) { getIntersections().insert(intersection); } inline Intersections& getIntersections() { return _parent ? _parent->_intersections : _intersections; } inline Intersection getFirstIntersection() { Intersections& intersections = getIntersections(); return intersections.empty() ? Intersection() : *(intersections.begin()); } inline void setStart(const osg::Vec3d& start) { _start = start; } inline const osg::Vec3d& getStart() const { return _start; } inline void setEnd(const osg::Vec3d& end) { _end = end; } inline const osg::Vec3d& getEnd() const { return _end; } void setThickness(double thickness); inline double getThickness() const { return _thicknessVal; } inline bool getOverlayIgnore() const { return _overlayIgnore; } public: virtual Intersector* clone(osgUtil::IntersectionVisitor& iv); virtual bool enter(const osg::Node& node); virtual void leave(); virtual void intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable); virtual void reset(); virtual bool containsIntersections() { return !getIntersections().empty(); } protected: // Internal constructor used for clone request PrimitiveIntersector(); bool intersects(const osg::BoundingSphere& bs); bool intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const osg::BoundingBox& bb); unsigned int findPrimitiveIndex(osg::Drawable* drawable, unsigned int index); PrimitiveIntersector* _parent; osg::Vec3d _start; osg::Vec3d _end; osg::Vec3d _thickness; double _thicknessVal; bool _overlayIgnore; Intersections _intersections; }; } } // namespace osgEarth #endif //OSGEARTH_PRIMITIVE_INTERSECTOR_H