/*
 * Copyright Staffan Gimåker 2008-2009.
 *
 * ---
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 */

#ifndef PEEKABOT_CLIENT_OCCUPANCY_GRID_2D_PROXY_HH_INCLUDED
#define PEEKABOT_CLIENT_OCCUPANCY_GRID_2D_PROXY_HH_INCLUDED


#include <vector>

#include "ObjectProxy.hh"
#include "../../Vector.hh"


namespace peekabot
{
    namespace client
    {
        /**
         * \brief Class used to feed data to
         * OccupancyGrid2DProxyBase::set_cells().
         *
         * \remark This class is not thread safe.
         */
        class PEEKABOT_API OccupancySet2D
        {
            friend class OccupancyGrid2DProxyBase;

        public:
            /**
             * \brief Add a cell and its occupancy value to the set.
             *
             * \param x An x-coordinate in the cell to update.
             * \param y An y-coordinate in the cell to update.
             * \param belief The occupancy value of the cell, in the
             * range [0,1], where 1 means fully occupied.
             */
            void set_cell(float x, float y, float belief);

            void clear();

            std::size_t size() const;

            bool empty() const;

        private:
            std::vector<std::pair<Vector2f, float> > m_cells;
        };

        /**
         * \brief Base class for OccupancyGrid2DProxy.
         */
        class PEEKABOT_API OccupancyGrid2DProxyBase : public ObjectProxyBase
        {
        public:
            OccupancyGrid2DProxyBase();

            OccupancyGrid2DProxyBase(OccupancyGrid2DProxyBase &p);

            /**
             * \brief Set/update occupancy values.
             *
             * Note that cell \em coordinates are used, not cell indices.
             * Furthermore, the <0,0> cell is assumed to be centered on the
             * origin.
             */
            DelayedDispatch set_cells(const OccupancySet2D &cells);
        };


        /**
         * \brief Proxy class used to add and manipulate 2D occupancy grids.
         *
         * Cells for which there's no information are drawn as translucent,
         * other cells are drawn in a color linearly interpolated between the
         * set unoccupied color (for belief/occupancy = 0) to the set occupied 
         * color (belief/occupancy = 1).
         */
        class PEEKABOT_API OccupancyGrid2DProxy :
            public OccupancyGrid2DProxyBase
        {
        public:
            OccupancyGrid2DProxy();

            OccupancyGrid2DProxy(OccupancyGrid2DProxy &p);

            OccupancyGrid2DProxy &operator=(const OccupancyGrid2DProxy &p) throw();

            OccupancyGrid2DProxy &operator=(const OccupancyGrid2DProxyBase &p) throw();

            /**
             * \copydoc ScalableProxy::assign(const ObjectProxyBase&)
             */
            DelayedDispatch assign(const ObjectProxyBase &p);

            /**
             * \copydoc ObjectProxy::assign(PeekabotClient&,const std::string&)
             */
            DelayedDispatch assign(
                PeekabotClient &client,
                const std::string &path);

            /**
             * \copydoc 
             * ObjectProxy::assign(const ObjectProxyBase&,const std::string&)
             */
            DelayedDispatch assign(
                const ObjectProxyBase &parent, 
                const std::string &rel_path);

            /**
             * \brief Add a 2D occupancy grid to the scene.
             *
             * Adds a occupancy grid to the scene at the given path and assigns 
             * the proxy to point to it. If an object already exists at the 
             * given path the outcome is governed by \a conflict_policy.
             *
             * \param path The path where the object will be added, including 
             * the name of the object.
             * \param cell_size The length of the sides, in meters, of the 
             * (square) occupancy grid cells.
             * \param conflict_policy Determines how name conflicts are handled.
             */
            DelayedDispatch add(
                PeekabotClient &client,
                const std::string &path,
                float cell_size,
                float unoccupied_r, float unoccupied_g, float unoccupied_b,
                float occupied_r, float occupied_g, float occupied_b, 
                NameConflictPolicy conflict_policy = AUTO_ENUMERATE_ON_CONFLICT);

            /**
             * \brief Add a 2D occupancy grid to the scene.
             *
             * Adds a occupancy grid with the given name under the the object 
             * referred to by \a parent and assigns the proxy to point to it. 
             * If an object already exists at the given path the outcome is 
             * governed by \a conflict_policy.
             *
             * \param parent The parent under which the object will be added.
             * \param name The name to assign to the created object.
             * \param cell_size The length of the sides, in meters, of the 
             * (square) occupancy grid cells.
             * \param conflict_policy Determines how name conflicts are handled.
             */
            DelayedDispatch add(
                const ObjectProxyBase &parent, 
                const std::string &name,
                float cell_size,
                float unoccupied_r, float unoccupied_g, float unoccupied_b,
                float occupied_r, float occupied_g, float occupied_b, 
                NameConflictPolicy conflict_policy = AUTO_ENUMERATE_ON_CONFLICT);
        };
    }
}


#endif // PEEKABOT_CLIENT_OCCUPANCY_GRID_2D_PROXY_HH_INCLUDED
