/* -*-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_FILTER_H
#define OSGEARTHFEATURES_FEATURE_FILTER_H 1

#include <osgEarthFeatures/Common>
#include <osgEarthFeatures/Feature>
#include <osgEarth/GeoData>
#include <osg/Matrix>
#include <list>

namespace osgEarth { namespace Features
{
    using namespace osgEarth;

    /**
     * Context within which a chain of filters is executed.
     */
    class OSGEARTHFEATURES_EXPORT FilterContext
    {
    public:
        FilterContext();

    public: // properties

        ///** spatial envelope that filters should honor when operating under this context */
        //optional<GeoExtent>& extent() {
        //    return _extent; }

        /** The spatial profile of the feature data in this context. */
        const FeatureProfile* profile() const {
            return _profile.get();
        }
        osg::ref_ptr<const FeatureProfile>& profile() {
            return _profile;
        }

        /** Whether to assume a geocentric coordinate system for geographic profiles. */
        bool isGeocentric() const {
            return _isGeocentric;
        }
        bool& isGeocentric() {
            return _isGeocentric; 
        }        

        // Multiplying by the reference frame takes a point from world coords into
        // local coords.
        const osg::Matrixd& referenceFrame() const {
            return _referenceFrame;
        }
        // Multiplying by the inverse reference frame takes a point from local
        // coords into world coords.
        const osg::Matrix& inverseReferenceFrame() const {
            return _inverseReferenceFrame;
        }

        void setReferenceFrame( const osg::Matrixd& in ) {
            _referenceFrame = in;
            _inverseReferenceFrame = osg::Matrixd::inverse( _referenceFrame );
        }      

    public:
        bool hasReferenceFrame() const {
            return !_referenceFrame.isIdentity();
        }

        osg::Vec3d toLocal( const osg::Vec3d& world ) const {
            return world * _referenceFrame; }

        osg::Vec3d toWorld( const osg::Vec3d& local ) const {
            return local * _inverseReferenceFrame; }

    protected:
        bool _isGeocentric;
        optional<GeoExtent> _extent;
        osg::ref_ptr<const FeatureProfile> _profile;
        osg::Matrixd _referenceFrame;
        osg::Matrixd _inverseReferenceFrame;
    };

    class OSGEARTHFEATURES_EXPORT FeatureFilter  : public osg::Referenced
    {
    public:
        virtual FilterContext push( FeatureList& input, const FilterContext& context ) =0;
    };

    typedef std::list< osg::ref_ptr<FeatureFilter> > FeatureFilterList;

} } // namespace osgEarth::Features

#endif // OSGEARTHFEATURES_FEATURE_FILTER_H

