/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2010 Pelican Ventures, Inc.
 * 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 SEAMLESS_PATCHSET
#define SEAMLESS_PATCHSET 1

#include <osg/CopyOp>
#include <osg/Node>
#include <osg/Object>
#include <osg/PrimitiveSet>
#include <osg/ref_ptr>
#include <osg/Transform>

#include <osgEarth/Map>

#include "Patch"
#include "PatchGroup"
#include "SeamlessOptions"

namespace seamless
{
class PatchSet : public osg::Object
{
public:
    PatchSet();
    PatchSet(const osgEarth::Drivers::SeamlessOptions& options,
             PatchOptions* poptionsPrototype = 0);
    PatchSet(const PatchSet& rhs,
             const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
    META_Object(seamless, PatchSet);
    int getResolution() const { return _resolution; }
    void setPrecisionFactor(double factor) { _precisionFactor = factor; }
    double getPrecisionFactor() const { return _precisionFactor; }
    double calcPrecisionFactor(int pixelError, double horiz_fov_deg = 60.0,
                               int screenRes = 1280, int dpi = 90);
    void setPrecisionFactor(int pixelError, double horiz_fov_deg = 60.0,
                            int screenRes = 1280, int dpi = 90)
    {
        setPrecisionFactor(calcPrecisionFactor(pixelError, horiz_fov_deg,
                                               screenRes, dpi));
    }
    void setPatchOptionsPrototype(PatchOptions* proto)
    {
        _patchOptionsPrototype = proto;
    }
    PatchOptions* getPatchOptionsPrototype() const
    {
        return _patchOptionsPrototype;
    }
    void setMaxLevel(int maxLevel) { _maxLevel = maxLevel; }
    int getMaxLevel() const { return _maxLevel; }
    void setVerticalScale(float  scale) { _verticalScale = scale; }
    float getVerticalScale() const { return _verticalScale; }
    void setMap(const osgEarth::Map* map);
    const osgEarth::Map* getMap() const { return _map.get(); }
    osgEarth::MapFrame& getMapFrame() const { return *_mapf; }
    virtual osg::Node* createPatchGroup(const std::string& filename,
                                        PatchOptions* poptions);
    virtual osg::Transform* createPatch(const std::string& filename,
                                        PatchOptions* poptions);
    virtual osg::Node* createPatchSetGraph(const std::string& filename);
    virtual osg::Node* createChild(const PatchOptions* parentOptions, int childNum);
    friend class Patch;
    /** Get the index (into attribute array) for vertex.
        @param x x grid coordinate
        @param y y grid coordinate
     */
    unsigned short makeIndex(int x, int y)
    {
        return static_cast<unsigned short>(y * (_resolution + 1) + x);
    }
    /** Rotate grid indices 90 deg counter-clockwise.
     */
    unsigned short rotateIndex(unsigned short index);
protected:
    virtual ~PatchSet();
    double _precisionFactor;
    int _resolution;
    int _maxLevel;
    float _verticalScale;
    osg::ref_ptr<PatchOptions> _patchOptionsPrototype;
    // Shared primitive sets for all the geometry.
    void initPrimitiveSets();
    osg::ref_ptr<osg::DrawElementsUShort> makeBasicTrile(int delta);
    osg::ref_ptr<osg::DrawElementsUShort> makeSingleStrip(int delta);
    osg::ref_ptr<osg::DrawElementsUShort> makeDualStrip();
    osg::ref_ptr<osg::DrawElementsUShort> trilePset[2][4];
    osg::ref_ptr<osg::DrawElementsUShort> stripPset[4][4];
    osg::ref_ptr<const osgEarth::Map> _map;
    osgEarth::MapFrame* _mapf;
    osgEarth::Drivers::SeamlessOptions _options;
};
}
#endif
