/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2012 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 <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTH_ELEVATION_QUERY_H
#define OSGEARTH_ELEVATION_QUERY_H 1

#include <osgEarth/MapFrame>
#include <osgEarth/Containers>

namespace osgEarth
{
    /**
     * ElevationQuery (EQ) lets you query the elevation at any point on a map.
     * 
     * Rather than intersecting with a loaded scene graph, EQ uses the osgEarth
     * engine to directly access the best terrain tile for elevation query. You
     * give it the DEM resolution at which you want an elevation point, and it will
     * access the necessary tile and sample it.
     *
     * EQ supports two types of sampling:
     *
     * PARAMTERIC - EQ will sample the actual heightfield directly. This method is the
     *   fastest since it does not require geometry intersection testing.
     *
     * GEOMETRIC - EQ will create a temporary tesselated terrain tile and do an 
     *   intersection test (using osgUtil::IntersectionVisitor). This method is slower
     *   but more visually correlated. (TODO: not really true anymore. This method
     *   will require a re-do in osgEarth 2.2 or 3.0)
     *
     * NOTE: EQ does NOT take into account rendering properties like vertical scale or
     * skirts. If you need a vertical scale, for example, simply scale the resulting
     * elevation value.
     */
    class OSGEARTH_EXPORT ElevationQuery
    {
    public:
        /**
         * Constructs a new elevation manager. If you are not using a MapNode,
         * use this constructor to perform elevation queries against a Map. If
         * you *do* have a MapNode, use the other CTOR that takes a MapNode.
         *
         * @param map
         *      Map against which to perform elevation queries.
         * @param technique
         *      Technique to use for elevation data sampling.
         */
        ElevationQuery( const Map* map );
        ElevationQuery( const MapFrame& mapFrame );

        /** dtor */
        virtual ~ElevationQuery() { }

        /**
         * Gets the terrain elevation at a point, given a terrain resolution.
         *
         * @param point
         *      Coordinates for which to query elevation.
         * @param out_elevation
         *      Stores the elevation result in this variable upon success.
         * @param desiredResolution
         *      Optimal resolution of elevation data to use for the query (if available).
         *      Pass in 0 (zero) to use the best available resolution.
         * @param out_resolution
         *      Resolution of the resulting elevation value (if the method returns true).
         * 
         * @return True if the query succeeded, false upon failure.
         */
        bool getElevation(
            const GeoPoint& point,
            double&         out_elevation,
            double          desiredResolution    =0.0,
            double*         out_actualResolution =0L );

        /** 
         * Gets elevations for a whole array of points, storing the result in the
         * "z" element. If "ignoreZ" is false, the new Z value will be offset by
         * the original Z value.
         */
        bool getElevations(
            std::vector<osg::Vec3d>& points,
            const SpatialReference*  pointsSRS,
            bool                     ignoreZ = true,
            double                   desiredResolution =0.0 );

        /**
         * Gets elevations for a whole array of points, storing the results in the
         * "out_elevations" vector.
         */
        bool getElevations(
            const std::vector<osg::Vec3d>& points,
            const SpatialReference*        pointsSRS,
            std::vector<double>&           out_elevations,
            double                         desiredResolution = 0.0 );

        /**
         * Sets the maximum cache size for elevation tiles.
         */
        void setMaxTilesToCache( int value );

        /**
         * Gets the maximum cache size for elevation tiles.
         */
        int getMaxTilesToCache() const;
        
        /**
        * Sets the maximum level override for elevation queries.
        * A value of -1 turns off the override.
        */
        void setMaxLevelOverride(int maxLevelOverride);

        /**
        * Gets the maximum level override for elevation queries.
        */
        int getMaxLevelOverride() const;

        /**
         * Gets the average time per query
         */
        double getAverageQueryTime() const { return _queries > 0.0 ? _totalTime/_queries : 0.0; }

        unsigned int getMaxLevel(double x, double y, const SpatialReference* srs, const Profile* profile ) const;

    private:
        MapFrame  _mapf;
        unsigned  _maxCacheSize;
        int       _tileSize;
        unsigned  _maxDataLevel;
        int       _maxLevelOverride;

        typedef LRUCache< TileKey, osg::ref_ptr<osg::HeightField> > TileCache;
        TileCache _tileCache;

        double _queries;
        double _totalTime;

    private:
        void postCTOR();
        void sync();

        bool getElevationImpl(
            const GeoPoint& point,
            double&         out_elevation,
            double          desiredResolution,
            double*         out_actualResolution =0L );
    };

} // namespace osgEarth

#endif // OSGEARTH_ELEVATION_QUERY_H
