/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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.
*/
// PickIntersectVisitor == Pick visitor - used for screen based picking
// based on osgUtil::IntersectVisitor BUT
// traversing into Projection Nodes using the modified projections.
// also supplies high level intersector for 'what is under a pixel in a sceneview'
// GWM Feb 2003.


#ifndef OSGUTIL_PICKINTERSECTVISITOR
#define OSGUTIL_PICKINTERSECTVISITOR 1

#include <osgUtil/IntersectVisitor>
#include <osgUtil/SceneView>

namespace osgUtil {

// PickIntersectVisitor simplifies picking - routines take x,y mouse pixel & detect hits
class OSGUTIL_EXPORT PickIntersectVisitor : public IntersectVisitor
{
public:
    PickIntersectVisitor()
    { 
        setNodeMaskOverride(0xffffffff); // need to make the visitor override the nodemask to visit invisible actions
    }
    ~PickIntersectVisitor() {  }
    HitList& getHits(osgUtil::SceneView *, int x, int y);
    HitList& PickIntersectVisitor::getIntersections(osg::Node *scene, osg::Vec3 nr, osg::Vec3 fr);
private:
    osg::ref_ptr<osg::LineSegment> _lineSegment;
    friend class osgUtil::IntersectVisitor;
};

// PickVisitor traverses whole scene and checks below all Projection nodes
class OSGUTIL_EXPORT PickVisitor : public osg::NodeVisitor
{
public:
    PickVisitor()
    { 
        xp=yp=0;    
        setNodeMaskOverride(0xffffffff); // need to make the visitor override the nodemask to visit invisible actions
        setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
    }
    ~PickVisitor() { }
    virtual void apply(osg::Projection& pr);
    osgUtil::IntersectVisitor::HitList& getHits(osg::Node *nd, const osg::Matrix &projm, const float x, const float y);
    osgUtil::IntersectVisitor::HitList& getHits(osgUtil::SceneView *, double x, double y);
    osgUtil::IntersectVisitor::HitList & getHits(osg::Node *nd, const osg::Vec3 near_point, const osg::Vec3 far_point);
    osgUtil::IntersectVisitor::HitList& getHits(void);
    inline void setxy(float xpt, float ypt) { xp=xpt; yp=ypt; }
    inline bool hits() { return _PIVsegHitList.size()>0;}
private:
    PickIntersectVisitor _piv;
    float xp, yp; // start point in viewport fraction coordiantes
    osgUtil::IntersectVisitor::HitList       _PIVsegHitList;
};
}// namespace osgUtil

#endif // match OSGUTIL_PICKINTERSECTVISITOR
