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

#include <osgEarth/Common>
#include <osgEarth/Units>
#include <osg/Shape>

namespace osgEarth
{
    class Geoid;
    class GeoExtent;

    /** 
     * Reference information for vertical (height) information.
     */
    class OSGEARTH_EXPORT VerticalSpatialReference : public osg::Referenced
    {
    public:
        /**
         * Creates an V-SRS from an initialization string.
         */
        static VerticalSpatialReference* create( const std::string& init );

        /** Adds a new geoid to the VSRS registry. You can thereafter create a VSRS based
            on this geoid with the VSRS::create() method, passing in the name of the geoid. */
        static void registerGeoid( const Geoid* geoid );

    public:
        /**
         * Transform a height value (at the specified lat/long location) to another VSRS.
         * The output height value will be in "to_srs" units.
         */
        bool transform( 
            const VerticalSpatialReference* toVSRS, 
            double lat_deg, double lon_deg, double z,
            double& out_z ) const;

        /**
         * Returns true if transformation from this VSRS to the target VSRS is both
         * possible and necessary.
         */
        bool canTransform( const VerticalSpatialReference* toVSRS ) const;

        /** 
         * Returns true if transformation from one VRS to another is possible and necessary
         */
        static bool canTransform( 
            const VerticalSpatialReference* from,
            const VerticalSpatialReference* toVSRS );

        /**
         * Creates a heightfield containing the "zero" refernce values relative to the ellipsoid.
         * For a vanilla ellipsoidal VSRS, the HF will be all zeros. for an orthometric (geoid) VSRS,
         * it will contain the raw geoid offsets.
         */
        osg::HeightField* createReferenceHeightField(const GeoExtent& extent, int cols, int rows) const;

        /** Gets the readable name of this SRS. */
        const std::string& getName() const { return _name; }

        /** Gets the linear units of height values */
        const Units& getUnits() const { return _units; }

        /** Gets the string that was used to initialize this SRS */
        const std::string& getInitString() const { return _initString; }

        /** Tests this SRS for equivalence with another. */
        virtual bool isEquivalentTo( const VerticalSpatialReference* rhs ) const;
        
    public:
        /** Creates a geoid-based VSRS. */
        VerticalSpatialReference(
            const std::string& name,
            const std::string& initString,
            const Geoid* geoid );

        /** Creates a simple ellipsoidal VSRS. */
        VerticalSpatialReference( const Units& units );

        std::string _name;
        std::string _initString;
        osg::ref_ptr<const Geoid> _geoid;
        Units _units;

        typedef std::map<std::string, osg::ref_ptr<const Geoid> > GeoidRegistry;
        static GeoidRegistry* _geoidRegistry;
    };

#if 0
    /** Proxy class for automatic registration of geoids */
    template<class T>
    struct  GeoidRegisterProxy {
        GeoidRegisterProxy() {
            VerticalSpatialReference::registerGeoid( new T );
        }
    };
#endif

}

#endif // OSGEARTH_VERTICAL_SPATIAL_REFERENCE_H
