
bool computeIntersections(osgViewer::Viewer & viewer, float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff)
{
	if (!viewer.getCamera()) return false;

	const osg::NodePath shortPath(nodePath.begin(), nodePath.end() - 1);
    
	float local_x, local_y = 0.0;    
	const osg::Camera* camera = viewer.getCameraContainingPosition(x, y, local_x, local_y);
	if (!camera) camera = viewer.getCamera();
    
	osg::Matrix matrix = osg::computeLocalToWorld(shortPath) *  camera->getViewMatrix() * camera->getProjectionMatrix();

	double zNear = -1.0;
	double zFar = 1.0;
	if (camera->getViewport())
	{
		matrix.postMult(camera->getViewport()->computeWindowMatrix());
		zNear = 0.0;
		zFar = 1.0;
	}

	osg::Matrix inverse;
	inverse.invert(matrix);

	osg::Vec3d startVertex = osg::Vec3d(local_x,local_y,zNear) * inverse;
	osg::Vec3d endVertex = osg::Vec3d(local_x,local_y,zFar) * inverse;
    
	osg::ref_ptr< osgUtil::LineSegmentIntersector > picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::MODEL, startVertex, endVertex);
    
	osgUtil::IntersectionVisitor iv(picker.get());
	iv.setTraversalMask(traversalMask);
	nodePath.back()->accept(iv);

	if (picker->containsIntersections())
	{
		intersections = picker->getIntersections();
		return true;
	}
	else
	{
		intersections.clear();
		return false;
	}
}

}
