/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2010 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 OSGEARTHFEATURES_FEATURE_GRIDDER_H
#define OSGEARTHFEATURES_FEATURE_GRIDDER_H 1

#include <osgEarthFeatures/Common>
#include <osgEarthFeatures/Feature>
#include <osgEarth/GeoData>


namespace osgEarth { namespace Features
{
    using namespace osgEarth;

    /**
     * Defines how to grid and crop feature data when creating a model.
     */
    class OSGEARTHFEATURES_EXPORT GriddingPolicy
    {
    public:
        enum CullingTechnique
        {
            /* crop a feature's shape to the oundaries of the grid cell;
               default for lines */
            CULL_BY_CROPPING,   

            /* include a feature in the cell only if its centroid is in the cell;
               default for polygons and points */
            CULL_BY_CENTROID
        };

    public: 
        GriddingPolicy( const Config& conf =Config() );

        bool enabled() const { return _cellSize.isSet() && *_cellSize > 0.0; }

    public:
        virtual Config getConfig() const;
        virtual void fromConfig( const Config& conf );

    public: // properties

        /** The maximum size (width and height) of each square grid cell. */
        optional<double>& cellSize() { return _cellSize; }
        const optional<double>& cellSize() const { return _cellSize; }

        /** The technique to use to figure out how to include a feature in a cell. */
        optional<CullingTechnique>& cullingTechnique() { return _cullingTechnique; }
        const optional<CullingTechnique>& cullingTechnique() const { return _cullingTechnique; }

        /** Whether to run the optimizer's spatialize-groups pass on the gridded geometry */
        optional<bool>& spatializeGroups() { return _spatializeGroups; }
        const optional<bool>& spatializeGroups() const { return _spatializeGroups; }

        /** Whether to install cluster cullers on the grid cell nodes */
        optional<bool>& clusterCulling() { return _clusterCulling; }
        const optional<bool>& clusterCulling() const { return _clusterCulling; }

    protected:
        optional<double> _cellSize;
        optional<CullingTechnique> _cullingTechnique;
        optional<bool> _spatializeGroups;
        optional<bool> _clusterCulling;
    };

    /**
     * Given a list of features, this will grid them up and return one
     * grid cell at a time. For each grid cell, it will crop the feature
     * geometry to the cell boundary.
     */
    class OSGEARTHFEATURES_EXPORT FeatureGridder : public osg::Referenced
    {
    public:
        static bool isSupported();

    public:
        FeatureGridder( const Bounds& bounds, const GriddingPolicy& policy );

        int getNumCells() const;

        bool getCellBounds( int i, Bounds& output ) const;

        bool cullFeatureListToCell( int i, FeatureList& features ) const;

    public:
        virtual ~FeatureGridder();

    protected:
        Bounds _inputBounds;
        GriddingPolicy _policy;
        int _cellsX, _cellsY;
        std::list<void*> _geosGeoms;
    };

} } // namespace osgEarth::Features

#endif //OSGEARTHFEATURES_FEATURE_GRIDDER_H
