/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. 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. 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/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#ifndef _GCTL_EDGE2D_H
#define _GCTL_EDGE2D_H

#include "triangle2d.h"

namespace gctl
{
    // Declaration of the basic edge type
    template <typename A, typename T = void> struct type_edge2d;

    typedef type_edge2d<void> edge2d; // edge type of attribute type of void

    /**
	 * @brief      Structure of a 2D edge under the Cartesian coordinates
	 * 
	 */
    template <typename A, typename T>
	struct type_edge2d : public entity<vertex2dc, 2, A>
	{
		type_triangle2d<T> *neigh[2]; ///< neighboring 2D triangles
        /**
         * Constructor
         */
		type_edge2d();
        /**
         * @brief      Set object from parameters
         *
         * @warning    This function will locate memories to store vertice
         *
         * @param[in]  p0     The first point
         * @param[in]  p1     The second point
         * @param[in]  index  The element index
         */
        type_edge2d(const point2dc &p0, const point2dc &p1, int index = 0);
        /**
         * @brief      Set vertice and element index
         *
         * @param      vert0  The vector 0
         * @param      vert1  The vector 1
         * @param[in]  index     The index
         */
        type_edge2d(vertex2dc &vert0, vertex2dc &vert1, int index = 0);
        /**
         * @brief      De-constructor
         */
        virtual ~type_edge2d(){}
        /**
         * @brief      Set object from parameters
         *
         * @warning    This function will locate memories to store vertice
         *
         * @param[in]  p0     The first point
         * @param[in]  p1     The second point
         * @param[in]  index  The element index
         */
        void set(const point2dc &p0, const point2dc &p1, int index = 0);
        /**
         * @brief      Set vertice and element index
         *
         * @param      vert0  The vector 0
         * @param      vert1  The vector 1
         * @param[in]  index     The index
         */
        void set(vertex2dc &vert0, vertex2dc &vert1, int index = 0);

        /**
         * @brief      Reset the structure to initial state
         */
        void reset();

        /**
         * @brief      Sets the neighbors
         *
         * @param      neigh_ptr0  The neighbor 0
         * @param      neigh_ptr1  The neighbor 1
         */
        void set_neighbor(type_triangle2d<T> &nei0, type_triangle2d<T> &nei1);
	};

    template <typename A, typename T>
    type_edge2d<A, T>::type_edge2d() : entity<vertex2dc, 2, A>::entity()
    {
        neigh[0] = neigh[1] = nullptr;
    }

    template <typename A, typename T>
    type_edge2d<A, T>::type_edge2d(const point2dc &p0, const point2dc &p1, int index) : type_edge2d()
    {
        set(p0, p1, index);
    }

    template <typename A, typename T>
    type_edge2d<A, T>::type_edge2d(vertex2dc &vert0, vertex2dc &vert1, int index) : type_edge2d()
    {
        set(vert0, vert1, index);
    }

    template <typename A, typename T>
    void type_edge2d<A, T>::set(const point2dc &p0, const point2dc &p1, int index)
    {
        if (index < 0)
        {
            throw out_of_range("Invalid index number, From type_edge2d::set(...)");
        }

        for (int i = 0; i < 2; ++i)
        {
            this->vert[i] = new vertex2dc;
        }
        this->self_host = true;

        this->id = index;
        this->vert[0]->set(p0, 2*index);
        this->vert[1]->set(p1, 2*index + 1);
        return;
    }

    template <typename A, typename T>
    void type_edge2d<A, T>::set(vertex2dc &vert0, vertex2dc &vert1, int index)
    {
        if (index < 0)
        {
            throw out_of_range("Invalid index number, From type_edge2d::set(...)");
        }

        this->id = index;
        if (vert0.id < vert1.id)
        {
            this->vert[0] = &vert0;
            this->vert[1] = &vert1;
        }
        else
        {
            this->vert[0] = &vert1;
            this->vert[1] = &vert0;
        }
        return;
    }

    template <typename A, typename T>
    void type_edge2d<A, T>::reset()
    {
        entity<vertex2dc, 2, A>::reset();
        neigh[0] = neigh[1] = nullptr;
        return;
    }

    template <typename A, typename T>
    void type_edge2d<A, T>::set_neighbor(type_triangle2d<T> &nei0, type_triangle2d<T> &nei1)
    {
        neigh[0] = &nei0;
        neigh[1] = &nei1;
        return;
    }
};

#endif // _GCTL_EDGE2D_H