/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library 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 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGUTIL_POLYTOPEINTERSECTOR
#define OSGUTIL_POLYTOPEINTERSECTOR 1

#include <osgUtil/IntersectionVisitor>

namespace osgUtil
{

/** Concrent class for implementing polytope intersections with the scene graph.
  * To be used in conjunction with IntersectionVisitor. */
class OSGUTIL_EXPORT PolytopeIntersector : public Intersector
{
    public:
    
        /** Construct a PolytopeIntersector using speified polytope in MODEL coordinates.*/
        PolytopeIntersector(const osg::Polytope& polytope);
        
        /** Construct a PolytopeIntersector using speified polytope in specified coordinate frame.*/
        PolytopeIntersector(CoordinateFrame cf, const osg::Polytope& polytope);
        
        /** Convinience constructor for supporting picking in WINDOW, or PROJECTION coorindates
          * In WINDOW coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.
          * In PROJECTION coordinates (clip space cube) creates a five sided polytope box that has a front face at -1 and sides around box xMin, yMin, xMax, yMax.
          * In VIEW and MODEL coordinates (clip space cube) creates a five sided polytope box that has a front face at 0.0 and sides around box xMin, yMin, xMax, yMax.*/
        PolytopeIntersector(CoordinateFrame cf, double xMin, double yMin, double xMax, double yMax);

        struct Intersection
        {
            Intersection() {}
        
            bool operator < (const Intersection& rhs) const
            {
                if (nodePath < rhs.nodePath) return true;
                if (rhs.nodePath < nodePath ) return false;
                return (drawable < rhs.drawable);
            }

            osg::NodePath                   nodePath;
            osg::ref_ptr<osg::Drawable>     drawable;
        };
        
        typedef std::set<Intersection> 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()); }

    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 !_intersections.empty(); }

    protected:

        PolytopeIntersector* _parent;

        osg::Polytope _polytope;
        
        Intersections _intersections;
    
};

}

#endif

