/* -*-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_BUILD_GEOMETRY_FILTER_H
#define OSGEARTHFEATURES_BUILD_GEOMETRY_FILTER_H 1

#include <osgEarthFeatures/Common>
#include <osgEarthFeatures/Feature>
#include <osgEarthFeatures/Annotation>
#include <osgEarthFeatures/Filter>
#include <osgEarthSymbology/Style>
#include <osg/Geode>

namespace osgEarth { namespace Features 
{
    using namespace osgEarth;

    /**
     * Builds geometry from a stream of input features.
     */
    class OSGEARTHFEATURES_EXPORT BuildGeometryFilter // : public Filter
    {
    public:
        BuildGeometryFilter();

        /** The style to apply to feature geometry */
        const Symbology::Style* getStyle() { return _style.get(); }
        void setStyle(const Symbology::Style* s) { _style = s; }

        /** Override and change the geometry type of incoming features. */
        optional<Symbology::Geometry::Type>& geomTypeOverride() { return _geomTypeOverride; }

        /**
         * For geocentric data, sets the granularity of edges created by the filter. This
         * is the maximum angle (in degrees) between the vectors representing two geocentric
         * points comprising an edge. Since large polygons won't naturally conform to the
         * ellipsoid very well, this setting lets you specify a minimum edge size so that they
         * will break down and conform better.
         */
        optional<double>& maxGranularity() { return _maxAngle_deg; }
        const optional<double>& maxGranularity() const { return _maxAngle_deg; }

        /** Pushes a list of features through the filter. */
        FilterContext push( FeatureList& input, osg::ref_ptr<osg::Node>& output, const FilterContext& context );

    protected:
        osg::ref_ptr<osg::Geode> _geode;
        osg::ref_ptr<const Symbology::Style> _style;
        optional<Symbology::Geometry::Type> _geomTypeOverride;
        optional<double> _maxAngle_deg;
        bool _hasPoints;
        bool _hasLines;
        void reset();
        
        bool push( Feature* input, const FilterContext& context );
        bool pushRegularFeature( Feature* input, const FilterContext& context );
        bool pushTextAnnotation( TextAnnotation* input, const FilterContext& context );
    };


    OSGEARTHFEATURES_EXPORT osg::Geode*
    createVolume(Symbology::Geometry* geom,
                 double               offset,
                 double               height,
                 const FilterContext& context );

} } // namespace osgEarth::Features

#endif // OSGEARTHFEATURES_BUILD_GEOMETRY_FILTER_H
